Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.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   * Course role filter
  19   *
  20   * @package   core_user
  21   * @category  user
  22   * @copyright 1999 Martin Dougiamas  http://dougiamas.com
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  require_once($CFG->dirroot .'/user/filters/lib.php');
  27  
  28  /**
  29   * User filter based on roles in a course identified by its shortname.
  30   * @copyright 1999 Martin Dougiamas  http://dougiamas.com
  31   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class user_filter_courserole extends user_filter_type {
  34      /**
  35       * Constructor
  36       * @param string $name the name of the filter instance
  37       * @param string $label the label of the filter instance
  38       * @param boolean $advanced advanced form element flag
  39       */
  40      public function __construct($name, $label, $advanced) {
  41          parent::__construct($name, $label, $advanced);
  42      }
  43  
  44      /**
  45       * Old syntax of class constructor. Deprecated in PHP7.
  46       *
  47       * @deprecated since Moodle 3.1
  48       */
  49      public function user_filter_courserole($name, $label, $advanced) {
  50          debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
  51          self::__construct($name, $label, $advanced);
  52      }
  53  
  54      /**
  55       * Returns an array of available roles
  56       * @return array of availble roles
  57       */
  58      public function get_roles() {
  59          $context = context_system::instance();
  60          $roles = array(0 => get_string('anyrole', 'filters')) + get_default_enrol_roles($context);
  61          return $roles;
  62      }
  63  
  64      /**
  65       * Returns an array of course categories
  66       * @return array of course categories
  67       */
  68      public function get_course_categories() {
  69          return array(0 => get_string('anycategory', 'filters')) + core_course_category::make_categories_list();
  70      }
  71  
  72      /**
  73       * Adds controls specific to this filter in the form.
  74       * @param moodleform $mform a MoodleForm object to setup
  75       */
  76      public function setupForm(&$mform) {
  77          $objs = array();
  78          $objs['role'] = $mform->createElement('select', $this->_name .'_rl', null, $this->get_roles());
  79          $objs['role']->setLabel(get_string('courserole', 'filters'));
  80          $objs['category'] = $mform->createElement('select', $this->_name .'_ct', null, $this->get_course_categories());
  81          $objs['category']->setLabel(get_string('coursecategory', 'filters'));
  82          $objs['value'] = $mform->createElement('text', $this->_name, null);
  83          $objs['value']->setLabel(get_string('coursevalue', 'filters'));
  84          $grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false);
  85          $mform->setType($this->_name, PARAM_TEXT);
  86          if ($this->_advanced) {
  87              $mform->setAdvanced($this->_name.'_grp');
  88          }
  89      }
  90  
  91      /**
  92       * Retrieves data from the form data
  93       * @param stdClass $formdata data submited with the form
  94       * @return mixed array filter data or false when filter not set
  95       */
  96      public function check_data($formdata) {
  97          $field    = $this->_name;
  98          $role     = $field .'_rl';
  99          $category = $field .'_ct';
 100  
 101          if (property_exists($formdata, $field)) {
 102              if (empty($formdata->$field) and empty($formdata->$role) and empty($formdata->$category)) {
 103                  // Nothing selected.
 104                  return false;
 105              }
 106              return array('value'      => (string)$formdata->$field,
 107                           'roleid'     => (int)$formdata->$role,
 108                           'categoryid' => (int)$formdata->$category);
 109          }
 110          return false;
 111      }
 112  
 113      /**
 114       * Returns the condition to be used with SQL where
 115       * @param array $data filter settings
 116       * @return array sql string and $params
 117       */
 118      public function get_sql_filter($data) {
 119          global $CFG, $DB;
 120          static $counter = 0;
 121          $pref = 'ex_courserole'.($counter++).'_';
 122  
 123          $value      = $data['value'];
 124          $roleid     = $data['roleid'];
 125          $categoryid = $data['categoryid'];
 126  
 127          $params = array();
 128  
 129          if (empty($value) and empty($roleid) and empty($categoryid)) {
 130              return array('', $params);
 131          }
 132  
 133          $where = "b.contextlevel=50";
 134          if ($roleid) {
 135              $where .= " AND a.roleid = :{$pref}roleid";
 136              $params[$pref.'roleid'] = $roleid;
 137          }
 138          if ($categoryid) {
 139              $where .= " AND c.category = :{$pref}categoryid";
 140              $params[$pref.'categoryid'] = $categoryid;
 141          }
 142          if ($value) {
 143              $where .= " AND c.shortname = :{$pref}course";
 144              $params[$pref.'course'] = $value;
 145          }
 146          return array("id IN (SELECT userid
 147                                 FROM {role_assignments} a
 148                           INNER JOIN {context} b ON a.contextid=b.id
 149                           INNER JOIN {course} c ON b.instanceid=c.id
 150                                WHERE $where)", $params);
 151      }
 152  
 153      /**
 154       * Returns a human friendly description of the filter used as label.
 155       * @param array $data filter settings
 156       * @return string active filter label
 157       */
 158      public function get_label($data) {
 159          global $DB;
 160  
 161          $value      = $data['value'];
 162          $roleid     = $data['roleid'];
 163          $categoryid = $data['categoryid'];
 164  
 165          $a = new stdClass();
 166          $a->label = $this->_label;
 167  
 168          if ($roleid) {
 169              $role = $DB->get_record('role', array('id' => $roleid));
 170              $a->rolename = '"'.role_get_name($role).'"';
 171          } else {
 172              $a->rolename = get_string('anyrole', 'filters');
 173          }
 174  
 175          if ($categoryid) {
 176              $catname = $DB->get_field('course_categories', 'name', array('id' => $categoryid));
 177              $a->categoryname = '"'.format_string($catname).'"';
 178          } else {
 179              $a->categoryname = get_string('anycategory', 'filters');
 180          }
 181  
 182          if ($value) {
 183              $a->coursename = '"'.s($value).'"';
 184              if (!$DB->record_exists('course', array('shortname' => $value))) {
 185                  return '<span class="notifyproblem">'.get_string('courserolelabelerror', 'filters', $a).'</span>';
 186              }
 187          } else {
 188              $a->coursename = get_string('anycourse', 'filters');
 189          }
 190  
 191          return get_string('courserolelabel', 'filters', $a);
 192      }
 193  }