File: upgradeforumread.php

package info (click to toggle)
moodle 1.6.3-2%2Betch3
  • links: PTS
  • area: main
  • in suites: etch
  • size: 37,172 kB
  • ctags: 51,688
  • sloc: php: 231,916; sql: 5,631; xml: 2,688; sh: 1,185; perl: 638; makefile: 48; pascal: 36
file content (119 lines) | stat: -rw-r--r-- 4,244 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?PHP  //$Id: upgradeforumread.php,v 1.8.2.1 2006/07/27 11:48:19 skodak Exp $

    require_once('../config.php');
    require_once($CFG->dirroot.'/mod/forum/lib.php');
    require_once($CFG->libdir.'/adminlib.php');

    $confirm = optional_param('confirm', 0, PARAM_BOOL);

    require_login();

    if (!isadmin()) {
        error("You must be an admin to use this script");
    }

    if ($CFG->version < 2005042300) {
        error("This script does not work with this old version of Moodle");
    }

    if (!$site = get_site()) {
        redirect("index.php");
    }


/// Print header

    $stradministration = get_string("administration");
    $strupgradingdata  = get_string("upgradingdata", "admin");

    print_header("$site->shortname: $stradministration: $strupgradingdata", "$site->fullname", 
                 "<a href=\"index.php\">$stradministration</a> -> $strupgradingdata");

    if (empty($confirm)) {
        notice_yesno(get_string("upgradeforumreadinfo", "admin"), 
                     "upgradeforumread.php?confirm=true&sesskey=$USER->sesskey", 
                     "index.php");
        print_footer();
        exit;
    } else if (!confirm_sesskey()) {
        error(get_string('confirmsesskeybad', 'error'));
    }


/// Turn off time limits, sometimes upgrades can be slow.

    @set_time_limit(0);
    @ob_implicit_flush(true);
    @ob_end_flush();

    execute_sql('TRUNCATE TABLE '.$CFG->prefix.'forum_read;', false);   // Trash all old entries

/// Enter initial read records for all posts older than 1 day.

/// 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')) {
        $dtotal = count($discussions);
        print_heading('Updating forum post read/unread records for '.$dtotal.' discussions...'.
                      'Please keep this window open until it completes', '', 3);

        $groups = array();
    
        $currcourse = 0;
        $users = 0;
        $count = 0;
        $dcount = 0;

        foreach ($discussions as $discussion) {
            $dcount++;
            print_progress($dcount, $dtotal);

            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, '', '', 'u.id,u.confirmed');
            }
            /// 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 '.$dateafter.' < modified AND modified < '.$onedayago, 
                                                 '', 'id,discussion,modified'))) {
                foreach ($users as $user) {
                    /// If its a group discussion, make sure the user is in the group.
                    if ($discussion->groupid) {
                        if (!isset($groups[$discussion->groupid][$user->id])) {
                            $groups[$discussion->groupid][$user->id] = ismember($discussion->groupid, $user->id);
                        }
                    }
                    if (!$discussion->groupid || !empty($groups[$discussion->groupid][$user->id])) {
                        foreach ($posts as $post) {
                            print_progress($dcount, $dtotal);
                            forum_tp_mark_post_read($user->id, $post, $discussion->forum);
                        }
                    }
                }
            }
        }
        print_progress($dcount, $dtotal, 0);
    }
    

    delete_records('config', 'name', 'upgrade', 'value', 'forumread');

    notify('Log upgrading was successful!', 'notifysuccess');

    print_continue('index.php');

    print_footer();

    exit;


?>