diff --git a/classes/local/datasource/roles.php b/classes/local/datasource/roles.php index 82eb9c6..f4d1ca9 100644 --- a/classes/local/datasource/roles.php +++ b/classes/local/datasource/roles.php @@ -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(); @@ -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); @@ -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 { @@ -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') { @@ -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;