2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * script for bulk user multi cohort add
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 $returnurl = optional_param('returnurl', '', PARAM_LOCALURL
);
38 $return = new moodle_url($returnurl ?
: '/admin/user/user_bulk.php');
40 $users = $SESSION->bulk_users
;
42 $strnever = get_string('never');
44 $cohorts = array(''=>get_string('choosedots'));
45 $allcohorts = $DB->get_records('cohort', null, 'name');
47 foreach ($allcohorts as $c) {
48 if (!empty($c->component
)) {
49 // external cohorts can not be modified
52 $context = context
::instance_by_id($c->contextid
);
53 if (!has_capability('moodle/cohort:assign', $context)) {
57 if (empty($c->idnumber
)) {
58 $cohorts[$c->id
] = format_string($c->name
);
60 $cohorts[$c->id
] = format_string($c->name
) . ' [' . $c->idnumber
. ']';
65 if (count($cohorts) < 2) {
66 redirect($return, get_string('bulknocohort', 'core_cohort'));
69 $countries = get_string_manager()->get_list_of_countries(true);
70 $userfieldsapi = \core_user\fields
::for_name();
71 $namefields = $userfieldsapi->get_sql('', false, '', '', false)->selects
;
72 foreach ($users as $key => $id) {
73 $user = $DB->get_record('user', array('id' => $id), 'id, ' . $namefields . ', username,
74 email, country, lastaccess, city, deleted');
75 $user->fullname
= fullname($user, true);
76 $user->country
= @$countries[$user->country
];
77 unset($user->firstname
);
78 unset($user->lastname
);
83 $mform = new user_bulk_cohortadd_form(null, $cohorts);
84 $mform->set_data(['returnurl' => $returnurl]);
86 if (empty($users) or $mform->is_cancelled()) {
89 } else if ($data = $mform->get_data()) {
91 foreach ($users as $user) {
92 if (!$user->deleted
&& !$DB->record_exists('cohort_members', array('cohortid' => $data->cohort
, 'userid' => $user->id
))) {
93 cohort_add_member($data->cohort
, $user->id
);
99 // Need to sort by date
100 function sort_compare($a, $b) {
102 if ($sort == 'lastaccess') {
103 $rez = $b->lastaccess
- $a->lastaccess
;
105 $rez = strcasecmp(@$a->$sort, @$b->$sort);
107 return $dir == 'desc' ?
-$rez : $rez;
109 usort($users, 'sort_compare');
111 $table = new html_table();
112 $table->width
= "95%";
113 $columns = array('fullname', 'email', 'city', 'country', 'lastaccess');
114 foreach ($columns as $column) {
115 $strtitle = get_string($column);
116 if ($sort != $column) {
120 $columndir = ($dir == 'asc') ?
'desc' : 'asc';
122 $iconstr = $columndir;
126 $columnicon = ' ' . $OUTPUT->pix_icon($icon, get_string($iconstr));
128 $table->head
[] = '<a href="user_bulk_cohortadd.php?sort='.$column.'&dir='.$columndir.'">'.$strtitle.'</a>'.$columnicon;
129 $table->align
[] = 'left';
132 foreach ($users as $user) {
133 if ($user->deleted
) {
134 $table->data
[] = array (
139 get_string('deleteduser', 'bulkusers')
142 $table->data
[] = array(
143 '<a href="' . $CFG->wwwroot
. '/user/view.php?id=' . $user->id
. '&course=' . SITEID
. '">' .
149 $user->lastaccess ?
format_time(time() - $user->lastaccess
) : $strnever
154 echo $OUTPUT->header();
155 echo $OUTPUT->heading(get_string('bulkadd', 'core_cohort'));
157 echo html_writer
::table($table);
159 echo $OUTPUT->box_start();
161 echo $OUTPUT->box_end();
163 echo $OUTPUT->footer();