migrated 'missingtype' questoin creation forms to use formslib
[moodle.git] / question / type / missingtype / edit_missingtype_form.php
CommitLineData
71534002 1<?php
2/**
3 * Defines the editing form for the missingtype 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 * missingtype editing form definition.
13 */
14class question_edit_missingtype_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 $creategrades = get_grade_options();
22 $gradeoptions = $creategrades->gradeoptionsfull;
23 $repeated = array();
24 $repeated[] =& $mform->createElement('header', 'choicehdr', get_string('choiceno', 'qtype_multichoice', '{no}'));
25 $repeated[] =& $mform->createElement('text', 'answer', get_string('answer', 'quiz'));
26 $repeated[] =& $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions);
27 $repeated[] =& $mform->createElement('htmleditor', 'feedback', get_string('feedback', 'quiz'));
28
29 if (isset($this->question->options)){
30 $countanswers = count($this->question->options->answers);
31 } else {
32 $countanswers = 0;
33 }
34 $repeatsatstart = (QUESTION_NUMANS_START > ($countanswers + QUESTION_NUMANS_ADD))?
35 QUESTION_NUMANS_START : ($countanswers + QUESTION_NUMANS_ADD);
36 $repeatedoptions = array();
37 $repeatedoptions['answer']['type'] = PARAM_NOTAGS;//text with no multilang support
38 $repeatedoptions['fraction']['default'] = 0;
39 $this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', QUESTION_NUMANS_ADD, get_string('addmorechoiceblanks', 'qtype_multichoice'));
40 }
41
42 function set_data($question) {
43 if (isset($question->options)){
44 $answers = $question->options->answers;
45 if (count($answers)) {
46 $key = 0;
47 foreach ($answers as $answer){
48 $default_values['answer['.$key.']'] = $answer->answer;
49 $default_values['fraction['.$key.']'] = $answer->fraction;
50 $default_values['feedback['.$key.']'] = $answer->feedback;
51 $key++;
52 }
53 }
54 $question = (object)((array)$question + $default_values);
55 }
56 parent::set_data($question);
57 }
58
59 function qtype() {
60 return 'missingtype';
61 }
62
63 function validation($data){
64 $errors = array();
65 $answers = $data['answer'];
66 $answercount = 0;
67
68 $totalfraction = 0;
69 $maxfraction = -1;
70
71 foreach ($answers as $key => $answer){
72 //check no of choices
73 $trimmedanswer = trim($answer);
74 if (!empty($trimmedanswer)){
75 $answercount++;
76 }
77 //check grades
78 if ($answer != '') {
79 if ($data['fraction'][$key] > 0) {
80 $totalfraction += $data['fraction'][$key];
81 }
82 if ($data['fraction'][$key] > $maxfraction) {
83 $maxfraction = $data['fraction'][$key];
84 }
85 }
86 }
87
88 if ($answercount==0){
89 $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_multichoice', 1);
90 }
91
92
93 $totalfraction = round($totalfraction, 2);
94 if ($totalfraction != 1) {
95 $totalfraction = $totalfraction * 100;
96 $errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice', $totalfraction);
97 }
98
99 return $errors;
100 }
101}
102?>