2024-02-09 14:48:15 +01:00
|
|
|
<?php
|
|
|
|
// 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/>.
|
2024-07-19 12:31:26 +02:00
|
|
|
|
2024-02-09 14:48:15 +01:00
|
|
|
/**
|
|
|
|
* Webservice class for handling studyplan reports
|
|
|
|
* @package local_treestudyplan
|
|
|
|
* @copyright 2023 P.M. Kuipers
|
|
|
|
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace local_treestudyplan;
|
|
|
|
defined('MOODLE_INTERNAL') || die();
|
|
|
|
|
|
|
|
use local_treestudyplan\local\helpers\webservicehelper;
|
2024-02-18 13:47:08 +01:00
|
|
|
use local_treestudyplan\period;
|
|
|
|
use local_treestudyplan\studyplan;
|
|
|
|
use local_treestudyplan\studyplanpage;
|
2024-02-09 14:48:15 +01:00
|
|
|
|
|
|
|
require_once($CFG->libdir.'/externallib.php');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Webservice class for handling studyplan reports
|
|
|
|
*/
|
|
|
|
class reportservice extends \external_api {
|
|
|
|
/**
|
|
|
|
* Capability required to view studyplans (for other users)
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const CAP_VIEW = "local/treestudyplan:viewuserreports";
|
|
|
|
|
2024-06-02 23:23:32 +02:00
|
|
|
/**
|
|
|
|
* Parameter description for webservice function
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public static function get_report_structure_parameters(): \external_function_parameters {
|
2024-02-09 14:48:15 +01:00
|
|
|
return new \external_function_parameters([
|
|
|
|
"pageid" => new \external_value(PARAM_INT, 'id of studyplan page'),
|
2024-06-02 19:23:40 +02:00
|
|
|
"firstperiod" => new \external_value(PARAM_INT, 'first period to include in report', VALUE_DEFAULT),
|
|
|
|
"lastperiod" => new \external_value(PARAM_INT, 'last period to include in report', VALUE_DEFAULT),
|
2024-02-09 14:48:15 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return value description for webservice function list_user_studyplans
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public static function get_report_structure_returns(): \external_description {
|
2024-02-09 14:48:15 +01:00
|
|
|
return new \external_single_structure([
|
2024-02-18 13:47:08 +01:00
|
|
|
"periods" => new \external_multiple_structure(new \external_single_structure([
|
|
|
|
"period" => period::structure(),
|
|
|
|
"lines" => new \external_multiple_structure(new \external_single_structure([
|
|
|
|
"line" => studyline::simple_structure(),
|
|
|
|
"items" => new \external_multiple_structure(studyitem::editor_structure(), "Information per studyitem"),
|
|
|
|
])),
|
|
|
|
])),
|
|
|
|
"firstperiod" => new \external_value(PARAM_INT, 'first period included in report'),
|
|
|
|
"lastperiod" => new \external_value(PARAM_INT, 'last period included in report'),
|
|
|
|
"studyplan" => studyplan::simple_structure(),
|
|
|
|
"page" => studyplanpage::simple_structure(),
|
2024-02-09 14:48:15 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get studyplan page structure for a report
|
|
|
|
* @param int $pageid ID of user to check specific info for
|
|
|
|
* @param int|null $firstperiod First period to include in report
|
|
|
|
* @param int|null $lastperiod Last period to include in report
|
|
|
|
* @return object
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public static function get_report_structure($pageid, $firstperiod=null, $lastperiod=null) {
|
2024-02-18 13:47:08 +01:00
|
|
|
$page = studyplanpage::find_by_id($pageid);
|
2024-06-02 19:23:40 +02:00
|
|
|
webservicehelper::require_capabilities(self::CAP_VIEW, $page->studyplan()->context());
|
2024-02-18 13:47:08 +01:00
|
|
|
|
|
|
|
$plan = $page->studyplan();
|
|
|
|
if (empty($firstperiod)) {
|
|
|
|
$firstperiod = 1;
|
2024-06-02 19:23:40 +02:00
|
|
|
}
|
2024-02-18 13:47:08 +01:00
|
|
|
if (empty($lastperiod)) {
|
2024-06-02 23:23:32 +02:00
|
|
|
// Index for periods starts at 1, so the period count is same as length.
|
2024-02-18 13:47:08 +01:00
|
|
|
$lastperiod = $page->periods();
|
|
|
|
}
|
|
|
|
|
|
|
|
$model = [
|
|
|
|
"studyplan" => $plan->simple_model(),
|
|
|
|
"page" => $page->simple_model(),
|
|
|
|
"firstperiod" => $firstperiod,
|
|
|
|
"lastperiod" => $lastperiod,
|
|
|
|
"periods" => [],
|
|
|
|
];
|
|
|
|
|
2024-06-02 23:23:32 +02:00
|
|
|
// Find all relevant parts in this order.
|
2024-02-18 13:47:08 +01:00
|
|
|
|
2024-06-02 23:23:32 +02:00
|
|
|
for ($i = $firstperiod; $i <= $lastperiod; $i++) {
|
2024-02-18 13:47:08 +01:00
|
|
|
$period = period::find($page, $i);
|
|
|
|
$pmodel = [
|
|
|
|
'period' => $period->model(),
|
|
|
|
'lines' => [],
|
|
|
|
];
|
|
|
|
|
|
|
|
$lines = studyline::find_page_children($page);
|
|
|
|
foreach ($lines as $line) {
|
|
|
|
$lmodel = [
|
|
|
|
"line" => $line->simple_model(),
|
|
|
|
"items" => [],
|
|
|
|
];
|
2024-06-02 19:23:40 +02:00
|
|
|
$items = studyitem::search_studyline_children($line, $i, studyitem::COURSE);
|
2024-02-18 13:47:08 +01:00
|
|
|
if (count($items) > 0) {
|
|
|
|
foreach ($items as $item) {
|
|
|
|
$lmodel["items"][] = $item->editor_model();
|
|
|
|
}
|
2024-06-02 19:23:40 +02:00
|
|
|
|
2024-02-18 13:47:08 +01:00
|
|
|
// Only include the line if it has actual children - saves us from muddying up the report.
|
|
|
|
$pmodel['lines'][] = $lmodel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$model["periods"][] = $pmodel;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $model;
|
2024-02-09 14:48:15 +01:00
|
|
|
}
|
|
|
|
|
2024-06-02 23:23:32 +02:00
|
|
|
/**
|
|
|
|
* Parameter description for webservice function
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public static function get_report_data_parameters(): \external_function_parameters {
|
2024-02-09 14:48:15 +01:00
|
|
|
return new \external_function_parameters([
|
|
|
|
"pageid" => new \external_value(PARAM_INT, 'id of studyplan page'),
|
|
|
|
"userid" => new \external_value(PARAM_INT, 'id of user'),
|
2024-06-02 19:23:40 +02:00
|
|
|
"firstperiod" => new \external_value(PARAM_INT, 'first period to include in report', VALUE_DEFAULT),
|
|
|
|
"lastperiod" => new \external_value(PARAM_INT, 'last period to include in report', VALUE_DEFAULT),
|
2024-02-09 14:48:15 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return value description for webservice function list_user_studyplans
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public static function get_report_data_returns(): \external_description {
|
2024-02-18 13:47:08 +01:00
|
|
|
return new \external_multiple_structure(studyitem::user_structure(), "Information per studyitem");
|
2024-02-09 14:48:15 +01:00
|
|
|
}
|
|
|
|
|
2024-06-02 23:23:32 +02:00
|
|
|
/**
|
|
|
|
* Webservice function get_report data
|
2024-06-03 04:00:46 +02:00
|
|
|
* @param int $pageid Id of the studyplan page to build the report for
|
|
|
|
* @param int $userid ID ofthe user to gather the report for
|
|
|
|
* @param int|null $firstperiod First period of the page to include in the report (first of page if left empty)
|
|
|
|
* @param int|null $lastperiod Last period of the page to include in the report (last of page if left empty)
|
2024-06-02 23:23:32 +02:00
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public static function get_report_data($pageid, $userid, $firstperiod=null, $lastperiod=null) {
|
2024-02-18 13:47:08 +01:00
|
|
|
$page = studyplanpage::find_by_id($pageid);
|
2024-06-02 19:23:40 +02:00
|
|
|
webservicehelper::require_capabilities(self::CAP_VIEW, $page->studyplan()->context());
|
2024-02-18 13:47:08 +01:00
|
|
|
$plan = $page->studyplan();
|
|
|
|
if (!$plan->has_linked_user($userid)) {
|
2024-06-02 19:23:40 +02:00
|
|
|
throw new \moodle_exception("error:usernotassociated", "local_treestudyplan");
|
2024-02-18 13:47:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($firstperiod)) {
|
|
|
|
$firstperiod = 1;
|
2024-06-02 19:23:40 +02:00
|
|
|
}
|
2024-02-18 13:47:08 +01:00
|
|
|
if (empty($lastperiod)) {
|
2024-06-02 23:23:32 +02:00
|
|
|
// Index for periods starts at 1, so the period count is same as length.
|
2024-02-18 13:47:08 +01:00
|
|
|
$lastperiod = $page->periods();
|
|
|
|
}
|
|
|
|
|
|
|
|
$model = [];
|
2024-06-02 23:23:32 +02:00
|
|
|
for ($i = $firstperiod; $i <= $lastperiod; $i++) {
|
2024-02-18 13:47:08 +01:00
|
|
|
$lines = studyline::find_page_children($page);
|
|
|
|
foreach ($lines as $line) {
|
2024-06-02 19:23:40 +02:00
|
|
|
$items = studyitem::search_studyline_children($line, $i, studyitem::COURSE);
|
2024-02-18 13:47:08 +01:00
|
|
|
if (count($items) > 0) {
|
|
|
|
foreach ($items as $item) {
|
|
|
|
$model[] = $item->user_model($userid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $model;
|
2024-02-09 14:48:15 +01:00
|
|
|
}
|
|
|
|
|
2024-06-02 23:23:32 +02:00
|
|
|
/**
|
|
|
|
* Parameter description for webservice function
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public static function get_report_details_parameters(): \external_function_parameters {
|
2024-02-09 14:48:15 +01:00
|
|
|
return new \external_function_parameters([
|
|
|
|
"itemid" => new \external_value(PARAM_INT, 'id of studyitem'),
|
|
|
|
"userid" => new \external_value(PARAM_INT, 'id of user'),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return value description for webservice function list_user_studyplans
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public static function get_report_details_returns(): \external_description {
|
2024-02-18 13:47:08 +01:00
|
|
|
return studyitem::user_structure();
|
2024-02-09 14:48:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get study item details for a user
|
|
|
|
* @param int $itemid ID of study item
|
|
|
|
* @param int $userid ID of user to check specific info for
|
|
|
|
* @return array
|
|
|
|
*/
|
2024-06-02 19:23:40 +02:00
|
|
|
public static function get_report_details($itemid, $userid) {
|
2024-02-18 13:47:08 +01:00
|
|
|
$item = studyitem::find_by_id($itemid);
|
|
|
|
$plan = $item->studyline()->studyplan();
|
2024-06-02 19:23:40 +02:00
|
|
|
webservicehelper::require_capabilities(self::CAP_VIEW, $plan->context());
|
2024-02-18 13:47:08 +01:00
|
|
|
if (!$plan->has_linked_user($userid)) {
|
2024-06-02 19:23:40 +02:00
|
|
|
throw new \moodle_exception("error:usernotassociated", "local_treestudyplan");
|
2024-02-18 13:47:08 +01:00
|
|
|
}
|
|
|
|
return $item->user_model($userid);
|
2024-02-09 14:48:15 +01:00
|
|
|
}
|
|
|
|
|
2024-06-02 19:23:40 +02:00
|
|
|
|
2024-02-09 14:48:15 +01:00
|
|
|
}
|