Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]

   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_survey_mod_form extends moodleform_mod {
   9  
  10      function definition() {
  11          global $CFG, $DB;
  12  
  13          $mform =& $this->_form;
  14  
  15          $strrequired = get_string('required');
  16  
  17  //-------------------------------------------------------------------------------
  18          $mform->addElement('header', 'general', get_string('general', 'form'));
  19  
  20          $mform->addElement('text', 'name', get_string('name'), array('size'=>'64'));
  21          if (!empty($CFG->formatstringstriptags)) {
  22              $mform->setType('name', PARAM_TEXT);
  23          } else {
  24              $mform->setType('name', PARAM_CLEANHTML);
  25          }
  26          $mform->addRule('name', null, 'required', null, 'client');
  27  
  28          if (!$options = $DB->get_records_menu("survey", array("template"=>0), "name", "id, name")) {
  29              throw new \moodle_exception('cannotfindsurveytmpt', 'survey');
  30          }
  31  
  32          foreach ($options as $id => $name) {
  33              $options[$id] = get_string($name, "survey");
  34          }
  35          $options = array(''=>get_string('choose').'...') + $options;
  36          $mform->addElement('select', 'template', get_string("surveytype", "survey"), $options);
  37          $mform->addRule('template', $strrequired, 'required', null, 'client');
  38          $mform->addHelpButton('template', 'surveytype', 'survey');
  39  
  40          $this->standard_intro_elements(get_string('customintro', 'survey'));
  41  
  42          $this->standard_coursemodule_elements();
  43  
  44  //-------------------------------------------------------------------------------
  45          // buttons
  46          $this->add_action_buttons();
  47      }
  48  
  49      /**
  50       * Allows module to modify the data returned by form get_data().
  51       * This method is also called in the bulk activity completion form.
  52       *
  53       * Only available on moodleform_mod.
  54       *
  55       * @param stdClass $data the form data to be modified.
  56       */
  57      public function data_postprocessing($data) {
  58          parent::data_postprocessing($data);
  59          if (!empty($data->completionunlocked)) {
  60              // Turn off completion settings if the checkboxes aren't ticked.
  61              $suffix = $this->get_suffix();
  62              $completion = $data->{'completion' . $suffix};
  63              $autocompletion = !empty($completion) && $completion == COMPLETION_TRACKING_AUTOMATIC;
  64              if (!$autocompletion || empty($data->{'completionsubmit' . $suffix})) {
  65                  $data->{'completionsubmit' . $suffix} = 0;
  66              }
  67          }
  68      }
  69  
  70      /**
  71       * Add completion rules to form.
  72       * @return array
  73       */
  74      public function add_completion_rules() {
  75          $mform =& $this->_form;
  76          $suffix = $this->get_suffix();
  77          $completionsubmitel = 'completionsubmit' . $suffix;
  78          $mform->addElement('checkbox', $completionsubmitel, '', get_string('completionsubmit', 'survey'));
  79          // Enable this completion rule by default.
  80          $mform->setDefault($completionsubmitel, 1);
  81          return [$completionsubmitel];
  82      }
  83  
  84      /**
  85       * Enable completion rules
  86       * @param array $data
  87       * @return bool
  88       */
  89      public function completion_rule_enabled($data) {
  90          $suffix = $this->get_suffix();
  91          return !empty($data['completionsubmit' . $suffix]);
  92      }
  93  }