From f871698811303a6fa13208b4264971ca5cdbfc0e Mon Sep 17 00:00:00 2001 From: mjollnir_ Date: Mon, 5 Sep 2005 01:31:08 +0000 Subject: [PATCH] Reworked my moodle overview api to be more efficient. Note that this changes how mod_print_overview is called --- course/lib.php | 12 +++++----- mod/forum/lib.php | 61 ++++++++++++++++++++++++++++------------------- 2 files changed, 43 insertions(+), 30 deletions(-) diff --git a/course/lib.php b/course/lib.php index 2dfe9a8ae42..76cf866596e 100644 --- a/course/lib.php +++ b/course/lib.php @@ -651,13 +651,13 @@ function print_overview($course) { print_simple_box_start("center", '400', '', 5, "coursebox"); print_heading(''.$course->fullname.''); - if ($mods = get_course_mods($course->id)) { - foreach ($mods as $mod) { - if (file_exists(dirname(dirname(__FILE__)).'/mod/'.$mod->modname.'/lib.php')) { - require_once(dirname(dirname(__FILE__)).'/mod/'.$mod->modname.'/lib.php'); - $fname = $mod->modname.'_print_overview'; + if ($modules = get_records('modules')) { + foreach ($modules as $mod) { + if (file_exists(dirname(dirname(__FILE__)).'/mod/'.$mod->name.'/lib.php')) { + require_once(dirname(dirname(__FILE__)).'/mod/'.$mod->name.'/lib.php'); + $fname = $mod->name.'_print_overview'; if (function_exists($fname)) { - $fname($course,$mod,$lastaccess); + $fname($course,$lastaccess); } } } diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 9e8dda2ab31..5f315107ab1 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -734,35 +734,48 @@ function forum_user_complete($course, $user, $mod, $forum) { } } -function forum_print_overview($course, $cm,$lastaccess) { +function forum_print_overview($course,$lastaccess) { global $USER, $CFG; - $forum = get_record("forum","id","$cm->instance"); - $str = '' - .get_string('forum','forum').': '.$forum->name.'
'; - if ($numnew = count_records_select("log","time > $lastaccess AND " - ." course = $course->id AND " - ." module = 'forum' AND cmid = $cm->id " - ." AND action LIKE 'add %' AND userid != $USER->id")) { - $str .= get_string('overviewnumpostssince','forum',$numnew)."
"; - $p = 1; + + if (!$forums = get_all_instances_in_course("forum", $course)) { + return; } - if ($CFG->forum_trackreadposts && forum_tp_can_track_forums($forum)) { - if (isset($forum->groupmode)) { - $groupmode = groupmode($course, $forum); /// Can do this because forum->groupmode is defined - } else { - $groupmode = NOGROUPS; + // get all forum logs in ONE query (much better!) + $new = get_records_sql("SELECT instance,cmid,COUNT(l.id) as count FROM {$CFG->prefix}log l JOIN {$CFG->prefix}course_modules cm ON cm.id = cmid WHERE time > $lastaccess AND l.course = ".$course->id + ." AND l.module = 'forum' AND action LIKE 'add%' AND userid != ".$USER->id." GROUP BY cmid,instance"); + foreach ($forums as $forum) { + $count = 0; + $unread = 0; + $showunread = false; + // either we have something from logs, or trackposts, or nothing. + if (array_key_exists($forum->id, $new) && !empty($new[$forum->id])) { + $count = $new[$forum->id]->count; } - $groupid = ($groupmode==SEPARATEGROUPS && !isteacheredit($course->id)) ? $currentgroup : false; - $unread = forum_tp_count_forum_posts($forum->id, $groupid) - - forum_tp_count_forum_read_records($USER->id, $forum->id, $groupid); - if ($unread > 0) { - $a->unread = $unread; - $str .= get_string('overviewnumunread','forum',$unread).'
'; - $p = 1; + if (forum_tp_can_track_forums($forum)) { + $showunread = true; + if (isset($forum->groupmode)) { + $groupmode = groupmode($course, $forum); /// Can do this because forum->groupmode is defined + } else { + $groupmode = NOGROUPS; + } + $groupid = ($groupmode==SEPARATEGROUPS && !isteacheredit($course->id)) ? $currentgroup : false; + $unread = forum_tp_count_forum_posts($forum->id, $groupid) - + forum_tp_count_forum_read_records($USER->id, $forum->id, $groupid); } - } - if (!empty($p)) { + if ($count > 0 || $unread > 0) { + $str .= '' + .get_string('forum','forum').': '.$forum->name.'
'; + $str .= get_string('overviewnumpostssince','forum',$count)."
"; + if (!empty($showunread)) { + $str .= get_string('overviewnumunread','forum',$unread).'
'; + } + } + } + print_heading(get_string("modulenameplural", "forum")); + if (!empty($str)) { echo $str; + } else { + echo get_string('nothingnew'); } }