source: moodle/trunk/fuentes/admin/user/user_bulk_cohortadd.php @ 136

Last change on this file since 136 was 136, checked in by mabarracus, 3 years ago

Ported code to xenial

File size: 4.6 KB
Line 
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.
13//
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/>.
16
17/**
18 * script for bulk user multi cohort add
19 *
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
24 */
25
26require('../../config.php');
27require_once($CFG->libdir.'/adminlib.php');
28require_once('user_bulk_cohortadd_form.php');
29require_once("$CFG->dirroot/cohort/lib.php");
30
31$sort = optional_param('sort', 'fullname', PARAM_ALPHA);
32$dir  = optional_param('dir', 'asc', PARAM_ALPHA);
33
34admin_externalpage_setup('userbulk');
35require_capability('moodle/cohort:assign', context_system::instance());
36
37$users = $SESSION->bulk_users;
38
39$strnever = get_string('never');
40
41$cohorts = array(''=>get_string('choosedots'));
42$allcohorts = $DB->get_records('cohort');
43foreach ($allcohorts as $c) {
44    if (!empty($c->component)) {
45        // external cohorts can not be modified
46        continue;
47    }
48    $context = context::instance_by_id($c->contextid);
49    if (!has_capability('moodle/cohort:assign', $context)) {
50        continue;
51    }
52
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 . ']';
57    }
58}
59unset($allcohorts);
60
61if (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;
68}
69
70$countries = get_string_manager()->get_list_of_countries(true);
71$namefields = get_all_user_name_fields(true);
72foreach ($users as $key => $id) {
73    $user = $DB->get_record('user', array('id'=>$id, 'deleted'=>0), 'id, ' . $namefields . ', username,
74            email, country, lastaccess, city');
75    $user->fullname = fullname($user, true);
76    $user->country = @$countries[$user->country];
77    unset($user->firstname);
78    unset($user->lastname);
79    $users[$key] = $user;
80}
81unset($countries);
82
83$mform = new user_bulk_cohortadd_form(null, $cohorts);
84
85if (empty($users) or $mform->is_cancelled()) {
86    redirect(new moodle_url('/admin/user/user_bulk.php'));
87
88} else if ($data = $mform->get_data()) {
89    // process request
90    foreach ($users as $user) {
91        if (!$DB->record_exists('cohort_members', array('cohortid'=>$data->cohort, 'userid'=>$user->id))) {
92            cohort_add_member($data->cohort, $user->id);
93        }
94    }
95    redirect(new moodle_url('/admin/user/user_bulk.php'));
96}
97
98// Need to sort by date
99function sort_compare($a, $b) {
100    global $sort, $dir;
101    if ($sort == 'lastaccess') {
102        $rez = $b->lastaccess - $a->lastaccess;
103    } else {
104        $rez = strcasecmp(@$a->$sort, @$b->$sort);
105    }
106    return $dir == 'desc' ? -$rez : $rez;
107}
108usort($users, 'sort_compare');
109
110$table = new html_table();
111$table->width = "95%";
112$columns = array('fullname', 'email', 'city', 'country', 'lastaccess');
113foreach ($columns as $column) {
114    $strtitle = get_string($column);
115    if ($sort != $column) {
116        $columnicon = '';
117        $columndir = 'asc';
118    } else {
119        $columndir = ($dir == 'asc') ? 'desc' : 'asc';
120        $columnicon = ' <img src="'.$OUTPUT->pix_url('t/'.($dir == 'asc' ? 'down' : 'up' )).'" alt="" />';
121    }
122    $table->head[] = '<a href="user_bulk_cohortadd.php?sort='.$column.'&amp;dir='.$columndir.'">'.$strtitle.'</a>'.$columnicon;
123    $table->align[] = 'left';
124}
125
126foreach ($users as $user) {
127    $table->data[] = array (
128        '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.SITEID.'">'.$user->fullname.'</a>',
129        $user->email,
130        $user->city,
131        $user->country,
132        $user->lastaccess ? format_time(time() - $user->lastaccess) : $strnever
133    );
134}
135
136echo $OUTPUT->header();
137echo $OUTPUT->heading(get_string('bulkadd', 'core_cohort'));
138
139echo html_writer::table($table);
140
141echo $OUTPUT->box_start();
142$mform->display();
143echo $OUTPUT->box_end();
144
145echo $OUTPUT->footer();
Note: See TracBrowser for help on using the repository browser.