[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/choice/ -> mod_form.php (source)

   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->add_intro_editor(true, 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  
  38          $mform->addElement('selectyesno', 'limitanswers', get_string('limitanswers', 'choice'));
  39          $mform->addHelpButton('limitanswers', 'limitanswers', 'choice');
  40  
  41          $repeatarray = array();
  42          $repeatarray[] = $mform->createElement('text', 'option', get_string('optionno', 'choice'));
  43          $repeatarray[] = $mform->createElement('text', 'limit', get_string('limitno', 'choice'));
  44          $repeatarray[] = $mform->createElement('hidden', 'optionid', 0);
  45  
  46          if ($this->_instance){
  47              $repeatno = $DB->count_records('choice_options', array('choiceid'=>$this->_instance));
  48              $repeatno += 2;
  49          } else {
  50              $repeatno = 5;
  51          }
  52  
  53          $repeateloptions = array();
  54          $repeateloptions['limit']['default'] = 0;
  55          $repeateloptions['limit']['disabledif'] = array('limitanswers', 'eq', 0);
  56          $repeateloptions['limit']['rule'] = 'numeric';
  57          $repeateloptions['limit']['type'] = PARAM_INT;
  58  
  59          $repeateloptions['option']['helpbutton'] = array('choiceoptions', 'choice');
  60          $mform->setType('option', PARAM_CLEANHTML);
  61  
  62          $mform->setType('optionid', PARAM_INT);
  63  
  64          $this->repeat_elements($repeatarray, $repeatno,
  65                      $repeateloptions, 'option_repeats', 'option_add_fields', 3, null, true);
  66  
  67          // Make the first option required
  68          if ($mform->elementExists('option[0]')) {
  69              $mform->addRule('option[0]', get_string('atleastoneoption', 'choice'), 'required', null, 'client');
  70          }
  71  
  72  //-------------------------------------------------------------------------------
  73          $mform->addElement('header', 'timerestricthdr', get_string('availability'));
  74          $mform->addElement('checkbox', 'timerestrict', get_string('timerestrict', 'choice'));
  75  
  76          $mform->addElement('date_time_selector', 'timeopen', get_string("choiceopen", "choice"));
  77          $mform->disabledIf('timeopen', 'timerestrict');
  78  
  79          $mform->addElement('date_time_selector', 'timeclose', get_string("choiceclose", "choice"));
  80          $mform->disabledIf('timeclose', 'timerestrict');
  81  
  82  //-------------------------------------------------------------------------------
  83          $mform->addElement('header', 'resultshdr', get_string('results', 'choice'));
  84  
  85          $mform->addElement('select', 'showresults', get_string("publish", "choice"), $CHOICE_SHOWRESULTS);
  86  
  87          $mform->addElement('select', 'publish', get_string("privacy", "choice"), $CHOICE_PUBLISH);
  88          $mform->disabledIf('publish', 'showresults', 'eq', 0);
  89  
  90          $mform->addElement('selectyesno', 'showunanswered', get_string("showunanswered", "choice"));
  91  
  92  
  93  //-------------------------------------------------------------------------------
  94          $this->standard_coursemodule_elements();
  95  //-------------------------------------------------------------------------------
  96          $this->add_action_buttons();
  97      }
  98  
  99      function data_preprocessing(&$default_values){
 100          global $DB;
 101          if (!empty($this->_instance) && ($options = $DB->get_records_menu('choice_options',array('choiceid'=>$this->_instance), 'id', 'id,text'))
 102                 && ($options2 = $DB->get_records_menu('choice_options', array('choiceid'=>$this->_instance), 'id', 'id,maxanswers')) ) {
 103              $choiceids=array_keys($options);
 104              $options=array_values($options);
 105              $options2=array_values($options2);
 106  
 107              foreach (array_keys($options) as $key){
 108                  $default_values['option['.$key.']'] = $options[$key];
 109                  $default_values['limit['.$key.']'] = $options2[$key];
 110                  $default_values['optionid['.$key.']'] = $choiceids[$key];
 111              }
 112  
 113          }
 114          if (empty($default_values['timeopen'])) {
 115              $default_values['timerestrict'] = 0;
 116          } else {
 117              $default_values['timerestrict'] = 1;
 118          }
 119  
 120      }
 121  
 122      function get_data() {
 123          $data = parent::get_data();
 124          if (!$data) {
 125              return false;
 126          }
 127          // Set up completion section even if checkbox is not ticked
 128          if (!empty($data->completionunlocked)) {
 129              if (empty($data->completionsubmit)) {
 130                  $data->completionsubmit = 0;
 131              }
 132          }
 133          return $data;
 134      }
 135  
 136      function add_completion_rules() {
 137          $mform =& $this->_form;
 138  
 139          $mform->addElement('checkbox', 'completionsubmit', '', get_string('completionsubmit', 'choice'));
 140          return array('completionsubmit');
 141      }
 142  
 143      function completion_rule_enabled($data) {
 144          return !empty($data['completionsubmit']);
 145      }
 146  }
 147  


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1