-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod_form.php
337 lines (291 loc) · 14.9 KB
/
mod_form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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
222
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
250
251
252
253
254
255
256
257
258
259
260
261
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
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
<?php
defined('MOODLE_INTERNAL') || die();
require_once $CFG->dirroot.'/course/moodleform_mod.php';
require_once $CFG->dirroot.'/question/editlib.php';
require_once $CFG->dirroot.'/question/category_class.php';
require_once "lib.php";
class mod_miquiz_mod_form extends moodleform_mod
{
protected $course = null;
public function __construct($current, $section, $cm, $course)
{
$this->course = $course;
parent::__construct($current, $section, $cm, $course);
}
public function definition()
{
global $CFG, $COURSE, $DB, $PAGE;
$mform =& $this->_form;
$mform->addElement('header', 'general', get_string('general', 'form'));
$mform->addElement('text', 'name', get_string('miquiz_create_name', 'miquiz'), array('size'=>'64'));
if (!empty($CFG->formatstringstriptags)) {
$mform->setType('name', PARAM_TEXT);
} else {
$mform->setType('name', PARAM_CLEANHTML);
}
$mform->addRule('name', null, 'required', null, 'client');
$mform->addRule('name', get_string('maximumchars', '', 50), 'maxlength', 50, 'client');
$mform->addElement('text', 'short_name', get_string('miquiz_create_short_name', 'miquiz'), array('size'=>'64'));
if (!empty($CFG->formatstringstriptags)) {
$mform->setType('short_name', PARAM_TEXT);
} else {
$mform->setType('short_name', PARAM_CLEANHTML);
}
$mform->addRule('short_name', null, 'required', null, 'client');
$mform->addRule('short_name', get_string('maximumchars', '', 10), 'maxlength', 10, 'client');
if ($this->_instance == '') {
$options = []; //use string keys as keys since conversion to numbers more complicated
$options[0] = get_string('miquiz_create_scoremode_0', 'miquiz');
$options[1] = get_string('miquiz_create_scoremode_1', 'miquiz');
$options[2] = get_string('miquiz_create_scoremode_2', 'miquiz');
$options[3] = get_string('miquiz_create_scoremode_3', 'miquiz');
$options[4] = get_string('miquiz_create_scoremode_4', 'miquiz');
$mform->addElement('select', 'scoremode', get_string('miquiz_create_scoremode', 'miquiz'), $options);
$mform->addHelpButton('scoremode', 'miquiz_create_scoremode', 'miquiz');
}
$mform->addElement('advcheckbox', 'statsonlyforfinishedgames', get_string('miquiz_create_statsonlyforfinishedgames', 'miquiz'));
$mform->addHelpButton('statsonlyforfinishedgames', 'miquiz_create_statsonlyforfinishedgames', 'miquiz');
$gameModes = [];
$gameModes[] = $mform->createElement('advcheckbox', 'game_mode_random_fight', get_string('miquiz_create_game_mode_random_fight', 'miquiz'));
$gameModes[] = $mform->createElement('advcheckbox', 'game_mode_picked_fight', get_string('miquiz_create_game_mode_picked_fight', 'miquiz'));
$gameModes[] = $mform->createElement('advcheckbox', 'game_mode_solo_fight', get_string('miquiz_create_game_mode_solo_fight', 'miquiz'));
$mform->addGroup($gameModes, 'game_modes', get_string('miquiz_create_game_modes', 'miquiz'), [''], false);
$mform->addHelpButton('game_modes', 'miquiz_create_game_modes', 'miquiz');
$mform->setDefault('game_mode_random_fight', '1');
$mform->addElement('date_time_selector', 'assesstimestart', get_string('miquiz_create_assesstimestart', 'miquiz'));
$mform->addElement('advcheckbox', 'has_training_phase', get_string('miquiz_create_activate_training_phase', 'miquiz'));
$mform->setDefault('has_training_phase', '1');
$mform->addHelpButton('has_training_phase', 'miquiz_create_activate_training_phase', 'miquiz');
$mform->addElement('date_time_selector', 'timeuntilproductive', get_string('miquiz_create_timeuntilproductive', 'miquiz'));
$mform->setDefault('timeuntilproductive', time() + 60 * 60 * 24);
$mform->disabledIf('timeuntilproductive', 'has_training_phase');
$mform->addElement('advcheckbox', 'show_always_in_production', get_string('miquiz_create_show_always_in_production', 'miquiz'));
$mform->addHelpButton('show_always_in_production', 'miquiz_create_show_always_in_production', 'miquiz');
$mform->addElement('date_time_selector', 'assesstimefinish', get_string('miquiz_create_assesstimefinish', 'miquiz'));
$mform->setDefault('assesstimefinish', time() + 60 * 60 * 24 * 2);
$this->standard_intro_elements(get_string('description', 'miquiz'));
$mform->addElement('header', 'modstandardelshdr', get_string("miquiz_create_questions", "miquiz").'<i class="icon fa fa-exclamation-circle text-danger fa-fw " aria-hidden="true" title="Required" aria-label="Required"></i>');
$mform->setExpanded('modstandardelshdr');
// https://www.examulator.com/er/4.1/tables/question_versions.html#Relationships
$PAGE->requires->css(new moodle_url('/mod/miquiz/static/css/questionchooser.css'));
$context = context_course::instance($COURSE->id);
$categories = $DB->get_records('question_categories', array('contextid' => $context->id));
// require_once($CFG->libdir . '/questionlib.php');
// list($context2, $course2, $cm2) = get_context_info_array($context->id);
// $contexts = new question_edit_contexts($context2);
// question_category_select_menu($contexts);
$questionchooser_categories = array();
foreach ($categories as $category) {
$query = 'select q.id, q.name , q.qtype, v.version
from {question} q
join {question_versions} v on q.id = v.questionid
join {question_bank_entries} e on e.id = v.questionbankentryid
where e.questioncategoryid = :category
and v.status = :status
order by e.id, v.version';
// TODO: get only latest version of questions that are not in draft
$questions = $DB->get_records_sql(
$query,
[
'category' => $category->id,
'status' => \core_question\local\bank\question_version_status::QUESTION_STATUS_READY
]
);
if (empty($questions)) {
continue;
}
$question_dtos = array();
foreach ($questions as $question) {
if ($question->qtype =='multichoice') {
array_push(
$question_dtos,
[
'question_id' => $question->id,
'question_name' => $question->name,
'question_version' => $question->version,
]
);
}
}
$questionchooser_categories[] = array(
"category_id" => $category->id,
"category_name" => $category->name,
"questions" => $question_dtos
);
}
$customel_rendered = $PAGE->get_renderer('mod_miquiz')->render_from_template(
'miquiz/questionchooser',
array(
"i18n_miquiz_create_questions_search" => get_string("miquiz_create_questions_search", "miquiz"),
"i18n_miquiz_create_questions_selected" => get_string("miquiz_create_questions_selected", "miquiz"),
'i18n_miquiz_create_questions_no_questions' => get_string('miquiz_create_questions_no_questions', 'miquiz'),
'i18n_miquiz_create_questions_create_questions' => get_string('miquiz_create_questions_create_questions', 'miquiz'),
"categories" => $questionchooser_categories,
'course_id' => $this->course->id,
'hasCategories' => count($questionchooser_categories) > 0,
)
);
$questionIds = $this->_instance === '' ? '' : implode(',', miquiz::getQuestionIdsForMiQuizId($this->_instance));
// $questionIds = '';
$fields = array(
$mform->createElement('hidden', 'questions', $questionIds),
$mform->createElement('html', $customel_rendered));
$mform->addGroup($fields, 'questiong', '', '', false);
$mform->setType('questions', PARAM_NOTAGS);
$this->standard_coursemodule_elements();
$this->add_action_buttons();
}
/**
* Enforce defaults here
*
* @param array $defaultvalues Form defaults
* @return void
**/
public function data_preprocessing(&$defaultvalues)
{
if (isset($defaultvalues['conditions'])) {
$conditions = unserialize($defaultvalues['conditions']);
$defaultvalues['timespent'] = $conditions->timespent;
$defaultvalues['completed'] = $conditions->completed;
$defaultvalues['gradebetterthan'] = $conditions->gradebetterthan;
}
// Set up the completion checkbox which is not part of standard data.
$defaultvalues['completiontimespentenabled'] =
!empty($defaultvalues['completiontimespent']) ? 1 : 0;
if ($this->current->instance) {
// Editing existing instance - copy existing files into draft area.
$draftitemid = file_get_submitted_draft_itemid('mediafile');
file_prepare_draft_area($draftitemid, $this->context->id, 'mod_lesson', 'mediafile', 0, array('subdirs'=>0, 'maxbytes' => $this->course->maxbytes, 'maxfiles' => 1));
$defaultvalues['mediafile'] = $draftitemid;
}
}
/**
* Enforce validation rules here
*
* @param object $data Post data to validate
* @return array
**/
public function validation($data, $files)
{
$errors = parent::validation($data, $files);
$this->local_validation($errors, $data, $files);
// Keep for documentation purpose
// $this->external_validation($errors, $data, $files);
return $errors;
}
private function local_validation(&$errors, $data, $files)
{
if ($data['assesstimestart'] >= $data['assesstimefinish']) {
$errors['assesstimefinish'] = get_string('miquiz_create_error_endbeforestart', 'miquiz');
}
if ($data['has_training_phase']) {
if ($data['timeuntilproductive'] >= $data['assesstimefinish']
|| $data['timeuntilproductive'] < $data['assesstimestart']
) {
$errors['timeuntilproductive'] = get_string('miquiz_create_error_betweenendstart', 'miquiz');
}
}
if (empty($data['game_mode_random_fight']) && empty($data['game_mode_picked_fight']) && empty($data['game_mode_solo_fight'])) {
$errors['game_modes'] = get_string('miquiz_create_error_game_modes', 'miquiz');
}
// Check open and close times are consistent.
if (isset($data['available'])) {
if ($data['available'] != 0 && $data['deadline'] != 0
&& $data['deadline'] < $data['available']
) {
$errors['deadline'] = get_string('closebeforeopen', 'lesson');
}
}
if (!empty($data['usepassword']) && empty($data['password'])) {
$errors['password'] = get_string('emptypassword', 'lesson');
}
if (empty($data['questions']) || count(explode(',', $data['questions'])) < 3) {
$errors['questiong'] = get_string('miquiz_create_questions_error', 'miquiz');
}
}
// Keep for documentation purpose (in case we need it for some other external validation rules)
// private function external_validation(&$errors, $data, $files)
// {
// global $DB;
// if ($this->current->instance) {
// $activities = $DB->get_records("miquiz", array("short_name" => $data["short_name"]));
// if (!empty($activities)) {
// $activity = array_pop($activities);
// }
// }
// # check categories in miquiz
// $categories = miquiz::api_get("api/categories");
// $exists_in_miquiz = false;
// foreach ($categories as $category) {
// if ($category["name"] === $data["short_name"]) {
// $exists_in_miquiz = !isset($activity) || strval($category['id']) !== $activity->miquizcategoryid;
// break;
// }
// }
// if ($exists_in_miquiz) {
// $errors['short_name'] = get_string('miquiz_create_error_unique', 'miquiz');
// }
// }
/**
* Display module-specific activity completion rules.
* Part of the API defined by moodleform_mod
*
* @return array Array of string IDs of added items, empty array if none
*/
public function add_completion_rules()
{
$mform = $this->_form;
$mform->addElement(
'checkbox',
'completionendreached',
get_string('completionendreached', 'lesson'),
get_string('completionendreached_desc', 'lesson')
);
// Enable this completion rule by default.
$mform->setDefault('completionendreached', 1);
$group = array();
$group[] =& $mform->createElement(
'checkbox',
'completiontimespentenabled',
'',
get_string('completiontimespent', 'lesson')
);
$group[] =& $mform->createElement('duration', 'completiontimespent', '', array('optional' => false));
$mform->addGroup($group, 'completiontimespentgroup', get_string('completiontimespentgroup', 'lesson'), array(' '), false);
$mform->disabledIf('completiontimespent[number]', 'completiontimespentenabled', 'notchecked');
$mform->disabledIf('completiontimespent[timeunit]', 'completiontimespentenabled', 'notchecked');
return array('completionendreached', 'completiontimespentgroup');
}
/**
* Called during validation. Indicates whether a module-specific completion rule is selected.
*
* @param array $data Input data (not yet validated)
* @return bool True if one or more rules is enabled, false if none are.
*/
public function completion_rule_enabled($data)
{
return !empty($data['completionendreached']) || $data['completiontimespent'] > 0;
}
/**
* Allows module to modify the data returned by form get_data().
* This method is also called in the bulk activity completion form.
*
* Only available on moodleform_mod.
*
* @param stdClass $data the form data to be modified.
*/
public function data_postprocessing($data)
{
parent::data_postprocessing($data);
// Turn off completion setting if the checkbox is not ticked.
if (!empty($data->completionunlocked)) {
$autocompletion = !empty($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC;
if (empty($data->completiontimespentenabled) || !$autocompletion) {
$data->completiontimespent = 0;
}
if (empty($data->completionendreached) || !$autocompletion) {
$data->completionendreached = 0;
}
}
}
}