libdir.'/adminlib.php'); $processed = optional_param('processed', '', PARAM_BOOL); $sort = optional_param('sort', 'fullname', PARAM_ALPHA); //Sort by full name $dir = optional_param('dir', 'asc', PARAM_ALPHA); //Order to sort (ASC) require_login(); admin_externalpage_setup('userbulk'); require_capability('moodle/role:assign', context_system::instance()); //TODO: use some enrol cap $return = $CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk.php'; //If no users selected then return to user_bulk.php if (empty($SESSION->bulk_users)) { redirect($return); } $users = $SESSION->bulk_users; //Get users to display $usertotal = get_users(false); //Total number of users registered $usercount = count($users); //number of users echo $OUTPUT->header(); //take user info foreach ($users as $key => $id) { $user = $DB->get_record('user', array('id'=>$id)); $user->fullname = fullname($user, true); unset($user->firstname); unset($user->lastname); $users[$key] = $user; } // Need to sort by date function sort_compare($a, $b) { global $sort, $dir; if($sort == 'lastaccess') { $rez = $b->lastaccess - $a->lastaccess; } else { $rez = strcasecmp(@$a->$sort, @$b->$sort); } return $dir == 'desc' ? -$rez : $rez; } usort($users, 'sort_compare'); //Take courses data (id, shortname, and fullname) $courses = get_courses_page(1, 'c.sortorder ASC', 'c.id,c.shortname,c.fullname,c.visible', $totalcount); $table = new html_table(); $table->width = "95%"; $columns = array('fullname'); foreach ($courses as $v) { $columns[] = $v->shortname; } //Print columns headers from table foreach ($columns as $column) { $strtitle = $column; if ($sort != $column) { $columnicon = ''; $columndir = 'asc'; } else { $columndir = ($dir == 'asc') ? 'desc' : 'asc'; $columnicon = ' '; } $table->head[] = ''.$strtitle.''.$columnicon; $table->align[] = 'left'; } // process data submitting if(!empty($processed)) { //Process data form here $total = count($courses) * count($users); for ( $i = 0; $i < $total; $i++ ) { $param = "selected".$i; $info = optional_param($param, '', PARAM_SEQUENCE); /** * user id: ids[0] * course id: ids[1] * enrol stat: ids[2] */ $ids = explode(',', $info); if(!empty($ids[2])) { $context = context_course::instance($ids[1]); role_assign(5, $ids[0], $context->id); //TODO: horrible!! } else { if( empty($ids[1] ) ) { continue; } $context = context_course::instance($ids[1]); role_unassign(5, $ids[0], $context->id); } } redirect($return, get_string('changessaved')); //TODO: horrible!! } //Form beginning echo '
'; echo $OUTPUT->footer();