Merge branch 'MDL-67513-m310' of https://github.com/NeillM/moodle into MOODLE_310_STABLE
[moodle.git] / question / type / match / edit_match_form.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
17 /**
18  * Defines the editing form for the match question type.
19  *
20  * @package   qtype_match
21  * @copyright 2007 Jamie Pratt me@jamiep.org
22  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
26 defined('MOODLE_INTERNAL') || die();
29 /**
30  * Match question type editing form definition.
31  *
32  * @copyright 2007 Jamie Pratt me@jamiep.org
33  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34  */
35 class qtype_match_edit_form extends question_edit_form {
37     protected function get_per_answer_fields($mform, $label, $gradeoptions,
38             &$repeatedoptions, &$answersoption) {
39         $mform->addElement('static', 'answersinstruct',
40                 get_string('availablechoices', 'qtype_match'),
41                 get_string('filloutthreeqsandtwoas', 'qtype_match'));
43         $repeated = array();
44         $repeated[] = $mform->createElement('editor', 'subquestions',
45                 $label, array('rows'=>3), $this->editoroptions);
46         $repeated[] = $mform->createElement('text', 'subanswers',
47                 get_string('answer', 'question'), array('size' => 50, 'maxlength' => 255));
48         $repeatedoptions['subquestions']['type'] = PARAM_RAW;
49         $repeatedoptions['subanswers']['type'] = PARAM_TEXT;
50         $answersoption = 'subquestions';
51         return $repeated;
52     }
54     /**
55      * Add question-type specific form fields.
56      *
57      * @param object $mform the form being built.
58      */
59     protected function definition_inner($mform) {
60         $mform->addElement('advcheckbox', 'shuffleanswers',
61                 get_string('shuffle', 'qtype_match'), null, null, array(0, 1));
62         $mform->addHelpButton('shuffleanswers', 'shuffle', 'qtype_match');
63         $mform->setDefault('shuffleanswers', 1);
65         $this->add_per_answer_fields($mform, get_string('questionno', 'question', '{no}'), 0);
67         $this->add_combined_feedback_fields(true);
68         $this->add_interactive_settings(true, true);
69     }
71     /**
72      * Language string to use for 'Add {no} more {whatever we call answers}'.
73      */
74     protected function get_more_choices_string() {
75         return get_string('blanksforxmorequestions', 'qtype_match');
76     }
78     protected function data_preprocessing($question) {
79         $question = parent::data_preprocessing($question);
80         $question = $this->data_preprocessing_combined_feedback($question, true);
81         $question = $this->data_preprocessing_hints($question, true, true);
83         if (empty($question->options)) {
84             return $question;
85         }
87         $question->shuffleanswers = $question->options->shuffleanswers;
89         $key = 0;
90         foreach ($question->options->subquestions as $subquestion) {
91             $question->subanswers[$key] = $subquestion->answertext;
93             $draftid = file_get_submitted_draft_itemid('subquestions[' . $key . ']');
94             $question->subquestions[$key] = array();
95             $question->subquestions[$key]['text'] = file_prepare_draft_area($draftid,
96                     $this->context->id, 'qtype_match', 'subquestion',
97                     !empty($subquestion->id) ? (int) $subquestion->id : null,
98                     $this->fileoptions, $subquestion->questiontext);
99             $question->subquestions[$key]['format'] = $subquestion->questiontextformat;
100             $question->subquestions[$key]['itemid'] = $draftid;
101             $key++;
102         }
104         return $question;
105     }
107     public function validation($data, $files) {
108         $errors = parent::validation($data, $files);
109         $answers = $data['subanswers'];
110         $questions = $data['subquestions'];
111         $questioncount = 0;
112         $answercount = 0;
113         foreach ($questions as $key => $question) {
114             $trimmedquestion = trim($question['text']);
115             $trimmedanswer = trim($answers[$key]);
116             if ($trimmedquestion != '') {
117                 $questioncount++;
118             }
119             if ($trimmedanswer != '' || $trimmedquestion != '') {
120                 $answercount++;
121             }
122             if ($trimmedquestion != '' && $trimmedanswer == '') {
123                 $errors['subanswers['.$key.']'] =
124                         get_string('nomatchinganswerforq', 'qtype_match', $trimmedquestion);
125             }
126         }
127         $numberqanda = new stdClass();
128         $numberqanda->q = 2;
129         $numberqanda->a = 3;
130         if ($questioncount < 1) {
131             $errors['subquestions[0]'] =
132                     get_string('notenoughqsandas', 'qtype_match', $numberqanda);
133         }
134         if ($questioncount < 2) {
135             $errors['subquestions[1]'] =
136                     get_string('notenoughqsandas', 'qtype_match', $numberqanda);
137         }
138         if ($answercount < 3) {
139             $errors['subanswers[2]'] =
140                     get_string('notenoughqsandas', 'qtype_match', $numberqanda);
141         }
142         return $errors;
143     }
145     public function qtype() {
146         return 'match';
147     }