[ Index ]

PHP Cross Reference of moodle-2.8

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


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1