Deleted student accounts from Moodle

Deleted student accounts from Moodle

Anne Grant發表於
Number of replies: 11

Hello

I have deleted a number of student accounts from Moodle but I am now required to reinstate these students. The version of Moodle is Moodle2.5.1+ (Build: 20130719)

I raised a ticket with my server hosting company but they have been unable to provide me with a solution.

How could I reinstate the deleted students without overwriting any current data?

I am not a technical person and know almost nothing about databases and MySQL databases.

Any assistance you can provide would be gratefully received.

Regards
Anne

評比平均分數: -
In reply to Anne Grant

Re: Deleted student accounts from Moodle

Itamar Tzadok發表於

There is probably no easy way to do that.

When you delete a user from Moodle the user record is not removed from DB but only modifed. If you just need to restore the user accounts you can ask your hosting service to run an SQL query on the mdl_user table to find the respective records and change the value in the 'deleted' column from 1 to 0. Then you will be able to see these user accounts under Administration > Accounts > Browse list of users, and proceed to change username and email address to the original.

Restoring data for these users is a whole different story. You may need to extract relevant data from a proper backup file and try to restore it. But this is going to be very difficult and very technical.

hth 微笑

In reply to Anne Grant

Re: Deleted student accounts from Moodle

G. M.發表於

Hello Anne,

The following info is for the people at the support area (or for a friend with the required knowledge); it will allow them to recover the deleted accounts. Please first see about the "Important notes" at the end of the post.

1. Run the following SQL statement:

SELECT id,
       username,
       firstname,
       lastname,
       email
FROM   mdl_user
WHERE  deleted = 1

That should get you a table with all the "deleted" users; something like this (I'm just showing one record; in your case you'll get many):

idusernamefirstnamelastnameemail
7 x5@example.com.1409337561 x5fn x5ln e3d704f3542b44a621ebed70dc0efe13

 

As shown, the query returns the Id, along with other useful data, of those accounts that have been deleted. The support area will have to send you the results so you may then send them back the list with the Id of the accounts you wish to be recovered.


Now, to undelete the required accounts they will need to run...

First this SQL statement:

UPDATE mdl_user
SET    deleted = 0,
       email = LEFT( username, LENGTH( username ) - 11 )
WHERE  id = #'
   OR  id = #''
   OR  id = #'''

 and then this one:

UPDATE mdl_user
SET    username = CONCAT( "user_", CAST( id AS CHAR ), "_rec" )
WHERE  id = #'
   OR  id = #''
   OR  id = #'''

Finally, the following query will allow to confirm about the changes done to the table:

SELECT id,
       username,
       firstname,
       lastname,
       email
FROM   mdl_user
WHERE  id = #'
   OR  id = #''
   OR  id = #'''

This query will return the list of the recovered accounts; something like this:

idusernamefirstnamelastnameemail
7 user_7_rec x5fn x5ln x5@example.com

 


Important notes.

1. The prefix "mdl_" of the user table is the default prefix used on a normal installation, but another prefix could have been used; if that were the case, then the SQL statements will have to be adjusted as required.

2. The number 11 at the second query (the first update query) refers to the number of characters to be skipped from the deleted username (shown at the first result table) in order to get the original email address.

3. The #', #'', #''' strings represent the Id numbers of the accounts to be recovered, so those strings need to be substituted with the real Id numbers, for example:

WHERE  id = 7
   OR  id = 22
   etc.

4. As shown in the last table, a username string was created based on the Id of the user; it will need to be updated by the Moodle admin with the correct, original value, for each user, through the User profile page.

https://docs.moodle.org/27/en/Update_profile

 

評比平均分數:Useful (1)
In reply to G. M.

Re: Deleted student accounts from Moodle

Colin Fraser發表於
Documentation writers的相片 Testers的相片

Wow... I am overwhelmed at the responses, and for a non-technical person, that explanation of Guillermo's is brilliant, and I expect, so difficult to follow.  

Too many steps to get it to work and fear of wrecking the database is going to prevent me from doing it but I would ask the hosts to do it for me, they should have the required skills... 

