MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / choice / mod_form.php
blob2a0ae3a05a19e1f928101bd67e64787a29b9212e
1 <?php
2 require_once ($CFG->dirroot.'/course/moodleform_mod.php');
4 class mod_choice_mod_form extends moodleform_mod {
6 function definition() {
7 global $CHOICE_SHOWRESULTS, $CHOICE_PUBLISH, $CHOICE_DISPLAY;
9 $mform =& $this->_form;
11 //-------------------------------------------------------------------------------
12 $mform->addElement('header', 'general', get_string('general', 'form'));
14 $mform->addElement('text', 'name', get_string('choicename', 'choice'), array('size'=>'64'));
15 $mform->setType('name', PARAM_TEXT);
16 $mform->addRule('name', null, 'required', null, 'client');
18 $mform->addElement('htmleditor', 'text', get_string('choicetext', 'choice'));
19 $mform->setType('text', PARAM_RAW);
20 $mform->addRule('text', null, 'required', null, 'client');
21 $mform->setHelpButton('text', array('writing', 'questions', 'richtext'), false, 'editorhelpbutton');
23 $mform->addElement('format', 'format', get_string('format'));
25 //-------------------------------------------------------------------------------
26 $repeatarray=array();
27 $repeatarray[] = &MoodleQuickForm::createElement('header', '', get_string('choice','choice').' {no}');
28 $repeatarray[] = &MoodleQuickForm::createElement('text', 'option', get_string('choice','choice'));
29 $repeatarray[] = &MoodleQuickForm::createElement('text', 'limit', get_string('limit','choice'));
30 $repeatarray[] = &MoodleQuickForm::createElement('hidden', 'optionid', 0);
32 $menuoptions=array();
33 $menuoptions[0] = get_string('disable');
34 $menuoptions[1] = get_string('enable');
35 $mform->addElement('header', 'timerestricthdr', get_string('limit', 'choice'));
36 $mform->addElement('select', 'limitanswers', get_string('limitanswers', 'choice'), $menuoptions);
37 $mform->setHelpButton('limitanswers', array('limit', get_string('limit', 'choice'), 'choice'));
39 if ($this->_instance){
40 $repeatno=count_records('choice_options', 'choiceid', $this->_instance);
41 $repeatno += 2;
42 } else {
43 $repeatno = 5;
46 $repeateloptions = array();
47 $repeateloptions['limit']['default'] = 0;
48 $repeateloptions['limit']['disabledif'] = array('limitanswers', 'eq', 0);
49 $mform->setType('limit', PARAM_INT);
51 $repeateloptions['option']['helpbutton'] = array('options', get_string('modulenameplural', 'choice'), 'choice');
52 $mform->setType('option', PARAM_TEXT);
54 $mform->setType('optionid', PARAM_INT);
56 $this->repeat_elements($repeatarray, $repeatno,
57 $repeateloptions, 'option_repeats', 'option_add_fields', 3);
62 //-------------------------------------------------------------------------------
63 $mform->addElement('header', 'timerestricthdr', get_string('timerestrict', 'choice'));
64 $mform->addElement('checkbox', 'timerestrict', get_string('timerestrict', 'choice'));
65 $mform->setHelpButton('timerestrict', array("timerestrict", get_string("timerestrict","choice"), "choice"));
68 $mform->addElement('date_time_selector', 'timeopen', get_string("choiceopen", "choice"));
69 $mform->disabledIf('timeopen', 'timerestrict');
71 $mform->addElement('date_time_selector', 'timeclose', get_string("choiceclose", "choice"));
72 $mform->disabledIf('timeclose', 'timerestrict');
74 //-------------------------------------------------------------------------------
75 $mform->addElement('header', 'miscellaneoussettingshdr', get_string('miscellaneoussettings', 'form'));
77 $mform->addElement('select', 'display', get_string("displaymode","choice"), $CHOICE_DISPLAY);
79 $mform->addElement('select', 'showresults', get_string("publish", "choice"), $CHOICE_SHOWRESULTS);
81 $mform->addElement('select', 'publish', get_string("privacy", "choice"), $CHOICE_PUBLISH);
82 $mform->disabledIf('publish', 'showresults', 'eq', 0);
84 $mform->addElement('selectyesno', 'allowupdate', get_string("allowupdate", "choice"));
86 $mform->addElement('selectyesno', 'showunanswered', get_string("showunanswered", "choice"));
89 //-------------------------------------------------------------------------------
90 $features = new stdClass;
91 $features->groups = true;
92 $features->groupings = true;
93 $features->groupmembersonly = true;
94 $this->standard_coursemodule_elements($features);
95 //-------------------------------------------------------------------------------
96 $this->add_action_buttons();
99 function data_preprocessing(&$default_values){
100 if (!empty($this->_instance) && ($options = get_records_menu('choice_options','choiceid', $this->_instance, 'id', 'id,text'))
101 && ($options2 = get_records_menu('choice_options','choiceid', $this->_instance, 'id', 'id,maxanswers')) ) {
102 $choiceids=array_keys($options);
103 $options=array_values($options);
104 $options2=array_values($options2);
106 foreach (array_keys($options) as $key){
107 $default_values['option['.$key.']'] = $options[$key];
108 $default_values['limit['.$key.']'] = $options2[$key];
109 $default_values['optionid['.$key.']'] = $choiceids[$key];
113 if (empty($default_values['timeopen'])) {
114 $default_values['timerestrict'] = 0;
115 } else {
116 $default_values['timerestrict'] = 1;
121 function validation($data){
122 $errors = parent::validation($data);
123 if ($errors === true) {
124 $errors = array();
127 $choices = 0;
128 foreach ($data['option'] as $option){
129 if (trim($option) != ''){
130 $choices++;
134 if ($choices < 1) {
135 $errors['option[0]'] = get_string('fillinatleastoneoption', 'choice');
138 if ($choices < 2) {
139 $errors['option[1]'] = get_string('fillinatleastoneoption', 'choice');
142 if (count($errors) == 0) {
143 return true;
144 } else {
145 return $errors;