Skip to content

Commit

Permalink
Add role name and user fileds to role source #104 #105
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanheywood committed Oct 16, 2023
1 parent f17c02b commit ecf8076
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions classes/local/datasource/roles.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static function get_displayname(): string {
* @return \stdClass
*/
public function get_data(): \stdClass {
global $CFG;
global $CFG, $OUTPUT;

$data = new \stdClass();

Expand Down Expand Up @@ -91,8 +91,7 @@ public function get_data(): \stdClass {
require_once($CFG->dirroot .'/user/lib.php');

$context = \context_course::instance($this->cms->get('course'));

$rolesincontext = get_all_roles($context);
$rolesincontext = role_fix_names(get_all_roles(), $context, ROLENAME_ORIGINAL);

// Gets the roles assigned in the context, indexed by user ID, then assign ID.
$usersroles = get_users_roles($context, [], false);
Expand All @@ -114,6 +113,7 @@ public function get_data(): \stdClass {
$rolesdata[$shortname] = new \stdClass();
$rolesdata[$shortname]->shortname = $shortname;
$rolesdata[$shortname]->name = $record->coursealias ?: $record->name ?: $shortname;
$rolesdata[$shortname]->hasusers = false;
$rolesdata[$shortname]->users = [];
}
} else {
Expand All @@ -122,22 +122,39 @@ public function get_data(): \stdClass {
$rolesdata[$shortname] = new \stdClass();
$rolesdata[$shortname]->shortname = $shortname;
$rolesdata[$shortname]->name = ''; // Placeholder to ensure consistent ordering.
$rolesdata[$shortname]->hasusers = false;
$rolesdata[$shortname]->users = [];
}
// Add the course alias name.
foreach ($rolesincontext as $record) {
if (isset($rolesdata[$record->shortname])) {
$rolesdata[$record->shortname]->name = $record->coursealias ?: $record->name ?: $record->shortname;
$rolesdata[$record->shortname]->name = $record->coursealias ?: $record->localname ?: $record->shortname;
}
}
}

// Convert user indexed array to role indexed array.
foreach ($usersroles as $userid => $rolerecords) {

$user = $users[$userid];

$userobj = new \stdClass();
$userobj->id = $userid;
// Add user fields.
$userobj->fullname = $users[$userid]->fullname;
$userobj->fullname = $user->fullname;
$userobj->email = $user->email;
$userobj->username = $user->username;
$userobj->description = $user->description;

// TODO This is hacky, better to create a mustache helper instead so the template
// can dictate the avatar size.
$userobj->picture = $this->cms->issample ? '' : $OUTPUT->user_picture($user, [
'courseid' => SITEID, 'size' => 64]);
$userobj->picture100 = $this->cms->issample ? '' : $OUTPUT->user_picture($user, [
'courseid' => SITEID, 'size' => 100]);
$userobj->picture200 = $this->cms->issample ? '' : $OUTPUT->user_picture($user, [
'courseid' => SITEID, 'size' => 200]);

// TODO: In future, more fields may be needed.

if ($showuser === 'nest') {
Expand All @@ -157,6 +174,7 @@ public function get_data(): \stdClass {
}
if (!$alreadyadded || ($showuser === 'all')) {
$rolesdata[$record->shortname]->users[] = $userobj;
$rolesdata[$record->shortname]->hasusers = true;
}
$alreadyadded = true;
break;
Expand Down

0 comments on commit ecf8076

Please sign in to comment.