MDL-56439 course: added missing require
[moodle.git] / badges / criteria / award_criteria_manual.php
blob51a7959d0add570e35a92fe668aedaaaee1580e4
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 $roleids = array_map(create_function('$o', 'return $o->id;'), $roles);
69 $existing = array();
70 $missing = array();
72 if ($this->id !== 0) {
73 $existing = array_keys($this->params);
74 $missing = array_diff($existing, $roleids);
77 if (!empty($missing)) {
78 $mform->addElement('header', 'category_errors', get_string('criterror', 'badges'));
79 $mform->addHelpButton('category_errors', 'criterror', 'badges');
80 foreach ($missing as $m) {
81 $this->config_options($mform, array('id' => $m, 'checked' => true, 'name' => get_string('error:nosuchrole', 'badges'), 'error' => true));
82 $none = false;
86 if (!empty($roleids)) {
87 $mform->addElement('header', 'first_header', $this->get_title());
88 $mform->addHelpButton('first_header', 'criteria_' . $this->criteriatype, 'badges');
89 foreach ($roleids as $rid) {
90 $checked = false;
91 if (in_array($rid, $existing)) {
92 $checked = true;
94 $this->config_options($mform, array('id' => $rid, 'checked' => $checked, 'name' => self::get_role_name($rid), 'error' => false));
95 $none = false;
99 // Add aggregation.
100 if (!$none) {
101 $mform->addElement('header', 'aggregation', get_string('method', 'badges'));
102 $agg = array();
103 $agg[] =& $mform->createElement('radio', 'agg', '', get_string('allmethodmanual', 'badges'), 1);
104 $agg[] =& $mform->createElement('static', 'none_break', null, '<br/>');
105 $agg[] =& $mform->createElement('radio', 'agg', '', get_string('anymethodmanual', 'badges'), 2);
106 $mform->addGroup($agg, 'methodgr', '', array(' '), false);
107 if ($this->id !== 0) {
108 $mform->setDefault('agg', $this->method);
109 } else {
110 $mform->setDefault('agg', BADGE_CRITERIA_AGGREGATION_ANY);
114 return array($none, get_string('noparamstoadd', 'badges'));
118 * Get criteria details for displaying to users
120 * @return string
122 public function get_details($short = '') {
123 global $OUTPUT;
124 $output = array();
125 foreach ($this->params as $p) {
126 $str = self::get_role_name($p['role']);
127 if (!$str) {
128 $output[] = $OUTPUT->error_text(get_string('error:nosuchrole', 'badges'));
129 } else {
130 $output[] = $str;
134 if ($short) {
135 return implode(', ', $output);
136 } else {
137 return html_writer::alist($output, array(), 'ul');
142 * Review this criteria and decide if it has been completed
144 * @param int $userid User whose criteria completion needs to be reviewed.
145 * @param bool $filtered An additional parameter indicating that user list
146 * has been reduced and some expensive checks can be skipped.
148 * @return bool Whether criteria is complete
150 public function review($userid, $filtered = false) {
151 global $DB;
153 // Roles should always have a parameter.
154 if (empty($this->params)) {
155 return false;
158 // Users were already filtered by criteria completion.
159 if ($filtered) {
160 return true;
163 $overall = false;
164 foreach ($this->params as $param) {
165 $crit = $DB->get_record('badge_manual_award', array('issuerrole' => $param['role'], 'recipientid' => $userid, 'badgeid' => $this->badgeid));
166 if ($this->method == BADGE_CRITERIA_AGGREGATION_ALL) {
167 if (!$crit) {
168 return false;
169 } else {
170 $overall = true;
171 continue;
173 } else {
174 if (!$crit) {
175 $overall = false;
176 continue;
177 } else {
178 return true;
182 return $overall;
186 * Returns array with sql code and parameters returning all ids
187 * of users who meet this particular criterion.
189 * @return array list($join, $where, $params)
191 public function get_completed_criteria_sql() {
192 $join = '';
193 $where = '';
194 $params = array();
196 if ($this->method == BADGE_CRITERIA_AGGREGATION_ANY) {
197 foreach ($this->params as $param) {
198 $roledata[] = " bma.issuerrole = :issuerrole{$param['role']} ";
199 $params["issuerrole{$param['role']}"] = $param['role'];
201 if (!empty($roledata)) {
202 $extraon = implode(' OR ', $roledata);
203 $join = " JOIN {badge_manual_award} bma ON bma.recipientid = u.id
204 AND bma.badgeid = :badgeid{$this->badgeid} AND ({$extraon})";
205 $params["badgeid{$this->badgeid}"] = $this->badgeid;
207 return array($join, $where, $params);
208 } else {
209 foreach ($this->params as $param) {
210 $roledata[] = " bma.issuerrole = :issuerrole{$param['role']} ";
211 $params["issuerrole{$param['role']}"] = $param['role'];
213 if (!empty($roledata)) {
214 $extraon = implode(' AND ', $roledata);
215 $join = " JOIN {badge_manual_award} bma ON bma.recipientid = u.id
216 AND bma.badgeid = :badgeid{$this->badgeid} AND ({$extraon})";
217 $params["badgeid{$this->badgeid}"] = $this->badgeid;
219 return array($join, $where, $params);
224 * Delete this criterion
227 public function delete() {
228 global $DB;
230 // Remove any records of manual award.
231 $DB->delete_records('badge_manual_award', array('badgeid' => $this->badgeid));
233 parent::delete();