Merge branch 'MDL-51969-master' of https://github.com/dmitriim/moodle
[moodle.git] / badges / criteria / award_criteria_courseset.php
blobf60715c9e5684e55630de9f284ccef37c8993b1a
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 courseset completion 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();
28 require_once('award_criteria_course.php');
29 require_once($CFG->libdir . '/completionlib.php');
30 require_once($CFG->dirroot . '/grade/querylib.php');
31 require_once($CFG->libdir . '/gradelib.php');
33 /**
34 * Badge award criteria -- award on courseset completion
37 class award_criteria_courseset extends award_criteria {
39 /* @var int Criteria [BADGE_CRITERIA_TYPE_COURSESET] */
40 public $criteriatype = BADGE_CRITERIA_TYPE_COURSESET;
42 public $required_param = 'course';
43 public $optional_params = array('grade', 'bydate');
45 /**
46 * Get criteria details for displaying to users
48 * @return string
50 public function get_details($short = '') {
51 global $DB, $OUTPUT;
52 $output = array();
53 foreach ($this->params as $p) {
54 $coursename = $DB->get_field('course', 'fullname', array('id' => $p['course']));
55 if (!$coursename) {
56 $str = $OUTPUT->error_text(get_string('error:nosuchcourse', 'badges'));
57 } else {
58 $str = html_writer::tag('b', '"' . $coursename . '"');
59 if (isset($p['bydate'])) {
60 $str .= get_string('criteria_descr_bydate', 'badges', userdate($p['bydate'], get_string('strftimedate', 'core_langconfig')));
62 if (isset($p['grade'])) {
63 $str .= get_string('criteria_descr_grade', 'badges', $p['grade']);
66 $output[] = $str;
69 if ($short) {
70 return implode(', ', $output);
71 } else {
72 return html_writer::alist($output, array(), 'ul');
76 public function get_courses(&$mform) {
77 global $DB, $CFG;
78 require_once($CFG->dirroot . '/course/lib.php');
79 $buttonarray = array();
81 // Get courses with enabled completion.
82 $courses = $DB->get_records('course', array('enablecompletion' => COMPLETION_ENABLED));
83 if (!empty($courses)) {
84 $list = core_course_category::make_categories_list();
86 $select = array();
87 $selected = array();
88 foreach ($courses as $c) {
89 $select[$c->id] = $list[$c->category] . ' / ' . format_string($c->fullname, true, array('context' => context_course::instance($c->id)));
92 if ($this->id !== 0) {
93 $selected = array_keys($this->params);
95 $settings = array('multiple' => 'multiple', 'size' => 20, 'style' => 'width:300px');
96 $mform->addElement('select', 'courses', get_string('addcourse', 'badges'), $select, $settings);
97 $mform->addRule('courses', get_string('requiredcourse', 'badges'), 'required');
98 $mform->addHelpButton('courses', 'addcourse', 'badges');
100 $buttonarray[] =& $mform->createElement('submit', 'submitcourse', get_string('addcourse', 'badges'));
101 $buttonarray[] =& $mform->createElement('submit', 'cancel', get_string('cancel'));
102 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
104 $mform->addElement('hidden', 'addcourse', 'addcourse');
105 $mform->setType('addcourse', PARAM_TEXT);
106 if ($this->id !== 0) {
107 $mform->setDefault('courses', $selected);
109 $mform->setType('agg', PARAM_INT);
110 } else {
111 $mform->addElement('static', 'nocourses', '', get_string('error:nocourses', 'badges'));
112 $buttonarray[] =& $mform->createElement('submit', 'cancel', get_string('continue'));
113 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
117 public function add_courses($params = array()) {
118 global $DB;
119 $t = $DB->start_delegated_transaction();
120 if ($this->id !== 0) {
121 $critid = $this->id;
122 } else {
123 $fordb = new stdClass();
124 $fordb->criteriatype = $this->criteriatype;
125 $fordb->method = BADGE_CRITERIA_AGGREGATION_ALL;
126 $fordb->badgeid = $this->badgeid;
127 $critid = $DB->insert_record('badge_criteria', $fordb, true, true);
129 if ($critid) {
130 foreach ($params as $p) {
131 $newp = new stdClass();
132 $newp->critid = $critid;
133 $newp->name = 'course_' . $p;
134 $newp->value = $p;
135 if (!$DB->record_exists('badge_criteria_param', array('critid' => $critid, 'name' => $newp->name))) {
136 $DB->insert_record('badge_criteria_param', $newp, false, true);
140 $t->allow_commit();
141 return $critid;
145 * Add appropriate new criteria options to the form
148 public function get_options(&$mform) {
149 global $DB;
150 $none = true;
152 $mform->addElement('header', 'first_header', $this->get_title());
153 $mform->addHelpButton('first_header', 'criteria_' . $this->criteriatype, 'badges');
155 if ($courses = $DB->get_records('course', array('enablecompletion' => COMPLETION_ENABLED))) {
156 $mform->addElement('submit', 'addcourse', get_string('addcourse', 'badges'), array('class' => 'addcourse'));
159 // In courseset, print out only the ones that were already selected.
160 foreach ($this->params as $p) {
161 if ($course = $DB->get_record('course', array('id' => $p['course']))) {
162 $coursecontext = context_course::instance($course->id);
163 $param = array(
164 'id' => $course->id,
165 'checked' => true,
166 'name' => format_string($course->fullname, true, array('context' => $coursecontext)),
167 'error' => false
170 if (isset($p['bydate'])) {
171 $param['bydate'] = $p['bydate'];
173 if (isset($p['grade'])) {
174 $param['grade'] = $p['grade'];
176 $this->config_options($mform, $param);
177 $none = false;
178 } else {
179 $this->config_options($mform, array('id' => $p['course'], 'checked' => true,
180 'name' => get_string('error:nosuchcourse', 'badges'), 'error' => true));
184 // Add aggregation.
185 if (!$none) {
186 $mform->addElement('header', 'aggregation', get_string('method', 'badges'));
187 $agg = array();
188 $agg[] =& $mform->createElement('radio', 'agg', '', get_string('allmethodcourseset', 'badges'), 1);
189 $agg[] =& $mform->createElement('radio', 'agg', '', get_string('anymethodcourseset', 'badges'), 2);
190 $mform->addGroup($agg, 'methodgr', '', array('<br/>'), false);
191 if ($this->id !== 0) {
192 $mform->setDefault('agg', $this->method);
193 } else {
194 $mform->setDefault('agg', BADGE_CRITERIA_AGGREGATION_ANY);
198 return array($none, get_string('noparamstoadd', 'badges'));
202 * Review this criteria and decide if it has been completed
204 * @param int $userid User whose criteria completion needs to be reviewed.
205 * @param bool $filtered An additional parameter indicating that user list
206 * has been reduced and some expensive checks can be skipped.
208 * @return bool Whether criteria is complete
210 public function review($userid, $filtered = false) {
211 foreach ($this->params as $param) {
212 $course = new stdClass();
213 $course->id = $param['course'];
215 $info = new completion_info($course);
216 $check_grade = true;
217 $check_date = true;
219 if (isset($param['grade'])) {
220 $grade = grade_get_course_grade($userid, $course->id);
221 $check_grade = ($grade->grade >= $param['grade']);
224 if (!$filtered && isset($param['bydate'])) {
225 $cparams = array(
226 'userid' => $userid,
227 'course' => $course->id,
229 $completion = new completion_completion($cparams);
230 $date = $completion->timecompleted;
231 $check_date = ($date <= $param['bydate']);
234 $overall = false;
235 if ($this->method == BADGE_CRITERIA_AGGREGATION_ALL) {
236 if ($info->is_course_complete($userid) && $check_grade && $check_date) {
237 $overall = true;
238 continue;
239 } else {
240 return false;
242 } else {
243 if ($info->is_course_complete($userid) && $check_grade && $check_date) {
244 return true;
245 } else {
246 $overall = false;
247 continue;
252 return $overall;
256 * Returns array with sql code and parameters returning all ids
257 * of users who meet this particular criterion.
259 * @return array list($join, $where, $params)
261 public function get_completed_criteria_sql() {
262 $join = '';
263 $where = '';
264 $params = array();
266 if ($this->method == BADGE_CRITERIA_AGGREGATION_ANY) {
267 foreach ($this->params as $param) {
268 $coursedata[] = " cc.course = :completedcourse{$param['course']} ";
269 $params["completedcourse{$param['course']}"] = $param['course'];
271 if (!empty($coursedata)) {
272 $extraon = implode(' OR ', $coursedata);
273 $join = " JOIN {course_completions} cc ON cc.userid = u.id AND
274 cc.timecompleted > 0 AND ({$extraon})";
276 return array($join, $where, $params);
277 } else {
278 foreach ($this->params as $param) {
279 $join .= " LEFT JOIN {course_completions} cc{$param['course']} ON
280 cc{$param['course']}.userid = u.id AND
281 cc{$param['course']}.course = :completedcourse{$param['course']} AND
282 cc{$param['course']}.timecompleted > 0 ";
283 $where .= " AND cc{$param['course']}.course IS NOT NULL ";
284 $params["completedcourse{$param['course']}"] = $param['course'];
286 return array($join, $where, $params);