MDL-20636 Fix new stdClass -> stdClass(), and trailing whitespace.
[moodle.git] / question / type / calculated / 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){
315efce7 28 global $QTYPES, $DB;
8fc3e643 29 $this->question = $question;
30 $this->qtypeobj =& $QTYPES[$this->question->qtype];
4454447d
PS
31 // Validate the question category.
32 if (!$category = $DB->get_record('question_categories', array('id' => $question->category))) {
33 print_error('categorydoesnotexist', 'question', $returnurl);
34 }
315efce7 35 $this->category = $category;
36 $this->categorycontext = get_context_instance_by_id($category->contextid);
8fc3e643 37 parent::moodleform($submiturl);
38 }
39 function definition() {
60b5ecd3 40 global $SESSION;
8fc3e643 41 $mform =& $this->_form;
8fc3e643 42 $possibledatasets = $this->qtypeobj->find_dataset_names($this->question->questiontext);
43 $mandatorydatasets = array();
60b5ecd3 44 if (isset($this->question->options->answers)){
45 foreach ($this->question->options->answers as $answer) {
46 $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer->answer);
47 }
48 }else{
fbe2cfea 49 foreach ($SESSION->calculated->questionform->answers as $answer){
60b5ecd3 50 $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer);
51 }
8fc3e643 52 }
60b5ecd3 53
8fc3e643 54 $key = 0;
92e2ed8f 55 $datadefscat= array();
56 $datadefscat = $this->qtypeobj->get_dataset_definitions_category($this->question);
8fc3e643 57 $datasetmenus = array();
5940864e 58 $label = "<div class='mdl-align'>".get_string('datasetrole', 'qtype_calculated')."</div>";
79bb7202 59 $mform->addElement('html', $label);// explaining the role of datasets so other strings can be shortened
8ef9f8ea 60 $mform->addElement('header', 'mandatoryhdr', get_string('mandatoryhdr', 'qtype_calculated'));
5940864e 61 $labelsharedwildcard = get_string('sharedwildcard', 'qtype_calculated');
92e2ed8f 62
8fc3e643 63 foreach ($mandatorydatasets as $datasetname) {
64 if (!isset($datasetmenus[$datasetname])) {
65 list($options, $selected) =
66 $this->qtypeobj->dataset_options($this->question, $datasetname);
67 unset($options['0']); // Mandatory...
92e2ed8f 68 $label = get_string("wildcard", "quiz"). " <strong>$datasetname</strong> ";
8fc3e643 69 $mform->addElement('select', "dataset[$key]", $label, $options);
92e2ed8f 70 if (isset($datadefscat[$datasetname])){
d90b016b 71 $mform->addElement('static', "there is a category", get_string('sharedwildcard', 'qtype_calculated',$datasetname ), get_string('dataitemdefined','qtype_calculated', $datadefscat[$datasetname]));
92e2ed8f 72 }
8fc3e643 73 $mform->setDefault("dataset[$key]", $selected);
74 $datasetmenus[$datasetname]='';
75 $key++;
76 }
77 }
8ef9f8ea 78 $mform->addElement('header', 'possiblehdr', get_string('possiblehdr', 'qtype_calculated'));
79bb7202 79
92e2ed8f 80
8fc3e643 81 foreach ($possibledatasets as $datasetname) {
82 if (!isset($datasetmenus[$datasetname])) {
83 list($options, $selected) =
92e2ed8f 84 $this->qtypeobj->dataset_options($this->question, $datasetname,false);
85 $label = get_string("wildcard", "quiz"). " <strong>$datasetname</strong> ";
8fc3e643 86 $mform->addElement('select', "dataset[$key]", $label, $options);
92e2ed8f 87 // $mform->addRule("dataset[$key]", null, 'required', null, 'client');
88 if (isset($datadefscat[$datasetname])){
d90b016b 89 $mform->addElement('static', "there is a category", get_string('sharedwildcard', 'qtype_calculated',$datasetname ), get_string('dataitemdefined',"qtype_calculated", $datadefscat[$datasetname]));
92e2ed8f 90 }
79bb7202 91
92 // $selected ="0";
8fc3e643 93 $mform->setDefault("dataset[$key]", $selected);
94 $datasetmenus[$datasetname]='';
95 $key++;
96 }
97 }
1bf9a631 98 // temporary strings
d90b016b 99 // temporary strings
8ef9f8ea 100 $mform->addElement('header', 'synchronizehdr', get_string('synchronize', 'qtype_calculated'));
d90b016b
PP
101 $mform->addElement('radio', 'synchronize', '', get_string('synchronizeno', 'qtype_calculated'),0);
102 $mform->addElement('radio', 'synchronize', '', get_string('synchronizeyes', 'qtype_calculated'),1);
103 $mform->addElement('radio', 'synchronize', '', get_string('synchronizeyesdisplay', 'qtype_calculated'),2);
f79a4158 104 if (isset($this->question->options)&& isset($this->question->options->synchronize) ){
105 $mform->setDefault("synchronize", $this->question->options->synchronize);
106 } else {
107 $mform->setDefault("synchronize", 0 );
108 }
109
a8d2a373 110 $this->add_action_buttons(false, get_string('nextpage', 'qtype_calculated'));
60b5ecd3 111
112
113 //hidden elements
114 $mform->addElement('hidden', 'returnurl');
f065391d
PP
115 $mform->setType('returnurl', PARAM_LOCALURL);
116 $mform->setDefault('returnurl', 0);
60b5ecd3 117 $mform->addElement('hidden', 'id');
79bb7202 118 $mform->setType('id', PARAM_INT);
119
315efce7 120 $mform->addElement('hidden', 'category');
121 $mform->setType('category', PARAM_RAW);
122 $mform->setDefault('category', array('contexts' => array($this->categorycontext)));
aeb15530 123
271e6dec 124 $mform->addElement('hidden', 'courseid');
125 $mform->setType('courseid', PARAM_INT);
126 $mform->setDefault('courseid', 0);
79bb7202 127
271e6dec 128 $mform->addElement('hidden', 'cmid');
129 $mform->setType('cmid', PARAM_INT);
130 $mform->setDefault('cmid', 0);
79bb7202 131
60b5ecd3 132 $mform->setType('id', PARAM_INT);
133 $mform->addElement('hidden', 'wizard', 'datasetitems');
134 $mform->setType('wizard', PARAM_ALPHA);
8fc3e643 135 }
ab8b5142
PP
136/* function set_data($question) {
137 $formdata = array();
138 $fromform = new stdClass();
139 $default_values['synchronize'] = $question->options->synchronize ;
140 $question = (object)((array)$question + $default_values);
141 }*/
a78890d5 142 function validation($data, $files) {
143 $errors = parent::validation($data, $files);
92e2ed8f 144 $datasets = $data['dataset'];
145 $countvalid = 0 ;
146 foreach ($datasets as $key => $dataset){
147 if ($dataset !="0") {
148 $countvalid++;
149 }
150 }
151 if (!$countvalid){
152 foreach ($datasets as $key => $dataset){
5940864e 153 $errors['dataset['.$key.']'] = get_string('atleastonerealdataset', 'qtype_calculated');
92e2ed8f 154 }
155 }
156 return $errors;
157 }
8fc3e643 158
159}
aeb15530 160