User Merge Utility

User Merge Utility

by Scott Karren -
Number of replies: 76

Ok, here is my situation.

New employees for our organization need to register for classes generally on the first day that they start employment.  In some cases their network accounts have not been created yet, so they manually create a new user account in Moodle and register for the courses that they need.  When the network account is setup the user will start using that account which authenticates using LDAP in Moodle.  The probelm with this is that the user has to re-register for all of the courses that they need to take and they have lost any of the grades that have been posted under the other manual account.

Has anyone created a user merge utility that would allow you to take two user accounts and select which fields and information need to be transferred from one user to another?

Scott Karren

Average of ratings: Useful (2)
In reply to Scott Karren

Re: User Merge Utility

by Petr Kalis -
Hi
Just question, it won't work if you change manual account to LDAP account (you change username+password from LDAP accordingly) ? So only the old accounts stays, with grades etc...
I solved that similarly when transferring account from email to LDAP authentization.
PK
In reply to Scott Karren

Re: User Merge Utility

by Nicolas Dunand -
Picture of Core developers Picture of Plugin developers
Well, I came across a similar problem today.

For some reason, one of our users had two accounts. Of course he didn't realize it and wondered why he sometimes had access to some courses, and sometimes not.

After scratching my head for a while, a came with a quick and dirty solution (see attached file), which consists in updating the Moodle database.
Average of ratings: Useful (1)
In reply to Scott Karren

Re: User Merge Utility

by Mark van Hoek -
Picture of Core developers Picture of Plugin developers
I've modified Dunand's script to use Moodle's built-in variables and added some tracing output. It worked like a charm for me in 1.9.3+ (200912xxx)

It's attached.
Average of ratings: Useful (1)
In reply to Mark van Hoek

Re: User Merge Utility

by Nicolas Dunand -
Picture of Core developers Picture of Plugin developers
Thanks Mark for this nice update ... I'm keeping this for next time smile
In reply to Nicolas Dunand

Re: User Merge Utility

by Larry Elchuck -
Mark/Nicholas

Can you provide a brief "read me" on on how this file actually works and what it does? I don't want to run the file to gind out I just messed up users who intentially have multiple accounts.

thanks
L
In reply to Larry Elchuck

Re: User Merge Utility

by Nicolas Dunand -
Picture of Core developers Picture of Plugin developers
Hi Larry,

Sorry I didn't see your post earlier. But one hour ago, I took the time to review this thread, as I had to use this utility again.

So, I went through Mark's updated code (thanks again), triple-checked it, updated it a bit more myself, provided a few "readme" lines at the top of the file, and here it is.

Hope it suits you Larry, don't hesitate to ask if anything is unclear (you could also PM me if I'm not quick enough at replying).

Mark, for your info, my only significant change was to dispatch SQL "IN" clauses into any number of SQL "=" clauses, thus avoiding table update fail (tthis way, only concerned records would fail to update).

To anyone using this, please read the text at the top of the file, which mainly says "backup your database before using this script". Better safe than sorry clin d’oeil
Average of ratings: Useful (1)
In reply to Nicolas Dunand

This forum post has been removed

The content of this forum post has been removed and can no longer be accessed.
In reply to Nicolas Dunand

Re: User Merge Utility

by Stephen Martin -
Relatively new to PHP (Java, ASP, ASP.NET coder in past life), so forgive me if I've made some simple error.

I downloaded your updated mergeusers.php file and followed your readme notes (which were very clear--including the part about backing up the DB). When I try to launch the page, I get nothing. Just a blank page. No error messages. No hidden source. Just vast nothingness.

Any clues?

Thanks to a rogue database update that changed a number of student ID in our external database used for DB enrolment, I now have 22 pairs of IDs that I need to merge, so this script would be a lifesaver if I could get it to run.
In reply to Stephen Martin

Re: User Merge Utility

by Nicolas Dunand -
Picture of Core developers Picture of Plugin developers
Simon & Stephen,

Just to be clear, I will be refering to the last "mergeusers.php" file I posted (i.e., on January 26th).

Simon,

I'm not sure about the exact "adodb_logsql" table structure among installations. Could you run the SQL query "SHOW CREATE TABLE adodb_logsql;" and post the results ? This will only read the table structure from your database, and won't perform any modification.

Stephen,

Your symptoms look like a PHP parsing error (or similar), preventing to execute the script. If your PHP settings (usually a php.ini file on the server) disallow error reporting (which usually is the case on production sites, to avoid disclosure of information about server configuration), then the result of a syntax error within the run script would just display nothing.
So, did you by any chance introduce a syntax error ? The only thing you would change on my "mergeusers.php" file are the two "BLANK" occurences, on lines 37 and 38, and being careful not to delete the subsequent semicolon (which ends each PHP statement, and is mandatory).

Cheers.
In reply to Nicolas Dunand

This forum post has been removed

