[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/user/filters/ -> courserole.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   * 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 user_filter_courserole($name, $label, $advanced) {
  41          parent::user_filter_type($name, $label, $advanced);
  42      }
  43  
  44      /**
  45       * Returns an array of available roles
  46       * @return array of availble roles
  47       */
  48      public function get_roles() {
  49          $context = context_system::instance();
  50          $roles = array(0 => get_string('anyrole', 'filters')) + get_default_enrol_roles($context);
  51          return $roles;
  52      }
  53  
  54      /**
  55       * Returns an array of course categories
  56       * @return array of course categories
  57       */
  58      public function get_course_categories() {
  59          global $CFG;
  60          require_once($CFG->libdir.'/coursecatlib.php');
  61          return array(0 => get_string('anycategory', 'filters')) + coursecat::make_categories_list();
  62      }
  63  
  64      /**
  65       * Adds controls specific to this filter in the form.
  66       * @param moodleform $mform a MoodleForm object to setup
  67       */
  68      public function setupForm(&$mform) {
  69          $objs = array();
  70          $objs[] = $mform->createElement('select', $this->_name .'_rl', null, $this->get_roles());
  71          $objs[] = $mform->createElement('select', $this->_name .'_ct', null, $this->get_course_categories());
  72          $objs[] = $mform->createElement('text', $this->_name, null);
  73          $grp =& $mform->addElement('group', $this->_name.'_grp', $this->_label, $objs, '', false);
  74          $mform->setType($this->_name, PARAM_TEXT);
  75          if ($this->_advanced) {
  76              $mform->setAdvanced($this->_name.'_grp');
  77          }
  78      }
  79  
  80      /**
  81       * Retrieves data from the form data
  82       * @param stdClass $formdata data submited with the form
  83       * @return mixed array filter data or false when filter not set
  84       */
  85      public function check_data($formdata) {
  86          $field    = $this->_name;
  87          $role     = $field .'_rl';
  88          $category = $field .'_ct';
  89  
  90          if (array_key_exists($field, $formdata)) {
  91              if (empty($formdata->$field) and empty($formdata->$role) and empty($formdata->$category)) {
  92                  // Nothing selected.
  93                  return false;
  94              }
  95              return array('value'      => (string)$formdata->$field,
  96                           'roleid'     => (int)$formdata->$role,
  97                           'categoryid' => (int)$formdata->$category);
  98          }
  99          return false;
 100      }
 101  
 102      /**
 103       * Returns the condition to be used with SQL where
 104       * @param array $data filter settings
 105       * @return array sql string and $params
 106       */
 107      public function get_sql_filter($data) {
 108          global $CFG, $DB;
 109          static $counter = 0;
 110          $pref = 'ex_courserole'.($counter++).'_';
 111  
 112          $value      = $data['value'];
 113          $roleid     = $data['roleid'];
 114          $categoryid = $data['categoryid'];
 115  
 116          $params = array();
 117  
 118          if (empty($value) and empty($roleid) and empty($categoryid)) {
 119              return array('', $params);
 120          }
 121  
 122          $where = "b.contextlevel=50";
 123          if ($roleid) {
 124              $where .= " AND a.roleid = :{$pref}roleid";
 125              $params[$pref.'roleid'] = $roleid;
 126          }
 127          if ($categoryid) {
 128              $where .= " AND c.category = :{$pref}categoryid";
 129              $params[$pref.'categoryid'] = $categoryid;
 130          }
 131          if ($value) {
 132              $where .= " AND c.shortname = :{$pref}course";
 133              $params[$pref.'course'] = $value;
 134          }
 135          return array("id IN (SELECT userid
 136                                 FROM {role_assignments} a
 137                           INNER JOIN {context} b ON a.contextid=b.id
 138                           INNER JOIN {course} c ON b.instanceid=c.id
 139                                WHERE $where)", $params);
 140      }
 141  
 142      /**
 143       * Returns a human friendly description of the filter used as label.
 144       * @param array $data filter settings
 145       * @return string active filter label
 146       */
 147      public function get_label($data) {
 148          global $DB;
 149  
 150          $value      = $data['value'];
 151          $roleid     = $data['roleid'];
 152          $categoryid = $data['categoryid'];
 153  
 154          $a = new stdClass();
 155          $a->label = $this->_label;
 156  
 157          if ($roleid) {
 158              $role = $DB->get_record('role', array('id' => $roleid));
 159              $a->rolename = '"'.role_get_name($role).'"';
 160          } else {
 161              $a->rolename = get_string('anyrole', 'filters');
 162          }
 163  
 164          if ($categoryid) {
 165              $catname = $DB->get_field('course_categories', 'name', array('id' => $categoryid));
 166              $a->categoryname = '"'.format_string($catname).'"';
 167          } else {
 168              $a->categoryname = get_string('anycategory', 'filters');
 169          }
 170  
 171          if ($value) {
 172              $a->coursename = '"'.s($value).'"';
 173              if (!$DB->record_exists('course', array('shortname' => $value))) {
 174                  return '<span class="notifyproblem">'.get_string('courserolelabelerror', 'filters', $a).'</span>';
 175              }
 176          } else {
 177              $a->coursename = get_string('anycourse', 'filters');
 178          }
 179  
 180          return get_string('courserolelabel', 'filters', $a);
 181      }
 182  }


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