From 1b5910c4a5e9b7f3c4a3358357a7b43ddf2a3966 Mon Sep 17 00:00:00 2001 From: moodler Date: Sat, 26 Apr 2003 15:08:34 +0000 Subject: [PATCH] Trying a different tack for recent_activity since the old way was still producing too much data and overflowing PHP memory on busier sites (eg moodle.org). Now, there are more database queries, which is unfortunate, but the data is much more specific, and no sorting needs to be done, so this is a performance boost. I don't know how these will cancel out ... my guess is that very small sites may be very slightly slower on the course page, but that large sites will be much faster. Let's see. --- course/lib.php | 105 ++++++++++++++++++----------------------- mod/assignment/lib.php | 44 ++++++++--------- mod/forum/lib.php | 81 ++++++++++++++++--------------- mod/journal/lib.php | 52 ++++++++++---------- mod/quiz/lib.php | 13 ----- mod/survey/lib.php | 44 ++++++++--------- 6 files changed, 162 insertions(+), 177 deletions(-) diff --git a/course/lib.php b/course/lib.php index 5a68d46367..4f975d375e 100644 --- a/course/lib.php +++ b/course/lib.php @@ -322,21 +322,17 @@ function print_recent_activity($course) { $timestart = $timemaxrecent; } - if (! $logs = get_records_select("log", "time > '$timestart' AND ". - "course = '$course->id' AND ". - "module <> 'user' AND ". - "action <> 'view' AND ". - "action <> 'view all' ", "time ASC")) { - return; - } - // Firstly, have there been any new enrolments? $heading = false; $content = false; - foreach ($logs as $key => $log) { - if ($log->module == "course" and $log->action == "enrol") { + + $logs = get_records_select("log", "time > '$timestart' AND course = '$course->id' AND + module = 'course' AND action = 'enrol'", "time ASC"); + + if ($logs) { + foreach ($logs as $key => $log) { if (! $heading) { print_headline(get_string("newusers").":"); $heading = true; @@ -346,53 +342,48 @@ function print_recent_activity($course) { if (isstudent($course->id, $user->id)) { echo "

id&course=$course->id\">$user->firstname $user->lastname

