edit_multichoice_form.php 7.02 KB
Newer Older
1
<?php
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// 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
23
 * @copyright  2007 Jamie Pratt
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
25
 */

26
27
28
29

defined('MOODLE_INTERNAL') || die();


30
/**
31
32
 * Multiple choice editing form definition.
 *
33
34
 * @copyright  2007 Jamie Pratt
 * @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
39
40
41
    /**
     * Add question-type specific form fields.
     *
     * @param object $mform the form being built.
     */
42
    protected function definition_inner($mform) {
43
44
45
46
47
48
        $menu = array(
            get_string('answersingleno', 'qtype_multichoice'),
            get_string('answersingleyes', 'qtype_multichoice'),
        );
        $mform->addElement('select', 'single',
                get_string('answerhowmany', 'qtype_multichoice'), $menu);
49
50
        $mform->setDefault('single', $this->get_default_value('single',
            get_config('qtype_multichoice', 'answerhowmany')));
51

52
53
        $mform->addElement('advcheckbox', 'shuffleanswers',
                get_string('shuffleanswers', 'qtype_multichoice'), null, null, array(0, 1));
54
        $mform->addHelpButton('shuffleanswers', 'shuffleanswers', 'qtype_multichoice');
55
56
        $mform->setDefault('shuffleanswers', $this->get_default_value('shuffleanswers',
            get_config('qtype_multichoice', 'shuffleanswers')));
57

58
59
        $mform->addElement('select', 'answernumbering',
                get_string('answernumbering', 'qtype_multichoice'),
60
                qtype_multichoice::get_numbering_styles());
61
62
        $mform->setDefault('answernumbering', $this->get_default_value('answernumbering',
            get_config('qtype_multichoice', 'answernumbering')));
63

64
65
66
        $mform->addElement('selectyesno', 'showstandardinstruction',
            get_string('showstandardinstruction', 'qtype_multichoice'), null, null, [0, 1]);
        $mform->addHelpButton('showstandardinstruction', 'showstandardinstruction', 'qtype_multichoice');
67
        $mform->setDefault('showstandardinstruction', $this->get_default_value('showstandardinstruction', 0));
68

69
        $this->add_per_answer_fields($mform, get_string('choiceno', 'qtype_multichoice', '{no}'),
70
                question_bank::fraction_options_full(), max(5, QUESTION_NUMANS_START));
71

72
73
        $this->add_combined_feedback_fields(true);
        $mform->disabledIf('shownumcorrect', 'single', 'eq', 1);
74

75
        $this->add_interactive_settings(true, true);
76
77
    }

78
79
80
81
    protected function get_per_answer_fields($mform, $label, $gradeoptions,
            &$repeatedoptions, &$answersoption) {
        $repeated = array();
        $repeated[] = $mform->createElement('editor', 'answer',
82
                $label, array('rows' => 1), $this->editoroptions);
83
84
85
86
87
88
89
90
91
92
        $repeated[] = $mform->createElement('select', 'fraction',
                get_string('grade'), $gradeoptions);
        $repeated[] = $mform->createElement('editor', 'feedback',
                get_string('feedback', 'question'), array('rows' => 1), $this->editoroptions);
        $repeatedoptions['answer']['type'] = PARAM_RAW;
        $repeatedoptions['fraction']['default'] = 0;
        $answersoption = 'answers';
        return $repeated;
    }

93
94
95
96
97
98
99
    protected function get_hint_fields($withclearwrong = false, $withshownumpartscorrect = false) {
        list($repeated, $repeatedoptions) = parent::get_hint_fields($withclearwrong, $withshownumpartscorrect);
        $repeatedoptions['hintclearwrong']['disabledif'] = array('single', 'eq', 1);
        $repeatedoptions['hintshownumcorrect']['disabledif'] = array('single', 'eq', 1);
        return array($repeated, $repeatedoptions);
    }

100
    protected function data_preprocessing($question) {
101
102
103
        $question = parent::data_preprocessing($question);
        $question = $this->data_preprocessing_answers($question, true);
        $question = $this->data_preprocessing_combined_feedback($question, true);
104
        $question = $this->data_preprocessing_hints($question, true, true);
105
106

        if (!empty($question->options)) {
107
108
109
            $question->single = $question->options->single;
            $question->shuffleanswers = $question->options->shuffleanswers;
            $question->answernumbering = $question->options->answernumbering;
110
            $question->showstandardinstruction = $question->options->showstandardinstruction;
111
112
        }

113
        return $question;
114
115
    }

116
    public function validation($data, $files) {
117
        $errors = parent::validation($data, $files);
118
119
        $answers = $data['answer'];
        $answercount = 0;
120
121
122
123

        $totalfraction = 0;
        $maxfraction = -1;

124
        foreach ($answers as $key => $answer) {
125
            // Check no of choices.
126
            $trimmedanswer = trim($answer['text']);
127
            $fraction = (float) $data['fraction'][$key];
128
            if ($trimmedanswer === '' && empty($fraction)) {
129
                continue;
130
            }
131
            if ($trimmedanswer === '') {
132
133
                $errors['fraction['.$key.']'] = get_string('errgradesetanswerblank', 'qtype_multichoice');
            }
134
135
136

            $answercount++;

137
            // Check grades.
138
139
140
141
142
            if ($data['fraction'][$key] > 0) {
                $totalfraction += $data['fraction'][$key];
            }
            if ($data['fraction'][$key] > $maxfraction) {
                $maxfraction = $data['fraction'][$key];
143
            }
144
        }
145

146
        if ($answercount == 0) {
147
148
            $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
            $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
149
        } else if ($answercount == 1) {
150
151
152
            $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);

        }
153

154
        // Perform sanity checks on fractional grades.
155
156
        if ($data['single']) {
            if ($maxfraction != 1) {
157
158
                $errors['fraction[0]'] = get_string('errfractionsnomax', 'qtype_multichoice',
                        $maxfraction * 100);
159
160
            }
        } else {
161
            $totalfraction = round($totalfraction, 2);
162
            if ($totalfraction != 1) {
163
164
                $errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice',
                        $totalfraction * 100);
165
166
            }
        }
167
168
        return $errors;
    }
169
170
171
172

    public function qtype() {
        return 'multichoice';
    }
173
}