MDL-10873 If both site default and user pref are empty for studentsperpage, we assume...
[moodle-pu.git] / course / edit.php
blobc7f3c052ebf4e5815353f8e923e9d248d3dd9fbc
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 if ($data->metacourse and has_capability('moodle/course:managemetacourse', $context)) {
104 // Redirect users with metacourse capability to student import
105 redirect($CFG->wwwroot."/course/importstudents.php?id=$course->id");
106 } else {
107 // Redirect to roles assignment
108 redirect($CFG->wwwroot."/$CFG->admin/roles/assign.php?contextid=$context->id");
111 } else {
112 if (!update_course($data)) {
113 print_error('coursenotupdated');
115 // MDL-9983
116 events_trigger('course_updated', $data);
117 redirect($CFG->wwwroot."/course/view.php?id=$course->id");
122 /// Print the form
124 $site = get_site();
126 $streditcoursesettings = get_string("editcoursesettings");
127 $straddnewcourse = get_string("addnewcourse");
128 $stradministration = get_string("administration");
129 $strcategories = get_string("categories");
130 $navlinks = array();
132 if (!empty($course)) {
133 $navlinks[] = array('name' => $streditcoursesettings,
134 'link' => null,
135 'type' => 'misc');
136 $title = $streditcoursesettings;
137 $fullname = $course->fullname;
138 } else {
139 $navlinks[] = array('name' => $stradministration,
140 'link' => "$CFG->wwwroot/$CFG->admin/index.php",
141 'type' => 'misc');
142 $navlinks[] = array('name' => $strcategories,
143 'link' => 'index.php',
144 'type' => 'misc');
145 $navlinks[] = array('name' => $straddnewcourse,
146 'link' => null,
147 'type' => 'misc');
148 $title = "$site->shortname: $straddnewcourse";
149 $fullname = $site->fullname;
152 $navigation = build_navigation($navlinks);
153 print_header($title, $fullname, $navigation, $editform->focus());
154 print_heading($streditcoursesettings);
156 $editform->display();
158 print_footer($course);