"; } - unset($logs[$key]); // No further need for it } } - if (! $logs) { - return; - } + // Next, have there been any modifications to the course structure? - // Next, have there been any changes to the course structure? - - foreach ($logs as $key => $log) { - if ($log->module == "course") { - if ($log->action == "add mod" or $log->action == "update mod" or $log->action == "delete mod") { - $info = split(" ", $log->info); - $modname = get_field($info[0], "name", "id", $info[1]); - //Create a temp valid module structure (course,id) - $tempmod->course = $log->course; - $tempmod->id = $info[1]; - //Obtain the visible property from the instance - $modvisible = instance_is_visible($info[0],$tempmod); - - //Only if the mod is visible - if ($modvisible) { - switch ($log->action) { - case "add mod": - $stradded = get_string("added", "moodle", get_string("modulename", $info[0])); - $changelist["$log->info"] = array ("operation" => "add", "text" => "$stradded:
wwwroot/course/$log->url\">$modname"); - break; - case "update mod": - $strupdated = get_string("updated", "moodle", get_string("modulename", $info[0])); - if (empty($changelist["$log->info"])) { - $changelist["$log->info"] = array ("operation" => "update", "text" => "$strupdated:
wwwroot/course/$log->url\">$modname"); - } - break; - case "delete mod": - if (!empty($changelist["$log->info"]["operation"]) and - $changelist["$log->info"]["operation"] == "add") { - $changelist["$log->info"] = NULL; - } else { - $strdeleted = get_string("deletedactivity", "moodle", get_string("modulename", $info[0])); - $changelist["$log->info"] = array ("operation" => "delete", "text" => $strdeleted); - } - break; - } + $logs = get_records_select("log", "time > '$timestart' AND course = '$course->id' AND + module = 'course' AND action LIKE '% mod'", "time ASC"); + + if ($logs) { + foreach ($logs as $key => $log) { + $info = split(" ", $log->info); + $modname = get_field($info[0], "name", "id", $info[1]); + //Create a temp valid module structure (course,id) + $tempmod->course = $log->course; + $tempmod->id = $info[1]; + //Obtain the visible property from the instance + $modvisible = instance_is_visible($info[0],$tempmod); + + //Only if the mod is visible + if ($modvisible) { + switch ($log->action) { + case "add mod": + $stradded = get_string("added", "moodle", get_string("modulename", $info[0])); + $changelist["$log->info"] = array ("operation" => "add", "text" => "$stradded:
wwwroot/course/$log->url\">$modname"); + break; + case "update mod": + $strupdated = get_string("updated", "moodle", get_string("modulename", $info[0])); + if (empty($changelist["$log->info"])) { + $changelist["$log->info"] = array ("operation" => "update", "text" => "$strupdated:
wwwroot/course/$log->url\">$modname"); + } + break; + case "delete mod": + if (!empty($changelist["$log->info"]["operation"]) and + $changelist["$log->info"]["operation"] == "add") { + $changelist["$log->info"] = NULL; + } else { + $strdeleted = get_string("deletedactivity", "moodle", get_string("modulename", $info[0])); + $changelist["$log->info"] = array ("operation" => "delete", "text" => $strdeleted); + } + break; } } - unset($logs[$key]); // No further need for it } } @@ -411,19 +402,17 @@ function print_recent_activity($course) { } } - if (! $logs) { - return; - } - // Now display new things from each module $mods = get_list_of_plugins("mod"); - foreach ($mods as $mod) { + $isteacher = isteacher($course->id); + + foreach ($mods as $mod) { // Each module gets it's own logs and prints them include_once("$CFG->dirroot/mod/$mod/lib.php"); $print_recent_activity = $mod."_print_recent_activity"; - if ($logs and function_exists($print_recent_activity)) { - $modcontent = $print_recent_activity($logs, isteacher($course->id)); + if (function_exists($print_recent_activity)) { + $modcontent = $print_recent_activity($course, $isteacher, $timestart); if ($modcontent) { $content = true; } diff --git a/mod/assignment/lib.php b/mod/assignment/lib.php index 3f3031d7e6..100a3d2386 100644 --- a/mod/assignment/lib.php +++ b/mod/assignment/lib.php @@ -183,29 +183,31 @@ function assignment_cron () { return true; } -function assignment_print_recent_activity(&$logs, $isteacher=false) { +function assignment_print_recent_activity($course, $isteacher, $timestart) { global $CFG; $content = false; $assignments = NULL; - foreach ($logs as $key => $log) { - if ($log->module == "assignment") { - if ($log->action == "upload") { - //Create a temp valid module structure (course,id) - $tempmod->course = $log->course; - $tempmod->id = $log->info; - //Obtain the visible property from the instance - $modvisible = instance_is_visible($log->module,$tempmod); - - //Only if the mod is visible - if ($modvisible) { - $assignments[$log->info] = assignment_log_info($log); - $assignments[$log->info]->time = $log->time; - $assignments[$log->info]->url = $log->url; - } - } - unset($logs[$key]); // No longer need this record + if (!$logs = get_records_select("log", "time > '$timestart' AND ". + "course = '$course->id' AND ". + "module = 'assignment' AND ". + "action = 'upload' ", "time ASC")) { + return false; + } + + foreach ($logs as $log) { + //Create a temp valid module structure (course,id) + $tempmod->course = $log->course; + $tempmod->id = $log->info; + //Obtain the visible property from the instance + $modvisible = instance_is_visible($log->module,$tempmod); + + //Only if the mod is visible + if ($modvisible) { + $assignments[$log->info] = assignment_log_info($log); + $assignments[$log->info]->time = $log->time; + $assignments[$log->info]->url = $log->url; } } @@ -215,10 +217,10 @@ function assignment_print_recent_activity(&$logs, $isteacher=false) { print_headline(get_string("newsubmissions", "assignment").":"); foreach ($assignments as $assignment) { $date = userdate($assignment->time, $strftimerecent); - echo "

$date - $assignment->firstname $assignment->lastname
"; - echo "\"wwwroot/mod/assignment/$assignment->url\">"; + echo "

$date - $assignment->firstname $assignment->lastname
"; + echo "\"
wwwroot/mod/assignment/$assignment->url\">"; echo "$assignment->name"; - echo "\"

"; + echo "\"

"; } } diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 72ada4c436..d8a8c58a33 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -296,59 +296,62 @@ function forum_user_complete($course, $user, $mod, $forum) { } } -function forum_print_recent_activity(&$logs, $isteacher=false) { +function forum_print_recent_activity($course, $isteacher, $timestart) { global $CFG; $heading = false; $content = false; + if (!$logs = get_records_select("log", "time > '$timestart' AND ". + "course = '$course->id' AND ". + "module = 'forum' AND ". + "action LIKE 'add %' ", "time ASC")){ + return false; + } + $strftimerecent = get_string("strftimerecent"); - foreach ($logs as $key => $log) { - if ($log->module == "forum") { - if ($log->action == "add post" or $log->action == "add discussion") { - //Get post info, I'll need it later - $post = forum_get_post_from_log($log); - - //Create a temp valid module structure (course,id) - $tempmod->course = $log->course; - $tempmod->id = $post->forum; - //Obtain the visible property from the instance - $modvisible = instance_is_visible($log->module,$tempmod); - - //Only if the mod is visible - if ($modvisible) { - if ($post) { - $teacheronly = ""; - if ($forum = get_record("forum", "id", $post->forum) ) { - if ($forum->type == "teacher") { - if ($isteacher) { - $teacheronly = "class=\"teacheronly\""; - } else { - continue; - } - } - } - if (! $heading) { - print_headline(get_string("newforumposts", "forum").":"); - $heading = true; - $content = true; - } - $date = userdate($post->modified, $strftimerecent); - echo "

