Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/enrol/ajax.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00029 define('AJAX_SCRIPT', true);
00030 
00031 require('../config.php');
00032 require_once("$CFG->dirroot/enrol/locallib.php");
00033 require_once("$CFG->dirroot/enrol/renderer.php");
00034 require_once("$CFG->dirroot/group/lib.php");
00035 
00036 // Must have the sesskey
00037 $id      = required_param('id', PARAM_INT); // course id
00038 $action  = required_param('action', PARAM_ACTION);
00039 
00040 $PAGE->set_url(new moodle_url('/enrol/ajax.php', array('id'=>$id, 'action'=>$action)));
00041 
00042 $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
00043 $context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
00044 
00045 if ($course->id == SITEID) {
00046     throw new moodle_exception('invalidcourse');
00047 }
00048 
00049 require_login($course);
00050 require_capability('moodle/course:enrolreview', $context);
00051 require_sesskey();
00052 
00053 echo $OUTPUT->header(); // send headers
00054 
00055 $manager = new course_enrolment_manager($PAGE, $course);
00056 
00057 $outcome = new stdClass;
00058 $outcome->success = true;
00059 $outcome->response = new stdClass;
00060 $outcome->error = '';
00061 
00062 switch ($action) {
00063     case 'unenrol':
00064         $ue = $DB->get_record('user_enrolments', array('id'=>required_param('ue', PARAM_INT)), '*', MUST_EXIST);
00065         list ($instance, $plugin) = $manager->get_user_enrolment_components($ue);
00066         if (!$instance || !$plugin || !$plugin->allow_unenrol_user($instance, $ue) || !has_capability("enrol/$instance->enrol:unenrol", $manager->get_context()) || !$manager->unenrol_user($ue)) {
00067             throw new enrol_ajax_exception('unenrolnotpermitted');
00068         }
00069         break;
00070     case 'unassign':
00071         $role = required_param('role', PARAM_INT);
00072         $user = required_param('user', PARAM_INT);
00073         if (!has_capability('moodle/role:assign', $manager->get_context()) || !$manager->unassign_role_from_user($user, $role)) {
00074             throw new enrol_ajax_exception('unassignnotpermitted');
00075         }
00076         break;
00077     case 'assign':
00078         $user = $DB->get_record('user', array('id'=>required_param('user', PARAM_INT)), '*', MUST_EXIST);
00079         $roleid = required_param('roleid', PARAM_INT);
00080         if (!array_key_exists($roleid, $manager->get_assignable_roles())) {
00081             throw new enrol_ajax_exception('invalidrole');
00082         }
00083         if (!has_capability('moodle/role:assign', $manager->get_context()) || !$manager->assign_role_to_user($roleid, $user->id)) {
00084             throw new enrol_ajax_exception('assignnotpermitted');
00085         }
00086         $outcome->response->roleid = $roleid;
00087         break;
00088     case 'getassignable':
00089         $otheruserroles = optional_param('otherusers', false, PARAM_BOOL);
00090         $outcome->response = array_reverse($manager->get_assignable_roles($otheruserroles), true);
00091         break;
00092     case 'searchotherusers':
00093         $search  = optional_param('search', '', PARAM_RAW);
00094         $page = optional_param('page', 0, PARAM_INT);
00095         $outcome->response = $manager->search_other_users($search, false, $page);
00096         foreach ($outcome->response['users'] as &$user) {
00097             $user->userId = $user->id;
00098             $user->picture = $OUTPUT->user_picture($user);
00099             $user->fullname = fullname($user);
00100             unset($user->id);
00101         }
00102         $outcome->success = true;
00103         break;
00104     default:
00105         throw new enrol_ajax_exception('unknowajaxaction');
00106 }
00107 
00108 echo json_encode($outcome);
00109 die();
 All Data Structures Namespaces Files Functions Variables Enumerations