Merge branch 'MDL-63625-master' of git://github.com/marinaglancy/moodle
[moodle.git] / badges / criteria / award_criteria_overall.php
blob3b518998d86097f7d7040fd94924a6318c28ffa4
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 overall badge award criteria type
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 * Overall badge award criteria
33 class award_criteria_overall extends award_criteria {
35 /* @var int Criteria [BADGE_CRITERIA_TYPE_OVERALL] */
36 public $criteriatype = BADGE_CRITERIA_TYPE_OVERALL;
38 /**
39 * Add appropriate form elements to the criteria form
41 * @param stdClass $data details of overall criterion
43 public function config_form_criteria($data) {
44 global $OUTPUT;
45 $prefix = 'criteria-' . $this->id;
46 if (count($data->criteria) > 2) {
47 echo $OUTPUT->box_start();
48 if (!empty($this->description)) {
49 $badge = new badge($this->badgeid);
50 echo $OUTPUT->box(
51 format_text($this->description, $this->descriptionformat, array('context' => $badge->get_context())),
52 'criteria-description');
54 echo $OUTPUT->heading($this->get_title(), 2);
56 $agg = $data->get_aggregation_methods();
57 if (!$data->is_locked() && !$data->is_active()) {
58 $editurl = new moodle_url('/badges/criteria_settings.php',
59 array('badgeid' => $this->badgeid,
60 'edit' => true,
61 'type' => $this->criteriatype,
62 'crit' => $this->id
65 $editaction = $OUTPUT->action_icon($editurl, new pix_icon('t/edit', get_string('edit')), null,
66 array('class' => 'criteria-action'));
67 echo $OUTPUT->box($editaction, array('criteria-header'));
69 $url = new moodle_url('criteria.php', array('id' => $data->id, 'sesskey' => sesskey()));
70 echo $OUTPUT->single_select($url, 'update', $agg, $data->get_aggregation_method($this->criteriatype),
71 null, null, array('aria-describedby' => 'overall'));
72 echo html_writer::span(get_string('overallcrit', 'badges'), '', array('id' => 'overall'));
73 } else {
74 echo $OUTPUT->box(get_string('criteria_descr_' . $this->criteriatype, 'badges',
75 core_text::strtoupper($agg[$data->get_aggregation_method()])), 'clearfix');
77 echo $OUTPUT->box_end();
81 /**
82 * Add appropriate parameter elements to the criteria form
85 public function config_options(&$mform, $param) {
88 /**
89 * Get criteria details for displaying to users
91 * @return string
93 public function get_details($short = '') {
96 /**
97 * Review this criteria and decide if it has been completed
98 * Overall criteria review should be called only from other criteria handlers.
100 * @param int $userid User whose criteria completion needs to be reviewed.
101 * @param bool $filtered An additional parameter indicating that user list
102 * has been reduced and some expensive checks can be skipped.
104 * @return bool Whether criteria is complete
106 public function review($userid, $filtered = false) {
107 global $DB;
109 $sql = "SELECT bc.*, bcm.critid, bcm.userid, bcm.datemet
110 FROM {badge_criteria} bc
111 LEFT JOIN {badge_criteria_met} bcm
112 ON bc.id = bcm.critid AND bcm.userid = :userid
113 WHERE bc.badgeid = :badgeid
114 AND bc.criteriatype != :criteriatype ";
116 $params = array(
117 'userid' => $userid,
118 'badgeid' => $this->badgeid,
119 'criteriatype' => BADGE_CRITERIA_TYPE_OVERALL
122 $criteria = $DB->get_records_sql($sql, $params);
123 $overall = false;
124 foreach ($criteria as $crit) {
125 if ($this->method == BADGE_CRITERIA_AGGREGATION_ALL) {
126 if ($crit->datemet === null) {
127 return false;
128 } else {
129 $overall = true;
130 continue;
132 } else {
133 if ($crit->datemet === null) {
134 $overall = false;
135 continue;
136 } else {
137 return true;
142 return $overall;
146 * Returns array with sql code and parameters returning all ids
147 * of users who meet this particular criterion.
149 * @return array list($join, $where, $params)
151 public function get_completed_criteria_sql() {
152 return array('', '', array());
156 * Add appropriate criteria elements to the form
159 public function get_options(&$mform) {
163 * Return criteria parameters
165 * @param int $critid Criterion ID
166 * @return array
168 public function get_params($cid) {
172 * Saves overall badge criteria description.
174 * @param array $params Values from the form or any other array.
176 public function save($params = array()) {
177 global $DB;
179 // Sort out criteria description.
180 // If it is coming from the form editor, it is an array of (text, format).
181 $description = '';
182 $descriptionformat = FORMAT_HTML;
183 if (isset($params['description']['text'])) {
184 $description = $params['description']['text'];
185 $descriptionformat = $params['description']['format'];
186 } else if (isset($params['description'])) {
187 $description = $params['description'];
190 $fordb = new stdClass();
191 $fordb->criteriatype = $this->criteriatype;
192 $fordb->badgeid = $this->badgeid;
193 $fordb->description = $description;
194 $fordb->descriptionformat = $descriptionformat;
195 if ($this->id !== 0) {
196 $fordb->id = $this->id;
197 $DB->update_record('badge_criteria', $fordb);
198 } else {
199 // New record in DB, set aggregation to ALL by default.
200 $fordb->method = BADGE_CRITERIA_AGGREGATION_ALL;
201 $DB->insert_record('badge_criteria', $fordb);