[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/admin/roles/classes/ -> existing_role_holders.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   * Existing user selector.
  19   *
  20   * @package    core_role
  21   * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * User selector subclass for the list of users who already have the role in
  29   * question on the assign roles page.
  30   */
  31  class core_role_existing_role_holders extends core_role_assign_user_selector_base {
  32  
  33      public function find_users($search) {
  34          global $DB;
  35  
  36          list($wherecondition, $params) = $this->search_sql($search, 'u');
  37          list($ctxcondition, $ctxparams) = $DB->get_in_or_equal($this->context->get_parent_context_ids(true), SQL_PARAMS_NAMED, 'ctx');
  38          $params = array_merge($params, $ctxparams);
  39          $params['roleid'] = $this->roleid;
  40  
  41          list($sort, $sortparams) = users_order_by_sql('u', $search, $this->accesscontext);
  42          $params = array_merge($params, $sortparams);
  43  
  44          $sql = "SELECT ra.id AS raid," . $this->required_fields_sql('u') . ",ra.contextid,ra.component
  45                    FROM {role_assignments} ra
  46                    JOIN {user} u ON u.id = ra.userid
  47                    JOIN {context} ctx ON ra.contextid = ctx.id
  48                   WHERE $wherecondition
  49                         AND ctx.id $ctxcondition
  50                         AND ra.roleid = :roleid
  51                ORDER BY ctx.depth DESC, ra.component, $sort";
  52          $contextusers = $DB->get_records_sql($sql, $params);
  53  
  54          // No users at all.
  55          if (empty($contextusers)) {
  56              return array();
  57          }
  58  
  59          // We have users. Out put them in groups by context depth.
  60          // To help the loop below, tack a dummy user on the end of the results
  61          // array, to trigger output of the last group.
  62          $dummyuser = new stdClass;
  63          $dummyuser->contextid = 0;
  64          $dummyuser->id = 0;
  65          $dummyuser->component = '';
  66          $contextusers[] = $dummyuser;
  67          $results = array(); // The results array we are building up.
  68          $doneusers = array(); // Ensures we only list each user at most once.
  69          $currentcontextid = $this->context->id;
  70          $currentgroup = array();
  71          foreach ($contextusers as $user) {
  72              if (isset($doneusers[$user->id])) {
  73                  continue;
  74              }
  75              $doneusers[$user->id] = 1;
  76              if ($user->contextid != $currentcontextid) {
  77                  // We have got to the end of the previous group. Add it to the results array.
  78                  if ($currentcontextid == $this->context->id) {
  79                      $groupname = $this->this_con_group_name($search, count($currentgroup));
  80                  } else {
  81                      $groupname = $this->parent_con_group_name($search, $currentcontextid);
  82                  }
  83                  $results[$groupname] = $currentgroup;
  84                  // Get ready for the next group.
  85                  $currentcontextid = $user->contextid;
  86                  $currentgroup = array();
  87              }
  88              // Add this user to the group we are building up.
  89              unset($user->contextid);
  90              if ($currentcontextid != $this->context->id) {
  91                  $user->disabled = true;
  92              }
  93              if ($user->component !== '') {
  94                  // Bad luck, you can tweak only manual role assignments.
  95                  $user->disabled = true;
  96              }
  97              unset($user->component);
  98              $currentgroup[$user->id] = $user;
  99          }
 100  
 101          return $results;
 102      }
 103  
 104      protected function this_con_group_name($search, $numusers) {
 105          if ($this->context->contextlevel == CONTEXT_SYSTEM) {
 106              // Special case in the System context.
 107              if ($search) {
 108                  return get_string('extusersmatching', 'core_role', $search);
 109              } else {
 110                  return get_string('extusers', 'core_role');
 111              }
 112          }
 113          $contexttype = context_helper::get_level_name($this->context->contextlevel);
 114          if ($search) {
 115              $a = new stdClass;
 116              $a->search = $search;
 117              $a->contexttype = $contexttype;
 118              if ($numusers) {
 119                  return get_string('usersinthisxmatching', 'core_role', $a);
 120              } else {
 121                  return get_string('noneinthisxmatching', 'core_role', $a);
 122              }
 123          } else {
 124              if ($numusers) {
 125                  return get_string('usersinthisx', 'core_role', $contexttype);
 126              } else {
 127                  return get_string('noneinthisx', 'core_role', $contexttype);
 128              }
 129          }
 130      }
 131  
 132      protected function parent_con_group_name($search, $contextid) {
 133          $context = context::instance_by_id($contextid);
 134          $contextname = $context->get_context_name(true, true);
 135          if ($search) {
 136              $a = new stdClass;
 137              $a->contextname = $contextname;
 138              $a->search = $search;
 139              return get_string('usersfrommatching', 'core_role', $a);
 140          } else {
 141              return get_string('usersfrom', 'core_role', $contextname);
 142          }
 143      }
 144  }


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