Much more efficient updating of forum_read table! I think it works ...!

This commit is contained in:
moodler 2005-04-22 15:48:05 +00:00
parent 76c78acfea
commit 36ad8acd31
2 changed files with 111 additions and 79 deletions

View File

@ -4,7 +4,7 @@ function forum_upgrade($oldversion) {
// This function does anything necessary to upgrade
// older versions to match current functionality
global $CFG;
global $CFG, $db;
if ($oldversion < 2002073008) {
execute_sql("DELETE FROM modules WHERE name = 'discuss' ");
@ -161,54 +161,70 @@ function forum_upgrade($oldversion) {
if ($oldversion < 2005011500) {
modify_database('','CREATE TABLE prefix_forum_read (
`id` int(10) unsigned NOT NULL auto_increment,
`userid` int(10) NOT NULL default \'0\',
`forumid` int(10) NOT NULL default \'0\',
`discussionid` int(10) NOT NULL default \'0\',
`postid` int(10) NOT NULL default \'0\',
`firstread` int(10) NOT NULL default \'0\',
`lastread` int(10) NOT NULL default \'0\',
PRIMARY KEY (`id`),
KEY `prefix_forum_user_forum_idx` (`userid`,`forumid`),
KEY `prefix_forum_user_discussion_idx` (`userid`,`discussionid`),
KEY `prefix_forum_user_post_idx` (`userid`,`postid`)
) COMMENT=\'Tracks each users read posts\';');
`id` int(10) unsigned NOT NULL auto_increment,
`userid` int(10) NOT NULL default \'0\',
`forumid` int(10) NOT NULL default \'0\',
`discussionid` int(10) NOT NULL default \'0\',
`postid` int(10) NOT NULL default \'0\',
`firstread` int(10) NOT NULL default \'0\',
`lastread` int(10) NOT NULL default \'0\',
PRIMARY KEY (`id`),
KEY `prefix_forum_user_forum_idx` (`userid`,`forumid`),
KEY `prefix_forum_user_discussion_idx` (`userid`,`discussionid`),
KEY `prefix_forum_user_post_idx` (`userid`,`postid`)
) COMMENT=\'Tracks each users read posts\';');
/// Enter initial read records for all posts older than 1 day.
modify_database('','TRUNCATE TABLE prefix_forum_read;');
require $CFG->dirroot.'/mod/forum/lib.php';
/// Timestamp for old posts (and therefore considered read).
$dateafter = time() - ($CFG->forum_oldpostdays*24*60*60);
/// Timestamp for one day ago.
$onedayago = time() - (24*60*60);
/// Enter initial read records for all posts older than 1 day.
/// Get all discussions that have had posts since the old post date.
if ($discrecords = get_records_select('forum_discussions', 'timemodified > '.$dateafter,
'course', 'id,course,forum,groupid')) {
$currcourse = 0;
$users = 0;
foreach ($discrecords as $discrecord) {
if ($discrecord->course != $currcourse) {
/// Discussions are ordered by course, so we only need to get any course's users once.
$currcourse = $discrecord->course;
$users = get_course_users($currcourse);
}
/// If this course has users, and posts more than a day old, mark them for each user.
if (is_array($users) &&
($posts = get_records_select('forum_posts', 'discussion = '.$discrecord->id.
' AND modified < '.$onedayago, '', 'id,discussion,modified'))) {
foreach($posts as $post) {
foreach ($users as $user) {
/// If its a group discussion, make sure the user is in the group.
if (!$discrecord->groupid || ismember($discrecord->groupid, $user->id)) {
forum_tp_mark_post_read($user->id, $post, $discrecord->forum);
}
}
}
}
}
}
require $CFG->dirroot.'/mod/forum/lib.php';
/// Timestamp for old posts (and therefore considered read).
$dateafter = time() - ($CFG->forum_oldpostdays*24*60*60);
/// Timestamp for one day ago.
$onedayago = time() - (24*60*60);
/// Get all discussions that have had posts since the old post date.
if ($discussions = get_records_select('forum_discussions', 'timemodified > '.$dateafter,
'course', 'id,course,forum,groupid,userid')) {
$roughposts = count_records_select('forum_posts', 'modified < '.$onedayago.' AND modified > '.$dateafter);
notify('Updating forum read/unread records for approx '.$roughposts.' posts...');
$db->debug = false; // Too much to look at!
$currcourse = 0;
$users = 0;
$count = 0;
foreach ($discussions as $discussion) {
if ($discussion->course != $currcourse) {
/// Discussions are ordered by course, so we only need to get any course's users once.
$currcourse = $discussion->course;
$users = get_course_users($currcourse, '', '', 'id,id');
}
/// If this course has users, and posts more than a day old, mark them for each user.
if ($users &&
($posts = get_records_select('forum_posts', 'discussion = '.$discussion->id.
' AND modified < '.$onedayago.' AND modified > '.$dateafter,
'', 'id,discussion,modified'))) {
foreach ($users as $user) {
/// If its a group discussion, make sure the user is in the group.
if (!$discussion->groupid ||
$discussion->userid == $user->id ||
ismember($discussion->groupid, $user->id)) {
foreach ($posts as $post) {
$count++;
if ($count % 100 == 0) {
echo $count.'<br />';
} else {
echo '.';
}
forum_tp_mark_post_read($user->id, $post, $discussion->forum);
}
}
}
}
}
$db->debug = true;
}
}
if ($oldversion < 2005032900) {

View File

@ -116,41 +116,57 @@ function forum_upgrade($oldversion) {
modify_database('','CREATE INDEX prefix_forum_user_discussion_idx ON prefix_forum_read (userid, discussionid);');
modify_database('','CREATE INDEX prefix_forum_user_post_idx ON prefix_forum_read (userid, postid);');
/// Enter initial read records for all posts older than 1 day.
modify_database('','TRUNCATE TABLE prefix_forum_read;'); // Just in case this is being re-run
require_once($CFG->dirroot.'/mod/forum/lib.php');
/// Timestamp for old posts (and therefore considered read).
$dateafter = time() - ($CFG->forum_oldpostdays*24*60*60);
/// Timestamp for one day ago.
$onedayago = time() - (24*60*60);
/// Enter initial read records for all posts older than 1 day.
/// Get all discussions that have had posts since the old post date.
if ($discrecords = get_records_select('forum_discussions', 'timemodified > '.$dateafter,
'course', 'id,course,forum,groupid')) {
$currcourse = 0;
$users = 0;
foreach ($discrecords as $discrecord) {
if ($discrecord->course != $currcourse) {
/// Discussions are ordered by course, so we only need to get any course's users once.
$currcourse = $discrecord->course;
$users = get_course_users($currcourse);
}
/// If this course has users, and posts more than a day old, mark them for each user.
if (is_array($users) &&
($posts = get_records_select('forum_posts', 'discussion = '.$discrecord->id.
' AND modified < '.$onedayago, '', 'id,discussion,modified'))) {
foreach($posts as $post) {
foreach ($users as $user) {
/// If its a group discussion, make sure the user is in the group.
if (!$discrecord->groupid || ismember($discrecord->groupid, $user->id)) {
forum_tp_mark_post_read($user->id, $post, $discrecord->forum);
}
}
}
}
}
}
require $CFG->dirroot.'/mod/forum/lib.php';
/// Timestamp for old posts (and therefore considered read).
$dateafter = time() - ($CFG->forum_oldpostdays*24*60*60);
/// Timestamp for one day ago.
$onedayago = time() - (24*60*60);
/// Get all discussions that have had posts since the old post date.
if ($discussions = get_records_select('forum_discussions', 'timemodified > '.$dateafter,
'course', 'id,course,forum,groupid,userid')) {
$roughposts = count_records_select('forum_posts', 'modified < '.$onedayago.' AND modified > '.$dateafter);
notify('Updating forum read/unread records for approx '.$roughposts.' posts...');
$db->debug = false; // Too much to look at!
$currcourse = 0;
$users = 0;
$count = 0;
foreach ($discussions as $discussion) {
if ($discussion->course != $currcourse) {
/// Discussions are ordered by course, so we only need to get any course's users once.
$currcourse = $discussion->course;
$users = get_course_users($currcourse, '', '', 'id,id');
}
/// If this course has users, and posts more than a day old, mark them for each user.
if ($users &&
($posts = get_records_select('forum_posts', 'discussion = '.$discussion->id.
' AND modified < '.$onedayago.' AND modified > '.$dateafter,
'', 'id,discussion,modified'))) {
foreach ($users as $user) {
/// If its a group discussion, make sure the user is in the group.
if (!$discussion->groupid ||
$discussion->userid == $user->id ||
ismember($discussion->groupid, $user->id)) {
foreach ($posts as $post) {
$count++;
if ($count % 100 == 0) {
echo $count.'<br />';
} else {
echo '.';
}
forum_tp_mark_post_read($user->id, $post, $discussion->forum);
}
}
}
}
}
$db->debug = true;
}
}
if ($oldversion < 2005032900) {