Here's a hack to connect to a second database:
- replicate the lines with the database settings in your config.php (and adapt the settings for your second database).
- instantiate a new object of the same class as the original $DB
- call the connect method, using your 2nd database settings
Example:
global $SOURCE_DB, $DB, $CFG;
$db_class = get_class($DB);
$SOURCE_DB = new $db_class(); // instantiate a new object of the same class as the original $DB
$SOURCE_DB->connect($CFG->source_dbhost, $CFG->source_dbuser, $CFG->source_dbpass, $CFG->source_dbname, false);
(Copied from the Moodle section of my tech wiki)
In your own code, you can use the new database connection object ($SOURCE_DB in our example) exactly like the original $DB object.
Of course, the existing Moodle activities (you mentioned quiz, lesson, etc.) are not going to magically use your second database connection object. You could create a local plugin which defines event handlers for the modules you mentioned. These event handlers would then save the data in a second database, presumably after some manipulation (otherwise you could just copy the data straight out of the original Moodle database with a 5 minute interval).