The content of this forum post has been removed and can no longer be accessed.
In reply to Deleted user

Re: User Merge Utility

by Nicolas Dunand -
Picture of Core developers Picture of Plugin developers
Hello Simon,

Yes, this is precisely what I'm after ... thanks. I was wondering if I was missing something about a different table setup, but it seems I'm not.

Well, it seems that my script is not returning what expected about this table. So, could you please run the following query manually as well ?

SHOW FIELDS FROM `adodb_logsql` WHERE (`Field` = 'userid' OR `Field` = 'user_id' OR `Field` = 'id_user');

and tell me what's returned ?

By the way, could you moreover tell me which database management system (MySQL, Postgres, MSSQL, ...) you're using, and which version ? Plus, which Moodle version exactly are you working on ?

I'm starting having ideas about what the problem could be, and I'm confident we'll find a solution very soon.
In reply to Nicolas Dunand

This forum post has been removed

The content of this forum post has been removed and can no longer be accessed.
In reply to Deleted user

Re: User Merge Utility

by Nicolas Dunand -
Picture of Core developers Picture of Plugin developers
Hi Simon,

Here I am again ... I guess some timezone difference hinders quick back and forth communication ... whatever.

So, I finally discovered some incompatibility of my script with MySQL versions < 5.

Thus, here's a version that _should_ work on MySQL 4.1. I added some more output, so that you have all the details of what's modified in the database, and how to undo it (even without recovering from the backup).

I however stress on this point once more : be SURE to backup your full database before running this, just in case something goes horribly wrong. I just ran it several times on my server, so it shouldn't, but who knows ?

So, please let me know if this works for you (it should), and if anything goes wring just send me the output (save the whole HTML page generated) of the script.
Average of ratings: Useful (1)
In reply to Nicolas Dunand

This forum post has been removed

The content of this forum post has been removed and can no longer be accessed.
In reply to Scott Karren

Re: User Merge Utility

by Mark van Hoek -
Picture of Core developers Picture of Plugin developers
I've turned this into a nice, easy to use form that takes usernames & called it v3
Average of ratings: Useful (1)
In reply to Mark van Hoek

Re: User Merge Utility

by Nicolas Dunand -
Picture of Core developers Picture of Plugin developers
Hi Mark,

I just tested your V3 today on a test server, and I noticed :
  • sesskey param missing when you GET the mergeusers_v3.php script (Moodle function required_param() whining)
  • if I click "submit" without filling any field, it looks like it replaces 'userid' of every row of every table with the value 'BLANK'
Did I miss something ?
Average of ratings: Useful (1)
In reply to Nicolas Dunand

Re: User Merge Utility

by Mark van Hoek -
Picture of Core developers Picture of Plugin developers
Hi Nicolas,

My bad. Please try the script attached here.
Average of ratings: Useful (1)
In reply to Mark van Hoek

Re: User Merge Utility

by Nicolas Dunand -
Picture of Core developers Picture of Plugin developers
Hi Mark,

Thanks for this, it looks good!

I couldn't find time to try it out now, but I will definitely some time soon. perhaps I could suggest you removing lines 41-42 though, as the "good" and "bad" users' IDs aren't hard-coded any more (thus those lines aren't needed) ?

Best regards.
In reply to Mark van Hoek

Re: User Merge Utility

by Shane Elliott -
Picture of Core developers Picture of Plugin developers
Hi guys,

I needed to merge 2 users for a client of ours but they are running on postgres. So I've taken the script and reworked it as an admin report plugin and added postgres to it (I was testing on mysql and postgres). My apologies to mssql and oracle users but I had nothing readily available to run tests.

In any case, it's attached here. Might add it to contrib and then others can work on it if they wish. Code should be easy enough for people to extend (eg mssql/oracle support).

Things still to be done:
1. Move the column/field name retrieval into generic moodle api function calls rather than db specific;
2. Traverse the moodle data directory;
3. Support for mssql and oracle.

Cheers,
Shane.

PS Nicolas and Mark, added you both as authors (with names as specified in this forum). Welcome to change to real/fake names or remove if you wish.
Average of ratings: Useful (1)
In reply to Shane Elliott

Re: User Merge Utility

by Luis de Vasconcelos -

Thanks Shane, this is great.

I need some help extending it to MSSQL (via FreeTDS). What is the MSSQL equivalent of this mysql code:

$query = mysql_query("SHOW TABLES");
if (mysql_num_rows($query) > 0) {
    while ($table = mysql_fetch_assoc($query)) {
        $tablename = trim($table['Tables_in_'.$CFG->dbname]);
        if (!empty($tablename)) {
            $tables[] = $tablename;
        }
    }
}
break;

To try and fix it I made the following changes to the mergeuser_get_tables function in locallib.php (attached):

