MDL-23532 enrol - separated AJAX functions from enrol/ajax.php to their respective enrol plugins

This commit is contained in:
Sam Hemelryk
2011-04-21 10:02:54 +08:00
parent b69ca6bee4
commit cf855c0b7a
7 changed files with 323 additions and 205 deletions
-96
View File
@@ -34,7 +34,6 @@ require_once("$CFG->dirroot/enrol/renderer.php");
require_once("$CFG->dirroot/group/lib.php");
// Must have the sesskey
require_sesskey();
$id = required_param('id', PARAM_INT); // course id
$action = required_param('action', PARAM_ACTION);
@@ -75,7 +74,6 @@ switch ($action) {
throw new enrol_ajax_exception('unassignnotpermitted');
}
break;
case 'assign':
$user = $DB->get_record('user', array('id'=>required_param('user', PARAM_INT)), '*', MUST_EXIST);
$roleid = required_param('roleid', PARAM_INT);
@@ -87,53 +85,10 @@ switch ($action) {
}
$outcome->response->roleid = $roleid;
break;
case 'getassignable':
$otheruserroles = optional_param('otherusers', false, PARAM_BOOL);
$outcome->response = array_reverse($manager->get_assignable_roles($otheruserroles), true);
break;
case 'getdefaultcohortrole': //TODO: use in ajax UI MDL-24280
$cohortenrol = enrol_get_plugin('cohort');
$outcome->response = $cohortenrol->get_config('roleid');
break;
case 'getcohorts':
require_capability('moodle/course:enrolconfig', $context);
$outcome->response = $manager->get_cohorts();
break;
case 'enrolcohort':
require_capability('moodle/course:enrolconfig', $context);
$roleid = required_param('roleid', PARAM_INT);
$cohortid = required_param('cohortid', PARAM_INT);
if (!$manager->enrol_cohort($cohortid, $roleid)) {
throw new enrol_ajax_exception('errorenrolcohort');
}
break;
case 'enrolcohortusers':
require_capability('moodle/course:enrolconfig', $context);
$roleid = required_param('roleid', PARAM_INT);
$cohortid = required_param('cohortid', PARAM_INT);
$result = $manager->enrol_cohort_users($cohortid, $roleid);
if ($result === false) {
throw new enrol_ajax_exception('errorenrolcohortusers');
}
$outcome->success = true;
$outcome->response->users = $result;
$outcome->response->title = get_string('success');
$outcome->response->message = get_string('enrollednewusers', 'enrol', $result);
$outcome->response->yesLabel = get_string('ok');
break;
case 'searchusers':
$enrolid = required_param('enrolid', PARAM_INT);
$search = optional_param('search', '', PARAM_RAW);
$page = optional_param('page', 0, PARAM_INT);
$outcome->response = $manager->get_potential_users($enrolid, $search, true, $page);
foreach ($outcome->response['users'] as &$user) {
$user->picture = $OUTPUT->user_picture($user);
$user->fullname = fullname($user);
}
$outcome->success = true;
break;
case 'searchotherusers':
$search = optional_param('search', '', PARAM_RAW);
$page = optional_param('page', 0, PARAM_INT);
@@ -146,57 +101,6 @@ switch ($action) {
}
$outcome->success = true;
break;
case 'enrol':
$enrolid = required_param('enrolid', PARAM_INT);
$userid = required_param('userid', PARAM_INT);
$roleid = optional_param('role', null, PARAM_INT);
$duration = optional_param('duration', 0, PARAM_INT);
$startdate = optional_param('startdate', 0, PARAM_INT);
$recovergrades = optional_param('recovergrades', 0, PARAM_INT);
if (empty($roleid)) {
$roleid = null;
}
switch($startdate) {
case 2:
$timestart = $course->startdate;
break;
case 3:
default:
$today = time();
$today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);
$timestart = $today;
break;
}
if ($duration <= 0) {
$timeend = 0;
} else {
$timeend = $timestart + ($duration*24*60*60);
}
$user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
$instances = $manager->get_enrolment_instances();
$plugins = $manager->get_enrolment_plugins();
if (!array_key_exists($enrolid, $instances)) {
throw new enrol_ajax_exception('invalidenrolinstance');
}
$instance = $instances[$enrolid];
$plugin = $plugins[$instance->enrol];
if ($plugin->allow_enrol($instance) && has_capability('enrol/'.$plugin->get_name().':enrol', $context)) {
$plugin->enrol_user($instance, $user->id, $roleid, $timestart, $timeend);
if ($recovergrades) {
require_once($CFG->libdir.'/gradelib.php');
grade_recover_history_grades($user->id, $instance->courseid);
}
} else {
throw new enrol_ajax_exception('enrolnotpermitted');
}
$outcome->success = true;
break;
default:
throw new enrol_ajax_exception('unknowajaxaction');
}
+108
View File
@@ -0,0 +1,108 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file processes AJAX enrolment actions and returns JSON for the cohort plugin
*
* 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
* @copyright 2011 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('AJAX_SCRIPT', true);
require('../../config.php');
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
$id = required_param('id', PARAM_INT); // course id
$action = required_param('action', PARAM_ACTION);
$PAGE->set_url(new moodle_url('/enrol/cohort/ajax.php', array('id'=>$id, 'action'=>$action)));
$course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
$context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
if ($course->id == SITEID) {
throw new moodle_exception('invalidcourse');
}
require_login($course);
require_capability('moodle/course:enrolreview', $context);
require_sesskey();
echo $OUTPUT->header(); // send headers
$manager = new course_enrolment_manager($course);
$outcome = new stdClass;
$outcome->success = true;
$outcome->response = new stdClass;
$outcome->error = '';
switch ($action) {
case 'getassignable':
$otheruserroles = optional_param('otherusers', false, PARAM_BOOL);
$outcome->response = array_reverse($manager->get_assignable_roles($otheruserroles), true);
break;
case 'getdefaultcohortrole': //TODO: use in ajax UI MDL-24280
$cohortenrol = enrol_get_plugin('cohort');
$outcome->response = $cohortenrol->get_config('roleid');
break;
case 'getcohorts':
require_capability('moodle/course:enrolconfig', $context);
$outcome->response = enrol_cohort_get_cohorts($manager);
break;
case 'enrolcohort':
require_capability('moodle/course:enrolconfig', $context);
$roleid = required_param('roleid', PARAM_INT);
$cohortid = required_param('cohortid', PARAM_INT);
$roles = $manager->get_assignable_roles();
$cohorts = enrol_cohort_get_cohorts($manager);
if (!array_key_exists($cohortid, $cohorts) || !array_key_exists($roleid, $roles)) {
throw new enrol_ajax_exception('errorenrolcohort');
}
$enrol = enrol_get_plugin('cohort');
$enrol->add_instance($manager->get_course(), array('customint1' => $cohortid, 'roleid' => $roleid));
enrol_cohort_sync($manager->get_course()->id);
break;
case 'enrolcohortusers':
require_capability('moodle/course:enrolconfig', $context);
$roleid = required_param('roleid', PARAM_INT);
$cohortid = required_param('cohortid', PARAM_INT);
$result = enrol_cohort_enrol_all_users($manager, $cohortid, $roleid);
if ($result === false) {
throw new enrol_ajax_exception('errorenrolcohortusers');
}
$outcome->success = true;
$outcome->response->users = $result;
$outcome->response->title = get_string('success');
$outcome->response->message = get_string('enrollednewusers', 'enrol', $result);
$outcome->response->yesLabel = get_string('ok');
break;
default:
throw new enrol_ajax_exception('unknowajaxaction');
}
echo json_encode($outcome);
die();
+1 -1
View File
@@ -172,7 +172,7 @@ class enrol_cohort_plugin extends enrol_plugin {
$function = 'M.enrol_cohort.quickenrolment.init';
$arguments = array(
'courseid' => $course->id,
'ajaxurl' => '/enrol/ajax.php',
'ajaxurl' => '/enrol/cohort/ajax.php',
'url' => $PAGE->url->out(false),
'manualEnrolment' => $hasmanualinstance);
$button->require_yui_module($modules, $function, array($arguments));
+87
View File
@@ -188,3 +188,90 @@ function enrol_cohort_sync($courseid = NULL) {
$rs->close();
}
/**
* Enrols all of the users in a cohort through a manual plugin instance.
*
* In order for this to succeed the course must contain a valid manual
* enrolment plugin instance that the user has permission to enrol users through.
*
* @global moodle_database $DB
* @param course_enrolment_manager $manager
* @param int $cohortid
* @param int $roleid
* @return int
*/
function enrol_cohort_enrol_all_users(course_enrolment_manager $manager, $cohortid, $roleid) {
global $DB;
$context = $manager->get_context();
require_capability('moodle/course:enrolconfig', $context);
$instance = false;
$instances = $manager->get_enrolment_instances();
foreach ($instances as $i) {
if ($i->enrol == 'manual') {
$instance = $i;
break;
}
}
$plugin = enrol_get_plugin('manual');
if (!$instance || !$plugin || !$plugin->allow_enrol($instance) || !has_capability('enrol/'.$plugin->get_name().':enrol', $context)) {
return false;
}
$sql = "SELECT com.userid
FROM {cohort_members} com
LEFT JOIN (
SELECT *
FROM {user_enrolments} ue
WHERE ue.enrolid = :enrolid
) ue ON ue.userid=com.userid
WHERE com.cohortid = :cohortid AND ue.id IS NULL";
$params = array('cohortid' => $cohortid, 'enrolid' => $instance->id);
$rs = $DB->get_recordset_sql($sql, $params);
$count = 0;
foreach ($rs as $user) {
$count++;
$plugin->enrol_user($instance, $user->userid, $roleid);
}
$rs->close();
return $count;
}
/**
* Gets all the cohorts the user is able to view.
*
* @global moodle_database $DB
* @return array
*/
function enrol_cohort_get_cohorts(course_enrolment_manager $manager) {
global $DB;
$context = $manager->get_context();
$cohorts = array();
$instances = $manager->get_enrolment_instances();
$enrolled = array();
foreach ($instances as $instance) {
if ($instance->enrol == 'cohort') {
$enrolled[] = $instance->customint1;
}
}
list($sqlparents, $params) = $DB->get_in_or_equal(get_parent_contexts($context));
$sql = "SELECT id, name, contextid
FROM {cohort}
WHERE contextid $sqlparents
ORDER BY name ASC";
$rs = $DB->get_recordset_sql($sql, $params);
foreach ($rs as $c) {
$context = get_context_instance_by_id($c->contextid);
if (!has_capability('moodle/cohort:view', $context)) {
continue;
}
$cohorts[$c->id] = array(
'cohortid'=>$c->id,
'name'=>format_string($c->name),
'users'=>$DB->count_records('cohort_members', array('cohortid'=>$c->id)),
'enrolled'=>in_array($c->id, $enrolled)
);
}
$rs->close();
return $cohorts;
}
-107
View File
@@ -789,113 +789,6 @@ class course_enrolment_manager {
return $users;
}
/**
* Gets all the cohorts the user is able to view.
*
* @global moodle_database $DB
* @return array
*/
public function get_cohorts() {
global $DB;
$context = $this->get_context();
$cohorts = array();
$instances = $this->get_enrolment_instances();
$enrolled = array();
foreach ($instances as $instance) {
if ($instance->enrol == 'cohort') {
$enrolled[] = $instance->customint1;
}
}
list($sqlparents, $params) = $DB->get_in_or_equal(get_parent_contexts($context));
$sql = "SELECT id, name, contextid
FROM {cohort}
WHERE contextid $sqlparents
ORDER BY name ASC";
$rs = $DB->get_recordset_sql($sql, $params);
foreach ($rs as $c) {
$context = get_context_instance_by_id($c->contextid);
if (!has_capability('moodle/cohort:view', $context)) {
continue;
}
$cohorts[$c->id] = array(
'cohortid'=>$c->id,
'name'=>format_string($c->name),
'users'=>$DB->count_records('cohort_members', array('cohortid'=>$c->id)),
'enrolled'=>in_array($c->id, $enrolled)
);
}
$rs->close();
return $cohorts;
}
/**
* Enrols a cohort in a course.
*
* Essentially this just adds a cohort enrolment plugin instance to the course
*
* @param int $cohortid
* @param int $roleid
* @return bool
*/
public function enrol_cohort($cohortid, $roleid) {
global $CFG;
require_capability('moodle/course:enrolconfig', $this->get_context());
require_once($CFG->dirroot.'/enrol/cohort/locallib.php');
$roles = $this->get_assignable_roles();
$cohorts = $this->get_cohorts();
if (!array_key_exists($cohortid, $cohorts) || !array_key_exists($roleid, $roles)) {
return false;
}
$enrol = enrol_get_plugin('cohort');
$enrol->add_instance($this->course, array('customint1'=>$cohortid, 'roleid'=>$roleid));
enrol_cohort_sync($this->course->id);
return true;
}
/**
* Enrols all of the users in a cohort within this course.
*
* Note this is VERY different from creating an enrolment instance for a cohort.
*
* @global moodle_database $DB
* @param int $cohortid
* @param int $roleid
* @return bool
*/
public function enrol_cohort_users($cohortid, $roleid) {
global $DB;
require_capability('moodle/course:enrolconfig', $this->get_context());
$instance = false;
$instances = $this->get_enrolment_instances();
foreach ($instances as $i) {
if ($i->enrol == 'manual') {
$instance = $i;
break;
}
}
$plugin = enrol_get_plugin('manual');
if (!$instance || !$plugin || !$plugin->allow_enrol($instance) || !has_capability('enrol/'.$plugin->get_name().':enrol', $this->get_context())) {
return false;
}
$sql = "SELECT com.userid
FROM {cohort_members} com
LEFT JOIN (
SELECT *
FROM {user_enrolments} ue
WHERE ue.enrolid = :enrolid
) ue ON ue.userid=com.userid
WHERE com.cohortid = :cohortid AND ue.id IS NULL";
$params = array('cohortid'=>$cohortid, 'enrolid'=>$instance->id);
$rs = $DB->get_recordset_sql($sql, $params);
$count = 0;
foreach ($rs as $user) {
$count++;
$plugin->enrol_user($instance, $user->userid, $roleid);
}
$rs->close();
return $count;
}
/**
* Gets an array of users for display, this includes minimal user information
* as well as minimal information on the users roles, groups, and enrolments.
+126
View File
@@ -0,0 +1,126 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file processes AJAX enrolment actions and returns JSON for the manual enrolments plugin
*
* 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 manual
* @copyright 2010 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('AJAX_SCRIPT', true);
require('../../config.php');
require_once($CFG->dirroot.'/enrol/locallib.php');
require_once($CFG->dirroot.'/group/lib.php');
// Must have the sesskey
$id = required_param('id', PARAM_INT); // course id
$action = required_param('action', PARAM_ACTION);
$PAGE->set_url(new moodle_url('/enrol/ajax.php', array('id'=>$id, 'action'=>$action)));
$course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
$context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
if ($course->id == SITEID) {
throw new moodle_exception('invalidcourse');
}
require_login($course);
require_capability('moodle/course:enrolreview', $context);
require_sesskey();
echo $OUTPUT->header(); // send headers
$manager = new course_enrolment_manager($course);
$outcome = new stdClass;
$outcome->success = true;
$outcome->response = new stdClass;
$outcome->error = '';
switch ($action) {
case 'getassignable':
$otheruserroles = optional_param('otherusers', false, PARAM_BOOL);
$outcome->response = array_reverse($manager->get_assignable_roles($otheruserroles), true);
break;
case 'searchusers':
$enrolid = required_param('enrolid', PARAM_INT);
$search = optional_param('search', '', PARAM_RAW);
$page = optional_param('page', 0, PARAM_INT);
$outcome->response = $manager->get_potential_users($enrolid, $search, true, $page);
foreach ($outcome->response['users'] as &$user) {
$user->picture = $OUTPUT->user_picture($user);
$user->fullname = fullname($user);
}
$outcome->success = true;
break;
case 'enrol':
$enrolid = required_param('enrolid', PARAM_INT);
$userid = required_param('userid', PARAM_INT);
$roleid = optional_param('role', null, PARAM_INT);
$duration = optional_param('duration', 0, PARAM_INT);
$startdate = optional_param('startdate', 0, PARAM_INT);
if (empty($roleid)) {
$roleid = null;
}
switch($startdate) {
case 2:
$timestart = $course->startdate;
break;
case 3:
default:
$today = time();
$today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);
$timestart = $today;
break;
}
if ($duration <= 0) {
$timeend = 0;
} else {
$timeend = $timestart + ($duration*24*60*60);
}
$user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
$instances = $manager->get_enrolment_instances();
$plugins = $manager->get_enrolment_plugins();
if (!array_key_exists($enrolid, $instances)) {
throw new enrol_ajax_exception('invalidenrolinstance');
}
$instance = $instances[$enrolid];
$plugin = $plugins[$instance->enrol];
if ($plugin->allow_enrol($instance) && has_capability('enrol/'.$plugin->get_name().':enrol', $context)) {
$plugin->enrol_user($instance, $user->id, $roleid, $timestart, $timeend);
} else {
throw new enrol_ajax_exception('enrolnotpermitted');
}
$outcome->success = true;
break;
default:
throw new enrol_ajax_exception('unknowajaxaction');
}
echo json_encode($outcome);
die();
+1 -1
View File
@@ -216,7 +216,7 @@ class enrol_manual_plugin extends enrol_plugin {
$arguments = array(
'instances' => $instances,
'courseid' => $instance->courseid,
'ajaxurl' => '/enrol/ajax.php',
'ajaxurl' => '/enrol/manual/ajax.php',
'url' => $PAGE->url->out(false),
'optionsStartDate' => $startdateoptions,
'defaultRole' => $instance->roleid