Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/course/switchrole.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00033 require_once('../config.php');
00034 require_once($CFG->dirroot.'/course/lib.php');
00035 
00036 $id         = required_param('id', PARAM_INT);
00037 $switchrole = optional_param('switchrole',-1, PARAM_INT);
00038 $returnurl  = optional_param('returnurl', false, PARAM_URL);
00039 
00040 $PAGE->set_url('/course/switchrole.php', array('id'=>$id));
00041 
00042 if (!confirm_sesskey()) {
00043     print_error('confirmsesskeybad', 'error');
00044 }
00045 
00046 if (! ($course = $DB->get_record('course', array('id'=>$id)))) {
00047     print_error('invalidcourseid', 'error');
00048 }
00049 
00050 if (!$context = get_context_instance(CONTEXT_COURSE, $course->id)) {
00051     print_error('nocontext');
00052 }
00053 
00054 // Remove any switched roles before checking login
00055 if ($switchrole == 0) {
00056     role_switch($switchrole, $context);
00057 }
00058 require_login($course);
00059 
00060 // Switchrole - sanity check in cost-order...
00061 if ($switchrole > 0 && has_capability('moodle/role:switchroles', $context)) {
00062     // is this role assignable in this context?
00063     // inquiring minds want to know...
00064     $aroles = get_switchable_roles($context);
00065     if (is_array($aroles) && isset($aroles[$switchrole])) {
00066         role_switch($switchrole, $context);
00067         // Double check that this role is allowed here
00068         require_login($course);
00069     }
00070 }
00071 
00072 // TODO: Using SESSION->returnurl is deprecated and should be removed in the future.
00073 // Till then this code remains to support any external applications calling this script.
00074 if (!empty($returnurl) && is_numeric($returnurl)) {
00075     $returnurl = false;
00076     if (!empty($SESSION->returnurl) && strpos($SESSION->returnurl, 'moodle_url')!==false) {
00077         debugging('Code calling switchrole should be passing a URL as a param.', DEBUG_DEVELOPER);
00078         $returnurl = @unserialize($SESSION->returnurl);
00079         if (!($returnurl instanceof moodle_url)) {
00080             $returnurl = false;
00081         }
00082     }
00083 }
00084 
00085 if ($returnurl === false) {
00086     $returnurl = new moodle_url('/course/view.php', array('id' => $course->id));
00087 }
00088 
00089 redirect($returnurl);
 All Data Structures Namespaces Files Functions Variables Enumerations