Skip to content

Instantly share code, notes, and snippets.

@aaronfulton
Created April 24, 2013 02:00
Show Gist options
  • Save aaronfulton/5449023 to your computer and use it in GitHub Desktop.
Save aaronfulton/5449023 to your computer and use it in GitHub Desktop.
Patch for postgres support for the moodle-mergeusers project
diff --git a/README.txt b/README.txt
index b89a2bf..d9f9ebc 100644
--- a/README.txt
+++ b/README.txt
@@ -40,7 +40,7 @@ This. Is. Bad. Practice safe script. Always backup first.
MINIMUM REQUIREMENTS:
-- MySQL v5.0
+- MySQL v5.0 or MSSQL or Postgres
- Moodle v2.0
Based on the mergeusers_v2.php script written by Nicolas Dunand.
diff --git a/index.php b/index.php
index 9cf951a..92a09e7 100644
--- a/index.php
+++ b/index.php
@@ -77,7 +77,7 @@ $mergeuserform = new mergeuserform();
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('mergeusers', 'report_mergeusers'));
echo $OUTPUT->box_start();
-
+
$data = $mergeuserform->get_data();
// Any submitted data?
@@ -114,10 +114,17 @@ if ($data = $mergeuserform->get_data()) {
// MSSQL
$tableNames = $DB->get_records_sql("SELECT name FROM sys.Tables WHERE name LIKE '".$CFG->prefix."%' AND type = 'U' ORDER BY name");
}
- else {
+ else if ($CFG->dbtype == 'mysql') {
// MySQL
$tableNames = $DB->get_records_sql('SHOW TABLES like "'.$CFG->prefix.'%"');
}
+ else if ($CFG->dbtype == 'pgsql') {
+ //PGSQL
+ $tableNames = $DB->get_records_sql("SELECT table_name FROM information_schema.tables WHERE table_name LIKE '".$CFG->prefix."%' AND table_schema = 'public'");
+ }
+ else {
+ print_error('errordatabase', 'report_mergeusers');
+ }
// $numtables = sizeof($tableNames);
// echo "<h2>".$numtables. " tables found in database &quot;".$CFG->dbname."&quot;</h2>";
@@ -137,10 +144,17 @@ if ($data = $mergeuserform->get_data()) {
// MSSQL
$columnList = "SELECT * FROM INFORMATION_SCHEMA.Columns WHERE TABLE_NAME = '".$table_name."' AND COLUMN_NAME IN ('userid', 'user_id', 'id_user', 'user')";
}
- else {
+ else if ($CFG->dbtype == 'mysql') {
// MySQL
$columnList = "SHOW COLUMNS FROM ".$table_name." where Field IN ('userid', 'user_id', 'id_user', 'user')";
}
+ else if ($CFG->dbtype == 'pgsql') {
+ // PGSQL
+ $columnList = "SELECT column_name FROM information_schema.columns WHERE table_name ='". $table_name ."' and column_name IN ('userid', 'user_id', 'id_user', 'user');";
+ }
+ else {
+ print_error('errordatabase', 'report_mergeusers');
+ }
$columns = $DB->get_records_sql($columnList);
@@ -148,15 +162,23 @@ if ($data = $mergeuserform->get_data()) {
// no matching or multiple matching fields in this table, move onto the next table.
continue;
}
+
// Now we have the appropriate fieldname and we know what to update!
if ($CFG->dbtype == 'sqlsrv') {
// MSSQL
$field_name = array_shift($columns)->column_name; // get the fieldname
}
- else {
+ else if ($CFG->dbtype == 'mysql') {
// MySQL
$field_name = array_shift($columns)->field; // get the fieldname
}
+ else if ($CFG->dbtype == 'pgsql') {
+ //PGSQL
+ $field_name = array_shift($columns)->column_name; // get the fieldname
+ }
+ else {
+ print_error('errordatabase', 'report_mergeusers');
+ }
$recordsToUpdate = $DB->get_records_sql("SELECT ".PRIMARY_KEY." FROM ".$table_name." WHERE ".$field_name." = '".$currentUser."'");
if(count($recordsToUpdate) == 0) {
diff --git a/lang/en/report_mergeusers.php b/lang/en/report_mergeusers.php
index 0a8714c..5aec344 100644
--- a/lang/en/report_mergeusers.php
+++ b/lang/en/report_mergeusers.php
@@ -18,6 +18,7 @@ $string['description'] = '
<p>Given a user ID to be deleted and a user ID to keep, this will merge the user data associated with the former user ID into the latter user ID. Note that both user IDs must already exist and no accounts will actually be deleted. That process is left to the administrator to do manually.</p>
<p>This process involves some database dependant functions and may not have been fully tested on your particular choice of database. <strong>Only do this if you know what you are doing as it is not reversable!</strong></p>';
$string['errornouserid'] = 'Cannot retrieve user ID';
+$string['errordatabase'] = 'Unsupported database type';
$string['mergeusers'] = 'Merge user accounts';
$string['merging'] = 'Merging';
$string['into'] = 'into';
diff --git a/locallib.php b/locallib.php
index 95220d2..c29a63c 100644
--- a/locallib.php
+++ b/locallib.php
@@ -109,7 +109,7 @@ function disableOldUserEnrollments($newId, $currentId) {
if(!empty($enrollmentsToUpdate)) { // it's possible we won't have any
// First, let's move the courses belonging to the old user over to the new one.
$updateIds = implode(', ', $enrollmentsToUpdate);
- $sql = 'UPDATE '.$CFG->prefix.'user_enrolments SET userid = "'.$newId.'" WHERE id IN ('.$updateIds.')';
+ $sql = 'UPDATE '.$CFG->prefix.'user_enrolments SET userid = '.$newId.' WHERE id IN ('.$updateIds.')';
if($DB->execute($sql)) {
// echo($sql);
// echo '<p style="color:#0c0;">'.get_string('tableok', 'report_mergeusers', "{$CFG->prefix}user_enrolments (#1)").'</p>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment