get_context_name()})"); } } /** * Find and validate a given context by id * @param int $contextid The id of the context * @return \context The found context by id * @throws \InvalidArgumentException When the context is not found */ public static function find_context($contextid): \context{ if(isset($contextid) && is_int($contextid) && $contextid > 0){ if(!in_array($contextid,self::$validated_contexts)){ // Cache the context and make sure it is only validated once... try{ $context = \context::instance_by_id($contextid); } catch(\dml_missing_record_exception $x){ throw new \InvalidArgumentException("Context {$contextid} not available"); // Just throw it up again. catch is included here to make sure we know it throws this exception } // Validate the found context \external_api::validate_context($context); self::$validated_contexts[$contextid] = $context; } return self::$validated_contexts[$contextid]; } else{ return static::system_context(); // This function ensures the system context is validated just once this call } } /** * Return the validated system context (validation happens only once for this call) * @return \context_system The system context, validated to use as this context */ public static function system_context(): \context_system { if(!isset(static::$systemcontext)){ static::$systemcontext = \context_system::instance(); \external_api::validate_context(static::$systemcontext); } return static::$systemcontext; } }