diff --git a/admin/user/user_bulk.php b/admin/user/user_bulk.php index 442e985cf19b5..62a4d299cd916 100644 --- a/admin/user/user_bulk.php +++ b/admin/user/user_bulk.php @@ -26,6 +26,7 @@ case 5: redirect($CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk_download.php'); //case 6: redirect($CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk_enrol.php'); //TODO: MDL-24064 case 7: redirect($CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk_forcepasswordchange.php'); + case 8: redirect($CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk_cohortadd.php'); } } @@ -80,6 +81,7 @@ $ufiltering->display_active(); $user_bulk_form->display(); + $action_form->display(); echo $OUTPUT->footer(); diff --git a/admin/user/user_bulk_cohortadd.php b/admin/user/user_bulk_cohortadd.php new file mode 100644 index 0000000000000..f97216d89772e --- /dev/null +++ b/admin/user/user_bulk_cohortadd.php @@ -0,0 +1,143 @@ +. + +/** + * script for bulk user multi cohort add + * + * @package core + * @subpackage user + * @copyright 2011 Petr Skoda (http://skodak.org) + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +require('../../config.php'); +require_once($CFG->libdir.'/adminlib.php'); +require_once('user_bulk_cohortadd_form.php'); +require_once("$CFG->dirroot/cohort/lib.php"); + +$sort = optional_param('sort', 'fullname', PARAM_ALPHA); +$dir = optional_param('dir', 'asc', PARAM_ALPHA); + +admin_externalpage_setup('userbulk'); +require_capability('moodle/cohort:assign', get_context_instance(CONTEXT_SYSTEM)); + +$users = $SESSION->bulk_users; + +$strnever = get_string('never'); + +$cohorts = array(''=>get_string('choosedots')); +$allcohorts = $DB->get_records('cohort'); +foreach ($allcohorts as $c) { + if (!empty($c->component)) { + // external cohorts can not be modified + continue; + } + $context = get_context_instance_by_id($c->contextid); + if (!has_capability('moodle/cohort:assign', $context)) { + continue; + } + + if (empty($c->idnumber)) { + $cohorts[$c->id] = format_string($c->name); + } else { + $cohorts[$c->id] = format_string($c->name) . ' [' . $c->idnumber . ']'; + } +} +unset($allcohorts); + +if (count($cohorts) < 2) { + echo $OUTPUT->header(); + echo $OUTPUT->heading(get_string('bulkadd', 'core_cohort')); + echo $OUTPUT->notification(get_string('bulknocohort', 'core_cohort')); + echo $OUTPUT->continue_button(new moodle_url('/admin/user/user_bulk.php')); + echo $OUTPUT->footer(); + die; +} + +$countries = get_string_manager()->get_list_of_countries(true); +foreach ($users as $key => $id) { + $user = $DB->get_record('user', array('id'=>$id, 'deleted'=>0), 'id, firstname, lastname, username, email, country, lastaccess, city'); + $user->fullname = fullname($user, true); + $user->country = @$countries[$user->country]; + unset($user->firstname); + unset($user->lastname); + $users[$key] = $user; +} +unset($countries); + +$mform = new user_bulk_cohortadd_form(null, $cohorts); + +if (empty($users) or $mform->is_cancelled()) { + redirect(new moodle_url('/admin/user/user_bulk.php')); + +} else if ($data = $mform->get_data()) { + // process request + foreach ($users as $user) { + if (!$DB->record_exists('cohort_members', array('cohortid'=>$data->cohort, 'userid'=>$user->id))) { + cohort_add_member($data->cohort, $user->id); + } + } + redirect(new moodle_url('/admin/user/user_bulk.php')); +} + +// 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'); + +$table = new html_table(); +$table->width = "95%"; +$columns = array('fullname', 'email', 'city', 'country', 'lastaccess'); +foreach ($columns as $column) { + $strtitle = get_string($column); + if ($sort != $column) { + $columnicon = ''; + $columndir = 'asc'; + } else { + $columndir = ($dir == 'asc') ? 'desc' : 'asc'; + $columnicon = ' '; + } + $table->head[] = ''.$strtitle.''.$columnicon; + $table->align[] = 'left'; +} + +foreach ($users as $user) { + $table->data[] = array ( + ''.$user->fullname.'', + $user->email, + $user->city, + $user->country, + $user->lastaccess ? format_time(time() - $user->lastaccess) : $strnever + ); +} + +echo $OUTPUT->header(); +echo $OUTPUT->heading(get_string('bulkadd', 'core_cohort')); + +echo html_writer::table($table); + +echo $OUTPUT->box_start(); +$mform->display(); +echo $OUTPUT->box_end(); + +echo $OUTPUT->footer(); diff --git a/admin/user/user_bulk_cohortadd_form.php b/admin/user/user_bulk_cohortadd_form.php new file mode 100644 index 0000000000000..5ba19cce557cc --- /dev/null +++ b/admin/user/user_bulk_cohortadd_form.php @@ -0,0 +1,39 @@ +. + +/** + * form for bulk user multi cohort add + * + * @package core + * @subpackage user + * @copyright 2011 Petr Skoda (http://skodak.org) + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +defined('MOODLE_INTERNAL') || die(); + +require_once($CFG->libdir.'/formslib.php'); + +class user_bulk_cohortadd_form extends moodleform { + function definition() { + $mform = $this->_form; + $cohorts = $this->_customdata; + + $mform->addElement('select', 'cohort', get_string('cohort', 'core_cohort'), $cohorts); + $mform->addRule('cohort', get_string('required'), 'required', null, 'client'); + + $this->add_action_buttons(true, get_string('bulkadd', 'core_cohort')); + } +} diff --git a/admin/user/user_bulk_forms.php b/admin/user/user_bulk_forms.php index ea210c20028ee..fa119be581235 100644 --- a/admin/user/user_bulk_forms.php +++ b/admin/user/user_bulk_forms.php @@ -31,6 +31,9 @@ function definition() { if (has_capability('moodle/user:update', $syscontext)) { $actions[7] = get_string('forcepasswordchange'); } + if (has_capability('moodle/cohort:assign', $syscontext)) { + $actions[8] = get_string('bulkadd', 'core_cohort'); + } $objs = array(); $objs[] =& $mform->createElement('select', 'action', null, $actions); $objs[] =& $mform->createElement('submit', 'doaction', get_string('go')); diff --git a/lang/en/cohort.php b/lang/en/cohort.php index a7786d05050a8..90abea83df805 100644 --- a/lang/en/cohort.php +++ b/lang/en/cohort.php @@ -29,6 +29,8 @@ $string['assign'] = 'Assign'; $string['assignto'] = 'Cohort \'{$a}\' members'; $string['backtocohorts'] = 'Back to cohorts'; +$string['bulkadd'] = 'Add to cohort'; +$string['bulknocohort'] = 'No available cohorts found'; $string['cohort'] = 'Cohort'; $string['cohorts'] = 'Cohorts'; $string['cohortsin'] = '{$a}: available cohorts';