First I added a 'mssql_n' case to the switch ($CFG->dbtype) statement since I'm connecting to MSSQL using the 'mssql_n' driver (FreeTDS). Inside it I put:

case 'mssql_n':
$query = mssql_query("select table_name from information_schema.tables");

if (mssql_num_rows($query) > 0) {
    while ($table = mssql_fetch_assoc($query)) {
        $tablename = trim($table['Tables_in_'.$CFG->dbname]);
        if (!empty($tablename)) {
            $tables[] = $table;
        }
    }
}
break;

It throws the following message:

Notice: Undefined index: Tables_in_myMoodleDBName in C:\BreakingMoodle\admin\report\mergeusers\locallib.php on line 224

The $tablename = trim($table['Tables_in_'.$CFG->dbname]); statement seems to be the problem - 'Tables_in_' is probably not valid MSSQL syntax.

How do I write the above code for MSSQL?

In reply to Mark van Hoek

Re: User Merge Utility

by Luis de Vasconcelos -

Thanks Mark. I ran your V3 on my dev box to test - it could be a useful script one day! However, it doesn't seem to work with a MSSQL 2005 database.

First I had to rename it from mergeusers_v3.php to mergeusers.php.

Then I was able to run it and I got the following error:

Check: Found both usernames
Check: Usernames differ

Invalid column name 'ezb0805'.

SELECT id from mdl_user WHERE username="ezb0805"
  • line 686 of lib\dmllib.php: call to debugging()
  • line 481 of lib\dmllib.php: call to get_recordset_sql()
  • line 112 of admin\mergeusers.php: call to get_record_sql()

Error: "Bad" username not found in the database
Skip "good" user check due to sanity

Fatal error: Call to undefined function mysql_close() in D:\MoodlePlayground\admin\mergeusers.php on line 249

ezb0805 was the name of one of the users whose record I was trying to merge into another record.

My server is running:

  • Moodle version 1.9.5+ Build: 20090916
  • Windows Server 2003
  • MSSQL 2005
  • IIS6
In reply to Luis de Vasconcelos

Re: User Merge Utility

by Nicolas Dunand -
Picture of Core developers Picture of Plugin developers
Hi Luis,

Please try my version 2. I unsucsessfully tried out Mark's version 3 but it seemed buggy. I use v2 on a regular basis and it hasn't ever let me down.
Average of ratings: Useful (1)
In reply to Nicolas Dunand

Re: User Merge Utility

by Luis de Vasconcelos -

Thanks Nicolas. Do you know if it works on a MSSQL database?

I tried the v2 script on my MSSQL 2005 instances of Moodle 1.9.4 and 1.9.5 and I got the following error on both versions of Moodle:

Fatal error: Call to undefined function mysql_query() in C:\Moodle\mergeusers_v2.php on line 71

In reply to Luis de Vasconcelos

Re: User Merge Utility

by Nicolas Dunand -
Picture of Core developers Picture of Plugin developers
Hello Luis,

I'm sorry I read your last message too quickly : didn't notice you were using MSSQL whereas I'm using MySQL.

I modifier the "V2" script as it contains MySQL-specific code ... now it should be working with MSSQL. But be careful as I don't have any MSSQL server here to try it on, so please backup everything first and ensure you can recover from your backup before proceeding!

Even better, test it first on a test Moodle site (if you have one) and see if everything works properly.

I should take some time (hopefully soon) to rewrite this using the Moodle DB layer abstraction, which would then make the script cross-DB compatible.

Please let me know what comes up.

Regards.
Average of ratings: Useful (1)
In reply to Nicolas Dunand

Re: User Merge Utility

by Clinton Graham -
Has anyone entered this functionality and script into the Tracker as an enhancement request? It would be a helpful addition for Moodle administration.
In reply to Clinton Graham

Re: User Merge Utility

by Adam Barbary -
I haven't yet, but there is at least one problem we have with it. If the old/bad/unwanted user has files, then they become orphaned in the system. Grades etc stay, but as the user id is used for file storage the links break when mapped to a new user. Our php programmer is working on a fix to rename or move files into appropriate folders. We are also working on a nice HTML front end to hack into the admin pages to support this, and perhaps a CSV, or XML feed for bulk processing. Stay tuned....

Moodle 1.9.7 (patched)
Postgres
Average of ratings: Useful (1)
In reply to Adam Barbary

Re: User Merge Utility

by Adam Barbary -
It has been ages since I posted here, but essentially we ran low on resources, and simply reused some of the posted code. The attached file was dropped into the admin folder of Moodle and called by direct reference to the url. Please don't ask me what changes, if any, were made to the code, as I don't actually know. All I do know is that it works for us. This does not fix the issue of abandoned files. sad

Cheers,

Adam
Average of ratings: Useful (1)
In reply to Adam Barbary

Re: User Merge Utility

by Guillermo Madero -

