Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

MDL-30643 - Now loading table definitions from XML files.

  • Loading branch information...
commit d352e733b3729ee820de8859d67c859cda81ee34 1 parent 678f53c
Tyler Bannister authored
Showing with 152 additions and 119 deletions.
  1. +41 −0 lib/db/temp_stats_log_template.xml
  2. +111 −119 lib/statslib.php
View
41 lib/db/temp_stats_log_template.xml
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<XMLDB PATH="lib/db" VERSION="20120631" COMMENT="XMLDB file for statistics temporary log tables"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:noNamespaceSchemaLocation="../../lib/xmldb/xmldb.xsd">
+ <TABLES>
+ <TABLE NAME="temp_log1" COMMENT="Temporary storage for daily logs" NEXT="temp_log2">
+ <FIELDS>
+ <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" NEXT="userid"/>
+ <FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="id" NEXT="course"/>
+ <FIELD NAME="course" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="userid" NEXT="action"/>
+ <FIELD NAME="action" TYPE="char" LENGTH="40" NOTNULL="true" SEQUENCE="false" PREVIOUS="course"/>
+ </FIELDS>
+ <KEYS>
+ <KEY NAME="primary" TYPE="primary" FIELDS="id"/>
+ </KEYS>
+ <INDEXES>
+ <INDEX NAME="action" UNIQUE="false" FIELDS="action" NEXT="course"/>
+ <INDEX NAME="course" UNIQUE="false" FIELDS="course" PREVIOUS="action" NEXT="user"/>
+ <INDEX NAME="user" UNIQUE="false" FIELDS="userid" PREVIOUS="course" NEXT="usercourseaction"/>
+ <INDEX NAME="usercourseaction" UNIQUE="false" FIELDS="userid, course, action" PREVIOUS="user"/>
+ </INDEXES>
+ </TABLE>
+ <TABLE NAME="temp_log2" COMMENT="Duplicate temporary storage for daily logs (work around for MySQL issue)" PREVIOUS="temp_log1">
+ <FIELDS>
+ <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" NEXT="userid"/>
+ <FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="id" NEXT="course"/>
+ <FIELD NAME="course" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="userid" NEXT="action"/>
+ <FIELD NAME="action" TYPE="char" LENGTH="40" NOTNULL="true" SEQUENCE="false" PREVIOUS="course"/>
+ </FIELDS>
+ <KEYS>
+ <KEY NAME="primary" TYPE="primary" FIELDS="id"/>
+ </KEYS>
+ <INDEXES>
+ <INDEX NAME="action" UNIQUE="false" FIELDS="action" NEXT="course"/>
+ <INDEX NAME="course" UNIQUE="false" FIELDS="course" PREVIOUS="action" NEXT="user"/>
+ <INDEX NAME="user" UNIQUE="false" FIELDS="userid" PREVIOUS="course" NEXT="usercourseaction"/>
+ <INDEX NAME="usercourseaction" UNIQUE="false" FIELDS="userid, course, action" PREVIOUS="user"/>
+ </INDEXES>
+ </TABLE>
+ </TABLES>
+</XMLDB>
View
230 lib/statslib.php
@@ -104,7 +104,7 @@ function stats_cron_daily($maxdays=1) {
$now = time();
- $fpcontext = context_course::instance(SITEID, MUST_EXIST);
+ $fpcontext = get_context_instance(CONTEXT_COURSE, SITEID, MUST_EXIST);
// read last execution date from db
if (!$timestart = get_config(NULL, 'statslastdaily')) {
@@ -190,17 +190,17 @@ function stats_cron_daily($maxdays=1) {
break;
}
- stats_progress('in');
+ stats_progress('0');
/// find out if any logs available for this day
- $sql = "SELECT 'x' FROM {tmp_log1} l";
+ $sql = "SELECT 'x' FROM {temp_log1} l";
$logspresent = $DB->get_records_sql($sql, null, 0, 1);
/// process login info first
- $sql = "INSERT INTO {tmp_stats_user_daily} (stattype, timeend, courseid, userid, statsreads)
+ $sql = "INSERT INTO {temp_stats_user_daily} (stattype, timeend, courseid, userid, statsreads)
SELECT 'logins', $nextmidnight AS timeend, ".SITEID." AS courseid, userid, count(id) as statsreads
- FROM {tmp_log1} l
+ FROM {temp_log1} l
WHERE action = 'login'
GROUP BY timeend, courseid, userid
HAVING count(statsreads) > 0";
@@ -211,11 +211,11 @@ function stats_cron_daily($maxdays=1) {
}
stats_progress('1');
- $sql = "INSERT INTO {tmp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
+ $sql = "INSERT INTO {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
SELECT 'logins' AS stattype, $nextmidnight AS timeend, ".SITEID." as courseid, 0,
COALESCE(SUM(statsreads), 0) as stat1, COUNT('x') as stat2
- FROM {tmp_stats_user_daily}
+ FROM {temp_stats_user_daily}
WHERE stattype = 'logins' AND timeend = $nextmidnight";
if ($logspresent and !$DB->execute($sql)) {
@@ -235,7 +235,7 @@ function stats_cron_daily($maxdays=1) {
// in that case, we'll count non-deleted users.
//
- $sql = "INSERT INTO {tmp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
+ $sql = "INSERT INTO {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
SELECT 'enrolments' as stattype, $nextmidnight as timeend, e.courseid, ra.roleid,
COUNT(DISTINCT ue.userid) as stat1, 0 as stat2
@@ -251,33 +251,34 @@ function stats_cron_daily($maxdays=1) {
}
stats_progress('3');
+ // Set stat2 to the number distinct users with role assignments in the course that were active
// using table alias in UPDATE does not work in pg < 8.2
- $sql = "UPDATE {tmp_stats_daily}
+ $sql = "UPDATE {temp_stats_daily}
SET stat2 = (SELECT COUNT(DISTINCT ra.userid)
FROM {role_assignments} ra
JOIN {context} c ON (c.id = ra.contextid AND c.contextlevel = :courselevel)
JOIN {enrol} e ON e.courseid = c.instanceid
JOIN {user_enrolments} ue ON (ue.enrolid = e.id AND ue.userid = ra.userid)
- WHERE ra.roleid = {tmp_stats_daily}.roleid AND
- e.courseid = {tmp_stats_daily}.courseid AND
+ WHERE ra.roleid = {temp_stats_daily}.roleid AND
+ e.courseid = {temp_stats_daily}.courseid AND
EXISTS (SELECT 'x'
- FROM {tmp_log1} l
- WHERE l.course = {tmp_stats_daily}.courseid AND
+ FROM {temp_log1} l
+ WHERE l.course = {temp_stats_daily}.courseid AND
l.userid = ra.userid))
- WHERE {tmp_stats_daily}.stattype = 'enrolments' AND
- {tmp_stats_daily}.timeend = $nextmidnight AND
- {tmp_stats_daily}.courseid IN
+ WHERE {temp_stats_daily}.stattype = 'enrolments' AND
+ {temp_stats_daily}.timeend = $nextmidnight AND
+ {temp_stats_daily}.courseid IN
(SELECT DISTINCT course
- FROM {tmp_log2})";
+ FROM {temp_log2})";
- if (!$DB->execute($sql, array('courselevel'=>CONTEXT_COURSE))) {
+ if ($logspresent and !$DB->execute($sql, array('courselevel'=>CONTEXT_COURSE))) {
$failed = true;
break;
}
stats_progress('4');
- /// now get course total enrolments (roleid==0) - except frontpage
- $sql = "INSERT INTO {tmp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
+ /// now get course total enrolments (roleid==0) - except frontpage
+ $sql = "INSERT INTO {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
SELECT 'enrolments', $nextmidnight AS timeend, e.courseid AS courseid, 0 AS roleid,
COUNT(DISTINCT userid) AS stat1, 0 AS stat2
@@ -292,21 +293,22 @@ function stats_cron_daily($maxdays=1) {
}
stats_progress('5');
- $sql = "UPDATE {tmp_stats_daily}
+ // Set stat 2 to the number of enrolled users who were active in the course
+ $sql = "UPDATE {temp_stats_daily}
SET stat2 = (SELECT COUNT(DISTINCT ue.userid)
FROM {enrol} e
JOIN {user_enrolments} ue ON ue.enrolid = e.id
- WHERE e.courseid = {tmp_stats_daily}.courseid AND
+ WHERE e.courseid = {temp_stats_daily}.courseid AND
EXISTS (SELECT 'x'
- FROM {tmp_log1} l
- WHERE l.course = {tmp_stats_daily}.courseid AND
+ FROM {temp_log1} l
+ WHERE l.course = {temp_stats_daily}.courseid AND
l.userid = ue.userid))
- WHERE {tmp_stats_daily}.stattype = 'enrolments' AND
- {tmp_stats_daily}.timeend = $nextmidnight AND
- {tmp_stats_daily}.roleid = 0 AND
- {tmp_stats_daily}.courseid IN
+ WHERE {temp_stats_daily}.stattype = 'enrolments' AND
+ {temp_stats_daily}.timeend = $nextmidnight AND
+ {temp_stats_daily}.roleid = 0 AND
+ {temp_stats_daily}.courseid IN
(SELECT l.course
- FROM {tmp_log2} l
+ FROM {temp_log2} l
WHERE l.course <> ".SITEID.")";
if ($logspresent and !$DB->execute($sql, array())) {
@@ -315,8 +317,8 @@ function stats_cron_daily($maxdays=1) {
}
stats_progress('6');
- /// frontpage(==site) enrolments total
- $sql = "INSERT INTO {tmp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
+ /// frontpage(==site) enrolments total
+ $sql = "INSERT INTO {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
SELECT 'enrolments', $nextmidnight, ".SITEID.", 0,
(SELECT COUNT('x')
@@ -324,7 +326,7 @@ function stats_cron_daily($maxdays=1) {
WHERE u.deleted = 0) AS stat1,
(SELECT COUNT(DISTINCT u.id)
FROM {user} u
- JOIN {tmp_log1} l ON l.userid = u.id
+ JOIN {temp_log1} l ON l.userid = u.id
WHERE u.deleted = 0) AS stat2" .
$DB->sql_null_from_clause();
@@ -334,11 +336,11 @@ function stats_cron_daily($maxdays=1) {
}
stats_progress('7');
- /// Default frontpage role enrolments are all site users (not deleted)
+ /// Default frontpage role enrolments are all site users (not deleted)
if ($defaultfproleid) {
// first remove default frontpage role counts if created by previous query
$sql = "DELETE
- FROM {tmp_stats_daily}
+ FROM {temp_stats_daily}
WHERE stattype = 'enrolments' AND courseid = ".SITEID." AND
roleid = $defaultfproleid AND timeend = $nextmidnight";
if ($logspresent and !$DB->execute($sql)) {
@@ -347,7 +349,7 @@ function stats_cron_daily($maxdays=1) {
}
stats_progress('8');
- $sql = "INSERT INTO {tmp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
+ $sql = "INSERT INTO {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
SELECT 'enrolments', $nextmidnight, ".SITEID.", $defaultfproleid,
(SELECT COUNT('x')
@@ -355,7 +357,7 @@ function stats_cron_daily($maxdays=1) {
WHERE u.deleted = 0) AS stat1,
(SELECT COUNT(DISTINCT u.id)
FROM {user} u
- JOIN {tmp_log1} l ON l.userid = u.id
+ JOIN {temp_log1} l ON l.userid = u.id
WHERE u.deleted = 0) AS stat2" .
$DB->sql_null_from_clause();;
@@ -371,15 +373,15 @@ function stats_cron_daily($maxdays=1) {
}
- /// individual user stats (including not-logged-in) in each course, this is slow - reuse this data if possible
+ /// individual user stats (including not-logged-in) in each course, this is slow - reuse this data if possible
list($viewactionssql, $params1) = $DB->get_in_or_equal($viewactions, SQL_PARAMS_NAMED, 'view');
list($postactionssql, $params2) = $DB->get_in_or_equal($postactions, SQL_PARAMS_NAMED, 'post');
- $sql = "INSERT INTO {tmp_stats_user_daily} (stattype, timeend, courseid, userid, statsreads, statswrites)
+ $sql = "INSERT INTO {temp_stats_user_daily} (stattype, timeend, courseid, userid, statsreads, statswrites)
SELECT 'activity' AS stattype, $nextmidnight AS timeend, course as courseid, userid,
SUM(CASE WHEN action $viewactionssql THEN 1 ELSE 0 END) AS statsreads,
SUM(CASE WHEN action $postactionssql THEN 1 ELSE 0 END) AS statswrites
- FROM {tmp_log1} l
+ FROM {temp_log1} l
WHERE !(course = 0 AND userid = 0)
GROUP BY userid, courseid";
@@ -390,13 +392,13 @@ function stats_cron_daily($maxdays=1) {
stats_progress('10');
- /// how many view/post actions in each course total
- $sql = "INSERT INTO {tmp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
+ /// how many view/post actions in each course total
+ $sql = "INSERT INTO {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
SELECT 'activity' AS stattype, $nextmidnight AS timeend, c.id AS courseid, 0,
SUM(CASE WHEN l.action $viewactionssql THEN 1 ELSE 0 END) AS stat1,
SUM(CASE WHEN l.action $postactionssql THEN 1 ELSE 0 END) AS stat2
- FROM {course} c, {tmp_log1} l
+ FROM {course} c, {temp_log1} l
WHERE l.course = c.id
GROUP BY courseid";
@@ -407,14 +409,14 @@ function stats_cron_daily($maxdays=1) {
stats_progress('11');
- /// how many view actions for each course+role - excluding guests and frontpage
+ /// how many view actions for each course+role - excluding guests and frontpage
- $sql = "INSERT INTO {tmp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
+ $sql = "INSERT INTO {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
SELECT 'activity', $nextmidnight AS timeend, courseid, roleid, SUM(statsreads), SUM(statswrites)
FROM (
SELECT pl.courseid, pl.roleid, sud.statsreads, sud.statswrites
- FROM {tmp_stats_user_daily} sud,
+ FROM {temp_stats_user_daily} sud,
(SELECT DISTINCT ra.userid, ra.roleid, e.courseid
FROM {role_assignments} ra
JOIN {context} c ON (c.id = ra.contextid AND c.contextlevel = :courselevel)
@@ -437,16 +439,16 @@ function stats_cron_daily($maxdays=1) {
}
stats_progress('12');
- /// how many view actions from guests only in each course - excluding frontpage
- /// normal users may enter course with temporary guest access too
+ /// how many view actions from guests only in each course - excluding frontpage
+ /// normal users may enter course with temporary guest access too
- $sql = "INSERT INTO {tmp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
+ $sql = "INSERT INTO {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
SELECT 'activity', $nextmidnight as timeend, courseid, $guestrole AS roleid,
SUM(statsreads), SUM(statswrites)
FROM (
SELECT sud.courseid, sud.statsreads, sud.statswrites
- FROM {tmp_stats_user_daily} sud
+ FROM {temp_stats_user_daily} sud
WHERE sud.timeend = $nextmidnight AND sud.courseid <> ".SITEID." AND
sud.stattype='activity' AND
(sud.userid = $guest OR sud.userid
@@ -465,14 +467,14 @@ function stats_cron_daily($maxdays=1) {
stats_progress('13');
- /// how many view actions for each role on frontpage - excluding guests, not-logged-in and default frontpage role
- $sql = "INSERT INTO {tmp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
+ /// how many view actions for each role on frontpage - excluding guests, not-logged-in and default frontpage role
+ $sql = "INSERT INTO {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
SELECT 'activity', $nextmidnight AS timeend, courseid, roleid,
SUM(statsreads), SUM(statswrites)
FROM (
SELECT pl.courseid, pl.roleid, sud.statsreads, sud.statswrites
- FROM {tmp_stats_user_daily} sud,
+ FROM {temp_stats_user_daily} sud,
(SELECT DISTINCT ra.userid, ra.roleid, c.instanceid AS courseid
FROM {role_assignments} ra
JOIN {context} c ON c.id = ra.contextid
@@ -496,14 +498,14 @@ function stats_cron_daily($maxdays=1) {
stats_progress('14');
- /// how many view actions for default frontpage role on frontpage only
- $sql = "INSERT INTO {tmp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
+ /// how many view actions for default frontpage role on frontpage only
+ $sql = "INSERT INTO {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
SELECT 'activity', timeend, courseid, $defaultfproleid AS roleid,
SUM(statsreads), SUM(statswrites)
FROM (
SELECT sud.timeend AS timeend, sud.courseid, sud.statsreads, sud.statswrites
- FROM {tmp_stats_user_daily} sud
+ FROM {temp_stats_user_daily} sud
WHERE sud.timeend = :nextm AND sud.courseid = :siteid AND
sud.stattype='activity' AND
sud.userid <> $guest AND sud.userid <> 0 AND sud.userid
@@ -521,8 +523,8 @@ function stats_cron_daily($maxdays=1) {
}
stats_progress('15');
- /// how many view actions for guests or not-logged-in on frontpage
- $sql = "INSERT INTO {tmp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
+ /// how many view actions for guests or not-logged-in on frontpage
+ $sql = "INSERT INTO {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2)
SELECT 'activity', $nextmidnight AS timeend, ".SITEID." AS courseid, $guestrole AS roleid,
SUM(statsreads), SUM(statswrites)
@@ -530,7 +532,7 @@ function stats_cron_daily($maxdays=1) {
SELECT pl.statsreads, pl.statswrites
FROM (
SELECT sud.statsreads, sud.statswrites
- FROM {tmp_stats_user_daily} sud
+ FROM {temp_stats_user_daily} sud
WHERE (sud.userid = $guest OR sud.userid = 0) AND
sud.timeend = $nextmidnight AND sud.courseid = ".SITEID." AND
sud.stattype='activity'
@@ -565,11 +567,11 @@ function stats_cron_daily($maxdays=1) {
if ($failed) {
$days--;
- mtrace("...error occurred, completed $days days of statistics.");
+ mtrace("...error occurred, completed $days days of statistics in {$total} s.");
return false;
} else if ($timeout) {
- mtrace("...stopping early, reached maximum number of $maxdays days - will continue next time.");
+ mtrace("...stopping early, reached maximum number of $maxdays days ({$total} s) - will continue next time.");
return false;
} else {
@@ -707,7 +709,7 @@ function stats_cron_weekly() {
set_config('statslastweekly', $nextstartweek);
$elapsed = time()-$weekstart;
- mtrace(" finished until $nextstartweek: ".userdate($nextstartweek) ." ( in $elapsed s)");
+ mtrace(" finished until $nextstartweek: ".userdate($nextstartweek) ." (in $elapsed s)");
$timestart = $nextstartweek;
$nextstartweek = stats_get_next_week_start($nextstartweek);
@@ -1346,7 +1348,7 @@ function stats_get_report_options($courseid,$mode) {
switch ($mode) {
case STATS_MODE_GENERAL:
$reportoptions[STATS_REPORT_ACTIVITY] = get_string('statsreport'.STATS_REPORT_ACTIVITY);
- if ($courseid != SITEID && $context = context_course::instance($courseid)) {
+ if ($courseid != SITEID && $context = get_context_instance(CONTEXT_COURSE, $courseid)) {
$sql = 'SELECT r.id, r.name FROM {role} r JOIN {stats_daily} s ON s.roleid = r.id WHERE s.courseid = :courseid GROUP BY r.id, r.name';
if ($roles = $DB->get_records_sql($sql, array('courseid' => $courseid))) {
foreach ($roles as $role) {
@@ -1364,13 +1366,13 @@ function stats_get_report_options($courseid,$mode) {
case STATS_MODE_DETAILED:
$reportoptions[STATS_REPORT_USER_ACTIVITY] = get_string('statsreport'.STATS_REPORT_USER_ACTIVITY);
$reportoptions[STATS_REPORT_USER_ALLACTIVITY] = get_string('statsreport'.STATS_REPORT_USER_ALLACTIVITY);
- if (has_capability('report/stats:view', context_system::instance())) {
+ if (has_capability('report/stats:view', get_context_instance(CONTEXT_SYSTEM))) {
$site = get_site();
$reportoptions[STATS_REPORT_USER_LOGINS] = get_string('statsreport'.STATS_REPORT_USER_LOGINS);
}
break;
case STATS_MODE_RANKED:
- if (has_capability('report/stats:view', context_system::instance())) {
+ if (has_capability('report/stats:view', get_context_instance(CONTEXT_SYSTEM))) {
$reportoptions[STATS_REPORT_ACTIVE_COURSES] = get_string('statsreport'.STATS_REPORT_ACTIVE_COURSES);
$reportoptions[STATS_REPORT_ACTIVE_COURSES_WEIGHTED] = get_string('statsreport'.STATS_REPORT_ACTIVE_COURSES_WEIGHTED);
$reportoptions[STATS_REPORT_PARTICIPATORY_COURSES] = get_string('statsreport'.STATS_REPORT_PARTICIPATORY_COURSES);
@@ -1487,60 +1489,50 @@ function stats_temp_table_create() {
stats_temp_table_drop();
- $log = new xmldb_table('tmp_log1');
-
- $log->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
- $log->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
- $log->add_field('course', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null);
- $log->add_field('action', XMLDB_TYPE_CHAR, '40', null, XMLDB_NOTNULL, null, null);
-
- $log->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
- $log->add_index('tmp_tl_course_ix', XMLDB_INDEX_NOTUNIQUE, array('course'));
- $log->add_index('tmp_tl_act_ix', XMLDB_INDEX_NOTUNIQUE, array('action'));
- $log->add_index('tmp_tl_user_ix', XMLDB_INDEX_NOTUNIQUE, array('userid'));
- $log->add_index('tmp_tl_usecouact_ix', XMLDB_INDEX_NOTUNIQUE, array('userid','course','action'));
-
- $user = new xmldb_table('tmp_stats_daily');
- $user->add_field('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
- $user->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
- $user->add_field('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, null, 'activity');
- $user->add_field('timeend', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
- $user->add_field('stat1', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
- $user->add_field('stat2', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
-
- $user->add_index('tmp_tsd_courseid_ix', XMLDB_INDEX_NOTUNIQUE, array('courseid'));
- $user->add_index('tmp_tsd_roleid_ix', XMLDB_INDEX_NOTUNIQUE, array('roleid'));
- $user->add_index('tmp_tsd_statype_ix', XMLDB_INDEX_NOTUNIQUE, array('stattype'));
- $user->add_index('tmp_tsd_timeend_ix', XMLDB_INDEX_NOTUNIQUE, array('timeend'));
-
- $daily = new xmldb_table('tmp_stats_user_daily');
- $daily->add_field('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
- $daily->add_field('userid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
- $daily->add_field('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
- $daily->add_field('timeend', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
- $daily->add_field('statsreads', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
- $daily->add_field('statswrites', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, 0);
- $daily->add_field('stattype', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null);
-
- $daily->add_index('tmp_tsud_courseid_ix', XMLDB_INDEX_NOTUNIQUE, array('courseid'));
- $daily->add_index('tmp_tsud_userid_ix', XMLDB_INDEX_NOTUNIQUE, array('userid'));
- $daily->add_index('tmp_tsud_roleid_ix', XMLDB_INDEX_NOTUNIQUE, array('roleid'));
- $daily->add_index('tmp_tsud_stattype_ix', XMLDB_INDEX_NOTUNIQUE, array('stattype'));
- $daily->add_index('tmp_tsud_timeend_ix', XMLDB_INDEX_NOTUNIQUE, array('timeend'));
+ $xmlfile = $CFG->dirroot . '/lib/db/install.xml';
+ $tempfile = $CFG->dirroot . '/lib/db/temp_stats_log_template.xml';
+ $tables = array();
+ $files = array(
+ $xmlfile => array('stats_daily', 'stats_user_daily'),
+ $tempfile => array('temp_log1', 'temp_log2'),
+ );
+
+ foreach ($files as $file => $contents) {
+
+ $xmldb_file = new xmldb_file($file);
+ if (!$xmldb_file->fileExists()) {
+ throw new ddl_exception('ddlxmlfileerror', null, 'File does not exist');
+ }
+ $loaded = $xmldb_file->loadXMLStructure();
+ if (!$loaded || !$xmldb_file->isLoaded()) {
+ throw new ddl_exception('ddlxmlfileerror', null, 'not loaded??');
+ }
+ $xmldb_structure = $xmldb_file->getStructure();
- try {
- $dbman->create_temp_table($log);
+ foreach ($contents as $name) {
+ $table = $xmldb_structure->getTable($name);
- $log->name = 'tmp_log2';
+ if (is_null($table)) {
+ throw new ddl_exception('ddlunknowntable', null, 'The table '. $name .' is not defined in the file '. $xmlfile);
+ }
+ $table->setNext(null);
+ $table->setPrevious(null);
+ $tables[$name] = $table;
+ }
+ }
- $dbman->create_temp_table($log);
+ // Rename the standard table templates so that they don't conflict with the standard tables
+ $tables['stats_daily']->setName('temp_stats_daily');
+ $tables['stats_user_daily']->setName('temp_stats_user_daily');
- $dbman->create_temp_table($user);
+ try {
- $dbman->create_temp_table($daily);
+ foreach ($tables as $table) {
+ $dbman->create_temp_table($table);
+ }
} catch (Exception $e) {
- mtrace("Temporary table creation failed!");
+ mtrace('Temporary table creation failed: '. $e->getMessage());
return false;
}
@@ -1555,7 +1547,7 @@ function stats_temp_table_drop() {
$dbman = $DB->get_manager();
- $tables = array('tmp_log1', 'tmp_log2', 'tmp_stats_daily', 'tmp_stats_user_daily');
+ $tables = array('temp_log1', 'temp_log2', 'temp_stats_daily', 'temp_stats_user_daily');
foreach ($tables as $name) {
@@ -1563,7 +1555,7 @@ function stats_temp_table_drop() {
$table = new xmldb_table($name);
try {
- $dbman->drop_temp_table($table);
+ $dbman->drop_table($table);
} catch (Exception $e) {
mtrace("Error occured while dropping temporary tables!");
}
@@ -1581,7 +1573,7 @@ function stats_temp_table_drop() {
function stats_temp_table_fill($timestart, $timeend) {
global $DB;
- $sql = "INSERT INTO {tmp_log1}
+ $sql = "INSERT INTO {temp_log1}
SELECT id, userid, course, action FROM {log} l
WHERE l.time >= $timestart AND l.time < $timeend";
@@ -1589,8 +1581,8 @@ function stats_temp_table_fill($timestart, $timeend) {
return false;
}
- $sql = "INSERT INTO {tmp_log2}
- SELECT * FROM {tmp_log1}";
+ $sql = "INSERT INTO {temp_log2}
+ SELECT * FROM {temp_log1}";
if (!$DB->execute($sql)) {
return false;
@@ -1611,15 +1603,15 @@ function stats_temp_table_clean() {
$sql = array();
$sql['up1'] = 'INSERT INTO {stats_daily} (courseid, roleid, stattype, timeend, stat1, stat2)'
- .' SELECT courseid, roleid, stattype, timeend, stat1, stat2 FROM {tmp_stats_daily}';
+ .' SELECT courseid, roleid, stattype, timeend, stat1, stat2 FROM {temp_stats_daily}';
$sql['up2'] = 'INSERT INTO {stats_user_daily}'
.' (courseid, userid, roleid, timeend, statsreads, statswrites, stattype)'
.' SELECT courseid, userid, roleid, timeend, statsreads, statswrites, stattype'
- .' FROM {tmp_stats_user_daily}';
+ .' FROM {temp_stats_user_daily}';
$x = 1;
- $tables = array('tmp_log1', 'tmp_log2', 'tmp_stats_daily', 'tmp_stats_user_daily');
+ $tables = array('temp_log1', 'temp_log2', 'temp_stats_daily', 'temp_stats_user_daily');
foreach ($tables as $name) {
$sql['tr'. $x] = "TRUNCATE TABLE {{$name}}";
Please sign in to comment.
Something went wrong with that request. Please try again.