Merge branch 'w37_MDL-27856_m24_cohortrestore' of git://github.com/skodak/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2012-09-14 01:19:50 +02:00
commit b01c955fef
12 changed files with 148 additions and 82 deletions

View File

@ -1,5 +1,4 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
@ -18,8 +17,7 @@
/**
* Adds new instance of enrol_cohort to specified course.
*
* @package enrol
* @subpackage cohort
* @package enrol_cohort
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@ -28,7 +26,7 @@ require('../../config.php');
require_once("$CFG->dirroot/enrol/cohort/addinstance_form.php");
require_once("$CFG->dirroot/enrol/cohort/locallib.php");
$id = required_param('id', PARAM_INT); // course id
$id = required_param('id', PARAM_INT); // Course id.
$course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
$context = context_course::instance($course->id, MUST_EXIST);
@ -42,7 +40,7 @@ $PAGE->set_pagelayout('admin');
navigation_node::override_active_url(new moodle_url('/enrol/instances.php', array('id'=>$course->id)));
// Try and make the manage instances node on the navigation active
// Try and make the manage instances node on the navigation active.
$courseadmin = $PAGE->settingsnav->get('courseadmin');
if ($courseadmin && $courseadmin->get('users') && $courseadmin->get('users')->get('manageinstances')) {
$courseadmin->get('users')->get('manageinstances')->make_active();

View File

@ -1,5 +1,4 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
@ -18,8 +17,7 @@
/**
* Adds instance form
*
* @package enrol
* @subpackage cohort
* @package enrol_cohort
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@ -29,17 +27,20 @@ defined('MOODLE_INTERNAL') || die();
require_once("$CFG->libdir/formslib.php");
class enrol_cohort_addinstance_form extends moodleform {
protected $course;
function definition() {
global $CFG, $DB;
$mform = $this->_form;
$course = $this->_customdata;
$coursecontext = context_course::instance($course->id);
$this->course = $this->_customdata;
$coursecontext = context_course::instance($this->course->id);
$enrol = enrol_get_plugin('cohort');
$cohorts = array('' => get_string('choosedots'));
list($sqlparents, $params) = $DB->get_in_or_equal(get_parent_contexts($coursecontext));
list($sqlparents, $params) = $DB->get_in_or_equal($coursecontext->get_parent_context_ids());
$sql = "SELECT id, name, contextid
FROM {cohort}
WHERE contextid $sqlparents
@ -56,7 +57,7 @@ class enrol_cohort_addinstance_form extends moodleform {
$roles = get_assignable_roles($coursecontext);
$roles[0] = get_string('none');
$roles = array_reverse($roles, true); // descending default sortorder
$roles = array_reverse($roles, true); // Descending default sortorder.
$mform->addElement('header','general', get_string('pluginname', 'enrol_cohort'));
@ -72,8 +73,18 @@ class enrol_cohort_addinstance_form extends moodleform {
$this->add_action_buttons(true, get_string('addinstance', 'enrol'));
$this->set_data(array('id'=>$course->id));
$this->set_data(array('id'=>$this->course->id));
}
//TODO: validate duplicate role-cohort does not exist
function validation($data, $files) {
global $DB;
$errors = parent::validation($data, $files);
if ($DB->record_exists('enrol', array('roleid'=>$data['roleid'], 'customint1'=>$data['cohortid'], 'courseid'=>$this->course->id, 'enrol'=>'cohort'))) {
$errors['cohortid'] = get_string('instanceexists', 'enrol_cohort');
}
return $errors;
}
}

View File

@ -20,8 +20,7 @@
* The general idea behind this file is that any errors should throw exceptions
* which will be returned and acted upon by the calling AJAX script.
*
* @package enrol
* @subpackage cohort
* @package enrol_cohort
* @copyright 2011 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@ -33,7 +32,7 @@ require_once($CFG->dirroot.'/enrol/locallib.php');
require_once($CFG->dirroot.'/enrol/cohort/locallib.php');
require_once($CFG->dirroot.'/group/lib.php');
// Must have the sesskey
// Must have the sesskey.
$id = required_param('id', PARAM_INT); // course id
$action = required_param('action', PARAM_ALPHANUMEXT);
@ -50,7 +49,7 @@ require_login($course);
require_capability('moodle/course:enrolreview', $context);
require_sesskey();
echo $OUTPUT->header(); // send headers
echo $OUTPUT->header(); // Send headers.
$manager = new course_enrolment_manager($PAGE, $course);

View File

@ -23,8 +23,7 @@
* - you need to change the "www-data" to match the apache user account
* - use "su" if "sudo" not available
*
* @package enrol
* @subpackage cohort
* @package enrol_cohort
* @copyright 2011 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@ -35,7 +34,7 @@ require(dirname(dirname(dirname(dirname(__FILE__)))).'/config.php');
require_once($CFG->libdir.'/clilib.php');
require_once("$CFG->dirroot/enrol/cohort/locallib.php");
// now get cli options
// Now get cli options.
list($options, $unrecognized) = cli_get_params(array('verbose'=>false, 'help'=>false), array('v'=>'verbose', 'h'=>'help'));
if ($unrecognized) {

View File

@ -36,7 +36,7 @@ $capabilities = array(
)
),
/* This is used only when sync suspends users instead of full unenrolment */
/* This is used only when sync suspends users instead of full unenrolment. */
'enrol/cohort:unenrol' => array(
'captype' => 'write',
@ -47,8 +47,3 @@ $capabilities = array(
),
);

