/** * Get the potential assignees selector for a given context. * * If this context is a course context, or inside a course context (module or * some blocks) then return a core_role_potential_assignees_below_course object. Otherwise * return a core_role_potential_assignees_course_and_above. * * @param context $context a context. * @param string $name passed to user selector constructor. * @param array $options to user selector constructor. * @return user_selector_base an appropriate user selector. */ function core_role_get_potential_user_selector(context $context, $name, $options) { $blockinsidecourse = false; if ($context->contextlevel == CONTEXT_BLOCK) { $parentcontext = $context->get_parent_context(); $blockinsidecourse = in_array($parentcontext->contextlevel, array(CONTEXT_MODULE, CONTEXT_COURSE)); } if (($context->contextlevel == CONTEXT_MODULE || $blockinsidecourse) && !is_inside_frontpage($context)) { $potentialuserselector = new core_role_potential_assignees_below_course('addselect', $options); } else { $potentialuserselector = new core_role_potential_assignees_course_and_above('addselect', $options); } return $potentialuserselector; }
/** * Return the id of the parent of this context, or false if there is no parent (only happens if this * is the site context.) * * @deprecated since 2.2, use $context->get_parent_context() instead * @param context $context * @return integer the id of the parent context. */ function get_parent_contextid(context $context) { if ($parent = $context->get_parent_context()) { return $parent->id; } else { return false; } }
/** * Constructor * @param context $context The context used for the capability check * @param string $capability The required capability * @param string $errormessage The error message to show the user * @param string $stringfile */ function __construct($context, $capability, $errormessage, $stringfile) { $capabilityname = get_capability_string($capability); if ($context->contextlevel == CONTEXT_MODULE and preg_match('/:view$/', $capability)) { // we can not go to mod/xx/view.php because we most probably do not have cap to view it, let's go to course instead $parentcontext = $context->get_parent_context(); $link = $parentcontext->get_url(); } else { $link = $context->get_url(); } parent::__construct($errormessage, $stringfile, $link, $capabilityname); }
/** * Set the main context to which this page belongs. * * @param context $context a context object. You normally get this with context_xxxx::instance(). */ public function set_context($context) { if ($context === null) { // Extremely ugly hack which sets context to some value in order to prevent warnings, // use only for core error handling!!!! if (!$this->_context) { $this->_context = context_system::instance(); } return; } // Ideally we should set context only once. if (isset($this->_context) && $context->id !== $this->_context->id) { $current = $this->_context->contextlevel; if ($current == CONTEXT_SYSTEM or $current == CONTEXT_COURSE) { // Hmm - not ideal, but it might produce too many warnings due to the design of require_login. } else { if ($current == CONTEXT_MODULE and $parentcontext = $context->get_parent_context() and $this->_context->id == $parentcontext->id) { // Hmm - most probably somebody did require_login() and after that set the block context. } else { // We do not want devs to do weird switching of context levels on the fly because we might have used // the context already such as in text filter in page title. // This is explicitly allowed for webservices though which may // call "external_api::validate_context on many contexts in a single request. if (!WS_SERVER) { debugging("Coding problem: unsupported modification of PAGE->context from {$current} to {$context->contextlevel}"); } } } } $this->_context = $context; }
/** * Return the id of the parent of this context, or false if there is no parent (only happens if this * is the site context.) * * @deprecated since Moodle 2.2 * @see context::get_parent_context() * @param context $context * @return integer the id of the parent context. */ function get_parent_contextid(context $context) { debugging('get_parent_contextid() is deprecated, please use $context->get_parent_context() instead.', DEBUG_DEVELOPER); if ($parent = $context->get_parent_context()) { return $parent->id; } else { return false; } }