edit_multichoice_form.php 4.89 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
        $mform->setDefault('single', 1);
50

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

56
57
        $mform->addElement('select', 'answernumbering',
                get_string('answernumbering', 'qtype_multichoice'),
58
                qtype_multichoice::get_numbering_styles());
59
60
        $mform->setDefault('answernumbering', 'abc');

61
        $creategrades = get_grade_options();
62
63
        $this->add_per_answer_fields($mform, get_string('choiceno', 'qtype_multichoice', '{no}'),
                $creategrades->gradeoptionsfull, max(5, QUESTION_NUMANS_START));
64

65
66
        $this->add_combined_feedback_fields(true);
        $mform->disabledIf('shownumcorrect', 'single', 'eq', 1);
67

68
        $this->add_interactive_settings(true, true);
69
70
    }

71
    protected function data_preprocessing($question) {
72
73
74
        $question = parent::data_preprocessing($question);
        $question = $this->data_preprocessing_answers($question, true);
        $question = $this->data_preprocessing_combined_feedback($question, true);
75
        $question = $this->data_preprocessing_hints($question, true, true);
76
77

        if (!empty($question->options)) {
78
79
80
            $question->single = $question->options->single;
            $question->shuffleanswers = $question->options->shuffleanswers;
            $question->answernumbering = $question->options->answernumbering;
81
82
        }

83
        return $question;
84
85
    }

86
    public function validation($data, $files) {
87
        $errors = parent::validation($data, $files);
88
89
        $answers = $data['answer'];
        $answercount = 0;
90
91
92
93

        $totalfraction = 0;
        $maxfraction = -1;

94
        foreach ($answers as $key => $answer) {
95
            //check no of choices
96
            $trimmedanswer = trim($answer);
97
98
            if (empty($trimmedanswer)) {
                continue;
99
            }
100
101
102

            $answercount++;

103
            //check grades
104
105
106
107
108
            if ($data['fraction'][$key] > 0) {
                $totalfraction += $data['fraction'][$key];
            }
            if ($data['fraction'][$key] > $maxfraction) {
                $maxfraction = $data['fraction'][$key];
109
            }
110
        }
111

112
        if ($answercount == 0) {
113
114
            $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
            $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
115
        } else if ($answercount == 1) {
116
117
118
            $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);

        }
119
120
121
122

        /// Perform sanity checks on fractional grades
        if ($data['single']) {
            if ($maxfraction != 1) {
123
124
                $errors['fraction[0]'] = get_string('errfractionsnomax', 'qtype_multichoice',
                        $maxfraction * 100);
125
126
            }
        } else {
127
            $totalfraction = round($totalfraction, 2);
128
            if ($totalfraction != 1) {
129
130
                $errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice',
                        $totalfraction * 100);
131
132
            }
        }
133
134
        return $errors;
    }
135
136
137
138

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