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]

   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   * Adhoc task handling course module deletion.
  19   *
  20   * @package    core_course
  21   * @copyright  2016 Jake Dallimore <jrhdallimore@gmail.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_course\task;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  /**
  29   * Class handling course module deletion.
  30   *
  31   * This task supports an array of course module object as custom_data, and calls course_delete_module() in synchronous deletion
  32   * mode for each of them.
  33   * This will:
  34   * 1. call any 'mod_xxx_pre_course_module_deleted' functions (e.g. Recycle bin)
  35   * 2. delete the module
  36   * 3. fire the deletion event
  37   *
  38   * @package core_course
  39   * @copyright 2016 Jake Dallimore <jrhdallimore@gmail.com>
  40   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class course_delete_modules extends \core\task\adhoc_task {
  43  
  44      /**
  45       * Run the deletion task.
  46       *
  47       * @throws \coding_exception if the module could not be removed.
  48       */
  49      public function execute() {
  50          global $CFG;
  51          require_once($CFG->dirroot. '/course/lib.php');
  52  
  53          // Set the proper user.
  54          if ($this->get_custom_data()->userid !== $this->get_custom_data()->realuserid) {
  55              $realuser = \core_user::get_user($this->get_custom_data()->realuserid, '*', MUST_EXIST);
  56              \core\cron::setup_user($realuser);
  57              \core\session\manager::loginas($this->get_custom_data()->userid, \context_system::instance(), false);
  58          } else {
  59              $user = \core_user::get_user($this->get_custom_data()->userid, '*', MUST_EXIST);
  60              \core\cron::setup_user($user);
  61          }
  62  
  63          $cms = $this->get_custom_data()->cms;
  64          foreach ($cms as $cm) {
  65              try {
  66                  course_delete_module($cm->id);
  67              } catch (\Exception $e) {
  68                  throw new \coding_exception("The course module {$cm->id} could not be deleted. "
  69                      . "{$e->getMessage()}: {$e->getFile()}({$e->getLine()}) {$e->getTraceAsString()}");
  70              }
  71          }
  72      }
  73  }