3 * script for bulk user multi enrol operations
6 die('this needs to be rewritten to use new enrol framework, sorry'); //TODO: MDL-24064
8 require_once('../../config.php');
9 require_once($CFG->libdir
.'/adminlib.php');
10 $processed = optional_param('processed', '', PARAM_BOOL
);
11 $sort = optional_param('sort', 'fullname', PARAM_ALPHA
); //Sort by full name
12 $dir = optional_param('dir', 'asc', PARAM_ALPHA
); //Order to sort (ASC)
15 admin_externalpage_setup('userbulk');
16 require_capability('moodle/role:assign', context_system
::instance()); //TODO: use some enrol cap
17 $return = $CFG->wwwroot
.'/'.$CFG->admin
.'/user/user_bulk.php';
18 //If no users selected then return to user_bulk.php
19 if (empty($SESSION->bulk_users
)) {
22 $users = $SESSION->bulk_users
; //Get users to display
23 $usertotal = get_users(false); //Total number of users registered
24 $usercount = count($users); //number of users
26 echo $OUTPUT->header();
29 foreach ($users as $key => $id) {
30 $user = $DB->get_record('user', array('id'=>$id));
31 $user->fullname
= fullname($user, true);
32 unset($user->firstname
);
33 unset($user->lastname
);
37 // Need to sort by date
38 function sort_compare($a, $b) {
40 if($sort == 'lastaccess') {
41 $rez = $b->lastaccess
- $a->lastaccess
;
43 $rez = strcasecmp(@$a->$sort, @$b->$sort);
45 return $dir == 'desc' ?
-$rez : $rez;
47 usort($users, 'sort_compare');
49 //Take courses data (id, shortname, and fullname)
50 $courses = get_courses_page(1, 'c.sortorder ASC', 'c.id,c.shortname,c.fullname,c.visible', $totalcount);
51 $table = new html_table();
52 $table->width
= "95%";
53 $columns = array('fullname');
54 foreach ($courses as $v)
56 $columns[] = $v->shortname
;
59 //Print columns headers from table
60 foreach ($columns as $column) {
62 if ($sort != $column) {
66 $columndir = ($dir == 'asc') ?
'desc' : 'asc';
67 $columnicon = ' <img src="'.$OUTPUT->pix_url('t/'.($dir == 'asc' ?
'down' : 'up' )).'" alt="" />';
69 $table->head
[] = '<a href="user_bulk_enrol.php?sort='.$column.'&dir='.$columndir.'">'.$strtitle.'</a>'.$columnicon;
70 $table->align
[] = 'left';
73 // process data submitting
74 if(!empty($processed)) {
75 //Process data form here
76 $total = count($courses) * count($users);
78 for ( $i = 0; $i < $total; $i++
)
80 $param = "selected".$i;
81 $info = optional_param($param, '', PARAM_SEQUENCE
);
87 $ids = explode(',', $info);
89 $context = context_course
::instance($ids[1]);
90 role_assign(5, $ids[0], $context->id
); //TODO: horrible!!
92 if( empty($ids[1] ) ) {
95 $context = context_course
::instance($ids[1]);
96 role_unassign(5, $ids[0], $context->id
);
99 redirect($return, get_string('changessaved')); //TODO: horrible!!
103 echo '<form id="multienrol" name="multienrol" method="post" action="user_bulk_enrol.php">';
104 echo '<input type="hidden" name="processed" value="yes" />';
106 foreach($users as $user) {
108 '<a href="'.$CFG->wwwroot
.'/user/view.php?id='.$user->id
.'&course='.SITEID
.'">'.$user->fullname
.'</a>'
110 $mycourses = enrol_get_users_courses($user->id
, false);
111 foreach($courses as $acourse) {
113 if (isset($mycourses[$acourse->id
])) {
114 $state = 'checked="checked"';
116 $temparray[] = '<input type="hidden" name="selected' . $count .
117 '" value="' . $user->id
. ',' . $acourse->id
. ',0" />' .
118 '<input type="checkbox" name="selected' . $count .
119 '" value="' . $user->id
. ',' . $acourse->id
. ',1" ' . $state . '/>';
122 $table->data
[] = $temparray;
124 echo $OUTPUT->heading("$usercount / $usertotal ".get_string('users'));
125 echo html_writer
::table($table);
126 echo '<div class="continuebutton">';
127 echo '<input type="submit" name="multienrolsubmit" value="save changes" />';
131 echo $OUTPUT->footer();