MDL-74075 core_badges: Check accepted criterias
[moodle.git] / mod / data / mod_form.php
blob5adcf9ce07410103b4ec1effa3f505211fcbed8d
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'));
41 if (empty($CFG->usecomments)) {
42 $mform->hardFreeze('comments');
43 $mform->setConstant('comments', 0);
46 $countoptions = array(0=>get_string('none'))+
47 (array_combine(range(1, DATA_MAX_ENTRIES), // Keys.
48 range(1, DATA_MAX_ENTRIES))); // Values.
49 /*only show fields if there are legacy values from
50 *before completionentries was added*/
51 if (!empty($this->current->requiredentries)) {
52 $group = array();
53 $group[] = $mform->createElement('select', 'requiredentries',
54 get_string('requiredentries', 'data'), $countoptions);
55 $mform->addGroup($group, 'requiredentriesgroup', get_string('requiredentries', 'data'), array(''), false);
56 $mform->addHelpButton('requiredentriesgroup', 'requiredentries', 'data');
57 $mform->addElement('html', $OUTPUT->notification( get_string('requiredentrieswarning', 'data')));
60 $mform->addElement('select', 'requiredentriestoview', get_string('requiredentriestoview', 'data'), $countoptions);
61 $mform->addHelpButton('requiredentriestoview', 'requiredentriestoview', 'data');
63 $mform->addElement('select', 'maxentries', get_string('maxentries', 'data'), $countoptions);
64 $mform->addHelpButton('maxentries', 'maxentries', 'data');
66 // ----------------------------------------------------------------------
67 $mform->addElement('header', 'availibilityhdr', get_string('availability'));
69 $mform->addElement('date_time_selector', 'timeavailablefrom', get_string('availablefromdate', 'data'),
70 array('optional' => true));
72 $mform->addElement('date_time_selector', 'timeavailableto', get_string('availabletodate', 'data'),
73 array('optional' => true));
75 $mform->addElement('date_time_selector', 'timeviewfrom', get_string('viewfromdate', 'data'),
76 array('optional' => true));
78 $mform->addElement('date_time_selector', 'timeviewto', get_string('viewtodate', 'data'),
79 array('optional' => true));
81 // ----------------------------------------------------------------------
82 if ($CFG->enablerssfeeds && $CFG->data_enablerssfeeds) {
83 $mform->addElement('header', 'rsshdr', get_string('rss'));
84 $mform->addElement('select', 'rssarticles', get_string('numberrssarticles', 'data') , $countoptions);
87 $this->standard_grading_coursemodule_elements();
89 $this->standard_coursemodule_elements();
91 //-------------------------------------------------------------------------------
92 // buttons
93 $this->add_action_buttons();
96 /**
97 * Enforce validation rules here
99 * @param array $data array of ("fieldname"=>value) of submitted data
100 * @param array $files array of uploaded files "element_name"=>tmp_file_path
101 * @return array
103 public function validation($data, $files) {
104 $errors = parent::validation($data, $files);
106 // Check open and close times are consistent.
107 if ($data['timeavailablefrom'] && $data['timeavailableto'] &&
108 $data['timeavailableto'] < $data['timeavailablefrom']) {
109 $errors['timeavailableto'] = get_string('availabletodatevalidation', 'data');
111 if ($data['timeviewfrom'] && $data['timeviewto'] &&
112 $data['timeviewto'] < $data['timeviewfrom']) {
113 $errors['timeviewto'] = get_string('viewtodatevalidation', 'data');
116 return $errors;
120 * Display module-specific activity completion rules.
121 * Part of the API defined by moodleform_mod
122 * @return array Array of string IDs of added items, empty array if none
124 public function add_completion_rules() {
125 $mform = & $this->_form;
126 $group = array();
127 $group[] = $mform->createElement('checkbox', 'completionentriesenabled', '',
128 get_string('completionentriescount', 'data'));
129 $group[] = $mform->createElement('text', 'completionentries',
130 get_string('completionentriescount', 'data'), array('size' => '1'));
132 $mform->addGroup($group, 'completionentriesgroup', get_string('completionentries', 'data'),
133 array(' '), false);
134 $mform->disabledIf('completionentries', 'completionentriesenabled', 'notchecked');
135 $mform->setDefault('completionentries', 1);
136 $mform->setType('completionentries', PARAM_INT);
137 /* This ensures the elements are disabled unless completion rules are enabled */
138 return array('completionentriesgroup');
142 * Called during validation. Indicates if a module-specific completion rule is selected.
144 * @param array $data
145 * @return bool True if one or more rules is enabled, false if none are.
147 public function completion_rule_enabled($data) {
148 return ($data['completionentries'] != 0);
152 * Set up the completion checkbox which is not part of standard data.
154 * @param array $defaultvalues
157 public function data_preprocessing(&$defaultvalues) {
158 parent::data_preprocessing($defaultvalues);
159 $defaultvalues['completionentriesenabled'] = !empty($defaultvalues['completionentries']) ? 1 : 0;
160 if (empty($defaultvalues['completionentries'])) {
161 $defaultvalues['completionentries'] = 1;
166 * Allows modules to modify the data returned by form get_data().
167 * This method is also called in the bulk activity completion form.
169 * Only available on moodleform_mod.
171 * @param stdClass $data the form data to be modified.
173 public function data_postprocessing($data) {
174 parent::data_postprocessing($data);
175 if (!empty($data->completionunlocked)) {
176 $autocompletion = !empty($data->completion) && $data->completion == COMPLETION_TRACKING_AUTOMATIC;
177 if (empty($data->completionentriesenabled) || !$autocompletion) {
178 $data->completionentries = 0;