MDL-11328 Added the missing variable
[moodle-pu.git] / course / edit.php
blobf25fb849f57437412ec69b8aa247ec2d4533a311
1 <?php // $Id$
2 // Edit course settings
4 require_once('../config.php');
5 require_once($CFG->dirroot.'/enrol/enrol.class.php');
6 require_once($CFG->libdir.'/blocklib.php');
7 require_once('lib.php');
8 require_once('edit_form.php');
10 $id = optional_param('id', 0, PARAM_INT); // course id
11 $categoryid = optional_param('category', 0, PARAM_INT); // course category - can be changed in edit form
14 /// basic access control checks
15 if ($id) { // editing course
16 if (!$course = get_record('course', 'id', $id)) {
17 error('Course ID was incorrect');
19 require_login($course->id);
20 $category = get_record('course_categories', 'id', $course->category);
21 require_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $course->id));
23 } else if ($categoryid) { // creating new course in this category
24 $course = null;
25 require_login();
26 if (!$category = get_record('course_categories', 'id', $categoryid)) {
27 error('Category ID was incorrect');
29 require_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $category->id));
30 } else {
31 require_login();
32 error('Either course id or category must be specified');
35 /// prepare course
36 if (!empty($course)) {
37 $allowedmods = array();
38 if (!empty($course)) {
39 if ($am = get_records('course_allowed_modules','course',$course->id)) {
40 foreach ($am as $m) {
41 $allowedmods[] = $m->module;
43 } else {
44 if (empty($course->restrictmodules)) {
45 $allowedmods = explode(',',$CFG->defaultallowedmodules);
46 } // it'll be greyed out but we want these by default anyway.
48 $course->allowedmods = $allowedmods;
50 if ($course->enrolstartdate){
51 $course->enrolstartdisabled = 0;
54 if ($course->enrolenddate) {
55 $course->enrolenddisabled = 0;
61 /// first create the form
62 $editform = new course_edit_form('edit.php', compact('course', 'category'));
63 // now override defaults if course already exists
64 if (!empty($course)) {
65 $course->enrolpassword = $course->password; // we need some other name for password field MDL-9929
66 $editform->set_data($course);
68 if ($editform->is_cancelled()){
69 if (empty($course)) {
70 redirect($CFG->wwwroot);
71 } else {
72 redirect($CFG->wwwroot.'/course/view.php?id='.$course->id);
75 } else if ($data = $editform->get_data()) {
77 $data->password = $data->enrolpassword; // we need some other name for password field MDL-9929
78 /// process data if submitted
80 //preprocess data
81 if ($data->enrolstartdisabled){
82 $data->enrolstartdate = 0;
85 if ($data->enrolenddisabled) {
86 $data->enrolenddate = 0;
89 $data->timemodified = time();
91 if (empty($course)) {
92 if (!$course = create_course($data)) {
93 print_error('coursenotcreated');
96 $context = get_context_instance(CONTEXT_COURSE, $course->id);
98 // assign default role to creator if not already having permission to manage course assignments
99 if (!has_capability('moodle/course:view', $context) or !has_capability('moodle/role:assign', $context)) {
100 role_assign($CFG->creatornewroleid, $USER->id, 0, $context->id);
103 // ensure we can use the course right after creating it
104 // this means trigger a reload of accessinfo...
105 mark_context_dirty($context->path);
107 if ($data->metacourse and has_capability('moodle/course:managemetacourse', $context)) {
108 // Redirect users with metacourse capability to student import
109 redirect($CFG->wwwroot."/course/importstudents.php?id=$course->id");
110 } else {
111 // Redirect to roles assignment
112 redirect($CFG->wwwroot."/$CFG->admin/roles/assign.php?contextid=$context->id");
115 } else {
116 if (!update_course($data)) {
117 print_error('coursenotupdated');
119 // MDL-9983
120 events_trigger('course_updated', $data);
121 redirect($CFG->wwwroot."/course/view.php?id=$course->id");
126 /// Print the form
128 $site = get_site();
130 $streditcoursesettings = get_string("editcoursesettings");
131 $straddnewcourse = get_string("addnewcourse");
132 $stradministration = get_string("administration");
133 $strcategories = get_string("categories");
134 $navlinks = array();
136 if (!empty($course)) {
137 $navlinks[] = array('name' => $streditcoursesettings,
138 'link' => null,
139 'type' => 'misc');
140 $title = $streditcoursesettings;
141 $fullname = $course->fullname;
142 } else {
143 $navlinks[] = array('name' => $stradministration,
144 'link' => "$CFG->wwwroot/$CFG->admin/index.php",
145 'type' => 'misc');
146 $navlinks[] = array('name' => $strcategories,
147 'link' => 'index.php',
148 'type' => 'misc');
149 $navlinks[] = array('name' => $straddnewcourse,
150 'link' => null,
151 'type' => 'misc');
152 $title = "$site->shortname: $straddnewcourse";
153 $fullname = $site->fullname;
156 $navigation = build_navigation($navlinks);
157 print_header($title, $fullname, $navigation, $editform->focus());
158 print_heading($streditcoursesettings);
160 $editform->display();
162 print_footer($course);