| 1 | efrain | 1 | <?php
 | 
        
           |  |  | 2 | if (!defined('MOODLE_INTERNAL')) {
 | 
        
           |  |  | 3 |     die('Direct access to this script is forbidden.');    ///  It must be included from a Moodle page
 | 
        
           |  |  | 4 | }
 | 
        
           |  |  | 5 |   | 
        
           |  |  | 6 | require_once ($CFG->dirroot.'/course/moodleform_mod.php');
 | 
        
           |  |  | 7 |   | 
        
           |  |  | 8 | class mod_data_mod_form extends moodleform_mod {
 | 
        
           |  |  | 9 |   | 
        
           |  |  | 10 |     function definition() {
 | 
        
           |  |  | 11 |         global $CFG, $DB, $OUTPUT;
 | 
        
           |  |  | 12 |   | 
        
           |  |  | 13 |         $mform =& $this->_form;
 | 
        
           |  |  | 14 |   | 
        
           |  |  | 15 |         //-------------------------------------------------------------------------------
 | 
        
           |  |  | 16 |         $mform->addElement('header', 'general', get_string('general', 'form'));
 | 
        
           |  |  | 17 |   | 
        
           |  |  | 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);
 | 
        
           |  |  | 23 |         }
 | 
        
           |  |  | 24 |         $mform->addRule('name', null, 'required', null, 'client');
 | 
        
           |  |  | 25 |         $mform->addRule('name', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
 | 
        
           |  |  | 26 |   | 
        
           |  |  | 27 |         $this->standard_intro_elements(get_string('intro', 'data'));
 | 
        
           |  |  | 28 |   | 
        
           |  |  | 29 |         // ----------------------------------------------------------------------
 | 
        
           |  |  | 30 |         $mform->addElement('header', 'entrieshdr', get_string('entries', 'data'));
 | 
        
           |  |  | 31 |   | 
        
           |  |  | 32 |         $mform->addElement('selectyesno', 'approval', get_string('requireapproval', 'data'));
 | 
        
           |  |  | 33 |         $mform->addHelpButton('approval', 'requireapproval', 'data');
 | 
        
           |  |  | 34 |   | 
        
           |  |  | 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);
 | 
        
           |  |  | 39 |   | 
        
           |  |  | 40 |         $mform->addElement('selectyesno', 'comments', get_string('allowcomments', 'data'));
 | 
        
           |  |  | 41 |         if (empty($CFG->usecomments)) {
 | 
        
           |  |  | 42 |             $mform->hardFreeze('comments');
 | 
        
           |  |  | 43 |             $mform->setConstant('comments', 0);
 | 
        
           |  |  | 44 |         }
 | 
        
           |  |  | 45 |   | 
        
           |  |  | 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')));
 | 
        
           |  |  | 58 |         }
 | 
        
           |  |  | 59 |   | 
        
           |  |  | 60 |         $mform->addElement('select', 'requiredentriestoview', get_string('requiredentriestoview', 'data'), $countoptions);
 | 
        
           |  |  | 61 |         $mform->addHelpButton('requiredentriestoview', 'requiredentriestoview', 'data');
 | 
        
           |  |  | 62 |   | 
        
           |  |  | 63 |         $mform->addElement('select', 'maxentries', get_string('maxentries', 'data'), $countoptions);
 | 
        
           |  |  | 64 |         $mform->addHelpButton('maxentries', 'maxentries', 'data');
 | 
        
           |  |  | 65 |   | 
        
           |  |  | 66 |         // ----------------------------------------------------------------------
 | 
        
           |  |  | 67 |         $mform->addElement('header', 'availibilityhdr', get_string('availability'));
 | 
        
           |  |  | 68 |   | 
        
           |  |  | 69 |         $mform->addElement('date_time_selector', 'timeavailablefrom', get_string('availablefromdate', 'data'),
 | 
        
           |  |  | 70 |                            array('optional' => true));
 | 
        
           |  |  | 71 |   | 
        
           |  |  | 72 |         $mform->addElement('date_time_selector', 'timeavailableto', get_string('availabletodate', 'data'),
 | 
        
           |  |  | 73 |                            array('optional' => true));
 | 
        
           |  |  | 74 |   | 
        
           |  |  | 75 |         $mform->addElement('date_time_selector', 'timeviewfrom', get_string('viewfromdate', 'data'),
 | 
        
           |  |  | 76 |                            array('optional' => true));
 | 
        
           |  |  | 77 |   | 
        
           |  |  | 78 |         $mform->addElement('date_time_selector', 'timeviewto', get_string('viewtodate', 'data'),
 | 
        
           |  |  | 79 |                            array('optional' => true));
 | 
        
           |  |  | 80 |   | 
        
           |  |  | 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);
 | 
        
           |  |  | 85 |         }
 | 
        
           |  |  | 86 |   | 
        
           |  |  | 87 |         $this->standard_grading_coursemodule_elements();
 | 
        
           |  |  | 88 |   | 
        
           |  |  | 89 |         $this->standard_coursemodule_elements();
 | 
        
           |  |  | 90 |   | 
        
           |  |  | 91 | //-------------------------------------------------------------------------------
 | 
        
           |  |  | 92 |         // buttons
 | 
        
           |  |  | 93 |         $this->add_action_buttons();
 | 
        
           |  |  | 94 |     }
 | 
        
           |  |  | 95 |   | 
        
           |  |  | 96 |     /**
 | 
        
           |  |  | 97 |      * Enforce validation rules here
 | 
        
           |  |  | 98 |      *
 | 
        
           |  |  | 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
 | 
        
           |  |  | 102 |      **/
 | 
        
           |  |  | 103 |     public function validation($data, $files) {
 | 
        
           |  |  | 104 |         $errors = parent::validation($data, $files);
 | 
        
           |  |  | 105 |   | 
        
           |  |  | 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');
 | 
        
           |  |  | 110 |         }
 | 
        
           |  |  | 111 |         if ($data['timeviewfrom'] && $data['timeviewto'] &&
 | 
        
           |  |  | 112 |                 $data['timeviewto'] < $data['timeviewfrom']) {
 | 
        
           |  |  | 113 |             $errors['timeviewto'] = get_string('viewtodatevalidation', 'data');
 | 
        
           |  |  | 114 |         }
 | 
        
           |  |  | 115 |   | 
        
           |  |  | 116 |         return $errors;
 | 
        
           |  |  | 117 |     }
 | 
        
           |  |  | 118 |   | 
        
           |  |  | 119 |     /**
 | 
        
           |  |  | 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
 | 
        
           |  |  | 123 |      */
 | 
        
           |  |  | 124 |     public function add_completion_rules() {
 | 
        
           |  |  | 125 |         $mform = & $this->_form;
 | 
        
           |  |  | 126 |         $group = [];
 | 
        
           |  |  | 127 |   | 
        
           |  |  | 128 |         $suffix = $this->get_suffix();
 | 
        
           |  |  | 129 |         $completionentriesenabledel = 'completionentriesenabled' . $suffix;
 | 
        
           |  |  | 130 |         $group[] = $mform->createElement(
 | 
        
           |  |  | 131 |             'checkbox',
 | 
        
           |  |  | 132 |             $completionentriesenabledel,
 | 
        
           |  |  | 133 |             '',
 | 
        
           |  |  | 134 |             get_string('completionentriescount', 'data')
 | 
        
           |  |  | 135 |         );
 | 
        
           |  |  | 136 |         $completionentriesel = 'completionentries' . $suffix;
 | 
        
           |  |  | 137 |         $group[] = $mform->createElement(
 | 
        
           |  |  | 138 |             'text',
 | 
        
           |  |  | 139 |             $completionentriesel,
 | 
        
           |  |  | 140 |             get_string('completionentriescount', 'data'),
 | 
        
           |  |  | 141 |             ['size' => '1']
 | 
        
           |  |  | 142 |         );
 | 
        
           |  |  | 143 |   | 
        
           |  |  | 144 |         $completionentriesgroupel = 'completionentriesgroup' . $suffix;
 | 
        
           |  |  | 145 |         $mform->addGroup(
 | 
        
           |  |  | 146 |             $group,
 | 
        
           |  |  | 147 |             $completionentriesgroupel,
 | 
        
           |  |  | 148 |             '',
 | 
        
           |  |  | 149 |             [' '],
 | 
        
           |  |  | 150 |             false
 | 
        
           |  |  | 151 |         );
 | 
        
           |  |  | 152 |         $mform->hideIf($completionentriesel, $completionentriesenabledel, 'notchecked');
 | 
        
           |  |  | 153 |         $mform->setDefault($completionentriesel, 1);
 | 
        
           |  |  | 154 |         $mform->setType($completionentriesel, PARAM_INT);
 | 
        
           |  |  | 155 |         /* This ensures the elements are disabled unless completion rules are enabled */
 | 
        
           |  |  | 156 |         return [$completionentriesgroupel];
 | 
        
           |  |  | 157 |     }
 | 
        
           |  |  | 158 |   | 
        
           |  |  | 159 |     /**
 | 
        
           |  |  | 160 |      * Called during validation. Indicates if a module-specific completion rule is selected.
 | 
        
           |  |  | 161 |      *
 | 
        
           |  |  | 162 |      * @param array $data
 | 
        
           |  |  | 163 |      * @return bool True if one or more rules is enabled, false if none are.
 | 
        
           |  |  | 164 |      */
 | 
        
           |  |  | 165 |     public function completion_rule_enabled($data) {
 | 
        
           |  |  | 166 |         $suffix = $this->get_suffix();
 | 
        
           |  |  | 167 |         return (!empty($data['completionentriesenabled' . $suffix]) && $data['completionentries' . $suffix] != 0);
 | 
        
           |  |  | 168 |     }
 | 
        
           |  |  | 169 |   | 
        
           |  |  | 170 |       /**
 | 
        
           |  |  | 171 |        * Set up the completion checkbox which is not part of standard data.
 | 
        
           |  |  | 172 |        *
 | 
        
           |  |  | 173 |        * @param array $defaultvalues
 | 
        
           |  |  | 174 |        *
 | 
        
           |  |  | 175 |        */
 | 
        
           |  |  | 176 |     public function data_preprocessing(&$defaultvalues) {
 | 
        
           |  |  | 177 |         parent::data_preprocessing($defaultvalues);
 | 
        
           |  |  | 178 |   | 
        
           |  |  | 179 |         $suffix = $this->get_suffix();
 | 
        
           |  |  | 180 |         $completionentriesenabledel = 'completionentriesenabled' . $suffix;
 | 
        
           |  |  | 181 |         $completionentriesel = 'completionentries' . $suffix;
 | 
        
           |  |  | 182 |         $defaultvalues[$completionentriesenabledel] = !empty($defaultvalues[$completionentriesel]) ? 1 : 0;
 | 
        
           |  |  | 183 |         if (empty($defaultvalues[$completionentriesel])) {
 | 
        
           |  |  | 184 |             $defaultvalues[$completionentriesel] = 1;
 | 
        
           |  |  | 185 |         }
 | 
        
           |  |  | 186 |     }
 | 
        
           |  |  | 187 |   | 
        
           |  |  | 188 |     /**
 | 
        
           |  |  | 189 |      * Allows modules to modify the data returned by form get_data().
 | 
        
           |  |  | 190 |      * This method is also called in the bulk activity completion form.
 | 
        
           |  |  | 191 |      *
 | 
        
           |  |  | 192 |      * Only available on moodleform_mod.
 | 
        
           |  |  | 193 |      *
 | 
        
           |  |  | 194 |      * @param stdClass $data the form data to be modified.
 | 
        
           |  |  | 195 |      */
 | 
        
           |  |  | 196 |     public function data_postprocessing($data) {
 | 
        
           |  |  | 197 |         parent::data_postprocessing($data);
 | 
        
           |  |  | 198 |         if (!empty($data->completionunlocked)) {
 | 
        
           |  |  | 199 |             $suffix = $this->get_suffix();
 | 
        
           |  |  | 200 |             $completionel = 'completion' . $suffix;
 | 
        
           |  |  | 201 |             $completionentriesenabledel = 'completionentriesenabled' . $suffix;
 | 
        
           |  |  | 202 |             $autocompletion = !empty($data->{$completionel}) && $data->{$completionel} == COMPLETION_TRACKING_AUTOMATIC;
 | 
        
           |  |  | 203 |             if (empty($data->{$completionentriesenabledel}) || !$autocompletion) {
 | 
        
           |  |  | 204 |                 $completionentriesel = 'completionentries' . $suffix;
 | 
        
           |  |  | 205 |                 $data->{$completionentriesel} = 0;
 | 
        
           |  |  | 206 |             }
 | 
        
           |  |  | 207 |         }
 | 
        
           |  |  | 208 |     }
 | 
        
           |  |  | 209 |   | 
        
           |  |  | 210 | }
 |