Commit | Line | Data |
---|---|---|
aeb15530 | 1 | <?php |
fe6ce234 DC |
2 | // This file is part of Moodle - http://moodle.org/ |
3 | // | |
4 | // Moodle is free software: you can redistribute it and/or modify | |
5 | // it under the terms of the GNU General Public License as published by | |
6 | // the Free Software Foundation, either version 3 of the License, or | |
7 | // (at your option) any later version. | |
8 | // | |
9 | // Moodle is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | // | |
14 | // You should have received a copy of the GNU General Public License | |
15 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
16 | ||
271ffe3f | 17 | /** |
c9c989a0 | 18 | * Defines the editing form for the multiple choice question type. |
271ffe3f | 19 | * |
b04a4319 | 20 | * @package qtype |
c9c989a0 | 21 | * @subpackage multichoice |
b04a4319 TH |
22 | * @copyright 2007 Jamie Pratt |
23 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
271ffe3f | 24 | */ |
25 | ||
a17b297d TH |
26 | |
27 | defined('MOODLE_INTERNAL') || die(); | |
28 | ||
29 | ||
271ffe3f | 30 | /** |
c9c989a0 TH |
31 | * Multiple choice editing form definition. |
32 | * | |
b04a4319 TH |
33 | * @copyright 2007 Jamie Pratt |
34 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
271ffe3f | 35 | */ |
6e34cd29 | 36 | class qtype_multichoice_edit_form extends question_edit_form { |
271ffe3f | 37 | /** |
38 | * Add question-type specific form fields. | |
39 | * | |
40 | * @param object $mform the form being built. | |
41 | */ | |
c9c989a0 | 42 | protected function definition_inner($mform) { |
12039f2e TH |
43 | $menu = array( |
44 | get_string('answersingleno', 'qtype_multichoice'), | |
45 | get_string('answersingleyes', 'qtype_multichoice'), | |
46 | ); | |
47 | $mform->addElement('select', 'single', | |
48 | get_string('answerhowmany', 'qtype_multichoice'), $menu); | |
c6fc9988 | 49 | $mform->setDefault('single', 1); |
271ffe3f | 50 | |
12039f2e TH |
51 | $mform->addElement('advcheckbox', 'shuffleanswers', |
52 | get_string('shuffleanswers', 'qtype_multichoice'), null, null, array(0, 1)); | |
5fcefc97 | 53 | $mform->addHelpButton('shuffleanswers', 'shuffleanswers', 'qtype_multichoice'); |
c6fc9988 | 54 | $mform->setDefault('shuffleanswers', 1); |
271ffe3f | 55 | |
12039f2e TH |
56 | $mform->addElement('select', 'answernumbering', |
57 | get_string('answernumbering', 'qtype_multichoice'), | |
c9c989a0 | 58 | qtype_multichoice::get_numbering_styles()); |
3e0647ff | 59 | $mform->setDefault('answernumbering', 'abc'); |
60 | ||
2aef1fe5 | 61 | $this->add_per_answer_fields($mform, get_string('choiceno', 'qtype_multichoice', '{no}'), |
92111e8d | 62 | question_bank::fraction_options_full(), max(5, QUESTION_NUMANS_START)); |
271ffe3f | 63 | |
c9c989a0 TH |
64 | $this->add_combined_feedback_fields(true); |
65 | $mform->disabledIf('shownumcorrect', 'single', 'eq', 1); | |
271ffe3f | 66 | |
c9c989a0 | 67 | $this->add_interactive_settings(true, true); |
271ffe3f | 68 | } |
69 | ||
2a6c5c52 | 70 | protected function get_per_answer_fields($mform, $label, $gradeoptions, |
71 | &$repeatedoptions, &$answersoption) { | |
72 | $repeated = array(); | |
73 | $repeated[] = $mform->createElement('header', 'answerhdr', $label); | |
74 | $repeated[] = $mform->createElement('editor', 'answer', | |
75 | get_string('answer', 'question'), array('rows' => 1), $this->editoroptions); | |
76 | $repeated[] = $mform->createElement('select', 'fraction', | |
77 | get_string('grade'), $gradeoptions); | |
78 | $repeated[] = $mform->createElement('editor', 'feedback', | |
79 | get_string('feedback', 'question'), array('rows' => 1), $this->editoroptions); | |
80 | $repeatedoptions['answer']['type'] = PARAM_RAW; | |
81 | $repeatedoptions['fraction']['default'] = 0; | |
82 | $answersoption = 'answers'; | |
83 | return $repeated; | |
84 | } | |
85 | ||
c7df5006 | 86 | protected function data_preprocessing($question) { |
c9c989a0 TH |
87 | $question = parent::data_preprocessing($question); |
88 | $question = $this->data_preprocessing_answers($question, true); | |
89 | $question = $this->data_preprocessing_combined_feedback($question, true); | |
8b9dfc2b | 90 | $question = $this->data_preprocessing_hints($question, true, true); |
c9c989a0 TH |
91 | |
92 | if (!empty($question->options)) { | |
706d06e2 TH |
93 | $question->single = $question->options->single; |
94 | $question->shuffleanswers = $question->options->shuffleanswers; | |
95 | $question->answernumbering = $question->options->answernumbering; | |
271ffe3f | 96 | } |
271ffe3f | 97 | |
c9c989a0 | 98 | return $question; |
271ffe3f | 99 | } |
100 | ||
c9c989a0 | 101 | public function validation($data, $files) { |
fe93ba83 | 102 | $errors = parent::validation($data, $files); |
271ffe3f | 103 | $answers = $data['answer']; |
104 | $answercount = 0; | |
24f7485b | 105 | |
106 | $totalfraction = 0; | |
107 | $maxfraction = -1; | |
108 | ||
c9c989a0 | 109 | foreach ($answers as $key => $answer) { |
24f7485b | 110 | //check no of choices |
2a6c5c52 | 111 | $trimmedanswer = trim($answer['text']); |
5a6084cf | 112 | $fraction = (float) $data['fraction'][$key]; |
db05d595 | 113 | if ($trimmedanswer === '' && empty($fraction)) { |
c9c989a0 | 114 | continue; |
271ffe3f | 115 | } |
db05d595 | 116 | if ($trimmedanswer === '') { |
5a6084cf ARN |
117 | $errors['fraction['.$key.']'] = get_string('errgradesetanswerblank', 'qtype_multichoice'); |
118 | } | |
c9c989a0 TH |
119 | |
120 | $answercount++; | |
121 | ||
24f7485b | 122 | //check grades |
c9c989a0 TH |
123 | if ($data['fraction'][$key] > 0) { |
124 | $totalfraction += $data['fraction'][$key]; | |
125 | } | |
126 | if ($data['fraction'][$key] > $maxfraction) { | |
127 | $maxfraction = $data['fraction'][$key]; | |
24f7485b | 128 | } |
271ffe3f | 129 | } |
24f7485b | 130 | |
c9c989a0 | 131 | if ($answercount == 0) { |
271ffe3f | 132 | $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_multichoice', 2); |
133 | $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2); | |
12039f2e | 134 | } else if ($answercount == 1) { |
271ffe3f | 135 | $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2); |
136 | ||
137 | } | |
24f7485b | 138 | |
139 | /// Perform sanity checks on fractional grades | |
140 | if ($data['single']) { | |
141 | if ($maxfraction != 1) { | |
12039f2e TH |
142 | $errors['fraction[0]'] = get_string('errfractionsnomax', 'qtype_multichoice', |
143 | $maxfraction * 100); | |
24f7485b | 144 | } |
145 | } else { | |
12039f2e | 146 | $totalfraction = round($totalfraction, 2); |
24f7485b | 147 | if ($totalfraction != 1) { |
12039f2e TH |
148 | $errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice', |
149 | $totalfraction * 100); | |
24f7485b | 150 | } |
151 | } | |
271ffe3f | 152 | return $errors; |
153 | } | |
c9c989a0 TH |
154 | |
155 | public function qtype() { | |
156 | return 'multichoice'; | |
157 | } | |
271ffe3f | 158 | } |