Change Date format for user_bulk_download.php

Change Date format for user_bulk_download.php

by Daryl Moon -
Number of replies: 2

I've made a small change to the report user_bulk_download.php in /var/www/html/moodle/admin/user to add the field lastaccess so I can run an extract to MailChimp that includes the user's last access date.

The extract report works but lastaccess is stored in UnixTime format and I need it in DD/MM/YYYY format for Mailchimp.

I know that I need to do something like this:

DATE_FORMAT(FROM_UNIXTIME(`lastaccess`),'%d/%m/%Y') AS lastaccess

 to reformat the lastaccess date format, but I'm unsure where to put this in the code:

The code snippet below starts at line 42 in user_bulk_download.php


if ($dataformat) {
    $fields = array('id'        => 'id',
                    'username'  => 'username',
                    'email'     => 'email',
                    'firstname' => 'firstname',
                    'lastname'  => 'lastname',
                    'city'      => 'city',
                    'country'   => 'country',
'lastaccess' => 'lastaccess');
    if ($extrafields = $DB->get_records('user_info_field')) {
        foreach ($extrafields as $n => $field) {
            $fields['profile_field_'.$field->shortname] = $field->shortname;
            require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
        }
    }

Can someone please point me in the right direction?

thanks

Average of ratings: -
In reply to Daryl Moon

Re: Change Date format for user_bulk_download.php

by Darko Miletić -

Your assumption is incorrect. The DATE_FORMAT applies only if you have control over the SQL query which is not the case here. Since the data is built in the PHP we need to use PHP to handle it.

Modification should be made in the anonymous function that builds the data.

    download_as_dataformat($filename, $dataformat, $fields, $iterator, function($userid) use ($extrafields, $fields) {
global $DB;
$row = array();
if (!$user = $DB->get_record('user', array('id' => $userid))) {
return null;
}
foreach ($extrafields as $field) {
$newfield = 'profile_field_'.$field->datatype;
$formfield = new $newfield($field->id, $user->id);
$formfield->edit_load_user_data($user);
}
$userprofiledata = array();
foreach ($fields as $field => $unused) {
// Custom user profile textarea fields come in an array
// The first element is the text and the second is the format.
// We only take the text.
if (is_array($user->$field)) {
$userprofiledata[$field] = reset($user->$field);
} else {
$userprofiledata[$field] = $user->$field;
}
}
return $userprofiledata;
});


And should be changed to this.

    download_as_dataformat($filename, $dataformat, $fields, $iterator, function($userid) use ($extrafields, $fields) {
global $DB;
$row = array();
if (!$user = $DB->get_record('user', array('id' => $userid))) {
return null;
}
foreach ($extrafields as $field) {
$newfield = 'profile_field_'.$field->datatype;
$formfield = new $newfield($field->id, $user->id);
$formfield->edit_load_user_data($user);
}
$userprofiledata = array();
foreach ($fields as $field => $unused) {
// Custom user profile textarea fields come in an array
// The first element is the text and the second is the format.
// We only take the text.
if (is_array($user->$field)) {
$userprofiledata[$field] = reset($user->$field);
} else {
$value = $user->$field;
if ($field == 'lastaccess') {
$value = userdate($value, '%d/%m/%Y');
}
$userprofiledata[$field] = $value;
}
}
return $userprofiledata;
});


Average of ratings: Useful (1)
In reply to Darko Miletić

Re: Change Date format for user_bulk_download.php

by Daryl Moon -

Thank you so much Darko, that reply was perfect! I really appreciate the time you took to help me solve this, thanks again.