Another thing I could do is ask my hosts if I could access the database via phpMyAdmin, depending on the type of database they are using. They are likely to say no, but if you don't ask, then you will never know. phpMyAdmin is easy to use, just look for the mdl_user table in the left column then the Deleted field and edit the line to change the 1 to a 0. 

If your hosts are entirely uncooperative, you might want to think about changing services... 

Another thing you could do, and probably one of the first to try is to look at this page in Moodle Docs: https://docs.moodle.org/27/en/Upload_users  

and create a csv file that appends the user's name with the field "deleted" that looks like: 

username,firstname,lastname,email,deleted

henry.smith,henry,smith,smith.henry31@mysite.net.tz, 0

mary.jones,mary,jones,jones.mary6@mysite.net.tz,0


This provides the minimum information required to allow a csv file to run. Give yourself the best opportunity to get this to work by not using Notepad, try Notepad++ or TextPad or TextEdit or any text writer that saves a file in the UTF8 format. I have created the file in MS-Excel using the columns to ensure I am putting things in the right place and format, (because *.csv files need to be exactly right) then saving as a *.csv file. I have then opened it in TextPad, added some text, deleted it, resaved it as UTF8 and then uploaded it. This gets rid of any unseen characters that may be added when saved, like a bom, or other eof char hidden when saved.  

If you have a test moodle somewhere, try it there first, if you don't then I seriously suggest you download and install the Xampp Moodle, assuming you use Windows at home, and test things out there before you do anything else. This also helps you gain some serious knowledge about Moodle, by blundering about and wrecking your home Moodle and not your production Moodle makes you look good too..微笑  

 

評比平均分數:Useful (1)
In reply to Colin Fraser

Re: Deleted student accounts from Moodle

G. M.發表於

Hello Colin,

I was warned and that's why I started with, "The following info is for the people at the support area微笑

Truly, it's not that a complex solution, one select and two update queries, and a couple of pointers to avoid possible problems. It may just look harder than it is; giving brief trouble-free written descriptions is almost impossible, so one has to extend a bit.

