Merge branch 'MDL-67513-m310' of https://github.com/NeillM/moodle into MOODLE_310_STABLE
[moodle.git] / question / type / match / edit_match_form.php
CommitLineData
aeb15530 1<?php
b04a4319
TH
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/>.
16
271ffe3f 17/**
18 * Defines the editing form for the match question type.
19 *
9887aaeb
TH
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
271ffe3f 23 */
24
b04a4319 25
a17b297d
TH
26defined('MOODLE_INTERNAL') || die();
27
28
271ffe3f 29/**
b04a4319
TH
30 * Match question type editing form definition.
31 *
9887aaeb
TH
32 * @copyright 2007 Jamie Pratt me@jamiep.org
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
271ffe3f 34 */
6e34cd29 35class qtype_match_edit_form extends question_edit_form {
2aef1fe5 36
3758786f
TH
37 protected function get_per_answer_fields($mform, $label, $gradeoptions,
38 &$repeatedoptions, &$answersoption) {
c7218aef
CC
39 $mform->addElement('static', 'answersinstruct',
40 get_string('availablechoices', 'qtype_match'),
41 get_string('filloutthreeqsandtwoas', 'qtype_match'));
42
2aef1fe5 43 $repeated = array();
3758786f 44 $repeated[] = $mform->createElement('editor', 'subquestions',
c7218aef 45 $label, array('rows'=>3), $this->editoroptions);
3758786f 46 $repeated[] = $mform->createElement('text', 'subanswers',
cbe0a1f6 47 get_string('answer', 'question'), array('size' => 50, 'maxlength' => 255));
2aef1fe5 48 $repeatedoptions['subquestions']['type'] = PARAM_RAW;
49 $repeatedoptions['subanswers']['type'] = PARAM_TEXT;
50 $answersoption = 'subquestions';
51 return $repeated;
52 }
53
271ffe3f 54 /**
55 * Add question-type specific form fields.
56 *
57 * @param object $mform the form being built.
58 */
93cadb1e 59 protected function definition_inner($mform) {
3758786f
TH
60 $mform->addElement('advcheckbox', 'shuffleanswers',
61 get_string('shuffle', 'qtype_match'), null, null, array(0, 1));
5fcefc97 62 $mform->addHelpButton('shuffleanswers', 'shuffle', 'qtype_match');
04786f08 63 $mform->setDefault('shuffleanswers', 1);
271ffe3f 64
5e8a85aa 65 $this->add_per_answer_fields($mform, get_string('questionno', 'question', '{no}'), 0);
93cadb1e
TH
66
67 $this->add_combined_feedback_fields(true);
68 $this->add_interactive_settings(true, true);
271ffe3f 69 }
70
baa6159b
TH
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 }
77
c7df5006 78 protected function data_preprocessing($question) {
93cadb1e
TH
79 $question = parent::data_preprocessing($question);
80 $question = $this->data_preprocessing_combined_feedback($question, true);
81 $question = $this->data_preprocessing_hints($question, true, true);
82
83 if (empty($question->options)) {
84 return $question;
85 }
fe6ce234 86
42e6506e 87 $question->shuffleanswers = $question->options->shuffleanswers;
fe6ce234 88
93cadb1e 89 $key = 0;
3758786f 90 foreach ($question->options->subquestions as $subquestion) {
93cadb1e
TH
91 $question->subanswers[$key] = $subquestion->answertext;
92
93 $draftid = file_get_submitted_draft_itemid('subquestions[' . $key . ']');
94 $question->subquestions[$key] = array();
9887aaeb
TH
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);
93cadb1e
TH
99 $question->subquestions[$key]['format'] = $subquestion->questiontextformat;
100 $question->subquestions[$key]['itemid'] = $draftid;
101 $key++;
271ffe3f 102 }
271ffe3f 103
93cadb1e 104 return $question;
271ffe3f 105 }
106
93cadb1e 107 public function validation($data, $files) {
fe93ba83 108 $errors = parent::validation($data, $files);
271ffe3f 109 $answers = $data['subanswers'];
110 $questions = $data['subquestions'];
111 $questioncount = 0;
6cfa5ec1 112 $answercount = 0;
3758786f 113 foreach ($questions as $key => $question) {
fe6ce234 114 $trimmedquestion = trim($question['text']);
271ffe3f 115 $trimmedanswer = trim($answers[$key]);
3758786f 116 if ($trimmedquestion != '') {
271ffe3f 117 $questioncount++;
118 }
3758786f 119 if ($trimmedanswer != '' || $trimmedquestion != '') {
6cfa5ec1 120 $answercount++;
121 }
3758786f
TH
122 if ($trimmedquestion != '' && $trimmedanswer == '') {
123 $errors['subanswers['.$key.']'] =
124 get_string('nomatchinganswerforq', 'qtype_match', $trimmedquestion);
271ffe3f 125 }
126 }
0ff4bd08 127 $numberqanda = new stdClass();
6cfa5ec1 128 $numberqanda->q = 2;
129 $numberqanda->a = 3;
3758786f
TH
130 if ($questioncount < 1) {
131 $errors['subquestions[0]'] =
132 get_string('notenoughqsandas', 'qtype_match', $numberqanda);
6cfa5ec1 133 }
3758786f
TH
134 if ($questioncount < 2) {
135 $errors['subquestions[1]'] =
136 get_string('notenoughqsandas', 'qtype_match', $numberqanda);
6cfa5ec1 137 }
3758786f
TH
138 if ($answercount < 3) {
139 $errors['subanswers[2]'] =
140 get_string('notenoughqsandas', 'qtype_match', $numberqanda);
271ffe3f 141 }
142 return $errors;
143 }
93cadb1e 144
c7df5006 145 public function qtype() {
93cadb1e
TH
146 return 'match';
147 }
271ffe3f 148}