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.
   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   * Category enrolment plugin.
  19   *
  20   * @package    enrol_category
  21   * @copyright  2010 Petr Skoda {@link http://skodak.org}
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  
  28  /**
  29   * category enrolment plugin implementation.
  30   * @author  Petr Skoda
  31   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class enrol_category_plugin extends enrol_plugin {
  34  
  35     /**
  36       * Is it possible to delete enrol instance via standard UI?
  37       *
  38       * @param stdClass $instance
  39       * @return bool
  40       */
  41      public function can_delete_instance($instance) {
  42          global $DB;
  43  
  44          $context = context_course::instance($instance->courseid);
  45          if (!has_capability('enrol/category:config', $context)) {
  46              return false;
  47          }
  48  
  49          if (!enrol_is_enabled('category')) {
  50              return true;
  51          }
  52          // Allow delete only when no synced users here.
  53          return !$DB->record_exists('user_enrolments', array('enrolid'=>$instance->id));
  54      }
  55  
  56      /**
  57       * Is it possible to hide/show enrol instance via standard UI?
  58       *
  59       * @param stdClass $instance
  60       * @return bool
  61       */
  62      public function can_hide_show_instance($instance) {
  63          $context = context_course::instance($instance->courseid);
  64          return has_capability('enrol/category:config', $context);
  65      }
  66  
  67      /**
  68       * Returns link to page which may be used to add new instance of enrolment plugin in course.
  69       * @param int $courseid
  70       * @return moodle_url page url
  71       */
  72      public function get_newinstance_link($courseid) {
  73          // Instances are added automatically as necessary.
  74          return null;
  75      }
  76  
  77      /**
  78       * Called after updating/inserting course.
  79       *
  80       * @param bool $inserted true if course just inserted
  81       * @param stdClass $course
  82       * @param stdClass $data form data
  83       * @return void
  84       */
  85      public function course_updated($inserted, $course, $data) {
  86          global $CFG;
  87  
  88          if (!enrol_is_enabled('category')) {
  89              return;
  90          }
  91  
  92          // Sync category enrols.
  93          require_once("$CFG->dirroot/enrol/category/locallib.php");
  94          enrol_category_sync_course($course);
  95      }
  96  
  97      /**
  98       * Automatic enrol sync executed during restore.
  99       * Useful for automatic sync by course->idnumber or course category.
 100       * @param stdClass $course course record
 101       */
 102      public function restore_sync_course($course) {
 103          global $CFG;
 104          require_once("$CFG->dirroot/enrol/category/locallib.php");
 105          enrol_category_sync_course($course);
 106      }
 107  }