I would not mind, for the time being, moving all files manually, but first I'd need to find them. Searching each and every course folder, then looking into every activity folder for the user's id folder to see if there are any files in there would be really time consuming.

Does anybody know if there is a program where one could input the user id and then get a list of all the folders (by course and activity) where the user has files in them? I think this could work in a similar fashion to mergeusers.php and it would be a really useful tool.

Thanks in advance,

Guillermo

In reply to Nicolas Dunand

Re: User Merge Utility

by Luis de Vasconcelos -

Thanks Nicolas,

I tried the mergeusers_v2_MSSQL.php script on my MSSQL 2005 database and I get the following output:

Be careful!
tables found in database "Moodle"
DB update OK (modifications made: 0)

The two user records are not merged.

Notice that there is no value at the beginning of the "tables found in database Moodle" part of the message. I guessing that this means that the mssql_num_rows($query1) call isn't returning a value. In your code the $query1 variable is assigned the value of mssql_query("SHOW TABLES"). Is "SHOW TABLES" a valid parameter for mssql_query()?

In reply to Luis de Vasconcelos

Re: User Merge Utility

by Luis de Vasconcelos -

To play a bit I changed:

$query1 = mssql_query("SHOW TABLES");

to:

$query1 = mssql_query("SELECT NAME FROM SYSOBJECTS WHERE XTYPE = 'U';");

And the output is now:

Be careful!

199 tables found in database "Moodle"

Skipping due to blank table name. Array=Array ( [NAME] => mdl_forum )
Skipping due to blank table name. Array=Array ( [NAME] => mdl_backup_ids )
Skipping due to blank table name. Array=Array ( [NAME] => mdl_assignments )
Skipping due to blank table name. Array=Array ( [NAME] => mdl_grade_items )
...

<Snipped lots of similar output for the other tables>
...
DB update OK (modifications made: 0)

So there's still something wrong with $table_name.
The two records were not merged.

In reply to Luis de Vasconcelos

Re: User Merge Utility

