moodle_local_treestudyplan/classes/studentstudyplanservice.php

370 lines
12 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/>.
/**
2023-08-27 23:27:07 +02:00
* Webservice class for retrieving student studyplans
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
*/
namespace local_treestudyplan;
2023-08-25 12:04:27 +02:00
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/externallib.php');
use \local_treestudyplan\local\helpers\webservicehelper;
require_once($CFG->libdir.'/badgeslib.php');
2023-08-27 23:27:07 +02:00
/**
* Webservice class for retrieving student studyplans
*/
2023-08-25 10:41:56 +02:00
class studentstudyplanservice extends \external_api {
2023-08-27 23:27:07 +02:00
/**
* Capability required to view studyplans of other users
* @var string
*/
const CAP_VIEWOTHER = "local/treestudyplan:viewuserreports";
/************************
* *
* list_user_studyplans *
* *
************************/
2023-08-27 21:57:21 +02:00
/**
* Parameter description for webservice function list_user_studyplans
*/
2023-08-28 08:51:52 +02:00
public static function list_user_studyplans_parameters() : \external_function_parameters {
return new \external_function_parameters([
"userid" => new \external_value(PARAM_INT, 'id of student', VALUE_DEFAULT),
]);
}
2023-08-27 21:57:21 +02:00
/**
* Return value description for webservice function list_user_studyplans
*/
2023-08-28 08:51:52 +02:00
public static function list_user_studyplans_returns() : \external_description {
return new \external_multiple_structure(
studyplan::simple_structure()
);
}
2023-08-27 23:27:07 +02:00
/**
* Return overview of the studyplans for a specific user
* @param int $userid ID of user to check specific info for
* @return array
*/
public static function list_user_studyplans($userid) {
global $CFG, $DB;
$list = [];
$studyplans = studyplan::find_for_user($userid);
2023-08-24 23:02:41 +02:00
foreach ($studyplans as $studyplan) {
2023-08-25 09:44:34 +02:00
// Only include studyplans in the context the user has permissions for.
2023-08-24 23:02:41 +02:00
if (webservicehelper::has_capabilities(self::CAP_VIEWOTHER, $studyplan->context(), false)) {
2023-08-25 17:33:20 +02:00
$list[] = $studyplan->simple_model();
}
}
return $list;
}
/************************
* *
* get_user_studyplans *
* *
************************/
2023-08-27 21:57:21 +02:00
/**
* Parameter description for webservice function get_user_studyplans
*/
2023-08-28 08:51:52 +02:00
public static function get_user_studyplans_parameters() : \external_function_parameters {
return new \external_function_parameters( [
"userid" => new \external_value(PARAM_INT, 'id of user'),
] );
}
2023-08-27 21:57:21 +02:00
/**
* Return value description for webservice function get_user_studyplans
*/
2023-08-28 08:51:52 +02:00
public static function get_user_studyplans_returns() : \external_description {
return new \external_multiple_structure(
studyplan::user_structure()
);
}
2023-08-27 23:27:07 +02:00
/**
* Get the full studyplans for a specific user
* @param int $userid ID of user to check specific info for
* @return array
*/
2023-08-24 23:02:41 +02:00
public static function get_user_studyplans($userid) {
global $CFG, $DB;
$studyplans = studyplan::find_for_user($userid);
2023-08-24 23:02:41 +02:00
$map = [];
foreach ($studyplans as $studyplan) {
2023-08-25 09:44:34 +02:00
// Only include studyplans in the context the user has permissions for.
2023-08-24 23:02:41 +02:00
if (webservicehelper::has_capabilities(self::CAP_VIEWOTHER, $studyplan->context(), false)) {
$map[] = $studyplan->user_model($userid);
}
}
return $map;
}
/************************
* *
* get_user_studyplan *
* *
************************/
2023-08-27 21:57:21 +02:00
/**
* Parameter description for webservice function get_user_studyplan
*/
2023-08-28 08:51:52 +02:00
public static function get_user_studyplan_parameters() : \external_function_parameters {
return new \external_function_parameters( [
"userid" => new \external_value(PARAM_INT, 'id of user'),
"studyplanid" => new \external_value(PARAM_INT, 'id of specific studyplan to provide'),
] );
}
2023-08-27 21:57:21 +02:00
/**
* Return value description for webservice function get_user_studyplan
*/
2023-08-28 08:51:52 +02:00
public static function get_user_studyplan_returns() : \external_description {
return studyplan::user_structure();
}
2023-08-27 23:27:07 +02:00
/**
* Get a specific studyplan for a given user
* @param int $userid ID of user to check specific info for
* @param int $studyplanid ID of studyplan to view
* @return array
*/
2023-08-24 23:02:41 +02:00
public static function get_user_studyplan($userid, $studyplanid) {
global $CFG, $DB;
2023-08-25 17:33:20 +02:00
$studyplan = studyplan::find_by_id($studyplanid);
2023-08-24 23:02:41 +02:00
webservicehelper::require_capabilities(self::CAP_VIEWOTHER, $studyplan->context());
2023-08-24 23:02:41 +02:00
if ($studyplan->has_linked_user($userid)) {
return $studyplan->user_model($userid);
2023-08-24 23:09:20 +02:00
} else {
return null;
}
}
/****************************
* *
* get_invited_studyplan *
* *
****************************/
2023-08-27 21:57:21 +02:00
/**
* Parameter description for webservice function get_invited_studyplan
*/
2023-08-28 08:51:52 +02:00
public static function get_invited_studyplan_parameters() : \external_function_parameters {
return new \external_function_parameters( [
"invitekey" => new \external_value(PARAM_RAW, 'invite key'),
] );
}
2023-08-27 21:57:21 +02:00
/**
* Return value description for webservice function get_invited_studyplan
*/
2023-08-28 08:51:52 +02:00
public static function get_invited_studyplan_returns() : \external_description {
return new \external_multiple_structure(
studyplan::user_structure()
);
}
2023-08-27 23:27:07 +02:00
/**
* Get studyplan based on invite key
* @param int $invitekey Invitation key
* @return array
*/
2023-08-24 23:02:41 +02:00
public static function get_invited_studyplan($invitekey) {
global $CFG, $DB;
2023-07-04 23:38:08 +02:00
2023-08-25 11:52:05 +02:00
$invite = $DB->get_record_select(
"local_treestudyplan_invit",
$DB->sql_compare_text("invitekey"). " = " . $DB->sql_compare_text(":invitekey"),
['invitekey' => $invitekey]
);
2023-08-24 23:02:41 +02:00
if (empty($invite)) {
return [];
}
2023-08-24 23:02:41 +02:00
// Validate context now.
2023-07-04 23:38:08 +02:00
\external_api::validate_context(\context_system::instance());
$userid = $invite->user_id;
$map = [];
$studyplans = studyplan::find_for_user($userid);
2023-08-24 23:02:41 +02:00
foreach ($studyplans as $studyplan) {
$map[] = $studyplan->user_model($userid);
}
return $map;
}
/************************
* *
* list_own_studyplans *
* *
************************/
2023-08-27 21:57:21 +02:00
/**
* Parameter description for webservice function list_own_studyplans
*/
2023-08-28 08:51:52 +02:00
public static function list_own_studyplans_parameters() : \external_function_parameters {
return new \external_function_parameters([]);
}
2023-08-27 21:57:21 +02:00
/**
* Return value description for webservice function list_own_studyplans
*/
2023-08-28 08:51:52 +02:00
public static function list_own_studyplans_returns() : \external_description {
return new \external_multiple_structure(
studyplan::simple_structure()
);
}
2023-08-27 23:27:07 +02:00
/**
* Return overview of the studyplans the current user is linked to
* @return array
*/
public static function list_own_studyplans() {
global $CFG, $DB, $USER;
$userid = $USER->id;
$list = [];
$studyplans = studyplan::find_for_user($userid);
2023-08-24 23:02:41 +02:00
foreach ($studyplans as $studyplan) {
2023-08-25 17:33:20 +02:00
$list[] = $studyplan->simple_model();
}
return $list;
}
/************************
* *
* get_own_studyplan *
* *
************************/
2023-08-27 21:57:21 +02:00
/**
* Parameter description for webservice function get_own_studyplan
*/
2023-08-28 08:51:52 +02:00
public static function get_own_studyplan_parameters() : \external_function_parameters {
return new \external_function_parameters( [
"id" => new \external_value(PARAM_INT, 'id of specific studyplan to provide', VALUE_DEFAULT),
] );
}
2023-08-27 21:57:21 +02:00
/**
* Return value description for webservice function get_own_studyplan
*/
2023-08-28 08:51:52 +02:00
public static function get_own_studyplan_returns() : \external_description {
return new \external_multiple_structure(
studyplan::user_structure()
);
}
2023-08-27 23:27:07 +02:00
/**
* Get all studyplans for current user or a specific one
* @param int $id Optional id of specific studyplan
* @return array
*/
2023-08-25 13:04:19 +02:00
public static function get_own_studyplan($id = null) {
2023-07-04 23:38:08 +02:00
global $USER;
// Validate this call in the system context.
\external_api::validate_context(\context_system::instance());
$userid = $USER->id;
$studyplans = studyplan::find_for_user($userid);
2023-08-24 23:02:41 +02:00
if (isset($id) && $id > 0) {
if (isset($studyplans[$id])) {
$studyplan = $studyplans[$id];
return [$studyplan->user_model($userid)];
} else {
return [];
}
2023-08-24 23:09:20 +02:00
} else {
$map = [];
2023-08-24 23:02:41 +02:00
foreach ($studyplans as $studyplan) {
$map[] = $studyplan->user_model($userid);
}
return $map;
}
}
/***************************
* *
* get_teaching_studyplans *
* *
***************************/
2023-08-27 21:57:21 +02:00
/**
* Parameter description for webservice function get_teaching_studyplans
*/
2023-08-28 08:51:52 +02:00
public static function get_teaching_studyplans_parameters() : \external_function_parameters {
return new \external_function_parameters( [
"id" => new \external_value(PARAM_INT, 'id of specific studyplan to provide', VALUE_DEFAULT),
] );
}
2023-08-27 21:57:21 +02:00
/**
* Return value description for webservice function get_teaching_studyplans
*/
2023-08-28 08:51:52 +02:00
public static function get_teaching_studyplans_returns() : \external_description {
return new \external_multiple_structure(
studyplan::editor_structure()
);
}
2023-08-27 23:27:07 +02:00
/**
* Get all or one studyplan the current user is teaching in
* @param int $id Optional specific id of studyplan
* @return array
*/
2023-08-25 13:04:19 +02:00
public static function get_teaching_studyplans($id = null) {
global $CFG, $DB, $USER;
$userid = $USER->id;
2023-08-24 23:02:41 +02:00
\external_api::validate_context(\context_system::instance());
$studyplans = teachingfinder::list_my_plans();
2023-08-24 23:02:41 +02:00
if (isset($id) && $id > 0) {
if (isset($studyplans[$id])) {
$studyplan = $studyplans[$id];
return [$studyplan->editor_model($userid)];
} else {
return [];
}
2023-08-24 23:09:20 +02:00
} else {
$map = [];
2023-08-24 23:02:41 +02:00
foreach ($studyplans as $studyplan) {
$map[] = $studyplan->editor_model($userid);
}
return $map;
}
}
2023-08-25 11:52:05 +02:00
}