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/>.
18 * Adds new instance of enrol_cohort to specified course.
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 require('../../config.php');
26 require_once("$CFG->dirroot/enrol/cohort/edit_form.php");
27 require_once("$CFG->dirroot/enrol/cohort/locallib.php");
28 require_once("$CFG->dirroot/group/lib.php");
30 $courseid = required_param('courseid', PARAM_INT
);
31 $instanceid = optional_param('id', 0, PARAM_INT
);
33 $course = $DB->get_record('course', array('id'=>$courseid), '*', MUST_EXIST
);
34 $context = context_course
::instance($course->id
, MUST_EXIST
);
36 require_login($course);
37 require_capability('moodle/course:enrolconfig', $context);
38 require_capability('enrol/cohort:config', $context);
40 $PAGE->set_url('/enrol/cohort/edit.php', array('courseid'=>$course->id
, 'id'=>$instanceid));
41 $PAGE->set_pagelayout('admin');
43 $returnurl = new moodle_url('/enrol/instances.php', array('id'=>$course->id
));
44 if (!enrol_is_enabled('cohort')) {
48 $enrol = enrol_get_plugin('cohort');
51 $instance = $DB->get_record('enrol', array('courseid'=>$course->id
, 'enrol'=>'cohort', 'id'=>$instanceid), '*', MUST_EXIST
);
54 // No instance yet, we have to add new instance.
55 if (!$enrol->get_newinstance_link($course->id
)) {
58 navigation_node
::override_active_url(new moodle_url('/enrol/instances.php', array('id'=>$course->id
)));
59 $instance = new stdClass();
61 $instance->courseid
= $course->id
;
62 $instance->enrol
= 'cohort';
63 $instance->customint1
= ''; // Cohort id.
64 $instance->customint2
= 0; // Optional group id.
67 // Try and make the manage instances node on the navigation active.
68 $courseadmin = $PAGE->settingsnav
->get('courseadmin');
69 if ($courseadmin && $courseadmin->get('users') && $courseadmin->get('users')->get('manageinstances')) {
70 $courseadmin->get('users')->get('manageinstances')->make_active();
74 $mform = new enrol_cohort_edit_form(null, array($instance, $enrol, $course));
76 if ($mform->is_cancelled()) {
79 } else if ($data = $mform->get_data()) {
81 // NOTE: no cohort changes here!!!
82 if ($data->roleid
!= $instance->roleid
) {
83 // The sync script can only add roles, for perf reasons it does not modify them.
84 role_unassign_all(array('contextid'=>$context->id
, 'roleid'=>$instance->roleid
, 'component'=>'enrol_cohort', 'itemid'=>$instance->id
));
86 $instance->name
= $data->name
;
87 $instance->status
= $data->status
;
88 $instance->roleid
= $data->roleid
;
89 $instance->customint2
= $data->customint2
;
90 $instance->timemodified
= time();
91 $DB->update_record('enrol', $instance);
93 $enrol->add_instance($course, array('name'=>$data->name
, 'status'=>$data->status
, 'customint1'=>$data->customint1
, 'roleid'=>$data->roleid
, 'customint2'=>$data->customint2
));
95 $trace = new null_progress_trace();
96 enrol_cohort_sync($trace, $course->id
);
101 $PAGE->set_heading($course->fullname
);
102 $PAGE->set_title(get_string('pluginname', 'enrol_cohort'));
104 echo $OUTPUT->header();
106 echo $OUTPUT->footer();