forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-15449, enrol multiple users to multiple courses, including bugs f…
…ix, see tracker, sent by Jonathan.
- Loading branch information
dongsheng
committed
Jul 10, 2008
1 parent
554606c
commit 51471f2
Showing
6 changed files
with
159 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
<?php // $Id$ | ||
/** | ||
* script for bulk user multy enrol operations | ||
*/ | ||
require_once('../../config.php'); | ||
require_once($CFG->libdir.'/adminlib.php'); | ||
$processed = optional_param('processed', '', PARAM_CLEAN); | ||
$sort = optional_param('sort', 'fullname', PARAM_ALPHA); //Sort by full name | ||
$dir = optional_param('dir', 'asc', PARAM_ALPHA); //Order to sort (ASC) | ||
|
||
admin_externalpage_setup('userbulk'); | ||
require_capability('moodle/user:delete', get_context_instance(CONTEXT_SYSTEM)); | ||
$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 | ||
|
||
admin_externalpage_print_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->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 = ' <img src="'.$CFG->pixpath.'/t/'.($dir == 'asc' ? 'down' : 'up' ).'.gif" alt="" />'; | ||
} | ||
$table->head[] = '<a href="user_bulk_enrol.php?sort='.$column.'&dir='.$columndir.'">'.$strtitle.'</a>'.$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_CLEAN); | ||
/** | ||
* user id: ids[0] | ||
* course id: ids[1] | ||
* enrol stat: ids[2] | ||
*/ | ||
$ids = explode(',', $info); | ||
if(!empty($ids[2])) { | ||
$context = get_context_instance(CONTEXT_COURSE, $ids[1]); | ||
if( role_assign(5, $ids[0], 0, $context->id) ) { | ||
continue; | ||
} | ||
} else { | ||
if( empty($ids[1] ) ) { | ||
continue; | ||
} | ||
$context = get_context_instance(CONTEXT_COURSE, $ids[1]); | ||
if( role_unassign(5, $ids[0], 0, $context->id) ) { | ||
continue; | ||
} | ||
} | ||
} | ||
redirect($return, get_string('changessaved')); | ||
} | ||
echo <<<EOD | ||
<script type="text/javascript"> | ||
function toggleck(id, uid, cid) { | ||
var el = document.getElementById('ck-'+id); | ||
if(!document.getElementById('ck-'+id+'-c').checked) { | ||
el.name = 'selected'+id; | ||
el.value = uid+','+cid+',0'; | ||
} else { | ||
el.name = 'unselected'; | ||
} | ||
} | ||
</script> | ||
EOD; | ||
//Form beginning | ||
echo '<form id="multienrol" name="multienrol" method="post" action="user_bulk_enrol.php">'; | ||
echo '<input type="hidden" name="processed" value="yes" />'; | ||
$count = 0; | ||
foreach($users as $user) | ||
{ | ||
$temparray = array ( | ||
'<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&course='.SITEID.'">'.$user->fullname.'</a>' | ||
); | ||
$mycourses = get_my_courses($user->id); | ||
$i = 1; | ||
foreach($courses as $acourse) | ||
{ | ||
$temparray[$i] = ''; | ||
if(isset($mycourses[$acourse->id])) { | ||
// already enrolled | ||
$temparray[$i] .= '<input type="hidden" id="ck-'.$count.'" name="unselected'.$count. | ||
'" value="'.$user->id.','.$acourse->id.',1" />'; | ||
$temparray[$i] .= "<input type='checkbox' id='ck-".$count. | ||
"-c' checked='yes' onclick='toggleck(\"". | ||
$count."\",".$user->id.",".$acourse->id.",false)' />"; | ||
} else { | ||
// unenrol | ||
$temparray[$i] .= '<input type="checkbox" name="selected'. | ||
$count.'" value="'.$user->id.','.$acourse->id.','.true.'" />'; | ||
} | ||
$i++; | ||
$count++; | ||
} | ||
$table->data[] = $temparray; | ||
} | ||
print_heading("$usercount / $usertotal ".get_string('users')); | ||
print_table($table); | ||
echo '<div class="continuebutton">'; | ||
echo '<input type="submit" name="multienrolsubmit" value="save changes">'; | ||
echo '</div>'; | ||
echo '</form>'; | ||
|
||
admin_externalpage_print_footer(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters