Moved create_course and update_course into course/lib.php and cleaned them up to be more useful in general MDL-9015
This commit is contained in:
+48
-170
@@ -31,31 +31,30 @@
|
||||
}
|
||||
|
||||
/// 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;
|
||||
$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;
|
||||
}
|
||||
} 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/// first create the form
|
||||
$editform = new course_edit_form('edit.php', compact('course', 'category'));
|
||||
@@ -70,7 +69,8 @@ if (!empty($course)) {
|
||||
redirect($CFG->wwwroot.'/course/view.php?id='.$course->id);
|
||||
}
|
||||
|
||||
} elseif ($data = $editform->get_data()) {
|
||||
} else if ($data = $editform->get_data()) {
|
||||
|
||||
/// process data if submitted
|
||||
|
||||
//preprocess data
|
||||
@@ -85,14 +85,36 @@ if (!empty($course)) {
|
||||
$data->timemodified = time();
|
||||
|
||||
if (empty($course)) {
|
||||
create_course($data);
|
||||
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");
|
||||
}
|
||||
|
||||
} else {
|
||||
update_course($data);
|
||||
if (!update_course($data)) {
|
||||
print_error('coursenotupdated');
|
||||
}
|
||||
|
||||
redirect($CFG->wwwroot."/course/view.php?id=$course->id");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///print the form
|
||||
/// Print the form
|
||||
|
||||
$site = get_site();
|
||||
|
||||
@@ -112,153 +134,9 @@ if (!empty($course)) {
|
||||
}
|
||||
|
||||
print_heading($streditcoursesettings);
|
||||
|
||||
$editform->display();
|
||||
|
||||
print_footer($course);
|
||||
|
||||
die;
|
||||
|
||||
|
||||
/// internal functions
|
||||
|
||||
function create_course($data) {
|
||||
global $CFG, $USER;
|
||||
|
||||
// preprocess allowed mods
|
||||
$allowedmods = empty($data->allowedmods) ? array() : $data->allowedmods;
|
||||
unset($data->allowedmods);
|
||||
if (!has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
|
||||
if ($CFG->restrictmodulesfor == 'all') {
|
||||
$data->restrictmodules = 1;
|
||||
} else {
|
||||
$data->restrictmodules = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$data->timecreated = time();
|
||||
|
||||
// place at beginning of category
|
||||
fix_course_sortorder();
|
||||
$data->sortorder = get_field_sql("SELECT min(sortorder)-1 FROM {$CFG->prefix}course WHERE category=$data->category");
|
||||
if (empty($data->sortorder)) {
|
||||
$data->sortorder = 100;
|
||||
}
|
||||
|
||||
if ($newcourseid = insert_record('course', $data)) { // Set up new course
|
||||
|
||||
$course = get_record('course', 'id', $newcourseid);
|
||||
|
||||
// Setup the blocks
|
||||
$page = page_create_object(PAGE_COURSE_VIEW, $course->id);
|
||||
blocks_repopulate_page($page); // Return value not checked because you can always edit later
|
||||
|
||||
if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
|
||||
update_restricted_mods($course, $allowedmods);
|
||||
}
|
||||
|
||||
$section = new object();
|
||||
$section->course = $course->id; // Create a default section.
|
||||
$section->section = 0;
|
||||
$section->id = insert_record('course_sections', $section);
|
||||
|
||||
fix_course_sortorder();
|
||||
add_to_log(SITEID, "course", "new", "view.php?id=$course->id", "$data->fullname (ID $course->id)") ;
|
||||
$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");
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
error("Serious Error! Could not create the new course!");
|
||||
}
|
||||
die;
|
||||
}
|
||||
|
||||
function update_course($data) {
|
||||
global $USER, $CFG;
|
||||
|
||||
// preprocess allowed mods
|
||||
$allowedmods = empty($data->allowedmods) ? array() : $data->allowedmods;
|
||||
unset($data->allowedmods);
|
||||
if (!has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
|
||||
unset($data->restrictmodules);
|
||||
}
|
||||
|
||||
$oldcourse = get_record('course', 'id', $data->id); // should not fail, already tested above
|
||||
if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $oldcourse->category))
|
||||
or !has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $data->category))) {
|
||||
// can not move to new category, keep the old one
|
||||
unset($data->category);
|
||||
}
|
||||
|
||||
// Update with the new data
|
||||
if (update_record('course', $data)) {
|
||||
|
||||
$course = get_record('course', 'id', $data->id);
|
||||
|
||||
add_to_log($course->id, "course", "update", "edit.php?id=$course->id", "");
|
||||
if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
|
||||
update_restricted_mods($course, $allowedmods);
|
||||
}
|
||||
fix_course_sortorder();
|
||||
|
||||
// Test for and remove blocks which aren't appropriate anymore
|
||||
$page = page_create_object(PAGE_COURSE_VIEW, $course->id);
|
||||
blocks_remove_inappropriate($page);
|
||||
|
||||
// put custom role names into db
|
||||
$context = get_context_instance(CONTEXT_COURSE, $course->id);
|
||||
|
||||
foreach ($data as $dname => $dvalue) {
|
||||
|
||||
// is this the right param?
|
||||
$dvalue = clean_param($dvalue, PARAM_NOTAGS);
|
||||
|
||||
if (!strstr($dname, 'role_')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$dt = explode('_', $dname);
|
||||
$roleid = $dt[1];
|
||||
// make up our mind whether we want to delete, update or insert
|
||||
|
||||
if (empty($dvalue)) {
|
||||
|
||||
delete_records('role_names', 'contextid', $context->id, 'roleid', $roleid);
|
||||
|
||||
} else if ($t = get_record('role_names', 'contextid', $context->id, 'roleid', $roleid)) {
|
||||
|
||||
$t->text = $dvalue;
|
||||
update_record('role_names', $t);
|
||||
|
||||
} else {
|
||||
|
||||
$t->contextid = $context->id;
|
||||
$t->roleid = $roleid;
|
||||
$t->text = $dvalue;
|
||||
insert_record('role_names', $t);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
redirect($CFG->wwwroot."/course/view.php?id=$course->id");
|
||||
|
||||
} else {
|
||||
error("Serious Error! Could not update the course record! (id = $form->id)");
|
||||
}
|
||||
die;
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
+135
@@ -2571,6 +2571,141 @@ function can_delete_course($courseid) {
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Create a course and either return a $course object or false
|
||||
*
|
||||
* @param object $data - all the data needed for an entry in the 'course' table
|
||||
*/
|
||||
function create_course($data) {
|
||||
global $CFG, $USER;
|
||||
|
||||
// preprocess allowed mods
|
||||
$allowedmods = empty($data->allowedmods) ? array() : $data->allowedmods;
|
||||
unset($data->allowedmods);
|
||||
if (!has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
|
||||
if ($CFG->restrictmodulesfor == 'all') {
|
||||
$data->restrictmodules = 1;
|
||||
} else {
|
||||
$data->restrictmodules = 0;
|
||||
}
|
||||
}
|
||||
|
||||
$data->timecreated = time();
|
||||
|
||||
// place at beginning of category
|
||||
fix_course_sortorder();
|
||||
$data->sortorder = get_field_sql("SELECT min(sortorder)-1 FROM {$CFG->prefix}course WHERE category=$data->category");
|
||||
if (empty($data->sortorder)) {
|
||||
$data->sortorder = 100;
|
||||
}
|
||||
|
||||
if ($newcourseid = insert_record('course', $data)) { // Set up new course
|
||||
|
||||
$course = get_record('course', 'id', $newcourseid);
|
||||
|
||||
// Setup the blocks
|
||||
$page = page_create_object(PAGE_COURSE_VIEW, $course->id);
|
||||
blocks_repopulate_page($page); // Return value not checked because you can always edit later
|
||||
|
||||
if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
|
||||
update_restricted_mods($course, $allowedmods);
|
||||
}
|
||||
|
||||
$section = new object();
|
||||
$section->course = $course->id; // Create a default section.
|
||||
$section->section = 0;
|
||||
$section->id = insert_record('course_sections', $section);
|
||||
|
||||
fix_course_sortorder();
|
||||
|
||||
add_to_log(SITEID, 'course', 'new', 'view.php?id='.$course->id, $data->fullname.' (ID '.$course->id.')');
|
||||
|
||||
return $course;
|
||||
}
|
||||
|
||||
return false; // error
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Update a course and return true or false
|
||||
*
|
||||
* @param object $data - all the data needed for an entry in the 'course' table
|
||||
*/
|
||||
function update_course($data) {
|
||||
global $USER, $CFG;
|
||||
|
||||
// preprocess allowed mods
|
||||
$allowedmods = empty($data->allowedmods) ? array() : $data->allowedmods;
|
||||
unset($data->allowedmods);
|
||||
if (!has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
|
||||
unset($data->restrictmodules);
|
||||
}
|
||||
|
||||
$oldcourse = get_record('course', 'id', $data->id); // should not fail, already tested above
|
||||
if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $oldcourse->category))
|
||||
or !has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $data->category))) {
|
||||
// can not move to new category, keep the old one
|
||||
unset($data->category);
|
||||
}
|
||||
|
||||
// Update with the new data
|
||||
if (update_record('course', $data)) {
|
||||
|
||||
$course = get_record('course', 'id', $data->id);
|
||||
|
||||
add_to_log($course->id, "course", "update", "edit.php?id=$course->id", "");
|
||||
|
||||
if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) {
|
||||
update_restricted_mods($course, $allowedmods);
|
||||
}
|
||||
|
||||
fix_course_sortorder();
|
||||
|
||||
// Test for and remove blocks which aren't appropriate anymore
|
||||
$page = page_create_object(PAGE_COURSE_VIEW, $course->id);
|
||||
blocks_remove_inappropriate($page);
|
||||
|
||||
// put custom role names into db
|
||||
$context = get_context_instance(CONTEXT_COURSE, $course->id);
|
||||
|
||||
foreach ($data as $dname => $dvalue) {
|
||||
|
||||
// is this the right param?
|
||||
$dvalue = clean_param($dvalue, PARAM_NOTAGS);
|
||||
|
||||
if (!strstr($dname, 'role_')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$dt = explode('_', $dname);
|
||||
$roleid = $dt[1];
|
||||
// make up our mind whether we want to delete, update or insert
|
||||
|
||||
if (empty($dvalue)) {
|
||||
|
||||
delete_records('role_names', 'contextid', $context->id, 'roleid', $roleid);
|
||||
|
||||
} else if ($t = get_record('role_names', 'contextid', $context->id, 'roleid', $roleid)) {
|
||||
|
||||
$t->text = $dvalue;
|
||||
update_record('role_names', $t);
|
||||
|
||||
} else {
|
||||
|
||||
$t->contextid = $context->id;
|
||||
$t->roleid = $roleid;
|
||||
$t->text = $dvalue;
|
||||
insert_record('role_names', $t);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user