Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/admin/user.php
Go to the documentation of this file.
00001 <?php
00002 
00003     require_once('../config.php');
00004     require_once($CFG->libdir.'/adminlib.php');
00005     require_once($CFG->dirroot.'/user/filters/lib.php');
00006 
00007     $delete       = optional_param('delete', 0, PARAM_INT);
00008     $confirm      = optional_param('confirm', '', PARAM_ALPHANUM);   //md5 confirmation hash
00009     $confirmuser  = optional_param('confirmuser', 0, PARAM_INT);
00010     $sort         = optional_param('sort', 'name', PARAM_ALPHANUM);
00011     $dir          = optional_param('dir', 'ASC', PARAM_ALPHA);
00012     $page         = optional_param('page', 0, PARAM_INT);
00013     $perpage      = optional_param('perpage', 30, PARAM_INT);        // how many per page
00014     $ru           = optional_param('ru', '2', PARAM_INT);            // show remote users
00015     $lu           = optional_param('lu', '2', PARAM_INT);            // show local users
00016     $acl          = optional_param('acl', '0', PARAM_INT);           // id of user to tweak mnet ACL (requires $access)
00017     $suspend      = optional_param('suspend', 0, PARAM_INT);
00018     $unsuspend    = optional_param('unsuspend', 0, PARAM_INT);
00019 
00020     admin_externalpage_setup('editusers');
00021 
00022     $sitecontext = get_context_instance(CONTEXT_SYSTEM);
00023     $site = get_site();
00024 
00025     if (!has_capability('moodle/user:update', $sitecontext) and !has_capability('moodle/user:delete', $sitecontext)) {
00026         print_error('nopermissions', 'error', '', 'edit/delete users');
00027     }
00028 
00029     $stredit   = get_string('edit');
00030     $strdelete = get_string('delete');
00031     $strdeletecheck = get_string('deletecheck');
00032     $strshowallusers = get_string('showallusers');
00033     $strsuspend = get_string('suspenduser', 'admin');
00034     $strunsuspend = get_string('unsuspenduser', 'admin');
00035     $strconfirm = get_string('confirm');
00036 
00037     if (empty($CFG->loginhttps)) {
00038         $securewwwroot = $CFG->wwwroot;
00039     } else {
00040         $securewwwroot = str_replace('http:','https:',$CFG->wwwroot);
00041     }
00042 
00043     $returnurl = new moodle_url('/admin/user.php', array('sort' => $sort, 'dir' => $dir, 'perpage' => $perpage, 'page'=>$page));
00044 
00045     if ($confirmuser and confirm_sesskey()) {
00046         require_capability('moodle/user:update', $sitecontext);
00047         if (!$user = $DB->get_record('user', array('id'=>$confirmuser, 'mnethostid'=>$CFG->mnet_localhost_id))) {
00048             print_error('nousers');
00049         }
00050 
00051         $auth = get_auth_plugin($user->auth);
00052 
00053         $result = $auth->user_confirm($user->username, $user->secret);
00054 
00055         if ($result == AUTH_CONFIRM_OK or $result == AUTH_CONFIRM_ALREADY) {
00056             redirect($returnurl);
00057         } else {
00058             echo $OUTPUT->header();
00059             redirect($returnurl, get_string('usernotconfirmed', '', fullname($user, true)));
00060         }
00061 
00062     } else if ($delete and confirm_sesskey()) {              // Delete a selected user, after confirmation
00063         require_capability('moodle/user:delete', $sitecontext);
00064 
00065         $user = $DB->get_record('user', array('id'=>$delete, 'mnethostid'=>$CFG->mnet_localhost_id), '*', MUST_EXIST);
00066 
00067         if (is_siteadmin($user->id)) {
00068             print_error('useradminodelete', 'error');
00069         }
00070 
00071         if ($confirm != md5($delete)) {
00072             echo $OUTPUT->header();
00073             $fullname = fullname($user, true);
00074             echo $OUTPUT->heading(get_string('deleteuser', 'admin'));
00075             $optionsyes = array('delete'=>$delete, 'confirm'=>md5($delete), 'sesskey'=>sesskey());
00076             echo $OUTPUT->confirm(get_string('deletecheckfull', '', "'$fullname'"), new moodle_url($returnurl, $optionsyes), $returnurl);
00077             echo $OUTPUT->footer();
00078             die;
00079         } else if (data_submitted() and !$user->deleted) {
00080             if (delete_user($user)) {
00081                 session_gc(); // remove stale sessions
00082                 redirect($returnurl);
00083             } else {
00084                 session_gc(); // remove stale sessions
00085                 echo $OUTPUT->header();
00086                 echo $OUTPUT->notification($returnurl, get_string('deletednot', '', fullname($user, true)));
00087             }
00088         }
00089     } else if ($acl and confirm_sesskey()) {
00090         if (!has_capability('moodle/user:update', $sitecontext)) {
00091             print_error('nopermissions', 'error', '', 'modify the NMET access control list');
00092         }
00093         if (!$user = $DB->get_record('user', array('id'=>$acl))) {
00094             print_error('nousers', 'error');
00095         }
00096         if (!is_mnet_remote_user($user)) {
00097             print_error('usermustbemnet', 'error');
00098         }
00099         $accessctrl = strtolower(required_param('accessctrl', PARAM_ALPHA));
00100         if ($accessctrl != 'allow' and $accessctrl != 'deny') {
00101             print_error('invalidaccessparameter', 'error');
00102         }
00103         $aclrecord = $DB->get_record('mnet_sso_access_control', array('username'=>$user->username, 'mnet_host_id'=>$user->mnethostid));
00104         if (empty($aclrecord)) {
00105             $aclrecord = new stdClass();
00106             $aclrecord->mnet_host_id = $user->mnethostid;
00107             $aclrecord->username = $user->username;
00108             $aclrecord->accessctrl = $accessctrl;
00109             $DB->insert_record('mnet_sso_access_control', $aclrecord);
00110         } else {
00111             $aclrecord->accessctrl = $accessctrl;
00112             $DB->update_record('mnet_sso_access_control', $aclrecord);
00113         }
00114         $mnethosts = $DB->get_records('mnet_host', null, 'id', 'id,wwwroot,name');
00115         redirect($returnurl);
00116 
00117     } else if ($suspend and confirm_sesskey()) {
00118         require_capability('moodle/user:update', $sitecontext);
00119 
00120         if ($user = $DB->get_record('user', array('id'=>$suspend, 'mnethostid'=>$CFG->mnet_localhost_id, 'deleted'=>0))) {
00121             if (!is_siteadmin($user) and $USER->id != $user->id and $user->suspended != 1) {
00122                 $user->suspended = 1;
00123                 $user->timemodified = time();
00124                 $DB->set_field('user', 'suspended', $user->suspended, array('id'=>$user->id));
00125                 $DB->set_field('user', 'timemodified', $user->timemodified, array('id'=>$user->id));
00126                 // force logout
00127                 session_kill_user($user->id);
00128                 events_trigger('user_updated', $user);
00129             }
00130         }
00131         redirect($returnurl);
00132 
00133     } else if ($unsuspend and confirm_sesskey()) {
00134         require_capability('moodle/user:update', $sitecontext);
00135 
00136         if ($user = $DB->get_record('user', array('id'=>$unsuspend, 'mnethostid'=>$CFG->mnet_localhost_id, 'deleted'=>0))) {
00137             if ($user->suspended != 0) {
00138                 $user->suspended = 0;
00139                 $user->timemodified = time();
00140                 $DB->set_field('user', 'suspended', $user->suspended, array('id'=>$user->id));
00141                 $DB->set_field('user', 'timemodified', $user->timemodified, array('id'=>$user->id));
00142                 events_trigger('user_updated', $user);
00143             }
00144         }
00145         redirect($returnurl);
00146     }
00147 
00148     // create the user filter form
00149     $ufiltering = new user_filtering();
00150     echo $OUTPUT->header();
00151 
00152     // Carry on with the user listing
00153     $context = context_system::instance();
00154     $extracolumns = get_extra_user_fields($context);
00155     $columns = array_merge(array('firstname', 'lastname'), $extracolumns,
00156             array('city', 'country', 'lastaccess'));
00157 
00158     foreach ($columns as $column) {
00159         $string[$column] = get_user_field_name($column);
00160         if ($sort != $column) {
00161             $columnicon = "";
00162             if ($column == "lastaccess") {
00163                 $columndir = "DESC";
00164             } else {
00165                 $columndir = "ASC";
00166             }
00167         } else {
00168             $columndir = $dir == "ASC" ? "DESC":"ASC";
00169             if ($column == "lastaccess") {
00170                 $columnicon = $dir == "ASC" ? "up":"down";
00171             } else {
00172                 $columnicon = $dir == "ASC" ? "down":"up";
00173             }
00174             $columnicon = " <img src=\"" . $OUTPUT->pix_url('t/' . $columnicon) . "\" alt=\"\" />";
00175 
00176         }
00177         $$column = "<a href=\"user.php?sort=$column&amp;dir=$columndir\">".$string[$column]."</a>$columnicon";
00178     }
00179 
00180     if ($sort == "name") {
00181         $sort = "firstname";
00182     }
00183 
00184     list($extrasql, $params) = $ufiltering->get_sql_filter();
00185     $users = get_users_listing($sort, $dir, $page*$perpage, $perpage, '', '', '',
00186             $extrasql, $params, $context);
00187     $usercount = get_users(false);
00188     $usersearchcount = get_users(false, '', false, null, "", '', '', '', '', '*', $extrasql, $params);
00189 
00190     if ($extrasql !== '') {
00191         echo $OUTPUT->heading("$usersearchcount / $usercount ".get_string('users'));
00192         $usercount = $usersearchcount;
00193     } else {
00194         echo $OUTPUT->heading("$usercount ".get_string('users'));
00195     }
00196 
00197     $strall = get_string('all');
00198 
00199     $baseurl = new moodle_url('/admin/user.php', array('sort' => $sort, 'dir' => $dir, 'perpage' => $perpage));
00200     echo $OUTPUT->paging_bar($usercount, $page, $perpage, $baseurl);
00201 
00202     flush();
00203 
00204 
00205     if (!$users) {
00206         $match = array();
00207         echo $OUTPUT->heading(get_string('nousersfound'));
00208 
00209         $table = NULL;
00210 
00211     } else {
00212 
00213         $countries = get_string_manager()->get_list_of_countries(false);
00214         if (empty($mnethosts)) {
00215             $mnethosts = $DB->get_records('mnet_host', null, 'id', 'id,wwwroot,name');
00216         }
00217 
00218         foreach ($users as $key => $user) {
00219             if (isset($countries[$user->country])) {
00220                 $users[$key]->country = $countries[$user->country];
00221             }
00222         }
00223         if ($sort == "country") {  // Need to resort by full country name, not code
00224             foreach ($users as $user) {
00225                 $susers[$user->id] = $user->country;
00226             }
00227             asort($susers);
00228             foreach ($susers as $key => $value) {
00229                 $nusers[] = $users[$key];
00230             }
00231             $users = $nusers;
00232         }
00233 
00234         $override = new stdClass();
00235         $override->firstname = 'firstname';
00236         $override->lastname = 'lastname';
00237         $fullnamelanguage = get_string('fullnamedisplay', '', $override);
00238         if (($CFG->fullnamedisplay == 'firstname lastname') or
00239             ($CFG->fullnamedisplay == 'firstname') or
00240             ($CFG->fullnamedisplay == 'language' and $fullnamelanguage == 'firstname lastname' )) {
00241             $fullnamedisplay = "$firstname / $lastname";
00242         } else { // ($CFG->fullnamedisplay == 'language' and $fullnamelanguage == 'lastname firstname')
00243             $fullnamedisplay = "$lastname / $firstname";
00244         }
00245 
00246         $table = new html_table();
00247         $table->head = array ();
00248         $table->align = array();
00249         $table->head[] = $fullnamedisplay;
00250         $table->align[] = 'left';
00251         foreach ($extracolumns as $field) {
00252             $table->head[] = ${$field};
00253             $table->align[] = 'left';
00254         }
00255         $table->head[] = $city;
00256         $table->align[] = 'left';
00257         $table->head[] = $country;
00258         $table->align[] = 'left';
00259         $table->head[] = $lastaccess;
00260         $table->align[] = 'left';
00261         $table->head[] = get_string('edit');
00262         $table->align[] = 'center';
00263         $table->head[] = "";
00264         $table->align[] = 'center';
00265 
00266         $table->width = "95%";
00267         foreach ($users as $user) {
00268             if (isguestuser($user)) {
00269                 continue; // do not display guest here
00270             }
00271 
00272             $buttons = array();
00273             $lastcolumn = '';
00274 
00275             // delete button
00276             if (has_capability('moodle/user:delete', $sitecontext)) {
00277                 if (is_mnet_remote_user($user) or $user->id == $USER->id or is_siteadmin($user)) {
00278                     // no deleting of self, mnet accounts or admins allowed
00279                 } else {
00280                     $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));
00281                 }
00282             }
00283 
00284             // suspend button
00285             if (has_capability('moodle/user:update', $sitecontext)) {
00286                 if (is_mnet_remote_user($user)) {
00287                     // mnet users have special access control, they can not be deleted the standard way or suspended
00288                     $accessctrl = 'allow';
00289                     if ($acl = $DB->get_record('mnet_sso_access_control', array('username'=>$user->username, 'mnet_host_id'=>$user->mnethostid))) {
00290                         $accessctrl = $acl->accessctrl;
00291                     }
00292                     $changeaccessto = ($accessctrl == 'deny' ? 'allow' : 'deny');
00293                     $buttons[] = " (<a href=\"?acl={$user->id}&amp;accessctrl=$changeaccessto&amp;sesskey=".sesskey()."\">".get_string($changeaccessto, 'mnet') . " access</a>)";
00294 
00295                 } else {
00296                     if ($user->suspended) {
00297                         $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));
00298                     } else {
00299                         if ($user->id == $USER->id or is_siteadmin($user)) {
00300                             // no suspending of admins or self!
00301                         } else {
00302                             $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));
00303                         }
00304                     }
00305 
00306                 }
00307             }
00308 
00309             // edit button
00310             if (has_capability('moodle/user:update', $sitecontext)) {
00311                 // prevent editing of admins by non-admins
00312                 if (is_siteadmin($USER) or !is_siteadmin($user)) {
00313                     $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));
00314                 }
00315             }
00316 
00317             // the last column - confirm or mnet info
00318             if (is_mnet_remote_user($user)) {
00319                 // all mnet users are confirmed, let's print just the name of the host there
00320                 if (isset($mnethosts[$user->mnethostid])) {
00321                     $lastcolumn = get_string($accessctrl, 'mnet').': '.$mnethosts[$user->mnethostid]->name;
00322                 } else {
00323                     $lastcolumn = get_string($accessctrl, 'mnet');
00324                 }
00325 
00326             } else if ($user->confirmed == 0) {
00327                 if (has_capability('moodle/user:update', $sitecontext)) {
00328                     $lastcolumn = html_writer::link(new moodle_url($returnurl, array('confirmuser'=>$user->id, 'sesskey'=>sesskey())), $strconfirm);
00329                 } else {
00330                     $lastcolumn = "<span class=\"dimmed_text\">".get_string('confirm')."</span>";
00331                 }
00332             }
00333 
00334             if ($user->lastaccess) {
00335                 $strlastaccess = format_time(time() - $user->lastaccess);
00336             } else {
00337                 $strlastaccess = get_string('never');
00338             }
00339             $fullname = fullname($user, true);
00340 
00341             $row = array ();
00342             $row[] = "<a href=\"../user/view.php?id=$user->id&amp;course=$site->id\">$fullname</a>";
00343             foreach ($extracolumns as $field) {
00344                 $row[] = $user->{$field};
00345             }
00346             $row[] = $user->city;
00347             $row[] = $user->country;
00348             $row[] = $strlastaccess;
00349             if ($user->suspended) {
00350                 foreach ($row as $k=>$v) {
00351                     $row[$k] = html_writer::tag('span', $v, array('class'=>'usersuspended'));
00352                 }
00353             }
00354             $row[] = implode(' ', $buttons);
00355             $row[] = $lastcolumn;
00356             $table->data[] = $row;
00357         }
00358     }
00359 
00360     // add filters
00361     $ufiltering->display_add();
00362     $ufiltering->display_active();
00363 
00364     if (has_capability('moodle/user:create', $sitecontext)) {
00365         echo $OUTPUT->heading('<a href="'.$securewwwroot.'/user/editadvanced.php?id=-1">'.get_string('addnewuser').'</a>');
00366     }
00367     if (!empty($table)) {
00368         echo html_writer::table($table);
00369         echo $OUTPUT->paging_bar($usercount, $page, $perpage, $baseurl);
00370         if (has_capability('moodle/user:create', $sitecontext)) {
00371             echo $OUTPUT->heading('<a href="'.$securewwwroot.'/user/editadvanced.php?id=-1">'.get_string('addnewuser').'</a>');
00372         }
00373     }
00374 
00375     echo $OUTPUT->footer();
00376 
00377 
00378 
 All Data Structures Namespaces Files Functions Variables Enumerations