jpahullo / moodle-tool_mergeusers

Merge users script for Moodle
https://moodle.org/plugins/view.php?plugin=tool_mergeusers
22 stars 50 forks source link

Merge users plugin for Moodle

This admin tool will merge two Moodle user accounts, "user A" and "user B". The intent of the plugin is to assign all activity & records from user A to user B. This will give the effect of user B seeming to have done everything both users have ever done in Moodle.

Moodle requirements and release notes

Check the CHANGES.md file for Moodle version requirements, system requirements and any news related to the current release of this plugin.

How merge users plugin works

The basic function of the plugin is to loop through the tables and update the userid of every record from user A to user B. This works well for most tables. We do however, have a few special cases:

Command-line script

A cli/climerger.php script is added. You can now perform user mergings by command line having their user ids.

You can go further and develop your own CLI script by extending the Gathering interface (see lib/cligathering.php for an example). Ok, but let us explain how to do it step by step:

  1. Develop a class, namely MyGathering, in lib/mygathering.php, implementing the interface Gathering. Be sure the class name and the filename are the same, but filename all in lowercase ending with ".php". See lib/cligathering for an example.
  2. Create or edit the file config/config.local.php with at least the following content:
<?php

return array(

    // gathering tool
    'gathering' => 'MyGathering',
);
  1. Run as a command line in a form like this: $ time php cli/climerger.php.

Events and event management

Once the merging action is completed, an event 'merging_success' is triggered if it was ok, or an event 'merging_failed' otherwise. The available data on the event are as follows:

The goal of this event triggering is the ability to be detected by other parts of Moodle.

This plugin also manages the 'merging_success' event is trigered, what includes:

  1. Suspending the user (user.suspended = 1). This prevents the person to log in with the old account.
  2. Changing the old user's profile picture by the given on pix/suspended.jpg. It is a simple white image with the text "suspended user", which could help to teachers and managers to rapidly detect them.

Correct way of testing this plugin

First of all, check admin/settings.php?section=mergeusers_settings for the description of the setting tool_mergeusers | transactions_only if your database type and version supports transcations. If so, no action will actually be committed if something goes wrong.

Mainly, these are the main steps to test this plugin:

  1. You should have a replica of your Moodle instance, with a full replica of your Moodle database where you run this plugin.
  2. Run a sufficient amount of user merging to check if anything goes wrong.
  3. What if...?
    1. ... all was ok? You are almost confident that all will be ok also in your production instance of Moodle.
    2. ... something went wrong? There are several reasons for that:
      1. Non-core plugins installed on your Moodle and not assumed in this plugin.
      2. Local database changes on Moodle that may affect to the normal execution of this plugin.
      3. Some compound index not detected yet.

If in your tests or already in production something went wrong, please, report the error log on the official plugin website on moodle.org. And if you have some PHP skill, you can try to solve it and share both the error and the patch to solve it ;-)

Common sense

Before running this plugin, it is highly recommended to back up your database. That will help you to restore the state before any merging action was done.

This plugin stores a log for any user merging, with the list of actions done or errors produced. If your database supports transactions (see above section), automatic rollbacks are done at database level, so that your database state remains consistent.

However, running this plugin in databases without transaction support can put you in trouble. That is, there is no provision for automatic rollbacks, so if something were to fail midway through, you will end up with a half-updated database. Nevertheless, if you found a problem when merging users A and B, do not panic. Merging will be successfully completed when a solution for your problem is included into this plugin, and you rerun merging users A and B.

Development

Developing and testing phase

We recommend to use the moodlehq/moodle-docker project to run your own Moodle instances for developing and testing.

PHPUnit testing

To quickly setup your own development, we suggest to run the command:

php admin/tool/phpunit/cli/util.php --buildcomponentconfigs

as documented at https://docs.moodle.org/dev/PHPUnit to have the phpunit.xml file under admin/tool/mergeusers/phpunit.xml.

Then, you can run all plugin's tests as follows:

vendor/bin/phpunit -c admin/tool/mergeusers

or also like this, without the need of running the buildcomponentconfigs:

vendor/bin/phpunit --group tool_mergeusers

There are also other PHPUnit groups created to help testing only the part of the plugin of your choice. Take a look at the tests code for other group names.

License

GNU GPL v3 or later. http://www.gnu.org/copyleft/gpl.html

Contributors

Maintained by:

See all Github contributors