e2367e683671895e2b4385069000fcfe02dc01c0
[moodle.git] / question / type / multichoice / edit_multichoice_form.php
1 <?php
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/>.
17 /**
18  * Defines the editing form for the multiple choice question type.
19  *
20  * @package    qtype
21  * @subpackage multichoice
22  * @copyright  2007 Jamie Pratt
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
27 defined('MOODLE_INTERNAL') || die();
30 /**
31  * Multiple choice editing form definition.
32  *
33  * @copyright  2007 Jamie Pratt
34  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35  */
36 class qtype_multichoice_edit_form extends question_edit_form {
37     /**
38      * Add question-type specific form fields.
39      *
40      * @param object $mform the form being built.
41      */
42     protected function definition_inner($mform) {
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);
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);
68     }
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     }
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;
96         }
98         return $question;
99     }
101     public function validation($data, $files) {
102         $errors = parent::validation($data, $files);
103         $answers = $data['answer'];
104         $answercount = 0;
106         $totalfraction = 0;
107         $maxfraction = -1;
109         foreach ($answers as $key => $answer) {
110             //check no of choices
111             $trimmedanswer = trim($answer['text']);
112             if (empty($trimmedanswer)) {
113                 continue;
114             }
116             $answercount++;
118             //check grades
119             if ($data['fraction'][$key] > 0) {
120                 $totalfraction += $data['fraction'][$key];
121             }
122             if ($data['fraction'][$key] > $maxfraction) {
123                 $maxfraction = $data['fraction'][$key];
124             }
125         }
127         if ($answercount == 0) {
128             $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
129             $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
130         } else if ($answercount == 1) {
131             $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
133         }
135         /// Perform sanity checks on fractional grades
136         if ($data['single']) {
137             if ($maxfraction != 1) {
138                 $errors['fraction[0]'] = get_string('errfractionsnomax', 'qtype_multichoice',
139                         $maxfraction * 100);
140             }
141         } else {
142             $totalfraction = round($totalfraction, 2);
143             if ($totalfraction != 1) {
144                 $errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice',
145                         $totalfraction * 100);
146             }
147         }
148         return $errors;
149     }
151     public function qtype() {
152         return 'multichoice';
153     }