diff --git a/cohort/assign.php b/cohort/assign.php new file mode 100644 index 0000000000000..b52eed255339a --- /dev/null +++ b/cohort/assign.php @@ -0,0 +1,123 @@ +. + +/** + * Cohort related management functions, this file needs to be included manually. + * + * @package moodlecore + * @subpackage cohort + * @copyright 2010 Petr Skoda (info@skodak.org) + * @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 + +?> +
+ +footer(); diff --git a/cohort/edit.php b/cohort/edit.php new file mode 100644 index 0000000000000..fe9350717f2cb --- /dev/null +++ b/cohort/edit.php @@ -0,0 +1,126 @@ +. + + +/** + * Cohort related management functions, this file needs to be included manually. + * + * @package moodlecore + * @subpackage cohort + * @copyright 2010 Petr Skoda (info@skodak.org) + * @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(); + diff --git a/cohort/edit_form.php b/cohort/edit_form.php new file mode 100644 index 0000000000000..c62322ca640aa --- /dev/null +++ b/cohort/edit_form.php @@ -0,0 +1,72 @@ +. + +/** + * Cohort related management functions, this file needs to be included manually. + * + * @package moodlecore + * @subpackage cohort + * @copyright 2010 Petr Skoda (info@skodak.org) + * @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; + } +} + \ No newline at end of file diff --git a/cohort/index.php b/cohort/index.php new file mode 100644 index 0000000000000..f3273c4054ec8 --- /dev/null +++ b/cohort/index.php @@ -0,0 +1,117 @@ +. + +/** + * Cohort related management functions, this file needs to be included manually. + * + * @package moodlecore + * @subpackage cohort + * @copyright 2010 Petr Skoda (info@skodak.org) + * @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(); \ No newline at end of file diff --git a/cohort/lib.php b/cohort/lib.php new file mode 100644 index 0000000000000..2eda91de6beb9 --- /dev/null +++ b/cohort/lib.php @@ -0,0 +1,216 @@ +. + +/** + * Cohort related management functions, this file needs to be included manually. + * + * @package moodlecore + * @subpackage cohort + * @copyright 2010 Petr Skoda (info@skodak.org) + * @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