MDL-10383 - some more refactoring/cleanup, enablegroupings experimental admin setting...
[moodle.git] / question / type / datasetdependent / datasetdefinitions_form.php
CommitLineData
8fc3e643 1<?php
830e47a8 2/**
3 * @package questionbank
4 * @subpackage questiontypes
5 */
6
8fc3e643 7class question_dataset_dependent_definitions_form extends moodleform {
8 /**
9 * Question object with options and answers already loaded by get_question_options
10 * Be careful how you use this it is needed sometimes to set up the structure of the
11 * form in definition_inner but data is always loaded into the form with set_defaults.
12 *
13 * @var object
14 */
15 var $question;
16 /**
17 * Reference to question type object
18 *
19 * @var question_dataset_dependent_questiontype
20 */
21 var $qtypeobj;
22 /**
23 * Add question-type specific form fields.
24 *
25 * @param MoodleQuickForm $mform the form being built.
26 */
27 function question_dataset_dependent_definitions_form($submiturl, $question){
28 global $QTYPES;
29 $this->question = $question;
30 $this->qtypeobj =& $QTYPES[$this->question->qtype];
31 parent::moodleform($submiturl);
32 }
33 function definition() {
60b5ecd3 34 global $SESSION;
8fc3e643 35 $mform =& $this->_form;
92e2ed8f 36 $stringfile = 'qtype_'.$this->question->qtype ;
8fc3e643 37 $possibledatasets = $this->qtypeobj->find_dataset_names($this->question->questiontext);
38 $mandatorydatasets = array();
60b5ecd3 39 if (isset($this->question->options->answers)){
40 foreach ($this->question->options->answers as $answer) {
41 $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer->answer);
42 }
43 }else{
44 foreach ($SESSION->datasetdependent->questionform->answers as $answer){
45 $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer);
46 }
8fc3e643 47 }
60b5ecd3 48
8fc3e643 49 $key = 0;
92e2ed8f 50 $datadefscat= array();
51 $datadefscat = $this->qtypeobj->get_dataset_definitions_category($this->question);
8fc3e643 52 $datasetmenus = array();
92e2ed8f 53 $label = "<div align=\"center\">".get_string('datasetrole', 'qtype_datasetdependent','numerical')."</div>";
54 $mform->addElement('html', $label);// explaining the role of datasets so other strings can be shortened
55 $mform->addElement('header', 'mandatoryhdr', get_string('mandatoryhdr', $stringfile));
56 $labelsharedwildcard = get_string("sharedwildcard", "qtype_datasetdependent");
57
8fc3e643 58 foreach ($mandatorydatasets as $datasetname) {
59 if (!isset($datasetmenus[$datasetname])) {
60 list($options, $selected) =
61 $this->qtypeobj->dataset_options($this->question, $datasetname);
62 unset($options['0']); // Mandatory...
92e2ed8f 63 $label = get_string("wildcard", "quiz"). " <strong>$datasetname</strong> ";
8fc3e643 64 $mform->addElement('select', "dataset[$key]", $label, $options);
92e2ed8f 65 if (isset($datadefscat[$datasetname])){
66 $mform->addElement('static', "there is a category", $labelsharedwildcard." <strong>$datasetname </strong>", get_string('dataitemdefined',"qtype_datasetdependent", $datadefscat[$datasetname]));
67 }
8fc3e643 68 $mform->setDefault("dataset[$key]", $selected);
69 $datasetmenus[$datasetname]='';
70 $key++;
71 }
72 }
92e2ed8f 73 $mform->addElement('header', 'possiblehdr', get_string('possiblehdr', $stringfile));
74
75
8fc3e643 76 foreach ($possibledatasets as $datasetname) {
77 if (!isset($datasetmenus[$datasetname])) {
78 list($options, $selected) =
92e2ed8f 79 $this->qtypeobj->dataset_options($this->question, $datasetname,false);
80 $label = get_string("wildcard", "quiz"). " <strong>$datasetname</strong> ";
8fc3e643 81 $mform->addElement('select', "dataset[$key]", $label, $options);
92e2ed8f 82 // $mform->addRule("dataset[$key]", null, 'required', null, 'client');
83 if (isset($datadefscat[$datasetname])){
84 $mform->addElement('static', "there is a category", $labelsharedwildcard." <strong>$datasetname </strong>", get_string('dataitemdefined',"qtype_datasetdependent", $datadefscat[$datasetname]));
85 }
86
87 // $selected ="0";
8fc3e643 88 $mform->setDefault("dataset[$key]", $selected);
89 $datasetmenus[$datasetname]='';
90 $key++;
91 }
92 }
a8d2a373 93 $this->add_action_buttons(false, get_string('nextpage', 'qtype_calculated'));
60b5ecd3 94
95
96 //hidden elements
97 $mform->addElement('hidden', 'returnurl');
98 $mform->setType('returnurl', PARAM_URL);
99 $mform->addElement('hidden', 'qtype');
100 $mform->setType('qtype', PARAM_ALPHA);
101 $mform->addElement('hidden', 'category');
102 $mform->setType('category', PARAM_INT);
103 $mform->addElement('hidden', 'id');
271e6dec 104
105 $mform->addElement('hidden', 'courseid');
106 $mform->setType('courseid', PARAM_INT);
107 $mform->setDefault('courseid', 0);
108
109 $mform->addElement('hidden', 'cmid');
110 $mform->setType('cmid', PARAM_INT);
111 $mform->setDefault('cmid', 0);
112
60b5ecd3 113 $mform->setType('id', PARAM_INT);
114 $mform->addElement('hidden', 'wizard', 'datasetitems');
115 $mform->setType('wizard', PARAM_ALPHA);
8fc3e643 116 }
92e2ed8f 117 function validation($data){
118 $errors = array();
119 $datasets = $data['dataset'];
120 $countvalid = 0 ;
121 foreach ($datasets as $key => $dataset){
122 if ($dataset !="0") {
123 $countvalid++;
124 }
125 }
126 if (!$countvalid){
127 foreach ($datasets as $key => $dataset){
128 $errors['dataset['.$key.']'] = get_string('atleastonerealdataset', 'qtype_datasetdependent');
129 }
130 }
131 return $errors;
132 }
8fc3e643 133
134}
135?>