Skip to content

Commit 13e7d07

Browse files
committed
Review chnage on form and upgarde
1 parent 794ee94 commit 13e7d07

File tree

2 files changed

+68
-132
lines changed

2 files changed

+68
-132
lines changed

db/upgrade.php

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -32,67 +32,8 @@ function xmldb_qtype_drawlines_upgrade($oldversion) {
3232
$dbman = $DB->get_manager();
3333

3434
if ($oldversion < 2024043000) {
35-
36-
// Define table qtype_drawlines_options to be created.
37-
$table = new xmldb_table('qtype_drawlines_options');
38-
39-
// Adding fields to table qtype_drawlines_options.
40-
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
41-
$table->add_field('questionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
42-
$table->add_field('grademethod', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, 'partial');
43-
$table->add_field('correctfeedback', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
44-
$table->add_field('correctfeedbackformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
45-
$table->add_field('partiallycorrectfeedback', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
46-
$table->add_field('partiallycorrectfeedbackformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
47-
$table->add_field('incorrectfeedback', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
48-
$table->add_field('incorrectfeedbackformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
49-
$table->add_field('shownumcorrect', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
50-
$table->add_field('showmisplaced', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0');
51-
52-
// Adding keys to table qtype_drawlines_options.
53-
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
54-
$table->add_key('questionid', XMLDB_KEY_FOREIGN, ['questionid'], 'question', ['id']);
55-
56-
// Conditionally launch create table for qtype_drawlines_options.
57-
if (!$dbman->table_exists($table)) {
58-
$dbman->create_table($table);
59-
}
60-
61-
// Drawlines savepoint reached.
62-
upgrade_plugin_savepoint(true, 2024043000, 'qtype', 'drawlines');
63-
}
64-
65-
if ($oldversion < 2024043001) {
66-
67-
// Define table qtype_drawlines_lines to be created.
68-
$table = new xmldb_table('qtype_drawlines_lines');
69-
70-
// Adding fields to table qtype_drawlines_lines.
71-
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
72-
$table->add_field('questionid', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
73-
$table->add_field('number', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0');
74-
$table->add_field('type', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
75-
$table->add_field('labelstart', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
76-
$table->add_field('labelmiddle', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
77-
$table->add_field('labelend', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
78-
$table->add_field('zonestart', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
79-
$table->add_field('zoneend', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
80-
81-
// Adding keys to table qtype_drawlines_lines.
82-
$table->add_key('primary', XMLDB_KEY_PRIMARY, ['id']);
83-
$table->add_key('questionid', XMLDB_KEY_FOREIGN, ['questionid'], 'question', ['id']);
84-
85-
// Adding indexes to table qtype_drawlines_lines.
86-
$table->add_index('questionid-number', XMLDB_INDEX_UNIQUE, ['questionid', 'number']);
87-
88-
// Conditionally launch create table for qtype_drawlines_lines.
89-
if (!$dbman->table_exists($table)) {
90-
$dbman->create_table($table);
91-
}
92-
93-
// Drawlines savepoint reached.
94-
upgrade_plugin_savepoint(true, 2024043001, 'qtype', 'drawlines');
35+
// Adding new fields to table qtype_drawlines_options.
36+
// Adding new fields to table qtype_drawlines_lines.
9537
}
96-
9738
return true;
9839
}

edit_drawlines_form.php

Lines changed: 66 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ protected function set_current_settings(): void {
6161
}
6262

6363
/**
64-
* Options shared by all file pickers in the form.
64+
* Options shared by all file pickers in the form for background image.
6565
*
66-
* @return array Array of filepicker options.
66+
* @return array Array of filepicker options for selecting the background image.
6767
*/
6868
public static function file_picker_options(): array {
6969
$filepickeroptions = [];
@@ -79,11 +79,8 @@ protected function definition_inner($mform): void {
7979

8080
$this->set_current_settings();
8181

82-
// Add hidden form elements, for choices.
83-
for ($number = 1; $number <= ($this->numberoflines * 2); $number++) {
84-
$mform->addElement('hidden', 'c' . $number);
85-
$mform->setType('c' . $number, PARAM_RAW);
86-
}
82+
$mform->addElement('advcheckbox', 'showmisplaced', get_string('showmisplaced', 'qtype_ddmarker'));
83+
$mform->setDefault('showmisplaced', $this->get_default_value('showmisplaced', 0));
8784

8885
$grademethodmenu = [
8986
'partial' => get_string('gradepartialcredit', 'qtype_' . $this->qtype()),
@@ -113,6 +110,65 @@ protected function definition_inner($mform): void {
113110
$this->add_interactive_settings(true, true);
114111
}
115112

113+
/**
114+
* Add a set of form fields, obtained from get_per_line_fields.
115+
*
116+
* @param MoodleQuickForm $mform the form being built.
117+
* @param string $label the label to use for each line.
118+
*/
119+
protected function add_per_line_fields(MoodleQuickForm $mform, string $label) {
120+
if (isset($this->question->lines)) {
121+
$repeatsatstart = count($this->question->lines);
122+
} else {
123+
$repeatsatstart = line::LINE_NUMBER_START;
124+
}
125+
$repeatedoptions = [];
126+
$this->repeat_elements($this->get_per_line_fields($mform, $label, $repeatedoptions),
127+
$repeatsatstart, $repeatedoptions,
128+
'numberoflines', 'addlines', line::LINE_NUMBER_ADD,
129+
get_string('addmoreblanks', 'qtype_drawlines', 'lines'), true);
130+
}
131+
132+
/**
133+
* Returns a line object with relevant input fields.
134+
*
135+
* @param MoodleQuickForm $mform
136+
* @param string $label
137+
* @param array $repeatedoptions
138+
* @return array
139+
*/
140+
protected function get_per_line_fields(MoodleQuickForm $mform, string $label, array &$repeatedoptions): array {
141+
142+
$repeated = [];
143+
144+
$repeated[] = $mform->createElement('header', 'linexheader', $label);
145+
146+
$repeated[] = $mform->createElement('select', 'type',
147+
get_string('type', 'qtype_' . $this->qtype()),
148+
array_merge(['choose' => get_string('choose')], line::get_line_types()));
149+
150+
$repeated[] = $mform->createElement('text', 'labelstart',
151+
get_string('labelstart', 'qtype_' . $this->qtype()), ['size' => 20]);
152+
$mform->setType('labelstart', PARAM_RAW_TRIMMED);
153+
154+
$repeated[] = $mform->createElement('text', 'labelmiddle',
155+
get_string('labelmiddle', 'qtype_' . $this->qtype()), ['size' => 20]);
156+
$mform->setType('labelmiddle', PARAM_RAW_TRIMMED);
157+
158+
$repeated[] = $mform->createElement('text', 'labelend',
159+
get_string('labelend', 'qtype_' . $this->qtype()), ['size' => 20]);
160+
$mform->setType('labelend', PARAM_RAW_TRIMMED);
161+
162+
$repeated[] = $mform->createElement('text', 'zonestart',
163+
get_string('zonestart', 'qtype_' . $this->qtype()), ['size' => 20]);
164+
$mform->setType('zonestart', PARAM_RAW_TRIMMED);
165+
166+
$repeated[] = $mform->createElement('text', 'zoneend',
167+
get_string('zoneend', 'qtype_' . $this->qtype()), ['size' => 20]);
168+
$mform->setType('zoneend', PARAM_RAW_TRIMMED);
169+
return $repeated;
170+
}
171+
116172
/**
117173
* Create the form elements required by one hint.
118174
*
@@ -230,7 +286,7 @@ public function validation($data, $files) {
230286
if ($bgimagesize === null) {
231287
$errors["bgimage"] = get_string('formerror_nobgimage', 'qtype_' . $this->qtype());
232288
}
233-
$validlines = 0;
289+
$hasbothcoordinates = false;
234290
// Validate whether the line type error needed to be displayed.
235291
for ($i = 0; $i < count($data["type"]); $i++) {
236292
// Validate line type.
@@ -252,11 +308,11 @@ public function validation($data, $files) {
252308
if (in_array($data["type"][$i], array_keys(line::get_line_types())) &&
253309
line::is_zone_coordinates_valid($data["zonestart"][$i]) &&
254310
line::is_zone_coordinates_valid($data['zoneend'][$i])) {
255-
$validlines = 1;
311+
$hasbothcoordinates = true;
256312
}
257313
}
258314
// There should be atleast one valid line.
259-
if (empty($errors) && $validlines == 0) {
315+
if (empty($errors) && $hasbothcoordinates == false) {
260316
$errors["type[0]"] = get_string('formerror_notype', 'qtype_' . $this->qtype(), 1);
261317
}
262318
return $errors;
@@ -287,65 +343,4 @@ public function get_image_size_in_draft_area($draftitemid) {
287343
}
288344
return null;
289345
}
290-
291-
/**
292-
* Add a set of form fields, obtained from get_per_line_fields.
293-
*
294-
* @param MoodleQuickForm $mform the form being built.
295-
* @param string $label the label to use for each line.
296-
*/
297-
protected function add_per_line_fields(MoodleQuickForm $mform, string $label) {
298-
if (isset($this->question->lines)) {
299-
$repeatsatstart = count($this->question->lines);
300-
} else {
301-
$repeatsatstart = line::LINE_NUMBER_START;
302-
}
303-
$repeatedoptions = [];
304-
$this->repeat_elements($this->get_per_line_fields($mform, $label, $repeatedoptions),
305-
$repeatsatstart, $repeatedoptions,
306-
'numberoflines', 'addlines', line::LINE_NUMBER_ADD,
307-
get_string('addmoreblanks', 'qtype_drawlines', 'lines'), true);
308-
}
309-
310-
/**
311-
* Returns a line object with relevant input fields.
312-
*
313-
* @param MoodleQuickForm $mform
314-
* @param string $label
315-
* @param array $repeatedoptions
316-
* @return array
317-
*/
318-
protected function get_per_line_fields(MoodleQuickForm $mform, string $label, array &$repeatedoptions): array {
319-
320-
$repeated = [];
321-
322-
$repeated[] = $mform->createElement('header', 'linexheader', $label);
323-
$mform->setType('linexheader', PARAM_RAW);
324-
325-
$repeated[] = $mform->createElement('select', 'type',
326-
get_string('type', 'qtype_' . $this->qtype()),
327-
array_merge(['choose' => get_string('choose')], line::get_line_types()));
328-
$mform->setType('type', PARAM_RAW);
329-
330-
$repeated[] = $mform->createElement('text', 'labelstart',
331-
get_string('labelstart', 'qtype_' . $this->qtype()), ['size' => 20]);
332-
$mform->setType('labelstart', PARAM_RAW_TRIMMED);
333-
334-
$repeated[] = $mform->createElement('text', 'labelmiddle',
335-
get_string('labelmiddle', 'qtype_' . $this->qtype()), ['size' => 20]);
336-
$mform->setType('labelmiddle', PARAM_RAW_TRIMMED);
337-
338-
$repeated[] = $mform->createElement('text', 'labelend',
339-
get_string('labelend', 'qtype_' . $this->qtype()), ['size' => 20]);
340-
$mform->setType('labelend', PARAM_RAW_TRIMMED);
341-
342-
$repeated[] = $mform->createElement('text', 'zonestart',
343-
get_string('zonestart', 'qtype_' . $this->qtype()), ['size' => 20]);
344-
$mform->setType('zonestart', PARAM_RAW_TRIMMED);
345-
346-
$repeated[] = $mform->createElement('text', 'zoneend',
347-
get_string('zoneend', 'qtype_' . $this->qtype()), ['size' => 20]);
348-
$mform->setType('zoneend', PARAM_RAW_TRIMMED);
349-
return $repeated;
350-
}
351346
}

0 commit comments

Comments
 (0)