TextPad is certainly an excellent text editor (it's the one I use) though it doesn't really support Unicode (only works with the windows-1252 code page):

http://forums.textpad.com/viewtopic.php?t=11019

Going through the Excel-then-edit path may also present issues because of the character codes, just as you mentioned; maybe it would be best just to create the CSV file directly in the text editor. Regardless, all this CSV talking is really irrelevant because the idea doesn't work.

While I knew that the deleted field can be used to delete an account, I had never heard of using it to recover a deleted account. There is an enrolstatus field that can be used to suspend or activate an account, but that's a different thing.

I got intrigued and while it was clear that trying to restore an account using the original username was a lost cause, I did try it and, evidently, I got a "User not updated-does not exist" result status.

I went then and used the modified username (generated by Moodle when the account is deleted) along with the modified email address. First of all, I received an "Invalid email address. Duplicate address" preview status; so I changed the "Prevent email address duplicates" option to "no" and did the upload. This time I got a "User updated" result status, but the only thing that changed was the authentication mode: it went from "email" (as this was an email self-registered account) to "manual". The account remained deleted.

I'm totally in favor with the idea, but advising "to download & install" to a non-technical user? Now, that's certainly more complicated than asking the support area to run three queries. My solution stands 吐舌頭

In reply to G. M.

Re: Deleted student accounts from Moodle

Colin Fraser發表於
Documentation writers的相片 Testers的相片

My apologies, I missed a bit. You need to ensure that the courses they were enrolled in are also reentered in the csv file, and once they are reenrolled, any existing data is returned to active status and is accessible.  You can reenroll them manually, but if you have a lot of courses that becomes an annoyance. But it does work at restoring users, but there are issues. I tested this with a manual reenrollment and the data returned no problem. 

I seriously suggest you create your own moodle using the same version as on your production server. Run a restore of the course you want to reenroll people into, make sure you have the maximum details you can get into the csv file and then run the csv file. 

Problem 1, I do not know if the backup will take in deleted users, so restoring the course may not add in the original data when the missing students are reenrolled in your test Moodle.

Problem 2. Unless you target users accurately, it is likely to create new records for them, so the whole purpose is lost. 

Try it with one user, see what happens. Worst is it is not going to work as I describe, but I think it will - as long as the identifying information of the user is clear enough to it does not create a new record for the user.   


Clearly the most accurate method is as you suggest Guillermo, and that is one I would use but in this case, an approach to the host is probably the only viable option, let it be their problem. I only suggested this as a possibility as csv files are considerably easier to work with than SQL. 

I am familiar with SQL and would write the script myself. The best option for someone who is not that familiar with it, I suggest, is phpMyAdmin, and once the Moodle table structure is understood, it is seriously easy to edit. And you do not need to know much in the way of SQL let the tool do the work for you. You can use the plpMyAdmin UI to search huge tables quickly and easily, to edit them either inline or in bulk, Just a brilliant tool.  

So here is a question, Users can be easily deleted, their records are retained but why then is there no easy way to reinstate Users?  

Another question, Can phpMyAdmin be reinstated as a plugin but then be restricted to the database of the Moodle it is plugged into? 

mmmmm 

評比平均分數:Useful (1)
In reply to Colin Fraser

Re: Deleted student accounts from Moodle

G. M.發表於

Hi Colin,

On the contrary, it sounded like an interesting idea; I never had thought of it and it really was worth trying out. Now we know.

Indeed, enrolling definitely should be done via a CSV file as it will save her some time. Yes, course data will be there for each user once reenrolled. When a user is deleted, only role assignments, cohort and group membership, enrolments and some other data is deleted, the rest (e.g. grades, forum participation) remains at the database.

Yes, you are completely right and therein lies the missing piece of the answer! 是She should install the Moodle phpMyAdmin plugin... well, actually, the Moodle Adminer plugin:

https://moodle.org/plugins/view.php?plugin=local_adminer

as it seems that phpMyAdmin has been removed from the plugins directory.

Regarding your last questions...

1. I'm just finishing a search business site and so, I'm a bit familiar with PHP (procedural), but I'm not with the Moodle code, so eventhough the idea is simple enough, I wouldn't even know where to start: a stand-alone CLI script, a tool, a plugin? It certainly would be a good addition to the admin toolset.

2. Yes. The Adminer plugin reads the Moodle config.php file and so it only has access to the Moodle database (well, in the case of MySQL, along with the information_schema database, too):

adminer restricted to moodle database only

Well, once Adminer is installed, which is a process easy enough, the rest will be like a walk in the park 微笑

1. Select the moodle database and then select the mdl_user table.

2. Select the SQL command link.

3. Enter the SQL statement at the SQL command text box and click the Execute button.

4. Do the same for each query.

5. Select the Logout, and then the Close, links.

6. Go to the User browse page and update the username at the Edit profile page.

use adminer to recover a deleted account, 1

use adminer to recover a deleted account, 2

In reply to G. M.

Re: Deleted student accounts from Moodle

G. M.發表於

For anyone that may wonder about the changes made to the username and email address fields when a user account is deleted in Moodle:

1. The 10 digit number appended to the email address (all of which will then be used as the modified username) corresponds to the unix time stamp of the date & time (GMT) when the account was deleted. For example, this post was published at 1410797776.

2. The 32 hex character string at the email address is the MD5 hash of the original username. For example, if the original username was "alice", then at the email address will be saved the string "6384e2b2184bcbf58eccf10ca7a6563c".

評比平均分數:Useful (1)
In reply to G. M.

Re: Deleted student accounts from Moodle

Colin Fraser發表於
Documentation writers的相片 Testers的相片

Thanks Guillermo, Adminer is exactly what I was thinking of. And took me maybe all of a minute to install and edit... what a great development that is going to be. 

So, recommendation in future will be to use Adminer.. love it already!!!! 

In reply to Colin Fraser

Re: Deleted student accounts from Moodle

G. M.發表於

Yes, a great to plugin to have installed when one doesn't have direct access to the database, maybe because of limitations with the hosting service provider.

In my case, I'm a phpMyAdmin user 微笑

In reply to Anne Grant

Re: Deleted student accounts from Moodle GRATEFUL THANKS

Anne Grant發表於

Thank you to everyone who responded to my post. I am most grateful.

I have sent your comments to a technical person who hopefully can now fix the issues.

Warmest regards
Anne