MDL-15680 Began implementing a simple YUI treeview of the grade items in their catego...
[moodle.git] / question / type / calculated / datasetitems_form.php
CommitLineData
8fc3e643 1<?php
2class question_dataset_dependent_items_form extends moodleform {
3 /**
4 * Question object with options and answers already loaded by get_question_options
5 * Be careful how you use this it is needed sometimes to set up the structure of the
6 * form in definition_inner but data is always loaded into the form with set_defaults.
7 *
8 * @var object
9 */
f34488b2 10 public $question;
8fc3e643 11 /**
12 * Reference to question type object
13 *
14 * @var question_dataset_dependent_questiontype
15 */
f34488b2 16 public $qtypeobj;
60b5ecd3 17
f34488b2 18 public $datasetdefs;
60b5ecd3 19
f34488b2 20 public $maxnumber = -1;
60b5ecd3 21
f34488b2 22 public $regenerate;
60b5ecd3 23
f34488b2 24 public $noofitems;
8fc3e643 25 /**
26 * Add question-type specific form fields.
27 *
28 * @param MoodleQuickForm $mform the form being built.
29 */
60b5ecd3 30 function question_dataset_dependent_items_form($submiturl, $question, $regenerate){
f34488b2 31 global $QTYPES, $SESSION, $CFG, $DB;
60b5ecd3 32 $this->regenerate = $regenerate;
8fc3e643 33 $this->question = $question;
34 $this->qtypeobj =& $QTYPES[$this->question->qtype];
60b5ecd3 35 //get the dataset defintions for this question
36 if (empty($question->id)) {
fbe2cfea 37 $this->datasetdefs = $this->qtypeobj->get_dataset_definitions($question->id, $SESSION->calculated->definitionform->dataset);
60b5ecd3 38 } else {
39 if (empty($question->options)) {
40 $this->get_question_options($question);
41 }
42 $this->datasetdefs = $this->qtypeobj->get_dataset_definitions($question->id, array());
43 }
44
45 foreach ($this->datasetdefs as $datasetdef) {
46
47 // Get maxnumber
48 if ($this->maxnumber == -1 || $datasetdef->itemcount < $this->maxnumber) {
49 $this->maxnumber = $datasetdef->itemcount;
50 }
51 }
52 foreach ($this->datasetdefs as $defid => $datasetdef) {
53 if (isset($datasetdef->id)) {
f34488b2 54 $this->datasetdefs[$defid]->items = $DB->get_records_sql( // Use number as key!!
60b5ecd3 55 " SELECT itemnumber, definition, id, value
f34488b2 56 FROM {question_dataset_items}
57 WHERE definition = ? ", array($datasetdef->id));
60b5ecd3 58 }
59 }
8fc3e643 60 parent::moodleform($submiturl);
61 }
62 function definition() {
63 $mform =& $this->_form;
60b5ecd3 64 $strquestionlabel = $this->qtypeobj->comment_header($this->question);
60b5ecd3 65 if ($this->maxnumber != -1){
66 $this->noofitems = $this->maxnumber;
67 } else {
68 $this->noofitems = 0;
8fc3e643 69 }
60b5ecd3 70//------------------------------------------------------------------------------------------------------------------------------
38a2212c 71 $mform->addElement('submit', 'updatedatasets', get_string('updatedatasetparam', 'qtype_datasetdependent'));
60b5ecd3 72 $mform->addElement('header', 'additemhdr', get_string('itemtoadd', 'qtype_datasetdependent'));
73 $idx = 1;
a8d2a373 74 $j = (($this->noofitems) * count($this->datasetdefs))+1;
60b5ecd3 75 foreach ($this->datasetdefs as $defkey => $datasetdef){
76 $mform->addElement('text', "number[$j]", get_string('param', 'qtype_datasetdependent', $datasetdef->name));
87cd4f54 77 $mform->setType("number[$j]", PARAM_NUMBER);
f34488b2 78 $this->qtypeobj->custom_generator_tools_part($mform, $idx, $j);
60b5ecd3 79 $idx++;
a8d2a373 80 $mform->addElement('hidden', "definition[$j]");
81 $mform->addElement('hidden', "itemid[$j]");
60b5ecd3 82 $mform->addElement('static', "divider[$j]", '', '<hr />');
a8d2a373 83 $j++;
60b5ecd3 84 }
85
86 if ('' != $strquestionlabel){
87 $mform->addElement('static', 'answercomment['.($this->noofitems+1).']', $strquestionlabel);
88 }
451373ed 89 $addremoveoptions = Array();
90 $addremoveoptions['1']='1';
79bb7202 91 for ($i=10; $i<=100 ; $i+=10){
451373ed 92 $addremoveoptions["$i"]="$i";
93 }
87cd4f54 94 $mform->addElement('header', 'additemhdr', get_string('add', 'moodle'));
451373ed 95 $mform->closeHeaderBefore('additemhdr');
60b5ecd3 96
8fc3e643 97 if ($this->qtypeobj->supports_dataset_item_generation()){
98 $radiogrp = array();
60b5ecd3 99 $radiogrp[] =& $mform->createElement('radio', 'nextpageparam[forceregeneration]', null, get_string('reuseifpossible', 'qtype_datasetdependent'), 0);
100 $radiogrp[] =& $mform->createElement('radio', 'nextpageparam[forceregeneration]', null, get_string('forceregeneration', 'qtype_datasetdependent'), 1);
451373ed 101 $mform->addGroup($radiogrp, 'forceregenerationgrp', get_string('nextitemtoadd', 'qtype_calculated'), "<br/>", false);
8fc3e643 102 }
103
60b5ecd3 104 $mform->addElement('submit', 'getnextbutton', get_string('getnextnow', 'qtype_datasetdependent'));
451373ed 105 $mform->addElement('static', "dividera", '', '<hr />');
106 $addgrp = array();
107 $addgrp[] =& $mform->createElement('submit', 'addbutton', get_string('add', 'moodle'));
108 $addgrp[] =& $mform->createElement('select', "selectadd", get_string('additem', 'qtype_datasetdependent'), $addremoveoptions);
109 $addgrp[] = & $mform->createElement('static',"stat","Items",get_string('item(s)', 'qtype_datasetdependent'));
110 $mform->addGroup($addgrp, 'addgrp', '', ' ', false);
111 $mform->addElement('static', "divideradd", '', '');
112 // $mform->closeHeaderBefore('divideradd');
113 if ($this->noofitems > 0) {
114 $mform->addElement('header', 'additemhdr', get_string('delete', 'moodle'));
115 $deletegrp = array();
116 $deletegrp[] =& $mform->createElement('submit', 'deletebutton', get_string('delete', 'moodle'));
117 $deletegrp[] =& $mform->createElement('select', "selectdelete", get_string('deleteitem', 'qtype_datasetdependent')."1", $addremoveoptions);
118 $deletegrp[] = & $mform->createElement('static',"stat","Items",get_string('lastitem(s)', 'qtype_datasetdependent'));
119 $mform->addGroup($deletegrp, 'deletegrp', '', ' ', false);
120 // $mform->addElement('static', "dividerdelete", '', '<hr />');
121 // $mform->closeHeaderBefore('dividerdelete');
122 } else {
123 $mform->addElement('static','warning','','<span class="error">'.get_string('youmustaddatleastoneitem', 'qtype_datasetdependent').'</span>');
124 }
8fc3e643 125
a8d2a373 126//------------------------------------------------------------------------------------------------------------------------------
127 $j = $this->noofitems * count($this->datasetdefs);
128 for ($i = $this->noofitems; $i >= 1 ; $i--){
129 $mform->addElement('header', '', get_string('itemno', 'qtype_datasetdependent', $i));
130 foreach ($this->datasetdefs as $defkey => $datasetdef){
131 $mform->addElement('text', "number[$j]", get_string('param', 'qtype_datasetdependent', $datasetdef->name));
87cd4f54 132 $mform->setType("number[$j]", PARAM_NUMBER);
a8d2a373 133 $mform->addElement('hidden', "itemid[$j]");
87cd4f54 134 $mform->setType("itemid[$j]", PARAM_INT);
a8d2a373 135
136 $mform->addElement('hidden', "definition[$j]");
87cd4f54 137 $mform->setType("definition[$j]", PARAM_NOTAGS);
a8d2a373 138
139 $j--;
140 }
141 if ('' != $strquestionlabel){
142 $repeated[] =& $mform->addElement('static', "answercomment[$i]", $strquestionlabel);
143 }
a8d2a373 144 }
a8d2a373 145
146//------------------------------------------------------------------------------------------------------------------------------
60b5ecd3 147 //non standard name for button element needed so not using add_action_buttons
a8d2a373 148 $mform->addElement('submit', 'backtoquiz', get_string('savechanges'));
149 $mform->closeHeaderBefore('backtoquiz');
8fc3e643 150
60b5ecd3 151 //hidden elements
79bb7202 152 $mform->addElement('hidden', 'id');
153 $mform->setType('id', PARAM_INT);
154
271e6dec 155 $mform->addElement('hidden', 'courseid');
156 $mform->setType('courseid', PARAM_INT);
157 $mform->setDefault('courseid', 0);
79bb7202 158
271e6dec 159 $mform->addElement('hidden', 'cmid');
160 $mform->setType('cmid', PARAM_INT);
161 $mform->setDefault('cmid', 0);
79bb7202 162
60b5ecd3 163 $mform->addElement('hidden', 'wizard', 'datasetitems');
164 $mform->setType('wizard', PARAM_ALPHA);
f34488b2 165
2dc8beaf 166 $mform->addElement('hidden', 'returnurl');
167 $mform->setType('returnurl', PARAM_LOCALURL);
168 $mform->setDefault('returnurl', 0);
60b5ecd3 169 }
8fc3e643 170
60b5ecd3 171 function set_data($question){
172 $formdata = array();
8fc3e643 173
60b5ecd3 174 //fill out all data sets and also the fields for the next item to add.
a8d2a373 175 $j = $this->noofitems * count($this->datasetdefs);
176 for ($itemnumber = $this->noofitems; $itemnumber >= 1; $itemnumber--){
60b5ecd3 177 $data = array();
178 foreach ($this->datasetdefs as $defid => $datasetdef){
179 if (isset($datasetdef->items[$itemnumber])){
180 $formdata["number[$j]"] = $datasetdef->items[$itemnumber]->value;
181 $formdata["definition[$j]"] = $defid;
182 $formdata["itemid[$j]"] = $datasetdef->items[$itemnumber]->id;
183 $data[$datasetdef->name] = $datasetdef->items[$itemnumber]->value;
184 }
a8d2a373 185 $j--;
60b5ecd3 186 }
187 $formdata['answercomment['.$itemnumber.']'] = $this->qtypeobj->comment_on_datasetitems($this->question, $data, $itemnumber);
188 }
8fc3e643 189
60b5ecd3 190 $formdata['nextpageparam[forceregeneration]'] = $this->regenerate;
451373ed 191 $formdata['selectdelete'] = '1';
192 $formdata['selectadd'] = '1';
a8d2a373 193 $j = $this->noofitems * count($this->datasetdefs)+1;
60b5ecd3 194 $data = array(); // data for comment_on_datasetitems later
195 //dataset generation dafaults
196 if ($this->qtypeobj->supports_dataset_item_generation()) {
197 $itemnumber = $this->noofitems+1;
198 foreach ($this->datasetdefs as $defid => $datasetdef){
199 $formdata["number[$j]"] = $this->qtypeobj->generate_dataset_item($datasetdef->options);
200 $formdata["definition[$j]"] = $defid;
201 $formdata["itemid[$j]"] =
202 isset($datasetdef->items[$itemnumber])?$datasetdef->items[$itemnumber]->id:0;
203 $data[$datasetdef->name] = $formdata["number[$j]"];
204 $j++;
205 }
8fc3e643 206 }
60b5ecd3 207
208 //existing records override generated data depending on radio element
a8d2a373 209 $j = $this->noofitems * count($this->datasetdefs)+1;
60b5ecd3 210 if (!$this->regenerate){
211 $idx = 1;
212 $itemnumber = $this->noofitems+1;
213 foreach ($this->datasetdefs as $defid => $datasetdef){
214 if (isset($datasetdef->items[$itemnumber])){
215 $formdata["number[$j]"] = $datasetdef->items[$itemnumber]->value;
216 $formdata["definition[$j]"] = $defid;
217 $formdata["itemid[$j]"] = $datasetdef->items[$itemnumber]->id;
218 $data[$datasetdef->name] = $datasetdef->items[$itemnumber]->value;
219 }
220 $j++;
221 }
222
223 }
224 //default answercomment will get ignored if answer element is not in the form.
225 $formdata['answercomment['.($this->noofitems+1).']'] = $this->qtypeobj->comment_on_datasetitems($this->question, $data, ($this->noofitems+1));
226
227 $formdata = $this->qtypeobj->custom_generator_set_data($this->datasetdefs, $formdata);
228
229 parent::set_data((object)($formdata + (array)$question));
8fc3e643 230 }
231
a78890d5 232 function validation($data, $files) {
60b5ecd3 233 $errors = array();
38a2212c 234 if (isset($data['backtoquiz']) && ($this->noofitems==0)){
451373ed 235 $errors['warning'] = get_string('warning', 'mnet');
60b5ecd3 236 }
237 return $errors;
238 }
239
240
8fc3e643 241}
f34488b2 242?>