diff --git a/admin/user/user_bulk_download.php b/admin/user/user_bulk_download.php index 3220518624f..1e908f67cce 100644 --- a/admin/user/user_bulk_download.php +++ b/admin/user/user_bulk_download.php @@ -22,6 +22,8 @@ * @package core */ +use core_user\fields; + define('NO_OUTPUT_BUFFERING', true); require_once('../../config.php'); require_once($CFG->libdir.'/adminlib.php'); @@ -37,7 +39,7 @@ if (empty($SESSION->bulk_users)) { } if ($dataformat) { - $fields = array('id' => 'id', + $originfields = array('id' => 'id', 'username' => 'username', 'email' => 'email', 'firstname' => 'firstname', @@ -51,8 +53,10 @@ if ($dataformat) { 'country' => 'country'); $extrafields = profile_get_user_fields_with_data(0); + $profilefields = []; foreach ($extrafields as $formfield) { - $fields['profile_field_'.$formfield->get_shortname()] = 'profile_field_'.$formfield->get_shortname(); + $profilefields[fields::PROFILE_FIELD_PREFIX . $formfield->get_shortname()] = fields::PROFILE_FIELD_PREFIX . + $formfield->get_shortname(); } $filename = clean_filename(get_string('users')); @@ -60,17 +64,17 @@ if ($dataformat) { $downloadusers = new ArrayObject($SESSION->bulk_users); $iterator = $downloadusers->getIterator(); - \core\dataformat::download_data($filename, $dataformat, $fields, $iterator, function($userid, $supportshtml) - use ($extrafields, $fields) { + \core\dataformat::download_data($filename, $dataformat, array_merge($originfields, $profilefields), $iterator, + function($userid, $supportshtml) use ($originfields) { global $DB; if (!$user = $DB->get_record('user', array('id' => $userid))) { return null; } - profile_load_data($user); + $userprofiledata = array(); - foreach ($fields as $field => $unused) { + foreach ($originfields as $field) { // 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. @@ -82,6 +86,19 @@ if ($dataformat) { $userprofiledata[$field] = $user->$field; } } + + + // Formatting extra field if transform is true. + $extrafields = profile_get_user_fields_with_data($userid); + foreach ($extrafields as $field) { + $fieldkey = fields::PROFILE_FIELD_PREFIX . $field->get_shortname(); + if ($field->is_transform_supported()) { + $userprofiledata[$fieldkey] = $field->display_data(); + } else { + $userprofiledata[$fieldkey] = $field->data; + } + } + return $userprofiledata; }); diff --git a/user/profile/field/datetime/field.class.php b/user/profile/field/datetime/field.class.php index bb58a6fc1a5..7ffa3efe64a 100644 --- a/user/profile/field/datetime/field.class.php +++ b/user/profile/field/datetime/field.class.php @@ -134,4 +134,13 @@ class profile_field_datetime extends profile_field_base { public function get_field_properties() { return array(PARAM_INT, NULL_NOT_ALLOWED); } + + /** + * Check if the field should convert the raw data into user-friendly data when exporting + * + * @return bool + */ + public function is_transform_supported(): bool { + return true; + } } diff --git a/user/profile/lib.php b/user/profile/lib.php index d1a64098c98..ea04d63109e 100644 --- a/user/profile/lib.php +++ b/user/profile/lib.php @@ -582,6 +582,15 @@ class profile_field_base { public function get_field_properties() { return array(PARAM_RAW, NULL_NOT_ALLOWED); } + + /** + * Check if the field should convert the raw data into user-friendly data when exporting + * + * @return bool + */ + public function is_transform_supported(): bool { + return false; + } } /** diff --git a/user/upgrade.txt b/user/upgrade.txt index 9f45297eaf1..8ee39d48394 100644 --- a/user/upgrade.txt +++ b/user/upgrade.txt @@ -1,6 +1,11 @@ This files describes API changes for code that uses the user API. === 4.1 === + +* Added a new method is_transform_supported() in the profile_field_base class. + The purpose is to allow the field to be transformed during the export process. + It has been implemented in the Date/Time data type (Applied in 4.1, 4.0.6). + * user_get_user_details_courses() now accepts an optional second parameter, an array of userfields that should be returned. The values passed into the $userfields parameter must all be included in the return from user_get_default_fields().