PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/badges/criteria/award_criteria_course.php

https://github.com/pauln/moodle
PHP | 239 lines | 142 code | 35 blank | 62 comment | 24 complexity | 55d15da947a90a2711d6e366cf64b662 MD5 | raw file
  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 INNER JOIN {course} c ON b.courseid = c.id
  45. WHERE b.id = :badgeid ', array('badgeid' => $this->badgeid));
  46. $this->courseid = $this->course->id;
  47. }
  48. /**
  49. * Add appropriate form elements to the criteria form
  50. *
  51. * @param moodleform $mform Moodle forms object
  52. * @param stdClass $data details of various modules
  53. */
  54. public function config_form_criteria($data) {
  55. global $OUTPUT;
  56. $editurl = new moodle_url('/badges/criteria_settings.php', array('badgeid' => $this->badgeid, 'edit' => true, 'type' => $this->criteriatype, 'crit' => $this->id));
  57. $deleteurl = new moodle_url('/badges/criteria_action.php', array('badgeid' => $this->badgeid, 'delete' => true, 'type' => $this->criteriatype));
  58. $editaction = $OUTPUT->action_icon($editurl, new pix_icon('t/edit', get_string('edit')), null, array('class' => 'criteria-action'));
  59. $deleteaction = $OUTPUT->action_icon($deleteurl, new pix_icon('t/delete', get_string('delete')), null, array('class' => 'criteria-action'));
  60. echo $OUTPUT->box_start();
  61. if (!$data->is_locked() && !$data->is_active()) {
  62. echo $OUTPUT->box($deleteaction . $editaction, array('criteria-header'));
  63. }
  64. echo $OUTPUT->heading($this->get_title() . $OUTPUT->help_icon('criteria_' . $this->criteriatype, 'badges'), 3, 'main help');
  65. if (!empty($this->description)) {
  66. echo $OUTPUT->box(
  67. format_text($this->description, $this->descriptionformat,
  68. array('context' => context_course::instance($this->courseid))
  69. ),
  70. 'criteria-description'
  71. );
  72. }
  73. if (!empty($this->params)) {
  74. echo $OUTPUT->box(get_string('criteria_descr_' . $this->criteriatype, 'badges') . $this->get_details(), array('clearfix'));
  75. }
  76. echo $OUTPUT->box_end();
  77. }
  78. /**
  79. * Get criteria details for displaying to users
  80. *
  81. * @return string
  82. */
  83. public function get_details($short = '') {
  84. global $DB;
  85. $param = reset($this->params);
  86. $course = $DB->get_record('course', array('id' => $param['course']));
  87. if (!$course) {
  88. $str = $OUTPUT->error_text(get_string('error:nosuchcourse', 'badges'));
  89. } else {
  90. $options = array('context' => context_course::instance($course->id));
  91. $str = html_writer::tag('b', '"' . format_string($course->fullname, true, $options) . '"');
  92. if (isset($param['bydate'])) {
  93. $str .= get_string('criteria_descr_bydate', 'badges', userdate($param['bydate'], get_string('strftimedate', 'core_langconfig')));
  94. }
  95. if (isset($param['grade'])) {
  96. $str .= get_string('criteria_descr_grade', 'badges', $param['grade']);
  97. }
  98. }
  99. return $str;
  100. }
  101. /**
  102. * Add appropriate new criteria options to the form
  103. *
  104. */
  105. public function get_options(&$mform) {
  106. global $DB;
  107. $param = array();
  108. if ($this->id !== 0) {
  109. $param = reset($this->params);
  110. } else {
  111. $param['course'] = $mform->getElementValue('course');
  112. $mform->removeElement('course');
  113. }
  114. $course = $DB->get_record('course', array('id' => $param['course']));
  115. if (!($course->enablecompletion == COMPLETION_ENABLED)) {
  116. $none = true;
  117. $message = get_string('completionnotenabled', 'badges');
  118. } else {
  119. $mform->addElement('header', 'criteria_course', $this->get_title());
  120. $mform->addHelpButton('criteria_course', 'criteria_' . $this->criteriatype, 'badges');
  121. $parameter = array();
  122. $parameter[] =& $mform->createElement('static', 'mgrade_', null, get_string('mingrade', 'badges'));
  123. $parameter[] =& $mform->createElement('text', 'grade_' . $param['course'], '', array('size' => '5'));
  124. $parameter[] =& $mform->createElement('static', 'complby_' . $param['course'], null, get_string('bydate', 'badges'));
  125. $parameter[] =& $mform->createElement('date_selector', 'bydate_' . $param['course'], '', array('optional' => true));
  126. $mform->setType('grade_' . $param['course'], PARAM_INT);
  127. $mform->addGroup($parameter, 'param_' . $param['course'], '', array(' '), false);
  128. $mform->disabledIf('bydate_' . $param['course'] . '[day]', 'bydate_' . $param['course'] . '[enabled]', 'notchecked');
  129. $mform->disabledIf('bydate_' . $param['course'] . '[month]', 'bydate_' . $param['course'] . '[enabled]', 'notchecked');
  130. $mform->disabledIf('bydate_' . $param['course'] . '[year]', 'bydate_' . $param['course'] . '[enabled]', 'notchecked');
  131. // Set existing values.
  132. if (isset($param['bydate'])) {
  133. $mform->setDefault('bydate_' . $param['course'], $param['bydate']);
  134. }
  135. if (isset($param['grade'])) {
  136. $mform->setDefault('grade_' . $param['course'], $param['grade']);
  137. }
  138. // Add hidden elements.
  139. $mform->addElement('hidden', 'course_' . $course->id, $course->id);
  140. $mform->setType('course_' . $course->id, PARAM_INT);
  141. $mform->addElement('hidden', 'agg', BADGE_CRITERIA_AGGREGATION_ALL);
  142. $mform->setType('agg', PARAM_INT);
  143. $none = false;
  144. $message = '';
  145. }
  146. return array($none, $message);
  147. }
  148. /**
  149. * Review this criteria and decide if it has been completed
  150. *
  151. * @param int $userid User whose criteria completion needs to be reviewed.
  152. * @param bool $filtered An additional parameter indicating that user list
  153. * has been reduced and some expensive checks can be skipped.
  154. *
  155. * @return bool Whether criteria is complete
  156. */
  157. public function review($userid, $filtered = false) {
  158. $course = $this->course;
  159. if ($this->course->startdate > time()) {
  160. return false;
  161. }
  162. $info = new completion_info($course);
  163. foreach ($this->params as $param) {
  164. $check_grade = true;
  165. $check_date = true;
  166. if (isset($param['grade'])) {
  167. $grade = grade_get_course_grade($userid, $course->id);
  168. $check_grade = ($grade->grade >= $param['grade']);
  169. }
  170. if (!$filtered && isset($param['bydate'])) {
  171. $cparams = array(
  172. 'userid' => $userid,
  173. 'course' => $course->id,
  174. );
  175. $completion = new completion_completion($cparams);
  176. $date = $completion->timecompleted;
  177. $check_date = ($date <= $param['bydate']);
  178. }
  179. if ($info->is_course_complete($userid) && $check_grade && $check_date) {
  180. return true;
  181. }
  182. }
  183. return false;
  184. }
  185. /**
  186. * Returns array with sql code and parameters returning all ids
  187. * of users who meet this particular criterion.
  188. *
  189. * @return array list($join, $where, $params)
  190. */
  191. public function get_completed_criteria_sql() {
  192. // We have only one criterion here, so taking the first one.
  193. $coursecriteria = reset($this->params);
  194. $join = " LEFT JOIN {course_completions} cc ON cc.userid = u.id AND cc.timecompleted > 0";
  195. $where = ' AND cc.course = :courseid ';
  196. $params['courseid'] = $this->courseid;
  197. // Add by date parameter.
  198. if (isset($param['bydate'])) {
  199. $where .= ' AND cc.timecompleted <= :completebydate';
  200. $params['completebydate'] = $coursecriteria['bydate'];
  201. }
  202. return array($join, $where, $params);
  203. }
  204. }