MDL-27505 Remove the ou-specific question types from the main branch.
[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 */
44 var $question;
45 /**
46 * Reference to question type object
47 *
48 * @var question_dataset_dependent_questiontype
49 */
50 var $qtypeobj;
51 /**
52 * Add question-type specific form fields.
53 *
54 * @param MoodleQuickForm $mform the form being built.
55 */
56 function question_dataset_dependent_definitions_form($submiturl, $question){
315efce7 57 global $QTYPES, $DB;
8fc3e643 58 $this->question = $question;
59 $this->qtypeobj =& $QTYPES[$this->question->qtype];
4454447d
PS
60 // Validate the question category.
61 if (!$category = $DB->get_record('question_categories', array('id' => $question->category))) {
62 print_error('categorydoesnotexist', 'question', $returnurl);
63 }
315efce7 64 $this->category = $category;
65 $this->categorycontext = get_context_instance_by_id($category->contextid);
8fc3e643 66 parent::moodleform($submiturl);
67 }
c7df5006
TH
68
69 protected function definition() {
60b5ecd3 70 global $SESSION;
8fc3e643 71 $mform =& $this->_form;
8fc3e643 72 $possibledatasets = $this->qtypeobj->find_dataset_names($this->question->questiontext);
73 $mandatorydatasets = array();
60b5ecd3 74 if (isset($this->question->options->answers)){
75 foreach ($this->question->options->answers as $answer) {
76 $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer->answer);
77 }
78 }else{
fbe2cfea 79 foreach ($SESSION->calculated->questionform->answers as $answer){
60b5ecd3 80 $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer);
81 }
8fc3e643 82 }
60b5ecd3 83
8fc3e643 84 $key = 0;
92e2ed8f 85 $datadefscat= array();
86 $datadefscat = $this->qtypeobj->get_dataset_definitions_category($this->question);
8fc3e643 87 $datasetmenus = array();
5940864e 88 $label = "<div class='mdl-align'>".get_string('datasetrole', 'qtype_calculated')."</div>";
79bb7202 89 $mform->addElement('html', $label);// explaining the role of datasets so other strings can be shortened
8ef9f8ea 90 $mform->addElement('header', 'mandatoryhdr', get_string('mandatoryhdr', 'qtype_calculated'));
5940864e 91 $labelsharedwildcard = get_string('sharedwildcard', 'qtype_calculated');
92e2ed8f 92
8fc3e643 93 foreach ($mandatorydatasets as $datasetname) {
94 if (!isset($datasetmenus[$datasetname])) {
95 list($options, $selected) =
96 $this->qtypeobj->dataset_options($this->question, $datasetname);
97 unset($options['0']); // Mandatory...
5e8a85aa 98 $label = get_string("wildcard", "qtype_calculated"). " <strong>$datasetname</strong> ";
8fc3e643 99 $mform->addElement('select', "dataset[$key]", $label, $options);
92e2ed8f 100 if (isset($datadefscat[$datasetname])){
d90b016b 101 $mform->addElement('static', "there is a category", get_string('sharedwildcard', 'qtype_calculated',$datasetname ), get_string('dataitemdefined','qtype_calculated', $datadefscat[$datasetname]));
92e2ed8f 102 }
8fc3e643 103 $mform->setDefault("dataset[$key]", $selected);
104 $datasetmenus[$datasetname]='';
105 $key++;
106 }
107 }
8ef9f8ea 108 $mform->addElement('header', 'possiblehdr', get_string('possiblehdr', 'qtype_calculated'));
79bb7202 109
92e2ed8f 110
8fc3e643 111 foreach ($possibledatasets as $datasetname) {
112 if (!isset($datasetmenus[$datasetname])) {
113 list($options, $selected) =
92e2ed8f 114 $this->qtypeobj->dataset_options($this->question, $datasetname,false);
5e8a85aa 115 $label = get_string("wildcard", "qtype_calculated"). " <strong>$datasetname</strong> ";
8fc3e643 116 $mform->addElement('select', "dataset[$key]", $label, $options);
92e2ed8f 117 // $mform->addRule("dataset[$key]", null, 'required', null, 'client');
118 if (isset($datadefscat[$datasetname])){
d90b016b 119 $mform->addElement('static', "there is a category", get_string('sharedwildcard', 'qtype_calculated',$datasetname ), get_string('dataitemdefined',"qtype_calculated", $datadefscat[$datasetname]));
92e2ed8f 120 }
79bb7202 121
122 // $selected ="0";
8fc3e643 123 $mform->setDefault("dataset[$key]", $selected);
124 $datasetmenus[$datasetname]='';
125 $key++;
126 }
127 }
1bf9a631 128 // temporary strings
d90b016b 129 // temporary strings
8ef9f8ea 130 $mform->addElement('header', 'synchronizehdr', get_string('synchronize', 'qtype_calculated'));
d90b016b
PP
131 $mform->addElement('radio', 'synchronize', '', get_string('synchronizeno', 'qtype_calculated'),0);
132 $mform->addElement('radio', 'synchronize', '', get_string('synchronizeyes', 'qtype_calculated'),1);
133 $mform->addElement('radio', 'synchronize', '', get_string('synchronizeyesdisplay', 'qtype_calculated'),2);
f79a4158 134 if (isset($this->question->options)&& isset($this->question->options->synchronize) ){
135 $mform->setDefault("synchronize", $this->question->options->synchronize);
136 } else {
137 $mform->setDefault("synchronize", 0 );
138 }
139
a8d2a373 140 $this->add_action_buttons(false, get_string('nextpage', 'qtype_calculated'));
60b5ecd3 141
142
143 //hidden elements
144 $mform->addElement('hidden', 'returnurl');
f065391d
PP
145 $mform->setType('returnurl', PARAM_LOCALURL);
146 $mform->setDefault('returnurl', 0);
60b5ecd3 147 $mform->addElement('hidden', 'id');
79bb7202 148 $mform->setType('id', PARAM_INT);
149
315efce7 150 $mform->addElement('hidden', 'category');
151 $mform->setType('category', PARAM_RAW);
152 $mform->setDefault('category', array('contexts' => array($this->categorycontext)));
aeb15530 153
271e6dec 154 $mform->addElement('hidden', 'courseid');
155 $mform->setType('courseid', PARAM_INT);
156 $mform->setDefault('courseid', 0);
79bb7202 157
271e6dec 158 $mform->addElement('hidden', 'cmid');
159 $mform->setType('cmid', PARAM_INT);
160 $mform->setDefault('cmid', 0);
79bb7202 161
60b5ecd3 162 $mform->setType('id', PARAM_INT);
163 $mform->addElement('hidden', 'wizard', 'datasetitems');
164 $mform->setType('wizard', PARAM_ALPHA);
8fc3e643 165 }
ab8b5142
PP
166/* function set_data($question) {
167 $formdata = array();
168 $fromform = new stdClass();
169 $default_values['synchronize'] = $question->options->synchronize ;
170 $question = (object)((array)$question + $default_values);
171 }*/
a78890d5 172 function validation($data, $files) {
173 $errors = parent::validation($data, $files);
92e2ed8f 174 $datasets = $data['dataset'];
175 $countvalid = 0 ;
176 foreach ($datasets as $key => $dataset){
177 if ($dataset !="0") {
178 $countvalid++;
179 }
180 }
181 if (!$countvalid){
182 foreach ($datasets as $key => $dataset){
5940864e 183 $errors['dataset['.$key.']'] = get_string('atleastonerealdataset', 'qtype_calculated');
92e2ed8f 184 }
185 }
186 return $errors;
187 }
8fc3e643 188
189}
aeb15530 190