MDL-19519 , MDL-1728 Adding database question_calculated_options
[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];
315efce7 31 // Validate the question category.
32 if (!$category = $DB->get_record('question_categories', array('id' => $question->category))) {
33 print_error('categorydoesnotexist', 'question', $returnurl);
34 }
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;
92e2ed8f 42 $stringfile = 'qtype_'.$this->question->qtype ;
8fc3e643 43 $possibledatasets = $this->qtypeobj->find_dataset_names($this->question->questiontext);
44 $mandatorydatasets = array();
60b5ecd3 45 if (isset($this->question->options->answers)){
46 foreach ($this->question->options->answers as $answer) {
47 $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer->answer);
48 }
49 }else{
fbe2cfea 50 foreach ($SESSION->calculated->questionform->answers as $answer){
60b5ecd3 51 $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer);
52 }
8fc3e643 53 }
60b5ecd3 54
8fc3e643 55 $key = 0;
92e2ed8f 56 $datadefscat= array();
57 $datadefscat = $this->qtypeobj->get_dataset_definitions_category($this->question);
8fc3e643 58 $datasetmenus = array();
ff9b4ea4 59 $label = "<div class='mdl-align'>".get_string('datasetrole', 'qtype_datasetdependent','numerical')."</div>";
79bb7202 60 $mform->addElement('html', $label);// explaining the role of datasets so other strings can be shortened
92e2ed8f 61 $mform->addElement('header', 'mandatoryhdr', get_string('mandatoryhdr', $stringfile));
62 $labelsharedwildcard = get_string("sharedwildcard", "qtype_datasetdependent");
63
8fc3e643 64 foreach ($mandatorydatasets as $datasetname) {
65 if (!isset($datasetmenus[$datasetname])) {
66 list($options, $selected) =
67 $this->qtypeobj->dataset_options($this->question, $datasetname);
68 unset($options['0']); // Mandatory...
92e2ed8f 69 $label = get_string("wildcard", "quiz"). " <strong>$datasetname</strong> ";
8fc3e643 70 $mform->addElement('select', "dataset[$key]", $label, $options);
92e2ed8f 71 if (isset($datadefscat[$datasetname])){
72 $mform->addElement('static', "there is a category", $labelsharedwildcard." <strong>$datasetname </strong>", get_string('dataitemdefined',"qtype_datasetdependent", $datadefscat[$datasetname]));
73 }
8fc3e643 74 $mform->setDefault("dataset[$key]", $selected);
75 $datasetmenus[$datasetname]='';
76 $key++;
77 }
78 }
92e2ed8f 79 $mform->addElement('header', 'possiblehdr', get_string('possiblehdr', $stringfile));
79bb7202 80
92e2ed8f 81
8fc3e643 82 foreach ($possibledatasets as $datasetname) {
83 if (!isset($datasetmenus[$datasetname])) {
84 list($options, $selected) =
92e2ed8f 85 $this->qtypeobj->dataset_options($this->question, $datasetname,false);
86 $label = get_string("wildcard", "quiz"). " <strong>$datasetname</strong> ";
8fc3e643 87 $mform->addElement('select', "dataset[$key]", $label, $options);
92e2ed8f 88 // $mform->addRule("dataset[$key]", null, 'required', null, 'client');
89 if (isset($datadefscat[$datasetname])){
90 $mform->addElement('static', "there is a category", $labelsharedwildcard." <strong>$datasetname </strong>", get_string('dataitemdefined',"qtype_datasetdependent", $datadefscat[$datasetname]));
91 }
79bb7202 92
93 // $selected ="0";
8fc3e643 94 $mform->setDefault("dataset[$key]", $selected);
95 $datasetmenus[$datasetname]='';
96 $key++;
97 }
98 }
a8d2a373 99 $this->add_action_buttons(false, get_string('nextpage', 'qtype_calculated'));
60b5ecd3 100
101
102 //hidden elements
103 $mform->addElement('hidden', 'returnurl');
104 $mform->setType('returnurl', PARAM_URL);
60b5ecd3 105 $mform->addElement('hidden', 'id');
79bb7202 106 $mform->setType('id', PARAM_INT);
107
315efce7 108 $mform->addElement('hidden', 'category');
109 $mform->setType('category', PARAM_RAW);
110 $mform->setDefault('category', array('contexts' => array($this->categorycontext)));
111
271e6dec 112 $mform->addElement('hidden', 'courseid');
113 $mform->setType('courseid', PARAM_INT);
114 $mform->setDefault('courseid', 0);
79bb7202 115
271e6dec 116 $mform->addElement('hidden', 'cmid');
117 $mform->setType('cmid', PARAM_INT);
118 $mform->setDefault('cmid', 0);
79bb7202 119
60b5ecd3 120 $mform->setType('id', PARAM_INT);
121 $mform->addElement('hidden', 'wizard', 'datasetitems');
122 $mform->setType('wizard', PARAM_ALPHA);
8fc3e643 123 }
a78890d5 124 function validation($data, $files) {
125 $errors = parent::validation($data, $files);
92e2ed8f 126 $datasets = $data['dataset'];
127 $countvalid = 0 ;
128 foreach ($datasets as $key => $dataset){
129 if ($dataset !="0") {
130 $countvalid++;
131 }
132 }
133 if (!$countvalid){
134 foreach ($datasets as $key => $dataset){
135 $errors['dataset['.$key.']'] = get_string('atleastonerealdataset', 'qtype_datasetdependent');
136 }
137 }
138 return $errors;
139 }
8fc3e643 140
141}
ff9b4ea4 142?>