MDL-9628 Fixed typos and added language strings. Corrected a bug that prevented a...
[moodle-pu.git] / mod / choice / mod_form.php
blob0760054af863ae6b33eac621cf1d8154a3a83d40
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 $this->standard_coursemodule_elements();
91 //-------------------------------------------------------------------------------
92 $this->add_action_buttons();
95 function data_preprocessing(&$default_values){
96 if (!empty($this->_instance) && ($options = get_records_menu('choice_options','choiceid', $this->_instance, 'id', 'id,text'))
97 && ($options2 = get_records_menu('choice_options','choiceid', $this->_instance, 'id', 'id,maxanswers')) ) {
98 $choiceids=array_keys($options);
99 $options=array_values($options);
100 $options2=array_values($options2);
102 foreach (array_keys($options) as $key){
103 $default_values['option['.$key.']'] = $options[$key];
104 $default_values['limit['.$key.']'] = $options2[$key];
105 $default_values['optionid['.$key.']'] = $choiceids[$key];
109 if (empty($default_values['timeopen'])) {
110 $default_values['timerestrict'] = 0;
111 } else {
112 $default_values['timerestrict'] = 1;
117 function validation($data){
118 $choices=0;
119 foreach ($data['option'] as $option){
120 if (trim($option)!=''){
121 $choices++;
124 if ($choices>1){
125 return true;
126 } elseif ($choices==0) {
127 return array('option[0]'=>get_string('fillinatleastoneoption', 'choice'));
128 } else {
129 return array('option[1]'=>get_string('fillinatleastoneoption', 'choice'));