MDL-21781 very basic cohorts UI

This commit is contained in:
Petr Skoda
2010-04-23 09:07:51 +00:00
parent 059e689c0f
commit b980c56e2e
6 changed files with 702 additions and 0 deletions
+123
View File
@@ -0,0 +1,123 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Cohort related management functions, this file needs to be included manually.
*
* @package moodlecore
* @subpackage cohort
* @copyright 2010 Petr Skoda ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once('../config.php');
require_once($CFG->dirroot.'/cohort/lib.php');
$id = required_param('id', PARAM_INT);
require_login();
$cohort = $DB->get_record('cohort', array('id'=>$id), '*', MUST_EXIST);
$context = get_context_instance_by_id($cohort->contextid, MUST_EXIST);
require_capability('moodle/cohort:view', $context);
$PAGE->set_url('/cohort/assign.php', array('id'=>$id));
$PAGE->set_Context($context);
// TODO: ohlala, the navbar is not doing what I would expect
$PAGE->navbar->add(get_string('home'), new moodle_url('/'), navbar::TYPE_SYSTEM);
if ($context->contextlevel == CONTEXT_COURSECAT) {
$category = $DB->get_record('course_categories', array('id'=>$context->instanceid), '*', MUST_EXIST);
$PAGE->navbar->add($category->name, new moodle_url('/course/index.php', array('categoryedit'=>'1')));
}
$PAGE->navbar->add(get_string('cohorts', 'cohort'), new moodle_url('/cohort/', array('contextid'=>$context->id)));
$PAGE->navbar->add(get_string('assign', 'cohort'));
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('assignto', 'cohort', format_string($cohort->name)));
/// Get the user_selector we will need.
$potentialuserselector = new cohort_candidate_selector('addselect', array('cohortid'=>$cohort->id));
$existinguserselector = new cohort_existing_selector('removeselect', array('cohortid'=>$cohort->id));
/// Process incoming user assignments to cohorts
if (optional_param('add', false, PARAM_BOOL) && confirm_sesskey()) {
$userstoassign = $potentialuserselector->get_selected_users();
if (!empty($userstoassign)) {
foreach ($userstoassign as $adduser) {
// no duplicates please
if (!$DB->record_exists('cohort_members', array('cohortid'=>$cohort->id, 'userid'=>$adduser->id))) {
cohort_add_member($cohort->id, $adduser->id);
}
}
$potentialuserselector->invalidate_selected_users();
$existinguserselector->invalidate_selected_users();
}
}
/// Process removing user assignments to the service
if (optional_param('remove', false, PARAM_BOOL) && confirm_sesskey()) {
$userstoremove = $existinguserselector->get_selected_users();
if (!empty($userstoremove)) {
foreach ($userstoremove as $removeuser) {
cohort_remove_member($cohort->id, $removeuser->id);
}
$potentialuserselector->invalidate_selected_users();
$existinguserselector->invalidate_selected_users();
}
}
/// Print the form.
/// display the UI
?>
<form id="assignform" method="post" action="<?php echo $PAGE->url ?>"><div>
<input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
<table summary="" class="generaltable generalbox boxaligncenter" cellspacing="0">
<tr>
<td id="existingcell">
<p><label for="removeselect"><?php print_string('currentusers', 'cohort'); ?></label></p>
<?php $existinguserselector->display() ?>
</td>
<td id="buttonscell">
<div id="addcontrols">
<input name="add" id="add" type="submit" value="<?php echo $OUTPUT->larrow().'&nbsp;'.get_string('add'); ?>" title="<?php print_string('add'); ?>" /><br />
</div>
<div id="removecontrols">
<input name="remove" id="remove" type="submit" value="<?php echo get_string('remove').'&nbsp;'.$OUTPUT->rarrow(); ?>" title="<?php print_string('remove'); ?>" />
</div>
</td>
<td id="potentialcell">
<p><label for="addselect"><?php print_string('potusers', 'cohort'); ?></label></p>
<?php $potentialuserselector->display() ?>
</td>
</tr>
</table>
</div></form>
<?php
echo $OUTPUT->footer();
+126
View File
@@ -0,0 +1,126 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Cohort related management functions, this file needs to be included manually.
*
* @package moodlecore
* @subpackage cohort
* @copyright 2010 Petr Skoda ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require('../config.php');
require($CFG->dirroot.'/cohort/lib.php');
require($CFG->dirroot.'/cohort/edit_form.php');
$id = optional_param('id', 0, PARAM_INT);
$contextid = optional_param('contextid', 0, PARAM_INT);
$delete = optional_param('delete', 0, PARAM_BOOL);
$confirm = optional_param('confirm', 0, PARAM_BOOL);
require_login();
$category = null;
if ($id) {
$cohort = $DB->get_record('cohort', array('id'=>$id), '*', MUST_EXIST);
$context = get_context_instance_by_id($cohort->contextid, MUST_EXIST);
} else {
$context = get_context_instance_by_id($contextid, MUST_EXIST);
if ($context->contextlevel != CONTEXT_COURSECAT and $context->contextlevel != CONTEXT_SYSTEM) {
print_error('invalidcontext');
}
$cohort = new object();
$cohort->id = 0;
$cohort->contextid = $context->id;
$cohort->name = '';
$cohort->description = '';
}
require_capability('moodle/cohort:view', $context);
$returnurl = new moodle_url('/cohort/index.php', array('contextid'=>$context->id));
$PAGE->set_context($context);
$PAGE->set_url('/cohort/edit.php', array('contextid'=>$context->id, 'id'=>$cohort->id));
$PAGE->set_context($context);
// TODO: ohlala, the navbar is not doing what I would expect
$PAGE->navbar->add(get_string('home'), new moodle_url('/'), navbar::TYPE_SYSTEM);
if ($context->contextlevel == CONTEXT_COURSECAT) {
$category = $DB->get_record('course_categories', array('id'=>$context->instanceid), '*', MUST_EXIST);
$PAGE->navbar->add($category->name, new moodle_url('/course/index.php', array('categoryedit'=>'1')));
}
$PAGE->navbar->add(get_string('cohorts', 'cohort'), new moodle_url('/cohort/', array('contextid'=>$context->id)));
if ($delete and $cohort->id) {
$PAGE->url->param('delete', 1);
if ($confirm and confirm_sesskey()) {
cohort_delete_cohort($cohort);
redirect($returnurl);
}
$strheading = get_string('delcohort', 'cohort');
$PAGE->navbar->add($strheading);
$PAGE->set_title($strheading);
echo $OUTPUT->header();
echo $OUTPUT->heading($strheading);
$yesurl = new moodle_url('/cohort/edit.php', array('id'=>$cohort->id, 'delete'=>1, 'confirm'=>1,'sesskey'=>sesskey()));
$message = get_string('delconfirm', 'cohort', format_string($cohort->name));
echo $OUTPUT->confirm($message, $yesurl, $returnurl);
echo $OUTPUT->footer();
die;
}
$editoroptions = array('maxfiles'=>0, 'context'=>$context);
if ($cohort->id) {
// edit existing
$cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions);
$strheading = get_string('editcohort', 'cohort');
} else {
// add new
$cohort = file_prepare_standard_editor($cohort, 'description', $editoroptions);
$strheading = get_string('addcohort', 'cohort');
}
$PAGE->set_title($strheading);
$PAGE->navbar->add($strheading);
$editform = new cohort_edit_form(null, array('editoroptions'=>$editoroptions));
$editform->set_data($cohort);
if ($editform->is_cancelled()) {
redirect($returnurl);
} else if ($data = $editform->get_data()) {
$data = file_postupdate_standard_editor($data, 'description', $editoroptions, $context);
if ($data->id) {
cohort_update_cohort($data);
} else {
cohort_add_cohort($data);
}
redirect($returnurl);
}
echo $OUTPUT->header();
echo $OUTPUT->heading($strheading);
echo $editform->display();
echo $OUTPUT->footer();
+72
View File
@@ -0,0 +1,72 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Cohort related management functions, this file needs to be included manually.
*
* @package moodlecore
* @subpackage cohort
* @copyright 2010 Petr Skoda ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->dirroot.'/lib/formslib.php');
class cohort_edit_form extends moodleform {
// Define the form
function definition () {
global $USER, $CFG, $COURSE;
$mform = $this->_form;
$editoroptions = $this->_customdata['editoroptions'];
$mform->addElement('text', 'name', get_string('name', 'cohort'), 'maxlength="254" size="50"');
$mform->addRule('name', get_string('required'), 'required', null, 'client');
$mform->setType('name', PARAM_MULTILANG);
$mform->addElement('text', 'idnumber', get_string('idnumber', 'cohort'), 'maxlength="254" size="50"');
$mform->setType('name', PARAM_RAW);
$mform->addElement('editor', 'description_editor', get_string('description', 'cohort'), null, $editoroptions);
$mform->setType('description_editor', PARAM_RAW);
$mform->addElement('hidden','id');
$mform->setType('id', PARAM_INT);
$mform->addElement('hidden','contextid');
$mform->setType('contextid', PARAM_INT);
$this->add_action_buttons();
}
function validation($data, $files) {
global $DB;
$errors = parent::validation($data, $files);
$textlib = textlib_get_instance();
$idnumber = trim($data['idnumber']);
if ($data['id']) {
//TODO: validate there are no idnumber
}
return $errors;
}
}
+117
View File
@@ -0,0 +1,117 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Cohort related management functions, this file needs to be included manually.
*
* @package moodlecore
* @subpackage cohort
* @copyright 2010 Petr Skoda ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require('../config.php');
$contextid = optional_param('contextid', 0, PARAM_INT);
require_login();
if ($contextid) {
$context = get_context_instance_by_id($contextid, MUST_EXIST);
} else {
$context = get_context_instance(CONTEXT_SYSTEM);
}
if ($context->contextlevel != CONTEXT_COURSECAT and $context->contextlevel != CONTEXT_SYSTEM) {
print_error('invalidcontext');
}
$category = null;
if ($context->contextlevel == CONTEXT_COURSECAT) {
$category = $DB->get_record('course_categories', array('id'=>$context->instanceid), '*', MUST_EXIST);
}
$manager = has_capability('moodle/cohort:manage', $context);
if (!$manager) {
require_capability('moodle/cohort:view', $context);
}
// TODO: use admin_external_page in system context
$strcohorts = get_string('cohorts', 'cohort');
$PAGE->set_url('/cohort/index.php', array('contextid'=>$context->id));
$PAGE->set_title($strcohorts);
$PAGE->set_context($context);
// TODO: ohlala, the navbar is not doing what I would expect
$PAGE->navbar->add(get_string('home'), new moodle_url('/'), navbar::TYPE_SYSTEM);
if ($category) {
$PAGE->navbar->add($category->name, new moodle_url('/course/index.php', array('categoryedit'=>'1')));
}
$PAGE->navbar->add($strcohorts);
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('cohortsin', 'cohort', print_context_name($context)));
$cohorts = $DB->get_records('cohort', array('contextid'=>$context->id));
$data = array();
foreach($cohorts as $cohort) {
$line = array();
$line[] = format_string($cohort->name);
$line[] = $cohort->idnumber;
$line[] = format_text($cohort->description, $cohort->descriptionformat);
$line[] = $DB->count_records('cohort_members', array('cohortid'=>$cohort->id));
if (empty($cohort->component)) {
$line[] = get_string('nocomponent', 'cohort');
} else {
$line[] = get_string('pluginname', $cohort->component);
}
if ($manager) {
if (empty($cohort->component)) {
$buttons = html_writer::link(new moodle_url('/cohort/edit.php', array('id'=>$cohort->id)), get_string('edit'));
$buttons .= ' '.html_writer::link(new moodle_url('/cohort/edit.php', array('id'=>$cohort->id, 'delete'=>1)), get_string('delete'));
$buttons .= ' '.html_writer::link(new moodle_url('/cohort/assign.php', array('id'=>$cohort->id)), get_string('assign', 'cohort'));
} else {
$buttons = '';
}
} else {
$buttons = '';
}
$line[] = $buttons;
$data[] = $line;
}
$table = new html_table();
$table->head = array(get_string('name', 'cohort'), get_string('idnumber', 'cohort'), get_string('description', 'cohort'),
get_string('memberscount', 'cohort'), get_string('component', 'cohort'), get_string('edit'));
$table->size = array('20%', '10%', '40%', '10%', '10%', '10%');
$table->align = array('left', 'left', 'left', 'left','center', 'center');
$table->width = '80%';
$table->data = $data;
echo html_writer::table($table);
if ($manager) {
echo $OUTPUT->single_button(new moodle_url('/cohort/edit.php', array('contextid'=>$context->id)), get_string('add'));
}
echo $OUTPUT->footer();
+216
View File
@@ -0,0 +1,216 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Cohort related management functions, this file needs to be included manually.
*
* @package moodlecore
* @subpackage cohort
* @copyright 2010 Petr Skoda ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->dirroot . '/user/selector/lib.php');
/**
* Add new cohort.
* @param object $data
* @return void
*/
function cohort_add_cohort($data) {
global $DB;
$data->timecreated = time();
$data->timemodified = $data->timecreated;
$DB->insert_record('cohort', $data);
}
/**
* Update existing cohort.
* @param object $data
* @return void
*/
function cohort_update_cohort($data) {
global $DB;
$data->timemodified = time();
$DB->update_record('cohort', $data);
}
/**
* Delete cohort.
* @param object $cohort
* @return void
*/
function cohort_delete_cohort($cohort) {
global $DB;
if ($cohort->component) {
// TODO: add component delete callback
}
$DB->delete_records('cohort_members', array('cohortid'=>$cohort->id));
$DB->delete_records('cohort', array('id'=>$cohort->id));
}
/**
* Remove cohort member
* @param int $cohortid
* @param int $userid
* @return void
*/
function cohort_add_member($cohortid, $userid) {
global $DB;
$record = new object();
$record->cohortid = $cohortid;
$record->userid = $userid;
$record->timeadded = time();
$DB->insert_record('cohort_members', $record);
}
/**
* Add cohort member
* @param int $cohortid
* @param int $userid
* @return void
*/
function cohort_remove_member($cohortid, $userid) {
global $DB;
$DB->delete_records('cohort_members', array('cohortid'=>$cohortid, 'userid'=>$userid));
}
/**
* Cohort assignment candidates
*/
class cohort_candidate_selector extends user_selector_base {
protected $cohortid;
public function __construct($name, $options) {
$this->cohortid = $options['cohortid'];
parent::__construct($name, $options);
}
/**
* Candidate users
* @param <type> $search
* @return array
*/
public function find_users($search) {
global $DB;
//by default wherecondition retrieves all users except the deleted, not confirmed and guest
list($wherecondition, $params) = $this->search_sql($search, 'u');
$params['cohortid'] = $this->cohortid;
$fields = 'SELECT ' . $this->required_fields_sql('u');
$countfields = 'SELECT COUNT(1)';
$sql = " FROM {user} u
LEFT JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid)
WHERE cm.id IS NULL AND $wherecondition";
$order = ' ORDER BY u.lastname ASC, u.firstname ASC';
if (!$this->is_validating()) {
$potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
if ($potentialmemberscount > 100) {
return $this->too_many_results($search, $potentialmemberscount);
}
}
$availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
if (empty($availableusers)) {
return array();
}
if ($search) {
$groupname = get_string('potusersmatching', 'cohort', $search);
}
else {
$groupname = get_string('potusers', 'cohort');
}
return array($groupname => $availableusers);
}
protected function get_options() {
$options = parent::get_options();
$options['cohortid'] = $this->cohortid;
return $options;
}
}
/**
* Cohort assignment candidates
*/
class cohort_existing_selector extends user_selector_base {
protected $cohortid;
public function __construct($name, $options) {
$this->cohortid = $options['cohortid'];
parent::__construct($name, $options);
}
/**
* Candidate users
* @param <type> $search
* @return array
*/
public function find_users($search) {
global $DB;
//by default wherecondition retrieves all users except the deleted, not confirmed and guest
list($wherecondition, $params) = $this->search_sql($search, 'u');
$params['cohortid'] = $this->cohortid;
$fields = 'SELECT ' . $this->required_fields_sql('u');
$countfields = 'SELECT COUNT(1)';
$sql = " FROM {user} u
JOIN {cohort_members} cm ON (cm.userid = u.id AND cm.cohortid = :cohortid)
WHERE $wherecondition";
$order = ' ORDER BY u.lastname ASC, u.firstname ASC';
if (!$this->is_validating()) {
$potentialmemberscount = $DB->count_records_sql($countfields . $sql, $params);
if ($potentialmemberscount > 100) {
return $this->too_many_results($search, $potentialmemberscount);
}
}
$availableusers = $DB->get_records_sql($fields . $sql . $order, $params);
if (empty($availableusers)) {
return array();
}
if ($search) {
$groupname = get_string('currentusersmatching', 'cohort', $search);
}
else {
$groupname = get_string('currentusers', 'cohort');
}
return array($groupname => $availableusers);
}
protected function get_options() {
$options = parent::get_options();
$options['cohortid'] = $this->cohortid;
return $options;
}
}
+48
View File
@@ -0,0 +1,48 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Strings for component 'cohort', language 'en', branch 'MOODLE_20_STABLE'
*
* @package moodlecore
* @subpackage cohort
* @copyright 2010 Petr Skoda ([email protected])
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['addcohort'] = 'Add new cohort';
$string['assign'] = 'Assign';
$string['assignto'] = 'Cohort \'{$a}\' members';
$string['cohort'] = 'Cohort';
$string['cohorts'] = 'Cohorts';
$string['cohortsin'] = '{$a}: available cohorts';
$string['cohort:assign'] = 'Assign cohort members';
$string['cohort:manage'] = 'Manager cohorts';
$string['cohort:view'] = 'Use cohorts and view members';
$string['component'] = 'Source';
$string['currentusers'] = 'Current users';
$string['currentusersmatching'] = 'Current users matching';
$string['delcohort'] = 'Delete cohort';
$string['delconfirm'] = 'Do you really want to delete cohort $a?';
$string['description'] = 'Description';
$string['editcohort'] = 'Edit cohort';
$string['idnumber'] = 'ID number';
$string['memberscount'] = 'Cohort size';
$string['name'] = 'Name';
$string['nocomponent'] = 'Created manually';
$string['potusers'] = 'Potential users';
$string['potusersmatching'] = 'Potential matching users';