/** * Create temporary tables to speed up log generation */ function stats_temp_table_create() { global $CFG, $DB; $dbman = $DB->get_manager(); // We are going to use database_manager services stats_temp_table_drop(); $tables = array(); /// Define tables user to be created $table = new xmldb_table('temp_stats_daily'); $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); $table->add_field('courseid', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, '0'); $table->add_field('timeend', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, '0'); $table->add_field('roleid', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, '0'); $table->add_field('stattype', XMLDB_TYPE_CHAR, 20, null, XMLDB_NOTNULL, null, 'activity'); $table->add_field('stat1', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, '0'); $table->add_field('stat2', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, '0'); $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); $table->add_index('courseid', XMLDB_INDEX_NOTUNIQUE, array('courseid')); $table->add_index('timeend', XMLDB_INDEX_NOTUNIQUE, array('timeend')); $table->add_index('roleid', XMLDB_INDEX_NOTUNIQUE, array('roleid')); $tables['temp_stats_daily'] = $table; $table = new xmldb_table('temp_stats_user_daily'); $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); $table->add_field('courseid', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, '0'); $table->add_field('userid', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, '0'); $table->add_field('roleid', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, '0'); $table->add_field('timeend', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, '0'); $table->add_field('statsreads', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, '0'); $table->add_field('statswrites', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, '0'); $table->add_field('stattype', XMLDB_TYPE_CHAR, 30, null, XMLDB_NOTNULL, null, null); $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); $table->add_index('courseid', XMLDB_INDEX_NOTUNIQUE, array('courseid')); $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid')); $table->add_index('timeend', XMLDB_INDEX_NOTUNIQUE, array('timeend')); $table->add_index('roleid', XMLDB_INDEX_NOTUNIQUE, array('roleid')); $tables['temp_stats_user_daily'] = $table; $table = new xmldb_table('temp_enroled'); $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); $table->add_field('userid', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, '0'); $table->add_field('courseid', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, '0'); $table->add_field('roleid', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, null); $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); $table->add_index('userid', XMLDB_INDEX_NOTUNIQUE, array('userid')); $table->add_index('courseid', XMLDB_INDEX_NOTUNIQUE, array('courseid')); $table->add_index('roleid', XMLDB_INDEX_NOTUNIQUE, array('roleid')); $tables['temp_enroled'] = $table; $table = new xmldb_table('temp_log1'); $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null); $table->add_field('userid', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, '0'); $table->add_field('course', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, '0'); $table->add_field('action', XMLDB_TYPE_CHAR, 40, null, XMLDB_NOTNULL, null, null); $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id')); $table->add_index('action', XMLDB_INDEX_NOTUNIQUE, array('action')); $table->add_index('course', XMLDB_INDEX_NOTUNIQUE, array('course')); $table->add_index('user', XMLDB_INDEX_NOTUNIQUE, array('userid')); $table->add_index('usercourseaction', XMLDB_INDEX_NOTUNIQUE, array('userid', 'course', 'action')); $tables['temp_log1'] = $table; /// temp_log2 is exactly the same as temp_log1. $tables['temp_log2'] = clone $tables['temp_log1']; $tables['temp_log2']->setName('temp_log2'); try { foreach ($tables as $table) { $dbman->create_temp_table($table); } } catch (Exception $e) { mtrace('Temporary table creation failed: ' . $e->getMessage()); return false; } return true; }
/** * Create temporary tables to speed up log generation */ function stats_temp_table_create() { global $CFG, $DB; $dbman = $DB->get_manager(); // We are going to use database_manager services stats_temp_table_drop(); $xmlfile = $CFG->dirroot . '/lib/db/install.xml'; $tables = array(); // 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_enroled_template' => array('temp_enroled'), 'temp_log_template' => 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(); foreach ($contents as $template => $names) { $table = $xmldb_structure->getTable($template); 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); foreach ($names as $name) { $named = clone $table; $named->setName($name); $tables[$name] = $named; } } } try { foreach ($tables as $table) { $dbman->create_temp_table($table); } } catch (Exception $e) { mtrace('Temporary table creation failed: ' . $e->getMessage()); return false; } return true; }
/** * Test the function that clean out the temporary tables. * * @depends test_statslib_temp_table_create_and_drop */ public function test_statslib_temp_table_clean() { global $DB; $rows = array('temp_log1' => array('id' => 1, 'course' => 1), 'temp_log2' => array('id' => 1, 'course' => 1), 'temp_stats_daily' => array('id' => 1, 'courseid' => 1), 'temp_stats_user_daily' => array('id' => 1, 'courseid' => 1)); stats_temp_table_create(); foreach ($rows as $table => $row) { $DB->insert_record_raw($table, $row); $this->assertEquals(1, $DB->count_records($table)); } stats_temp_table_clean(); foreach ($rows as $table => $row) { $this->assertEquals(0, $DB->count_records($table)); } $this->assertEquals(1, $DB->count_records('stats_daily')); $this->assertEquals(1, $DB->count_records('stats_user_daily')); stats_temp_table_drop(); }