various formslib work including bug fixes for MDL-8039 id attribute collision between...
[moodle.git] / question / type / calculated / edit_calculated_form.php
CommitLineData
271ffe3f 1<?php
2/**
3 * Defines the editing form for the calculated 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
8 * @package questions
9 */
10
11/**
12 * calculated editing form definition.
13 */
14class question_edit_calculated_form extends question_edit_form {
15 /**
16 * Add question-type specific form fields.
17 *
18 * @param object $mform the form being built.
19 */
20 function definition_inner(&$mform) {
21 $mform->addElement('text', 'answers[0]', get_string("correctanswerformula", "quiz"));
22 $mform->setType('answers[0]', PARAM_NOTAGS);
23
24 $mform->addElement('hidden', 'fraction[0]', '1.0');
25
26 $mform->addElement('text', 'tolerance[0]', get_string("tolerance", "quiz"));
27 $mform->setType('tolerance[0]', PARAM_NOTAGS);
28
29 $mform->addElement('select', 'shuffleanswers', get_string('shuffleanswers', 'qtype_calculated') );
30 $mform->setHelpButton('shuffleanswers', array('calculatedshuffle', get_string('shuffleanswers','qtype_calculated'), 'quiz'));
31
32/* $mform->addElement('static', 'answersinstruct', get_string('choices', 'qtype_calculated'), get_string('fillouttwochoices', 'qtype_calculated'));
33 $mform->closeHeaderBefore('answersinstruct');
34*/
35 $creategrades = get_grade_options();
36 $gradeoptions = $creategrades->gradeoptionsfull;
37 $repeated = array();
38 $repeated[] =& $mform->createElement('header', 'choicehdr', get_string('choiceno', 'qtype_calculated', '{no}'));
39 $repeated[] =& $mform->createElement('text', 'answer', get_string('answer', 'quiz'));
40 $repeated[] =& $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions);
41 $repeated[] =& $mform->createElement('htmleditor', 'feedback', get_string('feedback', 'quiz'));
42
43 if (isset($this->question->options)){
44 $countanswers = count($this->question->options->answers);
45 } else {
46 $countanswers = 0;
47 }
48 $repeatsatstart = (QUESTION_NUMANS_START > ($countanswers + QUESTION_NUMANS_ADD))?
49 QUESTION_NUMANS_START : ($countanswers + QUESTION_NUMANS_ADD);
50 $repeatedoptions = array();
51 $repeatedoptions['answer']['type'] = PARAM_TEXT;
52 $repeatedoptions['fraction']['default'] = 0;
53 $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', QUESTION_NUMANS_ADD, get_string('addmorechoiceblanks', 'qtype_calculated'));
54
55 $mform->addElement('header', 'overallfeedbackhdr', get_string('overallfeedback', 'qtype_calculated'));
56
57 $mform->addElement('htmleditor', 'correctfeedback', get_string('correctfeedback', 'qtype_calculated'));
58 $mform->setType('correctfeedback', PARAM_RAW);
59
60 $mform->addElement('htmleditor', 'partiallycorrectfeedback', get_string('partiallycorrectfeedback', 'qtype_calculated'));
61 $mform->setType('partiallycorrectfeedback', PARAM_RAW);
62
63 $mform->addElement('htmleditor', 'incorrectfeedback', get_string('incorrectfeedback', 'qtype_calculated'));
64 $mform->setType('incorrectfeedback', PARAM_RAW);
65
66 }
67
68 function set_defaults($question) {
69 if (isset($question->options)){
70 $answers = $question->options->answers;
71 if (count($answers)) {
72 $key = 0;
73 foreach ($answers as $answer){
74 $default_values['answer['.$key.']'] = $answer->answer;
75 $default_values['fraction['.$key.']'] = $answer->fraction;
76 $default_values['feedback['.$key.']'] = $answer->feedback;
77 $key++;
78 }
79 }
80 $default_values['single'] = $question->options->single;
81 $default_values['shuffleanswers'] = $question->options->shuffleanswers;
82 $default_values['correctfeedback'] = $question->options->correctfeedback;
83 $default_values['partiallycorrectfeedback'] = $question->options->partiallycorrectfeedback;
84 $default_values['overallincorrectfeedback'] = $question->options->overallincorrectfeedback;
85 $question = (object)((array)$question + $default_values);
86 }
87 parent::set_defaults($question);
88 }
89
90 function qtype() {
91 return 'calculated';
92 }
93
94 function validation($data){
95 $errors = array();
96 $answers = $data['answer'];
97 $answercount = 0;
98 foreach ($answers as $answer){
99 $trimmedanswer = trim($answer);
100 if (!empty($trimmedanswer)){
101 $answercount++;
102 }
103 }
104 if ($answercount==0){
105 $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_calculated', 2);
106 $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_calculated', 2);
107 } elseif ($answercount==1){
108 $errors['answer[1]'] = get_string('notenoughanswers', 'qtype_calculated', 2);
109
110 }
111 return $errors;
112 }
113}
114?>