View File

@ -25,7 +25,7 @@
defined('MOODLE_INTERNAL') || die();
/* List of handlers */
/* List of handlers. */
$handlers = array (
'cohort_member_added' => array (
'handlerfile' => '/enrol/cohort/locallib.php',

View File

@ -17,8 +17,7 @@
/**
* Meta link enrolment plugin uninstallation.
*
* @package enrol
* @subpackage cohort
* @package enrol_cohort
* @copyright 2011 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@ -38,4 +37,4 @@ function xmldb_enrol_cohort_uninstall() {
role_unassign_all(array('component'=>'enrol_cohort'));
return true;
}
}

View File

@ -1,5 +1,4 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// 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/>.
/**
* Strings for component 'enrol_cohort', language 'en', branch 'MOODLE_20_STABLE'
* Strings for component 'enrol_cohort', language 'en'
*
* @package enrol
* @subpackage cohort
* @package enrol_cohort
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@ -28,5 +26,6 @@ $string['ajaxmore'] = 'More...';
$string['cohortsearch'] = 'Search';
$string['cohort:config'] = 'Configure cohort instances';
$string['cohort:unenrol'] = 'Unenrol suspended users';
$string['instanceexists'] = 'Cohort is already synchronised with selected role';
$string['pluginname'] = 'Cohort sync';
$string['pluginname_desc'] = 'Cohort enrolment plugin synchronises cohort members with course participants.';

View File

@ -17,8 +17,7 @@
/**
* Cohort enrolment plugin.
*
* @package enrol
* @subpackage cohort
* @package enrol_cohort
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@ -32,9 +31,9 @@ defined('MOODLE_INTERNAL') || die();
*/
class enrol_cohort_plugin extends enrol_plugin {
/**
* Returns localised name of enrol instance
* Returns localised name of enrol instance.
*
* @param object $instance (null is accepted too)
* @param stdClass $instance (null is accepted too)
* @return string
*/
public function get_instance_name($instance) {
@ -67,12 +66,12 @@ class enrol_cohort_plugin extends enrol_plugin {
if (!$this->can_add_new_instances($courseid)) {
return NULL;
}
// multiple instances supported - multiple parent courses linked
// Multiple instances supported - multiple parent courses linked.
return new moodle_url('/enrol/cohort/addinstance.php', array('id'=>$courseid));
}
/**
* Given a courseid this function returns true if the user is able to enrol or configure cohorts
* Given a courseid this function returns true if the user is able to enrol or configure cohorts.
* AND there are cohorts that the user can view.
*
* @param int $courseid
@ -100,7 +99,6 @@ class enrol_cohort_plugin extends enrol_plugin {
return false;
}
/**
* Called for all enabled enrol plugins that returned true from is_cron_required().
* @return void
@ -116,8 +114,8 @@ class enrol_cohort_plugin extends enrol_plugin {
* Called after updating/inserting course.
*
* @param bool $inserted true if course just inserted
* @param object $course
* @param object $data form data
* @param stdClass $course
* @param stdClass $data form data
* @return void
*/
public function course_updated($inserted, $course, $data) {
@ -158,7 +156,7 @@ class enrol_cohort_plugin extends enrol_plugin {
}
/**
* Gets an array of the user enrolment actions
* Gets an array of the user enrolment actions.
*
* @param course_enrolment_manager $manager
* @param stdClass $ue A user enrolment object
@ -210,7 +208,7 @@ class enrol_cohort_plugin extends enrol_plugin {
$button->strings_for_js('cohort', 'cohort');
$button->strings_for_js('users', 'moodle');
// No point showing this at all if the user cant manually enrol users
// No point showing this at all if the user cant manually enrol users.
$hasmanualinstance = has_capability('enrol/manual:enrol', $manager->get_context()) && $manager->has_instance('manual');
$modules = array('moodle-enrol_cohort-quickenrolment', 'moodle-enrol_cohort-quickenrolment-skin');
@ -224,6 +222,78 @@ class enrol_cohort_plugin extends enrol_plugin {
return $button;
}
/**
* Restore instance and map settings.
*
* @param restore_enrolments_structure_step $step
* @param stdClass $data
* @param stdClass $course
* @param int $oldid
*/
public function restore_instance(restore_enrolments_structure_step $step, stdClass $data, $course, $oldid) {
global $DB, $CFG;
if (!$step->get_task()->is_samesite()) {
// No cohort restore from other sites.
$step->set_mapping('enrol', $oldid, 0);
return;
}
if ($data->roleid and $DB->record_exists('cohort', array('id'=>$data->customint1))) {
$instance = $DB->get_record('enrol', array('roleid'=>$data->roleid, 'customint1'=>$data->customint1, 'courseid'=>$course->id, 'enrol'=>$this->get_name()));
if ($instance) {
$instanceid = $instance;
} else {
$instanceid = $this->add_instance($course, (array)$data);
}
$step->set_mapping('enrol', $oldid, $instanceid);
require_once("$CFG->dirroot/enrol/cohort/locallib.php");
enrol_cohort_sync($course->id, false);
} else if ($this->get_config('unenrolaction') == ENROL_EXT_REMOVED_SUSPENDNOROLES) {
$data->customint1 = 0;
$instance = $DB->get_record('enrol', array('roleid'=>$data->roleid, 'customint1'=>$data->customint1, 'courseid'=>$course->id, 'enrol'=>$this->get_name()));
if ($instance) {
$instanceid = $instance;
} else {
$data->status = ENROL_INSTANCE_DISABLED;
$instanceid = $this->add_instance($course, (array)$data);
}
$step->set_mapping('enrol', $oldid, $instanceid);
require_once("$CFG->dirroot/enrol/cohort/locallib.php");
enrol_cohort_sync($course->id, false);
} else {
$step->set_mapping('enrol', $oldid, 0);
}
}
/**
* Restore user enrolment.
*
* @param restore_enrolments_structure_step $step
* @param stdClass $data
* @param stdClass $instance
* @param int $oldinstancestatus
* @param int $userid
*/
public function restore_user_enrolment(restore_enrolments_structure_step $step, $data, $instance, $userid, $oldinstancestatus) {
global $DB;
if ($this->get_config('unenrolaction') != ENROL_EXT_REMOVED_SUSPENDNOROLES) {
// Enrolments were already synchronised in restore_instance(), we do not want any suspended leftovers.
return;
}
// ENROL_EXT_REMOVED_SUSPENDNOROLES means all previous enrolments are restored
// but without roles and suspended.
if (!$DB->record_exists('user_enrolments', array('enrolid'=>$instance->id, 'userid'=>$userid))) {
$this->enrol_user($instance, $userid, null, $data->timestart, $data->timeend, ENROL_USER_SUSPENDED);
}
}
}

View File

@ -17,8 +17,7 @@
/**
* Local stuff for cohort enrolment plugin.
*
* @package enrol
* @subpackage cohort
* @package enrol_cohort
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@ -36,7 +35,7 @@ require_once($CFG->dirroot . '/enrol/locallib.php');
*/
class enrol_cohort_handler {
/**
* Event processor - cohort member added
* Event processor - cohort member added.
* @param stdClass $ca
* @return bool
*/
@ -47,7 +46,7 @@ class enrol_cohort_handler {
return true;
}
// does any enabled cohort instance want to sync with this cohort?
// Does any enabled cohort instance want to sync with this cohort?
$sql = "SELECT e.*, r.id as roleexists
FROM {enrol} e
LEFT JOIN {role} r ON (r.id = e.roleid)
@ -60,14 +59,14 @@ class enrol_cohort_handler {
$plugin = enrol_get_plugin('cohort');
foreach ($instances as $instance) {
if ($instance->status != ENROL_INSTANCE_ENABLED ) {
// no roles for disabled instances
// No roles for disabled instances.
$instance->roleid = 0;
} else if ($instance->roleid and !$instance->roleexists) {
// invalid role - let's just enrol, they will have to create new sync and delete this one
// Invalid role - let's just enrol, they will have to create new sync and delete this one.
$instance->roleid = 0;
}
unset($instance->roleexists);
// no problem if already enrolled
// No problem if already enrolled.
$plugin->enrol_user($instance, $ca->userid, $instance->roleid, 0, 0, ENROL_USER_ACTIVE);
}
@ -75,14 +74,14 @@ class enrol_cohort_handler {
}
/**
* Event processor - cohort member removed
* Event processor - cohort member removed.
* @param stdClass $ca
* @return bool
*/
public static function member_removed($ca) {
global $DB;
// does anything want to sync with this cohort?
// Does anything want to sync with this cohort?
if (!$instances = $DB->get_records('enrol', array('customint1'=>$ca->cohortid, 'enrol'=>'cohort'), 'id ASC')) {
return true;
}
@ -110,14 +109,14 @@ class enrol_cohort_handler {
}
/**
* Event processor - cohort deleted
* Event processor - cohort deleted.
* @param stdClass $cohort
* @return bool
*/
public static function deleted($cohort) {
global $DB;
// does anything want to sync with this cohort?
// Does anything want to sync with this cohort?
if (!$instances = $DB->get_records('enrol', array('customint1'=>$cohort->id, 'enrol'=>'cohort'), 'id ASC')) {
return true;
}
@ -149,7 +148,7 @@ class enrol_cohort_handler {
function enrol_cohort_sync($courseid = NULL, $verbose = false) {
global $CFG, $DB;
// purge all roles if cohort sync disabled, those can be recreated later here by cron or CLI
// Purge all roles if cohort sync disabled, those can be recreated later here by cron or CLI.
if (!enrol_is_enabled('cohort')) {
if ($verbose) {
mtrace('Cohort sync plugin is disabled, unassigning all plugin roles and stopping.');
@ -158,7 +157,7 @@ function enrol_cohort_sync($courseid = NULL, $verbose = false) {
return 2;
}
// unfortunately this may take a long time, this script can be interrupted without problems
// Unfortunately this may take a long time, this script can be interrupted without problems.
@set_time_limit(0);
raise_memory_limit(MEMORY_HUGE);
@ -173,7 +172,7 @@ function enrol_cohort_sync($courseid = NULL, $verbose = false) {
$unenrolaction = $plugin->get_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
// iterate through all not enrolled yet users
// Iterate through all not enrolled yet users.
$onecourse = $courseid ? "AND e.courseid = :courseid" : "";
$sql = "SELECT cm.userid, e.id AS enrolid, ue.status
FROM {cohort_members} cm
@ -204,7 +203,7 @@ function enrol_cohort_sync($courseid = NULL, $verbose = false) {
$rs->close();
// unenrol as necessary
// Unenrol as necessary.
$sql = "SELECT ue.*, e.courseid
FROM {user_enrolments} ue
JOIN {enrol} e ON (e.id = ue.enrolid AND e.enrol = 'cohort' $onecourse)
@ -217,14 +216,14 @@ function enrol_cohort_sync($courseid = NULL, $verbose = false) {
}
$instance = $instances[$ue->enrolid];
if ($unenrolaction == ENROL_EXT_REMOVED_UNENROL) {
// remove enrolment together with group membership, grades, preferences, etc.
// Temove enrolment together with group membership, grades, preferences, etc.
$plugin->unenrol_user($instance, $ue->userid);
if ($verbose) {
mtrace(" unenrolling: $ue->userid ==> $instance->courseid via cohort $instance->customint1");
}
} else { // ENROL_EXT_REMOVED_SUSPENDNOROLES
// just disable and ignore any changes
// Just disable and ignore any changes.
if ($ue->status != ENROL_USER_SUSPENDED) {
$plugin->update_user_enrol($instance, $ue->userid, ENROL_USER_SUSPENDED);
$context = context_course::instance($instance->courseid);
@ -239,7 +238,7 @@ function enrol_cohort_sync($courseid = NULL, $verbose = false) {
unset($instances);
// now assign all necessary roles to enrolled users - skip suspended instances and users
// Now assign all necessary roles to enrolled users - skip suspended instances and users.
$onecourse = $courseid ? "AND e.courseid = :courseid" : "";
$sql = "SELECT e.roleid, ue.userid, c.id AS contextid, e.id AS itemid, e.courseid
FROM {user_enrolments} ue
@ -264,7 +263,7 @@ function enrol_cohort_sync($courseid = NULL, $verbose = false) {
$rs->close();
// remove unwanted roles - sync role can not be changed, we only remove role when unenrolled
// Remove unwanted roles - sync role can not be changed, we only remove role when unenrolled.
$onecourse = $courseid ? "AND e.courseid = :courseid" : "";
$sql = "SELECT ra.roleid, ra.userid, ra.contextid, ra.itemid, e.courseid
FROM {role_assignments} ra
@ -384,7 +383,7 @@ function enrol_cohort_get_cohorts(course_enrolment_manager $manager) {
}
/**
* Check if cohort exists and user is allowed to enrol it
* Check if cohort exists and user is allowed to enrol it.
*
* @global moodle_database $DB
* @param int $cohortid Cohort ID
@ -426,10 +425,10 @@ function enrol_cohort_search_cohorts(course_enrolment_manager $manager, $offset
list($sqlparents, $params) = $DB->get_in_or_equal(get_parent_contexts($context));
// Add some additional sensible conditions
// Add some additional sensible conditions.
$tests = array('contextid ' . $sqlparents);
// Modify the query to perform the search if required
// Modify the query to perform the search if required.
if (!empty($search)) {
$conditions = array(
'name',
@ -452,17 +451,17 @@ function enrol_cohort_search_cohorts(course_enrolment_manager $manager, $offset
$order = ' ORDER BY name ASC';
$rs = $DB->get_recordset_sql($fields . $sql . $order, $params, $offset);
// Produce the output respecting parameters
// Produce the output respecting parameters.
foreach ($rs as $c) {
// Track offset
// Track offset.
$offset++;
// Check capabilities
// Check capabilities.
$context = context::instance_by_id($c->contextid);
if (!has_capability('moodle/cohort:view', $context)) {
continue;
}
if ($limit === 0) {
// we have reached the required number of items and know that there are more, exit now
// we have reached the required number of items and know that there are more, exit now.
$offset--;
break;
}
@ -472,9 +471,9 @@ function enrol_cohort_search_cohorts(course_enrolment_manager $manager, $offset
'users'=>$DB->count_records('cohort_members', array('cohortid'=>$c->id)),
'enrolled'=>in_array($c->id, $enrolled)
);
// Count items
// Count items.
$limit--;
}
$rs->close();
return array('more' => !(bool)$limit, 'offset' => $offset, 'cohorts' => $cohorts);
}
}

View File

@ -17,8 +17,7 @@
/**
* Cohort enrolment plugin settings and presets.
*
* @package enrol
* @subpackage cohort
* @package enrol_cohort
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
@ -45,4 +44,3 @@ if ($ADMIN->fulltree) {
$settings->add(new admin_setting_configselect('enrol_cohort/unenrolaction', get_string('extremovedaction', 'enrol'), get_string('extremovedaction_help', 'enrol'), ENROL_EXT_REMOVED_UNENROL, $options));
}
}

View File

@ -17,8 +17,7 @@
/**
* Cohort enrolment plugin version specification.
*
* @package enrol
* @subpackage cohort
* @package enrol_cohort
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @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->requires = 2012061700; // Requires this Moodle version
$plugin->component = 'enrol_cohort'; // Full name of the plugin (used for diagnostics)
$plugin->cron = 60*60; // run cron every hour by default, it is not out-of-sync often
$plugin->cron = 60*60; // run cron every hour by default, it is not out-of-sync often