$date - $post->firstname $post->lastname
"; - echo "\"wwwroot/mod/forum/$log->url\">"; - if ($log->action == "add discussion") { - echo "$post->subject"; + foreach ($logs as $log) { + //Get post info, I'll need it later + $post = forum_get_post_from_log($log); + + //Create a temp valid module structure (course,id) + $tempmod->course = $log->course; + $tempmod->id = $post->forum; + //Obtain the visible property from the instance + $modvisible = instance_is_visible($log->module,$tempmod); + + //Only if the mod is visible + if ($modvisible) { + if ($post) { + $teacheronly = ""; + if ($forum = get_record("forum", "id", $post->forum) ) { + if ($forum->type == "teacher") { + if ($isteacher) { + $teacheronly = "class=\"teacheronly\""; } else { - echo "$post->subject"; + continue; } - echo "\"

"; } } + if (! $heading) { + print_headline(get_string("newforumposts", "forum").":"); + $heading = true; + $content = true; + } + $date = userdate($post->modified, $strftimerecent); + echo "

$date - $post->firstname $post->lastname
"; + echo "\"wwwroot/mod/forum/$log->url\">"; + if ($log->action == "add discussion") { + echo "$post->subject"; + } else { + echo "$post->subject"; + } + echo "\"

"; } - unset($logs[$key]); // No longer need this record } } + return $content; } diff --git a/mod/journal/lib.php b/mod/journal/lib.php index f932e27a68..0826c3b706 100644 --- a/mod/journal/lib.php +++ b/mod/journal/lib.php @@ -119,34 +119,36 @@ function journal_cron () { return true; } -function journal_print_recent_activity(&$logs, $isteacher=false) { +function journal_print_recent_activity($course, $isteacher, $timestart) { global $CFG; $content = false; $journals = NULL; - foreach ($logs as $key => $log) { - if ($log->module == "journal") { - if ($log->action == "add entry" or $log->action == "update entry") { - ///Get journal info. I'll need it later - $j_log_info = journal_log_info($log); - - //Create a temp valid module structure (course,id) - $tempmod->course = $log->course; - $tempmod->id = $j_log_info->id; - //Obtain the visible property from the instance - $modvisible = instance_is_visible($log->module,$tempmod); - - //Only if the mod is visible - if ($modvisible) { - if (!isset($journals[$log->info])) { - $journals[$log->info] = $j_log_info; - $journals[$log->info]->time = $log->time; - $journals[$log->info]->url = $log->url; - } - } + if (!$logs = get_records_select("log", "time > '$timestart' AND ". + "course = '$course->id' AND ". + "module = 'journal' AND ". + "(action = 'add entry' OR action = 'update entry')", "time ASC")){ + return false; + } + + foreach ($logs as $log) { + ///Get journal info. I'll need it later + $j_log_info = journal_log_info($log); + + //Create a temp valid module structure (course,id) + $tempmod->course = $log->course; + $tempmod->id = $j_log_info->id; + //Obtain the visible property from the instance + $modvisible = instance_is_visible($log->module,$tempmod); + + //Only if the mod is visible + if ($modvisible) { + if (!isset($journals[$log->info])) { + $journals[$log->info] = $j_log_info; + $journals[$log->info]->time = $log->time; + $journals[$log->info]->url = $log->url; } - unset($logs[$key]); // No longer need this record } } @@ -156,10 +158,10 @@ function journal_print_recent_activity(&$logs, $isteacher=false) { print_headline(get_string("newjournalentries", "journal").":"); foreach ($journals as $journal) { $date = userdate($journal->time, $strftimerecent); - echo "

$date - $journal->firstname $journal->lastname
"; - echo "\"wwwroot/mod/journal/$journal->url\">"; + echo "

$date - $journal->firstname $journal->lastname
"; + echo "\"
wwwroot/mod/journal/$journal->url\">"; echo "$journal->name"; - echo "\"

"; + echo "\"

"; } } diff --git a/mod/quiz/lib.php b/mod/quiz/lib.php index aefe826e69..effd244ef1 100644 --- a/mod/quiz/lib.php +++ b/mod/quiz/lib.php @@ -187,19 +187,6 @@ function quiz_user_complete($course, $user, $mod, $quiz) { return true; } -function quiz_print_recent_activity(&$logs, $isteacher=false) { -/// Given a list of logs, assumed to be those since the last login -/// this function prints a short list of changes related to this module -/// If isteacher is true then perhaps additional information is printed. -/// This function is called from course/lib.php: print_recent_activity() - - global $CFG; - - $content = ""; - - return $content; // True if anything was printed, otherwise false -} - function quiz_cron () { /// Function to be run periodically according to the moodle cron /// This function searches for things that need to be done, such diff --git a/mod/survey/lib.php b/mod/survey/lib.php index 406d51d2e2..119c7e4efa 100644 --- a/mod/survey/lib.php +++ b/mod/survey/lib.php @@ -100,29 +100,31 @@ function survey_user_complete($course, $user, $mod, $survey) { } } -function survey_print_recent_activity(&$logs, $isteacher=false) { +function survey_print_recent_activity($course, $isteacher, $timestart) { global $CFG; $content = false; $surveys = NULL; - foreach ($logs as $key => $log) { - if ($log->module == "survey") { - if ($log->action == "submit") { - //Create a temp valid module structure (course,id) - $tempmod->course = $log->course; - $tempmod->id = $log->info; - //Obtain the visible property from the instance - $modvisible = instance_is_visible($log->module,$tempmod); - - //Only if the mod is visible - if ($modvisible) { - $surveys[$log->id] = survey_log_info($log); - $surveys[$log->id]->time = $log->time; - $surveys[$log->id]->url = $log->url; - } - } - unset($logs[$key]); // No longer need this record + if (!$logs = get_records_select("log", "time > '$timestart' AND ". + "course = '$course->id' AND ". + "module = 'survey' AND ". + "action = 'submit' ", "time ASC")) { + return false; + } + + foreach ($logs as $log) { + //Create a temp valid module structure (course,id) + $tempmod->course = $log->course; + $tempmod->id = $log->info; + //Obtain the visible property from the instance + $modvisible = instance_is_visible($log->module,$tempmod); + + //Only if the mod is visible + if ($modvisible) { + $surveys[$log->id] = survey_log_info($log); + $surveys[$log->id]->time = $log->time; + $surveys[$log->id]->url = $log->url; } } @@ -132,10 +134,10 @@ function survey_print_recent_activity(&$logs, $isteacher=false) { print_headline(get_string("newsurveyresponses", "survey").":"); foreach ($surveys as $survey) { $date = userdate($survey->time, $strftimerecent); - echo "

$date - $survey->firstname $survey->lastname
"; - echo "\"wwwroot/mod/survey/$survey->url\">"; + echo "

$date - $survey->firstname $survey->lastname
"; + echo "\"
wwwroot/mod/survey/$survey->url\">"; echo "$survey->name"; - echo "\"

"; + echo "\"

"; } } -- 2.39.5