MDL-21781 cohorts may be moved to different course categories/system context

This commit is contained in:
Petr Skoda 2010-04-23 09:13:49 +00:00
parent 864e2805d0
commit 9e1065a812
2 changed files with 33 additions and 9 deletions

View file

@ -99,8 +99,7 @@ if ($cohort->id) {
$PAGE->set_title($strheading); $PAGE->set_title($strheading);
$PAGE->navbar->add($strheading); $PAGE->navbar->add($strheading);
$editform = new cohort_edit_form(null, array('editoroptions'=>$editoroptions)); $editform = new cohort_edit_form(null, array('editoroptions'=>$editoroptions, 'data'=>$cohort));
$editform->set_data($cohort);
if ($editform->is_cancelled()) { if ($editform->is_cancelled()) {
redirect($returnurl); redirect($returnurl);
@ -114,7 +113,8 @@ if ($editform->is_cancelled()) {
cohort_add_cohort($data); cohort_add_cohort($data);
} }
redirect($returnurl); // use new context id, it could have been changed
redirect(new moodle_url('/cohort/index.php', array('contextid'=>$data->contextid)));
} }
echo $OUTPUT->header(); echo $OUTPUT->header();

View file

@ -29,17 +29,21 @@ require_once($CFG->dirroot . '/lib/formslib.php');
class cohort_edit_form extends moodleform { class cohort_edit_form extends moodleform {
/** /**
* Define the form * Define the cohort edit form
*/ */
function definition() { public function definition() {
$mform = $this->_form; $mform = $this->_form;
$editoroptions = $this->_customdata['editoroptions']; $editoroptions = $this->_customdata['editoroptions'];
$cohort = $this->_customdata['data'];
$mform->addElement('text', 'name', get_string('name', 'cohort'), 'maxlength="254" size="50"'); $mform->addElement('text', 'name', get_string('name', 'cohort'), 'maxlength="254" size="50"');
$mform->addRule('name', get_string('required'), 'required', null, 'client'); $mform->addRule('name', get_string('required'), 'required', null, 'client');
$mform->setType('name', PARAM_MULTILANG); $mform->setType('name', PARAM_MULTILANG);
$options = $this->get_category_options($cohort->contextid);
$mform->addElement('select', 'contextid', get_string('context', 'role'), $options);
$mform->addElement('text', 'idnumber', get_string('idnumber', 'cohort'), 'maxlength="254" size="50"'); $mform->addElement('text', 'idnumber', get_string('idnumber', 'cohort'), 'maxlength="254" size="50"');
$mform->setType('name', PARAM_RAW); $mform->setType('name', PARAM_RAW);
@ -49,13 +53,12 @@ class cohort_edit_form extends moodleform {
$mform->addElement('hidden', 'id'); $mform->addElement('hidden', 'id');
$mform->setType('id', PARAM_INT); $mform->setType('id', PARAM_INT);
$mform->addElement('hidden', 'contextid');
$mform->setType('contextid', PARAM_INT);
$this->add_action_buttons(); $this->add_action_buttons();
$this->set_data($cohort);
} }
function validation($data, $files) { public function validation($data, $files) {
global $DB; global $DB;
$errors = parent::validation($data, $files); $errors = parent::validation($data, $files);
@ -76,5 +79,26 @@ class cohort_edit_form extends moodleform {
return $errors; return $errors;
} }
protected function get_category_options($currentcontextid) {
$displaylist = array();
$parentlist = array();
make_categories_list($displaylist, $parentlist, 'moodle/cohort:manage');
$options = array();
$syscontext = get_context_instance(CONTEXT_SYSTEM);
if (has_capability('moodle/cohort:manage', $syscontext)) {
$options[$syscontext->id] = print_context_name($syscontext);
}
foreach ($displaylist as $cid=>$name) {
$context = get_context_instance(CONTEXT_COURSECAT, $cid, MUST_EXIST);
$options[$context->id] = $name;
}
// always add current - this is not likely, but if the logic get's changed it might be a problem
if (!isset($options[$currentcontextid])) {
$context = get_context_instance_by_id($currentcontextid, MUST_EXIST);
$options[$context->id] = print_context_name($syscontext);
}
return $options;
}
} }