|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 if (!defined('MOODLE_INTERNAL')) { 00003 die('Direct access to this script is forbidden.'); 00004 } 00005 00006 require_once ($CFG->dirroot.'/course/moodleform_mod.php'); 00007 00008 class mod_choice_mod_form extends moodleform_mod { 00009 00010 function definition() { 00011 global $CFG, $CHOICE_SHOWRESULTS, $CHOICE_PUBLISH, $CHOICE_DISPLAY, $DB; 00012 00013 $mform =& $this->_form; 00014 00015 //------------------------------------------------------------------------------- 00016 $mform->addElement('header', 'general', get_string('general', 'form')); 00017 00018 $mform->addElement('text', 'name', get_string('choicename', 'choice'), array('size'=>'64')); 00019 if (!empty($CFG->formatstringstriptags)) { 00020 $mform->setType('name', PARAM_TEXT); 00021 } else { 00022 $mform->setType('name', PARAM_CLEANHTML); 00023 } 00024 $mform->addRule('name', null, 'required', null, 'client'); 00025 00026 $this->add_intro_editor(true, get_string('chatintro', 'chat')); 00027 00028 //------------------------------------------------------------------------------- 00029 $repeatarray = array(); 00030 $repeatarray[] = &MoodleQuickForm::createElement('header', '', get_string('option','choice').' {no}'); 00031 $repeatarray[] = &MoodleQuickForm::createElement('text', 'option', get_string('option','choice')); 00032 $repeatarray[] = &MoodleQuickForm::createElement('text', 'limit', get_string('limit','choice')); 00033 $repeatarray[] = &MoodleQuickForm::createElement('hidden', 'optionid', 0); 00034 00035 $menuoptions = array(); 00036 $menuoptions[0] = get_string('disable'); 00037 $menuoptions[1] = get_string('enable'); 00038 $mform->addElement('header', 'timerestricthdr', get_string('limit', 'choice')); 00039 $mform->addElement('select', 'limitanswers', get_string('limitanswers', 'choice'), $menuoptions); 00040 $mform->addHelpButton('limitanswers', 'limitanswers', 'choice'); 00041 00042 if ($this->_instance){ 00043 $repeatno = $DB->count_records('choice_options', array('choiceid'=>$this->_instance)); 00044 $repeatno += 2; 00045 } else { 00046 $repeatno = 5; 00047 } 00048 00049 $repeateloptions = array(); 00050 $repeateloptions['limit']['default'] = 0; 00051 $repeateloptions['limit']['disabledif'] = array('limitanswers', 'eq', 0); 00052 $repeateloptions['limit']['rule'] = 'numeric'; 00053 00054 $repeateloptions['option']['helpbutton'] = array('choiceoptions', 'choice'); 00055 $mform->setType('option', PARAM_CLEANHTML); 00056 00057 $mform->setType('optionid', PARAM_INT); 00058 00059 $this->repeat_elements($repeatarray, $repeatno, 00060 $repeateloptions, 'option_repeats', 'option_add_fields', 3); 00061 00062 00063 00064 00065 //------------------------------------------------------------------------------- 00066 $mform->addElement('header', 'timerestricthdr', get_string('timerestrict', 'choice')); 00067 $mform->addElement('checkbox', 'timerestrict', get_string('timerestrict', 'choice')); 00068 00069 $mform->addElement('date_time_selector', 'timeopen', get_string("choiceopen", "choice")); 00070 $mform->disabledIf('timeopen', 'timerestrict'); 00071 00072 $mform->addElement('date_time_selector', 'timeclose', get_string("choiceclose", "choice")); 00073 $mform->disabledIf('timeclose', 'timerestrict'); 00074 00075 //------------------------------------------------------------------------------- 00076 $mform->addElement('header', 'miscellaneoussettingshdr', get_string('miscellaneoussettings', 'form')); 00077 00078 $mform->addElement('select', 'display', get_string("displaymode","choice"), $CHOICE_DISPLAY); 00079 00080 $mform->addElement('select', 'showresults', get_string("publish", "choice"), $CHOICE_SHOWRESULTS); 00081 00082 $mform->addElement('select', 'publish', get_string("privacy", "choice"), $CHOICE_PUBLISH); 00083 $mform->disabledIf('publish', 'showresults', 'eq', 0); 00084 00085 $mform->addElement('selectyesno', 'allowupdate', get_string("allowupdate", "choice")); 00086 00087 $mform->addElement('selectyesno', 'showunanswered', get_string("showunanswered", "choice")); 00088 00089 00090 //------------------------------------------------------------------------------- 00091 $this->standard_coursemodule_elements(); 00092 //------------------------------------------------------------------------------- 00093 $this->add_action_buttons(); 00094 } 00095 00096 function data_preprocessing(&$default_values){ 00097 global $DB; 00098 if (!empty($this->_instance) && ($options = $DB->get_records_menu('choice_options',array('choiceid'=>$this->_instance), 'id', 'id,text')) 00099 && ($options2 = $DB->get_records_menu('choice_options', array('choiceid'=>$this->_instance), 'id', 'id,maxanswers')) ) { 00100 $choiceids=array_keys($options); 00101 $options=array_values($options); 00102 $options2=array_values($options2); 00103 00104 foreach (array_keys($options) as $key){ 00105 $default_values['option['.$key.']'] = $options[$key]; 00106 $default_values['limit['.$key.']'] = $options2[$key]; 00107 $default_values['optionid['.$key.']'] = $choiceids[$key]; 00108 } 00109 00110 } 00111 if (empty($default_values['timeopen'])) { 00112 $default_values['timerestrict'] = 0; 00113 } else { 00114 $default_values['timerestrict'] = 1; 00115 } 00116 00117 } 00118 00119 function validation($data, $files) { 00120 $errors = parent::validation($data, $files); 00121 00122 $choices = 0; 00123 foreach ($data['option'] as $option){ 00124 if (trim($option) != ''){ 00125 $choices++; 00126 } 00127 } 00128 00129 if ($choices < 1) { 00130 $errors['option[0]'] = get_string('atleastoneoption', 'choice'); 00131 } 00132 00133 return $errors; 00134 } 00135 00136 function get_data() { 00137 $data = parent::get_data(); 00138 if (!$data) { 00139 return false; 00140 } 00141 // Set up completion section even if checkbox is not ticked 00142 if (empty($data->completionsection)) { 00143 $data->completionsection=0; 00144 } 00145 return $data; 00146 } 00147 00148 function add_completion_rules() { 00149 $mform =& $this->_form; 00150 00151 $mform->addElement('checkbox', 'completionsubmit', '', get_string('completionsubmit', 'choice')); 00152 return array('completionsubmit'); 00153 } 00154 00155 function completion_rule_enabled($data) { 00156 return !empty($data['completionsubmit']); 00157 } 00158 } 00159