Merge branch 'MDL-69086-master' of git://github.com/andrewnicols/moodle
[moodle.git] / admin / user / user_bulk_cohortadd.php
blobf5e39475835cc9d8960f8f6a7b4b5a393c44ae79
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.
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/>.
17 /**
18 * script for bulk user multi cohort add
20 * @package core
21 * @subpackage user
22 * @copyright 2011 Petr Skoda (http://skodak.org)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require('../../config.php');
27 require_once($CFG->libdir.'/adminlib.php');
28 require_once('user_bulk_cohortadd_form.php');
29 require_once("$CFG->dirroot/cohort/lib.php");
31 $sort = optional_param('sort', 'fullname', PARAM_ALPHA);
32 $dir = optional_param('dir', 'asc', PARAM_ALPHA);
34 admin_externalpage_setup('userbulk');
35 require_capability('moodle/cohort:assign', context_system::instance());
37 $users = $SESSION->bulk_users;
39 $strnever = get_string('never');
41 $cohorts = array(''=>get_string('choosedots'));
42 $allcohorts = $DB->get_records('cohort', null, 'name');
44 foreach ($allcohorts as $c) {
45 if (!empty($c->component)) {
46 // external cohorts can not be modified
47 continue;
49 $context = context::instance_by_id($c->contextid);
50 if (!has_capability('moodle/cohort:assign', $context)) {
51 continue;
54 if (empty($c->idnumber)) {
55 $cohorts[$c->id] = format_string($c->name);
56 } else {
57 $cohorts[$c->id] = format_string($c->name) . ' [' . $c->idnumber . ']';
60 unset($allcohorts);
62 if (count($cohorts) < 2) {
63 redirect(new moodle_url('/admin/user/user_bulk.php'), get_string('bulknocohort', 'core_cohort'));
66 $countries = get_string_manager()->get_list_of_countries(true);
67 $namefields = get_all_user_name_fields(true);
68 foreach ($users as $key => $id) {
69 $user = $DB->get_record('user', array('id' => $id), 'id, ' . $namefields . ', username,
70 email, country, lastaccess, city, deleted');
71 $user->fullname = fullname($user, true);
72 $user->country = @$countries[$user->country];
73 unset($user->firstname);
74 unset($user->lastname);
75 $users[$key] = $user;
77 unset($countries);
79 $mform = new user_bulk_cohortadd_form(null, $cohorts);
81 if (empty($users) or $mform->is_cancelled()) {
82 redirect(new moodle_url('/admin/user/user_bulk.php'));
84 } else if ($data = $mform->get_data()) {
85 // process request
86 foreach ($users as $user) {
87 if (!$user->deleted && !$DB->record_exists('cohort_members', array('cohortid' => $data->cohort, 'userid' => $user->id))) {
88 cohort_add_member($data->cohort, $user->id);
91 redirect(new moodle_url('/admin/user/user_bulk.php'));
94 // Need to sort by date
95 function sort_compare($a, $b) {
96 global $sort, $dir;
97 if ($sort == 'lastaccess') {
98 $rez = $b->lastaccess - $a->lastaccess;
99 } else {
100 $rez = strcasecmp(@$a->$sort, @$b->$sort);
102 return $dir == 'desc' ? -$rez : $rez;
104 usort($users, 'sort_compare');
106 $table = new html_table();
107 $table->width = "95%";
108 $columns = array('fullname', 'email', 'city', 'country', 'lastaccess');
109 foreach ($columns as $column) {
110 $strtitle = get_string($column);
111 if ($sort != $column) {
112 $columnicon = '';
113 $columndir = 'asc';
114 } else {
115 $columndir = ($dir == 'asc') ? 'desc' : 'asc';
116 $icon = 't/down';
117 $iconstr = $columndir;
118 if ($dir != 'asc') {
119 $icon = 't/up';
121 $columnicon = ' ' . $OUTPUT->pix_icon($icon, get_string($iconstr));
123 $table->head[] = '<a href="user_bulk_cohortadd.php?sort='.$column.'&amp;dir='.$columndir.'">'.$strtitle.'</a>'.$columnicon;
124 $table->align[] = 'left';
127 foreach ($users as $user) {
128 if ($user->deleted) {
129 $table->data[] = array (
130 $user->fullname,
134 get_string('deleteduser', 'bulkusers')
136 } else {
137 $table->data[] = array(
138 '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $user->id . '&amp;course=' . SITEID . '">' .
139 $user->fullname .
140 '</a>',
141 s($user->email),
142 $user->city,
143 $user->country,
144 $user->lastaccess ? format_time(time() - $user->lastaccess) : $strnever
149 echo $OUTPUT->header();
150 echo $OUTPUT->heading(get_string('bulkadd', 'core_cohort'));
152 echo html_writer::table($table);
154 echo $OUTPUT->box_start();
155 $mform->display();
156 echo $OUTPUT->box_end();
158 echo $OUTPUT->footer();