Skip to content

Commit

Permalink
Use core user fields API #44
Browse files Browse the repository at this point in the history
  • Loading branch information
bwalkerl authored and brendanheywood committed Nov 11, 2024
1 parent 89e5991 commit 9addf95
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
2 changes: 1 addition & 1 deletion classes/complaints_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function __construct($uniqueid, \moodle_url $url, $perpage = 100) {
"u.id, u.email, up1.name, up2.name",
"{$DB->sql_cast_char2int('up1.value')} AS bouncecount", // Casting required for table sorting on numeric values.
"{$DB->sql_cast_char2int('up2.value')} AS sendcount",
get_all_user_name_fields(true, 'u'),
helper::get_username_fields('u'),
];
$from = '{user} u '; // Keep this trailing space.
$joins = [
Expand Down
55 changes: 55 additions & 0 deletions classes/helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Helper class for tool_emailutils.
*
* @package tool_emailutils
* @copyright Catalyst IT 2024
* @author Benjamin Walker <[email protected]>
* @copyright 2024 onwards Catalyst IT {@link http://www.catalyst-eu.net/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace tool_emailutils;

/**
* Helper class for tool_emailutils.
*
* @package tool_emailutils
* @copyright Catalyst IT 2024
* @author Benjamin Walker <[email protected]>
* @copyright 2024 onwards Catalyst IT {@link http://www.catalyst-eu.net/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class helper {

/**
* Gets the username fields for the fullname function. Contains legacy support for 3.9.
* @param string $tablealias
* @return string username fields, without a leading comma
*/
public static function get_username_fields(string $tablealias = ''): string {
// Use user field api, added in 3.11.
if (method_exists('\core_user\fields', 'for_name')) {
$userfields = \core_user\fields::for_name();
return $userfields->get_sql($tablealias, false, '', '', false)->selects;
} else {
// Legacy support for 3.9.
return get_all_user_name_fields(true, $tablealias);
}
}
}
2 changes: 1 addition & 1 deletion reset_bounces.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

if ($confirm && confirm_sesskey()) {
list($in, $params) = $DB->get_in_or_equal($SESSION->bulk_users);
$rs = $DB->get_recordset_select('user', "id $in", $params, '', 'id, ' . get_all_user_name_fields(true));
$rs = $DB->get_recordset_select('user', "id $in", $params, '', 'id, ' . \tool_emailutils\helper::get_username_fields());
foreach ($rs as $user) {
// Reset the user bounce count.
set_bounce_count($user, true);
Expand Down

0 comments on commit 9addf95

Please sign in to comment.