2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Defines the editing form for the multiple choice question type.
21 * @subpackage multichoice
22 * @copyright 2007 Jamie Pratt
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') || die();
31 * Multiple choice editing form definition.
33 * @copyright 2007 Jamie Pratt
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class qtype_multichoice_edit_form extends question_edit_form {
38 * Add question-type specific form fields.
40 * @param object $mform the form being built.
42 protected function definition_inner($mform) {
44 get_string('answersingleno', 'qtype_multichoice'),
45 get_string('answersingleyes', 'qtype_multichoice'),
47 $mform->addElement('select', 'single',
48 get_string('answerhowmany', 'qtype_multichoice'), $menu);
49 $mform->setDefault('single', 1);
51 $mform->addElement('advcheckbox', 'shuffleanswers',
52 get_string('shuffleanswers', 'qtype_multichoice'), null, null, array(0, 1));
53 $mform->addHelpButton('shuffleanswers', 'shuffleanswers', 'qtype_multichoice');
54 $mform->setDefault('shuffleanswers', 1);
56 $mform->addElement('select', 'answernumbering',
57 get_string('answernumbering', 'qtype_multichoice'),
58 qtype_multichoice::get_numbering_styles());
59 $mform->setDefault('answernumbering', 'abc');
61 $this->add_per_answer_fields($mform, get_string('choiceno', 'qtype_multichoice', '{no}'),
62 question_bank::fraction_options_full(), max(5, QUESTION_NUMANS_START));
64 $this->add_combined_feedback_fields(true);
65 $mform->disabledIf('shownumcorrect', 'single', 'eq', 1);
67 $this->add_interactive_settings(true, true);
70 protected function get_per_answer_fields($mform, $label, $gradeoptions,
71 &$repeatedoptions, &$answersoption) {
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';
86 protected function data_preprocessing($question) {
87 $question = parent::data_preprocessing($question);
88 $question = $this->data_preprocessing_answers($question, true);
89 $question = $this->data_preprocessing_combined_feedback($question, true);
90 $question = $this->data_preprocessing_hints($question, true, true);
92 if (!empty($question->options)) {
93 $question->single = $question->options->single;
94 $question->shuffleanswers = $question->options->shuffleanswers;
95 $question->answernumbering = $question->options->answernumbering;
101 public function validation($data, $files) {
102 $errors = parent::validation($data, $files);
103 $answers = $data['answer'];
109 foreach ($answers as $key => $answer) {
110 //check no of choices
111 $trimmedanswer = trim($answer['text']);
112 $fraction = (float) $data['fraction'][$key];
113 if ($trimmedanswer === '' && empty($fraction)) {
116 if ($trimmedanswer === '') {
117 $errors['fraction['.$key.']'] = get_string('errgradesetanswerblank', 'qtype_multichoice');
123 if ($data['fraction'][$key] > 0) {
124 $totalfraction += $data['fraction'][$key];
126 if ($data['fraction'][$key] > $maxfraction) {
127 $maxfraction = $data['fraction'][$key];
131 if ($answercount == 0) {
132 $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
133 $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
134 } else if ($answercount == 1) {
135 $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
139 /// Perform sanity checks on fractional grades
140 if ($data['single']) {
141 if ($maxfraction != 1) {
142 $errors['fraction[0]'] = get_string('errfractionsnomax', 'qtype_multichoice',
146 $totalfraction = round($totalfraction, 2);
147 if ($totalfraction != 1) {
148 $errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice',
149 $totalfraction * 100);
155 public function qtype() {
156 return 'multichoice';