MDL-8978 - Regression from MDL-6386. Merged from MOODLE_16_STABLE.
[moodle.git] / question / type / datasetdependent / datasetdefinitions_form.php
CommitLineData
8fc3e643 1<?php
2class question_dataset_dependent_definitions_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 */
10 var $question;
11 /**
12 * Reference to question type object
13 *
14 * @var question_dataset_dependent_questiontype
15 */
16 var $qtypeobj;
17 /**
18 * Add question-type specific form fields.
19 *
20 * @param MoodleQuickForm $mform the form being built.
21 */
22 function question_dataset_dependent_definitions_form($submiturl, $question){
23 global $QTYPES;
24 $this->question = $question;
25 $this->qtypeobj =& $QTYPES[$this->question->qtype];
26 parent::moodleform($submiturl);
27 }
28 function definition() {
60b5ecd3 29 global $SESSION;
8fc3e643 30 $mform =& $this->_form;
92e2ed8f 31 $stringfile = 'qtype_'.$this->question->qtype ;
8fc3e643 32 $possibledatasets = $this->qtypeobj->find_dataset_names($this->question->questiontext);
33 $mandatorydatasets = array();
60b5ecd3 34 if (isset($this->question->options->answers)){
35 foreach ($this->question->options->answers as $answer) {
36 $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer->answer);
37 }
38 }else{
39 foreach ($SESSION->datasetdependent->questionform->answers as $answer){
40 $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer);
41 }
8fc3e643 42 }
60b5ecd3 43
8fc3e643 44 $key = 0;
92e2ed8f 45 $datadefscat= array();
46 $datadefscat = $this->qtypeobj->get_dataset_definitions_category($this->question);
8fc3e643 47 $datasetmenus = array();
92e2ed8f 48 $label = "<div align=\"center\">".get_string('datasetrole', 'qtype_datasetdependent','numerical')."</div>";
49 $mform->addElement('html', $label);// explaining the role of datasets so other strings can be shortened
50 $mform->addElement('header', 'mandatoryhdr', get_string('mandatoryhdr', $stringfile));
51 $labelsharedwildcard = get_string("sharedwildcard", "qtype_datasetdependent");
52
8fc3e643 53 foreach ($mandatorydatasets as $datasetname) {
54 if (!isset($datasetmenus[$datasetname])) {
55 list($options, $selected) =
56 $this->qtypeobj->dataset_options($this->question, $datasetname);
57 unset($options['0']); // Mandatory...
92e2ed8f 58 $label = get_string("wildcard", "quiz"). " <strong>$datasetname</strong> ";
8fc3e643 59 $mform->addElement('select', "dataset[$key]", $label, $options);
92e2ed8f 60 if (isset($datadefscat[$datasetname])){
61 $mform->addElement('static', "there is a category", $labelsharedwildcard." <strong>$datasetname </strong>", get_string('dataitemdefined',"qtype_datasetdependent", $datadefscat[$datasetname]));
62 }
8fc3e643 63 $mform->setDefault("dataset[$key]", $selected);
64 $datasetmenus[$datasetname]='';
65 $key++;
66 }
67 }
92e2ed8f 68 $mform->addElement('header', 'possiblehdr', get_string('possiblehdr', $stringfile));
69
70
8fc3e643 71 foreach ($possibledatasets as $datasetname) {
72 if (!isset($datasetmenus[$datasetname])) {
73 list($options, $selected) =
92e2ed8f 74 $this->qtypeobj->dataset_options($this->question, $datasetname,false);
75 $label = get_string("wildcard", "quiz"). " <strong>$datasetname</strong> ";
8fc3e643 76 $mform->addElement('select', "dataset[$key]", $label, $options);
92e2ed8f 77 // $mform->addRule("dataset[$key]", null, 'required', null, 'client');
78 if (isset($datadefscat[$datasetname])){
79 $mform->addElement('static', "there is a category", $labelsharedwildcard." <strong>$datasetname </strong>", get_string('dataitemdefined',"qtype_datasetdependent", $datadefscat[$datasetname]));
80 }
81
82 // $selected ="0";
8fc3e643 83 $mform->setDefault("dataset[$key]", $selected);
84 $datasetmenus[$datasetname]='';
85 $key++;
86 }
87 }
a8d2a373 88 $this->add_action_buttons(false, get_string('nextpage', 'qtype_calculated'));
60b5ecd3 89
90
91 //hidden elements
92 $mform->addElement('hidden', 'returnurl');
93 $mform->setType('returnurl', PARAM_URL);
94 $mform->addElement('hidden', 'qtype');
95 $mform->setType('qtype', PARAM_ALPHA);
96 $mform->addElement('hidden', 'category');
97 $mform->setType('category', PARAM_INT);
98 $mform->addElement('hidden', 'id');
99 $mform->setType('id', PARAM_INT);
100 $mform->addElement('hidden', 'wizard', 'datasetitems');
101 $mform->setType('wizard', PARAM_ALPHA);
8fc3e643 102 }
92e2ed8f 103 function validation($data){
104 $errors = array();
105 $datasets = $data['dataset'];
106 $countvalid = 0 ;
107 foreach ($datasets as $key => $dataset){
108 if ($dataset !="0") {
109 $countvalid++;
110 }
111 }
112 if (!$countvalid){
113 foreach ($datasets as $key => $dataset){
114 $errors['dataset['.$key.']'] = get_string('atleastonerealdataset', 'qtype_datasetdependent');
115 }
116 }
117 return $errors;
118 }
8fc3e643 119
120}
121?>