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/>.
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
require_once($CFG->libdir.'/externallib.php');
|
|
|
|
|
|
|
|
use \local_treestudyplan\courseinfo;
|
2023-06-16 13:49:47 +02:00
|
|
|
use \local_treestudyplan\associationservice;
|
2023-05-17 21:19:14 +02:00
|
|
|
use \local_treestudyplan\local\helpers\webservicehelper;
|
2023-06-16 13:49:47 +02:00
|
|
|
use \local_treestudyplan\completionscanner;
|
|
|
|
use \local_treestudyplan\gradingscanner;
|
2023-05-17 21:19:14 +02:00
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
class courseservice extends \external_api
|
2023-05-17 21:19:14 +02:00
|
|
|
{
|
2023-06-16 13:49:47 +02:00
|
|
|
const CAP_EDIT = "local/treestudyplan:editstudyplan";
|
|
|
|
const CAP_VIEW = "local/treestudyplan:viewuserreports";
|
2023-05-17 21:19:14 +02:00
|
|
|
|
|
|
|
/************************
|
|
|
|
* *
|
|
|
|
* list_courses *
|
|
|
|
* *
|
|
|
|
************************/
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public static function map_categories_parameters()
|
2023-08-25 09:44:34 +02:00
|
|
|
{
|
2023-05-17 21:19:14 +02:00
|
|
|
return new \external_function_parameters( [
|
|
|
|
"root_id" => new \external_value(PARAM_INT, 'root category to use as base', VALUE_DEFAULT),
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
2023-08-25 09:44:34 +02:00
|
|
|
public static function map_categories_returns()
|
|
|
|
{
|
2023-05-17 21:19:14 +02:00
|
|
|
return new \external_multiple_structure(static::map_category_structure(false));
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
protected static function map_category_structure($lazy=false, $value=VALUE_REQUIRED) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$s = [
|
|
|
|
"id" => new \external_value(PARAM_INT, 'course category id'),
|
|
|
|
"context_id" => new \external_value(PARAM_INT, 'course category context id'),
|
|
|
|
"category" => contextinfo::structure(VALUE_OPTIONAL),
|
|
|
|
"haschildren" => new \external_value(PARAM_BOOL, 'True if the category has child categories'),
|
|
|
|
"hascourses" => new \external_value(PARAM_BOOL, 'True if the category contains courses'),
|
2023-08-24 23:02:41 +02:00
|
|
|
"studyplancount" => new \external_value(PARAM_INT, 'number of linked studyplans', VALUE_OPTIONAL),
|
2023-05-17 21:19:14 +02:00
|
|
|
];
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
if (!$lazy > 0) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$s["courses"] = new \external_multiple_structure( courseinfo::editor_structure() );
|
|
|
|
$s["children"] = new \external_multiple_structure( static::map_category_structure(true));
|
|
|
|
}
|
2023-08-24 23:02:41 +02:00
|
|
|
return new \external_single_structure($s, "CourseCat info", $value);
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-25 09:33:42 +02:00
|
|
|
public static function map_categories($rootid = 0) {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $CFG, $DB;
|
|
|
|
|
2023-08-25 09:33:42 +02:00
|
|
|
$root = \core_course_category::get($rootid);
|
2023-05-17 21:19:14 +02:00
|
|
|
$context = $root->get_context();
|
2023-08-24 23:02:41 +02:00
|
|
|
// Make sure the user has access to the context for editing purposes.
|
|
|
|
webservicehelper::require_capabilities(self::CAP_EDIT, $context);
|
2023-05-17 21:19:14 +02:00
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
// Determine top categories from provided context.
|
2023-05-17 21:19:14 +02:00
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($root->id == 0) {
|
2023-08-25 09:44:34 +02:00
|
|
|
// On the system level, determine the user's topmost allowed catecories.
|
2023-08-25 09:33:42 +02:00
|
|
|
$usertop = \core_course_category::user_top();
|
|
|
|
if ($usertop->id == 0) { // top category..
|
2023-08-24 23:02:41 +02:00
|
|
|
$children = $root->get_children(); // returns a list of çore_course_category, let it overwrite $children.
|
2023-05-17 21:19:14 +02:00
|
|
|
} else {
|
2023-08-25 09:33:42 +02:00
|
|
|
$children = [$usertop];
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
2023-08-24 23:02:41 +02:00
|
|
|
} else if ($root->is_uservisible()) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$children = [$root];
|
2023-08-24 23:02:41 +02:00
|
|
|
}
|
2023-05-17 21:19:14 +02:00
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($children as $cat) {
|
|
|
|
$list[] = static::map_category($cat, false);
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
return $list;
|
2023-08-24 23:02:41 +02:00
|
|
|
}
|
2023-05-17 21:19:14 +02:00
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public static function get_category_parameters()
|
2023-08-25 09:44:34 +02:00
|
|
|
{
|
2023-05-17 21:19:14 +02:00
|
|
|
return new \external_function_parameters( [
|
|
|
|
"id" => new \external_value(PARAM_INT, 'id of category'),
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
2023-08-25 09:44:34 +02:00
|
|
|
public static function get_category_returns()
|
|
|
|
{
|
2023-05-17 21:19:14 +02:00
|
|
|
return static::map_category_structure(false);
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public static function get_category($id) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$cat = \core_course_category::get($id);
|
|
|
|
return static::map_category($cat);
|
2023-08-24 23:02:41 +02:00
|
|
|
}
|
2023-05-17 21:19:14 +02:00
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
protected static function map_category(\core_course_category $cat, $lazy=false) {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB;
|
|
|
|
$catcontext = $cat->get_context();
|
2023-08-25 09:33:42 +02:00
|
|
|
$ctxinfo = new contextinfo($catcontext);
|
2023-08-24 23:02:41 +02:00
|
|
|
$children = $cat->get_children(); // only shows children visible to the current user.
|
2023-05-17 21:19:14 +02:00
|
|
|
$courses = $cat->get_courses();
|
|
|
|
$model = [
|
|
|
|
"id" => $cat->id,
|
|
|
|
"context_id" => $catcontext->id,
|
2023-08-25 09:33:42 +02:00
|
|
|
"category" => $ctxinfo->model(),
|
2023-05-17 21:19:14 +02:00
|
|
|
"haschildren" => !empty($children),
|
|
|
|
"hascourses" => !empty($courses),
|
|
|
|
];
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
if (!$lazy) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$model["courses"] = [];
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($courses as $course) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$courseinfo = new courseinfo($course->id);
|
|
|
|
$model["courses"][] = $courseinfo->editor_model();
|
|
|
|
}
|
|
|
|
|
|
|
|
$model["children"] = [];
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($children as $child) {
|
|
|
|
$model["children"][] = static::map_category($child, true);
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public static function list_accessible_categories_parameters()
|
2023-08-25 09:44:34 +02:00
|
|
|
{
|
2023-05-17 21:19:14 +02:00
|
|
|
return new \external_function_parameters( [
|
2023-08-24 23:02:41 +02:00
|
|
|
"operation" => new \external_value(PARAM_TEXT, 'type of operation ["view"|"edit"]', VALUE_DEFAULT), ]
|
2023-05-17 21:19:14 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-25 09:44:34 +02:00
|
|
|
public static function list_accessible_categories_returns()
|
|
|
|
{
|
2023-05-17 21:19:14 +02:00
|
|
|
return new \external_multiple_structure(static::map_category_structure(true));
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public static function list_accessible_categories($operation="edit")
|
2023-08-25 09:44:34 +02:00
|
|
|
{
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($operation == "edit") {
|
2023-06-16 13:49:47 +02:00
|
|
|
$capability = self::CAP_EDIT;
|
2023-08-25 09:44:34 +02:00
|
|
|
} else { // Operation == "view" || default.
|
2023-06-16 13:49:47 +02:00
|
|
|
$capability = self::CAP_VIEW;
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$cats = static::categories_by_capability($capability);
|
|
|
|
|
|
|
|
$list = [];
|
|
|
|
/* @var $cat \core_course_category */
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($cats as $cat) {
|
|
|
|
$list[] = static::map_category($cat, true);
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
return $list;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public static function categories_by_capability($capability, \core_course_category $parent=null) {
|
|
|
|
// List the categories in which the user has a specific capability.
|
2023-05-17 21:19:14 +02:00
|
|
|
$list = [];
|
2023-08-25 09:44:34 +02:00
|
|
|
// Initialize parent if needed.
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($parent == null) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$parent = \core_course_category::user_top();
|
2023-08-24 23:02:41 +02:00
|
|
|
if (has_capability($capability, $parent->get_context())) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$list[] = $parent;
|
|
|
|
}
|
|
|
|
}
|
2023-08-24 23:02:41 +02:00
|
|
|
|
2023-05-17 21:19:14 +02:00
|
|
|
$children = $parent->get_children();
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($children as $child) {
|
|
|
|
// Check if we should add this category.
|
|
|
|
if (has_capability($capability, $child->get_context())) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$list[] = $child;
|
2023-08-24 23:02:41 +02:00
|
|
|
// For optimization purposes, we include all its children now, since they will have inherited the permission.
|
|
|
|
// #PREMATURE_OPTIMIZATION ???.
|
|
|
|
$list = array_merge($list, self::recursive_child_categories($child));
|
2023-05-17 21:19:14 +02:00
|
|
|
} else {
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($child->get_children_count() > 0) {
|
|
|
|
$list = array_merge($list, self::categories_by_capability($capability, $child));
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
protected static function recursive_child_categories(\core_course_category $parent) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$list = [];
|
|
|
|
$children = $parent->get_children();
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($children as $child) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$list[] = $child;
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($child->get_children_count() > 0) {
|
|
|
|
$list = array_merge($list, self::recursive_child_categories($child));
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public static function list_used_categories_parameters()
|
2023-08-25 09:44:34 +02:00
|
|
|
{
|
2023-05-17 21:19:14 +02:00
|
|
|
return new \external_function_parameters( [
|
2023-08-24 23:02:41 +02:00
|
|
|
"operation" => new \external_value(PARAM_TEXT, 'type of operation ["view"|"edit"]', VALUE_DEFAULT),
|
2023-05-17 21:19:14 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2023-08-25 09:44:34 +02:00
|
|
|
public static function list_used_categories_returns()
|
|
|
|
{
|
2023-05-17 21:19:14 +02:00
|
|
|
return new \external_multiple_structure(static::map_category_structure(true));
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public static function list_used_categories($operation='edit')
|
2023-08-25 09:44:34 +02:00
|
|
|
{
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB;
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($operation == "edit") {
|
2023-06-16 13:49:47 +02:00
|
|
|
$capability = self::CAP_EDIT;
|
2023-08-25 09:44:34 +02:00
|
|
|
} else { // Operation == "view" || default.
|
2023-06-16 13:49:47 +02:00
|
|
|
$capability = self::CAP_VIEW;
|
2023-08-24 23:02:41 +02:00
|
|
|
}
|
2023-08-25 09:33:42 +02:00
|
|
|
$contextids = [];
|
2023-08-24 23:02:41 +02:00
|
|
|
$rs = $DB->get_recordset_sql("SELECT DISTINCT context_id, COUNT(*) as num FROM {local_treestudyplan}
|
2023-05-17 21:19:14 +02:00
|
|
|
GROUP BY context_id");
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($rs as $r) {
|
2023-08-25 09:33:42 +02:00
|
|
|
$contextids[$r->context_id] = $r->num;
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
$rs->close();
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
|
|
|
|
// Now filter the categories that the user has acces to by the used context id's.
|
|
|
|
// (That should filter out irrelevant stuff).
|
2023-05-17 21:19:14 +02:00
|
|
|
$cats = static::categories_by_capability($capability);
|
|
|
|
|
|
|
|
$list = [];
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($cats as $cat) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$count = 0;
|
|
|
|
$ctxid = $cat->get_context()->id;
|
2023-08-25 09:33:42 +02:00
|
|
|
if (array_key_exists($ctxid, $contextids)) {
|
|
|
|
$count = $contextids[$ctxid];
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
$o = static::map_category($cat, true);
|
2023-05-17 21:19:14 +02:00
|
|
|
$o["studyplancount"] = $count;
|
|
|
|
$list[] = $o;
|
|
|
|
}
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public static function list_accessible_categories_with_usage($operation='edit') {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $DB;
|
2023-08-24 23:02:41 +02:00
|
|
|
if ($operation == "edit") {
|
2023-06-16 13:49:47 +02:00
|
|
|
$capability = self::CAP_EDIT;
|
2023-08-25 09:44:34 +02:00
|
|
|
} else { // Operation == "view" || default.
|
2023-06-16 13:49:47 +02:00
|
|
|
$capability = self::CAP_VIEW;
|
2023-08-24 23:02:41 +02:00
|
|
|
}
|
2023-08-25 09:44:34 +02:00
|
|
|
// Retrieve context ids used.
|
2023-08-25 09:33:42 +02:00
|
|
|
$contextids = [];
|
2023-08-24 23:02:41 +02:00
|
|
|
$rs = $DB->get_recordset_sql("SELECT DISTINCT context_id, COUNT(*) as num FROM {local_treestudyplan}
|
2023-05-17 21:19:14 +02:00
|
|
|
GROUP BY context_id");
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($rs as $r) {
|
2023-08-25 09:33:42 +02:00
|
|
|
$contextids[$r->context_id] = $r->num;
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
$rs->close();
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
// Now filter the categories that the user has acces to by the used context id's.
|
|
|
|
// (That should filter out irrelevant stuff).
|
2023-05-17 21:19:14 +02:00
|
|
|
$cats = static::categories_by_capability($capability);
|
|
|
|
|
|
|
|
$list = [];
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($cats as $cat) {
|
2023-05-17 21:19:14 +02:00
|
|
|
$count = 0;
|
|
|
|
$ctxid = $cat->get_context()->id;
|
2023-08-25 09:33:42 +02:00
|
|
|
if (array_key_exists($ctxid, $contextids)) {
|
|
|
|
$count = $contextids[$ctxid];
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
$o = new \stdClass();
|
|
|
|
$o->cat = $cat;
|
2023-06-16 13:49:47 +02:00
|
|
|
$o->ctxid = $ctxid;
|
2023-05-17 21:19:14 +02:00
|
|
|
$o->count = $count;
|
|
|
|
$list[] = $o;
|
|
|
|
}
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-16 13:49:47 +02:00
|
|
|
/**************************************
|
2023-08-24 23:02:41 +02:00
|
|
|
*
|
2023-06-16 13:49:47 +02:00
|
|
|
* Progress scanners for teacherview
|
2023-08-24 23:02:41 +02:00
|
|
|
*
|
2023-06-16 13:49:47 +02:00
|
|
|
**************************************/
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public static function scan_grade_progress_parameters() {
|
2023-06-16 13:49:47 +02:00
|
|
|
return new \external_function_parameters( [
|
2023-08-24 23:02:41 +02:00
|
|
|
"gradeitemid" => new \external_value(PARAM_INT, 'Grade item ID to scan progress for', VALUE_DEFAULT),
|
|
|
|
"studyplanid" => new \external_value(PARAM_INT, 'Study plan id to check progress in', VALUE_DEFAULT),
|
2023-06-16 13:49:47 +02:00
|
|
|
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public static function scan_grade_progress_returns() {
|
2023-06-16 13:49:47 +02:00
|
|
|
return gradingscanner::structure(VALUE_REQUIRED);
|
|
|
|
}
|
2023-08-24 23:02:41 +02:00
|
|
|
|
|
|
|
public static function scan_grade_progress($gradeitemid, $studyplanid) {
|
2023-06-16 13:49:47 +02:00
|
|
|
global $DB;
|
2023-08-24 23:02:41 +02:00
|
|
|
// Verify access to the study plan.
|
2023-06-16 13:49:47 +02:00
|
|
|
$o = studyplan::findById($studyplanid);
|
2023-08-24 23:02:41 +02:00
|
|
|
webservicehelper::require_capabilities(self::CAP_VIEW, $o->context());
|
|
|
|
|
|
|
|
// Retrieve grade item.
|
2023-06-16 13:49:47 +02:00
|
|
|
$gi = \grade_item::fetch(["id" => $gradeitemid]);
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
// Validate course is linked to studyplan.
|
2023-06-16 13:49:47 +02:00
|
|
|
$courseid = $gi->courseid;
|
2023-08-24 23:02:41 +02:00
|
|
|
if (!$o->course_linked($courseid)) {
|
2023-06-16 13:49:47 +02:00
|
|
|
throw new \webservice_access_exception("Course {$courseid} linked to grade item {$gradeitemid} is not linked to studyplan {$o->id()}");
|
|
|
|
}
|
|
|
|
|
|
|
|
$scanner = new gradingscanner($gi);
|
|
|
|
return $scanner->model();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public static function scan_completion_progress_parameters()
|
2023-08-25 09:44:34 +02:00
|
|
|
{
|
2023-06-16 13:49:47 +02:00
|
|
|
return new \external_function_parameters( [
|
2023-08-24 23:02:41 +02:00
|
|
|
"criteriaid" => new \external_value(PARAM_INT, 'CriteriaID to scan progress for', VALUE_DEFAULT),
|
|
|
|
"studyplanid" => new \external_value(PARAM_INT, 'Study plan id to check progress in', VALUE_DEFAULT),
|
|
|
|
"courseid" => new \external_value(PARAM_INT, 'Course id of criteria', VALUE_DEFAULT),
|
2023-06-16 13:49:47 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2023-08-25 09:44:34 +02:00
|
|
|
public static function scan_completion_progress_returns()
|
|
|
|
{
|
2023-06-16 13:49:47 +02:00
|
|
|
return completionscanner::structure(VALUE_REQUIRED);
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public static function scan_completion_progress($criteriaid, $studyplanid, $courseid)
|
2023-08-25 09:44:34 +02:00
|
|
|
{
|
2023-06-16 13:49:47 +02:00
|
|
|
global $DB;
|
2023-08-24 23:02:41 +02:00
|
|
|
// Verify access to the study plan.
|
2023-06-16 13:49:47 +02:00
|
|
|
$o = studyplan::findById($studyplanid);
|
2023-08-24 23:02:41 +02:00
|
|
|
webservicehelper::require_capabilities(self::CAP_VIEW, $o->context());
|
2023-06-16 13:49:47 +02:00
|
|
|
|
|
|
|
$crit = \completion_criteria::fetch(["id" => $criteriaid]);
|
2023-08-24 23:02:41 +02:00
|
|
|
if (!$o->course_linked($courseid)) {
|
2023-06-16 13:49:47 +02:00
|
|
|
throw new \webservice_access_exception("Course {$courseid} linked to criteria {$criteriaid} is not linked to studyplan {$o->id()}");
|
|
|
|
}
|
2023-08-24 23:02:41 +02:00
|
|
|
$scanner = new completionscanner($crit, \get_course($courseid));
|
2023-06-16 13:49:47 +02:00
|
|
|
return $scanner->model();
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public static function scan_badge_progress_parameters()
|
2023-08-25 09:44:34 +02:00
|
|
|
{
|
2023-06-16 13:49:47 +02:00
|
|
|
return new \external_function_parameters( [
|
2023-08-24 23:02:41 +02:00
|
|
|
"badgeid" => new \external_value(PARAM_INT, 'Badge to scan progress for', VALUE_DEFAULT),
|
|
|
|
"studyplanid" => new \external_value(PARAM_INT, 'Study plan id to limit progress search to (to determine which students to scan)', VALUE_DEFAULT),
|
2023-06-16 13:49:47 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2023-08-25 09:44:34 +02:00
|
|
|
public static function scan_badge_progress_returns()
|
|
|
|
{
|
2023-06-16 13:49:47 +02:00
|
|
|
return new \external_single_structure([
|
|
|
|
"total" => new \external_value(PARAM_INT, 'Total number of students scanned'),
|
|
|
|
"issued" => new \external_value(PARAM_INT, 'Number of issued badges'),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
public static function scan_badge_progress($badgeid, $studyplanid)
|
2023-08-25 09:44:34 +02:00
|
|
|
{
|
2023-06-16 13:49:47 +02:00
|
|
|
global $DB;
|
2023-08-24 23:02:41 +02:00
|
|
|
// Check access to the study plan.
|
2023-06-16 13:49:47 +02:00
|
|
|
$o = studyplan::findById($studyplanid);
|
2023-08-24 23:02:41 +02:00
|
|
|
webservicehelper::require_capabilities(self::CAP_VIEW, $o->context());
|
2023-06-16 13:49:47 +02:00
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
// Validate that badge is linked to studyplan.
|
|
|
|
if (!$o->badge_linked($badgeid)) {
|
2023-06-16 13:49:47 +02:00
|
|
|
throw new \webservice_access_exception("Badge {$badgeid} is not linked to studyplan {$o->id()}");
|
|
|
|
}
|
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
// Get badge info.
|
2023-06-16 13:49:47 +02:00
|
|
|
$badge = new \core_badges\badge($badgeid);
|
|
|
|
$badgeinfo = new badgeinfo($badge);
|
|
|
|
|
2023-08-25 09:44:34 +02:00
|
|
|
// Get the connected users.
|
2023-06-16 13:49:47 +02:00
|
|
|
$students = associationservice::all_associated($studyplanid);
|
2023-08-24 23:02:41 +02:00
|
|
|
// Just get the user ids.
|
|
|
|
$studentids = array_map(function ($a) { return $a["id"];}, $students);
|
2023-06-16 13:49:47 +02:00
|
|
|
|
|
|
|
return [
|
|
|
|
"total" => count($studentids),
|
|
|
|
"issued" => $badgeinfo->count_issued($studentids),
|
|
|
|
];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|