MDL-9506 First draft of gradelib, and the stubs for the unit tests.
[moodle.git] / question / type / multianswer / edit_multianswer_form.php
CommitLineData
271ffe3f 1<?php
2/**
3 * Defines the editing form for the multianswer question type.
4 *
5 * @copyright &copy; 2007 Jamie Pratt
6 * @author Jamie Pratt me@jamiep.org
7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
830e47a8 8 * @package questionbank
9 * @subpackage questiontypes
271ffe3f 10 */
11
12/**
13 * multianswer editing form definition.
14 */
15class question_edit_multianswer_form extends question_edit_form {
16
17 // No question-type specific form fields.
120e5cbf 18 function definition(){
19 parent::definition();
20 $mform =& $this->_form;
21 $mform->addRule('questiontext', null, 'required', null, 'client');
22 }
32db0d42 23 function set_data($question) {
120e5cbf 24 if (isset($question->id) and $question->id and $question->qtype and $question->questiontext) {
c6fc9988 25
26 foreach ($question->options->questions as $key => $wrapped) {
27 // The old way of restoring the definitions is kept to gradually
28 // update all multianswer questions
29 if (empty($wrapped->questiontext)) {
30 $parsableanswerdef = '{' . $wrapped->defaultgrade . ':';
31 switch ($wrapped->qtype) {
32 case 'multichoice':
33 $parsableanswerdef .= 'MULTICHOICE:';
34 break;
35 case 'shortanswer':
36 $parsableanswerdef .= 'SHORTANSWER:';
37 break;
38 case 'numerical':
39 $parsableanswerdef .= 'NUMERICAL:';
40 break;
41 default:
42 error("questiontype $wrapped->qtype not recognized");
43 }
44 $separator= '';
45 foreach ($wrapped->options->answers as $subanswer) {
46 $parsableanswerdef .= $separator
47 . '%' . round(100*$subanswer->fraction) . '%';
48 $parsableanswerdef .= $subanswer->answer;
49 if (!empty($wrapped->options->tolerance)) {
50 // Special for numerical answers:
51 $parsableanswerdef .= ":{$wrapped->options->tolerance}";
52 // We only want tolerance for the first alternative, it will
53 // be applied to all of the alternatives.
54 unset($wrapped->options->tolerance);
55 }
56 if ($subanswer->feedback) {
57 $parsableanswerdef .= "#$subanswer->feedback";
58 }
59 $separator = '~';
60 }
61 $parsableanswerdef .= '}';
62 // Fix the questiontext fields of old questions
63 set_field('question', 'questiontext', addslashes($parsableanswerdef), 'id', $wrapped->id);
64 } else {
65 $parsableanswerdef = str_replace('&#', '&\#', $wrapped->questiontext);
66 }
67 $question->questiontext = str_replace("{#$key}", $parsableanswerdef, $question->questiontext);
68 }
69 }
120e5cbf 70 parent::set_data($question);
71 }
72
73 function validation($data){
74 //TODO would be nice to parse the question text here and output some error
75 //messages if there is a problem with the text.
76 $errors = array();
77 //extra check to make sure there is something in the htmlarea besides a <br />
78 $questiontext= trim(strip_tags($data['questiontext']));
79 if ($questiontext==''){
80 $errors['questiontext'] = get_string('err_required', 'form');
81 }
82 return $errors;
c6fc9988 83 }
271ffe3f 84
85 function qtype() {
86 return 'multianswer';
87 }
88}
89?>