Petr's patch for MDL-7327 to modify require_capability login a bit
[moodle.git] / admin / upgradeforumread.php
blob9d620e7afb084fa26b7e5450be8948911c4038b4
1 <?PHP //$Id$
3 require_once('../config.php');
4 require_once($CFG->dirroot.'/mod/forum/lib.php');
5 require_once($CFG->libdir.'/adminlib.php');
6 $adminroot = admin_get_root();
7 admin_externalpage_setup('upgradeforumread', $adminroot);
9 $confirm = optional_param('confirm', 0, PARAM_BOOL);
11 require_login();
13 require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM, SITEID));
15 if ($CFG->version < 2005042300) {
16 error("This script does not work with this old version of Moodle");
19 if (!$site = get_site()) {
20 redirect('index.php');
24 /// Print header
26 $strupgradingdata = get_string('upgradingdata', 'admin');
28 admin_externalpage_print_header($adminroot);
29 print_heading($strupgradingdata);
31 if (!data_submitted() or empty($confirm) or !confirm_sesskey()) {
32 $optionsyes = array('confirm'=>'1', 'sesskey'=>sesskey());
33 notice_yesno(get_string('upgradeforumreadinfo', 'admin'),
34 'upgradeforumread.php', 'index.php', $optionsyes, NULL, 'post', 'get');
35 admin_externalpage_print_footer($adminroot);
36 exit;
40 /// Turn off time limits, sometimes upgrades can be slow.
42 @set_time_limit(0);
43 @ob_implicit_flush(true);
44 while(@ob_end_flush());
46 execute_sql('TRUNCATE TABLE '.$CFG->prefix.'forum_read;', false); // Trash all old entries
48 /// Enter initial read records for all posts older than 1 day.
50 /// Timestamp for old posts (and therefore considered read).
51 $dateafter = time() - ($CFG->forum_oldpostdays*24*60*60);
53 /// Timestamp for one day ago.
54 $onedayago = time() - (24*60*60);
57 /// Get all discussions that have had posts since the old post date.
58 if ($discussions = get_records_select('forum_discussions', 'timemodified > '.$dateafter,
59 'course', 'id,course,forum,groupid,userid')) {
60 $dtotal = count($discussions);
61 print_heading('Updating forum post read/unread records for '.$dtotal.' discussions...'.
62 'Please keep this window open until it completes', '', 3);
64 $groups = array();
66 $currcourse = 0;
67 $users = 0;
68 $count = 0;
69 $dcount = 0;
71 foreach ($discussions as $discussion) {
72 $dcount++;
73 print_progress($dcount, $dtotal);
75 if ($discussion->course != $currcourse) {
76 /// Discussions are ordered by course, so we only need to get any course's users once.
77 $currcourse = $discussion->course;
78 $users = get_course_users($currcourse, '', '', 'u.id,u.confirmed');
80 /// If this course has users, and posts more than a day old, mark them for each user.
81 if ($users &&
82 ($posts = get_records_select('forum_posts', 'discussion = '.$discussion->id.
83 ' AND '.$dateafter.' < modified AND modified < '.$onedayago,
84 '', 'id,discussion,modified'))) {
85 foreach ($users as $user) {
86 /// If its a group discussion, make sure the user is in the group.
87 if ($discussion->groupid) {
88 if (!isset($groups[$discussion->groupid][$user->id])) {
89 $groups[$discussion->groupid][$user->id] = ismember($discussion->groupid, $user->id);
92 if (!$discussion->groupid || !empty($groups[$discussion->groupid][$user->id])) {
93 foreach ($posts as $post) {
94 print_progress($dcount, $dtotal);
95 forum_tp_mark_post_read($user->id, $post, $discussion->forum);
101 print_progress($dcount, $dtotal, 0);
105 delete_records('config', 'name', 'upgrade', 'value', 'forumread');
107 notify('Log upgrading was successful!', 'notifysuccess');
109 print_continue('index.php');
111 admin_externalpage_print_footer($adminroot);