by Jason Dilworth -
I had a similar problem, needing to merge accounts from an e-mail authenticated Moodle system which had been brought over to an LDAP authenticated installation using course backups and restores (I wasn't allowed access to the OS).

In this case there were many hundreds of accounts to migrate, so to avoid the hassle of finding each pair of accounts by name or ID I wrote a wrapper around the code I found in this thread - I've attached it here as merge.php.

It goes through the database looking for all LDAP accounts with firstname & surname that match each e-mail account in turn, then produces a ticklist for the user to choose which accounts to merge. Any rows that are ticked are then pumped through the merge_users routine, with the 'undo' output also being logged also to a text file.

This is only the 3rd piece of PHP code I've ever written, so it could probably do with some tidying...
There is a small problem with the ticklist in that clicking on the checkbox doesn't change the 'ticked' status, but clicking anywhere else on the row does.
I didn't know where log files should go so I dumped into /tmp
Finally, I have no idea how this will handle databases other than MySQL - my wrapper code will likely need changing in that respect too.

I hope this is useful to someone out there.
Jason


Average of ratings: Useful (1)
In reply to Jason Dilworth

Re: User Merge Utility

by Paul Broomfield -

Hi all,

Attached is my version of a merge user accounts script, its OO and very extendable.

I've filled it with comments that explain what is needed to use and extend it.

Obviously, use at your own risk, take backups, cross fingers and toes etc - I've used this on many of our live clients sites and it works for me, but I'd hate for it cause trouble for anyone else...

Some of the DB updates use built in Moodle functions and some use straight SQL, its very simple SQL so wouldnt take make to port it to which-ever server you are using.

Enjoy and please let me know of any improvements - feel free to make them yourself and update your changes here.

ta,

Paul Broomfield

p.s. I have written scripts that merge Scorm data between two different Scorms too (useful if you make a mistake uploading identical scorm packages and end up with two) - at somepoint I'll upload these.

Average of ratings: Useful (1)
In reply to Paul Broomfield

User Merge Utility for Moodle 2.0

by Forrest Gaston -

I was wondering if anyone has a merge users utility that works with Moodle 2.0?

Thanks again,
Forrest Gaston

In reply to Forrest Gaston

Re: User Merge Utility for Moodle 2.0

by Mike Holzer -

I am working on a version of the v2 utility for Moodle 2.

I'm stuck on the merging of the table grade_grades. It has a multiple-unique key on the userid field so unlike most other tables the column can't just be updated.

Hopefully I'll be able to share the updated script soon!

Average of ratings: Useful (1)
In reply to Mike Holzer

Re: User Merge Utility for Moodle 2.0

by Mike Holzer -

Alright, here's my updated script for Moodle 2.0+. This is based on mergeusers_v2.php by Nicolas Dunand.

The original script, as well as my updated version, functions by going through each Moodle table and changing the userId of the old user to that of the new user. My updated script differs from the original in the cases where userid is used as a compound key.

In the case of the grade_grades table, I'm actually deleting the common record for the user being replaced. This is ok because the record I just deleted can be found in grade_grades_history which will be updated to the new user.

A similar problem is encountered in the user_enrolments table. In this case, I'm actually locking the old user out of any courses that they were enrolled in shared by the replacing user.

There are also 3 user preference tables that my script is skipping altogether because there's no reason to update those 3 tables as they only affect that user's preferences or logging.

I recommend deleting the old user once you've run this script successfully. Also, be sure to backup your DB before running this script. I tested it on my installations but your setup may be different from mine which could cause problems!

For instructions and more info on how the script works and as to my thought process, check out the documentation at the top of the file. Good luck!

Average of ratings: Useful (3)
In reply to Mike Holzer

Re: User Merge Utility for Moodle 2.0

by Luis de Vasconcelos -

AAAH! THANKS! For a moment I was so excited...

But unfortunately it crashed! The script started off well:

Be careful!
Merging 13288 into 12822

and then:

Error reading from database
More information about this error
(which points to http://docs.moodle.org/20/en/error/moodle/dmlreadexception)

My PHP error log shows the following:

[22-Mar-2012 16:33:25 UTC] sqlsrv_query: SQLSTATE = 42000
[22-Mar-2012 16:33:25 UTC] sqlsrv_query: error code = 156
[22-Mar-2012 16:33:25 UTC] sqlsrv_query: message = [Microsoft][SQL Server Native Client 10.0][SQL Server]Incorrect syntax near the keyword 'like'.
[22-Mar-2012 16:33:25 UTC] Default exception handler: Error reading from database Debug: SQLState: 42000<br>
Error Code: 156<br>
Message: [Microsoft][SQL Server Native Client 10.0][SQL Server]Incorrect syntax near the keyword 'like'.<br>

SHOW TABLES like "mdl_%"
[array (
)]
* line 391 of \lib\dml\moodle_database.php: dml_read_exception thrown
* line 252 of \lib\dml\sqlsrv_native_moodle_database.php: call to moodle_database->query_end()
* line 368 of \lib\dml\sqlsrv_native_moodle_database.php: call to sqlsrv_native_moodle_database->query_end()
* line 773 of \lib\dml\sqlsrv_native_moodle_database.php: call to sqlsrv_native_moodle_database->do_query()
* line 807 of \lib\dml\sqlsrv_native_moodle_database.php: call to sqlsrv_native_moodle_database->get_recordset_sql()
* line 230 of \mergeusers_mdl_2.php: call to sqlsrv_native_moodle_database->get_records_sql() 

It seems that the script will not work on MSSQL - "SHOW TABLES like" is not supported by MSSQL...

Pity... This script would have added ten years to my life!

My dev server config:
Windows Server 2003 Standard Edition SP2
IIS 6
MSSQL Server 2005 Standard Edition
PHP 5.3.3 NTS VC9
MS FastCGI Handler x86 6.1
MS SQL Server Driver for PHP 2.0
MS Windows Cache Extension for PHP 1.1

My Moodle config:
$CFG->dbtype = 'sqlsrv';
$CFG->dblibrary = 'native';

 

In reply to Luis de Vasconcelos

Re: User Merge Utility for Moodle 2.0

by Mike Holzer -

Hi Luis,

Unfortunately my script was written for MySQL and it looks like you're using MSSQL. I will try to take some time to make it DB agnostic as soon as I can. Apparently the 'like' syntax differs between the two systems.

In reply to Mike Holzer

Re: User Merge Utility for Moodle 2.0

by Forrest Gaston -

Mike, 

This is greatly appreciated.  During my initial testing, I ran into the following error, I was wondering if you knew what I might be missing. 

Warning: require(config.php): failed to open stream: No such file or directory in /var/www/html/moodle/report/mergeusers_mdl_2.php on line 90 Fatal error: require(): Failed opening required 'config.php' (include_path='.:/usr/share/pear:/usr/share/php') in /var/www/html/moodle/report/mergeusers_mdl_2.php on line 90

Thanks again, 

Forrest Gaston

In reply to Forrest Gaston

Re: User Merge Utility for Moodle 2.0

by Mike Holzer -

Try putting the script in the same directory where the config file is located. In your case I'm guessing it will be in /var/www/html/moodle.

In reply to Mike Holzer

Re: User Merge Utility for Moodle 2.0

by Forrest Gaston -

Mike, 


Sorry I didn't catch that, now however I'm running into the followin debug messages:

Coding problem: $PAGE->context was not set. You may have forgotten to call require_login() or $PAGE->set_context(). The page may not display correctly as a result

  • line 345 of /lib/pagelib.php: call to debugging()
  • line 617 of /lib/pagelib.php: call to moodle_page->magic_get_context()
  • line 1220 of /lib/weblib.php: call to moodle_page->__get()
  • line 920 of /lib/pagelib.php: call to format_string()
  • line 2406 of /lib/deprecatedlib.php: call to moodle_page->set_title()
  • line 226 of /mergeusers.php: call to print_header_simple()

The script also appears to stop at Processing table = mdl_event table with an error writing to database. 


Thanks again, 

Forrest Gaston

In reply to Forrest Gaston

Re: User Merge Utility for Moodle 2.0

by Mike Holzer -

Forrest, the first of those are notices set by moodle when debugging is turned on. They are caused by the script calling print_header_simple() which is deprecated.  So nothing to worry about there.

I will look into the event table. That may also be one of those tables that have to be skipped due to a compound primary key.  Thanks for trying out my script and finding some bugs smile

In reply to Paul Broomfield

Re: User Merge Utility

by Luis de Vasconcelos -

Hi Paul,

Does your script work in Moodle 2.x?

And can your script be used on any database or have you only tested and used it on MySQL?

In reply to Paul Broomfield

Re: User Merge Utility

by Krishna B -

Hi Paul, 

Is this designed for Moodle 2.x.x

Thanks

 

In reply to Krishna B

Re: User Merge Utility

by Luis de Vasconcelos -

Krishna, Paul is no longer subscribed to this forum, so he probably has not seen our messages. Vote for MDL-24443 and maybe moodle.org will hear our calls for help...

In reply to Luis de Vasconcelos

Re: User Merge Utility

by IT Coordinator -

I'm concerned that when I rename the user accounts in my external database that it will create new accounts in Moodle.  Merging the duplicates sounds like the way to go. 

I'm using the standard MySql database on Moodle 1.9.  Can anyone point out which script it best for me?  I was hoping that an official one existed...

 

Thanks!

In reply to Nicolas Dunand

Re: User Merge Utility

by Luis de Vasconcelos -

Nicolas,

You said:
> I use v2 on a regular basis and it hasn't ever let me down.

This is going back a while, but do you still use that script? If so, can it handle the advanced features in Moodle like Groups, Groupings and even Cohorts.

Example:
User X is enrolled in Group XX in. Course XXX.
User Y is enrolled in Group YY in Course XXX.
Now you merge User X with User Y so that User Y remains. Which Group do you put User Y into in that course?

Does your script handle all of this? If not, then what happens to those group and groupings assignments during the merge?

And what happens when you throw other advanced Moodle functionalities like Cohorts into the picture?

Thanks.

In reply to Luis de Vasconcelos

Re: User Merge Utility

by Nicolas Dunand -
Picture of Core developers Picture of Plugin developers

Hi Luis,

I said this and this still stands true. Our main production site is still running Moodle 1.9.14 and the mergeusers.php works fine with it.

However, I still haven't had time to pursue the upgrade of this script to work with Moodle 2.x. I however plan to have a working version within the next few months, as we're planning an upgrade to Moodle 2.2 (or 2.3) next usummer.

In reply to Nicolas Dunand

Re: User Merge Utility

by Mike Holzer -

Nicolas,

Here's the script for Moodle 2 I wrote based on your script. Thanks for your original work!

Merge Users For Moodle 2

Average of ratings: Useful (2)
In reply to Mike Holzer

User Merge Utility - Please Test

by Forrest Gaston -

Mike, 

I want to thank you for your hard work, since I had some issues on my site, I had a vendor make some changes to your script. 

Attached is what I have received from the vendor, it needs to be installed on Moodle 2.2 (I don't know about earlier or later versions). 

It goes into /moodlefolder/reports

I had a GUI interface added, based on a past project. 

Mike and all users, please feel free to test and give me your feedback or make changes etc
. **PLEASE test on a development server **

Forrest 

Average of ratings: Useful (1)
In reply to Forrest Gaston

Re: User Merge Utility - Please Test

by Janet Smith -

Forrest,

Can you give some details on where this needs to be installed? Does it go into the admin folder? It doesn't seem to be a block or a module. Also how do you navigate to it? Via URL?


Thanks!

In reply to Forrest Gaston

Re: User Merge Utility - Please Test

by Leon hedding -

I found the attachment quite helpful. We are running 2.3 on Postgresql so I had to update the queries to work on Postgres and not MySQL. We only have userid's as 'userid' and don't have user_id and id_user so I did not make it work with them.

In reply to Forrest Gaston

Re: User Merge Utility - Please Test

by Nicolas Dunand -
Picture of Core developers Picture of Plugin developers
Hi Forrest, Many thanks for sharing this new plugin. After examining the code in index.php, I have trouble understanding why the script has to iterate twice over all tables in the DB. Indeed, in this script the foreach() blocks on lines 305-340 and lines 345-395 look very simmilar.
In reply to Nicolas Dunand

Re: User Merge Utility - Please Test

by Scott Karren -
I had forgotten that I posted the question originally. All I can say is WOW! it is amazing that a few in the community have taken a simple request for help and turned it into actual code and plugins. I love the Moodle Community!! Scott
In reply to Nicolas Dunand

Re: User Merge Utility - Please Test

by Forrest Gaston -

Nicholas, 

To be honest I hired a contractor to buid it, so I really don't know, my apologies. 

Forrest 

In reply to Forrest Gaston

Re: User Merge Utility - Please Test

by Forrest Gaston -

Attached is the latest version I had a vendor update for Moodle 2.3.  I ran into an issue where I couldn't assign the permission to merge users with out giving "change system" permissions.   

This one uses a menu GUI interface similar to an older one I had for 1.9

I'm using a Mysql Database on Linux. 

Thanks

Forrest Gaston

Average of ratings: Useful (2)
In reply to Forrest Gaston

Re: User Merge Utility - Please Test

by Nicolas Dunand -
Picture of Core developers Picture of Plugin developers

Hi Forrest,

Thanks a lot for the updated version. It seems to work great on Moodle 2.3.

I took the liberty to polish it up a bit, adding the following features:

  • code cleanup (indentation, etc.)
  • basic database transaction support (for the database engines supporting it, i.e. InnoDB)
  • localization support
  • cleaner output: full Moodle page with navigation & blocks, etc.
  • outputs a list of the queries effectively sent to the database, to help with debugging if necessary
  • and a few other little things I can't remember

What would remain to do is

  • support MSSQL, Oracle, PostgreSQL
  • enhance rollback capabilities
  • ...
Average of ratings: Useful (1)
In reply to Nicolas Dunand

Re: User Merge Utility - Please Test

by Alain Depaepe -

Hi Nicolas,

I've installed your revised scripts on our moodle 2.3.3 dev setup. During install I receive a 'success' message but after pushing the "continue" button I receive following errors :

Capability "report/mergeusers:view" was not found! This has to be fixed in code.

  • line 377 of /lib/accesslib.php: call to debugging()
  • line 1169 of /lib/adminlib.php: call to has_capability()
  • line 3418 of /lib/navigationlib.php: call to admin_externalpage->check_access()
  • line 3450 of /lib/navigationlib.php: call to settings_navigation->load_administration_settings()
  • line 3401 of /lib/navigationlib.php: call to settings_navigation->load_administration_settings()
  • line 3298 of /lib/navigationlib.php: call to settings_navigation->load_administration_settings()
  • line 716 of /lib/pagelib.php: call to settings_navigation->initialise()
  • line 732 of /lib/pagelib.php: call to moodle_page->magic_get_settingsnav()
  • line 5853 of /lib/adminlib.php: call to moodle_page->__get()
  • line 436 of /admin/index.php: call to admin_externalpage_setup()

I've checked the settings.php file and required libs but I'm unable to find the error.
Have you got any idea why this error is being generated?

Many thanks in advance,

Alain

In reply to Alain Depaepe

Re: User Merge Utility - Please Test

by Nicolas Dunand -
Picture of Core developers Picture of Plugin developers

Hi Alain,

Thanks for your problem report. The attached version fixes this problem.

Average of ratings: Useful (2)
In reply to Nicolas Dunand

Re: User Merge Utility - Please Test

by Luis de Vasconcelos -

Nicolas

Thanks. What happens inside the Moodledata folder when two user profiles are merged? Does any data in that Moodledata folder get changed or deleted?

I suppose the question is the same for the normal Delete User function in Moodle - what happens inside the Moodledata folder when a user is deleted in Moodle?

Thanks.

In reply to Mike Holzer

Re: User Merge Utility

by Nicolas Dunand -
Picture of Core developers Picture of Plugin developers

Hi Mike,

Many thanks! This script looks great, and much more thorough than the one I wrote quite a while ago.

In reply to Nicolas Dunand

Re: User Merge Utility

by Ashutosh Taunk -
Dear Nicolas,
Thanks for this lovely plugin.

I have been working on this from two days now. I am not able to use it on Moodle Version 2.8 . It throws me the following error.
Kindly suggest the the approach to adopt on the attached error please.

Attachment FireShot Screen Capture #007 - BLPL_ Administration_ Users_ Accounts_ Merge user accounts_ Merge user accounts - ataunk_on_ballisticlearning_com_MoodleCode_admin_tool_mergeusers_index_php.png
In reply to Mike Holzer

User Merge Utility -- Error with Moodle 2.5 (Build: 20130514)

by Steve Yoss -

Hey Team!

Thank you for making this plugin. I think this is a fantastic idea! I was able to get the plugin installed, but I'm not able to get it operate. When I try to operate the plugin on my dev site I'm getting the following error:

Merging «mergedaccount2» (user ID = 6) into «mergedaccount1» (user ID = 5)
Unsupported database type


Debug info:
Error code: errordatabase
Stack trace:
line 476 of /lib/setuplib.php: moodle_exception thrown
line 126 of /report/mergeusers/index.php: call to print_error()

Here is some information about by dev site:
Moodle -- 2.5 (Build: 20130514)
MySQL -- 5.5.30
PHP -- 5.4.15

Do you know why this error might be occurring? I've attempted to debug, but there isnt' much to work with. Any insight would be great appreciated. Thank you!!

Cheers.

Steve.

 

Attachment MergeAccount_Error.png
In reply to Steve Yoss

Re: User Merge Utility -- Error with Moodle 2.5 (Build: 20130514)

by Luis de Vasconcelos -

"Unsupported database type"

What database engine are you running Moodle on? MSSQL? Postgres? Oracle?

In reply to Scott Karren

Re: User Merge Utility

by Jordi Pujol-Ahulló -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Hi all,

Lastly we have reported a new version of the plugin including several new functionalities (see https://github.com/ndunand/moodle-mergeusers for more details). Among others, the plugin is improved by:

  • Considering several database compound indexes,
  • Manual and CLI scripts are allowed for merging users.
  • Merging results are now stored in database so that they are available for further reference within the same Moodle.
  • Highly customizable for your Moodle instance when necessary (mainly for non-core plugins reporting merging problems).

Try it and share your opinions!

PS: In the Moodle of our University we have it in production, and we are really very happy wink

Saludos,

Jordi

Average of ratings: Useful (1)
In reply to Jordi Pujol-Ahulló

Re: User Merge Utility

by Florent Lartet -

Hello,

it looks to be a good job.

Do you think it could be used to remove completely old users from the database ? I mean merging 'very old user' with a special user 'deleted user' many times for example ?

In reply to Jordi Pujol-Ahulló

Re: User Merge Utility

by James Hamilton -

Hi folks, 

Has this been tested on 3.0? We make extensive use of this plugin and would love to see a version bump in the plugins DB.

Ta, 

James

In reply to Scott Karren

Re: User Merge Utility

by alejandra porras -

Hello, 

I find it very interesting plugin, and install it, but when I want to merge accounts shows this error and do not know what to do. I have installed moodle 2.8.5.

Thanks

Attachment print_moodle.png
In reply to alejandra porras

Re: User Merge Utility

by Peter Sander -

Did you find an answer to your question? I seem to be getting exactly the same thing...



Notice: Use of undefined constant MCRYPT_MODE_ECB - assumed 'MCRYPT_MODE_ECB'


Merge user accounts

1. Choose users to merge /  2. Confirm users to merge /  3. Merging results and log

Merged «abar» (user ID = 1744) into «ba300333» (user ID = 1829)

For further reference, these results are recorded in the log id 2.
Some error occurred:

Exception thrown when merging: 'Error writing to database".

Trace:
#0 /var/www/moodle/lib/dml/pgsql_native_moodle_database.php(244): moodle_database->query_end(false)
#1 /var/www/moodle/lib/dml/pgsql_native_moodle_database.php(682): pgsql_native_moodle_database->query_end(false)
#2 /var/www/moodle/admin/tool/mergeusers/lib/table/generictablemerger.php(225): pgsql_native_moodle_database->execute('UPDATE mdl_assi...')
#3 /var/www/moodle/admin/tool/mergeusers/lib/table/generictablemerger.php(80): GenericTableMerger->updateRecords(Array, Array, 'userid', Array, Array)
#4 /var/www/moodle/admin/tool/mergeusers/lib/mergeusertool.php(281): GenericTableMerger->merge(Array, Array, Array)
#5 /var/www/moodle/admin/tool/mergeusers/lib/mergeusertool.php(216): MergeUserTool->_merge('1829', '1744')
#6 /var/www/moodle/admin/tool/mergeusers/index.php(135): MergeUserTool->merge('1829', '1744')
#7 {main}


Merge failed! 
Your database engine supports transactions. Therefore, the whole current transaction has been rolled back and no modification has been made to your database.


In reply to Peter Sander

Re: User Merge Utility

by Gary Lynch -

Hi

If you look at this discussion on Github Here it explains the temporary fix for this issue

HTH Gary smile


In reply to Scott Karren

Re: User Merge Utility

by Tabitha Parker (was Roder) -

Hi

We have encountered an issue when merging to user accounts where both accounts have been used by the student.

Student starts a quiz, saves some answers but does not submit attempt. 

Student does the same thing in the other account. There are now two accounts and both have some submitted quiz attempts and some open quiz attempts.

Merge the two accounts and we lose the open attempts, we only get submitted quiz attempts in the remaining account.

Can this be rectified?

We have restored a backup of the whole site to another environment and have given students access to it so they can copy and paste all their unsubmitted attempts into the remaining user account on the production site but as you can imagine this is less than ideal given the course in question has over a thousand questions in total and we have no easy way to see what they need to copy and paste over to the production site.