2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
20 * @package enrol_cohort
21 * @copyright 2010 Petr Skoda {@link http://skodak.org}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
27 require_once("$CFG->libdir/formslib.php");
29 class enrol_cohort_edit_form
extends moodleform
{
31 function definition() {
34 $mform = $this->_form
;
36 list($instance, $plugin, $course) = $this->_customdata
;
37 $coursecontext = context_course
::instance($course->id
);
39 $enrol = enrol_get_plugin('cohort');
42 $groups = array(0 => get_string('none'));
43 foreach (groups_get_all_groups($course->id
) as $group) {
44 $groups[$group->id
] = format_string($group->name
, true, array('context'=>$coursecontext));
47 $mform->addElement('header','general', get_string('pluginname', 'enrol_cohort'));
49 $mform->addElement('text', 'name', get_string('custominstancename', 'enrol'));
50 $mform->setType('name', PARAM_TEXT
);
52 $options = array(ENROL_INSTANCE_ENABLED
=> get_string('yes'),
53 ENROL_INSTANCE_DISABLED
=> get_string('no'));
54 $mform->addElement('select', 'status', get_string('status', 'enrol_cohort'), $options);
57 if ($cohort = $DB->get_record('cohort', array('id'=>$instance->customint1
))) {
58 $cohorts = array($instance->customint1
=>format_string($cohort->name
, true, array('context'=>context
::instance_by_id($cohort->contextid
))));
60 $cohorts = array($instance->customint1
=>get_string('error'));
62 $mform->addElement('select', 'customint1', get_string('cohort', 'cohort'), $cohorts);
63 $mform->setConstant('customint1', $instance->customint1
);
64 $mform->hardFreeze('customint1', $instance->customint1
);
67 $cohorts = array('' => get_string('choosedots'));
68 list($sqlparents, $params) = $DB->get_in_or_equal($coursecontext->get_parent_context_ids());
69 $sql = "SELECT id, name, idnumber, contextid
71 WHERE contextid $sqlparents
72 ORDER BY name ASC, idnumber ASC";
73 $rs = $DB->get_recordset_sql($sql, $params);
75 $context = context
::instance_by_id($c->contextid
);
76 if (!has_capability('moodle/cohort:view', $context)) {
79 $cohorts[$c->id
] = format_string($c->name
);
82 $mform->addElement('select', 'customint1', get_string('cohort', 'cohort'), $cohorts);
83 $mform->addRule('customint1', get_string('required'), 'required', null, 'client');
86 $roles = get_assignable_roles($coursecontext);
87 $roles[0] = get_string('none');
88 $roles = array_reverse($roles, true); // Descending default sortorder.
89 $mform->addElement('select', 'roleid', get_string('assignrole', 'enrol_cohort'), $roles);
90 $mform->setDefault('roleid', $enrol->get_config('roleid'));
91 if ($instance->id
and !isset($roles[$instance->roleid
])) {
92 if ($role = $DB->get_record('role', array('id'=>$instance->roleid
))) {
93 $roles = role_fix_names($roles, $coursecontext, ROLENAME_ALIAS
, true);
94 $roles[$instance->roleid
] = role_get_name($role, $coursecontext);
96 $roles[$instance->roleid
] = get_string('error');
99 $mform->addElement('select', 'customint2', get_string('addgroup', 'enrol_cohort'), $groups);
101 $mform->addElement('hidden', 'courseid', null);
102 $mform->setType('courseid', PARAM_INT
);
104 $mform->addElement('hidden', 'id', null);
105 $mform->setType('id', PARAM_INT
);
108 $this->add_action_buttons(true);
110 $this->add_action_buttons(true, get_string('addinstance', 'enrol'));
113 $this->set_data($instance);
116 function validation($data, $files) {
119 $errors = parent
::validation($data, $files);
121 $params = array('roleid'=>$data['roleid'], 'customint1'=>$data['customint1'], 'courseid'=>$data['courseid'], 'id'=>$data['id']);
122 if ($DB->record_exists_select('enrol', "roleid = :roleid AND customint1 = :customint1 AND courseid = :courseid AND enrol = 'cohort' AND id <> :id", $params)) {
123 $errors['roleid'] = get_string('instanceexists', 'enrol_cohort');