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.
This commit is contained in:
Andrew Madden 2019-07-04 11:49:46 +10:00
parent f7e108438f
commit 7599223a1c

View File

@ -112,7 +112,7 @@ class award_criteria_badge extends award_criteria {
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');
@ -243,7 +243,6 @@ class award_criteria_badge extends award_criteria {
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) {
@ -254,7 +253,10 @@ class award_criteria_badge extends award_criteria {
$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.