diff --git a/lib/db/install.xml b/lib/db/install.xml
index d1cfa55916f..97d1977aff0 100644
--- a/lib/db/install.xml
+++ b/lib/db/install.xml
@@ -2882,7 +2882,7 @@
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/lib/statslib.php b/lib/statslib.php
index eee36ec741f..3d201fd1829 100644
--- a/lib/statslib.php
+++ b/lib/statslib.php
@@ -103,7 +103,7 @@ function stats_progress($ident) {
* @param string $sql The query to run
* @return boolean success
*/
-function stats_run_query($sql, $parameters) {
+function stats_run_query($sql, $parameters = array()) {
global $DB;
try {
@@ -190,7 +190,13 @@ function stats_cron_daily($maxdays=1) {
$days = 1;
$failed = true;
}
- mtrace("Temporary tables created");
+ mtrace('Temporary tables created');
+
+ if(!stats_temp_table_setup()) {
+ $days = 1;
+ $failed = true;
+ }
+ mtrace('Enrolments calculated');
$totalactiveusers = $DB->count_records('user', array('deleted' => '0'));
@@ -281,15 +287,12 @@ function stats_cron_daily($maxdays=1) {
$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
- 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)
+ SELECT 'enrolments' as stattype, $nextmidnight as timeend, courseid, roleid,
+ COUNT(DISTINCT userid) as stat1, 0 as stat2
+ FROM {temp_enroled}
GROUP BY courseid, roleid";
- if (!stats_run_query($sql, array('courselevel'=>CONTEXT_COURSE))) {
+ if (!stats_run_query($sql)) {
$failed = true;
break;
}
@@ -300,28 +303,23 @@ function stats_cron_daily($maxdays=1) {
$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 = {temp_stats_daily}.roleid
- AND e.courseid = {temp_stats_daily}.courseid
+ SELECT COUNT(DISTINCT userid)
+ FROM {temp_enroled} te
+ WHERE roleid = {temp_stats_daily}.roleid
+ AND courseid = {temp_stats_daily}.courseid
AND EXISTS (
SELECT 'x'
FROM {temp_log1} l
WHERE l.course = {temp_stats_daily}.courseid
- AND l.userid = ra.userid
+ AND l.userid = te.userid
)
- )
+ )
+ WHERE {temp_stats_daily}.stattype = 'enrolments'
+ AND {temp_stats_daily}.timeend = $nextmidnight
+ AND {temp_stats_daily}.courseid IN (
- WHERE {stats_daily}.stattype = 'enrolments'
- AND {stats_daily}.timeend = $nextmidnight
- AND {stats_daily}.courseid IN (
-
- SELECT DISTINCT course FROM {temp_log2}
- )";
+ SELECT DISTINCT course FROM {temp_log2})";
if ($logspresent && !stats_run_query($sql, array('courselevel'=>CONTEXT_COURSE))) {
$failed = true;
@@ -332,10 +330,9 @@ function stats_cron_daily($maxdays=1) {
// 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,
+ SELECT 'enrolments', $nextmidnight AS timeend, te.courseid AS courseid, 0 AS roleid,
COUNT(DISTINCT userid) AS stat1, 0 AS stat2
- FROM {enrol} e
- JOIN {user_enrolments} ue ON ue.enrolid = e.id
+ FROM {temp_enroled} te
GROUP BY courseid
HAVING COUNT(DISTINCT userid) > 0";
@@ -349,27 +346,26 @@ function stats_cron_daily($maxdays=1) {
$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 = {temp_stats_daily}.courseid
- AND EXISTS (
+ SELECT COUNT(DISTINCT te.userid)
+ FROM {temp_enroled} te
+ WHERE te.courseid = {temp_stats_daily}.courseid
+ AND EXISTS (
- SELECT 'x'
- FROM {temp_log1} l
- WHERE l.course = {temp_stats_daily}.courseid
- AND l.userid = ue.userid
- )
- )
+ SELECT 'x'
+ FROM {temp_log1} l
+ WHERE l.course = {temp_stats_daily}.courseid
+ AND l.userid = te.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 (
- SELECT l.course
- FROM {temp_log2} l
- WHERE l.course <> ".SITEID.")";
+ SELECT l.course
+ FROM {temp_log2} l
+ WHERE l.course <> ".SITEID.")";
if ($logspresent && !stats_run_query($sql, array())) {
$failed = true;
@@ -469,14 +465,12 @@ function stats_cron_daily($maxdays=1) {
SELECT pl.courseid, pl.roleid, sud.statsreads, sud.statswrites
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)
- 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 <> $guestrole
- AND ra.userid <> $guest
- ) pl
+ SELECT DISTINCT te.userid, te.roleid, te.courseid
+ FROM {temp_enroled} te
+ WHERE te.roleid <> $guestrole
+ AND te.userid <> $guest
+ ) pl
+
WHERE sud.userid = pl.userid
AND sud.courseid = pl.courseid
AND sud.timeend = $nextmidnight
@@ -508,10 +502,9 @@ function stats_cron_daily($maxdays=1) {
AND sud.stattype='activity'
AND (sud.userid = $guest OR sud.userid NOT IN (
- SELECT ue.userid
- FROM {user_enrolments} ue
- JOIN {enrol} e ON ue.enrolid = e.id
- WHERE e.courseid = sud.courseid
+ SELECT userid
+ FROM {temp_enroled} te
+ WHERE te.courseid = sud.courseid
))
) inline_view
@@ -1444,7 +1437,7 @@ function stats_get_report_options($courseid,$mode) {
$reportoptions[STATS_REPORT_PARTICIPATORY_COURSES] = get_string('statsreport'.STATS_REPORT_PARTICIPATORY_COURSES);
$reportoptions[STATS_REPORT_PARTICIPATORY_COURSES_RW] = get_string('statsreport'.STATS_REPORT_PARTICIPATORY_COURSES_RW);
}
- break;
+ break;
}
return $reportoptions;
@@ -1562,9 +1555,10 @@ function stats_temp_table_create() {
// Allows for the additional xml files to be used (if necessary)
$files = array(
$xmlfile => array(
- 'stats_daily' => array('temp_stats_daily'),
- 'stats_user_daily' => array('temp_stats_user_daily'),
- 'temp_log_template' => array('temp_log1', 'temp_log2'),
+ 'stats_daily' => array('temp_stats_daily'),
+ 'stats_user_daily' => array('temp_stats_user_daily'),
+ 'temp_enroled_template' => array('temp_enroled'),
+ 'temp_log_template' => array('temp_log1', 'temp_log2'),
),
);
@@ -1619,7 +1613,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('temp_log1', 'temp_log2', 'temp_stats_daily', 'temp_stats_user_daily', 'temp_enroled');
foreach ($tables as $name) {
@@ -1638,6 +1632,31 @@ function stats_temp_table_drop() {
/**
* Fills the temporary stats tables with new data
*
+ * This function is meant to be called once at the start of stats generation
+ *
+ * @param timestart timestamp of the start time of logs view
+ * @param timeend timestamp of the end time of logs view
+ * @returns boolen success (true) or failure(false)
+ */
+function stats_temp_table_setup() {
+ global $DB;
+
+ $sql = "INSERT INTO {temp_enroled} (userid, courseid, roleid)
+
+ SELECT ue.userid, e.courseid, ra.roleid
+ 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)";
+
+ return stats_run_query($sql, array('courselevel' => CONTEXT_COURSE));
+}
+
+/**
+ * Fills the temporary stats tables with new data
+ *
+ * This function is meant to be called to get a new day of data
+ *
* @param timestart timestamp of the start time of logs view
* @param timeend timestamp of the end time of logs view
* @returns boolen success (true) or failure(false)
@@ -1683,9 +1702,7 @@ function stats_temp_table_clean() {
FROM {temp_stats_user_daily}';
foreach ($sql as $id => $query) {
- try {
- $DB->execute($query);
- } catch (Exception $e) {
+ if (! stats_run_query($query)) {
mtrace("Error during table cleanup!");
return false;
}