|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00026 require_once('../config.php'); 00027 require_once('lib.php'); 00028 require_once('edit_form.php'); 00029 00030 $id = optional_param('id', 0, PARAM_INT); // course id 00031 $categoryid = optional_param('category', 0, PARAM_INT); // course category - can be changed in edit form 00032 $returnto = optional_param('returnto', 0, PARAM_ALPHANUM); // generic navigation return page switch 00033 00034 $PAGE->set_pagelayout('admin'); 00035 $PAGE->set_url('/course/edit.php'); 00036 00037 // basic access control checks 00038 if ($id) { // editing course 00039 if ($id == SITEID){ 00040 // don't allow editing of 'site course' using this from 00041 print_error('cannoteditsiteform'); 00042 } 00043 00044 $course = $DB->get_record('course', array('id'=>$id), '*', MUST_EXIST); 00045 require_login($course); 00046 $category = $DB->get_record('course_categories', array('id'=>$course->category), '*', MUST_EXIST); 00047 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); 00048 require_capability('moodle/course:update', $coursecontext); 00049 $PAGE->url->param('id',$id); 00050 00051 } else if ($categoryid) { // creating new course in this category 00052 $course = null; 00053 require_login(); 00054 $category = $DB->get_record('course_categories', array('id'=>$categoryid), '*', MUST_EXIST); 00055 $catcontext = get_context_instance(CONTEXT_COURSECAT, $category->id); 00056 require_capability('moodle/course:create', $catcontext); 00057 $PAGE->url->param('category',$categoryid); 00058 $PAGE->set_context($catcontext); 00059 00060 } else { 00061 require_login(); 00062 print_error('needcoursecategroyid'); 00063 } 00064 00065 // Prepare course and the editor 00066 $editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'maxbytes'=>$CFG->maxbytes, 'trusttext'=>false, 'noclean'=>true); 00067 if (!empty($course)) { 00068 $allowedmods = array(); 00069 if ($am = $DB->get_records('course_allowed_modules', array('course'=>$course->id))) { 00070 foreach ($am as $m) { 00071 $allowedmods[] = $m->module; 00072 } 00073 } else { 00074 // this happens in case we edit course created before enabling module restrictions or somebody disabled everything :-( 00075 if (empty($course->restrictmodules) and !empty($CFG->defaultallowedmodules)) { 00076 $allowedmods = explode(',', $CFG->defaultallowedmodules); 00077 } 00078 } 00079 $course->allowedmods = $allowedmods; 00080 //add context for editor 00081 $editoroptions['context'] = $coursecontext; 00082 $course = file_prepare_standard_editor($course, 'summary', $editoroptions, $coursecontext, 'course', 'summary', 0); 00083 00084 } else { 00085 //editor should respect category context if course context is not set. 00086 $editoroptions['context'] = $catcontext; 00087 $course = file_prepare_standard_editor($course, 'summary', $editoroptions, null, 'course', 'summary', null); 00088 } 00089 00090 // first create the form 00091 $editform = new course_edit_form(NULL, array('course'=>$course, 'category'=>$category, 'editoroptions'=>$editoroptions, 'returnto'=>$returnto)); 00092 if ($editform->is_cancelled()) { 00093 switch ($returnto) { 00094 case 'category': 00095 $url = new moodle_url($CFG->wwwroot.'/course/category.php', array('id'=>$categoryid)); 00096 break; 00097 case 'topcat': 00098 $url = new moodle_url($CFG->wwwroot.'/course/'); 00099 break; 00100 default: 00101 if (!empty($course->id)) { 00102 $url = new moodle_url($CFG->wwwroot.'/course/view.php', array('id'=>$course->id)); 00103 } else { 00104 $url = new moodle_url($CFG->wwwroot.'/course/'); 00105 } 00106 break; 00107 } 00108 redirect($url); 00109 00110 } else if ($data = $editform->get_data()) { 00111 // process data if submitted 00112 00113 if (empty($course->id)) { 00114 // In creating the course 00115 $course = create_course($data, $editoroptions); 00116 00117 // Get the context of the newly created course 00118 $context = get_context_instance(CONTEXT_COURSE, $course->id, MUST_EXIST); 00119 00120 if (!empty($CFG->creatornewroleid) and !is_viewing($context, NULL, 'moodle/role:assign') and !is_enrolled($context, NULL, 'moodle/role:assign')) { 00121 // deal with course creators - enrol them internally with default role 00122 enrol_try_internal_enrol($course->id, $USER->id, $CFG->creatornewroleid); 00123 00124 } 00125 if (!is_enrolled($context)) { 00126 // Redirect to manual enrolment page if possible 00127 $instances = enrol_get_instances($course->id, true); 00128 foreach($instances as $instance) { 00129 if ($plugin = enrol_get_plugin($instance->enrol)) { 00130 if ($plugin->get_manual_enrol_link($instance)) { 00131 // we know that the ajax enrol UI will have an option to enrol 00132 redirect(new moodle_url('/enrol/users.php', array('id'=>$course->id))); 00133 } 00134 } 00135 } 00136 } 00137 } else { 00138 // Save any changes to the files used in the editor 00139 update_course($data, $editoroptions); 00140 } 00141 00142 switch ($returnto) { 00143 case 'category': 00144 case 'topcat': //redirecting to where the new course was created by default. 00145 $url = new moodle_url($CFG->wwwroot.'/course/category.php', array('id'=>$categoryid)); 00146 break; 00147 default: 00148 $url = new moodle_url($CFG->wwwroot.'/course/view.php', array('id'=>$course->id)); 00149 break; 00150 } 00151 redirect($url); 00152 } 00153 00154 00155 // Print the form 00156 00157 $site = get_site(); 00158 00159 $streditcoursesettings = get_string("editcoursesettings"); 00160 $straddnewcourse = get_string("addnewcourse"); 00161 $stradministration = get_string("administration"); 00162 $strcategories = get_string("categories"); 00163 00164 if (!empty($course->id)) { 00165 $PAGE->navbar->add($streditcoursesettings); 00166 $title = $streditcoursesettings; 00167 $fullname = $course->fullname; 00168 } else { 00169 $PAGE->navbar->add($stradministration, new moodle_url('/admin/index.php')); 00170 $PAGE->navbar->add($strcategories, new moodle_url('/course/index.php')); 00171 $PAGE->navbar->add($straddnewcourse); 00172 $title = "$site->shortname: $straddnewcourse"; 00173 $fullname = $site->fullname; 00174 } 00175 00176 $PAGE->set_title($title); 00177 $PAGE->set_heading($fullname); 00178 00179 echo $OUTPUT->header(); 00180 echo $OUTPUT->heading($streditcoursesettings); 00181 00182 $editform->display(); 00183 00184 echo $OUTPUT->footer(); 00185