Updated API functions to yied more useful results

This commit is contained in:
PMKuipers 2024-03-04 22:51:51 +01:00
parent 8382c4a117
commit d07e6170f3

View file

@ -23,6 +23,7 @@
namespace local_treestudyplan; namespace local_treestudyplan;
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
use external_single_structure;
use local_treestudyplan\local\helpers\webservicehelper; use local_treestudyplan\local\helpers\webservicehelper;
require_once($CFG->libdir.'/externallib.php'); require_once($CFG->libdir.'/externallib.php');
@ -1950,6 +1951,30 @@ class studyplanservice extends \external_api {
* * * *
***************************/ ***************************/
/**
* Enrol status structure including user info
*/
public static function student_enrol_status_structure($required = VALUE_REQUIRED) {
return new \external_single_structure([
'user' => associationservice::user_structure(),
'enrol' => studyline::enrol_info_structure(),
],'',$required);
}
/**
* Create student enrol status model
* @param int $userid Id of user
* @param studyline $line Studyline to retrieve enrol status for
*/
public static function student_enrol_status_model($userid,$line) {
global $DB;
$r = $DB->get_record('user',["id" => $userid]);
return [
'user' => associationservice::make_user_model($r),
'enrol' => $line->enrol_info_model($userid),
];
}
/** /**
* Parameter description for webservice function get_teaching_page * Parameter description for webservice function get_teaching_page
*/ */
@ -1964,7 +1989,7 @@ class studyplanservice extends \external_api {
* Return value description for webservice function get_teaching_page * Return value description for webservice function get_teaching_page
*/ */
public static function line_enrol_students_returns() : \external_description { public static function line_enrol_students_returns() : \external_description {
return success::structure(); return new \external_multiple_structure(self::student_enrol_status_structure());
} }
/** /**
@ -1984,13 +2009,15 @@ class studyplanservice extends \external_api {
$context = $o->context(); $context = $o->context();
webservicehelper::require_capabilities(self::CAP_VIEW, $context); webservicehelper::require_capabilities(self::CAP_VIEW, $context);
$list = [];
foreach ($users as $userid) { foreach ($users as $userid) {
if ($o->can_enrol($userid)) { if ($o->can_enrol($userid)) {
$o->enrol($userid); $o->enrol($userid);
$list[] = self::student_enrol_status_model($userid,$o);
} }
} }
return success::success()->model(); return $list;
} }
/*************************** /***************************
@ -2013,7 +2040,7 @@ class studyplanservice extends \external_api {
* Return value description for webservice function get_teaching_page * Return value description for webservice function get_teaching_page
*/ */
public static function line_unenrol_students_returns() : \external_description { public static function line_unenrol_students_returns() : \external_description {
return success::structure(); return new \external_multiple_structure(self::student_enrol_status_structure());
} }
/** /**
@ -2033,11 +2060,13 @@ class studyplanservice extends \external_api {
$context = $o->context(); $context = $o->context();
webservicehelper::require_capabilities('local/treestudyplan:lineunenrol', $context); webservicehelper::require_capabilities('local/treestudyplan:lineunenrol', $context);
$list = [];
foreach ($users as $userid) { foreach ($users as $userid) {
$o->unenrol($userid); $o->unenrol($userid);
$list[] = self::student_enrol_status_model($userid,$o);
} }
return success::success()->model(); return $list;
} }
/*************************** /***************************
@ -2060,7 +2089,7 @@ class studyplanservice extends \external_api {
*/ */
public static function list_line_enrolled_students_returns() : \external_description { public static function list_line_enrolled_students_returns() : \external_description {
return new \external_single_structure([ return new \external_single_structure([
"userinfo" => new \external_multiple_structure(studyline::enrol_info_structure()), "userinfo" => new \external_multiple_structure(self::student_enrol_status_structure()),
"can_unenrol" => new \external_value(PARAM_BOOL,"True if the requesting user can unenrol students"), "can_unenrol" => new \external_value(PARAM_BOOL,"True if the requesting user can unenrol students"),
]); ]);
} }
@ -2078,7 +2107,7 @@ class studyplanservice extends \external_api {
$list = []; $list = [];
$p = $o->studyplan(); $p = $o->studyplan();
foreach( $p->find_linked_userids() as $userid) { foreach( $p->find_linked_userids() as $userid) {
$list[] = $o->enrol_info_model($userid); $list[] = self::student_enrol_status_model($userid,$o);
} }
return [ return [