Skip to content

Commit 191bcca

Browse files
committed
MDL-66032 badges: Prevent empty awarded badges criteria being created.
When creating or editing the awarded badges criteria, the select element should be required to prevent empty criteria from being created. In addition, if there are already badges created with the empty criteria, there should be not be an exception thrown when assessing whether the criteria has been completed.
1 parent 39ef515 commit 191bcca

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

badges/criteria/award_criteria_badge.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public function get_options(&$mform) {
112112
if ($this->id !== 0) {
113113
$selected = array_keys($this->params);
114114
}
115-
$settings = array('multiple' => 'multiple', 'size' => 20, 'class' => 'selectbadge');
115+
$settings = array('multiple' => 'multiple', 'size' => 20, 'class' => 'selectbadge', 'required' => 'required');
116116
$mform->addElement('select', 'badge_badges', get_string('addbadge', 'badges'), $select, $settings);
117117
$mform->addRule('badge_badges', get_string('requiredbadge', 'badges'), 'required');
118118
$mform->addHelpButton('badge_badges', 'addbadge', 'badges');
@@ -243,7 +243,6 @@ public function get_completed_criteria_sql() {
243243
if ($this->method == BADGE_CRITERIA_AGGREGATION_ANY) {
244244
// User has received ANY of the required badges.
245245
$join = " LEFT JOIN {badge_issued} bi2 ON bi2.userid = u.id";
246-
$where = "AND (";
247246
$i = 0;
248247
foreach ($this->params as $param) {
249248
if ($i == 0) {
@@ -254,7 +253,10 @@ public function get_completed_criteria_sql() {
254253
$params['badgeid'.$i] = $param['badge'];
255254
$i++;
256255
}
257-
$where .= ") ";
256+
// MDL-66032 Do not create expression if there are no badges in criteria.
257+
if (!empty($where)) {
258+
$where = ' AND (' . $where . ') ';
259+
}
258260
return array($join, $where, $params);
259261
} else {
260262
// User has received ALL of the required badges.

0 commit comments

Comments
 (0)