PageRenderTime 52ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/badges/criteria/award_criteria_course.php

https://github.com/dongsheng/moodle
PHP | 244 lines | 145 code | 36 blank | 63 comment | 25 complexity | afd2da36bf599248e5ce71dfee81f212 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, GPL-3.0, Apache-2.0, LGPL-2.1
  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.
  13. //
  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/>.
  16. /**
  17. * This file contains the course completion badge award criteria type class
  18. *
  19. * @package core
  20. * @subpackage badges
  21. * @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. * @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
  24. */
  25. defined('MOODLE_INTERNAL') || die();
  26. require_once($CFG->libdir . '/completionlib.php');
  27. require_once($CFG->dirroot . '/grade/querylib.php');
  28. require_once($CFG->libdir . '/gradelib.php');
  29. /**
  30. * Badge award criteria -- award on course completion
  31. *
  32. */
  33. class award_criteria_course extends award_criteria {
  34. /* @var int Criteria [BADGE_CRITERIA_TYPE_COURSE] */
  35. public $criteriatype = BADGE_CRITERIA_TYPE_COURSE;
  36. private $courseid;
  37. private $course;
  38. public $required_param = 'course';
  39. public $optional_params = array('grade', 'bydate');
  40. public function __construct($record) {
  41. global $DB;
  42. parent::__construct($record);
  43. $this->course = $DB->get_record_sql('SELECT c.id, c.enablecompletion, c.cacherev, c.startdate
  44. FROM {badge} b LEFT JOIN {course} c ON b.courseid = c.id
  45. WHERE b.id = :badgeid ', array('badgeid' => $this->badgeid), MUST_EXIST);
  46. // If the course doesn't exist but we're sure the badge does (thanks to the LEFT JOIN), then use the site as the course.
  47. if (empty($this->course->id)) {
  48. $this->course = get_course(SITEID);
  49. }
  50. $this->courseid = $this->course->id;
  51. }
  52. /**
  53. * Add appropriate form elements to the criteria form
  54. *
  55. * @param moodleform $mform Moodle forms object
  56. * @param stdClass $data details of various modules
  57. */
  58. public function config_form_criteria($data) {
  59. global $OUTPUT;
  60. $editurl = new moodle_url('/badges/criteria_settings.php', array('badgeid' => $this->badgeid, 'edit' => true, 'type' => $this->criteriatype, 'crit' => $this->id));
  61. $deleteurl = new moodle_url('/badges/criteria_action.php', array('badgeid' => $this->badgeid, 'delete' => true, 'type' => $this->criteriatype));
  62. $editaction = $OUTPUT->action_icon($editurl, new pix_icon('t/edit', get_string('edit')), null, array('class' => 'criteria-action'));
  63. $deleteaction = $OUTPUT->action_icon($deleteurl, new pix_icon('t/delete', get_string('delete')), null, array('class' => 'criteria-action'));
  64. echo $OUTPUT->box_start();
  65. if (!$data->is_locked() && !$data->is_active()) {
  66. echo $OUTPUT->box($deleteaction . $editaction, array('criteria-header'));
  67. }
  68. echo $OUTPUT->heading($this->get_title() . $OUTPUT->help_icon('criteria_' . $this->criteriatype, 'badges'), 3, 'main help');
  69. if (!empty($this->description)) {
  70. echo $OUTPUT->box(
  71. format_text($this->description, $this->descriptionformat,
  72. array('context' => context_course::instance($this->courseid))
  73. ),
  74. 'criteria-description'
  75. );
  76. }
  77. if (!empty($this->params)) {
  78. echo $OUTPUT->box(get_string('criteria_descr_' . $this->criteriatype, 'badges') . $this->get_details(), array('clearfix'));
  79. }
  80. echo $OUTPUT->box_end();
  81. }
  82. /**
  83. * Get criteria details for displaying to users
  84. *
  85. * @return string
  86. */
  87. public function get_details($short = '') {
  88. global $DB, $OUTPUT;
  89. $param = reset($this->params);
  90. $course = $DB->get_record('course', array('id' => $param['course']));
  91. if (!$course) {
  92. $str = $OUTPUT->error_text(get_string('error:nosuchcourse', 'badges'));
  93. } else {
  94. $options = array('context' => context_course::instance($course->id));
  95. $str = html_writer::tag('b', '"' . format_string($course->fullname, true, $options) . '"');
  96. if (isset($param['bydate'])) {
  97. $str .= get_string('criteria_descr_bydate', 'badges', userdate($param['bydate'], get_string('strftimedate', 'core_langconfig')));
  98. }
  99. if (isset($param['grade'])) {
  100. $str .= get_string('criteria_descr_grade', 'badges', $param['grade']);
  101. }
  102. }
  103. return $str;
  104. }
  105. /**
  106. * Add appropriate new criteria options to the form
  107. *
  108. */
  109. public function get_options(&$mform) {
  110. global $DB;
  111. $param = array();
  112. if ($this->id !== 0) {
  113. $param = reset($this->params);
  114. } else {
  115. $param['course'] = $mform->getElementValue('course');
  116. $mform->removeElement('course');
  117. }
  118. $course = $DB->get_record('course', array('id' => $param['course']));
  119. if (!($course->enablecompletion == COMPLETION_ENABLED)) {
  120. $none = true;
  121. $message = get_string('completionnotenabled', 'badges');
  122. } else {
  123. $mform->addElement('header', 'criteria_course', $this->get_title());
  124. $mform->addHelpButton('criteria_course', 'criteria_' . $this->criteriatype, 'badges');
  125. $parameter = array();
  126. $parameter[] =& $mform->createElement('static', 'mgrade_', null, get_string('mingrade', 'badges'));
  127. $parameter[] =& $mform->createElement('text', 'grade_' . $param['course'], '', array('size' => '5'));
  128. $parameter[] =& $mform->createElement('static', 'complby_' . $param['course'], null, get_string('bydate', 'badges'));
  129. $parameter[] =& $mform->createElement('date_selector', 'bydate_' . $param['course'], '', array('optional' => true));
  130. $mform->setType('grade_' . $param['course'], PARAM_INT);
  131. $mform->addGroup($parameter, 'param_' . $param['course'], '', array(' '), false);
  132. $mform->disabledIf('bydate_' . $param['course'] . '[day]', 'bydate_' . $param['course'] . '[enabled]', 'notchecked');
  133. $mform->disabledIf('bydate_' . $param['course'] . '[month]', 'bydate_' . $param['course'] . '[enabled]', 'notchecked');
  134. $mform->disabledIf('bydate_' . $param['course'] . '[year]', 'bydate_' . $param['course'] . '[enabled]', 'notchecked');
  135. // Set existing values.
  136. if (isset($param['bydate'])) {
  137. $mform->setDefault('bydate_' . $param['course'], $param['bydate']);
  138. }
  139. if (isset($param['grade'])) {
  140. $mform->setDefault('grade_' . $param['course'], $param['grade']);
  141. }
  142. // Add hidden elements.
  143. $mform->addElement('hidden', 'course_' . $course->id, $course->id);
  144. $mform->setType('course_' . $course->id, PARAM_INT);
  145. $mform->addElement('hidden', 'agg', BADGE_CRITERIA_AGGREGATION_ALL);
  146. $mform->setType('agg', PARAM_INT);
  147. $none = false;
  148. $message = '';
  149. }
  150. return array($none, $message);
  151. }
  152. /**
  153. * Review this criteria and decide if it has been completed
  154. *
  155. * @param int $userid User whose criteria completion needs to be reviewed.
  156. * @param bool $filtered An additional parameter indicating that user list
  157. * has been reduced and some expensive checks can be skipped.
  158. *
  159. * @return bool Whether criteria is complete
  160. */
  161. public function review($userid, $filtered = false) {
  162. $course = $this->course;
  163. if ($this->course->startdate > time()) {
  164. return false;
  165. }
  166. $info = new completion_info($course);
  167. foreach ($this->params as $param) {
  168. $check_grade = true;
  169. $check_date = true;
  170. if (isset($param['grade'])) {
  171. $grade = grade_get_course_grade($userid, $course->id);
  172. $check_grade = ($grade->grade >= $param['grade']);
  173. }
  174. if (!$filtered && isset($param['bydate'])) {
  175. $cparams = array(
  176. 'userid' => $userid,
  177. 'course' => $course->id,
  178. );
  179. $completion = new completion_completion($cparams);
  180. $date = $completion->timecompleted;
  181. $check_date = ($date <= $param['bydate']);
  182. }
  183. if ($info->is_course_complete($userid) && $check_grade && $check_date) {
  184. return true;
  185. }
  186. }
  187. return false;
  188. }
  189. /**
  190. * Returns array with sql code and parameters returning all ids
  191. * of users who meet this particular criterion.
  192. *
  193. * @return array list($join, $where, $params)
  194. */
  195. public function get_completed_criteria_sql() {
  196. // We have only one criterion here, so taking the first one.
  197. $coursecriteria = reset($this->params);
  198. $join = " LEFT JOIN {course_completions} cc ON cc.userid = u.id AND cc.timecompleted > 0";
  199. $where = ' AND cc.course = :courseid ';
  200. $params['courseid'] = $this->courseid;
  201. // Add by date parameter.
  202. if (isset($param['bydate'])) {
  203. $where .= ' AND cc.timecompleted <= :completebydate';
  204. $params['completebydate'] = $coursecriteria['bydate'];
  205. }
  206. return array($join, $where, $params);
  207. }
  208. }