/** * Makes sure user may execute functions in this context. * @param object $context * @return void */ protected static function validate_context($context) { global $CFG; if (empty($context)) { throw new invalid_parameter_exception('Context does not exist'); } if (empty(self::$contextrestriction)) { self::$contextrestriction = get_context_instance(CONTEXT_SYSTEM); } $rcontext = self::$contextrestriction; if ($rcontext->contextlevel == $context->contextlevel) { if ($rcontext->id != $context->id) { throw new restricted_context_exception(); } } else { if ($rcontext->contextlevel > $context->contextlevel) { throw new restricted_context_exception(); } else { $parents = get_parent_contexts($context); if (!in_array($rcontext->id, $parents)) { throw new restricted_context_exception(); } } } if ($context->contextlevel >= CONTEXT_COURSE) { list($context, $course, $cm) = get_context_info_array($context->id); require_login($course, false, $cm, false, true); } }
/** * Makes sure user may execute functions in this context. * * @param stdClass $context * @since Moodle 2.0 */ public static function validate_context($context) { global $CFG, $PAGE; if (empty($context)) { throw new invalid_parameter_exception('Context does not exist'); } if (empty(self::$contextrestriction)) { self::$contextrestriction = context_system::instance(); } $rcontext = self::$contextrestriction; if ($rcontext->contextlevel == $context->contextlevel) { if ($rcontext->id != $context->id) { throw new restricted_context_exception(); } } else { if ($rcontext->contextlevel > $context->contextlevel) { throw new restricted_context_exception(); } else { $parents = $context->get_parent_context_ids(); if (!in_array($rcontext->id, $parents)) { throw new restricted_context_exception(); } } } $PAGE->reset_theme_and_output(); list($unused, $course, $cm) = get_context_info_array($context->id); require_login($course, false, $cm, false, true); $PAGE->set_context($context); }
/** * Set context restriction for all following subsequent function calls. * @param stdClass $contex * @return void */ public static function set_context_restriction($context) { self::$contextrestriction = $context; }