Class: Studium::GUI::Libui::AskExamQuestion

Inherits:
Object
  • Object
show all
Defined in:
lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb

Overview

Studium::GUI::Libui::AskExamQuestion

Constant Summary collapse

TITLE =
#

TITLE

#
'Ask Exam Questions'
SLEEP_FOR_N_SECONDS =
#

SLEEP_FOR_N_SECONDS

#
3

Instance Method Summary collapse

Constructor Details

#initialize(run_already = true) ⇒ AskExamQuestion

#

initialize

#


36
37
38
39
40
41
# File 'lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb', line 36

def initialize(
    run_already = true
  )
  reset
  run if run_already
end

Instance Method Details

#cprubyObject

#

cpruby

#


145
146
147
148
149
150
151
# File 'lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb', line 145

def cpruby
  base_dir = ENV['USERS']
  base_dir = '/home' if base_dir.nil?
  path_to_setup_rb_file =
    "#{RUBY_SRC_DIR}roebe/lib/roebe/setup/setup.rb"
  copy(path_to_setup_rb_file, '.')
end

#create_skeletonObject

#

create_skeleton (create tag, skeleton tag)

#


181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb', line 181

def create_skeleton
  # ======================================================================= #
  # === @question_buffer
  # ======================================================================= #
  @question_buffer = text_view
  # ======================================================================= #
  # === @answer_buffer
  # ======================================================================= #
  @answer_buffer   = text_view
  create_the_buttons
  create_the_entries
  create_the_combo_box
  # ======================================================================= #
  # widget1, then widget2
  # ======================================================================= #
  ui_sync_connect(
    @combo_box_containing_all_exam_topics,
    @entry_for_the_exam_topic,
    @array_available_exam_topics
  )
end

#create_the_buttonsObject

#

create_the_buttons (buttons tag)

#


268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb', line 268

def create_the_buttons
  # ======================================================================= #
  # === Button ask a random exam question
  # ======================================================================= #
  @button_ask_a_random_exam_question = button('Ask a random exam question')
  @button_ask_a_random_exam_question.on_clicked {
    # ===================================================================== #
    # First obtain a random exam topic:
    # ===================================================================== #
    use_this_exam_topic = Studium.random_exam_topic? # => "Theoretische Informatik"
    use_this_exam_topic = Studium.find_corresponding_exam_topic(use_this_exam_topic)
    the_ask_exam_question_button_was_pressed(use_this_exam_topic)
  }
  # ======================================================================= #
  # === @button_ask_an_exam_question_from_the_currently_selected_exam_topic
  # ======================================================================= #
  @button_ask_an_exam_question_from_the_currently_selected_exam_topic = button(
    'Ask an exam question from the currently selected exam topic.'
  )
  @button_ask_an_exam_question_from_the_currently_selected_exam_topic.on_clicked {
    if current_exam_topic?.empty?
      # Add a safeguard here.
      @entry_for_the_exam_topic.set_text(Studium.random_exam_topic?.to_s)
    end
    the_ask_exam_question_button_was_pressed
  }
end

#create_the_combo_boxObject

#

create_the_combo_box

#


169
170
171
172
173
174
175
176
# File 'lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb', line 169

def create_the_combo_box
  # ======================================================================= #
  # === @combo_box_containing_all_exam_topics
  # ======================================================================= #
  @combo_box_containing_all_exam_topics = libui_combo_box(
    @array_available_exam_topics
  )
end

#create_the_entriesObject

#

create_the_entries

#


206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb', line 206

def create_the_entries
  # ======================================================================= #
  # === @entry_for_the_exam_topic
  # ======================================================================= #
  @entry_for_the_exam_topic = ui_search_entry # ui_entry
  # ======================================================================= #
  # === @entry_sleep_for_n_seconds
  #
  # This entry allows the user to specify the number of seconds
  # to sleep.
  # ======================================================================= #
  @entry_sleep_for_n_seconds = ui_entry(SLEEP_FOR_N_SECONDS.to_s)
end

#current_exam_topic?Boolean

#

current_exam_topic?

#

Returns:

  • (Boolean)


88
89
90
# File 'lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb', line 88

def current_exam_topic?
  @entry_for_the_exam_topic.text?.to_s
end

#n_seconds?Boolean Also known as: sleep_for_n_seconds?

#

n_seconds?

#

Returns:

  • (Boolean)


60
61
62
# File 'lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb', line 60

def n_seconds?
  @entry_sleep_for_n_seconds.text?.to_f
end

#resetObject

#

reset (reset tag)

#


46
47
48
49
50
51
52
53
54
55
# File 'lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb', line 46

def reset
  # ======================================================================= #
  # === @array_available_exam_topics
  # ======================================================================= #
  @array_available_exam_topics =
    ::Studium.available_exam_topics_in_short_form?.map(&:downcase).sort
  set_title_width_height(TITLE, '70%', '50%')
  e 'Starting with width of '+width?.to_s+
    ' px and a height of '+height?.to_s+' px.'    
end

#return_box_with_checkboxesObject

#

return_box_with_checkboxes

#


223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb', line 223

def return_box_with_checkboxes
  outer_vbox = padded_vbox
  vbox = padded_vbox
  vbox.add(
    text(
      'There are a total of '+Studium.n_exam_questions?.to_s+
      ' exam questions available.'
    )
  )
  # ======================================================================= #
  # === @checkbox_automatically_reveal_the_answer_after_a_delay
  # ======================================================================= #
  @checkbox_automatically_reveal_the_answer_after_a_delay = checkbox(
    "Automatically reveal the answer after a delay "\
    "of #{n_seconds?} seconds."
  )
  @checkbox_automatically_reveal_the_answer_after_a_delay.set_active
  vbox.add(@checkbox_automatically_reveal_the_answer_after_a_delay)
  outer_vbox.add(vbox)

  text_specify_the_delay = text('Specify the delay in n seconds:')
  lower_box = hbox
  lower_box.add(text_specify_the_delay)
  lower_box.maximal(@entry_sleep_for_n_seconds, 1)
  outer_vbox.add(lower_box)
  return outer_vbox
