MDL-29519 move plagiarism cron to after event queue
[moodle.git] / admin / user / user_bulk_cohortadd.php
blobf97216d89772e031342a24f6f1ed65223bda06ad
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', get_context_instance(CONTEXT_SYSTEM));
37 $users = $SESSION->bulk_users;
39 $strnever = get_string('never');
41 $cohorts = array(''=>get_string('choosedots'));
42 $allcohorts = $DB->get_records('cohort');
43 foreach ($allcohorts as $c) {
44 if (!empty($c->component)) {
45 // external cohorts can not be modified
46 continue;
48 $context = get_context_instance_by_id($c->contextid);
49 if (!has_capability('moodle/cohort:assign', $context)) {
50 continue;
53 if (empty($c->idnumber)) {
54 $cohorts[$c->id] = format_string($c->name);
55 } else {
56 $cohorts[$c->id] = format_string($c->name) . ' [' . $c->idnumber . ']';
59 unset($allcohorts);
61 if (count($cohorts) < 2) {
62 echo $OUTPUT->header();
63 echo $OUTPUT->heading(get_string('bulkadd', 'core_cohort'));
64 echo $OUTPUT->notification(get_string('bulknocohort', 'core_cohort'));
65 echo $OUTPUT->continue_button(new moodle_url('/admin/user/user_bulk.php'));
66 echo $OUTPUT->footer();
67 die;
70 $countries = get_string_manager()->get_list_of_countries(true);
71 foreach ($users as $key => $id) {
72 $user = $DB->get_record('user', array('id'=>$id, 'deleted'=>0), 'id, firstname, lastname, username, email, country, lastaccess, city');
73 $user->fullname = fullname($user, true);
74 $user->country = @$countries[$user->country];
75 unset($user->firstname);
76 unset($user->lastname);
77 $users[$key] = $user;
79 unset($countries);
81 $mform = new user_bulk_cohortadd_form(null, $cohorts);
83 if (empty($users) or $mform->is_cancelled()) {
84 redirect(new moodle_url('/admin/user/user_bulk.php'));
86 } else if ($data = $mform->get_data()) {
87 // process request
88 foreach ($users as $user) {
89 if (!$DB->record_exists('cohort_members', array('cohortid'=>$data->cohort, 'userid'=>$user->id))) {
90 cohort_add_member($data->cohort, $user->id);
93 redirect(new moodle_url('/admin/user/user_bulk.php'));
96 // Need to sort by date
97 function sort_compare($a, $b) {
98 global $sort, $dir;
99 if ($sort == 'lastaccess') {
100 $rez = $b->lastaccess - $a->lastaccess;
101 } else {
102 $rez = strcasecmp(@$a->$sort, @$b->$sort);
104 return $dir == 'desc' ? -$rez : $rez;
106 usort($users, 'sort_compare');
108 $table = new html_table();
109 $table->width = "95%";
110 $columns = array('fullname', 'email', 'city', 'country', 'lastaccess');
111 foreach ($columns as $column) {
112 $strtitle = get_string($column);
113 if ($sort != $column) {
114 $columnicon = '';
115 $columndir = 'asc';
116 } else {
117 $columndir = ($dir == 'asc') ? 'desc' : 'asc';
118 $columnicon = ' <img src="'.$OUTPUT->pix_url('t/'.($dir == 'asc' ? 'down' : 'up' )).'" alt="" />';
120 $table->head[] = '<a href="user_bulk_cohortadd.php?sort='.$column.'&amp;dir='.$columndir.'">'.$strtitle.'</a>'.$columnicon;
121 $table->align[] = 'left';
124 foreach ($users as $user) {
125 $table->data[] = array (
126 '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.SITEID.'">'.$user->fullname.'</a>',
127 $user->email,
128 $user->city,
129 $user->country,
130 $user->lastaccess ? format_time(time() - $user->lastaccess) : $strnever
134 echo $OUTPUT->header();
135 echo $OUTPUT->heading(get_string('bulkadd', 'core_cohort'));
137 echo html_writer::table($table);
139 echo $OUTPUT->box_start();
140 $mform->display();
141 echo $OUTPUT->box_end();
143 echo $OUTPUT->footer();