|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00026 require('../../config.php'); 00027 require_once($CFG->libdir.'/adminlib.php'); 00028 require_once('user_bulk_cohortadd_form.php'); 00029 require_once("$CFG->dirroot/cohort/lib.php"); 00030 00031 $sort = optional_param('sort', 'fullname', PARAM_ALPHA); 00032 $dir = optional_param('dir', 'asc', PARAM_ALPHA); 00033 00034 admin_externalpage_setup('userbulk'); 00035 require_capability('moodle/cohort:assign', get_context_instance(CONTEXT_SYSTEM)); 00036 00037 $users = $SESSION->bulk_users; 00038 00039 $strnever = get_string('never'); 00040 00041 $cohorts = array(''=>get_string('choosedots')); 00042 $allcohorts = $DB->get_records('cohort'); 00043 foreach ($allcohorts as $c) { 00044 if (!empty($c->component)) { 00045 // external cohorts can not be modified 00046 continue; 00047 } 00048 $context = get_context_instance_by_id($c->contextid); 00049 if (!has_capability('moodle/cohort:assign', $context)) { 00050 continue; 00051 } 00052 00053 if (empty($c->idnumber)) { 00054 $cohorts[$c->id] = format_string($c->name); 00055 } else { 00056 $cohorts[$c->id] = format_string($c->name) . ' [' . $c->idnumber . ']'; 00057 } 00058 } 00059 unset($allcohorts); 00060 00061 if (count($cohorts) < 2) { 00062 echo $OUTPUT->header(); 00063 echo $OUTPUT->heading(get_string('bulkadd', 'core_cohort')); 00064 echo $OUTPUT->notification(get_string('bulknocohort', 'core_cohort')); 00065 echo $OUTPUT->continue_button(new moodle_url('/admin/user/user_bulk.php')); 00066 echo $OUTPUT->footer(); 00067 die; 00068 } 00069 00070 $countries = get_string_manager()->get_list_of_countries(true); 00071 foreach ($users as $key => $id) { 00072 $user = $DB->get_record('user', array('id'=>$id, 'deleted'=>0), 'id, firstname, lastname, username, email, country, lastaccess, city'); 00073 $user->fullname = fullname($user, true); 00074 $user->country = @$countries[$user->country]; 00075 unset($user->firstname); 00076 unset($user->lastname); 00077 $users[$key] = $user; 00078 } 00079 unset($countries); 00080 00081 $mform = new user_bulk_cohortadd_form(null, $cohorts); 00082 00083 if (empty($users) or $mform->is_cancelled()) { 00084 redirect(new moodle_url('/admin/user/user_bulk.php')); 00085 00086 } else if ($data = $mform->get_data()) { 00087 // process request 00088 foreach ($users as $user) { 00089 if (!$DB->record_exists('cohort_members', array('cohortid'=>$data->cohort, 'userid'=>$user->id))) { 00090 cohort_add_member($data->cohort, $user->id); 00091 } 00092 } 00093 redirect(new moodle_url('/admin/user/user_bulk.php')); 00094 } 00095 00096 // Need to sort by date 00097 function sort_compare($a, $b) { 00098 global $sort, $dir; 00099 if ($sort == 'lastaccess') { 00100 $rez = $b->lastaccess - $a->lastaccess; 00101 } else { 00102 $rez = strcasecmp(@$a->$sort, @$b->$sort); 00103 } 00104 return $dir == 'desc' ? -$rez : $rez; 00105 } 00106 usort($users, 'sort_compare'); 00107 00108 $table = new html_table(); 00109 $table->width = "95%"; 00110 $columns = array('fullname', 'email', 'city', 'country', 'lastaccess'); 00111 foreach ($columns as $column) { 00112 $strtitle = get_string($column); 00113 if ($sort != $column) { 00114 $columnicon = ''; 00115 $columndir = 'asc'; 00116 } else { 00117 $columndir = ($dir == 'asc') ? 'desc' : 'asc'; 00118 $columnicon = ' <img src="'.$OUTPUT->pix_url('t/'.($dir == 'asc' ? 'down' : 'up' )).'" alt="" />'; 00119 } 00120 $table->head[] = '<a href="user_bulk_cohortadd.php?sort='.$column.'&dir='.$columndir.'">'.$strtitle.'</a>'.$columnicon; 00121 $table->align[] = 'left'; 00122 } 00123 00124 foreach ($users as $user) { 00125 $table->data[] = array ( 00126 '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&course='.SITEID.'">'.$user->fullname.'</a>', 00127 $user->email, 00128 $user->city, 00129 $user->country, 00130 $user->lastaccess ? format_time(time() - $user->lastaccess) : $strnever 00131 ); 00132 } 00133 00134 echo $OUTPUT->header(); 00135 echo $OUTPUT->heading(get_string('bulkadd', 'core_cohort')); 00136 00137 echo html_writer::table($table); 00138 00139 echo $OUTPUT->box_start(); 00140 $mform->display(); 00141 echo $OUTPUT->box_end(); 00142 00143 echo $OUTPUT->footer();