Commit 005e57a2 authored by Petr Škoda's avatar Petr Škoda
Browse files

MDL-35052 show all enrolled users in enrol UI included those enrolled via disabled plugins

parent d71c4865
...@@ -63,7 +63,7 @@ switch ($action) { ...@@ -63,7 +63,7 @@ switch ($action) {
case 'unenrol': case 'unenrol':
$ue = $DB->get_record('user_enrolments', array('id'=>required_param('ue', PARAM_INT)), '*', MUST_EXIST); $ue = $DB->get_record('user_enrolments', array('id'=>required_param('ue', PARAM_INT)), '*', MUST_EXIST);
list ($instance, $plugin) = $manager->get_user_enrolment_components($ue); list ($instance, $plugin) = $manager->get_user_enrolment_components($ue);
if (!$instance || !$plugin || !$plugin->allow_unenrol_user($instance, $ue) || !has_capability("enrol/$instance->enrol:unenrol", $manager->get_context()) || !$manager->unenrol_user($ue)) { if (!$instance || !$plugin || !enrol_is_enabled($instance->enrol) || !$plugin->allow_unenrol_user($instance, $ue) || !has_capability("enrol/$instance->enrol:unenrol", $manager->get_context()) || !$manager->unenrol_user($ue)) {
throw new enrol_ajax_exception('unenrolnotpermitted'); throw new enrol_ajax_exception('unenrolnotpermitted');
} }
break; break;
......
...@@ -97,6 +97,7 @@ class course_enrolment_manager { ...@@ -97,6 +97,7 @@ class course_enrolment_manager {
private $_instances = null; private $_instances = null;
private $_inames = null; private $_inames = null;
private $_plugins = null; private $_plugins = null;
private $_allplugins = null;
private $_roles = null; private $_roles = null;
private $_assignableroles = null; private $_assignableroles = null;
private $_assignablerolesothers = null; private $_assignablerolesothers = null;
...@@ -397,11 +398,13 @@ class course_enrolment_manager { ...@@ -397,11 +398,13 @@ class course_enrolment_manager {
/** /**
* Returns all of the enrolment instances for this course. * Returns all of the enrolment instances for this course.
* *
* NOTE: since 2.4 it includes instances of disabled plugins too.
*
* @return array * @return array
*/ */
public function get_enrolment_instances() { public function get_enrolment_instances() {
if ($this->_instances === null) { if ($this->_instances === null) {
$this->_instances = enrol_get_instances($this->course->id, true); $this->_instances = enrol_get_instances($this->course->id, false);
} }
return $this->_instances; return $this->_instances;
} }
...@@ -409,12 +412,14 @@ class course_enrolment_manager { ...@@ -409,12 +412,14 @@ class course_enrolment_manager {
/** /**
* Returns the names for all of the enrolment instances for this course. * Returns the names for all of the enrolment instances for this course.
* *
* NOTE: since 2.4 it includes instances of disabled plugins too.
*
* @return array * @return array
*/ */
public function get_enrolment_instance_names() { public function get_enrolment_instance_names() {
if ($this->_inames === null) { if ($this->_inames === null) {
$instances = $this->get_enrolment_instances(); $instances = $this->get_enrolment_instances();
$plugins = $this->get_enrolment_plugins(); $plugins = $this->get_enrolment_plugins(false);
foreach ($instances as $key=>$instance) { foreach ($instances as $key=>$instance) {
if (!isset($plugins[$instance->enrol])) { if (!isset($plugins[$instance->enrol])) {
// weird, some broken stuff in plugin // weird, some broken stuff in plugin
...@@ -430,13 +435,29 @@ class course_enrolment_manager { ...@@ -430,13 +435,29 @@ class course_enrolment_manager {
/** /**
* Gets all of the enrolment plugins that are active for this course. * Gets all of the enrolment plugins that are active for this course.
* *
* @param bool $onlyenabled return only enabled enrol plugins
* @return array * @return array
*/ */
public function get_enrolment_plugins() { public function get_enrolment_plugins($onlyenabled = true) {
if ($this->_plugins === null) { if ($this->_plugins === null) {
$this->_plugins = enrol_get_plugins(true); $this->_plugins = enrol_get_plugins(true);
} }
return $this->_plugins;
if ($onlyenabled) {
return $this->_plugins;
}
if ($this->_allplugins === null) {
// Make sure we have the same objects in _allplugins and _plugins.
$this->_allplugins = $this->_plugins;
foreach (enrol_get_plugins(false) as $name=>$plugin) {
if (!isset($this->_allplugins[$name])) {
$this->_allplugins[$name] = $plugin;
}
}
}
return $this->_allplugins;
} }
/** /**
...@@ -522,7 +543,7 @@ class course_enrolment_manager { ...@@ -522,7 +543,7 @@ class course_enrolment_manager {
$userenrolment = $DB->get_record('user_enrolments', array('id'=>(int)$userenrolment)); $userenrolment = $DB->get_record('user_enrolments', array('id'=>(int)$userenrolment));
} }
$instances = $this->get_enrolment_instances(); $instances = $this->get_enrolment_instances();
$plugins = $this->get_enrolment_plugins(); $plugins = $this->get_enrolment_plugins(false);
if (!$userenrolment || !isset($instances[$userenrolment->enrolid])) { if (!$userenrolment || !isset($instances[$userenrolment->enrolid])) {
return array(false, false); return array(false, false);
} }
...@@ -675,7 +696,7 @@ class course_enrolment_manager { ...@@ -675,7 +696,7 @@ class course_enrolment_manager {
} }
/** /**
* Gets the enrolments this user has in the course * Gets the enrolments this user has in the course - including all suspended plugins and instances.
* *
* @global moodle_database $DB * @global moodle_database $DB
* @param int $userid * @param int $userid
...@@ -687,7 +708,7 @@ class course_enrolment_manager { ...@@ -687,7 +708,7 @@ class course_enrolment_manager {
$params['userid'] = $userid; $params['userid'] = $userid;
$userenrolments = $DB->get_records_select('user_enrolments', "enrolid $instancessql AND userid = :userid", $params); $userenrolments = $DB->get_records_select('user_enrolments', "enrolid $instancessql AND userid = :userid", $params);
$instances = $this->get_enrolment_instances(); $instances = $this->get_enrolment_instances();
$plugins = $this->get_enrolment_plugins(); $plugins = $this->get_enrolment_plugins(false);
$inames = $this->get_enrolment_instance_names(); $inames = $this->get_enrolment_instance_names();
foreach ($userenrolments as &$ue) { foreach ($userenrolments as &$ue) {
$ue->enrolmentinstance = $instances[$ue->enrolid]; $ue->enrolmentinstance = $instances[$ue->enrolid];
...@@ -829,6 +850,8 @@ class course_enrolment_manager { ...@@ -829,6 +850,8 @@ class course_enrolment_manager {
$url = new moodle_url($pageurl, $this->get_url_params()); $url = new moodle_url($pageurl, $this->get_url_params());
$extrafields = get_extra_user_fields($context); $extrafields = get_extra_user_fields($context);
$enabledplugins = $this->get_enrolment_plugins(true);
$userdetails = array(); $userdetails = array();
foreach ($users as $user) { foreach ($users as $user) {
$details = $this->prepare_user_for_display($user, $extrafields, $now); $details = $this->prepare_user_for_display($user, $extrafields, $now);
...@@ -849,7 +872,15 @@ class course_enrolment_manager { ...@@ -849,7 +872,15 @@ class course_enrolment_manager {
// Enrolments // Enrolments
$details['enrolments'] = array(); $details['enrolments'] = array();
foreach ($this->get_user_enrolments($user->id) as $ue) { foreach ($this->get_user_enrolments($user->id) as $ue) {
if ($ue->timestart and $ue->timeend) { if (!isset($enabledplugins[$ue->enrolmentinstance->enrol])) {
$details['enrolments'][$ue->id] = array(
'text' => $ue->enrolmentinstancename,
'period' => null,
'dimmed' => true,
'actions' => array()
);
continue;
} else if ($ue->timestart and $ue->timeend) {
$period = get_string('periodstartend', 'enrol', array('start'=>userdate($ue->timestart), 'end'=>userdate($ue->timeend))); $period = get_string('periodstartend', 'enrol', array('start'=>userdate($ue->timestart), 'end'=>userdate($ue->timeend)));
$periodoutside = ($ue->timestart && $ue->timeend && $now < $ue->timestart && $now > $ue->timeend); $periodoutside = ($ue->timestart && $ue->timeend && $now < $ue->timestart && $now > $ue->timeend);
} else if ($ue->timestart) { } else if ($ue->timestart) {
...@@ -909,7 +940,7 @@ class course_enrolment_manager { ...@@ -909,7 +940,7 @@ class course_enrolment_manager {
} }
public function get_manual_enrol_buttons() { public function get_manual_enrol_buttons() {
$plugins = $this->get_enrolment_plugins(); $plugins = $this->get_enrolment_plugins(true); // Skip disabled plugins.
$buttons = array(); $buttons = array();
foreach ($plugins as $plugin) { foreach ($plugins as $plugin) {
$newbutton = $plugin->get_manual_enrol_button($this); $newbutton = $plugin->get_manual_enrol_button($this);
...@@ -941,7 +972,7 @@ class course_enrolment_manager { ...@@ -941,7 +972,7 @@ class course_enrolment_manager {
*/ */
public function get_filtered_enrolment_plugin() { public function get_filtered_enrolment_plugin() {
$instances = $this->get_enrolment_instances(); $instances = $this->get_enrolment_instances();
$plugins = $this->get_enrolment_plugins(); $plugins = $this->get_enrolment_plugins(false);
if (empty($this->instancefilter) || !array_key_exists($this->instancefilter, $instances)) { if (empty($this->instancefilter) || !array_key_exists($this->instancefilter, $instances)) {
return false; return false;
...@@ -965,7 +996,7 @@ class course_enrolment_manager { ...@@ -965,7 +996,7 @@ class course_enrolment_manager {
global $DB; global $DB;
$instances = $this->get_enrolment_instances(); $instances = $this->get_enrolment_instances();
$plugins = $this->get_enrolment_plugins(); $plugins = $this->get_enrolment_plugins(false);
if (!empty($this->instancefilter)) { if (!empty($this->instancefilter)) {
$instancesql = ' = :instanceid'; $instancesql = ' = :instanceid';
......
...@@ -113,11 +113,14 @@ switch ($action) { ...@@ -113,11 +113,14 @@ switch ($action) {
$user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST); $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
$instances = $manager->get_enrolment_instances(); $instances = $manager->get_enrolment_instances();
$plugins = $manager->get_enrolment_plugins(); $plugins = $manager->get_enrolment_plugins(true); // Do not allow actions on disabled plugins.
if (!array_key_exists($enrolid, $instances)) { if (!array_key_exists($enrolid, $instances)) {
throw new enrol_ajax_exception('invalidenrolinstance'); throw new enrol_ajax_exception('invalidenrolinstance');
} }
$instance = $instances[$enrolid]; $instance = $instances[$enrolid];
if (!isset($plugins[$instance->enrol])) {
throw new enrol_ajax_exception('enrolnotpermitted');
}
$plugin = $plugins[$instance->enrol]; $plugin = $plugins[$instance->enrol];
if ($plugin->allow_enrol($instance) && has_capability('enrol/'.$plugin->get_name().':enrol', $context)) { if ($plugin->allow_enrol($instance) && has_capability('enrol/'.$plugin->get_name().':enrol', $context)) {
$plugin->enrol_user($instance, $user->id, $roleid, $timestart, $timeend); $plugin->enrol_user($instance, $user->id, $roleid, $timestart, $timeend);
......
...@@ -449,7 +449,7 @@ class course_enrolment_table extends html_table implements renderable { ...@@ -449,7 +449,7 @@ class course_enrolment_table extends html_table implements renderable {
// Collect the bulk operations for the currently filtered plugin if there is one. // Collect the bulk operations for the currently filtered plugin if there is one.
$plugin = $manager->get_filtered_enrolment_plugin(); $plugin = $manager->get_filtered_enrolment_plugin();
if ($plugin) { if ($plugin and enrol_is_enabled($plugin->get_name())) {
$this->bulkoperations = $plugin->get_bulk_operations($manager); $this->bulkoperations = $plugin->get_bulk_operations($manager);
} }
} }
......
...@@ -54,6 +54,11 @@ if ($course->id == SITEID) { ...@@ -54,6 +54,11 @@ if ($course->id == SITEID) {
redirect(new moodle_url('/')); redirect(new moodle_url('/'));
} }
// Do not allow any changes if plugin disabled.
if (!enrol_is_enabled('self')) {
redirect(new moodle_url('/course/view.php', array('id'=>$course->id)));
}
// Obvioulsy // Obvioulsy
require_login($course); require_login($course);
// The user must be able to manage self enrolments within the course // The user must be able to manage self enrolments within the course
......
...@@ -8,6 +8,9 @@ required changes in code: ...@@ -8,6 +8,9 @@ required changes in code:
* use role_get_name() or role_fix_names() if you need any role names, using role.name * use role_get_name() or role_fix_names() if you need any role names, using role.name
directly from database is not correct any more directly from database is not correct any more
other changes:
* course enrolment manager now works with disabled plugins too
=== 2.2 === === 2.2 ===
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment