moodle_local_treestudyplan/classes/aggregator.php

141 lines
4.9 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;
require_once($CFG->libdir.'/externallib.php');
abstract class aggregator {
private const FALLBACK = "bistate";
2023-08-25 09:33:42 +02:00
private static $modsupported = [];
2023-08-24 23:02:41 +02:00
public static function supported($mod) {
2023-08-25 09:33:42 +02:00
if (!array_key_exists($mod, self::$modsupported)) {
self::$modsupported[$mod] = class_exists(self::aggregator_name($mod));
}
2023-08-25 09:33:42 +02:00
return self::$modsupported[$mod];
}
2023-08-24 23:02:41 +02:00
private static function aggregator_name($mod) {
return "\local_treestudyplan\\local\\aggregators\\{$mod}_aggregator";
}
2023-08-24 23:02:41 +02:00
public static function list() {
2023-08-25 09:44:34 +02:00
// Static list, since we'd need to implement a lot of static data for new aggregation methods anyway.
// And this is faster than any dynamic method.
return [
"core", # use moodle core completion
2023-08-24 23:02:41 +02:00
"bistate",
"tristate", # deprecated
];
}
2023-08-24 23:02:41 +02:00
public static function create($mod, $configstr) {
if (self::supported($mod)) {
2023-08-25 09:33:42 +02:00
$agclass = self::aggregator_name($mod);
return new $agclass($configstr);
} else {
throw new \InvalidArgumentException("Cannot find aggregator '{$mod}'");
}
}
2023-08-24 23:02:41 +02:00
public static function createOrDefault($mod, $configstr) {
try {
2023-08-24 23:02:41 +02:00
return self::create($mod, $configstr);
}
2023-08-24 23:02:41 +02:00
catch(\ValueError $x) {
return self::create(self::FALLBACK, "");
}
}
2023-08-24 23:02:41 +02:00
private function __construct($configstr) {
$this->initialize($configstr);
}
protected function initialize($configstr) {}
public abstract function needSelectGradables();
public abstract function isDeprecated();
public abstract function aggregate_course(courseinfo $courseinfo, studyitem $studyitem, $userid);
public abstract function aggregate_junction(array $completion, studyitem $studyitem, $userid);
public abstract function grade_completion(gradeinfo $gradeinfo, $userid);
2023-08-24 23:02:41 +02:00
// Aggregation method makes use of "required grades" in a course/module.
public abstract function useRequiredGrades();
2023-08-24 23:02:41 +02:00
// Aggregation method makes use of .
public abstract function useItemConditions();
2023-08-24 23:02:41 +02:00
// Whether the aggregation method uses core_completion, or treestudyplan custom completion.
public function usecorecompletioninfo() {
return false;
}
2023-08-24 23:02:41 +02:00
// Parameter editing functions - override in child class to implement parameter config for aggregation.
2023-08-24 23:02:41 +02:00
// Return the current configuration string.
public function config_string() {
return "";
}
2023-08-24 23:02:41 +02:00
public static function basic_structure($value=VALUE_REQUIRED) {
return new \external_single_structure([
"useRequiredGrades" => new \external_value(PARAM_BOOL, 'id of studyplan'),
"useItemConditions" => new \external_value(PARAM_BOOL, 'name of studyplan'),
2023-08-24 23:02:41 +02:00
], "Aggregator requirements", $value);
}
2023-08-24 23:02:41 +02:00
public function basic_model() {
return [
"useRequiredGrades" => $this->useRequiredGrades(),
"useItemConditions" => $this->useItemConditions(),
];
}
2023-08-24 23:02:41 +02:00
public static function list_structure($value=VALUE_REQUIRED) {
return new \external_multiple_structure(new \external_single_structure([
"id" => new \external_value(PARAM_TEXT, 'id of aggregator'),
"name" => new \external_value(PARAM_TEXT, 'name of agregator'),
"deprecated" => new \external_value(PARAM_BOOL, 'if method is deprecated'),
"defaultconfig" => new \external_value(PARAM_TEXT, 'default config of agregator'),
2023-08-24 23:02:41 +02:00
], "Available aggregators", $value));
}
2023-08-24 23:02:41 +02:00
public static function list_model() {
$list = [];
2023-08-24 23:02:41 +02:00
foreach (self::list() as $agid) {
$a = self::create($agid, ""); // create new one with empty config string.
$list[] = [
'id' => $agid,
2023-08-24 23:02:41 +02:00
'name' => get_string("{$agid}_aggregator_title", "local_treestudyplan"),
'deprecated' => $a->isDeprecated(),
'defaultconfig' => $a->config_string(),
];
}
2023-08-24 23:02:41 +02:00
return $list;
}
}