end

#reveal_the_answer(i = Studium.remove_html(@answer.to_s)) ⇒ Object

#

reveal_the_answer

#


67
68
69
70
71
# File 'lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb', line 67

def reveal_the_answer(
    i = Studium.remove_html(@answer.to_s)
  )
  @answer_buffer.set_text(i)
end

#reveal_this_answer_after_a_specified_delay(i = Studium.remove_html(@answer.to_s)) ⇒ Object

#

reveal_this_answer_after_a_specified_delay

#


76
77
78
79
80
81
82
83
# File 'lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb', line 76

def reveal_this_answer_after_a_specified_delay(i = Studium.remove_html(@answer.to_s))
  if @checkbox_automatically_reveal_the_answer_after_a_delay.is_active?
    Thread.new {
      sleep sleep_for_n_seconds?
      reveal_the_answer(i)
    }
  end
end

#rinstall2Object

#

rinstall2

#


156
157
158
159
160
161
162
163
164
# File 'lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb', line 156

def rinstall2
  cpruby
  system 'ruby setup.rb --quiet config'
  system 'ruby setup.rb --quiet setup'
  system 'ruby setup.rb --quiet install'
  delete_file 'setup.rb'
  delete_file 'InstalledFiles' if File.exist?('InstalledFiles')
  delete_file '.config'
end

#runObject

#

run (run tag)

#


95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb', line 95

def run
  create_skeleton
  main_window = padded_window(
    @title,
    @width.to_i,
    @height.to_i,
    0
  )
  outer_vbox = padded_vbox
  
  hbox = padded_hbox
  hbox.add(
    text('Exam topic: ')
  )
  hbox.add(@entry_for_the_exam_topic)
  hbox.minimal(@combo_box_containing_all_exam_topics)
  hbox.add(return_box_with_checkboxes)
  outer_vbox.add(hbox)
  outer_vbox.add(@question_buffer, 1)
  outer_vbox.add(@answer_buffer,   1)
  outer_vbox.add(text_view,   1) # This is the "guess answer" view.
  outer_vbox.minimal(hsep)
  # ======================================================================= #
  # === Button reveal the answer
  # ======================================================================= #
  button_reveal_the_answer = button('Reveal the answer')
  button_reveal_the_answer.on_clicked {
    reveal_the_answer
  }
  # ======================================================================= #
  # === Button solved
  # ======================================================================= #
  button_solved = button('Solved')
  button_solved.on_clicked {
    the_current_exam_question_is_now_solved
  }
  hbox1 = hbox(@button_ask_an_exam_question_from_the_currently_selected_exam_topic, @button_ask_a_random_exam_question)
  hbox2 = hbox(button_reveal_the_answer, button_solved) # Or padded hbox.

  outer_vbox.minimal(hbox1, 0)
  outer_vbox.minimal(libui_hsep)
  outer_vbox.minimal(hbox2, 1)

  main_window.child = outer_vbox
  main_window.intelligent_exit
end

#the_ask_exam_question_button_was_pressed(use_this_exam_topic = current_exam_topic? ) ⇒ Object

#

the_ask_exam_question_button_was_pressed

This is the method that is called when the main ask-question button has been pressed.

#


302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb', line 302

def the_ask_exam_question_button_was_pressed(
    use_this_exam_topic = current_exam_topic?
  )
  use_this_exam_topic = ::Studium.find_corresponding_exam_topic(use_this_exam_topic)
  # ======================================================================= #
  # Next, identify the file that we will use here:
  # ======================================================================= #
  path_to_the_exam_topics = Studium.exam_topics_directory?
  full_path = path_to_the_exam_topics+
              use_this_exam_topic
  @entry_for_the_exam_topic.set_text(use_this_exam_topic.to_s)
  if File.exist? full_path
    dataset = File.read(full_path)
    sanitized_dataset = Studium.filter_away_invalid_questions(dataset.split("\n"))
    available_questions = sanitized_dataset.reject {|line| line.include? ' []' }
    line = available_questions.sample
    @dataset = Studium::Exams::ExamQuestion.new(ARGV, :do_not_run_yet)
    @dataset.set_this_line(line)
    @dataset.parse_the_random_line_from_the_obtained_dataset

    @question = @dataset.question?.to_s.dup
    @answer   = @dataset.answer?.to_s.dup
    # ===================================================================== #
    # Note that @question_buffer is a Fiddle::Pointer that was set via
    # ui_text_view().
    # ===================================================================== #
    @question_buffer.set_text(
      Studium.remove_html(@question.to_s)
    )
    # ===================================================================== #
    # Clear the old buffer content on the answer-buffer, so that old
    # answers are not retained.
    # ===================================================================== #
    @answer_buffer.set_text('')
    @dataset.store_last_topic_into_file
    reveal_this_answer_after_a_specified_delay(Studium.remove_html(@answer.to_s))
  else
    e "No file exists at #{full_path}."
  end
end

#the_current_exam_question_is_now_solvedObject

#

the_current_exam_question_is_now_solved (solved tag)

This method will be run when the user clicks on the “solved” button.

#


256
257
258
259
260
261
262
263
# File 'lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb', line 256

def the_current_exam_question_is_now_solved
  _ = Studium::Exams::Solved.new { :do_not_run_yet }
  _.set_commandline
  _.run
  if is_on_roebe? and Object.const_defined?(:Roebe)
    rinstall2
  end
end