MDL-64906 mod: add support for hideif to activity modules
[moodle.git] / mod / data / mod_form.php
blob68406806d4b387f88929447caca5ad0df6e0f832
1 <?php
2 if (!defined('MOODLE_INTERNAL')) {
3 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
6 require_once ($CFG->dirroot.'/course/moodleform_mod.php');
8 class mod_data_mod_form extends moodleform_mod {
10 function definition() {
11 global $CFG, $DB, $OUTPUT;
13 $mform =& $this->_form;
15 //-------------------------------------------------------------------------------
16 $mform->addElement('header', 'general', get_string('general', 'form'));
18 $mform->addElement('text', 'name', get_string('name'), array('size'=>'64'));
19 if (!empty($CFG->formatstringstriptags)) {
20 $mform->setType('name', PARAM_TEXT);
21 } else {
22 $mform->setType('name', PARAM_CLEANHTML);
24 $mform->addRule('name', null, 'required', null, 'client');
25 $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
27 $this->standard_intro_elements(get_string('intro', 'data'));
29 // ----------------------------------------------------------------------
30 $mform->addElement('header', 'entrieshdr', get_string('entries', 'data'));
32 $mform->addElement('selectyesno', 'approval', get_string('requireapproval', 'data'));
33 $mform->addHelpButton('approval', 'requireapproval', 'data');
35 $mform->addElement('selectyesno', 'manageapproved', get_string('manageapproved', 'data'));
36 $mform->addHelpButton('manageapproved', 'manageapproved', 'data');
37 $mform->setDefault('manageapproved', 1);
38 $mform->hideIf('manageapproved', 'approval', 'eq', 0);
40 $mform->addElement('selectyesno', 'comments', get_string('allowcomments', 'data'));
42 $countoptions = array(0=>get_string('none'))+
43 (array_combine(range(1, DATA_MAX_ENTRIES), // Keys.
44 range(1, DATA_MAX_ENTRIES))); // Values.
45 /*only show fields if there are legacy values from
46 *before completionentries was added*/
47 if (!empty($this->current->requiredentries)) {
48 $group = array();
49 $group[] = $mform->createElement('select', 'requiredentries',
50 get_string('requiredentries', 'data'), $countoptions);
51 $mform->addGroup($group, 'requiredentriesgroup', get_string('requiredentries', 'data'), array(''), false);
52 $mform->addHelpButton('requiredentriesgroup', 'requiredentries', 'data');
53 $mform->addElement('html', $OUTPUT->notification( get_string('requiredentrieswarning', 'data')));
56 $mform->addElement('select', 'requiredentriestoview', get_string('requiredentriestoview', 'data'), $countoptions);
57 $mform->addHelpButton('requiredentriestoview', 'requiredentriestoview', 'data');
59 $mform->addElement('select', 'maxentries', get_string('maxentries', 'data'), $countoptions);
60 $mform->addHelpButton('maxentries', 'maxentries', 'data');
62 // ----------------------------------------------------------------------
63 $mform->addElement('header', 'availibilityhdr', get_string('availability'));
65 $mform->addElement('date_time_selector', 'timeavailablefrom', get_string('availablefromdate', 'data'),
66 array('optional' => true));
68 $mform->addElement('date_time_selector', 'timeavailableto', get_string('availabletodate', 'data'),
69 array('optional' => true));
71 $mform->addElement('date_time_selector', 'timeviewfrom', get_string('viewfromdate', 'data'),
72 array('optional' => true));
74 $mform->addElement('date_time_selector', 'timeviewto', get_string('viewtodate', 'data'),
75 array('optional' => true));
77 // ----------------------------------------------------------------------
78 if ($CFG->enablerssfeeds && $CFG->data_enablerssfeeds) {
79 $mform->addElement('header', 'rsshdr', get_string('rss'));
80 $mform->addElement('select', 'rssarticles', get_string('numberrssarticles', 'data') , $countoptions);
83 $this->standard_grading_coursemodule_elements();
85 $this->standard_coursemodule_elements();
87 //-------------------------------------------------------------------------------
88 // buttons
89 $this->add_action_buttons();
92 /**
93 * Enforce validation rules here
95 * @param array $data array of ("fieldname"=>value) of submitted data
96 * @param array $files array of uploaded files "element_name"=>tmp_file_path
97 * @return array
98 **/
99 public function validation($data, $files) {
100 $errors = parent::validation($data, $files);
102 // Check open and close times are consistent.
103 if ($data['timeavailablefrom'] && $data['timeavailableto'] &&
104 $data['timeavailableto'] < $data['timeavailablefrom']) {
105 $errors['timeavailableto'] = get_string('availabletodatevalidation', 'data');
107 if ($data['timeviewfrom'] && $data['timeviewto'] &&
108 $data['timeviewto'] < $data['timeviewfrom']) {
109 $errors['timeviewto'] = get_string('viewtodatevalidation', 'data');
112 return $errors;
116 * Display module-specific activity completion rules.
117 * Part of the API defined by moodleform_mod
118 * @return array Array of string IDs of added items, empty array if none
120 public function add_completion_rules() {
121 $mform = & $this->_form;
122 $group = array();
123 $group[] = $mform->createElement('checkbox', 'completionentriesenabled', '',
124 get_string('completionentriescount', 'data'));
125 $group[] = $mform->createElement('text', 'completionentries',
126 get_string('completionentriescount', 'data'), array('size' => '1'));
128 $mform->addGroup($group, 'completionentriesgroup', get_string('completionentries', 'data'),
129 array(' '), false);
130 $mform->disabledIf('completionentries', 'completionentriesenabled', 'notchecked');
131 $mform->setDefault('completionentries', 1);
132 $mform->setType('completionentries', PARAM_INT);
133 /* This ensures the elements are disabled unless completion rules are enabled */
134 return array('completionentriesgroup');
138 * Called during validation. Indicates if a module-specific completion rule is selected.
140 * @param array $data
141 * @return bool True if one or more rules is enabled, false if none are.
143 public function completion_rule_enabled($data) {
144 return ($data['completionentries'] != 0);
148 * Set up the completion checkbox which is not part of standard data.
150 * @param array $defaultvalues
153 public function data_preprocessing(&$defaultvalues) {
154 parent::data_preprocessing($defaultvalues);
155 $defaultvalues['completionentriesenabled'] = !empty($defaultvalues['completionentries']) ? 1 : 0;
156 if (empty($defaultvalues['completionentries'])) {
157 $defaultvalues['completionentries'] = 1;
162 * Allows modules to modify the data returned by form get_data().
163 * This method is also called in the bulk activity completion form.
165 * Only available on moodleform_mod.
167 * @param stdClass $data the form data to be modified.
169 public function data_postprocessing($data) {
170 parent::data_postprocessing($data);
171 if (!empty($data->completionunlocked)) {
172 $autocompletion = !empty($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC;
173 if (empty($data->completionentriesenabled) || !$autocompletion) {
174 $data->completionentries = 0;