Merge branch 'wip-MDL-46470-m26' of git://github.com/marinaglancy/moodle into MOODLE_...
[moodle.git] / admin / user / user_bulk_enrol.php
blob21ad105e257c89ab4a8ea581880af1ff75bcc208
1 <?php
2 /**
3 * script for bulk user multi enrol operations
4 */
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)
14 require_login();
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)) {
20 redirect($return);
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();
28 //take user info
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);
34 $users[$key] = $user;
37 // Need to sort by date
38 function sort_compare($a, $b) {
39 global $sort, $dir;
40 if($sort == 'lastaccess') {
41 $rez = $b->lastaccess - $a->lastaccess;
42 } else {
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) {
61 $strtitle = $column;
62 if ($sort != $column) {
63 $columnicon = '';
64 $columndir = 'asc';
65 } else {
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.'&amp;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);
82 /**
83 * user id: ids[0]
84 * course id: ids[1]
85 * enrol stat: ids[2]
87 $ids = explode(',', $info);
88 if(!empty($ids[2])) {
89 $context = context_course::instance($ids[1]);
90 role_assign(5, $ids[0], $context->id); //TODO: horrible!!
91 } else {
92 if( empty($ids[1] ) ) {
93 continue;
95 $context = context_course::instance($ids[1]);
96 role_unassign(5, $ids[0], $context->id);
99 redirect($return, get_string('changessaved')); //TODO: horrible!!
102 //Form beginning
103 echo '<form id="multienrol" name="multienrol" method="post" action="user_bulk_enrol.php">';
104 echo '<input type="hidden" name="processed" value="yes" />';
105 $count = 0;
106 foreach($users as $user) {
107 $temparray = array (
108 '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&amp;course='.SITEID.'">'.$user->fullname.'</a>'
110 $mycourses = enrol_get_users_courses($user->id, false);
111 foreach($courses as $acourse) {
112 $state = '';
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 . '/>';
120 $count++;
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" />';
128 echo '</div>';
129 echo '</form>';
131 echo $OUTPUT->footer();