Merge branch 'wip-mdl-27072' of git://github.com/rajeshtaneja/moodle
[moodle.git] / enrol / category / lib.php
blobef1699d42e1a8727bf56ebce572c3fc896132992
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.
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/>.
17 /**
18 * Category enrolment plugin.
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
25 defined('MOODLE_INTERNAL') || die();
28 /**
29 * category enrolment plugin implementation.
30 * @author Petr Skoda
31 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 class enrol_category_plugin extends enrol_plugin {
35 /**
36 * Is it possible to delete enrol instance via standard UI?
38 * @param stdClass $instance
39 * @return bool
41 public function can_delete_instance($instance) {
42 global $DB;
44 $context = context_course::instance($instance->courseid);
45 if (!has_capability('enrol/category:config', $context)) {
46 return false;
49 if (!enrol_is_enabled('category')) {
50 return true;
52 // Allow delete only when no synced users here.
53 return !$DB->record_exists('user_enrolments', array('enrolid'=>$instance->id));
56 /**
57 * Is it possible to hide/show enrol instance via standard UI?
59 * @param stdClass $instance
60 * @return bool
62 public function can_hide_show_instance($instance) {
63 $context = context_course::instance($instance->courseid);
64 return has_capability('enrol/category:config', $context);
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
72 public function get_newinstance_link($courseid) {
73 // Instances are added automatically as necessary.
74 return null;
77 /**
78 * Called for all enabled enrol plugins that returned true from is_cron_required().
79 * @return void
81 public function cron() {
82 global $CFG;
84 if (!enrol_is_enabled('category')) {
85 return;
88 require_once("$CFG->dirroot/enrol/category/locallib.php");
89 $trace = new null_progress_trace();
90 enrol_category_sync_full($trace);
93 /**
94 * Called after updating/inserting course.
96 * @param bool $inserted true if course just inserted
97 * @param stdClass $course
98 * @param stdClass $data form data
99 * @return void
101 public function course_updated($inserted, $course, $data) {
102 global $CFG;
104 if (!enrol_is_enabled('category')) {
105 return;
108 // Sync category enrols.
109 require_once("$CFG->dirroot/enrol/category/locallib.php");
110 enrol_category_sync_course($course);
114 * Automatic enrol sync executed during restore.
115 * Useful for automatic sync by course->idnumber or course category.
116 * @param stdClass $course course record
118 public function restore_sync_course($course) {
119 global $CFG;
120 require_once("$CFG->dirroot/enrol/category/locallib.php");
121 enrol_category_sync_course($course);