From 6b670477f7832ef4febfd8e89982f67ec22d72c8 Mon Sep 17 00:00:00 2001 From: Michael Hawkins Date: Tue, 8 Mar 2022 12:11:23 +0800 Subject: [PATCH] MDL-74074 badges: Ensure profile criteria exists before completion check --- badges/criteria/award_criteria_profile.php | 35 ++++++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/badges/criteria/award_criteria_profile.php b/badges/criteria/award_criteria_profile.php index da76bc87474..cf03a10ea29 100644 --- a/badges/criteria/award_criteria_profile.php +++ b/badges/criteria/award_criteria_profile.php @@ -39,6 +39,32 @@ class award_criteria_profile extends award_criteria { public $required_param = 'field'; public $optional_params = array(); + /* @var array The default profile fields allowed to be used as award criteria. + * + * Note: This is used instead of user_get_default_fields(), because it is not possible to + * determine which fields the user can modify. + */ + protected $allowed_default_fields = [ + 'firstname', + 'lastname', + 'email', + 'address', + 'phone1', + 'phone2', + 'icq', + 'skype', + 'yahoo', + 'aim', + 'msn', + 'department', + 'institution', + 'description', + 'picture', + 'city', + 'url', + 'country', + ]; + /** * Add appropriate new criteria options to the form * @@ -49,10 +75,7 @@ class award_criteria_profile extends award_criteria { $none = true; $existing = array(); $missing = array(); - - // Note: cannot use user_get_default_fields() here because it is not possible to decide which fields user can modify. - $dfields = array('firstname', 'lastname', 'email', 'address', 'phone1', 'phone2', 'icq', 'skype', 'yahoo', - 'aim', 'msn', 'department', 'institution', 'description', 'picture', 'city', 'url', 'country'); + $dfields = $this->allowed_default_fields; $sql = "SELECT uf.id as fieldid, uf.name as name, ic.id as categoryid, ic.name as categoryname, uf.datatype FROM {user_info_field} uf @@ -228,8 +251,8 @@ class award_criteria_profile extends award_criteria { $join .= " LEFT JOIN {user_info_data} uid{$idx} ON uid{$idx}.userid = u.id AND uid{$idx}.fieldid = :fieldid{$idx} "; $params["fieldid{$idx}"] = $param['field']; $whereparts[] = "uid{$idx}.id IS NOT NULL"; - } else { - // This is a field from {user} table. + } else if (in_array($param['field'], $this->allowed_default_fields)) { + // This is a valid field from {user} table. if ($param['field'] == 'picture') { // The picture field is numeric and requires special handling. $whereparts[] = "u.{$param['field']} != 0";