Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/question/type/calculated/datasetdefinitions_form.php
Go to the documentation of this file.
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/edit_question_form.php');
00030 
00031 
00038 class question_dataset_dependent_definitions_form extends question_wizard_form {
00046     protected $question;
00052     protected $qtypeobj;
00058     public function __construct($submiturl, $question) {
00059         global $DB;
00060         $this->question = $question;
00061         $this->qtypeobj = question_bank::get_qtype($this->question->qtype);
00062         // Validate the question category.
00063         if (!$category = $DB->get_record('question_categories',
00064                 array('id' => $question->category))) {
00065             print_error('categorydoesnotexist', 'question', $returnurl);
00066         }
00067         $this->category = $category;
00068         $this->categorycontext = get_context_instance_by_id($category->contextid);
00069         parent::__construct($submiturl);
00070     }
00071 
00072     protected function definition() {
00073         global $SESSION;
00074         $mform = $this->_form;
00075         $possibledatasets = $this->qtypeobj->find_dataset_names($this->question->questiontext);
00076         $mandatorydatasets = array();
00077         if (isset($this->question->options->answers)) {
00078             foreach ($this->question->options->answers as $answer) {
00079                 $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer->answer);
00080             }
00081         } else {
00082             foreach ($SESSION->calculated->questionform->answers as $answer) {
00083                 $mandatorydatasets += $this->qtypeobj->find_dataset_names($answer);
00084             }
00085         }
00086 
00087         $key = 0;
00088         $datadefscat= array();
00089         $datadefscat  = $this->qtypeobj->get_dataset_definitions_category($this->question);
00090         $datasetmenus = array();
00091         $label = "<div class='mdl-align'>".get_string('datasetrole', 'qtype_calculated')."</div>";
00092         // explaining the role of datasets so other strings can be shortened
00093         $mform->addElement('html', $label);
00094         $mform->addElement('header', 'mandatoryhdr',
00095                 get_string('mandatoryhdr', 'qtype_calculated'));
00096         $labelsharedwildcard = get_string('sharedwildcard', 'qtype_calculated');
00097 
00098         foreach ($mandatorydatasets as $datasetname) {
00099             if (!isset($datasetmenus[$datasetname])) {
00100                 list($options, $selected) =
00101                         $this->qtypeobj->dataset_options($this->question, $datasetname);
00102                 unset($options['0']); // Mandatory...
00103                 $label = get_string('wildcard', 'qtype_calculated', $datasetname);
00104                 $mform->addElement('select', "dataset[$key]", $label, $options);
00105                 if (isset($datadefscat[$datasetname])) {
00106                     $mform->addElement('static', "there is a category",
00107                             get_string('sharedwildcard', 'qtype_calculated', $datasetname),
00108                             get_string('dataitemdefined', 'qtype_calculated',
00109                             $datadefscat[$datasetname]));
00110                 }
00111                 $mform->setDefault("dataset[$key]", $selected);
00112                 $datasetmenus[$datasetname] = '';
00113                 $key++;
00114             }
00115         }
00116         $mform->addElement('header', 'possiblehdr', get_string('possiblehdr', 'qtype_calculated'));
00117 
00118         foreach ($possibledatasets as $datasetname) {
00119             if (!isset($datasetmenus[$datasetname])) {
00120                 list($options, $selected) = $this->qtypeobj->dataset_options(
00121                         $this->question, $datasetname, false);
00122                 $label = get_string('wildcard', 'qtype_calculated', $datasetname);
00123                 $mform->addElement('select', "dataset[$key]", $label, $options);
00124                 if (isset($datadefscat[$datasetname])) {
00125                     $mform->addElement('static', "there is a category",
00126                             get_string('sharedwildcard', 'qtype_calculated', $datasetname),
00127                             get_string('dataitemdefined', 'qtype_calculated',
00128                                     $datadefscat[$datasetname]));
00129                 }
00130 
00131                 $mform->setDefault("dataset[$key]", $selected);
00132                 $datasetmenus[$datasetname] = '';
00133                 $key++;
00134             }
00135         }
00136         // temporary strings
00137         $mform->addElement('header', 'synchronizehdr',
00138                 get_string('synchronize', 'qtype_calculated'));
00139         $mform->addElement('radio', 'synchronize', '',
00140                 get_string('synchronizeno', 'qtype_calculated'), 0);
00141         $mform->addElement('radio', 'synchronize', '',
00142                 get_string('synchronizeyes', 'qtype_calculated'), 1);
00143         $mform->addElement('radio', 'synchronize', '',
00144                 get_string('synchronizeyesdisplay', 'qtype_calculated'), 2);
00145         if (isset($this->question->options) &&
00146                 isset($this->question->options->synchronize)) {
00147             $mform->setDefault('synchronize', $this->question->options->synchronize);
00148         } else {
00149             $mform->setDefault('synchronize', 0);
00150         }
00151 
00152         $this->add_action_buttons(false, get_string('nextpage', 'qtype_calculated'));
00153 
00154         $this->add_hidden_fields();
00155 
00156         $mform->addElement('hidden', 'category');
00157         $mform->setType('category', PARAM_SEQUENCE);
00158 
00159         $mform->addElement('hidden', 'wizard', 'datasetitems');
00160         $mform->setType('wizard', PARAM_ALPHA);
00161     }
00162 
00163     public function validation($data, $files) {
00164         $errors = parent::validation($data, $files);
00165         $datasets = $data['dataset'];
00166         $countvalid = 0;
00167         foreach ($datasets as $key => $dataset) {
00168             if ($dataset != '0') {
00169                 $countvalid++;
00170             }
00171         }
00172         if (!$countvalid) {
00173             foreach ($datasets as $key => $dataset) {
00174                 $errors['dataset['.$key.']'] =
00175                         get_string('atleastonerealdataset', 'qtype_calculated');
00176             }
00177         }
00178         return $errors;
00179     }
00180 }
 All Data Structures Namespaces Files Functions Variables Enumerations