[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/badges/criteria/ -> award_criteria_activity.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 activity 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  
  30  /**
  31   * Badge award criteria -- award on activity completion
  32   *
  33   */
  34  class award_criteria_activity extends award_criteria {
  35  
  36      /* @var int Criteria [BADGE_CRITERIA_TYPE_ACTIVITY] */
  37      public $criteriatype = BADGE_CRITERIA_TYPE_ACTIVITY;
  38  
  39      private $courseid;
  40      private $coursestartdate;
  41  
  42      public $required_param = 'module';
  43      public $optional_params = array('bydate');
  44  
  45      public function __construct($record) {
  46          global $DB;
  47          parent::__construct($record);
  48  
  49          $course = $DB->get_record_sql('SELECT b.courseid, c.startdate
  50                          FROM {badge} b INNER JOIN {course} c ON b.courseid = c.id
  51                          WHERE b.id = :badgeid ', array('badgeid' => $this->badgeid));
  52          $this->courseid = $course->courseid;
  53          $this->coursestartdate = $course->startdate;
  54      }
  55  
  56      /**
  57       * Gets the module instance from the database and returns it.
  58       * If no module instance exists this function returns false.
  59       *
  60       * @return stdClass|bool
  61       */
  62      private function get_mod_instance($cmid) {
  63          global $DB;
  64          $rec = $DB->get_record_sql("SELECT md.name
  65                                 FROM {course_modules} cm,
  66                                      {modules} md
  67                                 WHERE cm.id = ? AND
  68                                       md.id = cm.module", array($cmid));
  69  
  70          if ($rec) {
  71              return get_coursemodule_from_id($rec->name, $cmid);
  72          } else {
  73              return null;
  74          }
  75      }
  76  
  77      /**
  78       * Get criteria description for displaying to users
  79       *
  80       * @return string
  81       */
  82      public function get_details($short = '') {
  83          global $DB, $OUTPUT;
  84          $output = array();
  85          foreach ($this->params as $p) {
  86              $mod = self::get_mod_instance($p['module']);
  87              if (!$mod) {
  88                  $str = $OUTPUT->error_text(get_string('error:nosuchmod', 'badges'));
  89              } else {
  90                  $str = html_writer::tag('b', '"' . ucfirst($mod->modname) . ' - ' . $mod->name . '"');
  91                  if (isset($p['bydate'])) {
  92                      $str .= get_string('criteria_descr_bydate', 'badges', userdate($p['bydate'], get_string('strftimedate', 'core_langconfig')));
  93                  }
  94              }
  95              $output[] = $str;
  96          }
  97  
  98          if ($short) {
  99              return implode(', ', $output);
 100          } else {
 101              return html_writer::alist($output, array(), 'ul');
 102          }
 103      }
 104  
 105      /**
 106       * Add appropriate new criteria options to the form
 107       *
 108       */
 109      public function get_options(&$mform) {
 110          global $DB;
 111  
 112          $none = true;
 113          $existing = array();
 114          $missing = array();
 115  
 116          $course = $DB->get_record('course', array('id' => $this->courseid));
 117          $info = new completion_info($course);
 118          $mods = $info->get_activities();
 119          $mids = array_keys($mods);
 120  
 121          if ($this->id !== 0) {
 122              $existing = array_keys($this->params);
 123              $missing = array_diff($existing, $mids);
 124          }
 125  
 126          if (!empty($missing)) {
 127              $mform->addElement('header', 'category_errors', get_string('criterror', 'badges'));
 128              $mform->addHelpButton('category_errors', 'criterror', 'badges');
 129              foreach ($missing as $m) {
 130                  $this->config_options($mform, array('id' => $m, 'checked' => true,
 131                          'name' => get_string('error:nosuchmod', 'badges'), 'error' => true));
 132                  $none = false;
 133              }
 134          }
 135  
 136          if (!empty($mods)) {
 137              $mform->addElement('header', 'first_header', $this->get_title());
 138              foreach ($mods as $mod) {
 139                  $checked = false;
 140                  if (in_array($mod->id, $existing)) {
 141                      $checked = true;
 142                  }
 143                  $param = array('id' => $mod->id,
 144                          'checked' => $checked,
 145                          'name' => ucfirst($mod->modname) . ' - ' . $mod->name,
 146                          'error' => false
 147                          );
 148  
 149                  if ($this->id !== 0 && isset($this->params[$mod->id]['bydate'])) {
 150                      $param['bydate'] = $this->params[$mod->id]['bydate'];
 151                  }
 152  
 153                  if ($this->id !== 0 && isset($this->params[$mod->id]['grade'])) {
 154                      $param['grade'] = $this->params[$mod->id]['grade'];
 155                  }
 156  
 157                  $this->config_options($mform, $param);
 158                  $none = false;
 159              }
 160          }
 161  
 162          // Add aggregation.
 163          if (!$none) {
 164              $mform->addElement('header', 'aggregation', get_string('method', 'badges'));
 165              $agg = array();
 166              $agg[] =& $mform->createElement('radio', 'agg', '', get_string('allmethodactivity', 'badges'), 1);
 167              $agg[] =& $mform->createElement('radio', 'agg', '', get_string('anymethodactivity', 'badges'), 2);
 168              $mform->addGroup($agg, 'methodgr', '', array('<br/>'), false);
 169              if ($this->id !== 0) {
 170                  $mform->setDefault('agg', $this->method);
 171              } else {
 172                  $mform->setDefault('agg', BADGE_CRITERIA_AGGREGATION_ANY);
 173              }
 174          }
 175  
 176          return array($none, get_string('error:noactivities', 'badges'));
 177      }
 178  
 179      /**
 180       * Review this criteria and decide if it has been completed
 181       *
 182       * @param int $userid User whose criteria completion needs to be reviewed.
 183       * @param bool $filtered An additional parameter indicating that user list
 184       *        has been reduced and some expensive checks can be skipped.
 185       *
 186       * @return bool Whether criteria is complete
 187       */
 188      public function review($userid, $filtered = false) {
 189          $completionstates = array(COMPLETION_COMPLETE, COMPLETION_COMPLETE_PASS);
 190          $course = new stdClass();
 191          $course->id = $this->courseid;
 192  
 193          if ($this->coursestartdate > time()) {
 194              return false;
 195          }
 196  
 197          $info = new completion_info($course);
 198  
 199          $overall = false;
 200          foreach ($this->params as $param) {
 201              $cm = new stdClass();
 202              $cm->id = $param['module'];
 203  
 204              $data = $info->get_data($cm, false, $userid);
 205              $check_date = true;
 206  
 207              if (isset($param['bydate'])) {
 208                  $date = $data->timemodified;
 209                  $check_date = ($date <= $param['bydate']);
 210              }
 211  
 212              if ($this->method == BADGE_CRITERIA_AGGREGATION_ALL) {
 213                  if (in_array($data->completionstate, $completionstates) && $check_date) {
 214                      $overall = true;
 215                      continue;
 216                  } else {
 217                      return false;
 218                  }
 219              } else {
 220                  if (in_array($data->completionstate, $completionstates) && $check_date) {
 221                      return true;
 222                  } else {
 223                      $overall = false;
 224                      continue;
 225                  }
 226              }
 227          }
 228  
 229          return $overall;
 230      }
 231  
 232      /**
 233       * Returns array with sql code and parameters returning all ids
 234       * of users who meet this particular criterion.
 235       *
 236       * @return array list($join, $where, $params)
 237       */
 238      public function get_completed_criteria_sql() {
 239          $join = '';
 240          $where = '';
 241          $params = array();
 242  
 243          if ($this->method == BADGE_CRITERIA_AGGREGATION_ANY) {
 244              foreach ($this->params as $param) {
 245                  $moduledata[] = " cmc.coursemoduleid = :completedmodule{$param['module']} ";
 246                  $params["completedmodule{$param['module']}"] = $param['module'];
 247              }
 248              if (!empty($moduledata)) {
 249                  $extraon = implode(' OR ', $moduledata);
 250                  $join = " JOIN {course_modules_completion} cmc ON cmc.userid = u.id AND
 251                            ( cmc.completionstate = :completionpass OR cmc.completionstate = :completioncomplete ) AND ({$extraon})";
 252                  $params["completionpass"] = COMPLETION_COMPLETE_PASS;
 253                  $params["completioncomplete"] = COMPLETION_COMPLETE;
 254              }
 255              return array($join, $where, $params);
 256          } else {
 257              foreach ($this->params as $param) {
 258                  $join .= " LEFT JOIN {course_modules_completion} cmc{$param['module']} ON
 259                            cmc{$param['module']}.userid = u.id AND
 260                            cmc{$param['module']}.coursemoduleid = :completedmodule{$param['module']} AND
 261                            ( cmc{$param['module']}.completionstate = :completionpass{$param['module']} OR
 262                              cmc{$param['module']}.completionstate = :completioncomplete{$param['module']} )";
 263                  $where .= " AND cmc{$param['module']}.coursemoduleid IS NOT NULL ";
 264                  $params["completedmodule{$param['module']}"] = $param['module'];
 265                  $params["completionpass{$param['module']}"] = COMPLETION_COMPLETE_PASS;
 266                  $params["completioncomplete{$param['module']}"] = COMPLETION_COMPLETE;
 267              }
 268              return array($join, $where, $params);
 269          }
 270      }
 271  }


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