moodle_local_treestudyplan/classes/contextinfo.php
2023-05-17 21:19:14 +02:00

49 lines
No EOL
1.6 KiB
PHP

<?php
namespace local_treestudyplan;
class contextinfo {
public $context;
public function __construct($context){
$this->context = $context;
}
public static function structure($value=VALUE_REQUIRED){
return new \external_single_structure([
"name" => new \external_value(PARAM_TEXT, 'context name'),
"shortname" => new \external_value(PARAM_TEXT, 'context short name'),
"path" => new \external_multiple_structure( new \external_value(PARAM_TEXT)),
"shortpath" => new \external_multiple_structure( new \external_value(PARAM_TEXT)),
], 'context information', $value);
}
public function model() {
$ctxPath = array_reverse($this->context->get_parent_context_ids(true));
if(count($ctxPath) > 1 && $ctxPath[0] == 1) {
array_shift($ctxPath);
}
return [
"name" => $this->context->get_context_name(false,false),
"shortname" => $this->context->get_context_name(false,true),
"path" => array_map(function($c){ return \context::instance_by_id($c)->get_context_name(false,false);},$ctxPath),
"shortpath" => array_map(function($c){ return \context::instance_by_id($c)->get_context_name(false,true);},$ctxPath),
];
}
public static function by_id($contextid): self {
return new self(self::context_by_id($contextid));
}
public static function context_by_id($contextid): \context {
if($contextid <= 1){
$contextid = 1;
}
return \context::instance_by_id($contextid);
}
}