From: skodak Date: Thu, 1 May 2008 22:05:26 +0000 (+0000) Subject: MDL-14605 removed old stats upgrade code X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=9a915276fcecd4a1101ff5114080b056a683b027;p=moodle.git MDL-14605 removed old stats upgrade code --- diff --git a/lib/statslib.php b/lib/statslib.php index 92b21fb194..2b848ea104 100644 --- a/lib/statslib.php +++ b/lib/statslib.php @@ -1417,246 +1417,4 @@ function stats_check_uptodate($courseid=0) { return get_string('statscatchupmode','error',$a); } -/** - * Calculate missing course totals in stats - */ -function stats_upgrade_totals() { - global $CFG; - - if (empty($CFG->statsrolesupgraded)) { - // stats not yet upgraded to cope with roles... - return; - } - - $types = array('daily', 'weekly', 'monthly'); - - $now = time(); - $y30 = 60*60*24*365*30; // 30 years ago :-O - $y20 = 60*60*24*365*20; // 20 years ago :-O - $limit = $now - $y20; - - foreach ($types as $i => $type) { - $type2 = $types[($i+1) % count($types)]; - - // delete previous incomplete data - $sql = "DELETE FROM {$CFG->prefix}stats_$type2 - WHERE timeend < $limit"; - execute_sql($sql); - - // clear the totals if already exist - $sql = "DELETE FROM {$CFG->prefix}stats_$type - WHERE (stattype = 'enrolments' OR stattype = 'activity') AND - roleid = 0"; - execute_sql($sql); - - $sql = "INSERT INTO {$CFG->prefix}stats_$type2 (stattype, timeend, courseid, roleid, stat1, stat2) - - SELECT stattype, (timeend - $y30), courseid, 0, SUM(stat1), SUM(stat2) - FROM {$CFG->prefix}stats_$type - WHERE (stattype = 'enrolments' OR stattype = 'activity') AND - roleid <> 0 - GROUP BY stattype, timeend, courseid"; - execute_sql($sql); - - $sql = "INSERT INTO {$CFG->prefix}stats_$type (stattype, timeend, courseid, roleid, stat1, stat2) - - SELECT stattype, (timeend + $y30), courseid, roleid, stat1, stat2 - FROM {$CFG->prefix}stats_$type2 - WHERE (stattype = 'enrolments' OR stattype = 'activity') AND - roleid = 0 AND timeend < $y20"; - execute_sql($sql); - - $sql = "DELETE FROM {$CFG->prefix}stats_$type2 - WHERE timeend < $limit"; - execute_sql($sql); - } -} - - -function stats_upgrade_for_roles_wrapper() { - global $CFG; - if (!empty($CFG->statsrolesupgraded)) { - return true; - } - - $result = begin_sql(); - - $result = $result && stats_upgrade_user_table_for_roles('daily'); - $result = $result && stats_upgrade_user_table_for_roles('weekly'); - $result = $result && stats_upgrade_user_table_for_roles('monthly'); - - $result = $result && stats_upgrade_table_for_roles('daily'); - $result = $result && stats_upgrade_table_for_roles('weekly'); - $result = $result && stats_upgrade_table_for_roles('monthly'); - - - $result = $result && commit_sql(); - - if (!empty($result)) { - set_config('statsrolesupgraded',time()); - } - - // finally upgade totals, no big deal if it fails - stats_upgrade_totals(); - - return $result; -} - -/** - * Upgrades a prefix_stats_user_* table for the new role based permission - * system. - * - * @param string $period daily, weekly or monthly: the stat period to upgrade - * @return boolean @todo maybe something else (error message) depending on - * how this will be called. - */ -function stats_upgrade_user_table_for_roles($period) { - global $CFG; - static $teacher_role_id, $student_role_id; - - if (!in_array($period, array('daily', 'weekly', 'monthly'))) { - error_log('stats upgrade: invalid period: ' . $period); - return false; - } - - if (!$teacher_role_id) { - $role = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW); - $role = array_keys($role); - $teacher_role_id = $role[0]; - $role = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW); - $role = array_keys($role); - $student_role_id = $role[0]; - } - - if (empty($teacher_role_id) || empty($student_role_id)) { - error_log("Couldn't find legacy roles for teacher or student"); - return false; - } - - $status = true; - - $status = $status && execute_sql("UPDATE {$CFG->prefix}stats_user_{$period} - SET roleid = $teacher_role_id - WHERE roleid = 1"); - $status = $status && execute_sql("UPDATE {$CFG->prefix}stats_user_{$period} - SET roleid = $student_role_id - WHERE roleid = 2"); - - return $status; -} - -/** - * Upgrades a prefix_stats_* table for the new role based permission system. - * - * @param string $period daily, weekly or monthly: the stat period to upgrade - * @return boolean @todo depends on how this will be called - */ -function stats_upgrade_table_for_roles ($period) { - global $CFG; - static $teacher_role_id, $student_role_id; - - if (!in_array($period, array('daily', 'weekly', 'monthly'))) { - return false; - } - - if (!$teacher_role_id) { - $role = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW); - $role = array_keys($role); - $teacher_role_id = $role[0]; - $role = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW); - $role = array_keys($role); - $student_role_id = $role[0]; - } - - if (empty($teacher_role_id) || empty($student_role_id)) { - error_log("Couldn't find legacy roles for teacher or student"); - return false; - } - - execute_sql("CREATE TABLE {$CFG->prefix}stats_{$period}_tmp AS - SELECT * FROM {$CFG->prefix}stats_{$period}"); - - $table = new XMLDBTable('stats_' . $period); - if (!drop_table($table)) { - return false; - } - - // Create a new stats table - // @todo this definition I have made blindly by looking at how definitions are - // made, it needs work to make sure it works properly - require_once("$CFG->libdir/xmldb/classes/XMLDBTable.class.php"); - - $table = new XMLDBTable('stats_' . $period); - $table->addFieldInfo('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, - XMLDB_NOTNULL, XMLDB_SEQUENCE, null, null, null); - - $table->addFieldInfo('courseid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, - XMLDB_NOTNULL, null, null, null, null); - - $table->addFieldInfo('roleid', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, - XMLDB_NOTNULL, null, null, null, null); - $table->addFieldInfo('timeend', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, - XMLDB_NOTNULL, null, null, null, null); - $table->addFieldInfo('stattype', XMLDB_TYPE_CHAR, '20', null, XMLDB_NOTNULL, - null, XMLDB_ENUM, array('enrolments', 'activity', 'logins'), 'activity'); - $table->addFieldInfo('stat1', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, - XMLDB_NOTNULL, null, null, null, null); - $table->addFieldInfo('stat2', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, - XMLDB_NOTNULL, null, null, null, null); - - /// Adding keys to table stats_daily - $table->addKeyInfo('primary', XMLDB_KEY_PRIMARY, array('id')); - - /// Adding indexes to table stats_daily - $table->addIndexInfo('courseid', XMLDB_INDEX_NOTUNIQUE, array('courseid')); - $table->addIndexInfo('timeend', XMLDB_INDEX_NOTUNIQUE, array('timeend')); - $table->addIndexInfo('roleid', XMLDB_INDEX_NOTUNIQUE, array('roleid')); - - if (!create_table($table)) { - return false; - } - - // - // Now insert the data from the temporary table into the new one - // - - // Student enrolments - execute_sql("INSERT INTO {$CFG->prefix}stats_{$period} - (courseid, roleid, timeend, stattype, stat1, stat2) - SELECT courseid, $student_role_id, timeend, 'enrolments', students, activestudents - FROM {$CFG->prefix}stats_{$period}_tmp"); - - // Teacher enrolments - execute_sql("INSERT INTO {$CFG->prefix}stats_{$period} - (courseid, roleid, timeend, stattype, stat1, stat2) - SELECT courseid, $teacher_role_id, timeend, 'enrolments', teachers, activeteachers - FROM {$CFG->prefix}stats_{$period}_tmp"); - - // Student activity - execute_sql("INSERT INTO {$CFG->prefix}stats_{$period} - (courseid, roleid, timeend, stattype, stat1, stat2) - SELECT courseid, $student_role_id, timeend, 'activity', studentreads, studentwrites - FROM {$CFG->prefix}stats_{$period}_tmp"); - - // Teacher activity - execute_sql("INSERT INTO {$CFG->prefix}stats_{$period} - (courseid, roleid, timeend, stattype, stat1, stat2) - SELECT courseid, $teacher_role_id, timeend, 'activity', teacherreads, teacherwrites - FROM {$CFG->prefix}stats_{$period}_tmp"); - - // Logins - execute_sql("INSERT INTO {$CFG->prefix}stats_{$period} - (courseid, roleid, timeend, stattype, stat1, stat2) - SELECT courseid, 0, timeend, 'logins', logins, uniquelogins - FROM {$CFG->prefix}stats_{$period}_tmp WHERE courseid = ".SITEID); - - // Drop the temporary table - $table = new XMLDBTable('stats_' . $period . '_tmp'); - if (!drop_table($table)) { - return false; - } - - return true; -} - ?>