diff --git a/lib/db/temp_stats_log_template.xml b/lib/db/temp_stats_log_template.xml deleted file mode 100644 index 78681cb5629..00000000000 --- a/lib/db/temp_stats_log_template.xml +++ /dev/null @@ -1,41 +0,0 @@ - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - -
-
-
\ No newline at end of file diff --git a/lib/statslib.php b/lib/statslib.php index ed720665fca..f44ae1ff571 100644 --- a/lib/statslib.php +++ b/lib/statslib.php @@ -104,7 +104,7 @@ function stats_cron_daily($maxdays=1) { $now = time(); - $fpcontext = get_context_instance(CONTEXT_COURSE, SITEID, MUST_EXIST); + $fpcontext = context_course::instance(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('0'); + stats_progress('in'); /// find out if any logs available for this day - $sql = "SELECT 'x' FROM {temp_log1} l"; + $sql = "SELECT 'x' FROM {tmp_log1} l"; $logspresent = $DB->get_records_sql($sql, null, 0, 1); /// process login info first - $sql = "INSERT INTO {temp_stats_user_daily} (stattype, timeend, courseid, userid, statsreads) + $sql = "INSERT INTO {tmp_stats_user_daily} (stattype, timeend, courseid, userid, statsreads) SELECT 'logins', $nextmidnight AS timeend, ".SITEID." AS courseid, userid, count(id) as statsreads - FROM {temp_log1} l + FROM {tmp_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 {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2) + $sql = "INSERT INTO {tmp_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 {temp_stats_user_daily} + FROM {tmp_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 {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2) + $sql = "INSERT INTO {tmp_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,34 +251,33 @@ 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 {temp_stats_daily} + $sql = "UPDATE {tmp_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 = {temp_stats_daily}.roleid AND - e.courseid = {temp_stats_daily}.courseid AND + WHERE ra.roleid = {tmp_stats_daily}.roleid AND + e.courseid = {tmp_stats_daily}.courseid AND EXISTS (SELECT 'x' - FROM {temp_log1} l - WHERE l.course = {temp_stats_daily}.courseid AND + FROM {tmp_log1} l + WHERE l.course = {tmp_stats_daily}.courseid AND l.userid = ra.userid)) - WHERE {temp_stats_daily}.stattype = 'enrolments' AND - {temp_stats_daily}.timeend = $nextmidnight AND - {temp_stats_daily}.courseid IN + WHERE {tmp_stats_daily}.stattype = 'enrolments' AND + {tmp_stats_daily}.timeend = $nextmidnight AND + {tmp_stats_daily}.courseid IN (SELECT DISTINCT course - FROM {temp_log2})"; + FROM {tmp_log2})"; - if ($logspresent and !$DB->execute($sql, array('courselevel'=>CONTEXT_COURSE))) { + if (!$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 {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2) + /// now get course total enrolments (roleid==0) - except frontpage + $sql = "INSERT INTO {tmp_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 @@ -293,22 +292,21 @@ function stats_cron_daily($maxdays=1) { } stats_progress('5'); - // Set stat 2 to the number of enrolled users who were active in the course - $sql = "UPDATE {temp_stats_daily} + $sql = "UPDATE {tmp_stats_daily} SET stat2 = (SELECT COUNT(DISTINCT ue.userid) FROM {enrol} e JOIN {user_enrolments} ue ON ue.enrolid = e.id - WHERE e.courseid = {temp_stats_daily}.courseid AND + WHERE e.courseid = {tmp_stats_daily}.courseid AND EXISTS (SELECT 'x' - FROM {temp_log1} l - WHERE l.course = {temp_stats_daily}.courseid AND + FROM {tmp_log1} l + WHERE l.course = {tmp_stats_daily}.courseid AND l.userid = ue.userid)) - WHERE {temp_stats_daily}.stattype = 'enrolments' AND - {temp_stats_daily}.timeend = $nextmidnight AND - {temp_stats_daily}.roleid = 0 AND - {temp_stats_daily}.courseid IN + WHERE {tmp_stats_daily}.stattype = 'enrolments' AND + {tmp_stats_daily}.timeend = $nextmidnight AND + {tmp_stats_daily}.roleid = 0 AND + {tmp_stats_daily}.courseid IN (SELECT l.course - FROM {temp_log2} l + FROM {tmp_log2} l WHERE l.course <> ".SITEID.")"; if ($logspresent and !$DB->execute($sql, array())) { @@ -317,8 +315,8 @@ function stats_cron_daily($maxdays=1) { } stats_progress('6'); - /// frontpage(==site) enrolments total - $sql = "INSERT INTO {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2) + /// frontpage(==site) enrolments total + $sql = "INSERT INTO {tmp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2) SELECT 'enrolments', $nextmidnight, ".SITEID.", 0, (SELECT COUNT('x') @@ -326,7 +324,7 @@ function stats_cron_daily($maxdays=1) { WHERE u.deleted = 0) AS stat1, (SELECT COUNT(DISTINCT u.id) FROM {user} u - JOIN {temp_log1} l ON l.userid = u.id + JOIN {tmp_log1} l ON l.userid = u.id WHERE u.deleted = 0) AS stat2" . $DB->sql_null_from_clause(); @@ -336,11 +334,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 {temp_stats_daily} + FROM {tmp_stats_daily} WHERE stattype = 'enrolments' AND courseid = ".SITEID." AND roleid = $defaultfproleid AND timeend = $nextmidnight"; if ($logspresent and !$DB->execute($sql)) { @@ -349,7 +347,7 @@ function stats_cron_daily($maxdays=1) { } stats_progress('8'); - $sql = "INSERT INTO {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2) + $sql = "INSERT INTO {tmp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2) SELECT 'enrolments', $nextmidnight, ".SITEID.", $defaultfproleid, (SELECT COUNT('x') @@ -357,7 +355,7 @@ function stats_cron_daily($maxdays=1) { WHERE u.deleted = 0) AS stat1, (SELECT COUNT(DISTINCT u.id) FROM {user} u - JOIN {temp_log1} l ON l.userid = u.id + JOIN {tmp_log1} l ON l.userid = u.id WHERE u.deleted = 0) AS stat2" . $DB->sql_null_from_clause();; @@ -373,15 +371,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 {temp_stats_user_daily} (stattype, timeend, courseid, userid, statsreads, statswrites) + $sql = "INSERT INTO {tmp_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 {temp_log1} l + FROM {tmp_log1} l WHERE !(course = 0 AND userid = 0) GROUP BY userid, courseid"; @@ -392,13 +390,13 @@ function stats_cron_daily($maxdays=1) { stats_progress('10'); - /// how many view/post actions in each course total - $sql = "INSERT INTO {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2) + /// how many view/post actions in each course total + $sql = "INSERT INTO {tmp_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, {temp_log1} l + FROM {course} c, {tmp_log1} l WHERE l.course = c.id GROUP BY courseid"; @@ -409,14 +407,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 {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2) + $sql = "INSERT INTO {tmp_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 {temp_stats_user_daily} sud, + FROM {tmp_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) @@ -439,16 +437,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 {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2) + $sql = "INSERT INTO {tmp_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 {temp_stats_user_daily} sud + FROM {tmp_stats_user_daily} sud WHERE sud.timeend = $nextmidnight AND sud.courseid <> ".SITEID." AND sud.stattype='activity' AND (sud.userid = $guest OR sud.userid @@ -467,14 +465,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 {temp_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 {tmp_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 {temp_stats_user_daily} sud, + FROM {tmp_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 @@ -498,14 +496,14 @@ function stats_cron_daily($maxdays=1) { stats_progress('14'); - /// how many view actions for default frontpage role on frontpage only - $sql = "INSERT INTO {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2) + /// how many view actions for default frontpage role on frontpage only + $sql = "INSERT INTO {tmp_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 {temp_stats_user_daily} sud + FROM {tmp_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 @@ -523,8 +521,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 {temp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2) + /// how many view actions for guests or not-logged-in on frontpage + $sql = "INSERT INTO {tmp_stats_daily} (stattype, timeend, courseid, roleid, stat1, stat2) SELECT 'activity', $nextmidnight AS timeend, ".SITEID." AS courseid, $guestrole AS roleid, SUM(statsreads), SUM(statswrites) @@ -532,7 +530,7 @@ function stats_cron_daily($maxdays=1) { SELECT pl.statsreads, pl.statswrites FROM ( SELECT sud.statsreads, sud.statswrites - FROM {temp_stats_user_daily} sud + FROM {tmp_stats_user_daily} sud WHERE (sud.userid = $guest OR sud.userid = 0) AND sud.timeend = $nextmidnight AND sud.courseid = ".SITEID." AND sud.stattype='activity' @@ -567,11 +565,11 @@ function stats_cron_daily($maxdays=1) { if ($failed) { $days--; - mtrace("...error occurred, completed $days days of statistics in {$total} s."); + mtrace("...error occurred, completed $days days of statistics."); return false; } else if ($timeout) { - mtrace("...stopping early, reached maximum number of $maxdays days ({$total} s) - will continue next time."); + mtrace("...stopping early, reached maximum number of $maxdays days - will continue next time."); return false; } else { @@ -709,7 +707,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); @@ -1348,7 +1346,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 = get_context_instance(CONTEXT_COURSE, $courseid)) { + if ($courseid != SITEID && $context = context_course::instance($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) { @@ -1366,13 +1364,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', get_context_instance(CONTEXT_SYSTEM))) { + if (has_capability('report/stats:view', context_system::instance())) { $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', get_context_instance(CONTEXT_SYSTEM))) { + if (has_capability('report/stats:view', context_system::instance())) { $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); @@ -1489,50 +1487,60 @@ function stats_temp_table_create() { stats_temp_table_drop(); - $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'), - ); + $log = new xmldb_table('tmp_log1'); - foreach ($files as $file => $contents) { + $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); - $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(); + $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')); - foreach ($contents as $name) { - $table = $xmldb_structure->getTable($name); + $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); - 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; - } - } + $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')); - // 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'); + $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')); try { + $dbman->create_temp_table($log); - foreach ($tables as $table) { - $dbman->create_temp_table($table); - } + $log->name = 'tmp_log2'; + + $dbman->create_temp_table($log); + + $dbman->create_temp_table($user); + + $dbman->create_temp_table($daily); } catch (Exception $e) { - mtrace('Temporary table creation failed: '. $e->getMessage()); + mtrace("Temporary table creation failed!"); return false; } @@ -1547,7 +1555,7 @@ function stats_temp_table_drop() { $dbman = $DB->get_manager(); - $tables = array('temp_log1', 'temp_log2', 'temp_stats_daily', 'temp_stats_user_daily'); + $tables = array('tmp_log1', 'tmp_log2', 'tmp_stats_daily', 'tmp_stats_user_daily'); foreach ($tables as $name) { @@ -1555,7 +1563,7 @@ function stats_temp_table_drop() { $table = new xmldb_table($name); try { - $dbman->drop_table($table); + $dbman->drop_temp_table($table); } catch (Exception $e) { mtrace("Error occured while dropping temporary tables!"); } @@ -1573,7 +1581,7 @@ function stats_temp_table_drop() { function stats_temp_table_fill($timestart, $timeend) { global $DB; - $sql = "INSERT INTO {temp_log1} + $sql = "INSERT INTO {tmp_log1} SELECT id, userid, course, action FROM {log} l WHERE l.time >= $timestart AND l.time < $timeend"; @@ -1581,8 +1589,8 @@ function stats_temp_table_fill($timestart, $timeend) { return false; } - $sql = "INSERT INTO {temp_log2} - SELECT * FROM {temp_log1}"; + $sql = "INSERT INTO {tmp_log2} + SELECT * FROM {tmp_log1}"; if (!$DB->execute($sql)) { return false; @@ -1603,15 +1611,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 {temp_stats_daily}'; + .' SELECT courseid, roleid, stattype, timeend, stat1, stat2 FROM {tmp_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 {temp_stats_user_daily}'; + .' FROM {tmp_stats_user_daily}'; $x = 1; - $tables = array('temp_log1', 'temp_log2', 'temp_stats_daily', 'temp_stats_user_daily'); + $tables = array('tmp_log1', 'tmp_log2', 'tmp_stats_daily', 'tmp_stats_user_daily'); foreach ($tables as $name) { $sql['tr'. $x] = "TRUNCATE TABLE {{$name}}";