mirror of
https://github.com/moodle/moodle.git
synced 2025-08-04 16:36:37 +02:00
MDL-34864 improve enrol_category phpdocs
This commit is contained in:
parent
455748c882
commit
6c326e2b19
9 changed files with 77 additions and 68 deletions
|
@ -1,5 +1,4 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
// This file is part of Moodle - http://moodle.org/
|
// This file is part of Moodle - http://moodle.org/
|
||||||
//
|
//
|
||||||
// Moodle is free software: you can redistribute it and/or modify
|
// Moodle is free software: you can redistribute it and/or modify
|
||||||
|
@ -19,15 +18,14 @@
|
||||||
* CLI sync for full category enrol synchronisation.
|
* CLI sync for full category enrol synchronisation.
|
||||||
*
|
*
|
||||||
* Sample execution:
|
* Sample execution:
|
||||||
* $sudo -u www-data /usr/bin/php /var/www/moodle/enrol/category/cli/sync.php
|
* $ sudo -u www-data /usr/bin/php /var/www/moodle/enrol/category/cli/sync.php
|
||||||
*
|
*
|
||||||
* Notes:
|
* Notes:
|
||||||
* - it is required to use the web server account when executing PHP CLI scripts
|
* - it is required to use the web server account when executing PHP CLI scripts
|
||||||
* - you need to change the "www-data" to match the apache user account
|
* - you need to change the "www-data" to match the apache user account
|
||||||
* - use "su" if "sudo" not available
|
* - use "su" if "sudo" not available
|
||||||
*
|
*
|
||||||
* @package enrol
|
* @package enrol_category
|
||||||
* @subpackage category
|
|
||||||
* @copyright 2010 Petr Skoda {@link http://skodak.org}
|
* @copyright 2010 Petr Skoda {@link http://skodak.org}
|
||||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||||
*/
|
*/
|
||||||
|
@ -63,7 +61,7 @@ Example:
|
||||||
|
|
||||||
|
|
||||||
if (!enrol_is_enabled('category')) {
|
if (!enrol_is_enabled('category')) {
|
||||||
cli_error('enrol_category plugin is disabled, synchronisation stopped');
|
cli_error('enrol_category plugin is disabled, synchronisation stopped', 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
$verbose = !empty($options['verbose']);
|
$verbose = !empty($options['verbose']);
|
||||||
|
|
|
@ -25,9 +25,9 @@
|
||||||
defined('MOODLE_INTERNAL') || die();
|
defined('MOODLE_INTERNAL') || die();
|
||||||
|
|
||||||
$capabilities = array(
|
$capabilities = array(
|
||||||
// marks roles that have category role assignments synchronised to course enrolments
|
// Marks roles that have category role assignments synchronised to course enrolments
|
||||||
// overrides below system context are ignored (for performance reasons).
|
// overrides below system context are ignored (for performance reasons).
|
||||||
// by default his is not allowed in new installs, admins have to explicitly allow category enrolments
|
// By default his is not allowed in new installs, admins have to explicitly allow category enrolments.
|
||||||
'enrol/category:synchronised' => array(
|
'enrol/category:synchronised' => array(
|
||||||
'captype' => 'write',
|
'captype' => 'write',
|
||||||
'contextlevel' => CONTEXT_SYSTEM,
|
'contextlevel' => CONTEXT_SYSTEM,
|
||||||
|
|
|
@ -17,10 +17,10 @@
|
||||||
/**
|
/**
|
||||||
* Category enrolment plugin event handler definition.
|
* Category enrolment plugin event handler definition.
|
||||||
*
|
*
|
||||||
* @package enrol_category
|
* @package enrol_category
|
||||||
* @category event
|
* @category event
|
||||||
* @copyright 2010 Petr Skoda {@link http://skodak.org}
|
* @copyright 2010 Petr Skoda {@link http://skodak.org}
|
||||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||||
*/
|
*/
|
||||||
|
|
||||||
defined('MOODLE_INTERNAL') || die();
|
defined('MOODLE_INTERNAL') || die();
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
// This file is part of Moodle - http://moodle.org/
|
// This file is part of Moodle - http://moodle.org/
|
||||||
//
|
//
|
||||||
// Moodle is free software: you can redistribute it and/or modify
|
// Moodle is free software: you can redistribute it and/or modify
|
||||||
|
@ -18,8 +17,7 @@
|
||||||
/**
|
/**
|
||||||
* category enrolment plugin installation.
|
* category enrolment plugin installation.
|
||||||
*
|
*
|
||||||
* @package enrol
|
* @package enrol_category
|
||||||
* @subpackage category
|
|
||||||
* @copyright 2010 Petr Skoda {@link http://skodak.org}
|
* @copyright 2010 Petr Skoda {@link http://skodak.org}
|
||||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
// This file is part of Moodle - http://moodle.org/
|
// This file is part of Moodle - http://moodle.org/
|
||||||
//
|
//
|
||||||
// Moodle is free software: you can redistribute it and/or modify
|
// Moodle is free software: you can redistribute it and/or modify
|
||||||
|
@ -16,10 +15,9 @@
|
||||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Strings for component 'enrol_category', language 'en', branch 'MOODLE_20_STABLE'
|
* Strings for component 'enrol_category', language 'en'.
|
||||||
*
|
*
|
||||||
* @package enrol
|
* @package enrol_category
|
||||||
* @subpackage category
|
|
||||||
* @copyright 2010 Petr Skoda {@link http://skodak.org}
|
* @copyright 2010 Petr Skoda {@link http://skodak.org}
|
||||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
// This file is part of Moodle - http://moodle.org/
|
// This file is part of Moodle - http://moodle.org/
|
||||||
//
|
//
|
||||||
// Moodle is free software: you can redistribute it and/or modify
|
// Moodle is free software: you can redistribute it and/or modify
|
||||||
|
@ -18,25 +17,25 @@
|
||||||
/**
|
/**
|
||||||
* Category enrolment plugin.
|
* Category enrolment plugin.
|
||||||
*
|
*
|
||||||
* @package enrol
|
* @package enrol_category
|
||||||
* @subpackage category
|
|
||||||
* @copyright 2010 Petr Skoda {@link http://skodak.org}
|
* @copyright 2010 Petr Skoda {@link http://skodak.org}
|
||||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||||
*/
|
*/
|
||||||
|
|
||||||
defined('MOODLE_INTERNAL') || die();
|
defined('MOODLE_INTERNAL') || die();
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* category enrolment plugin implementation.
|
* category enrolment plugin implementation.
|
||||||
* @author Petr Skoda
|
* @author Petr Skoda
|
||||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||||
*/
|
*/
|
||||||
class enrol_category_plugin extends enrol_plugin {
|
class enrol_category_plugin extends enrol_plugin {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Is it possible to delete enrol instance via standard UI?
|
* Is it possible to delete enrol instance via standard UI?
|
||||||
*
|
*
|
||||||
* @param object $instance
|
* @param stdClass $instance
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function instance_deleteable($instance) {
|
public function instance_deleteable($instance) {
|
||||||
|
@ -45,7 +44,7 @@ class enrol_category_plugin extends enrol_plugin {
|
||||||
if (!enrol_is_enabled('category')) {
|
if (!enrol_is_enabled('category')) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
// allow delete only when no synced users here
|
// Allow delete only when no synced users here.
|
||||||
return !$DB->record_exists('user_enrolments', array('enrolid'=>$instance->id));
|
return !$DB->record_exists('user_enrolments', array('enrolid'=>$instance->id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -55,8 +54,8 @@ class enrol_category_plugin extends enrol_plugin {
|
||||||
* @return moodle_url page url
|
* @return moodle_url page url
|
||||||
*/
|
*/
|
||||||
public function get_newinstance_link($courseid) {
|
public function get_newinstance_link($courseid) {
|
||||||
// instances are added automatically as necessary
|
// Instances are added automatically as necessary.
|
||||||
return NULL;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -78,8 +77,8 @@ class enrol_category_plugin extends enrol_plugin {
|
||||||
* Called after updating/inserting course.
|
* Called after updating/inserting course.
|
||||||
*
|
*
|
||||||
* @param bool $inserted true if course just inserted
|
* @param bool $inserted true if course just inserted
|
||||||
* @param object $course
|
* @param stdClass $course
|
||||||
* @param object $data form data
|
* @param stdClass $data form data
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function course_updated($inserted, $course, $data) {
|
public function course_updated($inserted, $course, $data) {
|
||||||
|
@ -89,10 +88,8 @@ class enrol_category_plugin extends enrol_plugin {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// sync category enrols
|
// Sync category enrols.
|
||||||
require_once("$CFG->dirroot/enrol/category/locallib.php");
|
require_once("$CFG->dirroot/enrol/category/locallib.php");
|
||||||
enrol_category_sync_course($course);
|
enrol_category_sync_course($course);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
// This file is part of Moodle - http://moodle.org/
|
// This file is part of Moodle - http://moodle.org/
|
||||||
//
|
//
|
||||||
// Moodle is free software: you can redistribute it and/or modify
|
// Moodle is free software: you can redistribute it and/or modify
|
||||||
|
@ -18,14 +17,14 @@
|
||||||
/**
|
/**
|
||||||
* Local stuff for category enrolment plugin.
|
* Local stuff for category enrolment plugin.
|
||||||
*
|
*
|
||||||
* @package enrol
|
* @package enrol_category
|
||||||
* @subpackage category
|
|
||||||
* @copyright 2010 Petr Skoda {@link http://skodak.org}
|
* @copyright 2010 Petr Skoda {@link http://skodak.org}
|
||||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||||
*/
|
*/
|
||||||
|
|
||||||
defined('MOODLE_INTERNAL') || die();
|
defined('MOODLE_INTERNAL') || die();
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Event handler for category enrolment plugin.
|
* Event handler for category enrolment plugin.
|
||||||
*
|
*
|
||||||
|
@ -33,6 +32,12 @@ defined('MOODLE_INTERNAL') || die();
|
||||||
* it may fail sometimes, so we always do a full sync in cron too.
|
* it may fail sometimes, so we always do a full sync in cron too.
|
||||||
*/
|
*/
|
||||||
class enrol_category_handler {
|
class enrol_category_handler {
|
||||||
|
/**
|
||||||
|
* Triggered when user is assigned a new role.
|
||||||
|
* @static
|
||||||
|
* @param stdClass $ra
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
public static function role_assigned($ra) {
|
public static function role_assigned($ra) {
|
||||||
global $DB;
|
global $DB;
|
||||||
|
|
||||||
|
@ -40,20 +45,20 @@ class enrol_category_handler {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
//only category level roles are interesting
|
// Only category level roles are interesting.
|
||||||
$parentcontext = get_context_instance_by_id($ra->contextid);
|
$parentcontext = get_context_instance_by_id($ra->contextid);
|
||||||
if ($parentcontext->contextlevel != CONTEXT_COURSECAT) {
|
if ($parentcontext->contextlevel != CONTEXT_COURSECAT) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// make sure the role is to be actually synchronised
|
// Make sure the role is to be actually synchronised,
|
||||||
// please note we are ignoring overrides of the synchronised capability (for performance reasons in full sync)
|
// please note we are ignoring overrides of the synchronised capability (for performance reasons in full sync).
|
||||||
$syscontext = context_system::instance();
|
$syscontext = context_system::instance();
|
||||||
if (!$DB->record_exists('role_capabilities', array('contextid'=>$syscontext->id, 'roleid'=>$ra->roleid, 'capability'=>'enrol/category:synchronised', 'permission'=>CAP_ALLOW))) {
|
if (!$DB->record_exists('role_capabilities', array('contextid'=>$syscontext->id, 'roleid'=>$ra->roleid, 'capability'=>'enrol/category:synchronised', 'permission'=>CAP_ALLOW))) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// add necessary enrol instances
|
// Add necessary enrol instances.
|
||||||
$plugin = enrol_get_plugin('category');
|
$plugin = enrol_get_plugin('category');
|
||||||
$sql = "SELECT c.*
|
$sql = "SELECT c.*
|
||||||
FROM {course} c
|
FROM {course} c
|
||||||
|
@ -67,7 +72,7 @@ class enrol_category_handler {
|
||||||
}
|
}
|
||||||
$rs->close();
|
$rs->close();
|
||||||
|
|
||||||
// now look for missing enrols
|
// Now look for missing enrolments.
|
||||||
$sql = "SELECT e.*
|
$sql = "SELECT e.*
|
||||||
FROM {course} c
|
FROM {course} c
|
||||||
JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :courselevel AND ctx.path LIKE :match)
|
JOIN {context} ctx ON (ctx.instanceid = c.id AND ctx.contextlevel = :courselevel AND ctx.path LIKE :match)
|
||||||
|
@ -84,6 +89,12 @@ class enrol_category_handler {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Triggered when user role is unassigned.
|
||||||
|
* @static
|
||||||
|
* @param stdClass $ra
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
public static function role_unassigned($ra) {
|
public static function role_unassigned($ra) {
|
||||||
global $DB;
|
global $DB;
|
||||||
|
|
||||||
|
@ -91,13 +102,13 @@ class enrol_category_handler {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// only category level roles are interesting
|
// Only category level roles are interesting.
|
||||||
$parentcontext = get_context_instance_by_id($ra->contextid);
|
$parentcontext = get_context_instance_by_id($ra->contextid);
|
||||||
if ($parentcontext->contextlevel != CONTEXT_COURSECAT) {
|
if ($parentcontext->contextlevel != CONTEXT_COURSECAT) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// now this is going to be a bit slow, take all enrolments in child courses and verify each separately
|
// Now this is going to be a bit slow, take all enrolments in child courses and verify each separately.
|
||||||
$syscontext = context_system::instance();
|
$syscontext = context_system::instance();
|
||||||
if (!$roles = get_roles_with_capability('enrol/category:synchronised', CAP_ALLOW, $syscontext)) {
|
if (!$roles = get_roles_with_capability('enrol/category:synchronised', CAP_ALLOW, $syscontext)) {
|
||||||
return true;
|
return true;
|
||||||
|
@ -119,7 +130,7 @@ class enrol_category_handler {
|
||||||
foreach ($rs as $instance) {
|
foreach ($rs as $instance) {
|
||||||
$coursecontext = context_course::instance($instance->courseid);
|
$coursecontext = context_course::instance($instance->courseid);
|
||||||
$contextids = get_parent_contexts($coursecontext);
|
$contextids = get_parent_contexts($coursecontext);
|
||||||
array_pop($contextids); // remove system context, we are interested in categories only
|
array_pop($contextids); // Remove system context, we are interested in categories only.
|
||||||
|
|
||||||
list($contextids, $contextparams) = $DB->get_in_or_equal($contextids, SQL_PARAMS_NAMED, 'c');
|
list($contextids, $contextparams) = $DB->get_in_or_equal($contextids, SQL_PARAMS_NAMED, 'c');
|
||||||
$params = array_merge($params, $contextparams);
|
$params = array_merge($params, $contextparams);
|
||||||
|
@ -128,7 +139,7 @@ class enrol_category_handler {
|
||||||
FROM {role_assignments} ra
|
FROM {role_assignments} ra
|
||||||
WHERE ra.userid = :userid AND ra.contextid $contextids AND ra.roleid $roleids";
|
WHERE ra.userid = :userid AND ra.contextid $contextids AND ra.roleid $roleids";
|
||||||
if (!$DB->record_exists_sql($sql, $params)) {
|
if (!$DB->record_exists_sql($sql, $params)) {
|
||||||
// user does not have any interesting role in any parent context, let's unenrol
|
// User does not have any interesting role in any parent context, let's unenrol.
|
||||||
$plugin->unenrol_user($instance, $ra->userid);
|
$plugin->unenrol_user($instance, $ra->userid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -140,7 +151,7 @@ class enrol_category_handler {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sync all category enrolments in one course
|
* Sync all category enrolments in one course
|
||||||
* @param int $courseid course id
|
* @param stdClass $course
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
function enrol_category_sync_course($course) {
|
function enrol_category_sync_course($course) {
|
||||||
|
@ -156,7 +167,7 @@ function enrol_category_sync_course($course) {
|
||||||
$roles = get_roles_with_capability('enrol/category:synchronised', CAP_ALLOW, $syscontext);
|
$roles = get_roles_with_capability('enrol/category:synchronised', CAP_ALLOW, $syscontext);
|
||||||
|
|
||||||
if (!$roles) {
|
if (!$roles) {
|
||||||
//nothing to sync, so remove the instance completely if exists
|
// Nothing to sync, so remove the instance completely if exists.
|
||||||
if ($instances = $DB->get_records('enrol', array('courseid'=>$course->id, 'enrol'=>'category'))) {
|
if ($instances = $DB->get_records('enrol', array('courseid'=>$course->id, 'enrol'=>'category'))) {
|
||||||
foreach ($instances as $instance) {
|
foreach ($instances as $instance) {
|
||||||
$plugin->delete_instance($instance);
|
$plugin->delete_instance($instance);
|
||||||
|
@ -165,10 +176,10 @@ function enrol_category_sync_course($course) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// first find out if any parent category context contains interesting role assignments
|
// First find out if any parent category context contains interesting role assignments.
|
||||||
$coursecontext = context_course::instance($course->id);
|
$coursecontext = context_course::instance($course->id);
|
||||||
$contextids = get_parent_contexts($coursecontext);
|
$contextids = get_parent_contexts($coursecontext);
|
||||||
array_pop($contextids); // remove system context, we are interested in categories only
|
array_pop($contextids); // Remove system context, we are interested in categories only.
|
||||||
|
|
||||||
list($roleids, $params) = $DB->get_in_or_equal(array_keys($roles), SQL_PARAMS_NAMED, 'r');
|
list($roleids, $params) = $DB->get_in_or_equal(array_keys($roles), SQL_PARAMS_NAMED, 'r');
|
||||||
list($contextids, $contextparams) = $DB->get_in_or_equal($contextids, SQL_PARAMS_NAMED, 'c');
|
list($contextids, $contextparams) = $DB->get_in_or_equal($contextids, SQL_PARAMS_NAMED, 'c');
|
||||||
|
@ -180,7 +191,7 @@ function enrol_category_sync_course($course) {
|
||||||
WHERE roleid $roleids AND contextid $contextids";
|
WHERE roleid $roleids AND contextid $contextids";
|
||||||
if (!$DB->record_exists_sql($sql, $params)) {
|
if (!$DB->record_exists_sql($sql, $params)) {
|
||||||
if ($instances = $DB->get_records('enrol', array('courseid'=>$course->id, 'enrol'=>'category'))) {
|
if ($instances = $DB->get_records('enrol', array('courseid'=>$course->id, 'enrol'=>'category'))) {
|
||||||
// should be max one instance, but anyway
|
// Should be max one instance, but anyway.
|
||||||
foreach ($instances as $instance) {
|
foreach ($instances as $instance) {
|
||||||
$plugin->delete_instance($instance);
|
$plugin->delete_instance($instance);
|
||||||
}
|
}
|
||||||
|
@ -188,7 +199,7 @@ function enrol_category_sync_course($course) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// make sure the enrol instance exists - there should be always only one instance
|
// Make sure the enrol instance exists - there should be always only one instance.
|
||||||
$delinstances = array();
|
$delinstances = array();
|
||||||
if ($instances = $DB->get_records('enrol', array('courseid'=>$course->id, 'enrol'=>'category'))) {
|
if ($instances = $DB->get_records('enrol', array('courseid'=>$course->id, 'enrol'=>'category'))) {
|
||||||
$instance = array_shift($instances);
|
$instance = array_shift($instances);
|
||||||
|
@ -198,7 +209,7 @@ function enrol_category_sync_course($course) {
|
||||||
$instance = $DB->get_record('enrol', array('id'=>$i));
|
$instance = $DB->get_record('enrol', array('id'=>$i));
|
||||||
}
|
}
|
||||||
|
|
||||||
// add new enrolments
|
// Add new enrolments.
|
||||||
$sql = "SELECT ra.userid, ra.estart
|
$sql = "SELECT ra.userid, ra.estart
|
||||||
FROM (SELECT xra.userid, MIN(xra.timemodified) AS estart
|
FROM (SELECT xra.userid, MIN(xra.timemodified) AS estart
|
||||||
FROM {role_assignments} xra
|
FROM {role_assignments} xra
|
||||||
|
@ -214,7 +225,7 @@ function enrol_category_sync_course($course) {
|
||||||
}
|
}
|
||||||
$rs->close();
|
$rs->close();
|
||||||
|
|
||||||
// remove unwanted enrolments
|
// Remove unwanted enrolments.
|
||||||
$sql = "SELECT DISTINCT ue.userid
|
$sql = "SELECT DISTINCT ue.userid
|
||||||
FROM {user_enrolments} ue
|
FROM {user_enrolments} ue
|
||||||
LEFT JOIN {role_assignments} ra ON (ra.roleid $roleids AND ra.contextid $contextids AND ra.userid = ue.userid)
|
LEFT JOIN {role_assignments} ra ON (ra.roleid $roleids AND ra.contextid $contextids AND ra.userid = ue.userid)
|
||||||
|
@ -226,13 +237,24 @@ function enrol_category_sync_course($course) {
|
||||||
$rs->close();
|
$rs->close();
|
||||||
|
|
||||||
if ($delinstances) {
|
if ($delinstances) {
|
||||||
// we have to do this as the last step in order to prevent temporary unenrolment
|
// We have to do this as the last step in order to prevent temporary unenrolment.
|
||||||
foreach ($delinstances as $delinstance) {
|
foreach ($delinstances as $delinstance) {
|
||||||
$plugin->delete_instance($delinstance);
|
$plugin->delete_instance($delinstance);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Synchronise courses in all categories.
|
||||||
|
*
|
||||||
|
* It gets out-of-sync if:
|
||||||
|
* - you move course to different category
|
||||||
|
* - reorder categories
|
||||||
|
* - disable enrol_category and enable it again
|
||||||
|
*
|
||||||
|
* @param bool $verbose
|
||||||
|
* @return int exit code - 0 is ok, 1 means error, 2 if plugin disabled
|
||||||
|
*/
|
||||||
function enrol_category_sync_full($verbose = false) {
|
function enrol_category_sync_full($verbose = false) {
|
||||||
global $DB;
|
global $DB;
|
||||||
|
|
||||||
|
@ -241,14 +263,14 @@ function enrol_category_sync_full($verbose = false) {
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
// we may need a lot of time here
|
// We may need a lot of time here.
|
||||||
@set_time_limit(0);
|
@set_time_limit(0);
|
||||||
|
|
||||||
$plugin = enrol_get_plugin('category');
|
$plugin = enrol_get_plugin('category');
|
||||||
|
|
||||||
$syscontext = context_system::instance();
|
$syscontext = context_system::instance();
|
||||||
|
|
||||||
// any interesting roles worth synchronising?
|
// Any interesting roles worth synchronising?
|
||||||
if (!$roles = get_roles_with_capability('enrol/category:synchronised', CAP_ALLOW, $syscontext)) {
|
if (!$roles = get_roles_with_capability('enrol/category:synchronised', CAP_ALLOW, $syscontext)) {
|
||||||
// yay, nothing to do, so let's remove all leftovers
|
// yay, nothing to do, so let's remove all leftovers
|
||||||
if ($verbose) {
|
if ($verbose) {
|
||||||
|
@ -273,10 +295,10 @@ function enrol_category_sync_full($verbose = false) {
|
||||||
$params['courselevel'] = CONTEXT_COURSE;
|
$params['courselevel'] = CONTEXT_COURSE;
|
||||||
$params['catlevel'] = CONTEXT_COURSECAT;
|
$params['catlevel'] = CONTEXT_COURSECAT;
|
||||||
|
|
||||||
// first of all add necessary enrol instances to all courses
|
// First of all add necessary enrol instances to all courses.
|
||||||
$parentcat = $DB->sql_concat("cat.path", "'/%'");
|
$parentcat = $DB->sql_concat("cat.path", "'/%'");
|
||||||
$parentcctx = $DB->sql_concat("cctx.path", "'/%'");
|
$parentcctx = $DB->sql_concat("cctx.path", "'/%'");
|
||||||
// need whole course records to be used by add_instance(), use inner view (ci) to
|
// Need whole course records to be used by add_instance(), use inner view (ci) to
|
||||||
// get distinct records only.
|
// get distinct records only.
|
||||||
// TODO: Moodle 2.1. Improve enrol API to accept courseid / courserec
|
// TODO: Moodle 2.1. Improve enrol API to accept courseid / courserec
|
||||||
$sql = "SELECT c.*
|
$sql = "SELECT c.*
|
||||||
|
@ -299,8 +321,8 @@ function enrol_category_sync_full($verbose = false) {
|
||||||
}
|
}
|
||||||
$rs->close();
|
$rs->close();
|
||||||
|
|
||||||
// now look for courses that do not have any interesting roles in parent contexts,
|
// Now look for courses that do not have any interesting roles in parent contexts,
|
||||||
// but still have the instance and delete them
|
// but still have the instance and delete them.
|
||||||
$sql = "SELECT e.*
|
$sql = "SELECT e.*
|
||||||
FROM {enrol} e
|
FROM {enrol} e
|
||||||
JOIN {context} ctx ON (ctx.instanceid = e.courseid AND ctx.contextlevel = :courselevel)
|
JOIN {context} ctx ON (ctx.instanceid = e.courseid AND ctx.contextlevel = :courselevel)
|
||||||
|
@ -316,7 +338,7 @@ function enrol_category_sync_full($verbose = false) {
|
||||||
}
|
}
|
||||||
$rs->close();
|
$rs->close();
|
||||||
|
|
||||||
// add missing enrolments
|
// Add missing enrolments.
|
||||||
$sql = "SELECT e.*, cat.userid, cat.estart
|
$sql = "SELECT e.*, cat.userid, cat.estart
|
||||||
FROM {enrol} e
|
FROM {enrol} e
|
||||||
JOIN {context} ctx ON (ctx.instanceid = e.courseid AND ctx.contextlevel = :courselevel)
|
JOIN {context} ctx ON (ctx.instanceid = e.courseid AND ctx.contextlevel = :courselevel)
|
||||||
|
@ -341,7 +363,7 @@ function enrol_category_sync_full($verbose = false) {
|
||||||
}
|
}
|
||||||
$rs->close();
|
$rs->close();
|
||||||
|
|
||||||
// remove stale enrolments
|
// Remove stale enrolments.
|
||||||
$sql = "SELECT e.*, ue.userid
|
$sql = "SELECT e.*, ue.userid
|
||||||
FROM {enrol} e
|
FROM {enrol} e
|
||||||
JOIN {context} ctx ON (ctx.instanceid = e.courseid AND ctx.contextlevel = :courselevel)
|
JOIN {context} ctx ON (ctx.instanceid = e.courseid AND ctx.contextlevel = :courselevel)
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
// This file is part of Moodle - http://moodle.org/
|
// This file is part of Moodle - http://moodle.org/
|
||||||
//
|
//
|
||||||
// Moodle is free software: you can redistribute it and/or modify
|
// Moodle is free software: you can redistribute it and/or modify
|
||||||
|
@ -16,10 +15,9 @@
|
||||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* category enrolment plugin settings and presets.
|
* Category enrolment plugin settings and presets.
|
||||||
*
|
*
|
||||||
* @package enrol
|
* @package enrol_category
|
||||||
* @subpackage category
|
|
||||||
* @copyright 2010 Petr Skoda {@link http://skodak.org}
|
* @copyright 2010 Petr Skoda {@link http://skodak.org}
|
||||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||||
*/
|
*/
|
||||||
|
@ -35,4 +33,3 @@ if ($ADMIN->fulltree) {
|
||||||
//--- enrol instance defaults ----------------------------------------------------------------------------
|
//--- enrol instance defaults ----------------------------------------------------------------------------
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,8 +17,7 @@
|
||||||
/**
|
/**
|
||||||
* Category enrolment plugin version specification.
|
* Category enrolment plugin version specification.
|
||||||
*
|
*
|
||||||
* @package enrol
|
* @package enrol_category
|
||||||
* @subpackage category
|
|
||||||
* @copyright 2010 Petr Skoda {@link http://skodak.org}
|
* @copyright 2010 Petr Skoda {@link http://skodak.org}
|
||||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||||
*/
|
*/
|
||||||
|
@ -28,4 +27,4 @@ defined('MOODLE_INTERNAL') || die();
|
||||||
$plugin->version = 2012061700; // The current plugin version (Date: YYYYMMDDXX)
|
$plugin->version = 2012061700; // The current plugin version (Date: YYYYMMDDXX)
|
||||||
$plugin->requires = 2012061700; // Requires this Moodle version
|
$plugin->requires = 2012061700; // Requires this Moodle version
|
||||||
$plugin->component = 'enrol_category'; // Full name of the plugin (used for diagnostics)
|
$plugin->component = 'enrol_category'; // Full name of the plugin (used for diagnostics)
|
||||||
$plugin->cron = 60;
|
$plugin->cron = 60;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue