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   * Bulk edit 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   * Bulk edit 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_bulkedit_form extends core_completion_edit_base_form {
  35      /** @var cm_info[] list of selected course modules */
  36      protected $cms = [];
  37      /** @var array Do not use directly, call $this->get_module_names() */
  38      protected $_modnames = null;
  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->cms as $cm) {
  51              $this->_modnames[$cm->modname] = $cm->modfullname;
  52          }
  53          return $this->_modnames;
  54      }
  55  
  56      /**
  57       * It will return the course module when $cms has only one course module; otherwise, null will be returned.
  58       *
  59       * @return cm_info|null
  60       */
  61      protected function get_cm(): ?cm_info {
  62          if (count($this->cms) === 1) {
  63              return reset($this->cms);
  64          }
  65  
  66          // If there are multiple modules, so none will be selected.
  67          return null;
  68      }
  69  
  70      /**
  71       * Returns an instance of component-specific module form for the first selected module
  72       *
  73       * @return moodleform_mod|null
  74       */
  75      protected function get_module_form() {
  76          global $CFG, $PAGE;
  77  
  78          if ($this->_moduleform) {
  79              return $this->_moduleform;
  80          }
  81  
  82          $cm = reset($this->cms);
  83          $course = $this->course;
  84          $modname = $cm->modname;
  85  
  86          $modmoodleform = "$CFG->dirroot/mod/$modname/mod_form.php";
  87          if (file_exists($modmoodleform)) {
  88              require_once($modmoodleform);
  89          } else {
  90              throw new \moodle_exception('noformdesc');
  91          }
  92  
  93          list($cmrec, $context, $module, $data, $cw) = get_moduleinfo_data($cm, $course);
  94          $data->return = 0;
  95          $data->sr = 0;
  96          $data->update = $modname;
  97  
  98          // Initialise the form but discard all JS requirements it adds, our form has already added them.
  99          $mformclassname = 'mod_'.$modname.'_mod_form';
 100          $PAGE->start_collecting_javascript_requirements();
 101          $this->_moduleform = new $mformclassname($data, 0, $cmrec, $course);
 102          $PAGE->end_collecting_javascript_requirements();
 103  
 104          return $this->_moduleform;
 105      }
 106  
 107      /**
 108       * Form definition
 109       */
 110      public function definition() {
 111          $this->cms = $this->_customdata['cms'];
 112          $cm = reset($this->cms); // First selected course module.
 113          $this->course = $cm->get_course();
 114  
 115          $mform = $this->_form;
 116  
 117          $idx = 0;
 118          foreach ($this->cms as $cm) {
 119              $mform->addElement('hidden', 'cmid['.$idx.']', $cm->id);
 120              $mform->setType('cmid['.$idx.']', PARAM_INT);
 121              $idx++;
 122          }
 123  
 124          parent::definition();
 125  
 126          $modform = $this->get_module_form();
 127          if ($modform) {
 128              // Pre-fill the form with the current completion rules of the first selected module.
 129              list($cmrec, $context, $module, $data, $cw) = get_moduleinfo_data($cm->get_course_module_record(), $this->course);
 130              $data = (array)$data;
 131              $modform->data_preprocessing($data);
 132              // Unset fields that will conflict with this form and set data to this form.
 133              unset($data['cmid']);
 134              unset($data['id']);
 135              $this->set_data($data);
 136          }
 137      }
 138  
 139      /**
 140       * Form validation
 141       *
 142       * @param array $data array of ("fieldname"=>value) of submitted data
 143       * @param array $files array of uploaded files "element_name"=>tmp_file_path
 144       * @return array of "element_name"=>"error_description" if there are errors,
 145       *         or an empty array if everything is OK (true allowed for backwards compatibility too).
 146       */
 147      public function validation($data, $files) {
 148          global $CFG;
 149          $errors = parent::validation($data, $files);
 150  
 151          // Completion: Don't let them choose automatic completion without turning
 152          // on some conditions.
 153          if (array_key_exists('completion', $data) &&
 154                  $data['completion'] == COMPLETION_TRACKING_AUTOMATIC &&
 155                  (!empty($data['completionusegrade']) || !empty($data['completionpassgrade']))) {
 156              require_once($CFG->libdir.'/gradelib.php');
 157              $moduleswithoutgradeitem = [];
 158              foreach ($this->cms as $cm) {
 159                  $item = grade_item::fetch(array('courseid' => $cm->course, 'itemtype' => 'mod',
 160                      'itemmodule' => $cm->modname, 'iteminstance' => $cm->instance,
 161                      'itemnumber' => 0));
 162                  if (!$item) {
 163                      $moduleswithoutgradeitem[] = $cm->get_formatted_name();
 164                  }
 165              }
 166              if ($moduleswithoutgradeitem) {
 167                  $errors['completionusegrade'] = get_string('nogradeitem', 'completion', join(', ', $moduleswithoutgradeitem));
 168              }
 169          }
 170  
 171          return $errors;
 172      }
 173  }