Merge branch 'MDL-28282-master' of git://github.com/ankitagarwal/moodle
[moodle.git] / mod / choice / mod_form.php
1 <?php
2 if (!defined('MOODLE_INTERNAL')) {
3     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
4 }
6 require_once ($CFG->dirroot.'/course/moodleform_mod.php');
8 class mod_choice_mod_form extends moodleform_mod {
10     function definition() {
11         global $CFG, $CHOICE_SHOWRESULTS, $CHOICE_PUBLISH, $CHOICE_DISPLAY, $DB;
13         $mform    =& $this->_form;
15 //-------------------------------------------------------------------------------
16         $mform->addElement('header', 'general', get_string('general', 'form'));
18         $mform->addElement('text', 'name', get_string('choicename', 'choice'), array('size'=>'64'));
19         if (!empty($CFG->formatstringstriptags)) {
20             $mform->setType('name', PARAM_TEXT);
21         } else {
22             $mform->setType('name', PARAM_CLEANHTML);
23         }
24         $mform->addRule('name', null, 'required', null, 'client');
26         $this->add_intro_editor(true, get_string('chatintro', 'chat'));
28 //-------------------------------------------------------------------------------
29         $repeatarray = array();
30         $repeatarray[] = &MoodleQuickForm::createElement('header', '', get_string('option','choice').' {no}');
31         $repeatarray[] = &MoodleQuickForm::createElement('text', 'option', get_string('option','choice'));
32         $repeatarray[] = &MoodleQuickForm::createElement('text', 'limit', get_string('limit','choice'));
33         $repeatarray[] = &MoodleQuickForm::createElement('hidden', 'optionid', 0);
35         $menuoptions = array();
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);
40         $mform->addHelpButton('limitanswers', 'limitanswers', 'choice');
42         if ($this->_instance){
43             $repeatno = $DB->count_records('choice_options', array('choiceid'=>$this->_instance));
44             $repeatno += 2;
45         } else {
46             $repeatno = 5;
47         }
49         $repeateloptions = array();
50         $repeateloptions['limit']['default'] = 0;
51         $repeateloptions['limit']['disabledif'] = array('limitanswers', 'eq', 0);
52         $repeateloptions['limit']['rule'] = 'numeric';
53         $mform->setType('limit', PARAM_INT);
55         $repeateloptions['option']['helpbutton'] = array('choiceoptions', 'choice');
56         $mform->setType('option', PARAM_CLEANHTML);
58         $mform->setType('optionid', PARAM_INT);
60         $this->repeat_elements($repeatarray, $repeatno,
61                     $repeateloptions, 'option_repeats', 'option_add_fields', 3);
66 //-------------------------------------------------------------------------------
67         $mform->addElement('header', 'timerestricthdr', get_string('timerestrict', 'choice'));
68         $mform->addElement('checkbox', 'timerestrict', get_string('timerestrict', 'choice'));
70         $mform->addElement('date_time_selector', 'timeopen', get_string("choiceopen", "choice"));
71         $mform->disabledIf('timeopen', 'timerestrict');
73         $mform->addElement('date_time_selector', 'timeclose', get_string("choiceclose", "choice"));
74         $mform->disabledIf('timeclose', 'timerestrict');
76 //-------------------------------------------------------------------------------
77         $mform->addElement('header', 'miscellaneoussettingshdr', get_string('miscellaneoussettings', 'form'));
79         $mform->addElement('select', 'display', get_string("displaymode","choice"), $CHOICE_DISPLAY);
81         $mform->addElement('select', 'showresults', get_string("publish", "choice"), $CHOICE_SHOWRESULTS);
83         $mform->addElement('select', 'publish', get_string("privacy", "choice"), $CHOICE_PUBLISH);
84         $mform->disabledIf('publish', 'showresults', 'eq', 0);
86         $mform->addElement('selectyesno', 'allowupdate', get_string("allowupdate", "choice"));
88         $mform->addElement('selectyesno', 'showunanswered', get_string("showunanswered", "choice"));
91 //-------------------------------------------------------------------------------
92         $this->standard_coursemodule_elements();
93 //-------------------------------------------------------------------------------
94         $this->add_action_buttons();
95     }
97     function data_preprocessing(&$default_values){
98         global $DB;
99         if (!empty($this->_instance) && ($options = $DB->get_records_menu('choice_options',array('choiceid'=>$this->_instance), 'id', 'id,text'))
100                && ($options2 = $DB->get_records_menu('choice_options', array('choiceid'=>$this->_instance), 'id', 'id,maxanswers')) ) {
101             $choiceids=array_keys($options);
102             $options=array_values($options);
103             $options2=array_values($options2);
105             foreach (array_keys($options) as $key){
106                 $default_values['option['.$key.']'] = $options[$key];
107                 $default_values['limit['.$key.']'] = $options2[$key];
108                 $default_values['optionid['.$key.']'] = $choiceids[$key];
109             }
111         }
112         if (empty($default_values['timeopen'])) {
113             $default_values['timerestrict'] = 0;
114         } else {
115             $default_values['timerestrict'] = 1;
116         }
118     }
120     function validation($data, $files) {
121         $errors = parent::validation($data, $files);
123         $choices = 0;
124         foreach ($data['option'] as $option){
125             if (trim($option) != ''){
126                 $choices++;
127             }
128         }
130         if ($choices < 1) {
131            $errors['option[0]'] = get_string('atleastoneoption', 'choice');
132         }
134         return $errors;
135     }
137     function get_data() {
138         $data = parent::get_data();
139         if (!$data) {
140             return false;
141         }
142         // Set up completion section even if checkbox is not ticked
143         if (empty($data->completionsection)) {
144             $data->completionsection=0;
145         }
146         return $data;
147     }
149     function add_completion_rules() {
150         $mform =& $this->_form;
152         $mform->addElement('checkbox', 'completionsubmit', '', get_string('completionsubmit', 'choice'));
153         return array('completionsubmit');
154     }
156     function completion_rule_enabled($data) {
157         return !empty($data['completionsubmit']);
158     }