MDL-25122 Quiz review page does not check and enforce separate groups mode.
[moodle.git] / question / type / randomsamatch / edit_randomsamatch_form.php
CommitLineData
aeb15530 1<?php
08224df1 2/**
3 * Defines the editing form for the randomsamatch question type.
4 *
5 * @copyright &copy; 2007 Jamie Pratt
6 * @author Jamie Pratt me@jamiep.org
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
830e47a8 8 * @package questionbank
9 * @subpackage questiontypes
08224df1 10 */
11
12/**
13 * randomsamatch editing form definition.
14 */
15class question_edit_randomsamatch_form extends question_edit_form {
16 /**
17 * Add question-type specific form fields.
18 *
19 * @param MoodleQuickForm $mform the form being built.
20 */
21 function definition_inner(&$mform) {
08224df1 22 $questionstoselect = array();
23 for ($i=2; $i<=QUESTION_NUMANS; $i++){
24 $questionstoselect[$i] = $i;
25 }
26
27 $mform->addElement('select', 'choose', get_string("randomsamatchnumber", "quiz"), $questionstoselect);
28 $mform->setType('feedback', PARAM_RAW);
29
30 $mform->addElement('hidden', 'fraction', 0);
d18e0fe6 31 $mform->setType('fraction', PARAM_RAW);
08224df1 32 }
33
fe6ce234 34 function data_preprocessing($question) {
08224df1 35 if (empty($question->name)) {
36 $question->name = get_string("randomsamatch", "quiz");
37 }
38
39 if (empty($question->questiontext)) {
fe6ce234 40 $question->questiontext = get_string("randomsamatchintro", "quiz");
08224df1 41 }
fe6ce234 42 return $question;
08224df1 43 }
44
45 function qtype() {
46 return 'randomsamatch';
47 }
48
a78890d5 49 function validation($data, $files) {
f34488b2 50 global $QTYPES, $DB;
fe93ba83 51 $errors = parent::validation($data, $files);
5e581dd4 52 if (isset($data->categorymoveto)) {
53 list($category) = explode(',', $data['categorymoveto']);
54 } else {
55 list($category) = explode(',', $data['category']);
c958ac93 56 }
c958ac93 57 $saquestions = $QTYPES['randomsamatch']->get_sa_candidates($category);
08224df1 58 $numberavailable = count($saquestions);
59 if ($saquestions === false){
7f389342 60 $a = new stdClass();
f34488b2 61 $a->catname = $DB->get_field('question_categories', 'name', array('id' => $category));
08224df1 62 $errors['choose'] = get_string('nosaincategory', 'qtype_randomsamatch', $a);
63
64 } elseif ($numberavailable < $data['choose']){
7f389342 65 $a = new stdClass();
f34488b2 66 $a->catname = $DB->get_field('question_categories', 'name', array('id' => $category));
08224df1 67 $a->nosaquestions = $numberavailable;
68 $errors['choose'] = get_string('notenoughsaincategory', 'qtype_randomsamatch', $a);
69 }
70 return $errors;
08224df1 71 }
08224df1 72}