Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/enrol/manual/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.'/group/lib.php');
00034 
00035 // Must have the sesskey
00036 $id      = required_param('id', PARAM_INT); // course id
00037 $action  = required_param('action', PARAM_ACTION);
00038 
00039 $PAGE->set_url(new moodle_url('/enrol/ajax.php', array('id'=>$id, 'action'=>$action)));
00040 
00041 $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST);
00042 $context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST);
00043 
00044 if ($course->id == SITEID) {
00045     throw new moodle_exception('invalidcourse');
00046 }
00047 
00048 require_login($course);
00049 require_capability('moodle/course:enrolreview', $context);
00050 require_sesskey();
00051 
00052 echo $OUTPUT->header(); // send headers
00053 
00054 $manager = new course_enrolment_manager($PAGE, $course);
00055 
00056 $outcome = new stdClass;
00057 $outcome->success = true;
00058 $outcome->response = new stdClass;
00059 $outcome->error = '';
00060 
00061 switch ($action) {
00062     case 'getassignable':
00063         $otheruserroles = optional_param('otherusers', false, PARAM_BOOL);
00064         $outcome->response = array_reverse($manager->get_assignable_roles($otheruserroles), true);
00065         break;
00066     case 'searchusers':
00067         $enrolid = required_param('enrolid', PARAM_INT);
00068         $search  = optional_param('search', '', PARAM_RAW);
00069         $page = optional_param('page', 0, PARAM_INT);
00070         $outcome->response = $manager->get_potential_users($enrolid, $search, true, $page);
00071         $extrafields = get_extra_user_fields($context);
00072         foreach ($outcome->response['users'] as &$user) {
00073             $user->picture = $OUTPUT->user_picture($user);
00074             $user->fullname = fullname($user);
00075             $fieldvalues = array();
00076             foreach ($extrafields as $field) {
00077                 $fieldvalues[] = s($user->{$field});
00078                 unset($user->{$field});
00079             }
00080             $user->extrafields = implode(', ', $fieldvalues);
00081         }
00082         $outcome->success = true;
00083         break;
00084     case 'enrol':
00085         $enrolid = required_param('enrolid', PARAM_INT);
00086         $userid = required_param('userid', PARAM_INT);
00087 
00088         $roleid = optional_param('role', null, PARAM_INT);
00089         $duration = optional_param('duration', 0, PARAM_INT);
00090         $startdate = optional_param('startdate', 0, PARAM_INT);
00091         $recovergrades = optional_param('recovergrades', 0, PARAM_INT);
00092 
00093         if (empty($roleid)) {
00094             $roleid = null;
00095         }
00096 
00097         switch($startdate) {
00098             case 2:
00099                 $timestart = $course->startdate;
00100                 break;
00101             case 3:
00102             default:
00103                 $today = time();
00104                 $today = make_timestamp(date('Y', $today), date('m', $today), date('d', $today), 0, 0, 0);
00105                 $timestart = $today;
00106                 break;
00107         }
00108         if ($duration <= 0) {
00109             $timeend = 0;
00110         } else {
00111             $timeend = $timestart + ($duration*24*60*60);
00112         }
00113 
00114         $user = $DB->get_record('user', array('id'=>$userid), '*', MUST_EXIST);
00115         $instances = $manager->get_enrolment_instances();
00116         $plugins = $manager->get_enrolment_plugins();
00117         if (!array_key_exists($enrolid, $instances)) {
00118             throw new enrol_ajax_exception('invalidenrolinstance');
00119         }
00120         $instance = $instances[$enrolid];
00121         $plugin = $plugins[$instance->enrol];
00122         if ($plugin->allow_enrol($instance) && has_capability('enrol/'.$plugin->get_name().':enrol', $context)) {
00123             $plugin->enrol_user($instance, $user->id, $roleid, $timestart, $timeend);
00124             if ($recovergrades) {
00125                 require_once($CFG->libdir.'/gradelib.php');
00126                 grade_recover_history_grades($user->id, $instance->courseid);
00127             }
00128         } else {
00129             throw new enrol_ajax_exception('enrolnotpermitted');
00130         }
00131         $outcome->success = true;
00132         break;
00133 
00134     default:
00135         throw new enrol_ajax_exception('unknowajaxaction');
00136 }
00137 
00138 echo json_encode($outcome);
 All Data Structures Namespaces Files Functions Variables Enumerations