3 require_once('../config.php');
4 require_once($CFG->dirroot
.'/mod/forum/lib.php');
5 require_once($CFG->libdir
.'/adminlib.php');
7 admin_externalpage_setup('upgradeforumread');
9 $confirm = optional_param('confirm', 0, PARAM_BOOL
);
11 if ($CFG->version
< 2005042300) {
12 error("This script does not work with this old version of Moodle");
15 if (!$site = get_site()) {
16 redirect('index.php');
22 $strupgradingdata = get_string('upgradingdata', 'admin');
24 admin_externalpage_print_header();
25 print_heading($strupgradingdata);
27 if (!data_submitted() or empty($confirm) or !confirm_sesskey()) {
28 $optionsyes = array('confirm'=>'1', 'sesskey'=>sesskey());
29 notice_yesno(get_string('upgradeforumreadinfo', 'admin'),
30 'upgradeforumread.php', 'index.php', $optionsyes, NULL, 'post', 'get');
31 admin_externalpage_print_footer();
36 /// Turn off time limits, sometimes upgrades can be slow.
39 @ob_implicit_flush
(true);
40 while(@ob_end_flush
());
42 execute_sql('TRUNCATE TABLE '.$CFG->prefix
.'forum_read;', false); // Trash all old entries
44 /// Enter initial read records for all posts older than 1 day.
46 /// Timestamp for old posts (and therefore considered read).
47 $dateafter = time() - ($CFG->forum_oldpostdays
*24*60*60);
49 /// Timestamp for one day ago.
50 $onedayago = time() - (24*60*60);
53 /// Get all discussions that have had posts since the old post date.
54 if ($discussions = get_records_select('forum_discussions', 'timemodified > '.$dateafter,
55 'course', 'id,course,forum,groupid,userid')) {
56 $dtotal = count($discussions);
57 print_heading('Updating forum post read/unread records for '.$dtotal.' discussions...'.
58 'Please keep this window open until it completes', '', 3);
67 foreach ($discussions as $discussion) {
69 print_progress($dcount, $dtotal);
71 if ($discussion->course
!= $currcourse) {
72 /// Discussions are ordered by course, so we only need to get any course's users once.
73 $currcourse = $discussion->course
;
74 $users = get_course_users($currcourse, '', '', 'u.id,u.confirmed');
76 /// If this course has users, and posts more than a day old, mark them for each user.
78 ($posts = get_records_select('forum_posts', 'discussion = '.$discussion->id
.
79 ' AND '.$dateafter.' < modified AND modified < '.$onedayago,
80 '', 'id,discussion,modified'))) {
81 foreach ($users as $user) {
82 /// If its a group discussion, make sure the user is in the group.
83 if ($discussion->groupid
) {
84 if (!isset($groups[$discussion->groupid
][$user->id
])) {
85 $groups[$discussion->groupid
][$user->id
] = ismember($discussion->groupid
, $user->id
);
88 if (!$discussion->groupid ||
!empty($groups[$discussion->groupid
][$user->id
])) {
89 foreach ($posts as $post) {
90 print_progress($dcount, $dtotal);
91 forum_tp_mark_post_read($user->id
, $post, $discussion->forum
);
97 print_progress($dcount, $dtotal, 0);
101 delete_records('config', 'name', 'upgrade', 'value', 'forumread');
103 notify('Log upgrading was successful!', 'notifysuccess');
105 print_continue('index.php');
107 admin_externalpage_print_footer();