From 51471f2ad531834e0501d5804f8a15ec4cdabae2 Mon Sep 17 00:00:00 2001 From: dongsheng Date: Thu, 10 Jul 2008 09:16:43 +0000 Subject: [PATCH] MDL-15449, enrol multiple users to multiple courses, including bugs fix, see tracker, sent by Jonathan. --- admin/user/user_bulk.php | 1 + admin/user/user_bulk_enrol.php | 151 +++++++++++++++++++++++++++++++++ admin/user/user_bulk_forms.php | 6 +- lang/en_utf8/admin.php | 1 + mod/forum/lib.php | 2 +- user/filters/lib.php | 2 +- 6 files changed, 159 insertions(+), 4 deletions(-) create mode 100644 admin/user/user_bulk_enrol.php diff --git a/admin/user/user_bulk.php b/admin/user/user_bulk.php index acd9a4f60f..a36261cc82 100755 --- a/admin/user/user_bulk.php +++ b/admin/user/user_bulk.php @@ -24,6 +24,7 @@ if ($data = $action_form->get_data()) { case 3: redirect($CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk_delete.php'); case 4: redirect($CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk_display.php'); case 5: redirect($CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk_download.php'); + case 6: redirect($CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk_enrol.php'); } } diff --git a/admin/user/user_bulk_enrol.php b/admin/user/user_bulk_enrol.php new file mode 100644 index 0000000000..ab7bb7968b --- /dev/null +++ b/admin/user/user_bulk_enrol.php @@ -0,0 +1,151 @@ +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 = ' '; + } + $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_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 << +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'; + } +} + +EOD; +//Form beginning +echo '
'; +echo ''; +$count = 0; +foreach($users as $user) +{ + $temparray = array ( + ''.$user->fullname.'' + ); + $mycourses = get_my_courses($user->id); + $i = 1; + foreach($courses as $acourse) + { + $temparray[$i] = ''; + if(isset($mycourses[$acourse->id])) { + // already enrolled + $temparray[$i] .= ''; + $temparray[$i] .= ""; + } else { + // unenrol + $temparray[$i] .= ''; + } + $i++; + $count++; + } + $table->data[] = $temparray; +} +print_heading("$usercount / $usertotal ".get_string('users')); +print_table($table); +echo '
'; +echo ''; +echo '
'; +echo '
'; + +admin_externalpage_print_footer(); diff --git a/admin/user/user_bulk_forms.php b/admin/user/user_bulk_forms.php index fad10cd344..3c841b4920 100644 --- a/admin/user/user_bulk_forms.php +++ b/admin/user/user_bulk_forms.php @@ -24,7 +24,9 @@ class user_bulk_action_form extends moodleform { if (has_capability('moodle/user:update', $syscontext)) { $actions[5] = get_string('download', 'admin'); } - + if (has_capability('moodle/role:assign', $syscontext)){ + $actions[6] = get_string('enrolmultipleusers', 'admin'); + } $objs = array(); $objs[] =& $mform->createElement('select', 'action', null, $actions); $objs[] =& $mform->createElement('submit', 'doaction', get_string('go')); @@ -106,4 +108,4 @@ class user_bulk_form extends moodleform { $renderer->setGroupElementTemplate($template, 'usersgrp'); } } -?> \ No newline at end of file +?> diff --git a/lang/en_utf8/admin.php b/lang/en_utf8/admin.php index e54da75843..26e6447ac6 100644 --- a/lang/en_utf8/admin.php +++ b/lang/en_utf8/admin.php @@ -334,6 +334,7 @@ $string['enablerssfeeds'] = 'Enable RSS feeds'; $string['enablestats'] = 'Enable statistics'; $string['enabletrusttext'] = 'Enable Trusted Content'; $string['encoding'] = 'Encoding'; +$string['enrolmultipleusers'] = 'Enrol the users'; $string['environment'] = 'Environment'; $string['environmenterrortodo'] = 'You must solve all the environmental problems (errors) found above before proceeding to install this Moodle version!'; $string['environmenterrorupgrade'] = 'Warning: you should solve all the environmental problems (errors) found above before proceeding to upgrade this Moodle version! Upgrading without fixing these requirements could cause problems such as data loss. Are you sure you want to continue with the upgrade?'; diff --git a/mod/forum/lib.php b/mod/forum/lib.php index 94ab2941cd..29ff1f37e1 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -5551,7 +5551,7 @@ function forum_remove_user_tracking($userid, $context) { break; case CONTEXT_COURSE: // For a whole course - if ($course = $DB->get_record('course', array('id' => $context->instanceid), '', '', '', '', 'id')) { + if ($course = $DB->get_record('course', array('id' => $context->instanceid), 'id')) { // find all forums in which this user has reading tracked if ($forums = $DB->get_records_sql("SELECT f.id, cm.id as coursemodule FROM {forum} f, diff --git a/user/filters/lib.php b/user/filters/lib.php index decedf7a72..6fc8d83e24 100644 --- a/user/filters/lib.php +++ b/user/filters/lib.php @@ -94,7 +94,7 @@ class user_filtering { * @return object filter */ function get_field($fieldname, $advanced) { - global $USER, $CFG, $DB; + global $USER, $CFG, $DB, $SITE; switch ($fieldname) { case 'username': return new user_filter_text('username', get_string('username'), $advanced, 'username'); -- 2.39.5