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

Last change on this file since 1331 was 1331, checked in by jrpelegrina, 3 years ago

Updated to moodle 3.0.3

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', null, 'name');
43
44foreach ($allcohorts as $c) {
45    if (!empty($c->component)) {
46        // external cohorts can not be modified
47        continue;
48    }
49    $context = context::instance_by_id($c->contextid);
50    if (!has_capability('moodle/cohort:assign', $context)) {
51        continue;
52    }
53
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 . ']';
58    }
59}
60unset($allcohorts);
61
62if (count($cohorts) < 2) {
63    echo $OUTPUT->header();
64    echo $OUTPUT->heading(get_string('bulkadd', 'core_cohort'));
65    echo $OUTPUT->notification(get_string('bulknocohort', 'core_cohort'));
66    echo $OUTPUT->continue_button(new moodle_url('/admin/user/user_bulk.php'));
67    echo $OUTPUT->footer();
68    die;
69}
70
71$countries = get_string_manager()->get_list_of_countries(true);
72$namefields = get_all_user_name_fields(true);
73foreach ($users as $key => $id) {
74    $user = $DB->get_record('user', array('id'=>$id, 'deleted'=>0), 'id, ' . $namefields . ', username,
75            email, country, lastaccess, city');
76    $user->fullname = fullname($user, true);
77    $user->country = @$countries[$user->country];
78    unset($user->firstname);
79    unset($user->lastname);
80    $users[$key] = $user;
81}
82unset($countries);
83
84$mform = new user_bulk_cohortadd_form(null, $cohorts);
85
86if (empty($users) or $mform->is_cancelled()) {
87    redirect(new moodle_url('/admin/user/user_bulk.php'));
88
89} else if ($data = $mform->get_data()) {
90    // process request
91    foreach ($users as $user) {
92        if (!$DB->record_exists('cohort_members', array('cohortid'=>$data->cohort, 'userid'=>$user->id))) {
93            cohort_add_member($data->cohort, $user->id);
94        }
95    }
96    redirect(new moodle_url('/admin/user/user_bulk.php'));
97}
98
99// Need to sort by date
100function 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}
109usort($users, 'sort_compare');
110
111$table = new html_table();
112$table->width = "95%";
113$columns = array('fullname', 'email', 'city', 'country', 'lastaccess');
114foreach ($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        $columnicon = ' <img src="'.$OUTPUT->pix_url('t/'.($dir == 'asc' ? 'down' : 'up' )).'" alt="" />';
122    }
123    $table->head[] = '<a href="user_bulk_cohortadd.php?sort='.$column.'&amp;dir='.$columndir.'">'.$strtitle.'</a>'.$columnicon;
124    $table->align[] = 'left';
125}
126
127foreach ($users as $user) {
128    $table->data[] = array (
129        '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.SITEID.'">'.$user->fullname.'</a>',
130        $user->email,
131        $user->city,
132        $user->country,
133        $user->lastaccess ? format_time(time() - $user->lastaccess) : $strnever
134    );
135}
136
137echo $OUTPUT->header();
138echo $OUTPUT->heading(get_string('bulkadd', 'core_cohort'));
139
140echo html_writer::table($table);
141
142echo $OUTPUT->box_start();
143$mform->display();
144echo $OUTPUT->box_end();
145
146echo $OUTPUT->footer();
Note: See TracBrowser for help on using the repository browser.