MDL-50373 questions: random Qs must not pick deleted or sub- questions
[moodle.git] / enrol / meta / lib.php
blob538fd1fd3ffdeef3b9eb54eef98b74adaec484f8
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 * Meta course enrolment plugin.
20 * @package enrol_meta
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();
27 /**
28 * ENROL_META_CREATE_GROUP constant for automatically creating a group for a meta course.
30 define('ENROL_META_CREATE_GROUP', -1);
32 /**
33 * Meta course enrolment plugin.
34 * @author Petr Skoda
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class enrol_meta_plugin extends enrol_plugin {
39 /**
40 * Returns localised name of enrol instance
42 * @param stdClass $instance (null is accepted too)
43 * @return string
45 public function get_instance_name($instance) {
46 global $DB;
48 if (empty($instance)) {
49 $enrol = $this->get_name();
50 return get_string('pluginname', 'enrol_'.$enrol);
51 } else if (empty($instance->name)) {
52 $enrol = $this->get_name();
53 $course = $DB->get_record('course', array('id'=>$instance->customint1));
54 if ($course) {
55 $coursename = format_string(get_course_display_name_for_list($course));
56 } else {
57 // Use course id, if course is deleted.
58 $coursename = $instance->customint1;
60 return get_string('pluginname', 'enrol_' . $enrol) . ' (' . $coursename . ')';
61 } else {
62 return format_string($instance->name);
66 /**
67 * Returns link to page which may be used to add new instance of enrolment plugin in course.
68 * @param int $courseid
69 * @return moodle_url page url
71 public function get_newinstance_link($courseid) {
72 $context = context_course::instance($courseid, MUST_EXIST);
73 if (!has_capability('moodle/course:enrolconfig', $context) or !has_capability('enrol/meta:config', $context)) {
74 return NULL;
76 // multiple instances supported - multiple parent courses linked
77 return new moodle_url('/enrol/meta/addinstance.php', array('id'=>$courseid));
80 /**
81 * Does this plugin allow manual unenrolment of a specific user?
82 * Yes, but only if user suspended...
84 * @param stdClass $instance course enrol instance
85 * @param stdClass $ue record from user_enrolments table
87 * @return bool - true means user with 'enrol/xxx:unenrol' may unenrol this user, false means nobody may touch this user enrolment
89 public function allow_unenrol_user(stdClass $instance, stdClass $ue) {
90 if ($ue->status == ENROL_USER_SUSPENDED) {
91 return true;
94 return false;
97 /**
98 * Gets an array of the user enrolment actions
100 * @param course_enrolment_manager $manager
101 * @param stdClass $ue A user enrolment object
102 * @return array An array of user_enrolment_actions
104 public function get_user_enrolment_actions(course_enrolment_manager $manager, $ue) {
105 $actions = array();
106 $context = $manager->get_context();
107 $instance = $ue->enrolmentinstance;
108 $params = $manager->get_moodlepage()->url->params();
109 $params['ue'] = $ue->id;
110 if ($this->allow_unenrol_user($instance, $ue) && has_capability('enrol/meta:unenrol', $context)) {
111 $url = new moodle_url('/enrol/unenroluser.php', $params);
112 $actions[] = new user_enrolment_action(new pix_icon('t/delete', ''), get_string('unenrol', 'enrol'), $url, array('class'=>'unenrollink', 'rel'=>$ue->id));
114 return $actions;
118 * Called after updating/inserting course.
120 * @param bool $inserted true if course just inserted
121 * @param stdClass $course
122 * @param stdClass $data form data
123 * @return void
125 public function course_updated($inserted, $course, $data) {
126 // Meta sync updates are slow, if enrolments get out of sync teacher will have to wait till next cron.
127 // We should probably add some sync button to the course enrol methods overview page.
131 * Update instance status
133 * @param stdClass $instance
134 * @param int $newstatus ENROL_INSTANCE_ENABLED, ENROL_INSTANCE_DISABLED
135 * @return void
137 public function update_status($instance, $newstatus) {
138 global $CFG;
140 parent::update_status($instance, $newstatus);
142 require_once("$CFG->dirroot/enrol/meta/locallib.php");
143 enrol_meta_sync($instance->courseid);
147 * Called for all enabled enrol plugins that returned true from is_cron_required().
148 * @return void
150 public function cron() {
151 global $CFG;
153 require_once("$CFG->dirroot/enrol/meta/locallib.php");
154 enrol_meta_sync();
158 * Is it possible to delete enrol instance via standard UI?
160 * @param stdClass $instance
161 * @return bool
163 public function can_delete_instance($instance) {
164 $context = context_course::instance($instance->courseid);
165 return has_capability('enrol/meta:config', $context);
169 * Is it possible to hide/show enrol instance via standard UI?
171 * @param stdClass $instance
172 * @return bool
174 public function can_hide_show_instance($instance) {
175 $context = context_course::instance($instance->courseid);
176 return has_capability('enrol/meta:config', $context);
180 * Restore instance and map settings.
182 * @param restore_enrolments_structure_step $step
183 * @param stdClass $data
184 * @param stdClass $course
185 * @param int $oldid
187 public function restore_instance(restore_enrolments_structure_step $step, stdClass $data, $course, $oldid) {
188 global $DB, $CFG;
190 if (!$step->get_task()->is_samesite()) {
191 // No meta restore from other sites.
192 $step->set_mapping('enrol', $oldid, 0);
193 return;
196 if (!empty($data->customint2)) {
197 $data->customint2 = $step->get_mappingid('group', $data->customint2);
200 if ($DB->record_exists('course', array('id' => $data->customint1))) {
201 $instance = $DB->get_record('enrol', array('roleid' => $data->roleid, 'customint1' => $data->customint1,
202 'courseid' => $course->id, 'enrol' => $this->get_name()));
203 if ($instance) {
204 $instanceid = $instance->id;
205 } else {
206 $instanceid = $this->add_instance($course, (array)$data);
208 $step->set_mapping('enrol', $oldid, $instanceid);
210 require_once("$CFG->dirroot/enrol/meta/locallib.php");
211 enrol_meta_sync($data->customint1);
213 } else {
214 $step->set_mapping('enrol', $oldid, 0);
219 * Restore user enrolment.
221 * @param restore_enrolments_structure_step $step
222 * @param stdClass $data
223 * @param stdClass $instance
224 * @param int $userid
225 * @param int $oldinstancestatus
227 public function restore_user_enrolment(restore_enrolments_structure_step $step, $data, $instance, $userid, $oldinstancestatus) {
228 global $DB;
230 if ($this->get_config('unenrolaction') != ENROL_EXT_REMOVED_SUSPENDNOROLES) {
231 // Enrolments were already synchronised in restore_instance(), we do not want any suspended leftovers.
232 return;
235 // ENROL_EXT_REMOVED_SUSPENDNOROLES means all previous enrolments are restored
236 // but without roles and suspended.
238 if (!$DB->record_exists('user_enrolments', array('enrolid' => $instance->id, 'userid' => $userid))) {
239 $this->enrol_user($instance, $userid, null, $data->timestart, $data->timeend, ENROL_USER_SUSPENDED);
240 if ($instance->customint2) {
241 groups_add_member($instance->customint2, $userid, 'enrol_meta', $instance->id);
247 * Restore user group membership.
248 * @param stdClass $instance
249 * @param int $groupid
250 * @param int $userid
252 public function restore_group_member($instance, $groupid, $userid) {
253 // Nothing to do here, the group members are added in $this->restore_group_restored().
254 return;
258 * Returns edit icons for the page with list of instances.
259 * @param stdClass $instance
260 * @return array
262 public function get_action_icons(stdClass $instance) {
263 global $OUTPUT;
265 if ($instance->enrol !== 'meta') {
266 throw new coding_exception('invalid enrol instance!');
268 $context = context_course::instance($instance->courseid);
270 $icons = array();
272 if (has_capability('enrol/meta:config', $context)) {
273 $editlink = new moodle_url("/enrol/meta/addinstance.php",
274 array('id' => $instance->courseid, 'enrolid' => $instance->id));
275 $icons[] = $OUTPUT->action_icon($editlink, new pix_icon('t/edit', get_string('edit'), 'core',
276 array('class' => 'iconsmall')));
279 return $icons;