|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00027 defined('MOODLE_INTERNAL') || die(); 00028 00029 require_once($CFG->dirroot . '/question/type/calculated/edit_calculated_form.php'); 00030 00031 00038 class qtype_calculatedsimple_edit_form extends qtype_calculated_edit_form { 00044 public $qtypeobj; 00045 00046 public $wildcarddisplay; 00047 00048 public $questiondisplay; 00049 00050 public $datasetdefs; 00051 00052 public $reload = false; 00053 00054 public $maxnumber = -1; 00055 00056 public $regenerate = true; 00057 00058 public $noofitems; 00059 00060 public $outsidelimit = false; 00061 00062 public $commentanswer = array(); 00063 00064 public $answer = array(); 00065 00066 public $nonemptyanswer = array(); 00067 00068 public $numbererrors = array(); 00069 00070 public $formdata = array(); 00071 00072 public function __construct($submiturl, $question, $category, $contexts, $formeditable = true) { 00073 $this->regenerate = true; 00074 $this->question = $question; 00075 00076 $this->qtypeobj = question_bank::get_qtype($this->question->qtype); 00077 // get the dataset definitions for this question 00078 // coming here everytime even when using a NoSubmitButton 00079 // so this will only set the values to the actual question database content 00080 // which is not what we want so this should be removed from here 00081 // get priority to paramdatasets 00082 00083 $this->reload = optional_param('reload', false, PARAM_BOOL); 00084 if (!$this->reload) { // use database data as this is first pass 00085 // question->id == 0 so no stored datasets 00086 if (!empty($question->id)) { 00087 00088 $this->datasetdefs = $this->qtypeobj->get_dataset_definitions( 00089 $question->id, array()); 00090 00091 if (!empty($this->datasetdefs)) { 00092 foreach ($this->datasetdefs as $defid => $datasetdef) { 00093 // first get the items in case their number does not correspond to itemcount 00094 if (isset($datasetdef->id)) { 00095 $this->datasetdefs[$defid]->items = 00096 $this->qtypeobj->get_database_dataset_items($datasetdef->id); 00097 if ($this->datasetdefs[$defid]->items != '') { 00098 $datasetdef->itemcount = count($this->datasetdefs[$defid]->items); 00099 } else { 00100 $datasetdef->itemcount = 0; 00101 } 00102 } 00103 // Get maxnumber 00104 if ($this->maxnumber == -1 || $datasetdef->itemcount < $this->maxnumber) { 00105 $this->maxnumber = $datasetdef->itemcount; 00106 } 00107 } 00108 } 00109 00110 $i = 0; 00111 foreach ($this->question->options->answers as $answer) { 00112 $this->answer[$i] = $answer; 00113 $i++; 00114 } 00115 $this->nonemptyanswer = $this->answer; 00116 } 00117 $datasettoremove = false; 00118 $newdatasetvalues = false; 00119 $newdataset = false; 00120 } else { 00121 // handle reload to get values from the form-elements 00122 // answers, datasetdefs and data_items. In any case the validation 00123 // step will warn the user of any error in settings the values. 00124 // Verification for the specific dataset values as the other parameters 00125 // unints, feeedback etc are handled elsewhere 00126 // handle request buttons : 00127 // 'analyzequestion' (Identify the wild cards {x..} present in answers) 00128 // 'addbutton' (create new set of datatitems) 00129 // 'updatedatasets' is handled automatically on each reload 00130 // The analyzequestion is done every time on reload 00131 // to detect any new wild cards so that the current display reflects 00132 // the mandatory (i.e. in answers) datasets 00133 // to implement : don't do any changes if the question is used in a quiz. 00134 // If new datadef, new properties should erase items. 00135 // Most of the data 00136 $datasettoremove = false; 00137 $newdatasetvalues = false; 00138 $newdataset = false; 00139 $dummyform = new stdClass(); 00140 $mandatorydatasets = array(); 00141 // should not test on adding a new answer 00142 // should test if there are already olddatasets or if the 'analyzequestion' 00143 // submit button has been clicked 00144 if (optional_param_array('datasetdef', false, PARAM_BOOL) || 00145 optional_param('analyzequestion', false, PARAM_BOOL)) { 00146 00147 if ($dummyform->answer = optional_param_array('answer', '', PARAM_NOTAGS)) { 00148 // there is always at least one answer... 00149 $fraction = optional_param_array('fraction', '', PARAM_NUMBER); 00150 $tolerance = optional_param_array('tolerance', '', PARAM_NUMBER); 00151 $tolerancetype = optional_param_array('tolerancetype', '', PARAM_NUMBER); 00152 $correctanswerlength = optional_param_array('correctanswerlength', '', PARAM_INT); 00153 $correctanswerformat = optional_param_array('correctanswerformat', '', PARAM_INT); 00154 00155 foreach ($dummyform->answer as $key => $answer) { 00156 if (trim($answer) != '') { // just look for non-empty 00157 $this->answer[$key] = new stdClass(); 00158 $this->answer[$key]->answer = $answer; 00159 $this->answer[$key]->fraction = $fraction[$key]; 00160 $this->answer[$key]->tolerance = $tolerance[$key]; 00161 $this->answer[$key]->tolerancetype = $tolerancetype[$key]; 00162 $this->answer[$key]->correctanswerlength = $correctanswerlength[$key]; 00163 $this->answer[$key]->correctanswerformat = $correctanswerformat[$key]; 00164 $this->nonemptyanswer[]= $this->answer[$key]; 00165 $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer); 00166 } 00167 } 00168 } 00169 $this->datasetdefs = array(); 00170 // rebuild datasetdefs from old values 00171 if ($olddef = optional_param_array('datasetdef', '', PARAM_RAW)) { 00172 $calcmin = optional_param_array('calcmin', '', PARAM_NUMBER); 00173 $calclength = optional_param_array('calclength', '', PARAM_INT); 00174 $calcmax = optional_param_array('calcmax', '', PARAM_NUMBER); 00175 $oldoptions = optional_param_array('defoptions', '', PARAM_RAW); 00176 $newdatasetvalues = false; 00177 $sizeofolddef = count($olddef); 00178 for ($key = 1; $key <= $sizeofolddef; $key++) { 00179 $def = $olddef[$key]; 00180 $this->datasetdefs[$def]= new stdClass(); 00181 $this->datasetdefs[$def]->type = 1; 00182 $this->datasetdefs[$def]->category = 0; 00183 $this->datasetdefs[$def]->options = $oldoptions[$key]; 00184 $this->datasetdefs[$def]->calcmin = $calcmin[$key]; 00185 $this->datasetdefs[$def]->calcmax = $calcmax[$key]; 00186 $this->datasetdefs[$def]->calclength = $calclength[$key]; 00187 //then compare with new values 00188 if (preg_match('~^(uniform|loguniform):([^:]*):([^:]*):([0-9]*)$~', 00189 $this->datasetdefs[$def]->options, $regs)) { 00190 if ($this->datasetdefs[$def]->calcmin != $regs[2]|| 00191 $this->datasetdefs[$def]->calcmax != $regs[3] || 00192 $this->datasetdefs[$def]->calclength != $regs[4]) { 00193 $newdatasetvalues = true; 00194 } 00195 } 00196 $this->datasetdefs[$def]->options = "uniform:" . 00197 $this->datasetdefs[$def]->calcmin . ":" . 00198 $this->datasetdefs[$def]->calcmax . ":" . 00199 $this->datasetdefs[$def]->calclength; 00200 } 00201 } 00202 // detect new datasets 00203 $newdataset = false; 00204 foreach ($mandatorydatasets as $datasetname) { 00205 if (!isset($this->datasetdefs["1-0-$datasetname"])) { 00206 $key = "1-0-$datasetname"; 00207 $this->datasetdefs[$key] = new stdClass(); 00208 $this->datasetdefs[$key]->type = 1; 00209 $this->datasetdefs[$key]->category = 0; 00210 $this->datasetdefs[$key]->name = $datasetname; 00211 $this->datasetdefs[$key]->options = "uniform:1.0:10.0:1"; 00212 $newdataset = true; 00213 } else { 00214 $this->datasetdefs["1-0-$datasetname"]->name = $datasetname; 00215 } 00216 } 00217 // remove obsolete datasets 00218 $datasettoremove = false; 00219 foreach ($this->datasetdefs as $defkey => $datasetdef) { 00220 if (!isset($datasetdef->name)) { 00221 $datasettoremove = true; 00222 unset($this->datasetdefs[$defkey]); 00223 } 00224 } 00225 } 00226 } // handle reload 00227 // create items if $newdataset and noofitems > 0 and !$newdatasetvalues 00228 // eliminate any items if $newdatasetvalues 00229 // eliminate any items if $datasettoremove, $newdataset, $newdatasetvalues 00230 if ($datasettoremove ||$newdataset ||$newdatasetvalues) { 00231 foreach ($this->datasetdefs as $defkey => $datasetdef) { 00232 $datasetdef->itemcount = 0; 00233 unset($datasetdef->items); 00234 } 00235 } 00236 $maxnumber = -1; 00237 if (optional_param('addbutton', false, PARAM_BOOL)) { 00238 $maxnumber = optional_param('selectadd', '', PARAM_INT); //FIXME: sloppy coding 00239 foreach ($this->datasetdefs as $defid => $datasetdef) { 00240 $datasetdef->itemcount = $maxnumber; 00241 unset($datasetdef->items); 00242 for ($numberadded = 1; $numberadded <= $maxnumber; $numberadded++) { 00243 $datasetitem = new stdClass(); 00244 $datasetitem->itemnumber = $numberadded; 00245 $datasetitem->id = 0; 00246 $datasetitem->value = $this->qtypeobj->generate_dataset_item( 00247 $datasetdef->options); 00248 $this->datasetdefs[$defid]->items[$numberadded] = $datasetitem; 00249 } 00250 } 00251 $this->maxnumber = $maxnumber; 00252 } else { 00253 // Handle reload dataset items 00254 if (optional_param_array('definition', '', PARAM_NOTAGS) && 00255 !($datasettoremove ||$newdataset ||$newdatasetvalues)) { 00256 $i = 1; 00257 $fromformdefinition = optional_param_array('definition', '', PARAM_NOTAGS); 00258 $fromformnumber = optional_param_array('number', '', PARAM_INT); 00259 $fromformitemid = optional_param_array('itemid', '', PARAM_INT); 00260 ksort($fromformdefinition); 00261 00262 foreach ($fromformdefinition as $key => $defid) { 00263 $addeditem = new stdClass(); 00264 $addeditem->id = $fromformitemid[$i]; 00265 $addeditem->value = $fromformnumber[$i]; 00266 $addeditem->itemnumber = ceil($i / count($this->datasetdefs)); 00267 $this->datasetdefs[$defid]->items[$addeditem->itemnumber] = $addeditem; 00268 $this->datasetdefs[$defid]->itemcount = $i; 00269 $i++; 00270 } 00271 } 00272 if (isset($addeditem->itemnumber) && $this->maxnumber < $addeditem->itemnumber) { 00273 $this->maxnumber = $addeditem->itemnumber; 00274 if (!empty($this->datasetdefs)) { 00275 foreach ($this->datasetdefs as $datasetdef) { 00276 $datasetdef->itemcount = $this->maxnumber; 00277 } 00278 } 00279 } 00280 } 00281 00282 parent::__construct($submiturl, $question, $category, $contexts, $formeditable); 00283 } 00284 00290 protected function definition_inner($mform) { 00291 $strquestionlabel = $this->qtypeobj->comment_header($this->nonemptyanswer); 00292 $label = get_string("sharedwildcards", "qtype_calculated"); 00293 $mform->addElement('hidden', 'synchronize', 0); 00294 $mform->addElement('hidden', 'initialcategory', 1); 00295 $mform->setType('initialcategory', PARAM_INT); 00296 $mform->addElement('hidden', 'reload', 1); 00297 $mform->setType('reload', PARAM_INT); 00298 $addfieldsname = 'updatequestion value'; 00299 $addstring = get_string("updatecategory", "qtype_calculated"); 00300 $mform->registerNoSubmitButton($addfieldsname); 00301 00302 $this->add_per_answer_fields($mform, get_string('answerhdr', 'qtype_calculated', '{no}'), 00303 question_bank::fraction_options(), 1, 1); 00304 00305 $this->add_unit_options($mform, $this); 00306 $this->add_unit_fields($mform, $this); 00307 $this->add_interactive_settings(); 00308 00309 $label = "<div class='mdl-align'></div><div class='mdl-align'>" . 00310 get_string('wildcardrole', 'qtype_calculatedsimple') . "</div>"; 00311 $mform->addElement('html', "<div class='mdl-align'> </div>"); 00312 // explaining the role of datasets so other strings can be shortened 00313 $mform->addElement('html', $label); 00314 00315 $mform->addElement('submit', 'analyzequestion', 00316 get_string('findwildcards', 'qtype_calculatedsimple')); 00317 $mform->registerNoSubmitButton('analyzequestion'); 00318 $mform->closeHeaderBefore('analyzequestion'); 00319 $this->wizarddisplay = optional_param('analyzequestion', false, PARAM_BOOL); 00320 if ($this->maxnumber != -1) { 00321 $this->noofitems = $this->maxnumber; 00322 } else { 00323 $this->noofitems = 0; 00324 } 00325 if (!empty($this->datasetdefs)) {//So there are some datadefs 00326 // we put them on the page 00327 $key = 0; 00328 $mform->addElement('header', 'additemhdr', 00329 get_string('wildcardparam', 'qtype_calculatedsimple')); 00330 $idx = 1; 00331 if (!empty($this->datasetdefs)) {// unnecessary test 00332 $j = (($this->noofitems) * count($this->datasetdefs))+1;// 00333 foreach ($this->datasetdefs as $defkey => $datasetdef) { 00334 $mform->addElement('static', "na[$j]", 00335 get_string('param', 'qtype_calculated', $datasetdef->name)); 00336 $this->qtypeobj->custom_generator_tools_part($mform, $idx, $j); 00337 $mform->addElement('hidden', "datasetdef[$idx]"); 00338 $mform->setType("datasetdef[$idx]", PARAM_RAW); 00339 $mform->addElement('hidden', "defoptions[$idx]"); 00340 $mform->setType("defoptions[$idx]", PARAM_RAW); 00341 $idx++; 00342 $mform->addElement('static', "divider[$j]", '', '<hr />'); 00343 $j++; 00344 } 00345 } 00346 //this should be done before the elements are created and stored as $this->formdata; 00347 //fill out all data sets and also the fields for the next item to add. 00348 /*Here we do already the values error analysis so that 00349 * we could force all wild cards values display if there is an error in values. 00350 * as using a , in a number */ 00351 $this->numbererrors = array(); 00352 if (!empty($this->datasetdefs)) { 00353 $j = $this->noofitems * count($this->datasetdefs); 00354 for ($itemnumber = $this->noofitems; $itemnumber >= 1; $itemnumber--) { 00355 $data = array(); 00356 $numbererrors = array(); 00357 $comment = new stdClass(); 00358 $comment->stranswers = array(); 00359 $comment->outsidelimit = false; 00360 $comment->answers = array(); 00361 00362 foreach ($this->datasetdefs as $defid => $datasetdef) { 00363 if (isset($datasetdef->items[$itemnumber])) { 00364 $this->formdata["definition[$j]"] = $defid; 00365 $this->formdata["itemid[$j]"] = 00366 $datasetdef->items[$itemnumber]->id; 00367 $data[$datasetdef->name] = $datasetdef->items[$itemnumber]->value; 00368 $this->formdata["number[$j]"] = $number = 00369 $datasetdef->items[$itemnumber]->value; 00370 if (! is_numeric($number)) { 00371 $a = new stdClass(); 00372 $a->name = '{'.$datasetdef->name.'}'; 00373 $a->value = $datasetdef->items[$itemnumber]->value; 00374 if (stristr($number, ',')) { 00375 $this->numbererrors["number[$j]"] = 00376 get_string('nocommaallowed', 'qtype_calculated'); 00377 $numbererrors .= $this->numbererrors['number['.$j.']']."<br />"; 00378 00379 } else { 00380 $this->numbererrors["number[$j]"] = 00381 get_string('notvalidnumber', 'qtype_calculated', $a); 00382 $numbererrors .= $this->numbererrors['number['.$j.']']."<br />"; 00383 } 00384 } else if (stristr($number, 'x')) { // hexa will pass the test 00385 $a = new stdClass(); 00386 $a->name = '{'.$datasetdef->name.'}'; 00387 $a->value = $datasetdef->items[$itemnumber]->value; 00388 $this->numbererrors['number['.$j.']'] = 00389 get_string('hexanotallowed', 'qtype_calculated', $a); 00390 $numbererrors .= $this->numbererrors['number['.$j.']']."<br />"; 00391 } else if (is_nan($number)) { 00392 $a = new stdClass(); 00393 $a->name = '{'.$datasetdef->name.'}'; 00394 $a->value = $datasetdef->items[$itemnumber]->value; 00395 $this->numbererrors["number[$j]"] = 00396 get_string('notvalidnumber', 'qtype_calculated', $a); 00397 $numbererrors .= $this->numbererrors['number['.$j.']']."<br />"; 00398 } 00399 } 00400 $j--; 00401 } 00402 if ($this->noofitems != 0) { 00403 if (empty($numbererrors)) { 00404 if (!isset($this->question->id)) { 00405 $this->question->id = 0; 00406 } 00407 $this->question->questiontext = !empty($this->question->questiontext) ? 00408 $this->question->questiontext : ''; 00409 $comment = $this->qtypeobj->comment_on_datasetitems( 00410 $this->qtypeobj, $this->question->id, 00411 $this->question->questiontext, $this->nonemptyanswer, 00412 $data, $itemnumber); 00413 if ($comment->outsidelimit) { 00414 $this->outsidelimit = $comment->outsidelimit; 00415 } 00416 $totalcomment = ''; 00417 00418 foreach ($this->nonemptyanswer as $key => $answer) { 00419 $totalcomment .= $comment->stranswers[$key].'<br/>'; 00420 } 00421 00422 $this->formdata['answercomment['.$itemnumber.']'] = $totalcomment; 00423 } 00424 } 00425 } 00426 $this->formdata['selectdelete'] = '1'; 00427 $this->formdata['selectadd'] = '1'; 00428 $j = $this->noofitems * count($this->datasetdefs)+1; 00429 $data = array(); // data for comment_on_datasetitems later 00430 $idx = 1; 00431 foreach ($this->datasetdefs as $defid => $datasetdef) { 00432 $this->formdata["datasetdef[$idx]"] = $defid; 00433 $idx++; 00434 } 00435 $this->formdata = $this->qtypeobj->custom_generator_set_data( 00436 $this->datasetdefs, $this->formdata); 00437 } 00438 00439 $addoptions = Array(); 00440 $addoptions['1'] = '1'; 00441 for ($i = 10; $i <= 100; $i += 10) { 00442 $addoptions["$i"] = "$i"; 00443 } 00444 $showoptions = Array(); 00445 $showoptions['1'] = '1'; 00446 $showoptions['2'] = '2'; 00447 $showoptions['5'] = '5'; 00448 for ($i = 10; $i <= 100; $i += 10) { 00449 $showoptions["$i"] = "$i"; 00450 } 00451 $mform->closeHeaderBefore('additemhdr'); 00452 $addgrp = array(); 00453 $addgrp[] = $mform->createElement('submit', 'addbutton', 00454 get_string('generatenewitemsset', 'qtype_calculatedsimple')); 00455 $addgrp[] = $mform->createElement('select', "selectadd", '', $addoptions); 00456 $addgrp[] = $mform->createElement('static', "stat", '', 00457 get_string('newsetwildcardvalues', 'qtype_calculatedsimple')); 00458 $mform->addGroup($addgrp, 'addgrp', '', ' ', false); 00459 $mform->registerNoSubmitButton('addbutton'); 00460 $mform->closeHeaderBefore('addgrp'); 00461 $addgrp1 = array(); 00462 $addgrp1[] = $mform->createElement('submit', 'showbutton', 00463 get_string('showitems', 'qtype_calculatedsimple')); 00464 $addgrp1[] = $mform->createElement('select', "selectshow", '', $showoptions); 00465 $addgrp1[] = $mform->createElement('static', "stat", '', 00466 get_string('setwildcardvalues', 'qtype_calculatedsimple')); 00467 $mform->addGroup($addgrp1, 'addgrp1', '', ' ', false); 00468 $mform->registerNoSubmitButton('showbutton'); 00469 $mform->closeHeaderBefore('addgrp1'); 00470 $mform->addElement('static', "divideradd", '', ''); 00471 if ($this->noofitems == 0) { 00472 $mform->addElement('static', 'warningnoitems', '', '<span class="error">' . 00473 get_string('youmustaddatleastonevalue', 'qtype_calculatedsimple') . 00474 '</span>'); 00475 $mform->closeHeaderBefore('warningnoitems'); 00476 } else { 00477 $mform->addElement('header', 'additemhdr1', 00478 get_string('wildcardvalues', 'qtype_calculatedsimple')); 00479 $mform->closeHeaderBefore('additemhdr1'); 00480 if (!empty($this->numbererrors) || $this->outsidelimit) { 00481 $mform->addElement('static', "alert", '', '<span class="error">' . 00482 get_string('useadvance', 'qtype_calculatedsimple').'</span>'); 00483 } 00484 00485 $mform->addElement('submit', 'updatedatasets', 00486 get_string('updatewildcardvalues', 'qtype_calculatedsimple')); 00487 $mform->registerNoSubmitButton('updatedatasets'); 00488 $mform->setAdvanced("updatedatasets", true); 00489 00490 //-------------------------------------------------------------- 00491 $j = $this->noofitems * count($this->datasetdefs); 00492 $k = optional_param('selectshow', 1, PARAM_INT); 00493 00494 for ($i = $this->noofitems; $i >= 1; $i--) { 00495 foreach ($this->datasetdefs as $defkey => $datasetdef) { 00496 if ($k > 0 || $this->outsidelimit || !empty($this->numbererrors)) { 00497 $mform->addElement('text', "number[$j]", get_string( 00498 'wildcard', 'qtype_calculatedsimple', $datasetdef->name)); 00499 $mform->setAdvanced("number[$j]", true); 00500 if (!empty($this->numbererrors['number['.$j.']'])) { 00501 $mform->addElement('static', "numbercomment[$j]", '', 00502 '<span class="error">' . 00503 $this->numbererrors['number['.$j.']'] . '</span>'); 00504 $mform->setAdvanced("numbercomment[$j]", true); 00505 } 00506 } else { 00507 $mform->addElement('hidden', "number[$j]", get_string( 00508 'wildcard', 'qtype_calculatedsimple', $datasetdef->name)); 00509 } 00510 $mform->setType("number[$j]", PARAM_NUMBER); 00511 00512 $mform->addElement('hidden', "itemid[$j]"); 00513 $mform->setType("itemid[$j]", PARAM_INT); 00514 00515 $mform->addElement('hidden', "definition[$j]"); 00516 $mform->setType("definition[$j]", PARAM_NOTAGS); 00517 00518 $j--; 00519 } 00520 if (!empty($strquestionlabel) && ($k > 0 || $this->outsidelimit || 00521 !empty($this->numbererrors))) { 00522 $mform->addElement('static', "answercomment[$i]", "<b>" . 00523 get_string('setno', 'qtype_calculatedsimple', $i) . 00524 "</b> " . $strquestionlabel); 00525 00526 } 00527 if ($k > 0 || $this->outsidelimit || !empty($this->numbererrors)) { 00528 $mform->addElement('static', "divider1[$j]", '', '<hr />'); 00529 00530 } 00531 $k--; 00532 } 00533 } 00534 } else { 00535 $mform->addElement('static', 'warningnowildcards', '', '<span class="error">' . 00536 get_string('atleastonewildcard', 'qtype_calculatedsimple') . '</span>'); 00537 $mform->closeHeaderBefore('warningnowildcards'); 00538 } 00539 00540 //---------------------------------------------------------------------- 00541 // Non standard name for button element needed so not using add_action_buttons 00542 // hidden elements 00543 00544 $mform->addElement('hidden', 'id'); 00545 $mform->setType('id', PARAM_INT); 00546 00547 $mform->addElement('hidden', 'courseid'); 00548 $mform->setType('courseid', PARAM_INT); 00549 $mform->setDefault('courseid', 0); 00550 00551 $mform->addElement('hidden', 'cmid'); 00552 $mform->setType('cmid', PARAM_INT); 00553 $mform->setDefault('cmid', 0); 00554 if (!empty($this->question->id)) { 00555 if ($this->question->formoptions->cansaveasnew) { 00556 $mform->addElement('header', 'additemhdr', 00557 get_string('converttocalculated', 'qtype_calculatedsimple')); 00558 $mform->closeHeaderBefore('additemhdr'); 00559 00560 $mform->addElement('checkbox', 'convert', '', 00561 get_string('willconverttocalculated', 'qtype_calculatedsimple')); 00562 $mform->setDefault('convert', 0); 00563 00564 } 00565 } 00566 } 00567 00568 public function data_preprocessing($question) { 00569 $question = parent::data_preprocessing($question); 00570 $question = $this->data_preprocessing_answers($question); 00571 $question = $this->data_preprocessing_hints($question); 00572 $question = $this->data_preprocessing_units($question); 00573 $question = $this->data_preprocessing_unit_options($question); 00574 00575 // This is a bit ugly, but it loads all the dataset values. 00576 $question = (object)((array)$question + $this->formdata); 00577 00578 return $question; 00579 } 00580 00581 public function qtype() { 00582 return 'calculatedsimple'; 00583 } 00584 00585 public function validation($data, $files) { 00586 $errors = parent::validation($data, $files); 00587 00588 if (empty($data['definition'])) { 00589 $errors['selectadd'] = get_string('youmustaddatleastonevalue', 'qtype_calculatedsimple'); 00590 } 00591 00592 return $errors; 00593 } 00594 }