Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   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 overall badge award criteria type
  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  
  29  /**
  30   * Overall badge award criteria
  31   *
  32   */
  33  class award_criteria_overall extends award_criteria {
  34  
  35      /* @var int Criteria [BADGE_CRITERIA_TYPE_OVERALL] */
  36      public $criteriatype = BADGE_CRITERIA_TYPE_OVERALL;
  37  
  38      /**
  39       * Add appropriate form elements to the criteria form
  40       *
  41       * @param stdClass $data details of overall criterion
  42       */
  43      public function config_form_criteria($data) {
  44          global $OUTPUT;
  45          $prefix = 'criteria-' . $this->id;
  46          if (count($data->criteria) > 2) {
  47              echo $OUTPUT->box_start();
  48              if (!empty($this->description)) {
  49                  $badge = new badge($this->badgeid);
  50                  echo $OUTPUT->box(
  51                      format_text($this->description, $this->descriptionformat, array('context' => $badge->get_context())),
  52                      'criteria-description');
  53              }
  54              echo $OUTPUT->heading($this->get_title(), 2);
  55  
  56              $agg = $data->get_aggregation_methods();
  57              if (!$data->is_locked() && !$data->is_active()) {
  58                  $editurl = new moodle_url('/badges/criteria_settings.php',
  59                                 array('badgeid' => $this->badgeid,
  60                                     'edit' => true,
  61                                     'type' => $this->criteriatype,
  62                                     'crit' => $this->id
  63                                 )
  64                          );
  65                  $editaction = $OUTPUT->action_icon($editurl, new pix_icon('t/edit', get_string('edit')), null,
  66                                array('class' => 'criteria-action'));
  67                  echo $OUTPUT->box($editaction, array('criteria-header'));
  68  
  69                  $url = new moodle_url('criteria.php', array('id' => $data->id, 'sesskey' => sesskey()));
  70                  echo $OUTPUT->single_select($url, 'update', $agg, $data->get_aggregation_method($this->criteriatype),
  71                      null, null, array('aria-describedby' => 'overall'));
  72                  echo html_writer::span(get_string('overallcrit', 'badges'), '', array('id' => 'overall'));
  73              } else {
  74                  echo $OUTPUT->box(get_string('criteria_descr_' . $this->criteriatype, 'badges',
  75                          core_text::strtoupper($agg[$data->get_aggregation_method()])), 'clearfix');
  76              }
  77              echo $OUTPUT->box_end();
  78          }
  79      }
  80  
  81      /**
  82       * Add appropriate parameter elements to the criteria form
  83       *
  84       */
  85      public function config_options(&$mform, $param) {
  86      }
  87  
  88      /**
  89       * Get criteria details for displaying to users
  90       *
  91       * @return string
  92       */
  93      public function get_details($short = '') {
  94      }
  95  
  96      /**
  97       * Review this criteria and decide if it has been completed
  98       * Overall criteria review should be called only from other criteria handlers.
  99       *
 100       * @param int $userid User whose criteria completion needs to be reviewed.
 101       * @param bool $filtered An additional parameter indicating that user list
 102       *        has been reduced and some expensive checks can be skipped.
 103       *
 104       * @return bool Whether criteria is complete
 105       */
 106      public function review($userid, $filtered = false) {
 107          global $DB;
 108  
 109          $sql = "SELECT bc.*, bcm.critid, bcm.userid, bcm.datemet
 110                  FROM {badge_criteria} bc
 111                  LEFT JOIN {badge_criteria_met} bcm
 112                      ON bc.id = bcm.critid AND bcm.userid = :userid
 113                  WHERE bc.badgeid = :badgeid
 114                      AND bc.criteriatype != :criteriatype ";
 115  
 116          $params = array(
 117                      'userid' => $userid,
 118                      'badgeid' => $this->badgeid,
 119                      'criteriatype' => BADGE_CRITERIA_TYPE_OVERALL
 120                  );
 121  
 122          $criteria = $DB->get_records_sql($sql, $params);
 123          $overall = false;
 124          foreach ($criteria as $crit) {
 125              if ($this->method == BADGE_CRITERIA_AGGREGATION_ALL) {
 126                  if ($crit->datemet === null) {
 127                      return false;
 128                  } else {
 129                      $overall = true;
 130                      continue;
 131                  }
 132              } else {
 133                  if ($crit->datemet === null) {
 134                      $overall = false;
 135                      continue;
 136                  } else {
 137                      return true;
 138                  }
 139              }
 140          }
 141  
 142          return $overall;
 143      }
 144  
 145      /**
 146       * Returns array with sql code and parameters returning all ids
 147       * of users who meet this particular criterion.
 148       *
 149       * @return array list($join, $where, $params)
 150       */
 151      public function get_completed_criteria_sql() {
 152          return array('', '', array());
 153      }
 154  
 155      /**
 156       * Add appropriate criteria elements to the form
 157       *
 158       */
 159      public function get_options(&$mform) {
 160      }
 161  
 162      /**
 163       * Return criteria parameters
 164       *
 165       * @param int $critid Criterion ID
 166       * @return array
 167       */
 168      public function get_params($cid) {
 169      }
 170  
 171      /**
 172       * Saves overall badge criteria description.
 173       *
 174       * @param array $params Values from the form or any other array.
 175       */
 176      public function save($params = array()) {
 177          global $DB;
 178  
 179          // Sort out criteria description.
 180          // If it is coming from the form editor, it is an array of (text, format).
 181          $description = '';
 182          $descriptionformat = FORMAT_HTML;
 183          if (isset($params['description']['text'])) {
 184              $description = $params['description']['text'];
 185              $descriptionformat = $params['description']['format'];
 186          } else if (isset($params['description'])) {
 187              $description = $params['description'];
 188          }
 189  
 190          $fordb = new stdClass();
 191          $fordb->criteriatype = $this->criteriatype;
 192          $fordb->badgeid = $this->badgeid;
 193          $fordb->description = $description;
 194          $fordb->descriptionformat = $descriptionformat;
 195          if ($this->id !== 0) {
 196              $fordb->id = $this->id;
 197              $DB->update_record('badge_criteria', $fordb);
 198          } else {
 199              // New record in DB, set aggregation to ALL by default.
 200              $fordb->method = BADGE_CRITERIA_AGGREGATION_ALL;
 201              $DB->insert_record('badge_criteria', $fordb);
 202          }
 203      }
 204  }