lara-moodle-plugin/classes/get_parents.php
2024-12-12 09:41:03 +01:00

66 lines
2 KiB
PHP

<?php
defined('MOODLE_INTERNAL') || die();
require_once $CFG->dirroot . '/lib/externallib.php';
class local_lara_get_parents extends external_api
{
/**
* Parameter description for get_roles().
*
* @return external_function_parameters.
*/
public static function get_parents_parameters()
{
return new external_function_parameters([
'param' => new external_single_structure([
'roleid' => new external_value(PARAM_INT, 'id of the parent role'),
'userid' => new external_value(PARAM_INT, 'child id'),
]),
]);
}
/**
* Return roleinformation.
*
* This function returns roleid, rolename and roleshortname for all roles or for given roles.
*
* @param array $param
* @return array Array of arrays with role informations.
*/
public static function get_parents($param)
{
global $CFG, $DB;
require_once "$CFG->dirroot/group/lib.php";
$params = self::validate_parameters(self::get_parents_parameters(), ['param' => $param]);
$roleid = $params['param']['roleid'];
$userid = $params['param']['userid'];
$parents = [];
$contexts = $DB->get_records('context', ['instanceid' => (string) $userid, 'contextlevel' => '30']);
foreach ($contexts as $context) {
$dbParents = $DB->get_records('role_assignments', ['contextid' => (string) $context->id]);
foreach ($dbParents as $parent) {
$cl = new stdClass;
$cl->id = ($parent->userid);
array_push($parents, $cl);
}
}
return $parents;
}
/**
* Parameter description for create_sections().
*
* @return external_description
*/
public static function get_parents_returns()
{
return new external_multiple_structure(
new external_single_structure([
'id' => new external_value(PARAM_INT, 'role id'),
]));
}
}