From 69d79bc31f6aaa8842104bc0e27c402c72323232 Mon Sep 17 00:00:00 2001 From: moodler Date: Sat, 31 Jan 2004 14:47:57 +0000 Subject: [PATCH] OK, some big improvements to the logs. Logs now include a field called modid which contains the coursemodule id. This makes it now possible to - see complete logs per-activity - do backup/restore of logs The upgrade process will currently try to scan all the old logs and rebuild this field based on available data (especially forums). STILL TO DO: alter all the non-forum modules to send the coursemodule id --- course/lib.php | 19 +++++++++++-------- course/log.php | 24 +++++------------------- lib/datalib.php | 33 ++++++++++----------------------- lib/db/mysql.php | 26 ++++++++++++++++++++++++++ lib/db/mysql.sql | 1 + lib/db/postgres7.php | 26 ++++++++++++++++++++++++++ lib/db/postgres7.sql | 1 + lib/weblib.php | 8 ++++---- mod/forum/discuss.php | 18 ++++++++++++------ mod/forum/lib.php | 14 +++++--------- mod/forum/post.php | 30 +++++++++++++++++++++++++----- mod/forum/subscribe.php | 6 ++++-- mod/forum/subscribers.php | 32 ++++++++++++++++++-------------- mod/forum/version.php | 4 ++-- mod/forum/view.php | 3 ++- version.php | 2 +- 16 files changed, 153 insertions(+), 94 deletions(-) diff --git a/course/lib.php b/course/lib.php index dd66733012..4953e0589a 100644 --- a/course/lib.php +++ b/course/lib.php @@ -22,7 +22,7 @@ define("FRONTPAGECATEGORYNAMES", 2); function print_log_selector_form($course, $selecteduser=0, $selecteddate="today", - $mod="", $modpage="", $modid=0) { + $mod="", $modid=0, $modaction="") { global $USER, $CFG; @@ -84,10 +84,10 @@ function print_log_selector_form($course, $selecteduser=0, $selecteddate="today" if (!$mod->visible) { $mod->name = "(".$mod->name.")"; } - $activities["$mod->mod/view.php?id=$mod->cm"] = $mod->name; + $activities["$mod->cm"] = $mod->name; if ($mod->cm == $modid) { - $selectedactivity = "$mod->mod/view.php?id=$mod->cm"; + $selectedactivity = "$mod->cm"; } } } @@ -135,7 +135,7 @@ function print_log_selector_form($course, $selecteduser=0, $selecteddate="today" } choose_from_menu ($users, "user", $selecteduser, get_string("allparticipants") ); choose_from_menu ($dates, "date", $selecteddate, get_string("alldays")); - choose_from_menu ($activities, "url", $selectedactivity, get_string("allactivities"), "", ""); + choose_from_menu ($activities, "modid", $selectedactivity, get_string("allactivities"), "", ""); echo ""; echo ""; echo ""; @@ -159,7 +159,7 @@ function make_log_url($module, $url) { function print_log($course, $user=0, $date=0, $order="l.time ASC", $page=0, $perpage=100, - $url="", $mod="", $modpage="", $modid=0) { + $url="", $mod="", $modid=0, $modaction="") { // It is assumed that $date is the GMT time of midnight for that day, // and so the next 86400 seconds worth of logs are printed. @@ -182,9 +182,12 @@ function print_log($course, $user=0, $date=0, $order="l.time ASC", $page=0, $per $selector .= " AND l.module = '$mod'"; } - if ($modpage and $modid) { - $LIKE = $CFG->dbtype == "mysql" ? "LIKE" : "ILIKE"; - $selector .= " AND l.url $LIKE '%id=$modid%'"; + if ($modid) { + $selector .= " AND l.cmid = '$modid'"; + } + + if ($modaction) { + $selector .= " AND l.action = '$modaction'"; } if ($user) { diff --git a/course/log.php b/course/log.php index bfcc979ea2..28ca916a4a 100644 --- a/course/log.php +++ b/course/log.php @@ -7,7 +7,8 @@ require_variable($id); // Course ID optional_variable($user, 0); // User to display optional_variable($date, 0); // Date to display - optional_variable($url, ""); // eg resource/view.php?id=66 + optional_variable($modid, ""); // course_module->id + optional_variable($modaction, ""); // an action as recorded in the logs optional_variable($page, "0"); // which page to show optional_variable($perpage, "100"); // how many per page @@ -45,21 +46,6 @@ $dateinfo = userdate($date, get_string("strftimedaydate")); } - if ($url) { // parse into parts: resource/view.php?id=66 - $urlraw = urldecode($url); - $urlparts = split("/", $urlraw); - if ($urlparts[0] != "section" and $urlparts[0] != "") { - $modname = $urlparts[0]; - $modparts = split('.php\?id=', $urlparts[1]); - if (count($modparts) == 2) { - if (record_exists('course_modules', 'id', $modparts[1], 'course', $course->id)) { - $modpage = $modparts[0]; - $modid = $modparts[1]; - } - } - } - } - if ($course->category) { print_header("$course->shortname: $strlogs", "$course->fullname", "id\">$course->shortname -> @@ -72,11 +58,11 @@ print_heading("$course->fullname: $userinfo, $dateinfo (".usertimezone().")"); - print_log_selector_form($course, $user, $date, $modname, $modpage, $modid); + print_log_selector_form($course, $user, $date, $modname, $modid, $modaction); print_log($course, $user, $date, "l.time DESC", $page, $perpage, - "log.php?id=$course->id&chooselog=1&user=$user&date=$date&url=$url", - $modname, $modpage, $modid); + "log.php?id=$course->id&chooselog=1&user=$user&date=$date&modid=$modid&modaction=$modaction", + $modname, $modid, $modaction); } else { if ($course->category) { diff --git a/lib/datalib.php b/lib/datalib.php index f6858d7af6..a2c1d4ab3b 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -1985,14 +1985,15 @@ function instance_is_visible($moduletype, $module) { * than web server hits, and provide a way to easily reconstruct what * any particular student has been doing. * -* @param int $course the course id -* @param string $module the module name - e.g. forum, journal, resource, course, user etc -* @param string $action view, edit, post (often but not always the same as the file.php) -* @param string $url the file and parameters used to see the results of the action -* @param string $info additional description information -* @param string $user optional, if log regards $user other than $USER +* @param int $course the course id +* @param string $module the module name - e.g. forum, journal, resource, course, user etc +* @param string $action view, edit, post (often but not always the same as the file.php) +* @param string $url the file and parameters used to see the results of the action +* @param string $info additional description information +* @param string $cm the course_module->id if there is one +* @param string $user if log regards $user other than $USER */ -function add_to_log($courseid, $module, $action, $url="", $info="", $user="") { +function add_to_log($courseid, $module, $action, $url="", $info="", $cm=0, $user=0) { global $db, $CFG, $USER, $REMOTE_ADDR; @@ -2008,22 +2009,8 @@ function add_to_log($courseid, $module, $action, $url="", $info="", $user="") { $timenow = time(); $info = addslashes($info); - $result = $db->Execute("INSERT INTO {$CFG->prefix}log (time, - userid, - course, - ip, - module, - action, - url, - info) - VALUES ('$timenow', - '$userid', - '$courseid', - '$REMOTE_ADDR', - '$module', - '$action', - '$url', - '$info')"); + $result = $db->Execute("INSERT INTO {$CFG->prefix}log (time, userid, course, ip, module, cmid, action, url, info) + VALUES ('$timenow', '$userid', '$courseid', '$REMOTE_ADDR', '$module', '$cm', '$action', '$url', '$info')"); if (!$result and ($CFG->debug > 7)) { echo "

Error: Could not insert a new entry to the Moodle log

"; // Don't throw an error diff --git a/lib/db/mysql.php b/lib/db/mysql.php index 1fe42506f7..0064c24fd1 100644 --- a/lib/db/mysql.php +++ b/lib/db/mysql.php @@ -634,6 +634,32 @@ function main_upgrade($oldversion=0) { table_column("config", "value", "value", "text", "", "", ""); } + if ($oldversion < 2004013101) { + table_column("log", "", "cmid", "integer", "10", "unsigned", "0", "", "module"); + + /// try and extract as many cmids as possible from the existing logs + + if ($coursemodules = get_records_sql("SELECT cm.*, m.name + FROM {$CFG->prefix}course_modules cm, + {$CFG->prefix}modules m + WHERE cm.module = m.id")) { + foreach ($coursemodules as $cm) { + execute_sql("UPDATE {$CFG->prefix}log SET cmid = '$cm->id' + WHERE module = '$cm->name' AND url LIKE 'view.php?id=$cm->id%'"); + if ($cm->name == "forum") { + execute_sql("UPDATE {$CFG->prefix}log SET cmid = '$cm->id' + WHERE module = 'forum' AND url LIKE '%?f=$cm->instance%'"); + if ($discussions = get_records("forum_discussions", "forum", $cm->instance)) { + foreach ($discussions as $discussion) { + execute_sql("UPDATE {$CFG->prefix}log SET cmid = '$cm->id' + WHERE module = 'forum' AND url LIKE '%?d=$discussion->id%'"); + } + } + } + } + } + } + return $result; } diff --git a/lib/db/mysql.sql b/lib/db/mysql.sql index b875ebf4c2..e484452981 100644 --- a/lib/db/mysql.sql +++ b/lib/db/mysql.sql @@ -201,6 +201,7 @@ CREATE TABLE `prefix_log` ( `ip` varchar(15) NOT NULL default '', `course` int(10) unsigned NOT NULL default '0', `module` varchar(10) NOT NULL default '', + `cmid` int(10) unsigned NOT NULL default '0', `action` varchar(15) NOT NULL default '', `url` varchar(100) NOT NULL default '', `info` varchar(255) NOT NULL default '', diff --git a/lib/db/postgres7.php b/lib/db/postgres7.php index dadb93faf6..ee402e341c 100644 --- a/lib/db/postgres7.php +++ b/lib/db/postgres7.php @@ -379,6 +379,32 @@ function main_upgrade($oldversion=0) { if ($oldversion < 2004012900) { table_column("config", "value", "value", "text", "", "", ""); } + + if ($oldversion < 2004013101) { + table_column("log", "", "cmid", "integer", "10", "unsigned", "0", "", "module"); + + /// try and extract as many cmids as possible from the existing logs + + if ($coursemodules = get_records_sql("SELECT cm.*, m.name + FROM {$CFG->prefix}course_modules cm, + {$CFG->prefix}modules m + WHERE cm.module = m.id")) { + foreach ($coursemodules as $cm) { + execute_sql("UPDATE {$CFG->prefix}log SET cmid = '$cm->id' + WHERE module = '$cm->name' AND url ILIKE 'view.php?id=$cm->id%'"); + if ($cm->name == "forum") { + execute_sql("UPDATE {$CFG->prefix}log SET cmid = '$cm->id' + WHERE module = 'forum' AND url ILIKE '%?f=$cm->instance%'"); + if ($discussions = get_records("forum_discussions", "forum", $cm->instance)) { + foreach ($discussions as $discussion) { + execute_sql("UPDATE {$CFG->prefix}log SET cmid = '$cm->id' + WHERE module = 'forum' AND url ILIKE '%?d=$discussion->id%'"); + } + } + } + } + } + } return $result; } diff --git a/lib/db/postgres7.sql b/lib/db/postgres7.sql index ba5c44e844..00833233cc 100644 --- a/lib/db/postgres7.sql +++ b/lib/db/postgres7.sql @@ -127,6 +127,7 @@ CREATE TABLE prefix_log ( ip varchar(15) NOT NULL default '', course integer NOT NULL default '0', module varchar(20) NOT NULL default '', + cmid integer NOT NULL default '0', action varchar(20) NOT NULL default '', url varchar(100) NOT NULL default '', info varchar(255) NOT NULL default '' diff --git a/lib/weblib.php b/lib/weblib.php index 23b9808ca7..607e1c4dcb 100644 --- a/lib/weblib.php +++ b/lib/weblib.php @@ -1701,10 +1701,10 @@ function navmenu($course, $cm=NULL, $targetwindow="self") { $previousmod = $mod; } } - if ($selected and $isteacher) { - $logslink = "wwwroot/course/log.php?chooselog=1&user=0&date=0&id=$course->id&url=". - urlencode($selected). - "\">pixpath/i/log.gif\">"; + if ($selectmod and $isteacher) { + $logslink = "wwwroot/course/log.php?chooselog=1&user=0&date=0&id=$course->id&modid=$selectmod->cm\">". + "pixpath/i/log.gif\">"; } if ($backmod) { diff --git a/mod/forum/discuss.php b/mod/forum/discuss.php index bd3c74dc0c..ef8a79f77d 100644 --- a/mod/forum/discuss.php +++ b/mod/forum/discuss.php @@ -33,7 +33,12 @@ } set_field("forum_discussions", "forum", $forum->id, "id", $discussion->id); $discussion->forum = $forum->id; - add_to_log($course->id, "forum", "move discussion", "discuss.php?d=$discussion->id", "$discussion->id"); + if ($cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) { + add_to_log($course->id, "forum", "move discussion", "discuss.php?d=$discussion->id", "$discussion->id", + $cm->id); + } else { + add_to_log($course->id, "forum", "move discussion", "discuss.php?d=$discussion->id", "$discussion->id"); + } $discussionmoved = true; } else { error("You can't move to that forum - it doesn't exist!"); @@ -46,15 +51,16 @@ } } - if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) { - //notify("Bad coursemodule for this discussion"); // Only affects navmenu - } - $logparameters = "d=$discussion->id"; if ($parent) { $logparameters .= "&parent=$parent"; } - add_to_log($course->id, "forum", "view discussion", "discuss.php?$logparameters", "$discussion->id"); + + if ($cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) { + add_to_log($course->id, "forum", "view discussion", "discuss.php?$logparameters", "$discussion->id", $cm->id); + } else { + add_to_log($course->id, "forum", "view discussion", "discuss.php?$logparameters", "$discussion->id"); + } unset($SESSION->fromdiscussion); diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 47e0543ef1..c7f77de23e 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -86,7 +86,6 @@ function forum_add_instance($forum) { error("Could not add the discussion for this forum"); } } - add_to_log($forum->course, "forum", "add", "index.php?f=$forum->id", "$forum->id"); return $forum->id; } @@ -142,12 +141,7 @@ function forum_update_instance($forum) { } } - if (update_record("forum", $forum)) { - add_to_log($forum->course, "forum", "update", "index.php?f=$forum->id", "$forum->id"); - return true; - } else { - return false; - } + return update_record("forum", $forum); } @@ -244,7 +238,9 @@ function forum_cron () { continue; // Be safe and don't send it to anyone } } - } + } else { + $cm->id = 0; + } if ($users = forum_subscribed_users($course, $forum)) { @@ -329,7 +325,7 @@ function forum_cron () { if (! email_to_user($userto, $userfrom, $postsubject, $posttext, $posthtml)) { echo "Error: mod/forum/cron.php: Could not send out mail for id $post->id to user $userto->id ($userto->email) .. not trying again.\n"; - add_to_log($course->id, 'forum', 'mail error', "discuss.php?d=$discussion->id#$post->id", substr($post->subject,0,15), $userto->id); + add_to_log($course->id, 'forum', 'mail error', "discuss.php?d=$discussion->id#$post->id", substr($post->subject,0,15), $cm->id, $userto->id); $errorcount++; } else { $mailcount++; diff --git a/mod/forum/post.php b/mod/forum/post.php index c0b97b8365..bd784010a0 100644 --- a/mod/forum/post.php +++ b/mod/forum/post.php @@ -25,13 +25,20 @@ $post->attachment = $_FILES["attachment"]; + if (!$cm = get_coursemodule_from_instance("forum", $post->forum, $post->course)) { // For the logs + $cm->id = 0; + } + if (!$post->subject or !$post->message) { $post->error = get_string("emptymessage", "forum"); } else if ($post->edit) { // Updating a post $post->id = $post->edit; if (forum_update_post($post)) { - add_to_log($post->course, "forum", "update post", "discuss.php?d=$post->discussion&parent=$post->id", "$post->id"); + + add_to_log($post->course, "forum", "update post", + "discuss.php?d=$post->discussion&parent=$post->id", "$post->id", $cm->id); + $message = get_string("postupdated", "forum"); $timemessage = 1; @@ -47,7 +54,10 @@ } else if ($post->discussion) { // Adding a new post to an existing discussion if ($post->id = forum_add_new_post($post)) { - add_to_log($post->course, "forum", "add post", "discuss.php?d=$post->discussion&parent=$post->id", "$post->id"); + + add_to_log($post->course, "forum", "add post", + "discuss.php?d=$post->discussion&parent=$post->id", "$post->id", $cm->id); + $message = get_string("postadded", "forum", format_time($CFG->maxeditingtime)); $timemessage = 2; @@ -67,7 +77,10 @@ $discussion->name = $post->subject; $discussion->intro = $post->message; if ($discussion->id = forum_add_discussion($discussion)) { - add_to_log($post->course, "forum", "add discussion", "discuss.php?d=$discussion->id", "$discussion->id"); + + add_to_log($post->course, "forum", "add discussion", + "discuss.php?d=$discussion->id", "$discussion->id", $cm->id); + $message = get_string("postadded", "forum", format_time($CFG->maxeditingtime)); $timemessage = 2; @@ -251,6 +264,9 @@ forum_go_back_to("discuss.php?id=$post->discussion")); } else { + if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $forum->course)) { // For the logs + $cm->id = 0; + } if (! $post->parent) { // post is a discussion topic as well, so delete discussion if ($forum->type == "single") { notice("Sorry, but you are not allowed to delete that discussion!", @@ -258,13 +274,17 @@ } forum_delete_discussion($discussion); - add_to_log($discussion->course, "forum", "delete discussion", "view.php?id=$discussion->forum", "$post->id"); + add_to_log($discussion->course, "forum", "delete discussion", + "view.php?id=$discussion->forum", "$post->id", $cm->id); + redirect("view.php?f=$discussion->forum", get_string("deleteddiscussion", "forum"), 1); } else if (forum_delete_post($post)) { - add_to_log($discussion->course, "forum", "delete post", "discuss.php?d=$post->discussion", "$post->id"); + add_to_log($discussion->course, "forum", "delete post", + "discuss.php?d=$post->discussion", "$post->id", $cm->id); + redirect(forum_go_back_to("discuss.php?d=$post->discussion"), get_string("deletedpost", "forum"), 1); } else { diff --git a/mod/forum/subscribe.php b/mod/forum/subscribe.php index c10a3f3c4c..8a762d29c2 100644 --- a/mod/forum/subscribe.php +++ b/mod/forum/subscribe.php @@ -50,6 +50,8 @@ error("Sorry, but you must be a group member to subscribe."); } } + } else { + $cm->id = NULL; } $returnto = forum_go_back_to("index.php?id=$course->id"); @@ -73,7 +75,7 @@ if ( forum_is_subscribed($user->id, $forum->id) ) { if (forum_unsubscribe($user->id, $forum->id) ) { - add_to_log($course->id, "forum", "unsubscribe", "view.php?f=$forum->id", "$forum->id"); + add_to_log($course->id, "forum", "unsubscribe", "view.php?f=$forum->id", $forum->id, $cm->id); redirect($returnto, get_string("nownotsubscribed", "forum", $info), 1); } else { error("Could not unsubscribe you from that forum", $_SERVER["HTTP_REFERER"]); @@ -81,7 +83,7 @@ } else { // subscribe if (forum_subscribe($user->id, $forum->id) ) { - add_to_log($course->id, "forum", "subscribe", "view.php?f=$forum->id", "$forum->id"); + add_to_log($course->id, "forum", "subscribe", "view.php?f=$forum->id", $forum->id, $cm->id); redirect($returnto, get_string("nowsubscribed", "forum", $info), 1); } else { error("Could not subscribe you to that forum", $_SERVER["HTTP_REFERER"]); diff --git a/mod/forum/subscribers.php b/mod/forum/subscribers.php index 0d8b152c64..fc3c812521 100644 --- a/mod/forum/subscribers.php +++ b/mod/forum/subscribers.php @@ -13,6 +13,10 @@ error("Could not find this course!"); } + if (! $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) { + $cm->id = 0; + } + require_login($course->id); if (!isteacher($course->id)) { @@ -21,18 +25,18 @@ unset($SESSION->fromdiscussion); - add_to_log($course->id, "forum", "view subscribers", "subscribers.php?id=$forum->id", ""); + add_to_log($course->id, "forum", "view subscribers", "subscribers.php?id=$forum->id", "", $cm->id); $strsubscribers = get_string("subscribers", "forum"); $strforums = get_string("forums", "forum"); if ($course->category) { - $navigation = "id\">$course->shortname -> - id\">$strforums -> - id\">$forum->name -> $strsubscribers"; + $navigation = "id\">$course->shortname -> + id\">$strforums -> + id\">$forum->name -> $strsubscribers"; } else { - $navigation = "id\">$strforums -> - id\">$forum->name -> $strsubscribers"; + $navigation = "id\">$strforums -> + id\">$forum->name -> $strsubscribers"; } print_header("$course->shortname: $strsubscribers", "$course->fullname", "$navigation"); @@ -42,19 +46,19 @@ } else { print_heading(get_string("subscribersto","forum", "'$forum->name'")); - echo ""; + echo '
'; foreach ($users as $user) { - echo ""; + echo ""; } - echo "
"; + echo "
"; print_user_picture($user->id, $course->id, $user->picture); - echo "cellcontent\">"; + echo "cellcontent\">"; echo "$user->firstname $user->lastname"; - echo "cellcontent\">"; + echo "cellcontent\">"; echo "$user->email"; - echo ""; - echo "id&user=$user->id\">unsubscribe"; - echo "
"; + echo "id&user=$user->id\">unsubscribe"; + echo "
"; + echo ""; } print_footer($course); diff --git a/mod/forum/version.php b/mod/forum/version.php index dc23962102..a24751c747 100644 --- a/mod/forum/version.php +++ b/mod/forum/version.php @@ -5,8 +5,8 @@ // This fragment is called by /admin/index.php //////////////////////////////////////////////////////////////////////////////// -$module->version = 2004013000; -$module->requires = 2004013000; // Requires this Moodle version +$module->version = 2004013100; +$module->requires = 2004013101; // Requires this Moodle version $module->cron = 60; ?> diff --git a/mod/forum/view.php b/mod/forum/view.php index 64a9864954..0165651381 100644 --- a/mod/forum/view.php +++ b/mod/forum/view.php @@ -35,6 +35,7 @@ if ($cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) { $buttontext = update_module_button($cm->id, $course->id, $strforum); } else { + $cm->id = NULL; $buttontext = ""; } @@ -61,7 +62,7 @@ } if ($cm) { - add_to_log($course->id, "forum", "view forum", "view.php?id=$cm->id", "$forum->id"); + add_to_log($course->id, "forum", "view forum", "view.php?id=$cm->id", "$forum->id", $cm->id); } else { add_to_log($course->id, "forum", "view forum", "view.php?f=$forum->id", "$forum->id"); } diff --git a/version.php b/version.php index 9559555cc3..5727c4713c 100644 --- a/version.php +++ b/version.php @@ -5,7 +5,7 @@ // database to determine whether upgrades should // be performed (see lib/db/*.php) -$version = 2004013100; // The current version is a date (YYYYMMDDXX) +$version = 2004013101; // The current version is a date (YYYYMMDDXX) $release = "1.2 development"; // User-friendly version number -- 2.39.5