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 * Classes to manage manual badge award.
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 require_once($CFG->libdir
. '/badgeslib.php');
30 require_once($CFG->dirroot
. '/user/selector/lib.php');
32 abstract class badge_award_selector_base
extends user_selector_base
{
35 * The id of the badge this selector is being used for
38 protected $badgeid = null;
40 * The context of the badge this selector is being used for
43 protected $context = null;
45 * The id of the role of badge issuer in current context
48 protected $issuerrole = null;
50 * The id of badge issuer
53 protected $issuerid = null;
56 * The return address. Accepts either a string or a moodle_url.
62 * The current group being displayed.
63 * @var int $currentgroup
70 * @param array $options
72 public function __construct($name, array $options) {
73 $options['accesscontext'] = $options['context'];
74 parent
::__construct($name, $options);
75 if (isset($options['context'])) {
76 if ($options['context'] instanceof context_system
) {
77 // If it is a site badge, we need to get context of frontpage.
78 $this->context
= context_course
::instance(SITEID
);
80 $this->context
= $options['context'];
83 if (isset($options['badgeid'])) {
84 $this->badgeid
= $options['badgeid'];
86 if (isset($options['issuerid'])) {
87 $this->issuerid
= $options['issuerid'];
89 if (isset($options['issuerrole'])) {
90 $this->issuerrole
= $options['issuerrole'];
92 if (isset($options['url'])) {
93 $this->url
= $options['url'];
95 if (isset($options['currentgroup'])) {
96 $this->currentgroup
= $options['currentgroup'];
98 // Returns group active in course, changes the group by default if 'group' page param present.
99 $this->currentgroup
= groups_get_course_group($COURSE, true);
104 * Returns an array of options to seralise and store for searches
108 protected function get_options() {
110 $options = parent
::get_options();
111 $options['file'] = 'badges/lib/awardlib.php';
112 $options['context'] = $this->context
;
113 $options['badgeid'] = $this->badgeid
;
114 $options['issuerid'] = $this->issuerid
;
115 $options['issuerrole'] = $this->issuerrole
;
116 // These will be used to filter potential badge recipients when searching.
117 $options['currentgroup'] = $this->currentgroup
;
122 * Restricts the selection of users to display, according to the groups they belong.
126 protected function get_groups_sql() {
129 $groupwheresqlparams = array();
130 if ($this->currentgroup
) {
131 $groupsql = ' JOIN {groups_members} gm ON gm.userid = u.id ';
132 $groupwheresql = ' AND gm.groupid = :gr_grpid ';
133 $groupwheresqlparams = array('gr_grpid' => $this->currentgroup
);
135 return array($groupsql, $groupwheresql, $groupwheresqlparams);
140 * A user selector control for potential users to award badge
142 class badge_potential_users_selector
extends badge_award_selector_base
{
143 const MAX_USERS_PER_PAGE
= 100;
146 * Existing recipients
148 protected $existingrecipients = array();
151 * Finds all potential badge recipients
153 * Potential badge recipients are all enroled users
154 * who haven't got a badge from current issuer role.
156 * @param string $search
159 public function find_users($search) {
162 $whereconditions = array();
163 list($wherecondition, $params) = $this->search_sql($search, 'u');
164 if ($wherecondition) {
165 $whereconditions[] = $wherecondition;
168 $existingids = array();
169 foreach ($this->existingrecipients
as $group) {
170 foreach ($group as $user) {
171 $existingids[] = $user->id
;
175 list($usertest, $userparams) = $DB->get_in_or_equal($existingids, SQL_PARAMS_NAMED
, 'ex', false);
176 $whereconditions[] = 'u.id ' . $usertest;
177 $params = array_merge($params, $userparams);
180 if ($whereconditions) {
181 $wherecondition = ' WHERE ' . implode(' AND ', $whereconditions);
184 list($groupsql, $groupwheresql, $groupwheresqlparams) = $this->get_groups_sql();
186 list($esql, $eparams) = get_enrolled_sql($this->context
, 'moodle/badges:earnbadge', 0, true);
187 $params = array_merge($params, $eparams, $groupwheresqlparams);
189 $fields = 'SELECT ' . $this->required_fields_sql('u');
190 $countfields = 'SELECT COUNT(u.id)';
192 $params['badgeid'] = $this->badgeid
;
193 $params['issuerrole'] = $this->issuerrole
;
195 $sql = " FROM {user} u JOIN ($esql) je ON je.id = u.id
196 LEFT JOIN {badge_manual_award} bm
197 ON (bm.recipientid = u.id AND bm.badgeid = :badgeid AND bm.issuerrole = :issuerrole)
199 $wherecondition AND bm.id IS NULL
202 list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext
);
203 $order = ' ORDER BY ' . $sort;
205 if (!$this->is_validating()) {
206 $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
207 if ($potentialmemberscount > self
::MAX_USERS_PER_PAGE
) {
208 return $this->too_many_results($search, $potentialmemberscount);
212 $availableusers = $DB->get_records_sql($fields . $sql . $order, array_merge($params, $sortparams));
214 if (empty($availableusers)) {
218 return array(get_string('potentialrecipients', 'badges') => $availableusers);
222 * Sets the existing recipients
223 * @param array $users
225 public function set_existing_recipients(array $users) {
226 $this->existingrecipients
= $users;
231 * A user selector control for existing users to award badge
233 class badge_existing_users_selector
extends badge_award_selector_base
{
236 * Finds all users who already have been awarded a badge by current role
238 * @param string $search
241 public function find_users($search) {
243 list($wherecondition, $params) = $this->search_sql($search, 'u');
244 $params['badgeid'] = $this->badgeid
;
245 $params['issuerrole'] = $this->issuerrole
;
247 list($esql, $eparams) = get_enrolled_sql($this->context
, 'moodle/badges:earnbadge', 0, true);
248 $fields = $this->required_fields_sql('u');
249 list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext
);
251 list($groupsql, $groupwheresql, $groupwheresqlparams) = $this->get_groups_sql();
253 $params = array_merge($params, $eparams, $sortparams, $groupwheresqlparams);
254 $recipients = $DB->get_records_sql("SELECT $fields
256 JOIN ($esql) je ON je.id = u.id
257 JOIN {badge_manual_award} s ON s.recipientid = u.id
259 WHERE $wherecondition AND s.badgeid = :badgeid AND s.issuerrole = :issuerrole
261 ORDER BY $sort", $params);
263 return array(get_string('existingrecipients', 'badges') => $recipients);
267 function process_manual_award($recipientid, $issuerid, $issuerrole, $badgeid) {
270 'badgeid' => $badgeid,
271 'issuerid' => $issuerid,
272 'issuerrole' => $issuerrole,
273 'recipientid' => $recipientid
276 if (!$DB->record_exists('badge_manual_award', $params)) {
277 $award = new stdClass();
278 $award->badgeid
= $badgeid;
279 $award->issuerid
= $issuerid;
280 $award->issuerrole
= $issuerrole;
281 $award->recipientid
= $recipientid;
282 $award->datemet
= time();
283 if ($DB->insert_record('badge_manual_award', $award)) {
291 * Manually revoke awarded badges.
293 * @param int $recipientid
294 * @param int $issuerid
295 * @param int $issuerrole
296 * @param int $badgeid
299 function process_manual_revoke($recipientid, $issuerid, $issuerrole, $badgeid) {
302 'badgeid' => $badgeid,
303 'issuerid' => $issuerid,
304 'issuerrole' => $issuerrole,
305 'recipientid' => $recipientid
307 if ($DB->record_exists('badge_manual_award', $params)) {
308 if ($DB->delete_records('badge_manual_award', array('badgeid' => $badgeid,
309 'issuerid' => $issuerid,
310 'recipientid' => $recipientid))
311 && $DB->delete_records('badge_issued', array('badgeid' => $badgeid,
312 'userid' => $recipientid))) {
314 // Trigger event, badge revoked.
315 $badge = new \badge
($badgeid);
316 $eventparams = array(
317 'objectid' => $badgeid,
318 'relateduserid' => $recipientid,
319 'context' => $badge->get_context()
321 $event = \core\event\badge_revoked
::create($eventparams);
327 throw new moodle_exception('error:badgenotfound', 'badges');