| 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_choice_mod_form extends moodleform_mod {
  | 
        
        
            | 
            | 
           9 | 
              | 
        
        
            | 
            | 
           10 | 
               function definition() {
  | 
        
        
            | 
            | 
           11 | 
                   global $CFG, $CHOICE_SHOWRESULTS, $CHOICE_PUBLISH, $CHOICE_DISPLAY, $DB;
  | 
        
        
            | 
            | 
           12 | 
              | 
        
        
            | 
            | 
           13 | 
                   $mform    =& $this->_form;
  | 
        
        
            | 
            | 
           14 | 
              | 
        
        
            | 
            | 
           15 | 
           //-------------------------------------------------------------------------------
  | 
        
        
            | 
            | 
           16 | 
                   $mform->addElement('header', 'general', get_string('general', 'form'));
  | 
        
        
            | 
            | 
           17 | 
              | 
        
        
            | 
            | 
           18 | 
                   $mform->addElement('text', 'name', get_string('choicename', 'choice'), 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('description', 'choice'));
  | 
        
        
            | 
            | 
           28 | 
              | 
        
        
            | 
            | 
           29 | 
                   $mform->addElement('select', 'display', get_string("displaymode","choice"), $CHOICE_DISPLAY);
  | 
        
        
            | 
            | 
           30 | 
              | 
        
        
            | 
            | 
           31 | 
                   //-------------------------------------------------------------------------------
  | 
        
        
            | 
            | 
           32 | 
                   $mform->addElement('header', 'optionhdr', get_string('options', 'choice'));
  | 
        
        
            | 
            | 
           33 | 
              | 
        
        
            | 
            | 
           34 | 
                   $mform->addElement('selectyesno', 'allowupdate', get_string("allowupdate", "choice"));
  | 
        
        
            | 
            | 
           35 | 
              | 
        
        
            | 
            | 
           36 | 
                   $mform->addElement('selectyesno', 'allowmultiple', get_string('allowmultiple', 'choice'));
  | 
        
        
            | 
            | 
           37 | 
                   if ($this->_instance) {
  | 
        
        
            | 
            | 
           38 | 
                       if ($DB->count_records('choice_answers', array('choiceid' => $this->_instance)) > 0) {
  | 
        
        
            | 
            | 
           39 | 
                           // Prevent user from toggeling the number of allowed answers once there are submissions.
  | 
        
        
            | 
            | 
           40 | 
                           $mform->freeze('allowmultiple');
  | 
        
        
            | 
            | 
           41 | 
                       }
  | 
        
        
            | 
            | 
           42 | 
                   }
  | 
        
        
            | 
            | 
           43 | 
              | 
        
        
            | 
            | 
           44 | 
                   $mform->addElement('selectyesno', 'limitanswers', get_string('limitanswers', 'choice'));
  | 
        
        
            | 
            | 
           45 | 
                   $mform->addHelpButton('limitanswers', 'limitanswers', 'choice');
  | 
        
        
            | 
            | 
           46 | 
              | 
        
        
            | 
            | 
           47 | 
                   $mform->addElement('selectyesno', 'showavailable', get_string('showavailable', 'choice'));
  | 
        
        
            | 
            | 
           48 | 
                   $mform->addHelpButton('showavailable', 'showavailable', 'choice');
  | 
        
        
            | 
            | 
           49 | 
                   $mform->hideIf('showavailable', 'limitanswers', 'eq', 0);
  | 
        
        
            | 
            | 
           50 | 
              | 
        
        
            | 
            | 
           51 | 
                   $repeatarray = array();
  | 
        
        
            | 
            | 
           52 | 
                   $repeatarray[] = $mform->createElement('text', 'option', get_string('optionno', 'choice'));
  | 
        
        
            | 
            | 
           53 | 
                   $repeatarray[] = $mform->createElement('text', 'limit', get_string('limitno', 'choice'));
  | 
        
        
            | 
            | 
           54 | 
                   $repeatarray[] = $mform->createElement('hidden', 'optionid', 0);
  | 
        
        
            | 
            | 
           55 | 
              | 
        
        
            | 
            | 
           56 | 
                   if ($this->_instance){
  | 
        
        
            | 
            | 
           57 | 
                       $repeatno = $DB->count_records('choice_options', array('choiceid'=>$this->_instance));
  | 
        
        
            | 
            | 
           58 | 
                       $repeatno += 2;
  | 
        
        
            | 
            | 
           59 | 
                   } else {
  | 
        
        
            | 
            | 
           60 | 
                       $repeatno = 5;
  | 
        
        
            | 
            | 
           61 | 
                   }
  | 
        
        
            | 
            | 
           62 | 
              | 
        
        
            | 
            | 
           63 | 
                   $repeateloptions = array();
  | 
        
        
            | 
            | 
           64 | 
                   $repeateloptions['limit']['default'] = 0;
  | 
        
        
            | 
            | 
           65 | 
                   $repeateloptions['limit']['hideif'] = array('limitanswers', 'eq', 0);
  | 
        
        
            | 
            | 
           66 | 
                   $repeateloptions['limit']['rule'] = 'numeric';
  | 
        
        
            | 
            | 
           67 | 
                   $repeateloptions['limit']['type'] = PARAM_INT;
  | 
        
        
            | 
            | 
           68 | 
              | 
        
        
            | 
            | 
           69 | 
                   $repeateloptions['option']['helpbutton'] = array('choiceoptions', 'choice');
  | 
        
        
            | 
            | 
           70 | 
                   $mform->setType('option', PARAM_CLEANHTML);
  | 
        
        
            | 
            | 
           71 | 
              | 
        
        
            | 
            | 
           72 | 
                   $mform->setType('optionid', PARAM_INT);
  | 
        
        
            | 
            | 
           73 | 
              | 
        
        
            | 
            | 
           74 | 
                   $this->repeat_elements($repeatarray, $repeatno,
  | 
        
        
            | 
            | 
           75 | 
                               $repeateloptions, 'option_repeats', 'option_add_fields', 3, null, true);
  | 
        
        
            | 
            | 
           76 | 
              | 
        
        
            | 
            | 
           77 | 
                   // Make the first option required
  | 
        
        
            | 
            | 
           78 | 
                   if ($mform->elementExists('option[0]')) {
  | 
        
        
            | 
            | 
           79 | 
                       $mform->addRule('option[0]', get_string('atleastoneoption', 'choice'), 'required', null, 'client');
  | 
        
        
            | 
            | 
           80 | 
                   }
  | 
        
        
            | 
            | 
           81 | 
              | 
        
        
            | 
            | 
           82 | 
           //-------------------------------------------------------------------------------
  | 
        
        
            | 
            | 
           83 | 
                   $mform->addElement('header', 'availabilityhdr', get_string('availability'));
  | 
        
        
            | 
            | 
           84 | 
                   $mform->addElement('date_time_selector', 'timeopen', get_string("choiceopen", "choice"),
  | 
        
        
            | 
            | 
           85 | 
                       array('optional' => true));
  | 
        
        
            | 
            | 
           86 | 
              | 
        
        
            | 
            | 
           87 | 
                   $mform->addElement('date_time_selector', 'timeclose', get_string("choiceclose", "choice"),
  | 
        
        
            | 
            | 
           88 | 
                       array('optional' => true));
  | 
        
        
            | 
            | 
           89 | 
              | 
        
        
            | 
            | 
           90 | 
                   $mform->addElement('advcheckbox', 'showpreview', get_string('showpreview', 'choice'));
  | 
        
        
            | 
            | 
           91 | 
                   $mform->addHelpButton('showpreview', 'showpreview', 'choice');
  | 
        
        
            | 
            | 
           92 | 
                   $mform->disabledIf('showpreview', 'timeopen[enabled]');
  | 
        
        
            | 
            | 
           93 | 
              | 
        
        
            | 
            | 
           94 | 
           //-------------------------------------------------------------------------------
  | 
        
        
            | 
            | 
           95 | 
                   $mform->addElement('header', 'resultshdr', get_string('results', 'choice'));
  | 
        
        
            | 
            | 
           96 | 
              | 
        
        
            | 
            | 
           97 | 
                   $mform->addElement('select', 'showresults', get_string("publish", "choice"), $CHOICE_SHOWRESULTS);
  | 
        
        
            | 
            | 
           98 | 
              | 
        
        
            | 
            | 
           99 | 
                   $mform->addElement('select', 'publish', get_string("privacy", "choice"), $CHOICE_PUBLISH);
  | 
        
        
            | 
            | 
           100 | 
                   $mform->hideIf('publish', 'showresults', 'eq', 0);
  | 
        
        
            | 
            | 
           101 | 
              | 
        
        
            | 
            | 
           102 | 
                   $mform->addElement('selectyesno', 'showunanswered', get_string("showunanswered", "choice"));
  | 
        
        
            | 
            | 
           103 | 
              | 
        
        
            | 
            | 
           104 | 
                   $mform->addElement('selectyesno', 'includeinactive', get_string('includeinactive', 'choice'));
  | 
        
        
            | 
            | 
           105 | 
                   $mform->setDefault('includeinactive', 0);
  | 
        
        
            | 
            | 
           106 | 
              | 
        
        
            | 
            | 
           107 | 
           //-------------------------------------------------------------------------------
  | 
        
        
            | 
            | 
           108 | 
                   $this->standard_coursemodule_elements();
  | 
        
        
            | 
            | 
           109 | 
           //-------------------------------------------------------------------------------
  | 
        
        
            | 
            | 
           110 | 
                   $this->add_action_buttons();
  | 
        
        
            | 
            | 
           111 | 
               }
  | 
        
        
            | 
            | 
           112 | 
              | 
        
        
            | 
            | 
           113 | 
               function data_preprocessing(&$default_values){
  | 
        
        
            | 
            | 
           114 | 
                   global $DB;
  | 
        
        
            | 
            | 
           115 | 
                   if (!empty($this->_instance) && ($options = $DB->get_records_menu('choice_options',array('choiceid'=>$this->_instance), 'id', 'id,text'))
  | 
        
        
            | 
            | 
           116 | 
                          && ($options2 = $DB->get_records_menu('choice_options', array('choiceid'=>$this->_instance), 'id', 'id,maxanswers')) ) {
  | 
        
        
            | 
            | 
           117 | 
                       $choiceids=array_keys($options);
  | 
        
        
            | 
            | 
           118 | 
                       $options=array_values($options);
  | 
        
        
            | 
            | 
           119 | 
                       $options2=array_values($options2);
  | 
        
        
            | 
            | 
           120 | 
              | 
        
        
            | 
            | 
           121 | 
                       foreach (array_keys($options) as $key){
  | 
        
        
            | 
            | 
           122 | 
                           $default_values['option['.$key.']'] = $options[$key];
  | 
        
        
            | 
            | 
           123 | 
                           $default_values['limit['.$key.']'] = $options2[$key];
  | 
        
        
            | 
            | 
           124 | 
                           $default_values['optionid['.$key.']'] = $choiceids[$key];
  | 
        
        
            | 
            | 
           125 | 
                       }
  | 
        
        
            | 
            | 
           126 | 
              | 
        
        
            | 
            | 
           127 | 
                   }
  | 
        
        
            | 
            | 
           128 | 
              | 
        
        
            | 
            | 
           129 | 
               }
  | 
        
        
            | 
            | 
           130 | 
              | 
        
        
            | 
            | 
           131 | 
               /**
  | 
        
        
            | 
            | 
           132 | 
                * Allows module to modify the data returned by form get_data().
  | 
        
        
            | 
            | 
           133 | 
                * This method is also called in the bulk activity completion form.
  | 
        
        
            | 
            | 
           134 | 
                *
  | 
        
        
            | 
            | 
           135 | 
                * Only available on moodleform_mod.
  | 
        
        
            | 
            | 
           136 | 
                *
  | 
        
        
            | 
            | 
           137 | 
                * @param stdClass $data the form data to be modified.
  | 
        
        
            | 
            | 
           138 | 
                */
  | 
        
        
            | 
            | 
           139 | 
               public function data_postprocessing($data) {
  | 
        
        
            | 
            | 
           140 | 
                   parent::data_postprocessing($data);
  | 
        
        
            | 
            | 
           141 | 
                   // Set up completion section even if checkbox is not ticked.
  | 
        
        
            | 
            | 
           142 | 
                   if (!empty($data->completionunlocked)) {
  | 
        
        
            | 
            | 
           143 | 
                       $suffix = $this->get_suffix();
  | 
        
        
            | 
            | 
           144 | 
                       if (empty($data->{'completionsubmit' . $suffix})) {
  | 
        
        
            | 
            | 
           145 | 
                           $data->{'completionsubmit' . $suffix} = 0;
  | 
        
        
            | 
            | 
           146 | 
                       }
  | 
        
        
            | 
            | 
           147 | 
                   }
  | 
        
        
            | 
            | 
           148 | 
               }
  | 
        
        
            | 
            | 
           149 | 
              | 
        
        
            | 
            | 
           150 | 
               /**
  | 
        
        
            | 
            | 
           151 | 
                * Enforce validation rules here
  | 
        
        
            | 
            | 
           152 | 
                *
  | 
        
        
            | 
            | 
           153 | 
                * @param array $data array of ("fieldname"=>value) of submitted data
  | 
        
        
            | 
            | 
           154 | 
                * @param array $files array of uploaded files "element_name"=>tmp_file_path
  | 
        
        
            | 
            | 
           155 | 
                * @return array
  | 
        
        
            | 
            | 
           156 | 
                **/
  | 
        
        
            | 
            | 
           157 | 
               public function validation($data, $files) {
  | 
        
        
            | 
            | 
           158 | 
                   $errors = parent::validation($data, $files);
  | 
        
        
            | 
            | 
           159 | 
              | 
        
        
            | 
            | 
           160 | 
                   // Check open and close times are consistent.
  | 
        
        
            | 
            | 
           161 | 
                   if ($data['timeopen'] && $data['timeclose'] &&
  | 
        
        
            | 
            | 
           162 | 
                           $data['timeclose'] < $data['timeopen']) {
  | 
        
        
            | 
            | 
           163 | 
                       $errors['timeclose'] = get_string('closebeforeopen', 'choice');
  | 
        
        
            | 
            | 
           164 | 
                   }
  | 
        
        
            | 
            | 
           165 | 
              | 
        
        
            | 
            | 
           166 | 
                   return $errors;
  | 
        
        
            | 
            | 
           167 | 
               }
  | 
        
        
            | 
            | 
           168 | 
              | 
        
        
            | 
            | 
           169 | 
               public function add_completion_rules() {
  | 
        
        
            | 
            | 
           170 | 
                   $mform =& $this->_form;
  | 
        
        
            | 
            | 
           171 | 
              | 
        
        
            | 
            | 
           172 | 
                   $suffix = $this->get_suffix();
  | 
        
        
            | 
            | 
           173 | 
                   $completionsubmitel = 'completionsubmit' . $suffix;
  | 
        
        
            | 
            | 
           174 | 
                   $mform->addElement('checkbox', $completionsubmitel, '', get_string('completionsubmit', 'choice'));
  | 
        
        
            | 
            | 
           175 | 
                   // Enable this completion rule by default.
  | 
        
        
            | 
            | 
           176 | 
                   $mform->setDefault($completionsubmitel, 1);
  | 
        
        
            | 
            | 
           177 | 
                   return [$completionsubmitel];
  | 
        
        
            | 
            | 
           178 | 
               }
  | 
        
        
            | 
            | 
           179 | 
              | 
        
        
            | 
            | 
           180 | 
               public function completion_rule_enabled($data) {
  | 
        
        
            | 
            | 
           181 | 
                   $suffix = $this->get_suffix();
  | 
        
        
            | 
            | 
           182 | 
                   return !empty($data['completionsubmit' . $suffix]);
  | 
        
        
            | 
            | 
           183 | 
               }
  | 
        
        
            | 
            | 
           184 | 
           }
  |