-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit_bsmultichoice_form.php
186 lines (160 loc) · 7.58 KB
/
edit_bsmultichoice_form.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
// 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/>.
/**
* Defines the editing form for the multiple choice question type.
*
* @package qtype
* @subpackage bsmultichoice
* @copyright 2007 Jamie Pratt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Multiple choice editing form definition.
*
* @copyright 2007 Jamie Pratt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_bsmultichoice_edit_form extends question_edit_form {
/**
* Add question-type specific form fields.
*
* @param object $mform the form being built.
*/
protected function definition_inner($mform) {
/// Add Infographic field ///
$tempinfographicdata = "";
if (isset($this->question->options->infographicdata)){
if($this->question->options->infographicdata !== ""){
$tempinfographicdata = $this->question->options->infographicdata;
}
}
$mform->addElement('editor',
'infographicdata',
get_string('infographicfieldlabel', 'qtype_bsmultichoice'),
array('rows' => 15), $this->editoroptions);
$mform->setType('infographicdata', PARAM_RAW);
$mform->setDefault('infographicdata',array('text'=>$tempinfographicdata,'format'=>'1'));
///
$menu = array(
get_string('answersingleno', 'qtype_bsmultichoice'),
get_string('answersingleyes', 'qtype_bsmultichoice'),
);
$mform->addElement('select', 'single',
get_string('answerhowmany', 'qtype_bsmultichoice'), $menu);
$mform->setDefault('single', get_config('qtype_bsmultichoice', 'answerhowmany'));
$mform->addElement('advcheckbox', 'shuffleanswers',
get_string('shuffleanswers', 'qtype_bsmultichoice'), null, null, array(0, 1));
$mform->addHelpButton('shuffleanswers', 'shuffleanswers', 'qtype_bsmultichoice');
$mform->setDefault('shuffleanswers', get_config('qtype_bsmultichoice', 'shuffleanswers'));
$mform->addElement('select', 'answernumbering',
get_string('answernumbering', 'qtype_bsmultichoice'),
qtype_bsmultichoice::get_numbering_styles());
$mform->setDefault('answernumbering', get_config('qtype_bsmultichoice', 'answernumbering'));
$mform->addElement('selectyesno', 'showstandardinstruction',
get_string('showstandardinstruction', 'qtype_bsmultichoice'), null, null, [0, 1]);
$mform->addHelpButton('showstandardinstruction', 'showstandardinstruction', 'qtype_bsmultichoice');
$mform->setDefault('showstandardinstruction', 0);
$this->add_per_answer_fields($mform, get_string('choiceno', 'qtype_bsmultichoice', '{no}'),
question_bank::fraction_options_full(), max(5, QUESTION_NUMANS_START));
$this->add_combined_feedback_fields(true);
$mform->disabledIf('shownumcorrect', 'single', 'eq', 1);
$this->add_interactive_settings(true, true);
}
protected function get_per_answer_fields($mform, $label, $gradeoptions,
&$repeatedoptions, &$answersoption) {
$repeated = array();
$repeated[] = $mform->createElement('editor', 'answer',
$label, array('rows' => 1), $this->editoroptions);
$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;
}
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);
}
protected function data_preprocessing($question) {
$question = parent::data_preprocessing($question);
$question = $this->data_preprocessing_answers($question, true);
$question = $this->data_preprocessing_combined_feedback($question, true);
$question = $this->data_preprocessing_hints($question, true, true);
if (!empty($question->options)) {
$question->single = $question->options->single;
$question->shuffleanswers = $question->options->shuffleanswers;
$question->answernumbering = $question->options->answernumbering;
$question->showstandardinstruction = $question->options->showstandardinstruction;
$question->infographicdata = $question->options->infographicdata;
}
return $question;
}
public function validation($data, $files) {
$errors = parent::validation($data, $files);
$answers = $data['answer'];
$answercount = 0;
$totalfraction = 0;
$maxfraction = -1;
foreach ($answers as $key => $answer) {
// Check no of choices.
$trimmedanswer = trim($answer['text']);
$fraction = (float) $data['fraction'][$key];
if ($trimmedanswer === '' && empty($fraction)) {
continue;
}
if ($trimmedanswer === '') {
$errors['fraction['.$key.']'] = get_string('errgradesetanswerblank', 'qtype_bsmultichoice');
}
$answercount++;
// Check grades.
if ($data['fraction'][$key] > 0) {
$totalfraction += $data['fraction'][$key];
}
if ($data['fraction'][$key] > $maxfraction) {
$maxfraction = $data['fraction'][$key];
}
}
if ($answercount == 0) {
$errors['answer[0]'] = get_string('notenoughanswers', 'qtype_bsmultichoice', 2);
$errors['answer[1]'] = get_string('notenoughanswers', 'qtype_bsmultichoice', 2);
} else if ($answercount == 1) {
$errors['answer[1]'] = get_string('notenoughanswers', 'qtype_bsmultichoice', 2);
}
// Perform sanity checks on fractional grades.
if ($data['single']) {
if ($maxfraction != 1) {
$errors['fraction[0]'] = get_string('errfractionsnomax', 'qtype_bsmultichoice',
$maxfraction * 100);
}
} else {
$totalfraction = round($totalfraction, 2);
if ($totalfraction != 1) {
$errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_bsmultichoice',
$totalfraction * 100);
}
}
return $errors;
}
public function qtype() {
return 'bsmultichoice';
}
}