moodle_local_treestudyplan/classes/contextinfo.php

70 lines
2.5 KiB
PHP
Raw Normal View History

<?php
2023-08-24 23:02:41 +02:00
// This file is part of the Studyplan plugin for Moodle
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
/**
*
* @package local_treestudyplan
* @copyright 2023 P.M. Kuipers
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace local_treestudyplan;
class contextinfo {
public $context;
2023-08-24 23:02:41 +02:00
public function __construct($context) {
$this->context = $context;
}
2023-08-25 13:04:19 +02:00
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() {
2023-08-24 23:02:41 +02:00
2023-08-25 13:34:31 +02:00
$ctxpath = array_reverse($this->context->get_parent_context_ids(true));
if (count($ctxpath) > 1 && $ctxpath[0] == 1) {
array_shift($ctxpath);
}
return [
2023-08-24 23:02:41 +02:00
"name" => $this->context->get_context_name(false, false),
"shortname" => $this->context->get_context_name(false, true),
2023-08-25 13:34:31 +02:00
"path" => array_map(function($c) {
return \context::instance_by_id($c)->get_context_name(false, false);
}, $ctxpath),
2023-08-25 11:52:05 +02:00
"shortpath" => array_map(function($c) {
2023-08-25 13:34:31 +02:00
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 {
2023-08-24 23:02:41 +02:00
if ($contextid <= 1) {
$contextid = 1;
}
return \context::instance_by_id($contextid);
}
2023-08-25 11:52:05 +02:00
}