2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * This file contains the badge earned badge award criteria type class
22 * @copyright 2017 Stephen Bourget
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') ||
die();
29 * Badge award criteria -- award on badge completion
33 * @copyright 2017 Stephen Bourget
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class award_criteria_badge
extends award_criteria
{
38 /* @var int Criteria [BADGE_CRITERIA_TYPE_BADGE] */
39 public $criteriatype = BADGE_CRITERIA_TYPE_BADGE
;
41 public $required_param = 'badge';
42 public $optional_params = array();
45 * Get criteria details for displaying to users
46 * @param string $short Print short version of criteria
49 public function get_details($short = '') {
52 foreach ($this->params
as $p) {
53 $badgename = $DB->get_field('badge', 'name', array('id' => $p['badge']));
55 $str = $OUTPUT->error_text(get_string('error:nosuchbadge', 'badges'));
57 $str = html_writer
::tag('b', '"' . $badgename . '"');
63 return implode(', ', $output);
65 return html_writer
::alist($output, array(), 'ul');
70 * Add appropriate new criteria options to the form
71 * @param object $mform moodle form
73 public function get_options(&$mform) {
76 $availablebadges = null;
78 $mform->addElement('header', 'first_header', $this->get_title());
79 $mform->addHelpButton('first_header', 'criteria_' . $this->criteriatype
, 'badges');
81 // Determine if this badge is a course badge or a site badge.
82 $thisbadge = $DB->get_record('badge', array('id' => $this->badgeid
));
84 if ($thisbadge->type
== BADGE_TYPE_SITE
) {
85 // Only list site badges that are enabled.
86 $select = " type = :site AND (status = :status1 OR status = :status2)";
87 $params = array('site' => BADGE_TYPE_SITE
,
88 'status1' => BADGE_STATUS_ACTIVE
,
89 'status2' => BADGE_STATUS_ACTIVE_LOCKED
);
90 $availablebadges = $DB->get_records_select_menu('badge', $select, $params, 'name ASC', 'id, name');
92 } else if ($thisbadge->type
== BADGE_TYPE_COURSE
) {
93 // List both site badges and course badges belonging to this course.
94 $select = " (type = :site OR (type = :course AND courseid = :courseid)) AND (status = :status1 OR status = :status2)";
95 $params = array('site' => BADGE_TYPE_SITE
,
96 'course' => BADGE_TYPE_COURSE
,
97 'courseid' => $thisbadge->courseid
,
98 'status1' => BADGE_STATUS_ACTIVE
,
99 'status2' => BADGE_STATUS_ACTIVE_LOCKED
);
100 $availablebadges = $DB->get_records_select_menu('badge', $select, $params, 'name ASC', 'id, name');
102 if (!empty($availablebadges)) {
105 foreach ($availablebadges as $bid => $badgename) {
106 if ($bid != $this->badgeid
) {
107 // Do not let it use itself as criteria.
108 $select[$bid] = format_string($badgename, true);
112 if ($this->id
!== 0) {
113 $selected = array_keys($this->params
);
115 $settings = array('multiple' => 'multiple', 'size' => 20, 'class' => 'selectbadge', 'required' => 'required');
116 $mform->addElement('select', 'badge_badges', get_string('addbadge', 'badges'), $select, $settings);
117 $mform->addRule('badge_badges', get_string('requiredbadge', 'badges'), 'required');
118 $mform->addHelpButton('badge_badges', 'addbadge', 'badges');
120 if ($this->id
!== 0) {
121 $mform->setDefault('badge_badges', $selected);
124 $mform->addElement('static', 'nobadges', '', get_string('error:nobadges', 'badges'));
130 $mform->addElement('header', 'aggregation', get_string('method', 'badges'));
132 $agg[] =& $mform->createElement('radio', 'agg', '', get_string('allmethodbadges', 'badges'), 1);
133 $agg[] =& $mform->createElement('radio', 'agg', '', get_string('anymethodbadges', 'badges'), 2);
134 $mform->addGroup($agg, 'methodgr', '', array('<br/>'), false);
135 if ($this->id
!== 0) {
136 $mform->setDefault('agg', $this->method
);
138 $mform->setDefault('agg', BADGE_CRITERIA_AGGREGATION_ANY
);
142 return array($none, get_string('noparamstoadd', 'badges'));
146 * Save criteria records
148 * @param array $params Values from the form or any other array.
150 public function save($params = array()) {
151 $badges = $params['badge_badges'];
152 unset($params['badge_badges']);
153 foreach ($badges as $badgeid) {
154 $params["badge_{$badgeid}"] = $badgeid;
157 parent
::save($params);
161 * Review this criteria and decide if it has been completed
163 * @param int $userid User whose criteria completion needs to be reviewed.
164 * @param bool $filtered An additional parameter indicating that user list
165 * has been reduced and some expensive checks can be skipped.
167 * @return bool Whether criteria is complete.
169 public function review($userid, $filtered = false) {
174 foreach ($this->params
as $param) {
175 $badge = $DB->get_record('badge', array('id' => $param['badge']));
176 // See if the user has earned this badge.
177 $awarded = $DB->get_record('badge_issued', array('badgeid' => $param['badge'], 'userid' => $userid));
179 // Extra check in case a badge was deleted while this badge is still active.
181 if ($this->method
== BADGE_CRITERIA_AGGREGATION_ALL
) {
188 if ($this->method
== BADGE_CRITERIA_AGGREGATION_ALL
) {
196 } else if ($this->method
== BADGE_CRITERIA_AGGREGATION_ANY
) {
210 * Checks criteria for any major problems.
212 * @return array A list containing status and an error message (if any).
214 public function validate() {
216 $params = array_keys($this->params
);
217 $method = ($this->method
== BADGE_CRITERIA_AGGREGATION_ALL
);
218 $singleparam = (count($params) == 1);
220 foreach ($params as $param) {
221 // Perform check if there only one parameter with any type of aggregation,
222 // Or there are more than one parameter with aggregation ALL.
224 if (($singleparam ||
$method) && !$DB->record_exists('badge', array('id' => $param))) {
225 return array(false, get_string('error:invalidparambadge', 'badges'));
229 return array(true, '');
233 * Returns array with sql code and parameters returning all ids
234 * of users who meet this particular criterion.
236 * @return array list($join, $where, $params)
238 public function get_completed_criteria_sql() {
243 if ($this->method
== BADGE_CRITERIA_AGGREGATION_ANY
) {
244 // User has received ANY of the required badges.
245 $join = " LEFT JOIN {badge_issued} bi2 ON bi2.userid = u.id";
247 foreach ($this->params
as $param) {
249 $where .= ' bi2.badgeid = :badgeid'.$i;
251 $where .= ' OR bi2.badgeid = :badgeid'.$i;
253 $params['badgeid'.$i] = $param['badge'];
256 // MDL-66032 Do not create expression if there are no badges in criteria.
257 if (!empty($where)) {
258 $where = ' AND (' . $where . ') ';
260 return array($join, $where, $params);
262 // User has received ALL of the required badges.
263 $join = " LEFT JOIN {badge_issued} bi2 ON bi2.userid = u.id";
265 foreach ($this->params
as $param) {
267 $where = ' AND bi2.badgeid = :badgeid'.$i;
268 $params['badgeid'.$i] = $param['badge'];
270 return array($join, $where, $params);