Permalink
Browse files

MDL-16300: use cache to minimize use of get_users_by_capability()

  • Loading branch information...
1 parent a4b1ac3 commit 87ca8583aad3dfd1a01dd98f21d321f9579ac08d gbateson committed Oct 30, 2008
Showing with 22 additions and 9 deletions.
  1. +22 −9 mod/hotpot/report.php
View
@@ -124,7 +124,7 @@
case 'allparticipants':
// anyone currently allowed to attempt this HotPot
- if ($records = get_users_by_capability($modulecontext, 'mod/hotpot:attempt', 'u.id,u.id', 'u.id')) {
+ if ($records = hotpot_get_users_by_capability($modulecontext, 'mod/hotpot:attempt')) {
foreach ($records as $record) {
$users[$record->id] = 1; // "1" means user is allowed to do this HotPot
}
@@ -134,13 +134,14 @@
case 'existingstudents':
// anyone currently allowed to attempt this HotPot who is not a teacher
- $teachers = get_users_by_capability($modulecontext, 'mod/hotpot:viewreport', 'u.id,u.id', 'u.id');
- if ($records = get_users_by_capability($modulecontext, 'mod/hotpot:attempt', 'u.id,u.id', 'u.id')) {
+ $teachers = hotpot_get_users_by_capability($modulecontext, 'mod/hotpot:viewreport');
+ if ($records = hotpot_get_users_by_capability($modulecontext, 'mod/hotpot:attempt')) {
foreach ($records as $record) {
if (empty($teachers[$record->id])) {
$users[$record->id] = 1;
}
}
+ unset($records);
}
break;
@@ -159,6 +160,7 @@
}
if (empty($user_ids)) {
print_heading(get_string('nousersyet'));
+ print_footer($course);
exit;
}
@@ -250,6 +252,7 @@
// stop now if no attempts were found
if (empty($attempts)) {
print_heading(get_string('noattemptstoshow','quiz'));
+ print_footer($course);
exit;
}
@@ -498,15 +501,17 @@ function hotpot_print_report_selector(&$course, &$hotpot, &$formdata) {
$cm = get_coursemodule_from_instance('hotpot', $hotpot->id);
$modulecontext = get_context_instance(CONTEXT_MODULE, $cm->id);
- // get teachers enrolled students
- $teachers = get_users_by_capability($modulecontext, 'mod/hotpot:viewreport', 'u.id,u.firstname,u.lastname', 'u.lastname');
- $students = get_users_by_capability($modulecontext, 'mod/hotpot:attempt', 'u.id,u.firstname,u.lastname', 'u.lastname');
+ $teachers = hotpot_get_users_by_capability($modulecontext, 'mod/hotpot:viewreport');
+ $students = hotpot_get_users_by_capability($modulecontext, 'mod/hotpot:attempt');
// current students
- if (!empty($students)) {
+ if ($students = hotpot_get_users_by_capability($modulecontext, 'mod/hotpot:attempt')) {
$firsttime = true;
- foreach ($students as $user) {
- if (isset($users[$user->id])) {
+ foreach ($users as $user) {
+ if (array_key_exists($user->id, $teachers)) {
+ continue; // skip teachers
+ }
+ if (array_key_exists($user->id, $students)) {
if ($firsttime) {
$firsttime = false; // so we only do this once
$menus['reportusers']['existingstudents'] = get_string('existingstudents');
@@ -516,6 +521,7 @@ function hotpot_print_report_selector(&$course, &$hotpot, &$formdata) {
unset($users[$user->id]);
}
}
+ unset($students);
}
// others (former students, teachers, admins, course creators)
if (!empty($users)) {
@@ -672,4 +678,11 @@ function hotpot_get_records_groupby($function, $fieldnames, $table, $select, $gr
return $records;
}
+function hotpot_get_users_by_capability(&$modulecontext, $capability) {
+ static $users = array();
+ if (! array_key_exists($capability, $users)) {
+ $users[$capability] = get_users_by_capability($modulecontext, $capability, 'u.id,u.id', 'u.id');
+ }
+ return $users[$capability];
+}
?>

0 comments on commit 87ca858

Please sign in to comment.