Quantcast
Channel: General developer forum
Viewing all articles
Browse latest Browse all 37673

Re: Extending Forms with Separate Database

$
0
0
by Onno Schuit.  

Here's a hack to connect to a second database:

  1. replicate the lines with the database settings in your config.php (and adapt the settings for your second database).
  2. instantiate a new object of the same class as the original $DB
  3. 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).


Viewing all articles
Browse latest Browse all 37673

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>