|
Moodle
2.2.1
http://www.collinsharper.com
|
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/cohort/locallib.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/cohort/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 'getassignable': 00064 $otheruserroles = optional_param('otherusers', false, PARAM_BOOL); 00065 $outcome->response = array_reverse($manager->get_assignable_roles($otheruserroles), true); 00066 break; 00067 case 'getdefaultcohortrole': //TODO: use in ajax UI MDL-24280 00068 $cohortenrol = enrol_get_plugin('cohort'); 00069 $outcome->response = $cohortenrol->get_config('roleid'); 00070 break; 00071 case 'getcohorts': 00072 require_capability('moodle/course:enrolconfig', $context); 00073 $offset = optional_param('offset', 0, PARAM_INT); 00074 $search = optional_param('search', '', PARAM_RAW); 00075 $outcome->response = enrol_cohort_search_cohorts($manager, $offset, 25, $search); 00076 break; 00077 case 'enrolcohort': 00078 require_capability('moodle/course:enrolconfig', $context); 00079 require_capability('enrol/cohort:config', $context); 00080 $roleid = required_param('roleid', PARAM_INT); 00081 $cohortid = required_param('cohortid', PARAM_INT); 00082 00083 $roles = $manager->get_assignable_roles(); 00084 if (!enrol_cohort_can_view_cohort($cohortid) || !array_key_exists($roleid, $roles)) { 00085 throw new enrol_ajax_exception('errorenrolcohort'); 00086 } 00087 $enrol = enrol_get_plugin('cohort'); 00088 $enrol->add_instance($manager->get_course(), array('customint1' => $cohortid, 'roleid' => $roleid)); 00089 enrol_cohort_sync($manager->get_course()->id); 00090 break; 00091 case 'enrolcohortusers': 00092 require_capability('enrol/manual:enrol', $context); 00093 $roleid = required_param('roleid', PARAM_INT); 00094 $cohortid = required_param('cohortid', PARAM_INT); 00095 $result = enrol_cohort_enrol_all_users($manager, $cohortid, $roleid); 00096 00097 $roles = $manager->get_assignable_roles(); 00098 if (!enrol_cohort_can_view_cohort($cohortid) || !array_key_exists($roleid, $roles)) { 00099 throw new enrol_ajax_exception('errorenrolcohort'); 00100 } 00101 if ($result === false) { 00102 throw new enrol_ajax_exception('errorenrolcohortusers'); 00103 } 00104 $outcome->success = true; 00105 $outcome->response->users = $result; 00106 $outcome->response->title = get_string('success'); 00107 $outcome->response->message = get_string('enrollednewusers', 'enrol', $result); 00108 $outcome->response->yesLabel = get_string('ok'); 00109 break; 00110 default: 00111 throw new enrol_ajax_exception('unknowajaxaction'); 00112 } 00113 00114 echo json_encode($outcome); 00115 die();