MDL-20636 Actually, passing to get_question summary is silly. Revert.
[moodle.git] / question / type / missingtype / edit_missingtype_form.php
CommitLineData
aeb15530 1<?php
71534002 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
830e47a8 8 * @package questionbank
9 * @subpackage questiontypes
71534002 10 */
11
12/**
13 * missingtype editing form definition.
14 */
15class question_edit_missingtype_form extends question_edit_form {
16 /**
17 * Add question-type specific form fields.
18 *
19 * @param object $mform the form being built.
20 */
21 function definition_inner(&$mform) {
22 $creategrades = get_grade_options();
2aef1fe5 23 $this->add_per_answer_fields($mform, get_string('choiceno', 'qtype_multichoice', '{no}'),
24 $creategrades->gradeoptionsfull);
71534002 25 }
26
27 function set_data($question) {
28 if (isset($question->options)){
29 $answers = $question->options->answers;
1a861d24 30 $default_values = array();
31 if (is_array($answers) && count($answers)) {
71534002 32 $key = 0;
33 foreach ($answers as $answer){
34 $default_values['answer['.$key.']'] = $answer->answer;
35 $default_values['fraction['.$key.']'] = $answer->fraction;
36 $default_values['feedback['.$key.']'] = $answer->feedback;
37 $key++;
38 }
39 }
40 $question = (object)((array)$question + $default_values);
41 }
42 parent::set_data($question);
43 }
44
45 function qtype() {
46 return 'missingtype';
47 }
48
a78890d5 49 function validation($data, $files) {
fe93ba83 50 $errors = parent::validation($data, $files);
71534002 51 $answers = $data['answer'];
52 $answercount = 0;
53
54 $totalfraction = 0;
55 $maxfraction = -1;
56
57 foreach ($answers as $key => $answer){
58 //check no of choices
59 $trimmedanswer = trim($answer);
60 if (!empty($trimmedanswer)){
61 $answercount++;
62 }
63 //check grades
64 if ($answer != '') {
65 if ($data['fraction'][$key] > 0) {
66 $totalfraction += $data['fraction'][$key];
67 }
68 if ($data['fraction'][$key] > $maxfraction) {
69 $maxfraction = $data['fraction'][$key];
70 }
71 }
72 }
73
74 if ($answercount==0){
75 $errors['answer[0]'] = get_string('notenoughanswers', 'qtype_multichoice', 1);
76 }
77
78
79 $totalfraction = round($totalfraction, 2);
80 if ($totalfraction != 1) {
81 $totalfraction = $totalfraction * 100;
82 $errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice', $totalfraction);
83 }
84
85 return $errors;
86 }
87}
aeb15530 88