MDL-63724 messaging: Prefix field with the table alias
[moodle.git] / badges / criteria / award_criteria_manual.php
blobff1e613d6d98a6d7d8829dfa9802061d64e4b277
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
18 * This file contains the manual badge award criteria type class
20 * @package core
21 * @subpackage badges
22 * @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
27 defined('MOODLE_INTERNAL') || die();
29 /**
30 * Manual badge award criteria
33 class award_criteria_manual extends award_criteria {
35 /* @var int Criteria [BADGE_CRITERIA_TYPE_MANUAL] */
36 public $criteriatype = BADGE_CRITERIA_TYPE_MANUAL;
38 public $required_param = 'role';
39 public $optional_params = array();
41 /**
42 * Gets role name.
43 * If no such role exists this function returns null.
45 * @return string|null
47 private function get_role_name($rid) {
48 global $DB, $PAGE;
49 $rec = $DB->get_record('role', array('id' => $rid));
51 if ($rec) {
52 return role_get_name($rec, $PAGE->context, ROLENAME_ALIAS);
53 } else {
54 return null;
58 /**
59 * Add appropriate new criteria options to the form
62 public function get_options(&$mform) {
63 global $PAGE;
64 $options = '';
65 $none = true;
67 $roles = get_roles_with_capability('moodle/badges:awardbadge', CAP_ALLOW, $PAGE->context);
68 $visibleroles = get_viewable_roles($PAGE->context);
69 $roleids = array_map(function($o) {
70 return $o->id;
71 }, $roles);
72 $existing = array();
73 $missing = array();
75 if ($this->id !== 0) {
76 $existing = array_keys($this->params);
77 $missing = array_diff($existing, $roleids);
80 if (!empty($missing)) {
81 $mform->addElement('header', 'category_errors', get_string('criterror', 'badges'));
82 $mform->addHelpButton('category_errors', 'criterror', 'badges');
83 foreach ($missing as $m) {
84 $this->config_options($mform, array('id' => $m, 'checked' => true, 'name' => get_string('error:nosuchrole', 'badges'), 'error' => true));
85 $none = false;
89 if (!empty($roleids)) {
90 $mform->addElement('header', 'first_header', $this->get_title());
91 $mform->addHelpButton('first_header', 'criteria_' . $this->criteriatype, 'badges');
92 foreach ($roleids as $rid) {
93 if (!key_exists($rid, $visibleroles)) {
94 continue;
96 $checked = false;
97 if (in_array($rid, $existing)) {
98 $checked = true;
100 $this->config_options($mform, array('id' => $rid, 'checked' => $checked, 'name' => self::get_role_name($rid), 'error' => false));
101 $none = false;
105 // Add aggregation.
106 if (!$none) {
107 $mform->addElement('header', 'aggregation', get_string('method', 'badges'));
108 $agg = array();
109 $agg[] =& $mform->createElement('radio', 'agg', '', get_string('allmethodmanual', 'badges'), 1);
110 $agg[] =& $mform->createElement('static', 'none_break', null, '<br/>');
111 $agg[] =& $mform->createElement('radio', 'agg', '', get_string('anymethodmanual', 'badges'), 2);
112 $mform->addGroup($agg, 'methodgr', '', array(' '), false);
113 if ($this->id !== 0) {
114 $mform->setDefault('agg', $this->method);
115 } else {
116 $mform->setDefault('agg', BADGE_CRITERIA_AGGREGATION_ANY);
120 return array($none, get_string('noparamstoadd', 'badges'));
124 * Get criteria details for displaying to users
126 * @return string
128 public function get_details($short = '') {
129 global $OUTPUT;
130 $output = array();
131 foreach ($this->params as $p) {
132 $str = self::get_role_name($p['role']);
133 if (!$str) {
134 $output[] = $OUTPUT->error_text(get_string('error:nosuchrole', 'badges'));
135 } else {
136 $output[] = $str;
140 if ($short) {
141 return implode(', ', $output);
142 } else {
143 return html_writer::alist($output, array(), 'ul');
148 * Review this criteria and decide if it has been completed
150 * @param int $userid User whose criteria completion needs to be reviewed.
151 * @param bool $filtered An additional parameter indicating that user list
152 * has been reduced and some expensive checks can be skipped.
154 * @return bool Whether criteria is complete
156 public function review($userid, $filtered = false) {
157 global $DB;
159 // Roles should always have a parameter.
160 if (empty($this->params)) {
161 return false;
164 // Users were already filtered by criteria completion.
165 if ($filtered) {
166 return true;
169 $overall = false;
170 foreach ($this->params as $param) {
171 $crit = $DB->get_record('badge_manual_award', array('issuerrole' => $param['role'], 'recipientid' => $userid, 'badgeid' => $this->badgeid));
172 if ($this->method == BADGE_CRITERIA_AGGREGATION_ALL) {
173 if (!$crit) {
174 return false;
175 } else {
176 $overall = true;
177 continue;
179 } else {
180 if (!$crit) {
181 $overall = false;
182 continue;
183 } else {
184 return true;
188 return $overall;
192 * Returns array with sql code and parameters returning all ids
193 * of users who meet this particular criterion.
195 * @return array list($join, $where, $params)
197 public function get_completed_criteria_sql() {
198 $join = '';
199 $where = '';
200 $params = array();
202 if ($this->method == BADGE_CRITERIA_AGGREGATION_ANY) {
203 foreach ($this->params as $param) {
204 $roledata[] = " bma.issuerrole = :issuerrole{$param['role']} ";
205 $params["issuerrole{$param['role']}"] = $param['role'];
207 if (!empty($roledata)) {
208 $extraon = implode(' OR ', $roledata);
209 $join = " JOIN {badge_manual_award} bma ON bma.recipientid = u.id
210 AND bma.badgeid = :badgeid{$this->badgeid} AND ({$extraon})";
211 $params["badgeid{$this->badgeid}"] = $this->badgeid;
213 return array($join, $where, $params);
214 } else {
215 foreach ($this->params as $param) {
216 $roledata[] = " bma.issuerrole = :issuerrole{$param['role']} ";
217 $params["issuerrole{$param['role']}"] = $param['role'];
219 if (!empty($roledata)) {
220 $extraon = implode(' AND ', $roledata);
221 $join = " JOIN {badge_manual_award} bma ON bma.recipientid = u.id
222 AND bma.badgeid = :badgeid{$this->badgeid} AND ({$extraon})";
223 $params["badgeid{$this->badgeid}"] = $this->badgeid;
225 return array($join, $where, $params);
230 * Delete this criterion
233 public function delete() {
234 global $DB;
236 // Remove any records of manual award.
237 $DB->delete_records('badge_manual_award', array('badgeid' => $this->badgeid));
239 parent::delete();