MDL-22164 backup - finished cleaning of questions backup code
[moodle.git] / question / type / numerical / edit_numerical_form.php
CommitLineData
aeb15530 1<?php
fe6ce234
DC
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
6e9b6ba2 18/**
19 * Defines the editing form for the numerical question type.
20 *
21 * @copyright &copy; 2007 Jamie Pratt
22 * @author Jamie Pratt me@jamiep.org
23 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
830e47a8 24 * @package questionbank
25 * @subpackage questiontypes
6e9b6ba2 26 */
27
28/**
29 * numerical editing form definition.
30 */
31class question_edit_numerical_form extends question_edit_form {
2aef1fe5 32
33 function get_per_answer_fields(&$mform, $label, $gradeoptions, &$repeatedoptions, &$answersoption) {
34 $repeated = parent::get_per_answer_fields($mform, $label, $gradeoptions, $repeatedoptions, $answersoption);
35
36 $tolerance =& $mform->createElement('text', 'tolerance', get_string('acceptederror', 'quiz'));
37 $repeatedoptions['tolerance']['type'] = PARAM_NUMBER;
38 array_splice($repeated, 3, 0, array($tolerance));
39 $repeated[1]->setSize(10);
40
41 return $repeated;
42 }
43
6e9b6ba2 44 /**
45 * Add question-type specific form fields.
46 *
47 * @param MoodleQuickForm $mform the form being built.
48 */
49 function definition_inner(&$mform) {
b003b818 50 global $QTYPES ;
6e9b6ba2 51
52//------------------------------------------------------------------------------------------
53 $creategrades = get_grade_options();
2aef1fe5 54 $this->add_per_answer_fields($mform, get_string('answerno', 'qtype_numerical', '{no}'),
55 $creategrades->gradeoptions);
6e9b6ba2 56//------------------------------------------------------------------------------------------
cf146692 57 $QTYPES['numerical']->add_units_options($mform,$this);
fe6ce234 58 $QTYPES['numerical']->add_units_elements($mform,$this);
6e9b6ba2 59 }
60
fe6ce234
DC
61 function data_preprocessing($question) {
62 global $QTYPES ;
6e9b6ba2 63 if (isset($question->options)){
64 $answers = $question->options->answers;
65 if (count($answers)) {
66 $key = 0;
67 foreach ($answers as $answer){
fe6ce234 68 $draftid = file_get_submitted_draft_itemid('feedback['.$key.']');
6e9b6ba2 69 $default_values['answer['.$key.']'] = $answer->answer;
70 $default_values['fraction['.$key.']'] = $answer->fraction;
71 $default_values['tolerance['.$key.']'] = $answer->tolerance;
fe6ce234
DC
72 $default_values['feedback['.$key.']'] = array();
73 $default_values['feedback['.$key.']']['format'] = $answer->feedbackformat;
74 $default_values['feedback['.$key.']']['text'] = file_prepare_draft_area(
75 $draftid, // draftid
76 $this->context->id, // context
77 'question', // component
78 'answerfeedback', // filarea
79 !empty($answer->id)?(int)$answer->id:null, // itemid
80 $this->fileoptions, // options
81 $answer->feedback // text
82 );
83 $default_values['feedback['.$key.']']['itemid'] = $draftid;
6e9b6ba2 84 $key++;
85 }
86 }
fe6ce234 87 $QTYPES['numerical']->set_numerical_unit_data($this, $question, $default_values);
4d2657c2
PP
88
89 /* if (isset($question->options->units)){
cf146692
PP
90 $units = array_values($question->options->units);
91 if (!empty($units)) {
92 foreach ($units as $key => $unit){
93 $default_values['unit['.$key.']'] = $unit->unit;
94 $default_values['multiplier['.$key.']'] = $unit->multiplier;
95 }
6e9b6ba2 96 }
4d2657c2 97 }*/
6e9b6ba2 98 $question = (object)((array)$question + $default_values);
99 }
fe6ce234 100 return $question;
6e9b6ba2 101 }
fe6ce234 102
a78890d5 103 function validation($data, $files) {
cf146692 104 global $QTYPES;
fe93ba83 105 $errors = parent::validation($data, $files);
51e89d95 106
107 // Check the answers.
6e9b6ba2 108 $answercount = 0;
51e89d95 109 $maxgrade = false;
110 $answers = $data['answer'];
111 foreach ($answers as $key => $answer) {
6e9b6ba2 112 $trimmedanswer = trim($answer);
d95c82ab 113 if ($trimmedanswer != '') {
6e9b6ba2 114 $answercount++;
d95c82ab 115 if (!(is_numeric($trimmedanswer) || $trimmedanswer == '*')) {
116 $errors["answer[$key]"] = get_string('answermustbenumberorstar', 'qtype_numerical');
117 }
51e89d95 118 if ($data['fraction'][$key] == 1) {
119 $maxgrade = true;
120 }
fe6ce234 121 } else if ($data['fraction'][$key] != 0 || !html_is_blank($data['feedback'][$key]['text'])) {
94a6d656 122 $errors["answer[$key]"] = get_string('answermustbenumberorstar', 'qtype_numerical');
123 $answercount++;
6e9b6ba2 124 }
125 }
26b26662 126 if ($answercount == 0) {
6e9b6ba2 127 $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_numerical');
128 }
51e89d95 129 if ($maxgrade == false) {
130 $errors['fraction[0]'] = get_string('fractionsnomax', 'question');
131 }
cf146692 132 $QTYPES['numerical']->validate_numerical_options($data, $errors) ;
51e89d95 133
6e9b6ba2 134 return $errors;
135 }
fe6ce234 136
6e9b6ba2 137 function qtype() {
138 return 'numerical';
139 }
140}