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 courseset completion badge award criteria type class
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');
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');
46 * Get criteria details for displaying to users
50 public function get_details($short = '') {
53 foreach ($this->params
as $p) {
54 $coursename = $DB->get_field('course', 'fullname', array('id' => $p['course']));
56 $str = $OUTPUT->error_text(get_string('error:nosuchcourse', 'badges'));
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']);
70 return implode(', ', $output);
72 return html_writer
::alist($output, array(), 'ul');
76 public function get_courses(&$mform) {
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();
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
);
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()) {
119 $t = $DB->start_delegated_transaction();
120 if ($this->id
!== 0) {
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);
130 foreach ($params as $p) {
131 $newp = new stdClass();
132 $newp->critid
= $critid;
133 $newp->name
= 'course_' . $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);
145 * Add appropriate new criteria options to the form
148 public function get_options(&$mform) {
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
);
166 'name' => format_string($course->fullname
, true, array('context' => $coursecontext)),
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);
179 $this->config_options($mform, array('id' => $p['course'], 'checked' => true,
180 'name' => get_string('error:nosuchcourse', 'badges'), 'error' => true));
186 $mform->addElement('header', 'aggregation', get_string('method', 'badges'));
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
);
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);
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'])) {
227 'course' => $course->id
,
229 $completion = new completion_completion($cparams);
230 $date = $completion->timecompleted
;
231 $check_date = ($date <= $param['bydate']);
235 if ($this->method
== BADGE_CRITERIA_AGGREGATION_ALL
) {
236 if ($info->is_course_complete($userid) && $check_grade && $check_date) {
243 if ($info->is_course_complete($userid) && $check_grade && $check_date) {
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() {
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);
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);