Setting a user custom field to the date/time the user was created

Setting a user custom field to the date/time the user was created

by Chris Swinney -
Number of replies: 6

Hi,

I am looking for a way to set a custom user profile date/time field to be equal to the time that the user object was created. 

The reason is that we are looking to filter and automate course assignments which can be invoked via a custom user profile field. This will also help with cohort assigning. 

We want to automatically create a cohort based on the month or bi month of creation, e.g.

  • Oct 2020, Nov 2020, Dec 2020
    or
  • Sept-Oct 2020, Nov-Dec 2002

etc


Average of ratings: -
In reply to Chris Swinney

Re: Setting a user custom field to the date/time the user was created

by Mark Sharp -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers
Having set up your profile field, you need to program an event observer, this could be in your theme, it could be in any of your own plugins.

The event you want to observe is: \core\event\user_created. The event will give you the userid of the new user, so you can do whatever you need to do with that user.

Look in /lib/classes/event/user_created.php for info on the event. And https://docs.moodle.org/dev/Events_API#Event_observers for some documentation on programming events.

Don't forget to search the code base for "user_created" and you'll find examples of it being used in Moodle.
Average of ratings: Useful (1)
In reply to Mark Sharp

Re: Setting a user custom field to the date/time the user was created

by Chris Swinney -
Thanks @mark. I did wonder if it would involve me building something (which is not something I have done in Moodle just yet), so this is going to take me time just to figure out opening an IDE ;).

I was hoping that there would be a hidden field in the user data table that already contained this info that I could then just copy across to the custom profile filed to expose. I guess not sad.
In reply to Chris Swinney

Re: Setting a user custom field to the date/time the user was created

by Chris Swinney -
Actually, I believe there IS a field that already contains this info (from https://docs.moodle.org/dev/Database_schema_introduction#Users_and_their_profiles - albeit out of date, but also https://www.examulator.com/er/output/tables/user.html or https://moodleschema.zoola.io/tables/user.html)

`user` table; `timecreated` field

This is a BIGINIT datatype, so would be (I guess) a Unix style epoch value. The question then is, how would I leverage this?
In reply to Chris Swinney

Re: Setting a user custom field to the date/time the user was created

by Randy Thornton -
Picture of Documentation writers
Chris,

Yes, the timecreated field in the user table is a Unix timestamp as are all dates and times internally in the database.

If you need to automate this, then you would need to do some coding, to extract and convert that field and push it into the custom profile field.

If you are okay with doing this manually on a periodic basis, you can use the SQL simple report below I just crufted together. You can use this with the Configurable Reports plugin to give you a properly formatted CSV file that you could then use in the Upload users tool to fill in the custom profile field.

In this basic example, I have named the custom profile field "yearmonth" formatted as YYYY-MM but it could be changed to be anything you need. of course.

SELECT
u.username,
u.email,
u.firstname,
u.lastname,
DATE_FORMAT(FROM_UNIXTIME(u.timecreated),'%Y-%m') AS profile_field_yearmonth
FROM prefix_user u
WHERE u.id > 2 AND u.deleted <> 1
ORDER BY u.username


Save this in Config Reports as a CSV file and you can upload as is to update all existing users. (The custom field itself needs to already exist of course.)

This could help to retro-fit any existing users. But if you need more or less real time updating when new users are created, then you will need some other method, with PHP or web access API. But hopefully this will point you in a useful direction.


Randy

Average of ratings: Useful (4)
In reply to Randy Thornton

Re: Setting a user custom field to the date/time the user was created

by Chris Swinney -
Nice, thanks, Randy.

FWIW, I did also try to use the AutoCohort plugin that can actually access their field and the field can eve be manipulated with a Moustache template, unfortunately, though, the plugin doesn't seem to pull in the template "helper" used elsewhere in Moodle that can actually format the Unix timestamp. Changing the code here is beyond what I am able to do, but the SQL stuff might well be doable.
In reply to Chris Swinney

Re: Setting a user custom field to the date/time the user was created

by Randy Thornton -
Picture of Documentation writers
Chris,

I had thought about mentioning that plugin, but it has not worked for me so well and it is getting a bit long in the tooth now. I would love it if the ULM plugin to put users in Cohorts by profile field would support the standard profile fields too. They do excellent work with their plugins.

I can't think of a better way to do this at the moment though.