Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

backporting changes in HEAD to fix MDL-10093, administrator or course…

…-creator assigned at course cat level can not restore new courses
  • Loading branch information...
commit d799be5424a37be755e37cd6f334ce726221f5c1 1 parent f24e0da
toyomoyo authored
View
17 backup/restore_check.html
@@ -153,6 +153,13 @@
$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 +196,7 @@
// 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 (!user_can_create_courses()) {
$restore->course_id = $id;
if ($restore->restoreto == 0) {
$restore->deleting = true;
@@ -199,7 +206,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 +217,8 @@
}
//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 get_capability_courses('moodle/site:restore')) {
+ 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,7 +241,7 @@
//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))) {
error("You need to be an edit teacher or admin to restore into selected course!");
View
49 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;
}
}
@@ -171,18 +170,52 @@
echo get_string("restoreto").":</b>";
echo "</td><td colspan=\"3\">";
- if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
+ // permission should have been checked already
+
+ /**
+ * 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/site: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/site: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 "</td></tr>";
- if (iscreator()) { //display these fields conditionally
+ if (user_can_create_courses()) { //display these fields conditionally
+
+ // find the list of cates user can edit
+ echo "<tr valign=\"top\" >";
+ echo "<td align=\"right\">";
+ print_string('category');
+ echo " :</td>";
+ echo "<td>";
+ choose_from_menu(get_creatable_categories(), "restore_restorecatto", $course_header->category->id, "");
+ echo "</td>";
+ echo "</tr>";
+
echo "<tr valign=\"top\" >";
echo "<td align=\"right\">";
print_string("shortname");
View
14 backup/restorelib.php
@@ -533,8 +533,18 @@ function restore_create_new_course($restore,&$course_header) {
$course_header->course_shortname = $currentshortname;
//Now calculate the category
- $category = get_record("course_categories","id",$course_header->category->id,
- "name",addslashes($course_header->category->name));
+
+ // 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
+ 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));
View
56 lib/datalib.php
@@ -1761,5 +1761,61 @@ 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:
?>
Please sign in to comment.
Something went wrong with that request. Please try again.