Skip to content

Commit

Permalink
MDL-66032 badges: Prevent empty awarded badges criteria being created.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
andrewmadden committed Jul 15, 2019
1 parent f7e1084 commit 7599223
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions badges/criteria/award_criteria_badge.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public function get_options(&$mform) {
if ($this->id !== 0) {
$selected = array_keys($this->params);
}
$settings = array('multiple' => 'multiple', 'size' => 20, 'class' => 'selectbadge');
$settings = array('multiple' => 'multiple', 'size' => 20, 'class' => 'selectbadge', 'required' => 'required');
$mform->addElement('select', 'badge_badges', get_string('addbadge', 'badges'), $select, $settings);
$mform->addRule('badge_badges', get_string('requiredbadge', 'badges'), 'required');
$mform->addHelpButton('badge_badges', 'addbadge', 'badges');
Expand Down Expand Up @@ -243,7 +243,6 @@ public function get_completed_criteria_sql() {
if ($this->method == BADGE_CRITERIA_AGGREGATION_ANY) {
// User has received ANY of the required badges.
$join = " LEFT JOIN {badge_issued} bi2 ON bi2.userid = u.id";
$where = "AND (";
$i = 0;
foreach ($this->params as $param) {
if ($i == 0) {
Expand All @@ -254,7 +253,10 @@ public function get_completed_criteria_sql() {
$params['badgeid'.$i] = $param['badge'];
$i++;
}
$where .= ") ";
// MDL-66032 Do not create expression if there are no badges in criteria.
if (!empty($where)) {
$where = ' AND (' . $where . ') ';
}
return array($join, $where, $params);
} else {
// User has received ALL of the required badges.
Expand Down

0 comments on commit 7599223

Please sign in to comment.