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

#


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

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

Instance Method Details

#cprubyObject

#

cpruby

#


238
239
240
241
242
243
244
# File 'lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb', line 238

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)

#


303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb', line 303

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

#


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb', line 138

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

#


291
292
293
294
295
296
297
298
# File 'lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb', line 291

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

#


328
329
330
331
332
333
334
335
336
337
# File 'lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb', line 328

def create_the_entries
  # ======================================================================= #
  # === @entry_for_the_exam_topic
  # ======================================================================= #
  @entry_for_the_exam_topic = ui_search_entry # ui_entry
  # ======================================================================= #
  # === @entry_sleep_for_n_seconds
  # ======================================================================= #
  @entry_sleep_for_n_seconds = ui_entry(SLEEP_FOR_N_SECONDS.to_s)
end

#current_exam_topic?Boolean

#

current_exam_topic?

#

Returns:

  • (Boolean)


169
170
171
# File 'lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb', line 169

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)


64
65
66
# File 'lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb', line 64

def n_seconds?
  @entry_sleep_for_n_seconds.text?.to_f
end

#resetObject

#

reset (reset tag)

#


48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb', line 48

def reset
  # ======================================================================= #
  # === @array_available_exam_topics
  # ======================================================================= #
  @array_available_exam_topics =
    ::Studium.available_exam_topics_in_short_form?.map(&:downcase).sort
  set_title(TITLE)
  set_width('70%')
  set_height('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

#


262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb', line 262

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?.to_s+' 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)
  lower_hbox = hbox
  lower_hbox.add(ui_text('Specify the delay in n seconds:'))
  lower_hbox.maximal(@entry_sleep_for_n_seconds, 1)
  outer_vbox.add(lower_hbox)
  return outer_vbox
end

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

#

reveal_the_answer

#


71
72
73
74
75
# File 'lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb', line 71

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

#


80
81
82
83
84
85
86
87
# File 'lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb', line 80

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

#


249
250
251
252
253
254
255
256
257
# File 'lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb', line 249

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'
  delete_file '.config'
end

#runObject

#

run (run tag)

#


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb', line 176

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 = libui_hbox(@button_ask_an_exam_question_from_the_currently_selected_exam_topic, @button_ask_a_random_exam_question)
  hbox2 = libui_padded_hbox(button_reveal_the_answer, button_solved)
  
  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.

#


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
# File 'lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb', line 95

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('')
    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)

#


226
227
228
229
230
231
232
233
# File 'lib/studium/gui/libui/ask_exam_question/ask_exam_question.rb', line 226

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