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