moodle/course/edit.php

143 lines
4.9 KiB
PHP
Raw Normal View History

2004-09-12 12:21:27 +00:00
<?php // $Id$
// Edit course settings
2001-11-22 06:23:56 +00:00
require_once('../config.php');
require_once($CFG->dirroot.'/enrol/enrol.class.php');
require_once($CFG->libdir.'/blocklib.php');
require_once('lib.php');
require_once('edit_form.php');
2001-11-22 06:23:56 +00:00
$id = optional_param('id', 0, PARAM_INT); // course id
$categoryid = optional_param('category', 0, PARAM_INT); // course category - can be changed in edit form
require_login();
/// basic access control checks
if ($id) { // editing course
if (!$course = get_record('course', 'id', $id)) {
error('Course ID was incorrect');
2001-11-22 06:23:56 +00:00
}
$category = get_record('course_categories', 'id', $course->category);
require_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $course->id));
2001-11-22 06:23:56 +00:00
} else if ($categoryid) { // creating new course in this category
$course = null;
if (!$category = get_record('course_categories', 'id', $categoryid)) {
error('Category ID was incorrect');
2001-11-22 06:23:56 +00:00
}
require_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $category->id));
} else {
error('Either course id or category must be specified');
}
2006-09-18 09:37:51 +00:00
/// prepare course
if (!empty($course)) {
$allowedmods = array();
if (!empty($course)) {
if ($am = get_records('course_allowed_modules','course',$course->id)) {
foreach ($am as $m) {
$allowedmods[] = $m->module;
}
} else {
if (empty($course->restrictmodules)) {
$allowedmods = explode(',',$CFG->defaultallowedmodules);
} // it'll be greyed out but we want these by default anyway.
}
$course->allowedmods = $allowedmods;
if ($course->enrolstartdate){
$course->enrolstartdisabled = 0;
}
if ($course->enrolenddate) {
$course->enrolenddisabled = 0;
}
}
}
2001-11-22 06:23:56 +00:00
/// first create the form
$editform = new course_edit_form('edit.php', compact('course', 'category'));
// now override defaults if course already exists
if (!empty($course)) {
$editform->set_data($course);
}
* Added setAdvanced functionality see http://docs.moodle.org/en/Development:lib/formslib.php_setAdvanced * Added MoodleQuickForm method closeHeaderBefore($elementName); http://docs.moodle.org/en/Development:lib/formslib.php_Form_Definition#Use_Fieldsets_to_group_Form_Elements * Added moodleform method add_action_buttons(); see http://docs.moodle.org/en/Development:lib/formslib.php_Form_Definition#add_action_buttons.28.24cancel_.3D_true.2C_.24revert_.3D_true.2C_.24submitlabel.3Dnull.29.3B * is_cancelled method added to moodleform http://docs.moodle.org/en/Development:lib/formslib.php_Usage#Basic_Usage_in_A_Normal_Page * added hidden labels to elements within groups such as the date_selector select boxes and other elements in 'groups' * quiz/mod.html migrated to formslib * glossary/edit.html migrated to formslib * extended registerNoSubmitButton() functionality to automatically add js to onclick to bypass client side js input validation. * added no_submit_button_pressed() function that can be used in a similar way to is_cancelled() as a test in the main script to see if some button in the page has been pressed that is a submit button that is used for some dynamic functionality within the form and not to submit the data for the whole form. * added new condition for disabledIf which allows to disable another form element if no options are selected from within a select element. * added default 'action' for moodleform - strip_querystring(qualified_me()) http://docs.moodle.org/en/Development:lib/formslib.php_Usage#Basic_Usage_in_A_Normal_Page
2006-12-19 07:03:08 +00:00
if ($editform->is_cancelled()){
if (empty($course)) {
redirect($CFG->wwwroot);
} else {
redirect($CFG->wwwroot.'/course/view.php?id='.$course->id);
}
2001-11-22 06:23:56 +00:00
} else if ($data = $editform->get_data()) {
/// process data if submitted
2001-11-22 06:23:56 +00:00
//preprocess data
if ($data->enrolstartdisabled){
$data->enrolstartdate = 0;
}
if ($data->enrolenddisabled) {
$data->enrolenddate = 0;
}
$data->timemodified = time();
2001-11-22 06:23:56 +00:00
if (empty($course)) {
if (!$course = create_course($data)) {
print_error('coursenotcreated');
}
$context = get_context_instance(CONTEXT_COURSE, $course->id);
// assign default role to creator if not already having permission to manage course assignments
if (!has_capability('moodle/course:view', $context) or !has_capability('moodle/role:assign', $context)) {
role_assign($CFG->creatornewroleid, $USER->id, 0, $context->id);
}
if ($data->metacourse and has_capability('moodle/course:managemetacourse', $context)) {
// Redirect users with metacourse capability to student import
redirect($CFG->wwwroot."/course/importstudents.php?id=$course->id");
} else {
// Redirect to roles assignment
redirect($CFG->wwwroot."/$CFG->admin/roles/assign.php?contextid=$context->id");
}
2001-11-22 06:23:56 +00:00
} else {
if (!update_course($data)) {
print_error('coursenotupdated');
}
redirect($CFG->wwwroot."/course/view.php?id=$course->id");
2001-11-22 06:23:56 +00:00
}
}
/// Print the form
$site = get_site();
2002-08-04 02:10:00 +00:00
$streditcoursesettings = get_string("editcoursesettings");
$straddnewcourse = get_string("addnewcourse");
$stradministration = get_string("administration");
$strcategories = get_string("categories");
2002-07-19 12:01:35 +00:00
2003-04-17 13:20:26 +00:00
if (!empty($course)) {
print_header($streditcoursesettings, $course->fullname,
"<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a>
-> $streditcoursesettings", $editform->focus());
} else {
print_header("$site->shortname: $straddnewcourse", $site->fullname,
"<a href=\"$CFG->wwwroot/$CFG->admin/index.php\">$stradministration</a> -> ".
"<a href=\"index.php\">$strcategories</a> -> $straddnewcourse", $editform->focus());
2001-11-22 06:23:56 +00:00
}
2002-08-04 02:10:00 +00:00
print_heading($streditcoursesettings);
$editform->display();
print_footer($course);
2001-11-22 06:23:56 +00:00
?>