Friday, August 31, 2012

Updating moodle core code

Well, I have made my first hack, errr, enhancement to the moodle core code.  I really did not want to do this, but i am a programmer at heart and wanted to appease my customer.  The scenario was simple.  She does a lot of bulk user uploads to create accounts, fine.  We are settings the default password for every new account with the requirement to update upon initial login.  No worries, moodle provides a select list with a choice to Force password updates for All records uploaded.  The *problem was if she forgot to change that value, she could be creating a lot of accounts with the same password that would not require the new user to update.  She asked me, "James, is there anyway to change the default value for that dropdown menu?".

Of course, there is always a way. 

How do I change the default behavior of a core moodle form?

I set off to find the code that was setting this and to change it.  Not an overly complex request.  I found this forum that discussed this very scenario, but the answer was for an older version of moodle.

After looking at the URL in moodle when in the user uploads mode, I was eventually lead into the following file.

webroot/sitename/admin/tool/uploaduser/user.form.php

After a bit of trial and error, I realized the $choices array that contained the options for the select box did not have a default selection!  The others around it did, but not the one I was after.  This is the line of code I added to the file that was successful in changing the default selection to ALL

$mform->setDefault('uuforcepasswordchange', 2); // starting at 0, 2 is represents the last option.

In the end, I added my fix to the forum discussion for moodle 2.3.

No comments:

Post a Comment