From: toyomoyo Date: Tue, 3 Apr 2007 09:19:09 +0000 (+0000) Subject: fix for MDL-8739, restore in course category X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=49860445a6f59e341791fd0619bdad9a7f4b3acb;p=moodle.git fix for MDL-8739, restore in course category --- diff --git a/backup/restore_check.html b/backup/restore_check.html index 359adb56c9..1bbf65afa1 100644 --- a/backup/restore_check.html +++ b/backup/restore_check.html @@ -39,6 +39,7 @@ if (empty($info) or empty($course_header)) { error( 'important information missing from SESSION' ); } + //If the restore object doesn't exist, we are going //to check every variable individually and create it @@ -152,7 +153,14 @@ if ($course_id) { $restore->course_id=$course_id; } - } + } + + // pass in the course category param + $cat_id = optional_param('restore_restorecatto', 0, PARAM_INT); + if ($cat_id) { + $restore->restore_restorecatto = $cat_id; + } + //We have the object with data, put it in the session $SESSION->restore = $restore; @@ -189,7 +197,11 @@ // 2-New course: Create the restore object and launch the execute. //If the user is a teacher and not a creator - if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) { + + + //if (!has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) { + + if (!user_can_create_courses()) { $restore->course_id = $id; if ($restore->restoreto == 0) { $restore->deleting = true; @@ -199,7 +211,8 @@ } //If the user is a creator (or admin) - if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) { + //if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) { + if (user_can_create_courses()) { //Set restore->deleting as needed if ($restore->restoreto == 0) { $restore->deleting = true; @@ -209,7 +222,9 @@ } //Now, select the course if needed - if (($restore->restoreto == 0 or $restore->restoreto == 1) and ($restore->course_id == 0) and has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) { + //if (($restore->restoreto == 0 or $restore->restoreto == 1) and ($restore->course_id == 0) and has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) { + if (($restore->restoreto == 0 or $restore->restoreto == 1) and ($restore->course_id == 0)) { + if ($courses = get_courses("all","c.fullname","c.id,c.fullname,c.shortname,c.visible")) { print_heading(get_string("choosecourse")); print_simple_box_start("center"); @@ -232,9 +247,9 @@ //Checks everything and execute restore } else if ((($restore->restoreto == 0 or $restore->restoreto == 1) and ($restore->course_id != 0)) or ($restore->restoreto == 2)) { //Final access control check - if ($restore->course_id == 0 and !has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) { + if ($restore->course_id == 0 and !user_can_create_courses()) { error("You need to be a creator or admin to restore into new course!"); - } else if ($restore->course_id != 0 and !has_capability('moodle/site:backup', get_context_instance(CONTEXT_COURSE, $restore->course_id))) { + } else if ($restore->course_id != 0 and !has_capability('moodle/site:restore', get_context_instance(CONTEXT_COURSE, $restore->course_id))) { error("You need to be an edit teacher or admin to restore into selected course!"); } $show_continue_button = true; diff --git a/backup/restore_form.html b/backup/restore_form.html index 0de63708b0..4ec0708e98 100644 --- a/backup/restore_form.html +++ b/backup/restore_form.html @@ -92,10 +92,9 @@ } if (!isset($restore_restoreto)) { - if (!has_capability('moodle/site:restore', get_context_instance(CONTEXT_SYSTEM, SITEID))) { + if (!user_can_create_courses()) { $restore_restoreto = 1; - } - if (has_capability('moodle/site:restore', get_context_instance(CONTEXT_SYSTEM, SITEID))) { + } else { $restore_restoreto = 2; } } @@ -170,19 +169,55 @@ function selectItemInCheckboxByName(formId, checkName, checked ) { echo ""; echo get_string("restoreto").":"; echo ""; + + + // permission should have been checked already - if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) { + /** + * if user has manageactivities in any course, we show + * existingcoursedeleting + * existingcourseadding + * else we show + * currentcoursedeleting + * currentcourse + * if user has course:create in any category, we show + * newcourse + */ + + $mycourses = get_capability_courses('moodle/course:restore'); + + // if the user can manage 2 or more courses, + // or if the only course the user can manage is not the current course + // we show options for existing courses + if (count($mycourses) > 1 || !in_array($id, $mycourses)) { + // if user can manage more than 1 course, or if user can restore to a single different course $restore_restoreto_options[0] = get_string("existingcoursedeleting"); $restore_restoreto_options[1] = get_string("existingcourseadding"); - $restore_restoreto_options[2] = get_string("newcourse"); - } else { // fix for MDL-7773 + // else if the user can write to current course + } else if (has_capability('moodle/course:restore', get_context_instance(CONTEXT_COURSE, $id))){ $restore_restoreto_options[0] = get_string("currentcoursedeleting"); - $restore_restoreto_options[1] = get_string("currentcourseadding"); + $restore_restoreto_options[1] = get_string("currentcourseadding"); + } + + // if user can create any course at all, give the option + if (user_can_create_courses()) { + $restore_restoreto_options[2] = get_string("newcourse"); } choose_from_menu($restore_restoreto_options, "restore_restoreto", $restore_restoreto, ""); echo ""; - if (iscreator()) { //display these fields conditionally + if (user_can_create_courses()) { //display these fields conditionally + + // find the list of cates user can edit + echo ""; + echo ""; + print_string('category'); + echo " :"; + echo ""; + choose_from_menu(get_creatable_categories(), "restore_restorecatto", $course_header->category->id, ""); + echo ""; + echo ""; + echo ""; echo ""; print_string("shortname"); diff --git a/backup/restorelib.php b/backup/restorelib.php index bf0f95aeb0..9b8229b534 100644 --- a/backup/restorelib.php +++ b/backup/restorelib.php @@ -519,17 +519,28 @@ $course_header->course_fullname = $currentfullname; $course_header->course_shortname = $currentshortname; + // first try to get it from restore + if ($restore->restore_restorecatto) { + $category = get_record('course_categories', 'id', $restore->restore_restorecatto); + } + + // else we try to get it from the xml file //Now calculate the category - $category = get_record("course_categories","id",$course_header->category->id, - "name",addslashes($course_header->category->name)); + if (!$category) { + $category = get_record("course_categories","id",$course_header->category->id, + "name",addslashes($course_header->category->name)); + } + //If no exists, try by name only if (!$category) { $category = get_record("course_categories","name",addslashes($course_header->category->name)); } + //If no exists, get category id 1 if (!$category) { $category = get_record("course_categories","id","1"); } + //If category 1 doesn'exists, lets create the course category (get it from backup file) if (!$category) { $ins_category->name = addslashes($course_header->category->name); diff --git a/lib/datalib.php b/lib/datalib.php index 23256723c2..8b659aa6e5 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -1675,5 +1675,62 @@ function xmldb_debug($message, $object) { error_log($message); } +/** + * Get the lists of courses the current user has $cap capability in + * I am not sure if this is needed, it loops through all courses so + * could cause performance problems. + * If it's not used, we can use a faster function to detect + * capability in restorelib.php + * @param string $cap + * @return array + */ +function get_capability_courses($cap) { + global $USER; + + $mycourses = array(); + if ($courses = get_records('course')) { + foreach ($courses as $course) { + if (has_capability($cap, get_context_instance(CONTEXT_COURSE, $course->id))) { + $mycourses[] = $course->id; + } + } + } + + return $mycourses; +} + +/** + * true or false function to see if user can create any courses at all + * @return bool + */ +function user_can_create_courses() { + global $USER; + // if user has course creation capability at any site or course cat, then return true; + + if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) { + return true; + } else { + return (bool) count(get_creatable_categories()); + } + +} + +/** + * get the list of categories the current user can create courses in + * @return array + */ +function get_creatable_categories() { + + $creatablecats = array(); + if ($cats = get_records('course_categories')) { + foreach ($cats as $cat) { + if (has_capability('moodle/course:create', get_context_instance(CONTEXT_COURSECAT, $cat->id))) { + $creatablecats[$cat->id] = $cat->name; + } + } + } + return $creatablecats; +} + // vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140: ?>