From 7da63d59f4e6c8a8c4f04aa11b3fab439e0a1015 Mon Sep 17 00:00:00 2001 From: Michael Milette Date: Sun, 8 Nov 2020 09:39:00 -0500 Subject: [PATCH] {profile_field_shortname} now supports textarea type custom fields. --- CHANGELOG.md | 1 + filter.php | 22 +++++++++++++--------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e407616..3fa8e54 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ All notable changes to this project will be documented in this file. - FAQ: Information on how to patch Moodle to enable FilterCodes in the custom menu. - FAQ: Search the README.md file for the word Troubleshooting to now find helpful information. - Fixed {diskfreespace} and {diskfreespacedata} on very large/unlimited storage. Note: Greater than about 84,703.29 Yottabyte (YB) is now considered infinite. +- {profile_field_shortname} now supports textarea type custom fields. ## [2.0.0] 2020-07-01 ### Added diff --git a/filter.php b/filter.php index 76c6d50..b802a83 100644 --- a/filter.php +++ b/filter.php @@ -427,19 +427,23 @@ public function filter($text, array $options = []) { // Custom Profile Fields. if (stripos($text, '{profile_field') !== false) { if (isloggedin() && !isguestuser()) { - // Cached the visibity status of all the defined custom profile fields. + // Cached the defined custom profile fields and data. static $profilefields; + static $profiledata; if (!isset($profilefields)) { - $profilefields = $DB->get_records('user_info_field', null, '', 'shortname, visible'); + $profilefields = $DB->get_records('user_info_field', null, '', 'id, shortname, visible'); + if (!empty($profilefields)) { + $profiledata = $DB->get_records_menu('user_info_data', ['userid' => $USER->id], '', 'fieldid, data'); + } } - foreach ($USER->profile as $field => $value) { - $shortname = strtolower($field); - // If the tag exists and it is not hidden in the custom profile field's settings. - if (stripos($text, '{profile_field_' . $shortname . '}') !== false - && $profilefields[$field]->visible != '0') { - $replace['/\{profile_field_' . $shortname . '\}/i'] = $value; + + foreach ($profilefields as $field) { + // If the tag exists and is not set to "Not visible" in the custom profile field's settings. + if (stripos($text, '{profile_field_' . $field->shortname . '}') !== false && $field->visible != '0') { + $data = !empty($profiledata[$field->id]) ? $profiledata[$field->id] : ''; + $replace['/\{profile_field_' . $field->shortname . '\}/i'] = $data; } else { - $replace['/\{profile_field_' . $shortname . '\}/i'] = ''; + $replace['/\{profile_field_' . $field->shortname . '\}/i'] = ''; } } }