Merge branch 'MDL-70248-310' of https://github.com/HuongNV13/moodle into MOODLE_310_S...
[moodle.git] / question / type / calculated / datasetdefinitions_form.php
CommitLineData
8fc3e643 1<?php
d3603157
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
830e47a8 17/**
d3603157
TH
18 * Defines the editing form for the calculated question data set definitions.
19 *
20 * @package qtype
21 * @subpackage calculated
22 * @copyright 2007 Jamie Pratt me@jamiep.org
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
830e47a8 24 */
25
d3603157 26
a17b297d
TH
27defined('MOODLE_INTERNAL') || die();
28
72553162
TH
29require_once($CFG->dirroot . '/question/type/edit_question_form.php');
30
a17b297d 31
d3603157
TH
32/**
33 * Calculated question data set definitions editing form definition.
34 *
35 * @copyright 2007 Jamie Pratt me@jamiep.org
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 */
72553162 38class question_dataset_dependent_definitions_form extends question_wizard_form {
8fc3e643 39 /**
40 * Question object with options and answers already loaded by get_question_options
41 * Be careful how you use this it is needed sometimes to set up the structure of the
42 * form in definition_inner but data is always loaded into the form with set_defaults.
43 *
44 * @var object
45 */
f184c65c 46 protected $question;
8fc3e643 47 /**
48 * Reference to question type object
49 *
50 * @var question_dataset_dependent_questiontype
51 */
f184c65c 52 protected $qtypeobj;
8fc3e643 53 /**
54 * Add question-type specific form fields.
55 *
56 * @param MoodleQuickForm $mform the form being built.
57 */
f184c65c
TH
58 public function __construct($submiturl, $question) {
59 global $DB;
8fc3e643 60 $this->question = $question;
18f9b2d2 61 $this->qtypeobj = question_bank::get_qtype($this->question->qtype);
4454447d 62 // Validate the question category.
f184c65c
TH
63 if (!$category = $DB->get_record('question_categories',
64 array('id' => $question->category))) {
4454447d
PS
65 print_error('categorydoesnotexist', 'question', $returnurl);
66 }
315efce7 67 $this->category = $category;
d197ea43 68 $this->categorycontext = context::instance_by_id($category->contextid);
f184c65c 69 parent::__construct($submiturl);
8fc3e643 70 }
c7df5006
TH
71
72 protected function definition() {
60b5ecd3 73 global $SESSION;
d6b8e192 74
f184c65c 75 $mform = $this->_form;
d6b8e192
TH
76 $mform->setDisableShortforms();
77
8fc3e643 78 $possibledatasets = $this->qtypeobj->find_dataset_names($this->question->questiontext);
79 $mandatorydatasets = array();
f184c65c 80 if (isset($this->question->options->answers)) {
60b5ecd3 81 foreach ($this->question->options->answers as $answer) {
82 $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer->answer);
83 }
f184c65c
TH
84 } else {
85 foreach ($SESSION->calculated->questionform->answers as $answer) {
60b5ecd3 86 $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer);
87 }
8fc3e643 88 }
60b5ecd3 89
8fc3e643 90 $key = 0;
92e2ed8f 91 $datadefscat= array();
92 $datadefscat = $this->qtypeobj->get_dataset_definitions_category($this->question);
8fc3e643 93 $datasetmenus = array();
5940864e 94 $label = "<div class='mdl-align'>".get_string('datasetrole', 'qtype_calculated')."</div>";
3d9645ae 95 // Explaining the role of datasets so other strings can be shortened.
f184c65c
TH
96 $mform->addElement('html', $label);
97 $mform->addElement('header', 'mandatoryhdr',
98 get_string('mandatoryhdr', 'qtype_calculated'));
5940864e 99 $labelsharedwildcard = get_string('sharedwildcard', 'qtype_calculated');
92e2ed8f 100
8fc3e643 101 foreach ($mandatorydatasets as $datasetname) {
102 if (!isset($datasetmenus[$datasetname])) {
103 list($options, $selected) =
104 $this->qtypeobj->dataset_options($this->question, $datasetname);
105 unset($options['0']); // Mandatory...
6ef90fde 106 $label = get_string('wildcard', 'qtype_calculated', $datasetname);
f4fe3968 107 $mform->addElement('select', "dataset[{$key}]", $label, $options);
f184c65c
TH
108 if (isset($datadefscat[$datasetname])) {
109 $mform->addElement('static', "there is a category",
110 get_string('sharedwildcard', 'qtype_calculated', $datasetname),
111 get_string('dataitemdefined', 'qtype_calculated',
112 $datadefscat[$datasetname]));
113 }
f4fe3968 114 $mform->setDefault("dataset[{$key}]", $selected);
f184c65c 115 $datasetmenus[$datasetname] = '';
8fc3e643 116 $key++;
117 }
118 }
f184c65c 119 $mform->addElement('header', 'possiblehdr', get_string('possiblehdr', 'qtype_calculated'));
92e2ed8f 120
8fc3e643 121 foreach ($possibledatasets as $datasetname) {
122 if (!isset($datasetmenus[$datasetname])) {
f184c65c
TH
123 list($options, $selected) = $this->qtypeobj->dataset_options(
124 $this->question, $datasetname, false);
6ef90fde 125 $label = get_string('wildcard', 'qtype_calculated', $datasetname);
f4fe3968 126 $mform->addElement('select', "dataset[{$key}]", $label, $options);
f184c65c
TH
127 if (isset($datadefscat[$datasetname])) {
128 $mform->addElement('static', "there is a category",
129 get_string('sharedwildcard', 'qtype_calculated', $datasetname),
130 get_string('dataitemdefined', 'qtype_calculated',
131 $datadefscat[$datasetname]));
132 }
79bb7202 133
f4fe3968 134 $mform->setDefault("dataset[{$key}]", $selected);
f184c65c 135 $datasetmenus[$datasetname] = '';
8fc3e643 136 $key++;
137 }
138 }
3d9645ae 139 // Temporary strings.
f184c65c
TH
140 $mform->addElement('header', 'synchronizehdr',
141 get_string('synchronize', 'qtype_calculated'));
142 $mform->addElement('radio', 'synchronize', '',
143 get_string('synchronizeno', 'qtype_calculated'), 0);
144 $mform->addElement('radio', 'synchronize', '',
145 get_string('synchronizeyes', 'qtype_calculated'), 1);
146 $mform->addElement('radio', 'synchronize', '',
147 get_string('synchronizeyesdisplay', 'qtype_calculated'), 2);
148 if (isset($this->question->options) &&
149 isset($this->question->options->synchronize)) {
150 $mform->setDefault('synchronize', $this->question->options->synchronize);
f79a4158 151 } else {
f184c65c 152 $mform->setDefault('synchronize', 0);
f79a4158 153 }
154
a8d2a373 155 $this->add_action_buttons(false, get_string('nextpage', 'qtype_calculated'));
60b5ecd3 156
72553162 157 $this->add_hidden_fields();
79bb7202 158
315efce7 159 $mform->addElement('hidden', 'category');
72553162 160 $mform->setType('category', PARAM_SEQUENCE);
79bb7202 161
60b5ecd3 162 $mform->addElement('hidden', 'wizard', 'datasetitems');
163 $mform->setType('wizard', PARAM_ALPHA);
8fc3e643 164 }
f184c65c
TH
165
166 public function validation($data, $files) {
a78890d5 167 $errors = parent::validation($data, $files);
92e2ed8f 168 $datasets = $data['dataset'];
f184c65c
TH
169 $countvalid = 0;
170 foreach ($datasets as $key => $dataset) {
171 if ($dataset != '0') {
92e2ed8f 172 $countvalid++;
173 }
174 }
f184c65c
TH
175 if (!$countvalid) {
176 foreach ($datasets as $key => $dataset) {
177 $errors['dataset['.$key.']'] =
178 get_string('atleastonerealdataset', 'qtype_calculated');
92e2ed8f 179 }
f184c65c 180 }
92e2ed8f 181 return $errors;
182 }
8fc3e643 183}