Merge branch 'master_MDL-30536' of git://github.com/danmarsden/moodle
[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();
ca538720 30 $repeatarray[] = &MoodleQuickForm::createElement('header', '', get_string('option','choice').' {no}');
31 $repeatarray[] = &MoodleQuickForm::createElement('text', 'option', get_string('option','choice'));
ebd3c7ac 32 $repeatarray[] = &MoodleQuickForm::createElement('text', 'limit', get_string('limit','choice'));
33 $repeatarray[] = &MoodleQuickForm::createElement('hidden', 'optionid', 0);
34
9b010a10 35 $menuoptions = array();
c12b1f67 36 $menuoptions[0] = get_string('disable');
37 $menuoptions[1] = get_string('enable');
38 $mform->addElement('header', 'timerestricthdr', get_string('limit', 'choice'));
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';
3a298174 53
cd188086 54 $repeateloptions['option']['helpbutton'] = array('choiceoptions', 'choice');
65c77131 55 $mform->setType('option', PARAM_CLEANHTML);
3a298174 56
57 $mform->setType('optionid', PARAM_INT);
ebff6e2c 58
ebd3c7ac 59 $this->repeat_elements($repeatarray, $repeatno,
ebff6e2c 60 $repeateloptions, 'option_repeats', 'option_add_fields', 3);
ebd3c7ac 61
62
63
ebd3c7ac 64
65//-------------------------------------------------------------------------------
66 $mform->addElement('header', 'timerestricthdr', get_string('timerestrict', 'choice'));
67 $mform->addElement('checkbox', 'timerestrict', get_string('timerestrict', 'choice'));
ebd3c7ac 68
c12b1f67 69 $mform->addElement('date_time_selector', 'timeopen', get_string("choiceopen", "choice"));
ebd3c7ac 70 $mform->disabledIf('timeopen', 'timerestrict');
71
72 $mform->addElement('date_time_selector', 'timeclose', get_string("choiceclose", "choice"));
73 $mform->disabledIf('timeclose', 'timerestrict');
74
75//-------------------------------------------------------------------------------
76 $mform->addElement('header', 'miscellaneoussettingshdr', get_string('miscellaneoussettings', 'form'));
77
78 $mform->addElement('select', 'display', get_string("displaymode","choice"), $CHOICE_DISPLAY);
79
80 $mform->addElement('select', 'showresults', get_string("publish", "choice"), $CHOICE_SHOWRESULTS);
81
82 $mform->addElement('select', 'publish', get_string("privacy", "choice"), $CHOICE_PUBLISH);
83 $mform->disabledIf('publish', 'showresults', 'eq', 0);
84
85 $mform->addElement('selectyesno', 'allowupdate', get_string("allowupdate", "choice"));
86
87 $mform->addElement('selectyesno', 'showunanswered', get_string("showunanswered", "choice"));
88
19194f82 89
ebd3c7ac 90//-------------------------------------------------------------------------------
17da2e6f 91 $this->standard_coursemodule_elements();
ebd3c7ac 92//-------------------------------------------------------------------------------
a23f0aaf 93 $this->add_action_buttons();
c12b1f67 94 }
ebd3c7ac 95
c12b1f67 96 function data_preprocessing(&$default_values){
f33e1ed4 97 global $DB;
98 if (!empty($this->_instance) && ($options = $DB->get_records_menu('choice_options',array('choiceid'=>$this->_instance), 'id', 'id,text'))
99 && ($options2 = $DB->get_records_menu('choice_options', array('choiceid'=>$this->_instance), 'id', 'id,maxanswers')) ) {
ebd3c7ac 100 $choiceids=array_keys($options);
101 $options=array_values($options);
102 $options2=array_values($options2);
103
104 foreach (array_keys($options) as $key){
105 $default_values['option['.$key.']'] = $options[$key];
106 $default_values['limit['.$key.']'] = $options2[$key];
107 $default_values['optionid['.$key.']'] = $choiceids[$key];
108 }
109
110 }
111 if (empty($default_values['timeopen'])) {
112 $default_values['timerestrict'] = 0;
113 } else {
114 $default_values['timerestrict'] = 1;
115 }
116
c12b1f67 117 }
118
a78890d5 119 function validation($data, $files) {
120 $errors = parent::validation($data, $files);
60243313 121
122 $choices = 0;
c12b1f67 123 foreach ($data['option'] as $option){
60243313 124 if (trim($option) != ''){
c12b1f67 125 $choices++;
126 }
127 }
60243313 128
129 if ($choices < 1) {
aa20227e 130 $errors['option[0]'] = get_string('atleastoneoption', 'choice');
60243313 131 }
132
a78890d5 133 return $errors;
c12b1f67 134 }
49eacbe3 135
bc499733
SM
136 function get_data() {
137 $data = parent::get_data();
138 if (!$data) {
139 return false;
140 }
141 // Set up completion section even if checkbox is not ticked
142 if (empty($data->completionsection)) {
143 $data->completionsection=0;
144 }
145 return $data;
146 }
147
148 function add_completion_rules() {
149 $mform =& $this->_form;
150
151 $mform->addElement('checkbox', 'completionsubmit', '', get_string('completionsubmit', 'choice'));
152 return array('completionsubmit');
153 }
154
155 function completion_rule_enabled($data) {
156 return !empty($data['completionsubmit']);
157 }
ebd3c7ac 158}
1adbd2c3 159