2023-05-17 21:19:14 +02:00
|
|
|
<?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/>.
|
|
|
|
/**
|
2023-08-27 17:00:23 +02:00
|
|
|
* Class to abstract information about (category) context for web service export
|
2023-08-24 23:02:41 +02:00
|
|
|
* @package local_treestudyplan
|
|
|
|
* @copyright 2023 P.M. Kuipers
|
|
|
|
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2023-05-17 21:19:14 +02:00
|
|
|
|
|
|
|
namespace local_treestudyplan;
|
2024-06-02 18:47:23 +02:00
|
|
|
use context;
|
2023-05-17 21:19:14 +02:00
|
|
|
|
2023-08-27 17:00:23 +02:00
|
|
|
/**
|
|
|
|
* Class to abstract information about (category) context for web service export
|
|
|
|
*/
|
2023-05-17 21:19:14 +02:00
|
|
|
class contextinfo {
|
2023-08-27 17:00:23 +02:00
|
|
|
/** @var context */
|
2023-05-17 21:19:14 +02:00
|
|
|
public $context;
|
2023-08-27 17:00:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create new based on context
|
|
|
|
* @param context $context The context to describe
|
|
|
|
*/
|
|
|
|
public function __construct(context $context) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$this->context = $context;
|
2023-11-05 15:49:32 +01:00
|
|
|
$this->ctxpath = array_reverse($this->context->get_parent_context_ids(true));
|
|
|
|
if (count($this->ctxpath) > 1 && $this->ctxpath[0] == 1) {
|
|
|
|
array_shift($this->ctxpath);
|
|
|
|
}
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-27 17:00:23 +02:00
|
|
|
/**
|
|
|
|
* Describe the result for the webservice model
|
|
|
|
* @param int $value Webservice requirement constant
|
|
|
|
*/
|
2023-08-28 08:51:52 +02:00
|
|
|
public static function structure($value = VALUE_REQUIRED) : \external_description {
|
2023-05-17 21:19:14 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2023-08-27 17:00:23 +02:00
|
|
|
/**
|
|
|
|
* Make the webservice result model
|
|
|
|
* @return array Webservice value
|
|
|
|
*/
|
2023-05-17 21:19:14 +02:00
|
|
|
public function model() {
|
2023-08-24 23:02:41 +02:00
|
|
|
|
2023-11-05 15:49:32 +01:00
|
|
|
|
2023-05-17 21:19:14 +02:00
|
|
|
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-11-05 15:49:32 +01:00
|
|
|
"path" => $this->path(false),
|
|
|
|
"shortpath" => $this->path(true),
|
2023-05-17 21:19:14 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2023-11-05 15:49:32 +01:00
|
|
|
/**
|
|
|
|
* Return context path names
|
|
|
|
* @param $short Use short names of contexts in path
|
|
|
|
* @return array of context path names
|
|
|
|
*/
|
|
|
|
public function path($short=false){
|
|
|
|
if ($short) {
|
|
|
|
return array_map(function($c) {
|
|
|
|
return \context::instance_by_id($c)->get_context_name(false, true);
|
|
|
|
}, $this->ctxpath);
|
|
|
|
} else {
|
|
|
|
return array_map(function($c) {
|
|
|
|
return \context::instance_by_id($c)->get_context_name(false, false);
|
|
|
|
}, $this->ctxpath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return full context path name
|
|
|
|
* @param $short Use short names of contexts in path
|
|
|
|
* @return string Concatenated string of paths
|
|
|
|
*/
|
|
|
|
public function pathstr($short=false){
|
|
|
|
return implode(" / ", $this->path($short));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-27 17:00:23 +02:00
|
|
|
/**
|
|
|
|
* Make new Contextinfo for context id
|
|
|
|
* @param int $contextid Context id
|
|
|
|
*/
|
2023-05-17 21:19:14 +02:00
|
|
|
public static function by_id($contextid): self {
|
|
|
|
return new self(self::context_by_id($contextid));
|
|
|
|
}
|
|
|
|
|
2023-08-27 17:00:23 +02:00
|
|
|
/**
|
|
|
|
* Find context by id, but also return system context for context id 0
|
|
|
|
* @param int $contextid Context id
|
|
|
|
* @return context The context
|
|
|
|
*/
|
2023-12-13 21:19:45 +01:00
|
|
|
public static function context_by_id($contextid): \context {
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($contextid <= 1) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$contextid = 1;
|
|
|
|
}
|
|
|
|
return \context::instance_by_id($contextid);
|
|
|
|
}
|
|
|
|
|
2023-08-25 11:52:05 +02:00
|
|
|
}
|