Search moodle.org's
Developer Documentation


/admin/ -> user.php (source)
   1  <?php
   2  
   3      require_once('../config.php');
   4      require_once($CFG->libdir.'/adminlib.php');
   5      require_once($CFG->libdir.'/authlib.php');
   6      require_once($CFG->dirroot.'/user/filters/lib.php');
   7      require_once($CFG->dirroot.'/user/lib.php');
   8  
   9      $delete       = optional_param('delete', 0, PARAM_INT);
  10      $confirm      = optional_param('confirm', '', PARAM_ALPHANUM);   //md5 confirmation hash
  11      $confirmuser  = optional_param('confirmuser', 0, PARAM_INT);
  12      $sort         = optional_param('sort', 'name', PARAM_ALPHANUM);
  13      $dir          = optional_param('dir', 'ASC', PARAM_ALPHA);
  14      $page         = optional_param('page', 0, PARAM_INT);
  15      $perpage      = optional_param('perpage', 30, PARAM_INT);        // how many per page
  16      $ru           = optional_param('ru', '2', PARAM_INT);            // show remote users
  17      $lu           = optional_param('lu', '2', PARAM_INT);            // show local users
  18      $acl          = optional_param('acl', '0', PARAM_INT);           // id of user to tweak mnet ACL (requires $access)
  19      $suspend      = optional_param('suspend', 0, PARAM_INT);
  20      $unsuspend    = optional_param('unsuspend', 0, PARAM_INT);
  21      $unlock       = optional_param('unlock', 0, PARAM_INT);
  22  
  23      admin_externalpage_setup('editusers');
  24  
  25      $sitecontext = context_system::instance();
  26      $site = get_site();
  27  
  28      if (!has_capability('moodle/user:update', $sitecontext) and !has_capability('moodle/user:delete', $sitecontext)) {
  29          print_error('nopermissions', 'error', '', 'edit/delete users');
  30      }
  31  
  32      $stredit   = get_string('edit');
  33      $strdelete = get_string('delete');
  34      $strdeletecheck = get_string('deletecheck');
  35      $strshowallusers = get_string('showallusers');
  36      $strsuspend = get_string('suspenduser', 'admin');
  37      $strunsuspend = get_string('unsuspenduser', 'admin');
  38      $strunlock = get_string('unlockaccount', 'admin');
  39      $strconfirm = get_string('confirm');
  40  
  41      if (empty($CFG->loginhttps)) {
  42          $securewwwroot = $CFG->wwwroot;
  43      } else {
  44          $securewwwroot = str_replace('http:','https:',$CFG->wwwroot);
  45      }
  46  
  47      $returnurl = new moodle_url('/admin/user.php', array('sort' => $sort, 'dir' => $dir, 'perpage' => $perpage, 'page'=>$page));
  48  
  49      // The $user variable is also used outside of these if statements.
  50      $user = null;
  51      if ($confirmuser and confirm_sesskey()) {
  52          require_capability('moodle/user:update', $sitecontext);
  53          if (!$user = $DB->get_record('user', array('id'=>$confirmuser, 'mnethostid'=>$CFG->mnet_localhost_id))) {
  54              print_error('nousers');
  55          }
  56  
  57          $auth = get_auth_plugin($user->auth);
  58  
  59          $result = $auth->user_confirm($user->username, $user->secret);
  60  
  61          if ($result == AUTH_CONFIRM_OK or $result == AUTH_CONFIRM_ALREADY) {
  62              redirect($returnurl);
  63          } else {
  64              echo $OUTPUT->header();
  65              redirect($returnurl, get_string('usernotconfirmed', '', fullname($user, true)));
  66          }
  67  
  68      } else if ($delete and confirm_sesskey()) {              // Delete a selected user, after confirmation
  69          require_capability('moodle/user:delete', $sitecontext);
  70  
  71          $user = $DB->get_record('user', array('id'=>$delete, 'mnethostid'=>$CFG->mnet_localhost_id), '*', MUST_EXIST);
  72  
  73          if (is_siteadmin($user->id)) {
  74              print_error('useradminodelete', 'error');
  75          }
  76  
  77          if ($confirm != md5($delete)) {
  78              echo $OUTPUT->header();
  79              $fullname = fullname($user, true);
  80              echo $OUTPUT->heading(get_string('deleteuser', 'admin'));
  81  
  82              $optionsyes = array('delete'=>$delete, 'confirm'=>md5($delete), 'sesskey'=>sesskey());
  83              $deleteurl = new moodle_url($returnurl, $optionsyes);
  84              $deletebutton = new single_button($deleteurl, get_string('delete'), 'post');
  85  
  86              echo $OUTPUT->confirm(get_string('deletecheckfull', '', "'$fullname'"), $deletebutton, $returnurl);
  87              echo $OUTPUT->footer();
  88              die;
  89          } else if (data_submitted() and !$user->deleted) {
  90              if (delete_user($user)) {
  91                  \core\session\manager::gc(); // Remove stale sessions.
  92                  redirect($returnurl);
  93              } else {
  94                  \core\session\manager::gc(); // Remove stale sessions.
  95                  echo $OUTPUT->header();
  96                  echo $OUTPUT->notification($returnurl, get_string('deletednot', '', fullname($user, true)));
  97              }
  98          }
  99      } else if ($acl and confirm_sesskey()) {
 100          if (!has_capability('moodle/user:update', $sitecontext)) {
 101              print_error('nopermissions', 'error', '', 'modify the NMET access control list');
 102          }
 103          if (!$user = $DB->get_record('user', array('id'=>$acl))) {
 104              print_error('nousers', 'error');
 105          }
 106          if (!is_mnet_remote_user($user)) {
 107              print_error('usermustbemnet', 'error');
 108          }
 109          $accessctrl = strtolower(required_param('accessctrl', PARAM_ALPHA));
 110          if ($accessctrl != 'allow' and $accessctrl != 'deny') {
 111              print_error('invalidaccessparameter', 'error');
 112          }
 113          $aclrecord = $DB->get_record('mnet_sso_access_control', array('username'=>$user->username, 'mnet_host_id'=>$user->mnethostid));
 114          if (empty($aclrecord)) {
 115              $aclrecord = new stdClass();
 116              $aclrecord->mnet_host_id = $user->mnethostid;
 117              $aclrecord->username = $user->username;
 118              $aclrecord->accessctrl = $accessctrl;
 119              $DB->insert_record('mnet_sso_access_control', $aclrecord);
 120          } else {
 121              $aclrecord->accessctrl = $accessctrl;
 122              $DB->update_record('mnet_sso_access_control', $aclrecord);
 123          }
 124          $mnethosts = $DB->get_records('mnet_host', null, 'id', 'id,wwwroot,name');
 125          redirect($returnurl);
 126  
 127      } else if ($suspend and confirm_sesskey()) {
 128          require_capability('moodle/user:update', $sitecontext);
 129  
 130          if ($user = $DB->get_record('user', array('id'=>$suspend, 'mnethostid'=>$CFG->mnet_localhost_id, 'deleted'=>0))) {
 131              if (!is_siteadmin($user) and $USER->id != $user->id and $user->suspended != 1) {
 132                  $user->suspended = 1;
 133                  // Force logout.
 134                  \core\session\manager::kill_user_sessions($user->id);
 135                  user_update_user($user, false);
 136              }
 137          }
 138          redirect($returnurl);
 139  
 140      } else if ($unsuspend and confirm_sesskey()) {
 141          require_capability('moodle/user:update', $sitecontext);
 142  
 143          if ($user = $DB->get_record('user', array('id'=>$unsuspend, 'mnethostid'=>$CFG->mnet_localhost_id, 'deleted'=>0))) {
 144              if ($user->suspended != 0) {
 145                  $user->suspended = 0;
 146                  user_update_user($user, false);
 147              }
 148          }
 149          redirect($returnurl);
 150  
 151      } else if ($unlock and confirm_sesskey()) {
 152          require_capability('moodle/user:update', $sitecontext);
 153  
 154          if ($user = $DB->get_record('user', array('id'=>$unlock, 'mnethostid'=>$CFG->mnet_localhost_id, 'deleted'=>0))) {
 155              login_unlock_account($user);
 156          }
 157          redirect($returnurl);
 158      }
 159  
 160      // create the user filter form
 161      $ufiltering = new user_filtering();
 162      echo $OUTPUT->header();
 163  
 164      // Carry on with the user listing
 165      $context = context_system::instance();
 166      $extracolumns = get_extra_user_fields($context);
 167      // Get all user name fields as an array.
 168      $allusernamefields = get_all_user_name_fields(false, null, null, null, true);
 169      $columns = array_merge($allusernamefields, $extracolumns, array('city', 'country', 'lastaccess'));
 170  
 171      foreach ($columns as $column) {
 172          $string[$column] = get_user_field_name($column);
 173          if ($sort != $column) {
 174              $columnicon = "";
 175              if ($column == "lastaccess") {
 176                  $columndir = "DESC";
 177              } else {
 178                  $columndir = "ASC";
 179              }
 180          } else {
 181              $columndir = $dir == "ASC" ? "DESC":"ASC";
 182              if ($column == "lastaccess") {
 183                  $columnicon = ($dir == "ASC") ? "sort_desc" : "sort_asc";
 184              } else {
 185                  $columnicon = ($dir == "ASC") ? "sort_asc" : "sort_desc";
 186              }
 187              $columnicon = "<img class='iconsort' src=\"" . $OUTPUT->pix_url('t/' . $columnicon) . "\" alt=\"\" />";
 188  
 189          }
 190          $$column = "<a href=\"user.php?sort=$column&amp;dir=$columndir\">".$string[$column]."</a>$columnicon";
 191      }
 192  
 193      // We need to check that alternativefullnameformat is not set to '' or language.
 194      // We don't need to check the fullnamedisplay setting here as the fullname function call further down has
 195      // the override parameter set to true.
 196      $fullnamesetting = $CFG->alternativefullnameformat;
 197      // If we are using language or it is empty, then retrieve the default user names of just 'firstname' and 'lastname'.
 198      if ($fullnamesetting == 'language' || empty($fullnamesetting)) {
 199          // Set $a variables to return 'firstname' and 'lastname'.
 200          $a = new stdClass();
 201          $a->firstname = 'firstname';
 202          $a->lastname = 'lastname';
 203          // Getting the fullname display will ensure that the order in the language file is maintained.
 204          $fullnamesetting = get_string('fullnamedisplay', null, $a);
 205      }
 206  
 207      // Order in string will ensure that the name columns are in the correct order.
 208      $usernames = order_in_string($allusernamefields, $fullnamesetting);
 209      $fullnamedisplay = array();
 210      foreach ($usernames as $name) {
 211          // Use the link from $$column for sorting on the user's name.
 212          $fullnamedisplay[] = ${$name};
 213      }
 214      // All of the names are in one column. Put them into a string and separate them with a /.
 215      $fullnamedisplay = implode(' / ', $fullnamedisplay);
 216      // If $sort = name then it is the default for the setting and we should use the first name to sort by.
 217      if ($sort == "name") {
 218          // Use the first item in the array.
 219          $sort = reset($usernames);
 220      }
 221  
 222      list($extrasql, $params) = $ufiltering->get_sql_filter();
 223      $users = get_users_listing($sort, $dir, $page*$perpage, $perpage, '', '', '',
 224              $extrasql, $params, $context);
 225      $usercount = get_users(false);
 226      $usersearchcount = get_users(false, '', false, null, "", '', '', '', '', '*', $extrasql, $params);
 227  
 228      if ($extrasql !== '') {
 229          echo $OUTPUT->heading("$usersearchcount / $usercount ".get_string('users'));
 230          $usercount = $usersearchcount;
 231      } else {
 232          echo $OUTPUT->heading("$usercount ".get_string('users'));
 233      }
 234  
 235      $strall = get_string('all');
 236  
 237      $baseurl = new moodle_url('/admin/user.php', array('sort' => $sort, 'dir' => $dir, 'perpage' => $perpage));
 238      echo $OUTPUT->paging_bar($usercount, $page, $perpage, $baseurl);
 239  
 240      flush();
 241  
 242  
 243      if (!$users) {
 244          $match = array();
 245          echo $OUTPUT->heading(get_string('nousersfound'));
 246  
 247          $table = NULL;
 248  
 249      } else {
 250  
 251          $countries = get_string_manager()->get_list_of_countries(false);
 252          if (empty($mnethosts)) {
 253              $mnethosts = $DB->get_records('mnet_host', null, 'id', 'id,wwwroot,name');
 254          }
 255  
 256          foreach ($users as $key => $user) {
 257              if (isset($countries[$user->country])) {
 258                  $users[$key]->country = $countries[$user->country];
 259              }
 260          }
 261          if ($sort == "country") {  // Need to resort by full country name, not code
 262              foreach ($users as $user) {
 263                  $susers[$user->id] = $user->country;
 264              }
 265              asort($susers);
 266              foreach ($susers as $key => $value) {
 267                  $nusers[] = $users[$key];
 268              }
 269              $users = $nusers;
 270          }
 271  
 272          $table = new html_table();
 273          $table->head = array ();
 274          $table->colclasses = array();
 275          $table->head[] = $fullnamedisplay;
 276          $table->attributes['class'] = 'admintable generaltable';
 277          foreach ($extracolumns as $field) {
 278              $table->head[] = ${$field};
 279          }
 280          $table->head[] = $city;
 281          $table->head[] = $country;
 282          $table->head[] = $lastaccess;
 283          $table->head[] = get_string('edit');
 284          $table->colclasses[] = 'centeralign';
 285          $table->head[] = "";
 286          $table->colclasses[] = 'centeralign';
 287  
 288          $table->id = "users";
 289          foreach ($users as $user) {
 290              $buttons = array();
 291              $lastcolumn = '';
 292  
 293              // delete button
 294              if (has_capability('moodle/user:delete', $sitecontext)) {
 295                  if (is_mnet_remote_user($user) or $user->id == $USER->id or is_siteadmin($user)) {
 296                      // no deleting of self, mnet accounts or admins allowed
 297                  } else {
 298                      $buttons[] = html_writer::link(new moodle_url($returnurl, array('delete'=>$user->id, 'sesskey'=>sesskey())), html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/delete'), 'alt'=>$strdelete, 'class'=>'iconsmall')), array('title'=>$strdelete));
 299                  }
 300              }
 301  
 302              // suspend button
 303              if (has_capability('moodle/user:update', $sitecontext)) {
 304                  if (is_mnet_remote_user($user)) {
 305                      // mnet users have special access control, they can not be deleted the standard way or suspended
 306                      $accessctrl = 'allow';
 307                      if ($acl = $DB->get_record('mnet_sso_access_control', array('username'=>$user->username, 'mnet_host_id'=>$user->mnethostid))) {
 308                          $accessctrl = $acl->accessctrl;
 309                      }
 310                      $changeaccessto = ($accessctrl == 'deny' ? 'allow' : 'deny');
 311                      $buttons[] = " (<a href=\"?acl={$user->id}&amp;accessctrl=$changeaccessto&amp;sesskey=".sesskey()."\">".get_string($changeaccessto, 'mnet') . " access</a>)";
 312  
 313                  } else {
 314                      if ($user->suspended) {
 315                          $buttons[] = html_writer::link(new moodle_url($returnurl, array('unsuspend'=>$user->id, 'sesskey'=>sesskey())), html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/show'), 'alt'=>$strunsuspend, 'class'=>'iconsmall')), array('title'=>$strunsuspend));
 316                      } else {
 317                          if ($user->id == $USER->id or is_siteadmin($user)) {
 318                              // no suspending of admins or self!
 319                          } else {
 320                              $buttons[] = html_writer::link(new moodle_url($returnurl, array('suspend'=>$user->id, 'sesskey'=>sesskey())), html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/hide'), 'alt'=>$strsuspend, 'class'=>'iconsmall')), array('title'=>$strsuspend));
 321                          }
 322                      }
 323  
 324                      if (login_is_lockedout($user)) {
 325                          $buttons[] = html_writer::link(new moodle_url($returnurl, array('unlock'=>$user->id, 'sesskey'=>sesskey())), html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/unlock'), 'alt'=>$strunlock, 'class'=>'iconsmall')), array('title'=>$strunlock));
 326                      }
 327                  }
 328              }
 329  
 330              // edit button
 331              if (has_capability('moodle/user:update', $sitecontext)) {
 332                  // prevent editing of admins by non-admins
 333                  if (is_siteadmin($USER) or !is_siteadmin($user)) {
 334                      $buttons[] = html_writer::link(new moodle_url($securewwwroot.'/user/editadvanced.php', array('id'=>$user->id, 'course'=>$site->id)), html_writer::empty_tag('img', array('src'=>$OUTPUT->pix_url('t/edit'), 'alt'=>$stredit, 'class'=>'iconsmall')), array('title'=>$stredit));
 335                  }
 336              }
 337  
 338              // the last column - confirm or mnet info
 339              if (is_mnet_remote_user($user)) {
 340                  // all mnet users are confirmed, let's print just the name of the host there
 341                  if (isset($mnethosts[$user->mnethostid])) {
 342                      $lastcolumn = get_string($accessctrl, 'mnet').': '.$mnethosts[$user->mnethostid]->name;
 343                  } else {
 344                      $lastcolumn = get_string($accessctrl, 'mnet');
 345                  }
 346  
 347              } else if ($user->confirmed == 0) {
 348                  if (has_capability('moodle/user:update', $sitecontext)) {
 349                      $lastcolumn = html_writer::link(new moodle_url($returnurl, array('confirmuser'=>$user->id, 'sesskey'=>sesskey())), $strconfirm);
 350                  } else {
 351                      $lastcolumn = "<span class=\"dimmed_text\">".get_string('confirm')."</span>";
 352                  }
 353              }
 354  
 355              if ($user->lastaccess) {
 356                  $strlastaccess = format_time(time() - $user->lastaccess);
 357              } else {
 358                  $strlastaccess = get_string('never');
 359              }
 360              $fullname = fullname($user, true);
 361  
 362              $row = array ();
 363              $row[] = "<a href=\"../user/view.php?id=$user->id&amp;course=$site->id\">$fullname</a>";
 364              foreach ($extracolumns as $field) {
 365                  $row[] = $user->{$field};
 366              }
 367              $row[] = $user->city;
 368              $row[] = $user->country;
 369              $row[] = $strlastaccess;
 370              if ($user->suspended) {
 371                  foreach ($row as $k=>$v) {
 372                      $row[$k] = html_writer::tag('span', $v, array('class'=>'usersuspended'));
 373                  }
 374              }
 375              $row[] = implode(' ', $buttons);
 376              $row[] = $lastcolumn;
 377              $table->data[] = $row;
 378          }
 379      }
 380  
 381      // add filters
 382      $ufiltering->display_add();
 383      $ufiltering->display_active();
 384  
 385      if (!empty($table)) {
 386          echo html_writer::start_tag('div', array('class'=>'no-overflow'));
 387          echo html_writer::table($table);
 388          echo html_writer::end_tag('div');
 389          echo $OUTPUT->paging_bar($usercount, $page, $perpage, $baseurl);
 390      }
 391      if (has_capability('moodle/user:create', $sitecontext)) {
 392          $url = new moodle_url($securewwwroot . '/user/editadvanced.php', array('id' => -1));
 393          echo $OUTPUT->single_button($url, get_string('addnewuser'), 'get');
 394      }
 395  
 396      echo $OUTPUT->footer();

Search This Site: