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 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
|
|
|
|
*/
|
|
|
|
|
2023-05-17 21:19:14 +02:00
|
|
|
namespace local_treestudyplan;
|
2023-08-25 12:04:27 +02:00
|
|
|
defined('MOODLE_INTERNAL') || die();
|
|
|
|
|
2023-05-17 21:19:14 +02:00
|
|
|
require_once($CFG->libdir.'/externallib.php');
|
|
|
|
|
|
|
|
use \local_treestudyplan\local\helpers\webservicehelper;
|
2024-03-08 11:54:39 +01:00
|
|
|
use local_treestudyplan\task\autocohortsync;
|
2023-05-17 21:19:14 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2023-05-17 21:19:14 +02:00
|
|
|
const CAP_VIEWOTHER = "local/treestudyplan:viewuserreports";
|
2024-03-09 00:11:42 +01:00
|
|
|
/**
|
|
|
|
* Capability required to be linked as coach to a studyplan
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const CAP_COACH = "local/treestudyplan:coach";
|
2023-05-17 21:19:14 +02:00
|
|
|
/************************
|
|
|
|
* *
|
|
|
|
* list_user_studyplans *
|
|
|
|
* *
|
|
|
|
************************/
|
2023-11-12 23:49:50 +01:00
|
|
|
|
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 {
|
2023-05-17 21:19:14 +02:00
|
|
|
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 11:26:14 +02:00
|
|
|
public static function list_user_studyplans_returns() : \external_description {
|
2023-05-17 21:19:14 +02:00
|
|
|
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) {
|
2023-05-17 21:19:14 +02:00
|
|
|
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-11-12 23:49:50 +01:00
|
|
|
$list[] = $studyplan->simple_model($userid);
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/************************
|
|
|
|
* *
|
2023-11-12 23:49:50 +01:00
|
|
|
* get_user_studyplan *
|
2023-05-17 21:19:14 +02:00
|
|
|
* *
|
|
|
|
************************/
|
|
|
|
|
2023-08-27 21:57:21 +02:00
|
|
|
/**
|
2023-11-12 23:49:50 +01:00
|
|
|
* Parameter description for webservice function get_user_studyplan
|
2023-08-27 21:57:21 +02:00
|
|
|
*/
|
2023-11-12 23:49:50 +01:00
|
|
|
public static function get_user_studyplan_parameters() : \external_function_parameters {
|
2023-05-17 21:19:14 +02:00
|
|
|
return new \external_function_parameters( [
|
|
|
|
"userid" => new \external_value(PARAM_INT, 'id of user'),
|
2023-11-12 23:49:50 +01:00
|
|
|
"studyplanid" => new \external_value(PARAM_INT, 'id of specific studyplan to provide'),
|
2023-05-17 21:19:14 +02:00
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
2023-08-27 21:57:21 +02:00
|
|
|
/**
|
2023-11-12 23:49:50 +01:00
|
|
|
* Return value description for webservice function get_user_studyplan
|
2023-08-27 21:57:21 +02:00
|
|
|
*/
|
2023-11-12 23:49:50 +01:00
|
|
|
public static function get_user_studyplan_returns() : \external_description {
|
|
|
|
return studyplan::user_structure();
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
2023-08-27 23:27:07 +02:00
|
|
|
|
|
|
|
/**
|
2023-11-12 23:49:50 +01:00
|
|
|
* Get a specific studyplan for a given user
|
2023-08-27 23:27:07 +02:00
|
|
|
* @param int $userid ID of user to check specific info for
|
2023-11-12 23:49:50 +01:00
|
|
|
* @param int $studyplanid ID of studyplan to view
|
2023-08-27 23:27:07 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
2023-11-12 23:49:50 +01:00
|
|
|
public static function get_user_studyplan($userid, $studyplanid) {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $CFG, $DB;
|
|
|
|
|
2023-11-12 23:49:50 +01:00
|
|
|
$studyplan = studyplan::find_by_id($studyplanid);
|
|
|
|
webservicehelper::require_capabilities(self::CAP_VIEWOTHER, $studyplan->context());
|
2023-05-17 21:19:14 +02:00
|
|
|
|
2023-11-12 23:49:50 +01:00
|
|
|
if ($studyplan->exist_for_user($userid)) {
|
2023-12-01 11:14:14 +01:00
|
|
|
|
|
|
|
$model = $studyplan->user_model($userid);
|
|
|
|
return $model;
|
2023-11-12 23:49:50 +01:00
|
|
|
} else {
|
|
|
|
return null;
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/************************
|
|
|
|
* *
|
2023-11-12 23:49:50 +01:00
|
|
|
* get_user_page *
|
2023-05-17 21:19:14 +02:00
|
|
|
* *
|
|
|
|
************************/
|
|
|
|
|
2023-08-27 21:57:21 +02:00
|
|
|
/**
|
2023-11-12 23:49:50 +01:00
|
|
|
* Parameter description for webservice function get_user_page
|
2023-08-27 21:57:21 +02:00
|
|
|
*/
|
2023-11-12 23:49:50 +01:00
|
|
|
public static function get_user_page_parameters() : \external_function_parameters {
|
2023-05-17 21:19:14 +02:00
|
|
|
return new \external_function_parameters( [
|
|
|
|
"userid" => new \external_value(PARAM_INT, 'id of user'),
|
2023-11-12 23:49:50 +01:00
|
|
|
"pageid" => new \external_value(PARAM_INT, 'id of specific studyplan to provide'),
|
2023-05-17 21:19:14 +02:00
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
2023-08-27 21:57:21 +02:00
|
|
|
/**
|
2023-11-12 23:49:50 +01:00
|
|
|
* Return value description for webservice function get_user_page
|
2023-08-27 21:57:21 +02:00
|
|
|
*/
|
2023-11-12 23:49:50 +01:00
|
|
|
public static function get_user_page_returns() : \external_description {
|
2023-05-17 21:19:14 +02:00
|
|
|
return studyplan::user_structure();
|
|
|
|
}
|
2023-08-27 23:27:07 +02:00
|
|
|
|
|
|
|
/**
|
2023-11-12 23:49:50 +01:00
|
|
|
* Get a specific studyplan page for a given user
|
2023-08-27 23:27:07 +02:00
|
|
|
* @param int $userid ID of user to check specific info for
|
2023-11-12 23:49:50 +01:00
|
|
|
* @param int $pageid ID of studyplan to view
|
2023-08-27 23:27:07 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
2023-11-12 23:49:50 +01:00
|
|
|
public static function get_user_page($userid, $pageid) {
|
2023-05-17 21:19:14 +02:00
|
|
|
global $CFG, $DB;
|
|
|
|
|
2023-11-12 23:49:50 +01:00
|
|
|
$studyplan = studyplan::find_by_id($pageid);
|
2023-08-24 23:02:41 +02:00
|
|
|
webservicehelper::require_capabilities(self::CAP_VIEWOTHER, $studyplan->context());
|
2023-05-17 21:19:14 +02:00
|
|
|
|
2023-11-12 23:49:50 +01:00
|
|
|
if ($studyplan->exist_for_user($userid)) {
|
2023-05-17 21:19:14 +02:00
|
|
|
return $studyplan->user_model($userid);
|
2023-08-24 23:09:20 +02:00
|
|
|
} else {
|
2023-05-17 21:19:14 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************
|
|
|
|
* *
|
2023-11-12 23:49:50 +01:00
|
|
|
* list_invited_studyplan *
|
2023-05-17 21:19:14 +02:00
|
|
|
* *
|
|
|
|
****************************/
|
|
|
|
|
2023-08-27 21:57:21 +02:00
|
|
|
/**
|
2023-11-12 23:49:50 +01:00
|
|
|
* Parameter description for webservice function list_invited_studyplan
|
2023-08-27 21:57:21 +02:00
|
|
|
*/
|
2023-11-12 23:49:50 +01:00
|
|
|
public static function list_invited_studyplans_parameters() : \external_function_parameters {
|
2023-05-17 21:19:14 +02:00
|
|
|
return new \external_function_parameters( [
|
|
|
|
"invitekey" => new \external_value(PARAM_RAW, 'invite key'),
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
2023-08-27 21:57:21 +02:00
|
|
|
/**
|
2023-11-12 23:49:50 +01:00
|
|
|
* Return value description for webservice function list_invited_studyplan
|
2023-08-27 21:57:21 +02:00
|
|
|
*/
|
2023-11-12 23:49:50 +01:00
|
|
|
public static function list_invited_studyplans_returns() : \external_description {
|
2023-05-17 21:19:14 +02:00
|
|
|
return new \external_multiple_structure(
|
2023-11-12 23:49:50 +01:00
|
|
|
studyplan::simple_structure()
|
2023-05-17 21:19:14 +02:00
|
|
|
);
|
|
|
|
}
|
2023-08-27 23:27:07 +02:00
|
|
|
|
|
|
|
/**
|
2023-11-12 23:49:50 +01:00
|
|
|
* List available studyplans based on invite key
|
2023-08-27 23:27:07 +02:00
|
|
|
* @param int $invitekey Invitation key
|
|
|
|
* @return array
|
|
|
|
*/
|
2023-11-12 23:49:50 +01:00
|
|
|
public static function list_invited_studyplans($invitekey) {
|
|
|
|
global $DB;
|
2023-12-11 23:57:56 +01:00
|
|
|
// First check if studyplan sharing is enabled.
|
|
|
|
if (!get_config("local_treestudyplan","enableplansharing")) {
|
|
|
|
return [];
|
|
|
|
}
|
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-05-17 21:19:14 +02:00
|
|
|
|
2023-08-24 23:02:41 +02:00
|
|
|
if (empty($invite)) {
|
2023-05-17 21:19:14 +02:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2023-11-23 07:44:04 +01:00
|
|
|
// Do not validate the context in this instance to avoid requiring logins when this is not wanted
|
2023-07-04 23:38:08 +02:00
|
|
|
|
2023-05-17 21:19:14 +02:00
|
|
|
$userid = $invite->user_id;
|
|
|
|
|
2023-11-12 23:49:50 +01:00
|
|
|
$list = [];
|
2023-05-17 21:19:14 +02:00
|
|
|
$studyplans = studyplan::find_for_user($userid);
|
2023-08-24 23:02:41 +02:00
|
|
|
foreach ($studyplans as $studyplan) {
|
2023-11-13 22:18:28 +01:00
|
|
|
$list[] = $studyplan->simple_model($userid);
|
2023-11-12 23:49:50 +01:00
|
|
|
}
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************
|
|
|
|
* *
|
|
|
|
* get_invited_studyplan *
|
|
|
|
* *
|
|
|
|
****************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parameter description for webservice function get_invited_studyplan
|
|
|
|
*/
|
|
|
|
public static function get_invited_studyplan_parameters() : \external_function_parameters {
|
|
|
|
return new \external_function_parameters( [
|
|
|
|
"invitekey" => new \external_value(PARAM_RAW, 'invite key'),
|
|
|
|
"studyplanid" => new \external_value(PARAM_INT, 'studyplan_id'),
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return value description for webservice function get_invited_studyplan
|
|
|
|
*/
|
|
|
|
public static function get_invited_studyplan_returns() : \external_description {
|
|
|
|
return studyplan::user_structure();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get studyplan based on invite key and studyplan id
|
|
|
|
* @param string $invitekey Invitation key
|
|
|
|
* @param int $studyplanid ID of the studyplan to retrieve
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function get_invited_studyplan($invitekey,$studyplanid) {
|
|
|
|
global $DB;
|
2023-12-11 23:57:56 +01:00
|
|
|
// First check if studyplan sharing is enabled.
|
|
|
|
if (!get_config("local_treestudyplan","enableplansharing")) {
|
|
|
|
return [];
|
|
|
|
}
|
2023-11-12 23:49:50 +01:00
|
|
|
$invite = $DB->get_record_select(
|
|
|
|
"local_treestudyplan_invit",
|
|
|
|
$DB->sql_compare_text("invitekey"). " = " . $DB->sql_compare_text(":invitekey"),
|
|
|
|
['invitekey' => $invitekey]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (empty($invite)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2023-11-23 07:44:04 +01:00
|
|
|
// Do not validate the context in this instance to avoid requiring logins when this is not wanted
|
2023-11-12 23:49:50 +01:00
|
|
|
|
|
|
|
$studyplan = studyplan::find_by_id($studyplanid);
|
|
|
|
$userid = $invite->user_id;
|
|
|
|
|
|
|
|
if ($studyplan->exist_for_user($userid)) {
|
|
|
|
return $studyplan->user_model($userid);
|
|
|
|
} else {
|
|
|
|
throw new \moodle_exception("Invitation's user is not linked to this studyplan");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************
|
|
|
|
* *
|
|
|
|
* get_invited_page *
|
|
|
|
* *
|
|
|
|
****************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parameter description for webservice function get_invited_studyplan
|
|
|
|
*/
|
|
|
|
public static function get_invited_page_parameters() : \external_function_parameters {
|
|
|
|
return new \external_function_parameters( [
|
|
|
|
"invitekey" => new \external_value(PARAM_RAW, 'invite key'),
|
|
|
|
"pageid" => new \external_value(PARAM_INT, 'studyplan page id'),
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return value description for webservice function get_invited_studyplan
|
|
|
|
*/
|
|
|
|
public static function get_invited_page_returns() : \external_description {
|
|
|
|
return studyplanpage::user_structure();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get studyplan based on invite key and studyplan id
|
|
|
|
* @param string $invitekey Invitation key
|
|
|
|
* @param int $pageid ID of the studyplan to retrieve
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function get_invited_page($invitekey,$pageid) {
|
|
|
|
global $DB;
|
2023-12-11 23:57:56 +01:00
|
|
|
// First check if studyplan sharing is enabled.
|
|
|
|
if (!get_config("local_treestudyplan","enableplansharing")) {
|
|
|
|
return [];
|
|
|
|
}
|
2023-11-12 23:49:50 +01:00
|
|
|
$invite = $DB->get_record_select(
|
|
|
|
"local_treestudyplan_invit",
|
|
|
|
$DB->sql_compare_text("invitekey"). " = " . $DB->sql_compare_text(":invitekey"),
|
|
|
|
['invitekey' => $invitekey]
|
|
|
|
);
|
|
|
|
|
|
|
|
if (empty($invite)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2023-11-23 07:44:04 +01:00
|
|
|
// Do not validate the context in this instance to avoid requiring logins when this is not wanted
|
2023-11-12 23:49:50 +01:00
|
|
|
|
|
|
|
$page = studyplanpage::find_by_id($pageid);
|
|
|
|
$studyplan = $page->studyplan();
|
|
|
|
$userid = $invite->user_id;
|
|
|
|
|
|
|
|
if ($studyplan->exist_for_user($userid)) {
|
|
|
|
return $page->user_model($userid);
|
|
|
|
} else {
|
|
|
|
throw new \moodle_exception("Invitation's user is not linked to this studyplan");
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/************************
|
|
|
|
* *
|
|
|
|
* 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 {
|
2023-05-17 21:19:14 +02:00
|
|
|
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 11:26:14 +02:00
|
|
|
public static function list_own_studyplans_returns() : \external_description {
|
2023-05-17 21:19:14 +02:00
|
|
|
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() {
|
2023-05-17 21:19:14 +02:00
|
|
|
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-11-12 23:49:50 +01:00
|
|
|
$list[] = $studyplan->simple_model($userid);
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
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 {
|
2023-05-17 21:19:14 +02:00
|
|
|
return new \external_function_parameters( [
|
2023-11-12 23:49:50 +01:00
|
|
|
"studyplanid" => new \external_value(PARAM_INT, 'id of studyplan to retrieve'),
|
2023-05-17 21:19:14 +02:00
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
2023-08-27 21:57:21 +02:00
|
|
|
/**
|
|
|
|
* Return value description for webservice function get_own_studyplan
|
|
|
|
*/
|
2023-08-28 11:26:14 +02:00
|
|
|
public static function get_own_studyplan_returns() : \external_description {
|
2023-11-12 23:49:50 +01:00
|
|
|
return studyplan::user_structure();
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-27 23:27:07 +02:00
|
|
|
/**
|
2023-11-12 23:49:50 +01:00
|
|
|
* Get a studyplans for the current user
|
|
|
|
* @param int $studyplanid Id of specific studyplan
|
2023-08-27 23:27:07 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
2023-11-12 23:49:50 +01:00
|
|
|
public static function get_own_studyplan($studyplanid) {
|
2023-07-04 23:38:08 +02:00
|
|
|
global $USER;
|
|
|
|
|
|
|
|
// Validate this call in the system context.
|
|
|
|
\external_api::validate_context(\context_system::instance());
|
|
|
|
|
2023-05-17 21:19:14 +02:00
|
|
|
$userid = $USER->id;
|
|
|
|
|
2023-11-12 23:49:50 +01:00
|
|
|
$studyplan = studyplan::find_by_id($studyplanid);
|
2023-05-17 21:19:14 +02:00
|
|
|
|
2023-11-12 23:49:50 +01:00
|
|
|
if ($studyplan->exist_for_user($userid)) {
|
2023-12-01 11:14:14 +01:00
|
|
|
$model = $studyplan->user_model($userid);
|
|
|
|
return $model;
|
2023-08-24 23:09:20 +02:00
|
|
|
} else {
|
2023-11-12 23:49:50 +01:00
|
|
|
throw new \moodle_exception("You do not have access to this studyplan");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/************************
|
|
|
|
* *
|
|
|
|
* get_own_page *
|
|
|
|
* *
|
|
|
|
************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parameter description for webservice function get_own_page
|
|
|
|
*/
|
|
|
|
public static function get_own_page_parameters() : \external_function_parameters {
|
|
|
|
return new \external_function_parameters( [
|
|
|
|
"pageid" => new \external_value(PARAM_INT, 'id of studyplan to retrieve'),
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return value description for webservice function get_own_page
|
|
|
|
*/
|
|
|
|
public static function get_own_page_returns() : \external_description {
|
|
|
|
return studyplan::user_structure();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get studyplanpage for current user
|
|
|
|
* @param int $pagid ID of specific studyplan page
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function get_own_page($pageid) {
|
|
|
|
global $USER;
|
|
|
|
|
|
|
|
// Validate this call in the system context.
|
|
|
|
\external_api::validate_context(\context_system::instance());
|
|
|
|
|
|
|
|
$userid = $USER->id;
|
|
|
|
|
|
|
|
$page = studyplanpage::find_by_id($pageid);
|
|
|
|
$studyplan = $page->studyplan();
|
|
|
|
|
|
|
|
if ($studyplan->exist_for_user($userid)) {
|
|
|
|
return $page->user_model($userid);
|
|
|
|
} else {
|
|
|
|
throw new \moodle_exception("You do not have access to this studyplan page");
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************
|
|
|
|
* *
|
2023-11-12 23:49:50 +01:00
|
|
|
* list_teaching_studyplans *
|
2023-05-17 21:19:14 +02:00
|
|
|
* *
|
|
|
|
***************************/
|
|
|
|
|
2023-08-27 21:57:21 +02:00
|
|
|
/**
|
2023-11-12 23:49:50 +01:00
|
|
|
* Parameter description for webservice function list_teaching_studyplans
|
2023-08-27 21:57:21 +02:00
|
|
|
*/
|
2023-11-12 23:49:50 +01:00
|
|
|
public static function list_teaching_studyplans_parameters() : \external_function_parameters {
|
|
|
|
return new \external_function_parameters( [] );
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
2023-08-27 21:57:21 +02:00
|
|
|
/**
|
2023-11-12 23:49:50 +01:00
|
|
|
* Return value description for webservice function list_teaching_studyplans
|
2023-08-27 21:57:21 +02:00
|
|
|
*/
|
2023-11-12 23:49:50 +01:00
|
|
|
public static function list_teaching_studyplans_returns() : \external_description {
|
2023-05-17 21:19:14 +02:00
|
|
|
return new \external_multiple_structure(
|
2023-11-12 23:49:50 +01:00
|
|
|
studyplan::simple_structure()
|
2023-05-17 21:19:14 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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-11-12 23:49:50 +01:00
|
|
|
public static function list_teaching_studyplans() {
|
|
|
|
global $USER;
|
2023-05-17 21:19:14 +02:00
|
|
|
$userid = $USER->id;
|
2023-08-24 23:02:41 +02:00
|
|
|
|
|
|
|
\external_api::validate_context(\context_system::instance());
|
2023-06-30 13:12:19 +02:00
|
|
|
$studyplans = teachingfinder::list_my_plans();
|
2023-05-17 21:19:14 +02:00
|
|
|
|
2023-11-12 23:49:50 +01:00
|
|
|
$list = [];
|
|
|
|
foreach ($studyplans as $studyplan) {
|
|
|
|
$list[] = $studyplan->simple_model($userid);
|
|
|
|
}
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************
|
2024-03-09 00:11:42 +01:00
|
|
|
* *
|
|
|
|
* list_coaching_studyplans *
|
|
|
|
* *
|
|
|
|
***************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parameter description for webservice function list_teaching_studyplans
|
|
|
|
*/
|
|
|
|
public static function list_coaching_studyplans_parameters() : \external_function_parameters {
|
|
|
|
return new \external_function_parameters( [] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return value description for webservice function list_teaching_studyplans
|
|
|
|
*/
|
|
|
|
public static function list_coaching_studyplans_returns() : \external_description {
|
|
|
|
return new \external_multiple_structure(
|
|
|
|
studyplan::simple_structure()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all or one studyplan the current user is teaching in
|
|
|
|
* @param int $id Optional specific id of studyplan
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function list_coaching_studyplans() {
|
|
|
|
global $USER, $DB;
|
|
|
|
$userid = $USER->id;
|
|
|
|
|
|
|
|
\external_api::validate_context(\context_system::instance());
|
|
|
|
|
|
|
|
$studyplanids = $DB->get_records("local_treestudyplan_coach",["user_id" => $userid]);
|
|
|
|
|
|
|
|
$list = [];
|
|
|
|
foreach ($studyplanids as $id) {
|
|
|
|
$studyplan = studyplan::find_by_id($id);
|
|
|
|
if (has_capability(self::CAP_COACH,$studyplan->context(),$USER)) {
|
|
|
|
$list[] = $studyplan->simple_model($userid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************
|
2023-11-12 23:49:50 +01:00
|
|
|
* *
|
|
|
|
* get_teaching_studyplan *
|
|
|
|
* *
|
|
|
|
***************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parameter description for webservice function get_teaching_studyplan
|
|
|
|
*/
|
|
|
|
public static function get_teaching_studyplan_parameters() : \external_function_parameters {
|
|
|
|
return new \external_function_parameters( [
|
|
|
|
"studyplanid" => new \external_value(PARAM_INT, 'id of specific studyplan to provide', VALUE_DEFAULT),
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return value description for webservice function get_teaching_studyplan
|
|
|
|
*/
|
|
|
|
public static function get_teaching_studyplan_returns() : \external_description {
|
|
|
|
return studyplan::editor_structure();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all or one studyplan the current user is teaching in
|
|
|
|
* @param int $studyplanid ID of studyplan to retrieve
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function get_teaching_studyplan($studyplanid) {
|
|
|
|
global $USER;
|
|
|
|
$userid = $USER->id;
|
|
|
|
|
|
|
|
\external_api::validate_context(\context_system::instance());
|
|
|
|
$studyplan = studyplan::find_by_id($studyplanid);
|
|
|
|
|
|
|
|
if (teachingfinder::am_teaching_studyplan($studyplan)) {
|
|
|
|
return $studyplan->editor_model();
|
2023-08-24 23:09:20 +02:00
|
|
|
} else {
|
2023-11-12 23:49:50 +01:00
|
|
|
throw new \moodle_exception("You are not teaching in this studyplan");
|
2023-05-17 21:19:14 +02:00
|
|
|
}
|
|
|
|
}
|
2023-11-12 23:49:50 +01:00
|
|
|
|
|
|
|
/***************************
|
|
|
|
* *
|
|
|
|
* get_teaching_page *
|
|
|
|
* *
|
|
|
|
***************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parameter description for webservice function get_teaching_page
|
|
|
|
*/
|
|
|
|
public static function get_teaching_page_parameters() : \external_function_parameters {
|
|
|
|
return new \external_function_parameters( [
|
|
|
|
"pageid" => new \external_value(PARAM_INT, 'id of specific studyplan page to provide', VALUE_DEFAULT),
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return value description for webservice function get_teaching_page
|
|
|
|
*/
|
|
|
|
public static function get_teaching_page_returns() : \external_description {
|
|
|
|
return studyplanpage::editor_structure();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all or one studyplan the current user is teaching in
|
|
|
|
* @param int $studyplanid ID of studyplan to retrieve
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function get_teaching_page($pageid) {
|
|
|
|
global $USER;
|
|
|
|
$userid = $USER->id;
|
|
|
|
|
|
|
|
\external_api::validate_context(\context_system::instance());
|
|
|
|
$page = studyplanpage::find_by_id($pageid);
|
|
|
|
$studyplan = $page->studyplan();
|
|
|
|
|
|
|
|
if (teachingfinder::am_teaching_studyplan($studyplan)) {
|
|
|
|
return $page->editor_model();
|
|
|
|
} else {
|
|
|
|
throw new \moodle_exception("You are not teaching in this studyplan");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-04 22:39:29 +01:00
|
|
|
/***************************
|
|
|
|
* *
|
|
|
|
* line_enrol_self *
|
|
|
|
* *
|
|
|
|
***************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parameter description for webservice function get_teaching_page
|
|
|
|
*/
|
|
|
|
public static function line_enrol_self_parameters() : \external_function_parameters {
|
|
|
|
return new \external_function_parameters( [
|
|
|
|
"id" => new \external_value(PARAM_INT, 'id of specific studyline to enrol into'),
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return value description for webservice function get_teaching_page
|
|
|
|
*/
|
|
|
|
public static function line_enrol_self_returns() : \external_description {
|
|
|
|
return studyline::enrol_info_structure();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all or one studyplan the current user is teaching in
|
|
|
|
* @param int $studyplanid ID of studyplan to retrieve
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function line_enrol_self($id) {
|
|
|
|
global $USER;
|
|
|
|
$userid = $USER->id;
|
|
|
|
|
|
|
|
$o = studyline::find_by_id($id);
|
|
|
|
if ($o->can_enrol($userid)) {
|
|
|
|
$o->enrol($userid);
|
|
|
|
}
|
|
|
|
|
2024-03-08 11:54:39 +01:00
|
|
|
// Trigger immediate cohort synchronization for this line only
|
|
|
|
autocohortsync::syncline($o);
|
|
|
|
|
2024-03-04 22:39:29 +01:00
|
|
|
return $o->enrol_info_model($userid);
|
|
|
|
}
|
|
|
|
|
2023-08-25 11:52:05 +02:00
|
|
|
}
|