More efficient querying of courses (from Martin Langhoff)
[moodle.git] / admin / upgradelogs.php
blobdc7fabdaf1412f32b0b1b18315256da32ece1573
1 <?PHP //$Id$
3 require("../config.php");
5 optional_variable($confirm);
7 require_login();
9 if (!isadmin()) {
10 error("You must be an admin to use this script");
13 if ($CFG->version < 2004013101) {
14 error("This script does not work with this old version of Moodle");
17 if (!$site = get_site()) {
18 redirect("index.php");
21 /// Turn off time limits, sometimes upgrades can be slow.
23 @set_time_limit(0);
26 /// Print header
28 $stradministration = get_string("administration");
29 $strupgradinglogs = get_string("upgradinglogs", "admin");
31 print_header("$site->shortname: $stradministration: $strupgradinglogs", "$site->fullname",
32 "<a href=\"index.php\">$stradministration</a> -> $strupgradinglogs");
34 if (empty($_GET['confirm'])) {
35 notice_yesno(get_string("upgradelogsinfo", "admin"),
36 "upgradelogs.php?confirm=true&sesskey=$USER->sesskey",
37 "index.php");
38 print_footer();
39 exit;
40 } else if (!confirm_sesskey()) {
41 error(get_string('confirmsesskeybad', 'error'));
45 /// Try and extract as many cmids as possible from the existing logs
47 if ($coursemodules = get_records_sql("SELECT cm.*, m.name
48 FROM {$CFG->prefix}course_modules cm,
49 {$CFG->prefix}modules m
50 WHERE cm.module = m.id")) {
51 $cmcount = count($coursemodules);
52 $count = 0;
53 $starttime = time();
54 $sleeptime = 0;
56 if ($CFG->dbtype == "mysql") {
57 $LIKE = "LIKE";
58 } else {
59 $LIKE = "ILIKE";
62 if ($cmcount > 10) {
63 print_simple_box('This process may take a very long time ... please be patient and let it finish.',
64 'center', '', '#ffcccc');
65 $sleeptime = 1;
67 foreach ($coursemodules as $cm) {
69 switch ($cm->name) {
70 case "forum":
71 execute_sql("UPDATE {$CFG->prefix}log SET cmid = '$cm->id'
72 WHERE module = '$cm->name' AND url = 'view.php?id=$cm->id'", false);
74 execute_sql("UPDATE {$CFG->prefix}log SET cmid = '$cm->id'
75 WHERE module = '$cm->name' AND url = 'view.php?f=$cm->instance'", false);
77 if ($discussions = get_records("forum_discussions", "forum", $cm->instance)) {
78 foreach ($discussions as $discussion) {
79 execute_sql("UPDATE {$CFG->prefix}log SET cmid = '$cm->id'
80 WHERE module = '$cm->name' AND url $LIKE 'discuss.php?d=$discussion->id%'", false);
83 break;
85 case "glossary":
86 execute_sql("UPDATE {$CFG->prefix}log SET cmid = '$cm->id'
87 WHERE module = '$cm->name' AND url $LIKE 'view.php?id=$cm->id%'", false);
88 break;
90 case "quiz":
91 execute_sql("UPDATE {$CFG->prefix}log SET cmid = '$cm->id'
92 WHERE module = '$cm->name' AND url = 'view.php?id=$cm->id'", false);
93 break;
95 case "assignment":
96 execute_sql("UPDATE {$CFG->prefix}log SET cmid = '$cm->id'
97 WHERE module = '$cm->name' AND url = 'view.php?id=$cm->id'", false);
98 execute_sql("UPDATE {$CFG->prefix}log SET cmid = '$cm->id'
99 WHERE module = '$cm->name' AND url = 'view.php?a=$cm->instance'", false);
100 execute_sql("UPDATE {$CFG->prefix}log SET cmid = '$cm->id'
101 WHERE module = '$cm->name' AND url = 'submissions.php?id=$cm->instance'", false);
102 break;
104 case "journal":
105 execute_sql("UPDATE {$CFG->prefix}log SET cmid = '$cm->id'
106 WHERE module = '$cm->name' AND url = 'report.php?id=$cm->id'", false);
108 execute_sql("UPDATE {$CFG->prefix}log SET cmid = '$cm->id'
109 WHERE module = '$cm->name' AND url = 'view.php?id=$cm->id'", false);
110 break;
114 $count++;
115 $elapsedtime = time() - $starttime;
116 $projectedtime = (int)(((float)$cmcount / (float)$count) * $elapsedtime) - $elapsedtime;
118 if ($cmcount > 10) {
119 notify("Processed $count of $cmcount coursemodules. Estimated completion: ".format_time($projectedtime));
120 flush();
121 sleep($sleeptime); // To help reduce database load
126 delete_records("config", "name", "upgrade", "value", "logs");
128 notify("Log upgrading was successful!");
131 print_footer();