PageRenderTime 57ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/badges/criteria/award_criteria_course.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 191 lines | 114 code | 26 blank | 51 comment | 21 complexity | e4743755e4c986d0a83a8e0aa89b005c MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0
  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. public $required_param = 'course';
  37. public $optional_params = array('grade', 'bydate');
  38. /**
  39. * Add appropriate form elements to the criteria form
  40. *
  41. * @param moodleform $mform Moodle forms object
  42. * @param stdClass $data details of various modules
  43. */
  44. public function config_form_criteria($data) {
  45. global $OUTPUT;
  46. $editurl = new moodle_url('/badges/criteria_settings.php', array('badgeid' => $this->badgeid, 'edit' => true, 'type' => $this->criteriatype, 'crit' => $this->id));
  47. $deleteurl = new moodle_url('/badges/criteria_action.php', array('badgeid' => $this->badgeid, 'delete' => true, 'type' => $this->criteriatype));
  48. $editaction = $OUTPUT->action_icon($editurl, new pix_icon('t/edit', get_string('edit')), null, array('class' => 'criteria-action'));
  49. $deleteaction = $OUTPUT->action_icon($deleteurl, new pix_icon('t/delete', get_string('delete')), null, array('class' => 'criteria-action'));
  50. echo $OUTPUT->box_start();
  51. if (!$data->is_locked() && !$data->is_active()) {
  52. echo $OUTPUT->box($deleteaction . $editaction, array('criteria-header'));
  53. }
  54. echo $OUTPUT->heading($this->get_title() . $OUTPUT->help_icon('criteria_' . $this->criteriatype, 'badges'), 3, 'main help');
  55. if (!empty($this->params)) {
  56. echo $OUTPUT->box(get_string('criteria_descr_' . $this->criteriatype, 'badges') . $this->get_details(), array('clearfix'));
  57. }
  58. echo $OUTPUT->box_end();
  59. }
  60. /**
  61. * Get criteria details for displaying to users
  62. *
  63. * @return string
  64. */
  65. public function get_details($short = '') {
  66. global $DB;
  67. $param = reset($this->params);
  68. $course = $DB->get_record('course', array('id' => $param['course']));
  69. if (!$course) {
  70. $str = $OUTPUT->error_text(get_string('error:nosuchcourse', 'badges'));
  71. } else {
  72. $options = array('context' => context_course::instance($course->id));
  73. $str = html_writer::tag('b', '"' . format_string($course->fullname, true, $options) . '"');
  74. if (isset($param['bydate'])) {
  75. $str .= get_string('criteria_descr_bydate', 'badges', userdate($param['bydate'], get_string('strftimedate', 'core_langconfig')));
  76. }
  77. if (isset($param['grade'])) {
  78. $str .= get_string('criteria_descr_grade', 'badges', $param['grade']);
  79. }
  80. }
  81. return $str;
  82. }
  83. /**
  84. * Add appropriate new criteria options to the form
  85. *
  86. */
  87. public function get_options(&$mform) {
  88. global $DB;
  89. $param = array();
  90. if ($this->id !== 0) {
  91. $param = reset($this->params);
  92. } else {
  93. $param['course'] = $mform->getElementValue('course');
  94. $mform->removeElement('course');
  95. }
  96. $course = $DB->get_record('course', array('id' => $param['course']));
  97. if (!($course->enablecompletion == COMPLETION_ENABLED)) {
  98. $none = true;
  99. $message = get_string('completionnotenabled', 'badges');
  100. } else {
  101. $mform->addElement('header', 'criteria_course', $this->get_title());
  102. $mform->addHelpButton('criteria_course', 'criteria_' . $this->criteriatype, 'badges');
  103. $parameter = array();
  104. $parameter[] =& $mform->createElement('static', 'mgrade_', null, get_string('mingrade', 'badges'));
  105. $parameter[] =& $mform->createElement('text', 'grade_' . $param['course'], '', array('size' => '5'));
  106. $parameter[] =& $mform->createElement('static', 'complby_' . $param['course'], null, get_string('bydate', 'badges'));
  107. $parameter[] =& $mform->createElement('date_selector', 'bydate_' . $param['course'], '', array('optional' => true));
  108. $mform->setType('grade_' . $param['course'], PARAM_INT);
  109. $mform->addGroup($parameter, 'param_' . $param['course'], '', array(' '), false);
  110. $mform->disabledIf('bydate_' . $param['course'] . '[day]', 'bydate_' . $param['course'] . '[enabled]', 'notchecked');
  111. $mform->disabledIf('bydate_' . $param['course'] . '[month]', 'bydate_' . $param['course'] . '[enabled]', 'notchecked');
  112. $mform->disabledIf('bydate_' . $param['course'] . '[year]', 'bydate_' . $param['course'] . '[enabled]', 'notchecked');
  113. // Set existing values.
  114. if (isset($param['bydate'])) {
  115. $mform->setDefault('bydate_' . $param['course'], $param['bydate']);
  116. }
  117. if (isset($param['grade'])) {
  118. $mform->setDefault('grade_' . $param['course'], $param['grade']);
  119. }
  120. // Add hidden elements.
  121. $mform->addElement('hidden', 'course_' . $course->id, $course->id);
  122. $mform->setType('course_' . $course->id, PARAM_INT);
  123. $mform->addElement('hidden', 'agg', BADGE_CRITERIA_AGGREGATION_ALL);
  124. $mform->setType('agg', PARAM_INT);
  125. $none = false;
  126. $message = '';
  127. }
  128. return array($none, $message);
  129. }
  130. /**
  131. * Review this criteria and decide if it has been completed
  132. *
  133. * @param int $userid User whose criteria completion needs to be reviewed.
  134. * @return bool Whether criteria is complete
  135. */
  136. public function review($userid) {
  137. global $DB;
  138. foreach ($this->params as $param) {
  139. $course = $DB->get_record('course', array('id' => $param['course']));
  140. if ($course->startdate > time()) {
  141. return false;
  142. }
  143. $info = new completion_info($course);
  144. $check_grade = true;
  145. $check_date = true;
  146. if (isset($param['grade'])) {
  147. $grade = grade_get_course_grade($userid, $course->id);
  148. $check_grade = ($grade->grade >= $param['grade']);
  149. }
  150. if (isset($param['bydate'])) {
  151. $cparams = array(
  152. 'userid' => $userid,
  153. 'course' => $course->id,
  154. );
  155. $completion = new completion_completion($cparams);
  156. $date = $completion->timecompleted;
  157. $check_date = ($date <= $param['bydate']);
  158. }
  159. if ($info->is_course_complete($userid) && $check_grade && $check_date) {
  160. return true;
  161. }
  162. }
  163. return false;
  164. }
  165. }