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  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Default activity completion form
  19   *
  20   * @package     core_completion
  21   * @copyright   2017 Marina Glancy
  22   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  class core_completion_defaultedit_form extends core_completion_edit_base_form {
  25      /** @var array */
  26      protected $modules;
  27      /** @var array */
  28      protected $_modnames;
  29  
  30      public function __construct(
  31          $action = null,
  32          $customdata = null,
  33          $method = 'post',
  34          $target = '',
  35          $attributes = null,
  36          $editable = true,
  37          $ajaxformdata = null
  38      ) {
  39          $this->modules = $customdata['modules'];
  40          if ($modname = $this->get_module_name()) {
  41              // Set the form suffix to the module name so that the form identifier is unique for each module type.
  42              $this->set_suffix('_' . $modname);
  43          }
  44  
  45          parent::__construct($action, $customdata, $method, $target, $attributes, $editable, $ajaxformdata);
  46      }
  47  
  48  
  49      /**
  50       * Returns list of types of selected modules
  51       *
  52       * @return array modname=>modfullname
  53       */
  54      protected function get_module_names() {
  55          if ($this->_modnames !== null) {
  56              return $this->_modnames;
  57          }
  58          $this->_modnames = [];
  59          foreach ($this->modules as $module) {
  60              $this->_modnames[$module->name] = $module->formattedname;
  61          }
  62          return $this->_modnames;
  63      }
  64  
  65      /**
  66       * Returns an instance of component-specific module form for the first selected module
  67       *
  68       * @return moodleform_mod|null
  69       */
  70      protected function get_module_form() {
  71          global $CFG, $PAGE;
  72  
  73          if ($this->_moduleform) {
  74              return $this->_moduleform;
  75          }
  76  
  77          $modnames = array_keys($this->get_module_names());
  78          $modname = $modnames[0];
  79          $course = $this->course;
  80  
  81          $modmoodleform = "$CFG->dirroot/mod/$modname/mod_form.php";
  82          if (file_exists($modmoodleform)) {
  83              require_once($modmoodleform);
  84          } else {
  85              throw new \moodle_exception('noformdesc');
  86          }
  87  
  88          list($module, $context, $cw, $cmrec, $data) = prepare_new_moduleinfo_data($course, $modname, 0, $this->get_suffix());
  89          $data->return = 0;
  90          $data->sr = 0;
  91          $data->add = $modname;
  92  
  93          // Initialise the form but discard all JS requirements it adds, our form has already added them.
  94          $mformclassname = 'mod_'.$modname.'_mod_form';
  95          $PAGE->start_collecting_javascript_requirements();
  96          $this->_moduleform = new $mformclassname($data, 0, $cmrec, $course);
  97          $this->_moduleform->set_suffix('_' . $modname);
  98          $PAGE->end_collecting_javascript_requirements();
  99  
 100          return $this->_moduleform;
 101      }
 102  
 103      /**
 104       * Form definition,
 105       */
 106      public function definition() {
 107          $course = $this->_customdata['course'];
 108          $this->course = is_numeric($course) ? get_course($course) : $course;
 109          $this->modules = $this->_customdata['modules'];
 110  
 111          $mform = $this->_form;
 112  
 113          foreach ($this->modules as $modid => $module) {
 114              $mform->addElement('hidden', 'modids['.$modid.']', $modid);
 115              $mform->setType('modids['.$modid.']', PARAM_INT);
 116          }
 117  
 118          parent::definition();
 119  
 120          $modform = $this->get_module_form();
 121          if ($modform) {
 122              $modnames = array_keys($this->get_module_names());
 123              $modname = $modnames[0];
 124              // Pre-fill the form with the current completion rules of the first selected module type.
 125              list($module, $context, $cw, $cmrec, $data) = prepare_new_moduleinfo_data(
 126                  $this->course,
 127                  $modname,
 128                  0,
 129                  $this->get_suffix()
 130              );
 131              $data = (array)$data;
 132              try {
 133                  $modform->data_preprocessing($data);
 134              } catch (moodle_exception $e) {
 135                  debugging(
 136                      'data_preprocessing function of module ' . $modnames[0] .
 137                      ' should be fixed so it can be shown together with other Default activity completion forms',
 138                      DEBUG_DEVELOPER
 139                  );
 140              }
 141              // Unset fields that will conflict with this form and set data to this form.
 142              unset($data['cmid']);
 143              unset($data['modids']);
 144              unset($data['id']);
 145              $this->set_data($data);
 146          }
 147      }
 148  
 149      /**
 150       * This method has been overridden because the form identifier must be unique for each module type.
 151       * Otherwise, the form will display the same data for each module type once it's submitted.
 152       */
 153      protected function get_form_identifier() {
 154          return parent::get_form_identifier() . $this->get_suffix();
 155      }
 156  }