edit_multichoice_form.php 7.47 KB
Newer Older
1
<?php
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

// 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/>.

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

20
21
22
23
24
25
/**
 * Defines the editing form for the multichoice question type.
 *
 * @copyright &copy; 2007 Jamie Pratt
 * @author Jamie Pratt me@jamiep.org
 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
26
27
 * @package questionbank
 * @subpackage questiontypes
28
29
30
31
32
33
34
35
36
37
38
39
 */

/**
 * multiple choice editing form definition.
 */
class question_edit_multichoice_form extends question_edit_form {
    /**
     * Add question-type specific form fields.
     *
     * @param object $mform the form being built.
     */
    function definition_inner(&$mform) {
40
        global $QTYPES;
41

42
43
        $menu = array(get_string('answersingleno', 'qtype_multichoice'), get_string('answersingleyes', 'qtype_multichoice'));
        $mform->addElement('select', 'single', get_string('answerhowmany', 'qtype_multichoice'), $menu);
44
        $mform->setDefault('single', 1);
45

46
        $mform->addElement('advcheckbox', 'shuffleanswers', get_string('shuffleanswers', 'qtype_multichoice'), null, null, array(0,1));
47
        $mform->addHelpButton('shuffleanswers', 'shuffleanswers', 'qtype_multichoice');
48
        $mform->setDefault('shuffleanswers', 1);
49

50
51
52
53
54
55
56
57
        $numberingoptions = $QTYPES[$this->qtype()]->get_numbering_styles();
        $menu = array();
        foreach ($numberingoptions as $numberingoption) {
            $menu[$numberingoption] = get_string('answernumbering' . $numberingoption, 'qtype_multichoice');
        }
        $mform->addElement('select', 'answernumbering', get_string('answernumbering', 'qtype_multichoice'), $menu);
        $mform->setDefault('answernumbering', 'abc');

58
59
60
61
/*        $mform->addElement('static', 'answersinstruct', get_string('choices', 'qtype_multichoice'), get_string('fillouttwochoices', 'qtype_multichoice'));
        $mform->closeHeaderBefore('answersinstruct');
*/
        $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

        $mform->addElement('header', 'overallfeedbackhdr', get_string('overallfeedback', 'qtype_multichoice'));

67
        foreach (array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback') as $feedbackname) {
68
69
            $mform->addElement('editor', $feedbackname, get_string($feedbackname, 'qtype_multichoice'),
                                array('course' => $this->coursefilesid), $this->editoroptions);
70
71
            $mform->setType($feedbackname, PARAM_RAW);
        }
72
73
74

    }

75
    function data_preprocessing($question) {
76
77
78
79
80
81
82
        if (isset($question->options)){
            $answers = $question->options->answers;
            if (count($answers)) {
                $key = 0;
                foreach ($answers as $answer){
                    $default_values['answer['.$key.']'] = $answer->answer;
                    $default_values['fraction['.$key.']'] = $answer->fraction;
83
84
85
86
87
88
89

                    // prepare question text
                    $draftid = file_get_submitted_draft_itemid('feedback['.$key.']');
                    $default_values['feedback['.$key.']'] = array();
                    $default_values['feedback['.$key.']']['text'] = file_prepare_draft_area($draftid, $this->context->id, 'question', 'answerfeedback', empty($answer->id)?null:(int)$answer->id, null, $answer->feedback);
                    $default_values['feedback['.$key.']']['format'] = $answer->feedbackformat;
                    $default_values['feedback['.$key.']']['itemid'] = $draftid;
90
91
92
93
                    $key++;
                }
            }
            $default_values['single'] =  $question->options->single;
94
            $default_values['answernumbering'] =  $question->options->answernumbering;
95
            $default_values['shuffleanswers'] =  $question->options->shuffleanswers;
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

            // prepare feedback editor to display files in draft area
            foreach (array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback') as $feedbackname) {
                $draftid = file_get_submitted_draft_itemid($feedbackname);
                $text = $question->options->$feedbackname;
                $feedbackformat = $feedbackname . 'format';
                $format = $question->options->$feedbackformat;
                $default_values[$feedbackname] = array();
                $default_values[$feedbackname]['text'] = file_prepare_draft_area(
                    $draftid,       // draftid
                    $this->context->id,    // context
                    'qtype_multichoice',   // component
                    $feedbackname,         // filarea
                    !empty($question->id)?(int)$question->id:null, // itemid
                    $this->fileoptions,    // options
                    $text      // text
                );
                $default_values[$feedbackname]['format'] = $format;
                $default_values[$feedbackname]['itemid'] = $draftid;
            }
            // prepare files code block ends

118
119
            $question = (object)((array)$question + $default_values);
        }
120
        return $question;
121
122
123
124
125
126
    }

    function qtype() {
        return 'multichoice';
    }

127
    function validation($data, $files) {
128
        $errors = parent::validation($data, $files);
129
130
        $answers = $data['answer'];
        $answercount = 0;
131
132
133
134
135
136

        $totalfraction = 0;
        $maxfraction = -1;

        foreach ($answers as $key => $answer){
            //check no of choices
137
138
139
140
            $trimmedanswer = trim($answer);
            if (!empty($trimmedanswer)){
                $answercount++;
            }
141
142
143
144
145
146
147
148
149
            //check grades
            if ($answer != '') {
                if ($data['fraction'][$key] > 0) {
                    $totalfraction += $data['fraction'][$key];
                }
                if ($data['fraction'][$key] > $maxfraction) {
                    $maxfraction = $data['fraction'][$key];
                }
            }
150
        }
151

152
153
154
155
156
157
158
        if ($answercount==0){
            $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
            $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
        } elseif ($answercount==1){
            $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);

        }
159
160
161
162
163
164
165
166
167
168
169
170
171
172

        /// Perform sanity checks on fractional grades
        if ($data['single']) {
            if ($maxfraction != 1) {
                $maxfraction = $maxfraction * 100;
                $errors['fraction[0]'] = get_string('errfractionsnomax', 'qtype_multichoice', $maxfraction);
            }
        } else {
            $totalfraction = round($totalfraction,2);
            if ($totalfraction != 1) {
                $totalfraction = $totalfraction * 100;
                $errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice', $totalfraction);
            }
        }
173
174
175
        return $errors;
    }
}