[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/badges/criteria/ -> award_criteria_course.php (source)

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1