Skip to content

Commit

Permalink
MDL-21781 very basic cohorts UI
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Apr 23, 2010
1 parent 059e689 commit b980c56
Show file tree
Hide file tree
Showing 6 changed files with 702 additions and 0 deletions.
123 changes: 123 additions & 0 deletions cohort/assign.php
Original file line number Diff line number Diff line change
@@ -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 changes: 126 additions & 0 deletions cohort/edit.php
Original file line number Diff line number Diff line change
@@ -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 changes: 72 additions & 0 deletions cohort/edit_form.php
Original file line number Diff line number Diff line change
@@ -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;
}
}

Loading

0 comments on commit b980c56

Please sign in to comment.