Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 
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");
30
 
31
$sort = optional_param('sort', 'fullname', PARAM_ALPHA);
32
$dir  = optional_param('dir', 'asc', PARAM_ALPHA);
33
 
34
admin_externalpage_setup('userbulk');
35
require_capability('moodle/cohort:assign', context_system::instance());
36
 
37
$returnurl = optional_param('returnurl', '', PARAM_LOCALURL);
38
$return = new moodle_url($returnurl ?: '/admin/user/user_bulk.php');
39
 
40
$users = $SESSION->bulk_users;
41
 
42
$strnever = get_string('never');
43
 
44
$cohorts = array(''=>get_string('choosedots'));
45
$allcohorts = $DB->get_records('cohort', null, 'name');
46
 
47
foreach ($allcohorts as $c) {
48
    if (!empty($c->component)) {
49
        // external cohorts can not be modified
50
        continue;
51
    }
52
    $context = context::instance_by_id($c->contextid);
53
    if (!has_capability('moodle/cohort:assign', $context)) {
54
        continue;
55
    }
56
 
57
    if (empty($c->idnumber)) {
58
        $cohorts[$c->id] = format_string($c->name);
59
    } else {
60
        $cohorts[$c->id] = format_string($c->name) . ' [' . $c->idnumber . ']';
61
    }
62
}
63
unset($allcohorts);
64
 
65
if (count($cohorts) < 2) {
66
    redirect($return, get_string('bulknocohort', 'core_cohort'));
67
}
68
 
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);
79
    $users[$key] = $user;
80
}
81
unset($countries);
82
 
83
$mform = new user_bulk_cohortadd_form(null, $cohorts);
84
$mform->set_data(['returnurl' => $returnurl]);
85
 
86
if (empty($users) or $mform->is_cancelled()) {
87
    redirect($return);
88
 
89
} else if ($data = $mform->get_data()) {
90
    // process request
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);
94
        }
95
    }
96
    redirect($return);
97
}
98
 
99
// Need to sort by date
100
function sort_compare($a, $b) {
101
    global $sort, $dir;
102
    if ($sort == 'lastaccess') {
103
        $rez = $b->lastaccess - $a->lastaccess;
104
    } else {
105
        $rez = strcasecmp(@$a->$sort, @$b->$sort);
106
    }
107
    return $dir == 'desc' ? -$rez : $rez;
108
}
109
usort($users, 'sort_compare');
110
 
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) {
117
        $columnicon = '';
118
        $columndir = 'asc';
119
    } else {
120
        $columndir = ($dir == 'asc') ? 'desc' : 'asc';
121
        $icon = 't/down';
122
        $iconstr = $columndir;
123
        if ($dir != 'asc') {
124
            $icon = 't/up';
125
        }
126
        $columnicon = ' ' . $OUTPUT->pix_icon($icon, get_string($iconstr));
127
    }
128
    $table->head[] = '<a href="user_bulk_cohortadd.php?sort='.$column.'&amp;dir='.$columndir.'">'.$strtitle.'</a>'.$columnicon;
129
    $table->align[] = 'left';
130
}
131
 
132
foreach ($users as $user) {
133
    if ($user->deleted) {
134
        $table->data[] = array (
135
            $user->fullname,
136
            '',
137
            '',
138
            '',
139
            get_string('deleteduser', 'bulkusers')
140
        );
141
    } else {
142
        $table->data[] = array(
143
            '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $user->id . '&amp;course=' . SITEID . '">' .
144
            $user->fullname .
145
            '</a>',
146
            s($user->email),
147
            $user->city,
148
            $user->country,
149
            $user->lastaccess ? format_time(time() - $user->lastaccess) : $strnever
150
        );
151
    }
152
}
153
 
154
echo $OUTPUT->header();
155
echo $OUTPUT->heading(get_string('bulkadd', 'core_cohort'));
156
 
157
echo html_writer::table($table);
158
 
159
echo $OUTPUT->box_start();
160
$mform->display();
161
echo $OUTPUT->box_end();
162
 
163
echo $OUTPUT->footer();