MDL-38736 mod_choice: fix missing param type
[moodle.git] / mod / choice / mod_form.php
CommitLineData
ebd3c7ac 1<?php
bfebaf64
MD
2if (!defined('MOODLE_INTERNAL')) {
3 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
4}
5
23a7718b 6require_once ($CFG->dirroot.'/course/moodleform_mod.php');
ebd3c7ac 7
f07b9627 8class mod_choice_mod_form extends moodleform_mod {
ebd3c7ac 9
c12b1f67 10 function definition() {
8eb1d25f 11 global $CFG, $CHOICE_SHOWRESULTS, $CHOICE_PUBLISH, $CHOICE_DISPLAY, $DB;
ebd3c7ac 12
c12b1f67 13 $mform =& $this->_form;
ebd3c7ac 14
15//-------------------------------------------------------------------------------
16 $mform->addElement('header', 'general', get_string('general', 'form'));
17
26de8d35 18 $mform->addElement('text', 'name', get_string('choicename', 'choice'), array('size'=>'64'));
8eb1d25f 19 if (!empty($CFG->formatstringstriptags)) {
20 $mform->setType('name', PARAM_TEXT);
21 } else {
b8ea3041 22 $mform->setType('name', PARAM_CLEANHTML);
8eb1d25f 23 }
c12b1f67 24 $mform->addRule('name', null, 'required', null, 'client');
ebd3c7ac 25
9b010a10 26 $this->add_intro_editor(true, get_string('chatintro', 'chat'));
ebd3c7ac 27
19194f82 28//-------------------------------------------------------------------------------
9b010a10 29 $repeatarray = array();
fabbf439
PS
30 $repeatarray[] = $mform->createElement('header', '', get_string('option','choice').' {no}');
31 $repeatarray[] = $mform->createElement('text', 'option', get_string('option','choice'));
32 $repeatarray[] = $mform->createElement('text', 'limit', get_string('limit','choice'));
33 $repeatarray[] = $mform->createElement('hidden', 'optionid', 0);
ebd3c7ac 34
9b010a10 35 $menuoptions = array();
c12b1f67 36 $menuoptions[0] = get_string('disable');
37 $menuoptions[1] = get_string('enable');
9d2a7df1 38 $mform->addElement('header', 'limithdr', get_string('limit', 'choice'));
c12b1f67 39 $mform->addElement('select', 'limitanswers', get_string('limitanswers', 'choice'), $menuoptions);
80de705f 40 $mform->addHelpButton('limitanswers', 'limitanswers', 'choice');
c12b1f67 41
ebd3c7ac 42 if ($this->_instance){
c18269c7 43 $repeatno = $DB->count_records('choice_options', array('choiceid'=>$this->_instance));
74e01e62 44 $repeatno += 2;
ebd3c7ac 45 } else {
74e01e62 46 $repeatno = 5;
ebd3c7ac 47 }
48
ebff6e2c 49 $repeateloptions = array();
3a298174 50 $repeateloptions['limit']['default'] = 0;
51 $repeateloptions['limit']['disabledif'] = array('limitanswers', 'eq', 0);
65c77131 52 $repeateloptions['limit']['rule'] = 'numeric';
8bd18293 53 $repeateloptions['limit']['type'] = PARAM_INT;
3a298174 54
cd188086 55 $repeateloptions['option']['helpbutton'] = array('choiceoptions', 'choice');
65c77131 56 $mform->setType('option', PARAM_CLEANHTML);
3a298174 57
58 $mform->setType('optionid', PARAM_INT);
ebff6e2c 59
ebd3c7ac 60 $this->repeat_elements($repeatarray, $repeatno,
ebff6e2c 61 $repeateloptions, 'option_repeats', 'option_add_fields', 3);
ebd3c7ac 62
9d2a7df1
RK
63 // Make the first option required
64 if ($mform->elementExists('option[0]')) {
65 $mform->addRule('option[0]', get_string('atleastoneoption', 'choice'), 'required', null, 'client');
66 }
ebd3c7ac 67
68//-------------------------------------------------------------------------------
69 $mform->addElement('header', 'timerestricthdr', get_string('timerestrict', 'choice'));
70 $mform->addElement('checkbox', 'timerestrict', get_string('timerestrict', 'choice'));
ebd3c7ac 71
c12b1f67 72 $mform->addElement('date_time_selector', 'timeopen', get_string("choiceopen", "choice"));
ebd3c7ac 73 $mform->disabledIf('timeopen', 'timerestrict');
74
75 $mform->addElement('date_time_selector', 'timeclose', get_string("choiceclose", "choice"));
76 $mform->disabledIf('timeclose', 'timerestrict');
77
78//-------------------------------------------------------------------------------
79 $mform->addElement('header', 'miscellaneoussettingshdr', get_string('miscellaneoussettings', 'form'));
80
81 $mform->addElement('select', 'display', get_string("displaymode","choice"), $CHOICE_DISPLAY);
82
83 $mform->addElement('select', 'showresults', get_string("publish", "choice"), $CHOICE_SHOWRESULTS);
84
85 $mform->addElement('select', 'publish', get_string("privacy", "choice"), $CHOICE_PUBLISH);
86 $mform->disabledIf('publish', 'showresults', 'eq', 0);
87
88 $mform->addElement('selectyesno', 'allowupdate', get_string("allowupdate", "choice"));
89
90 $mform->addElement('selectyesno', 'showunanswered', get_string("showunanswered", "choice"));
91
19194f82 92
ebd3c7ac 93//-------------------------------------------------------------------------------
17da2e6f 94 $this->standard_coursemodule_elements();
ebd3c7ac 95//-------------------------------------------------------------------------------
a23f0aaf 96 $this->add_action_buttons();
c12b1f67 97 }
ebd3c7ac 98
c12b1f67 99 function data_preprocessing(&$default_values){
f33e1ed4 100 global $DB;
101 if (!empty($this->_instance) && ($options = $DB->get_records_menu('choice_options',array('choiceid'=>$this->_instance), 'id', 'id,text'))
102 && ($options2 = $DB->get_records_menu('choice_options', array('choiceid'=>$this->_instance), 'id', 'id,maxanswers')) ) {
ebd3c7ac 103 $choiceids=array_keys($options);
104 $options=array_values($options);
105 $options2=array_values($options2);
106
107 foreach (array_keys($options) as $key){
108 $default_values['option['.$key.']'] = $options[$key];
109 $default_values['limit['.$key.']'] = $options2[$key];
110 $default_values['optionid['.$key.']'] = $choiceids[$key];
111 }
112
113 }
114 if (empty($default_values['timeopen'])) {
115 $default_values['timerestrict'] = 0;
116 } else {
117 $default_values['timerestrict'] = 1;
118 }
119
c12b1f67 120 }
121
bc499733
SM
122 function get_data() {
123 $data = parent::get_data();
124 if (!$data) {
125 return false;
126 }
127 // Set up completion section even if checkbox is not ticked
128 if (empty($data->completionsection)) {
129 $data->completionsection=0;
130 }
131 return $data;
132 }
133
134 function add_completion_rules() {
135 $mform =& $this->_form;
136
137 $mform->addElement('checkbox', 'completionsubmit', '', get_string('completionsubmit', 'choice'));
138 return array('completionsubmit');
139 }
140
141 function completion_rule_enabled($data) {
142 return !empty($data['completionsubmit']);
143 }
ebd3c7ac 144}
1adbd2c3 145