Merge branch 'MDL-38821_master' of git://github.com/dmonllao/moodle
[moodle.git] / enrol / cohort / edit_form.php
blob7f3011f2ed3b4e55e5185ed352be6be0895266d1
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
17 /**
18 * Adds instance form
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() {
32 global $CFG, $DB;
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);
56 if ($instance->id) {
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))));
59 } else {
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);
66 } else {
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
70 FROM {cohort}
71 WHERE contextid $sqlparents
72 ORDER BY name ASC, idnumber ASC";
73 $rs = $DB->get_recordset_sql($sql, $params);
74 foreach ($rs as $c) {
75 $context = context::instance_by_id($c->contextid);
76 if (!has_capability('moodle/cohort:view', $context)) {
77 continue;
79 $cohorts[$c->id] = format_string($c->name);
81 $rs->close();
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);
95 } else {
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);
107 if ($instance->id) {
108 $this->add_action_buttons(true);
109 } else {
110 $this->add_action_buttons(true, get_string('addinstance', 'enrol'));
113 $this->set_data($instance);
116 function validation($data, $files) {
117 global $DB;
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');
126 return $errors;