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;
58 * @param array $options
60 public function __construct($name, array $options) {
61 $options['accesscontext'] = $options['context'];
62 parent
::__construct($name, $options);
63 if (isset($options['context'])) {
64 if ($options['context'] instanceof context_system
) {
65 // If it is a site badge, we need to get context of frontpage.
66 $this->context
= context_course
::instance(SITEID
);
68 $this->context
= $options['context'];
71 if (isset($options['badgeid'])) {
72 $this->badgeid
= $options['badgeid'];
74 if (isset($options['issuerid'])) {
75 $this->issuerid
= $options['issuerid'];
77 if (isset($options['issuerrole'])) {
78 $this->issuerrole
= $options['issuerrole'];
83 * Returns an array of options to seralise and store for searches
87 protected function get_options() {
89 $options = parent
::get_options();
90 $options['file'] = 'badges/lib/awardlib.php';
91 $options['context'] = $this->context
;
92 $options['badgeid'] = $this->badgeid
;
93 $options['issuerid'] = $this->issuerid
;
94 $options['issuerrole'] = $this->issuerrole
;
100 * A user selector control for potential users to award badge
102 class badge_potential_users_selector
extends badge_award_selector_base
{
103 const MAX_USERS_PER_PAGE
= 100;
106 * Existing recipients
108 protected $existingrecipients = array();
111 * Finds all potential badge recipients
113 * Potential badge recipients are all enroled users
114 * who haven't got a badge from current issuer role.
116 * @param string $search
119 public function find_users($search) {
122 $whereconditions = array();
123 list($wherecondition, $params) = $this->search_sql($search, 'u');
124 if ($wherecondition) {
125 $whereconditions[] = $wherecondition;
128 $existingids = array();
129 foreach ($this->existingrecipients
as $group) {
130 foreach ($group as $user) {
131 $existingids[] = $user->id
;
135 list($usertest, $userparams) = $DB->get_in_or_equal($existingids, SQL_PARAMS_NAMED
, 'ex', false);
136 $whereconditions[] = 'u.id ' . $usertest;
137 $params = array_merge($params, $userparams);
140 if ($whereconditions) {
141 $wherecondition = ' WHERE ' . implode(' AND ', $whereconditions);
144 list($esql, $eparams) = get_enrolled_sql($this->context
, 'moodle/badges:earnbadge', 0, true);
145 $params = array_merge($params, $eparams);
147 $fields = 'SELECT ' . $this->required_fields_sql('u');
148 $countfields = 'SELECT COUNT(u.id)';
150 $params['badgeid'] = $this->badgeid
;
151 $params['issuerrole'] = $this->issuerrole
;
153 $sql = " FROM {user} u JOIN ($esql) je ON je.id = u.id
154 LEFT JOIN {badge_manual_award} bm
155 ON (bm.recipientid = u.id AND bm.badgeid = :badgeid AND bm.issuerrole = :issuerrole)
156 $wherecondition AND bm.id IS NULL";
158 list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext
);
159 $order = ' ORDER BY ' . $sort;
161 if (!$this->is_validating()) {
162 $potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
163 if ($potentialmemberscount > self
::MAX_USERS_PER_PAGE
) {
164 return $this->too_many_results($search, $potentialmemberscount);
168 $availableusers = $DB->get_records_sql($fields . $sql . $order, array_merge($params, $sortparams));
170 if (empty($availableusers)) {
174 return array(get_string('potentialrecipients', 'badges') => $availableusers);
178 * Sets the existing recipients
179 * @param array $users
181 public function set_existing_recipients(array $users) {
182 $this->existingrecipients
= $users;
187 * A user selector control for existing users to award badge
189 class badge_existing_users_selector
extends badge_award_selector_base
{
192 * Finds all users who already have been awarded a badge by current role
194 * @param string $search
197 public function find_users($search) {
199 list($wherecondition, $params) = $this->search_sql($search, 'u');
200 $params['badgeid'] = $this->badgeid
;
201 $params['issuerrole'] = $this->issuerrole
;
203 list($esql, $eparams) = get_enrolled_sql($this->context
, 'moodle/badges:earnbadge', 0, true);
204 $fields = $this->required_fields_sql('u');
205 list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext
);
207 $params = array_merge($params, $eparams, $sortparams);
208 $recipients = $DB->get_records_sql("SELECT $fields
210 JOIN ($esql) je ON je.id = u.id
211 JOIN {badge_manual_award} s ON s.recipientid = u.id
212 WHERE $wherecondition AND s.badgeid = :badgeid AND s.issuerrole = :issuerrole
213 ORDER BY $sort", $params);
215 return array(get_string('existingrecipients', 'badges') => $recipients);
219 function process_manual_award($recipientid, $issuerid, $issuerrole, $badgeid) {
222 'badgeid' => $badgeid,
223 'issuerid' => $issuerid,
224 'issuerrole' => $issuerrole,
225 'recipientid' => $recipientid
228 if (!$DB->record_exists('badge_manual_award', $params)) {
229 $award = new stdClass();
230 $award->badgeid
= $badgeid;
231 $award->issuerid
= $issuerid;
232 $award->issuerrole
= $issuerrole;
233 $award->recipientid
= $recipientid;
234 $award->datemet
= time();
235 if ($DB->insert_record('badge_manual_award', $award)) {