mirror of
https://github.com/moodle/moodle.git
synced 2025-08-06 01:16:44 +02:00
MDL-62708 question: Add idnumbers to question and question category
This commit is contained in:
parent
6902f39141
commit
6189fda47f
24 changed files with 502 additions and 13 deletions
|
@ -75,6 +75,7 @@ class qtype_ddwtos_test extends question_testcase {
|
|||
$dd->stamp = make_unique_id_code();
|
||||
$dd->version = make_unique_id_code();
|
||||
$dd->hidden = 0;
|
||||
$dd->idnumber = null;
|
||||
$dd->timecreated = time();
|
||||
$dd->timemodified = time();
|
||||
$dd->createdby = $USER->id;
|
||||
|
@ -122,6 +123,7 @@ class qtype_ddwtos_test extends question_testcase {
|
|||
$expected = test_question_maker::make_question('ddwtos');
|
||||
$expected->stamp = $qdata->stamp;
|
||||
$expected->version = $qdata->version;
|
||||
$expected->idnumber = null;
|
||||
|
||||
$q = $this->qtype->make_question($qdata);
|
||||
|
||||
|
@ -247,6 +249,7 @@ class qtype_ddwtos_test extends question_testcase {
|
|||
$qdata = new stdClass();
|
||||
$qdata->id = 123;
|
||||
$qdata->contextid = \context_system::instance()->id;
|
||||
$qdata->idnumber = null;
|
||||
$qdata->qtype = 'ddwtos';
|
||||
$qdata->name = 'A drag-and-drop question';
|
||||
$qdata->questiontext = 'Put these in order: [[1]], [[2]], [[3]].';
|
||||
|
@ -304,6 +307,7 @@ class qtype_ddwtos_test extends question_testcase {
|
|||
<defaultgrade>3</defaultgrade>
|
||||
<penalty>0.3333333</penalty>
|
||||
<hidden>0</hidden>
|
||||
<idnumber></idnumber>
|
||||
<shuffleanswers>1</shuffleanswers>
|
||||
<correctfeedback format="moodle_auto_format">
|
||||
<text><![CDATA[<p>Your answer is correct.</p>]]></text>
|
||||
|
|
|
@ -198,6 +198,10 @@ abstract class question_edit_form extends question_wizard_form {
|
|||
$mform->setType('generalfeedback', PARAM_RAW);
|
||||
$mform->addHelpButton('generalfeedback', 'generalfeedback', 'question');
|
||||
|
||||
$mform->addElement('text', 'idnumber', get_string('idnumber', 'question'), 'maxlength="100" size="10"');
|
||||
$mform->addHelpButton('idnumber', 'idnumber', 'question');
|
||||
$mform->setType('idnumber', PARAM_RAW);
|
||||
|
||||
// Any questiontype specific fields.
|
||||
$this->definition_inner($mform);
|
||||
|
||||
|
@ -791,6 +795,8 @@ abstract class question_edit_form extends question_wizard_form {
|
|||
}
|
||||
|
||||
public function validation($fromform, $files) {
|
||||
global $DB;
|
||||
|
||||
$errors = parent::validation($fromform, $files);
|
||||
if (empty($fromform['makecopy']) && isset($this->question->id)
|
||||
&& ($this->question->formoptions->canedit ||
|
||||
|
@ -810,6 +816,30 @@ abstract class question_edit_form extends question_wizard_form {
|
|||
$errors['defaultmark'] = get_string('defaultmarkmustbepositive', 'question');
|
||||
}
|
||||
|
||||
// Can only have one idnumber per category.
|
||||
if (strpos($fromform['category'], ',') !== false) {
|
||||
list($category, $categorycontextid) = explode(',', $fromform['category']);
|
||||
} else {
|
||||
$category = $fromform['category'];
|
||||
}
|
||||
if (isset($fromform['idnumber']) && ((string) $fromform['idnumber'] !== '')) {
|
||||
if (empty($fromform['usecurrentcat']) && !empty($fromform['categorymoveto'])) {
|
||||
$categoryinfo = $fromform['categorymoveto'];
|
||||
} else {
|
||||
$categoryinfo = $fromform['category'];
|
||||
}
|
||||
list($categoryid, $notused) = explode(',', $categoryinfo);
|
||||
$conditions = 'category = ? AND idnumber = ?';
|
||||
$params = [$categoryid, $fromform['idnumber']];
|
||||
if (!empty($this->question->id)) {
|
||||
$conditions .= ' AND id <> ?';
|
||||
$params[] = $this->question->id;
|
||||
}
|
||||
if ($DB->record_exists_select('question', $conditions, $params)) {
|
||||
$errors['idnumber'] = get_string('idnumbertaken', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
return $errors;
|
||||
}
|
||||
|
||||
|
|
|
@ -79,6 +79,7 @@ class qtype_gapselect_test extends question_testcase {
|
|||
$gapselect->stamp = make_unique_id_code();
|
||||
$gapselect->version = make_unique_id_code();
|
||||
$gapselect->hidden = 0;
|
||||
$gapselect->idnumber = null;
|
||||
$gapselect->timecreated = time();
|
||||
$gapselect->timemodified = time();
|
||||
$gapselect->createdby = $USER->id;
|
||||
|
@ -243,6 +244,7 @@ class qtype_gapselect_test extends question_testcase {
|
|||
$qdata = new stdClass();
|
||||
$qdata->id = 123;
|
||||
$qdata->contextid = \context_system::instance()->id;
|
||||
$qdata->idnumber = null;
|
||||
$qdata->qtype = 'gapselect';
|
||||
$qdata->name = 'A select missing words question';
|
||||
$qdata->questiontext = 'Put these in order: [[1]], [[2]], [[3]].';
|
||||
|
@ -294,6 +296,7 @@ class qtype_gapselect_test extends question_testcase {
|
|||
<defaultgrade>3</defaultgrade>
|
||||
<penalty>0.3333333</penalty>
|
||||
<hidden>0</hidden>
|
||||
<idnumber></idnumber>
|
||||
<shuffleanswers>1</shuffleanswers>
|
||||
<correctfeedback format="moodle_auto_format">
|
||||
<text><![CDATA[<p>Your answer is correct.</p>]]></text>
|
||||
|
|
|
@ -68,6 +68,7 @@ class qtype_match_test extends advanced_testcase {
|
|||
$q->stamp = make_unique_id_code();
|
||||
$q->version = make_unique_id_code();
|
||||
$q->hidden = 0;
|
||||
$q->idnumber = null;
|
||||
$q->timecreated = time();
|
||||
$q->timemodified = time();
|
||||
$q->createdby = $USER->id;
|
||||
|
|
|
@ -58,6 +58,7 @@ class qtype_missing_test extends question_testcase {
|
|||
$questiondata->stamp = make_unique_id_code();
|
||||
$questiondata->version = make_unique_id_code();
|
||||
$questiondata->hidden = 0;
|
||||
$questiondata->idnumber = null;
|
||||
$questiondata->timecreated = 0;
|
||||
$questiondata->timemodified = 0;
|
||||
$questiondata->createdby = 0;
|
||||
|
|
|
@ -350,6 +350,19 @@ class question_type {
|
|||
$question->defaultmark = $form->defaultmark;
|
||||
}
|
||||
|
||||
if (isset($form->idnumber) && ((string) $form->idnumber !== '')) {
|
||||
// While this check already exists in the form validation, this is a backstop preventing unnecessary errors.
|
||||
if (strpos($form->category, ',') !== false) {
|
||||
list($category, $categorycontextid) = explode(',', $form->category);
|
||||
} else {
|
||||
$category = $form->category;
|
||||
}
|
||||
if (!$DB->record_exists('question',
|
||||
['idnumber' => $form->idnumber, 'category' => $category])) {
|
||||
$question->idnumber = $form->idnumber;
|
||||
}
|
||||
}
|
||||
|
||||
// If the question is new, create it.
|
||||
if (empty($question->id)) {
|
||||
// Set the unique code.
|
||||
|
@ -851,6 +864,7 @@ class question_type {
|
|||
$question->stamp = $questiondata->stamp;
|
||||
$question->version = $questiondata->version;
|
||||
$question->hidden = $questiondata->hidden;
|
||||
$question->idnumber = $questiondata->idnumber;
|
||||
$question->timecreated = $questiondata->timecreated;
|
||||
$question->timemodified = $questiondata->timemodified;
|
||||
$question->createdby = $questiondata->createdby;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue