MDL-18910 moving modedit features to modname_supports()
[moodle.git] / mod / choice / mod_form.php
CommitLineData
ebd3c7ac 1<?php
23a7718b 2require_once ($CFG->dirroot.'/course/moodleform_mod.php');
ebd3c7ac 3
f07b9627 4class mod_choice_mod_form extends moodleform_mod {
ebd3c7ac 5
c12b1f67 6 function definition() {
8eb1d25f 7 global $CFG, $CHOICE_SHOWRESULTS, $CHOICE_PUBLISH, $CHOICE_DISPLAY, $DB;
ebd3c7ac 8
c12b1f67 9 $mform =& $this->_form;
ebd3c7ac 10
11//-------------------------------------------------------------------------------
12 $mform->addElement('header', 'general', get_string('general', 'form'));
13
26de8d35 14 $mform->addElement('text', 'name', get_string('choicename', 'choice'), array('size'=>'64'));
8eb1d25f 15 if (!empty($CFG->formatstringstriptags)) {
16 $mform->setType('name', PARAM_TEXT);
17 } else {
18 $mform->setType('name', PARAM_CLEAN);
19 }
c12b1f67 20 $mform->addRule('name', null, 'required', null, 'client');
ebd3c7ac 21
a8801a8b 22 $mform->addElement('htmleditor', 'intro', get_string('choicetext', 'choice'));
23 $mform->setType('intro', PARAM_RAW);
24 $mform->addRule('intro', null, 'required', null, 'client');
25 $mform->setHelpButton('intro', array('writing', 'questions', 'richtext2'), false, 'editorhelpbutton');
ebd3c7ac 26
a8801a8b 27 $mform->addElement('format', 'introformat', get_string('format'));
ebd3c7ac 28
19194f82 29//-------------------------------------------------------------------------------
ebd3c7ac 30 $repeatarray=array();
ca538720 31 $repeatarray[] = &MoodleQuickForm::createElement('header', '', get_string('option','choice').' {no}');
32 $repeatarray[] = &MoodleQuickForm::createElement('text', 'option', get_string('option','choice'));
ebd3c7ac 33 $repeatarray[] = &MoodleQuickForm::createElement('text', 'limit', get_string('limit','choice'));
34 $repeatarray[] = &MoodleQuickForm::createElement('hidden', 'optionid', 0);
35
c12b1f67 36 $menuoptions=array();
37 $menuoptions[0] = get_string('disable');
38 $menuoptions[1] = get_string('enable');
39 $mform->addElement('header', 'timerestricthdr', get_string('limit', 'choice'));
40 $mform->addElement('select', 'limitanswers', get_string('limitanswers', 'choice'), $menuoptions);
41 $mform->setHelpButton('limitanswers', array('limit', get_string('limit', 'choice'), 'choice'));
42
ebd3c7ac 43 if ($this->_instance){
c18269c7 44 $repeatno = $DB->count_records('choice_options', array('choiceid'=>$this->_instance));
74e01e62 45 $repeatno += 2;
ebd3c7ac 46 } else {
74e01e62 47 $repeatno = 5;
ebd3c7ac 48 }
49
ebff6e2c 50 $repeateloptions = array();
3a298174 51 $repeateloptions['limit']['default'] = 0;
52 $repeateloptions['limit']['disabledif'] = array('limitanswers', 'eq', 0);
53 $mform->setType('limit', PARAM_INT);
54
55 $repeateloptions['option']['helpbutton'] = array('options', get_string('modulenameplural', 'choice'), 'choice');
08aeb4aa 56 $mform->setType('option', PARAM_CLEAN);
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
63
64
ebd3c7ac 65
66//-------------------------------------------------------------------------------
67 $mform->addElement('header', 'timerestricthdr', get_string('timerestrict', 'choice'));
68 $mform->addElement('checkbox', 'timerestrict', get_string('timerestrict', 'choice'));
c12b1f67 69 $mform->setHelpButton('timerestrict', array("timerestrict", get_string("timerestrict","choice"), "choice"));
ebd3c7ac 70
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//-------------------------------------------------------------------------------
6d5b7706 94 $this->standard_coursemodule_elements($features);
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
a78890d5 122 function validation($data, $files) {
123 $errors = parent::validation($data, $files);
60243313 124
125 $choices = 0;
c12b1f67 126 foreach ($data['option'] as $option){
60243313 127 if (trim($option) != ''){
c12b1f67 128 $choices++;
129 }
130 }
60243313 131
132 if ($choices < 1) {
133 $errors['option[0]'] = get_string('fillinatleastoneoption', 'choice');
49eacbe3 134 }
135
60243313 136 if ($choices < 2) {
137 $errors['option[1]'] = get_string('fillinatleastoneoption', 'choice');
138 }
139
a78890d5 140 return $errors;
c12b1f67 141 }
49eacbe3 142
ebd3c7ac 143}
39504b69 144?>