Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401] [Versions 401 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  
  25  defined('MOODLE_INTERNAL') || die;
  26  
  27  /**
  28   * Default activity completion form
  29   *
  30   * @package     core_completion
  31   * @copyright   2017 Marina Glancy
  32   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class core_completion_defaultedit_form extends core_completion_edit_base_form {
  35      /** @var array */
  36      protected $modules;
  37      /** @var array */
  38      protected $_modnames;
  39  
  40      /**
  41       * Returns list of types of selected modules
  42       *
  43       * @return array modname=>modfullname
  44       */
  45      protected function get_module_names() {
  46          if ($this->_modnames !== null) {
  47              return $this->_modnames;
  48          }
  49          $this->_modnames = [];
  50          foreach ($this->modules as $module) {
  51              $this->_modnames[$module->name] = $module->formattedname;
  52          }
  53          return $this->_modnames;
  54      }
  55  
  56      /**
  57       * Returns an instance of component-specific module form for the first selected module
  58       *
  59       * @return moodleform_mod|null
  60       */
  61      protected function get_module_form() {
  62          global $CFG, $PAGE;
  63  
  64          if ($this->_moduleform) {
  65              return $this->_moduleform;
  66          }
  67  
  68          $modnames = array_keys($this->get_module_names());
  69          $modname = $modnames[0];
  70          $course = $this->course;
  71  
  72          $modmoodleform = "$CFG->dirroot/mod/$modname/mod_form.php";
  73          if (file_exists($modmoodleform)) {
  74              require_once($modmoodleform);
  75          } else {
  76              throw new \moodle_exception('noformdesc');
  77          }
  78  
  79          list($module, $context, $cw, $cmrec, $data) = prepare_new_moduleinfo_data($course, $modname, 0);
  80          $data->return = 0;
  81          $data->sr = 0;
  82          $data->add = $modname;
  83  
  84          // Initialise the form but discard all JS requirements it adds, our form has already added them.
  85          $mformclassname = 'mod_'.$modname.'_mod_form';
  86          $PAGE->start_collecting_javascript_requirements();
  87          $this->_moduleform = new $mformclassname($data, 0, $cmrec, $course);
  88          $PAGE->end_collecting_javascript_requirements();
  89  
  90          return $this->_moduleform;
  91      }
  92  
  93      /**
  94       * Form definition,
  95       */
  96      public function definition() {
  97          $this->course = $this->_customdata['course'];
  98          $this->modules = $this->_customdata['modules'];
  99  
 100          $mform = $this->_form;
 101  
 102          foreach ($this->modules as $modid => $module) {
 103              $mform->addElement('hidden', 'modids['.$modid.']', $modid);
 104              $mform->setType('modids['.$modid.']', PARAM_INT);
 105          }
 106  
 107          parent::definition();
 108  
 109          $modform = $this->get_module_form();
 110          if ($modform) {
 111              $modnames = array_keys($this->get_module_names());
 112              $modname = $modnames[0];
 113              // Pre-fill the form with the current completion rules of the first selected module type.
 114              list($module, $context, $cw, $cmrec, $data) = prepare_new_moduleinfo_data($this->course, $modname, 0);
 115              $data = (array)$data;
 116              $modform->data_preprocessing($data);
 117              // Unset fields that will conflict with this form and set data to this form.
 118              unset($data['cmid']);
 119              unset($data['modids']);
 120              unset($data['id']);
 121              $this->set_data($data);
 122          }
 123      }
 124  }