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 cohort membership badge award criteria type class
22 * @copyright 2016 onwards Catalyst IT {@link https://www.catalyst.net.nz/}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @author Eugene Venter <eugene@catalyst.net.nz>
27 defined('MOODLE_INTERNAL') ||
die();
29 require_once($CFG->dirroot
.'/cohort/lib.php');
32 * Badge award criteria -- award on cohort membership
36 * @copyright 2016 onwards Catalyst IT {@link https://www.catalyst.net.nz/}
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 * @author Eugene Venter <eugene@catalyst.net.nz>
40 class award_criteria_cohort
extends award_criteria
{
42 /* @var int $criteriatype Criteria [BADGE_CRITERIA_TYPE_COHORT] */
43 public $criteriatype = BADGE_CRITERIA_TYPE_COHORT
;
45 /* @var string $required_param Required form param */
46 public $required_param = 'cohort';
48 /* @var array $optional_params Optional form params */
49 public $optional_params = array();
52 * Get criteria details for displaying to users
53 * @param string $short Print short version of criteria
56 public function get_details($short = '') {
59 foreach ($this->params
as $p) {
60 $cohortname = $DB->get_field('cohort', 'name', array('id' => $p['cohort']));
62 $str = $OUTPUT->error_text(get_string('error:nosuchcohort', 'badges'));
64 $str = html_writer
::tag('b', '"' . format_string($cohortname, true) . '"');
70 return implode(', ', $output);
72 return html_writer
::alist($output, array(), 'ul');
78 * Add appropriate new criteria options to the form
80 * @param object $mform moodle form
82 public function get_options(&$mform) {
86 $mform->addElement('header', 'first_header', $this->get_title());
87 $mform->addHelpButton('first_header', 'criteria_' . $this->criteriatype
, 'badges');
90 $cohorts = $DB->get_records_menu('cohort', array(), 'name ASC', 'id, name');
91 if (!empty($cohorts)) {
94 foreach ($cohorts as $cid => $cohortname) {
95 $select[$cid] = format_string($cohortname, true);
98 if ($this->id
!== 0) {
99 $selected = array_keys($this->params
);
101 $settings = array('multiple' => 'multiple', 'size' => 20, 'class' => 'selectcohort');
102 $mform->addElement('select', 'cohort_cohorts', get_string('addcohort', 'badges'), $select, $settings);
103 $mform->addRule('cohort_cohorts', get_string('requiredcohort', 'badges'), 'required');
104 $mform->addHelpButton('cohort_cohorts', 'addcohort', 'badges');
106 if ($this->id
!== 0) {
107 $mform->setDefault('cohort_cohorts', $selected);
110 $mform->addElement('static', 'nocohorts', '', get_string('error:nocohorts', 'badges'));
116 $mform->addElement('header', 'aggregation', get_string('method', 'badges'));
118 $agg[] =& $mform->createElement('radio', 'agg', '', get_string('allmethodcohort', 'badges'), 1);
119 $agg[] =& $mform->createElement('radio', 'agg', '', get_string('anymethodcohort', 'badges'), 2);
120 $mform->addGroup($agg, 'methodgr', '', array('<br/>'), false);
121 if ($this->id
!== 0) {
122 $mform->setDefault('agg', $this->method
);
124 $mform->setDefault('agg', BADGE_CRITERIA_AGGREGATION_ANY
);
128 return array($none, get_string('noparamstoadd', 'badges'));
132 * Save criteria records
134 * @param array $params Values from the form or any other array.
136 public function save($params = array()) {
137 $cohorts = $params['cohort_cohorts'];
139 unset($params['cohort_cohorts']);
140 foreach ($cohorts as $cohortid) {
141 $params["cohort_{$cohortid}"] = $cohortid;
144 parent
::save($params);
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) {
160 foreach ($this->params
as $param) {
161 $cohort = $DB->get_record('cohort', array('id' => $param['cohort']));
163 // Extra check in case a cohort was deleted while badge is still active.
165 if ($this->method
== BADGE_CRITERIA_AGGREGATION_ALL
) {
172 if ($this->method
== BADGE_CRITERIA_AGGREGATION_ALL
) {
173 if (cohort_is_member($cohort->id
, $userid)) {
179 } else if ($this->method
== BADGE_CRITERIA_AGGREGATION_ANY
) {
180 if (cohort_is_member($cohort->id
, $userid)) {
193 * Checks criteria for any major problems.
195 * @return array A list containing status and an error message (if any).
197 public function validate() {
199 $params = array_keys($this->params
);
200 $method = ($this->method
== BADGE_CRITERIA_AGGREGATION_ALL
);
201 $singleparam = (count($params) == 1);
203 foreach ($params as $param) {
204 // Perform check if there only one parameter with any type of aggregation,
205 // Or there are more than one parameter with aggregation ALL.
206 if (($singleparam ||
$method) && !$DB->record_exists('cohort', array('id' => $param))) {
207 return array(false, get_string('error:invalidparamcohort', 'badges'));
211 return array(true, '');
215 * Returns array with sql code and parameters returning all ids
216 * of users who meet this particular criterion.
218 * @return array list($join, $where, $params)
220 public function get_completed_criteria_sql() {
225 if ($this->method
== BADGE_CRITERIA_AGGREGATION_ANY
) {
226 // User is a member of ANY of the specified cohorts.
227 $join = " LEFT JOIN {cohort_members} cm ON cm.userid = u.id";
230 foreach ($this->params
as $param) {
232 $where .= ' cm.cohortid = :cohortid'.$i;
234 $where .= ' OR cm.cohortid = :cohortid'.$i;
236 $params['cohortid'.$i] = $param['cohort'];
240 return array($join, $where, $params);
242 // User is a member of ALL of the specified cohorts.
243 $join = " LEFT JOIN {cohort_members} cm ON cm.userid = u.id";
245 foreach ($this->params
as $param) {
247 $where = ' AND cm.cohortid = :cohortid'.$i;
248 $params['cohortid'.$i] = $param['cohort'];
250 return array($join, $where, $params);