Merge branch 'wip-mdl-41590' of git://github.com/rajeshtaneja/moodle
[moodle.git] / enrol / meta / lib.php
CommitLineData
df997f84 1<?php
df997f84
PS
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 * Meta course enrolment plugin.
19 *
31ac2aef 20 * @package enrol_meta
e2382027
PS
21 * @copyright 2010 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
df997f84
PS
23 */
24
e2382027 25defined('MOODLE_INTERNAL') || die();
df997f84
PS
26
27/**
28 * Meta course enrolment plugin.
29 * @author Petr Skoda
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 */
32class enrol_meta_plugin extends enrol_plugin {
33
34 /**
35 * Returns localised name of enrol instance
36 *
ad68ed74 37 * @param stdClass $instance (null is accepted too)
df997f84
PS
38 * @return string
39 */
40 public function get_instance_name($instance) {
41 global $DB;
42
43 if (empty($instance)) {
44 $enrol = $this->get_name();
45 return get_string('pluginname', 'enrol_'.$enrol);
46 } else if (empty($instance->name)) {
47 $enrol = $this->get_name();
70f4ac5a 48 $course = $DB->get_record('course', array('id'=>$instance->customint1));
b7a5eb1e
RT
49 if ($course) {
50 $coursename = format_string(get_course_display_name_for_list($course));
51 } else {
52 // Use course id, if course is deleted.
53 $coursename = $instance->customint1;
54 }
70f4ac5a 55 return get_string('pluginname', 'enrol_' . $enrol) . ' (' . $coursename . ')';
df997f84
PS
56 } else {
57 return format_string($instance->name);
58 }
59 }
60
61 /**
62 * Returns link to page which may be used to add new instance of enrolment plugin in course.
63 * @param int $courseid
64 * @return moodle_url page url
65 */
e25f2466 66 public function get_newinstance_link($courseid) {
55bcef29 67 $context = context_course::instance($courseid, MUST_EXIST);
e2382027 68 if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/meta:config', $context)) {
df997f84
PS
69 return NULL;
70 }
71 // multiple instances supported - multiple parent courses linked
72 return new moodle_url('/enrol/meta/addinstance.php', array('id'=>$courseid));
73 }
74
ad68ed74
PS
75 /**
76 * Does this plugin allow manual unenrolment of a specific user?
77 * Yes, but only if user suspended...
78 *
79 * @param stdClass $instance course enrol instance
80 * @param stdClass $ue record from user_enrolments table
81 *
82 * @return bool - true means user with 'enrol/xxx:unenrol' may unenrol this user, false means nobody may touch this user enrolment
83 */
84 public function allow_unenrol_user(stdClass $instance, stdClass $ue) {
85 if ($ue->status == ENROL_USER_SUSPENDED) {
86 return true;
87 }
88
89 return false;
90 }
91
92 /**
93 * Gets an array of the user enrolment actions
94 *
95 * @param course_enrolment_manager $manager
96 * @param stdClass $ue A user enrolment object
97 * @return array An array of user_enrolment_actions
98 */
99 public function get_user_enrolment_actions(course_enrolment_manager $manager, $ue) {
100 $actions = array();
101 $context = $manager->get_context();
102 $instance = $ue->enrolmentinstance;
103 $params = $manager->get_moodlepage()->url->params();
104 $params['ue'] = $ue->id;
105 if ($this->allow_unenrol_user($instance, $ue) && has_capability('enrol/meta:unenrol', $context)) {
106 $url = new moodle_url('/enrol/unenroluser.php', $params);
107 $actions[] = new user_enrolment_action(new pix_icon('t/delete', ''), get_string('unenrol', 'enrol'), $url, array('class'=>'unenrollink', 'rel'=>$ue->id));
108 }
109 return $actions;
110 }
df997f84
PS
111
112 /**
113 * Called after updating/inserting course.
114 *
115 * @param bool $inserted true if course just inserted
ad68ed74
PS
116 * @param stdClass $course
117 * @param stdClass $data form data
df997f84
PS
118 * @return void
119 */
120 public function course_updated($inserted, $course, $data) {
121 global $CFG;
122
123 if (!$inserted) {
124 // sync cohort enrols
125 require_once("$CFG->dirroot/enrol/meta/locallib.php");
126 enrol_meta_sync($course->id);
127 } else {
128 // cohorts are never inserted automatically
129 }
ad68ed74
PS
130 }
131
132 /**
133 * Update instance status
134 *
135 * @param stdClass $instance
136 * @param int $newstatus ENROL_INSTANCE_ENABLED, ENROL_INSTANCE_DISABLED
137 * @return void
138 */
139 public function update_status($instance, $newstatus) {
140 global $CFG;
141
142 parent::update_status($instance, $newstatus);
df997f84 143
ad68ed74
PS
144 require_once("$CFG->dirroot/enrol/meta/locallib.php");
145 enrol_meta_sync($instance->courseid);
df997f84
PS
146 }
147
148 /**
149 * Called for all enabled enrol plugins that returned true from is_cron_required().
150 * @return void
151 */
152 public function cron() {
153 global $CFG;
154
df997f84
PS
155 require_once("$CFG->dirroot/enrol/meta/locallib.php");
156 enrol_meta_sync();
157 }
158}
159