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
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;
// 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;
}
//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;
}
//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");
//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;
}
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;
}
}
echo "<td align=\"right\"><b>";
echo get_string("restoreto").":</b>";
echo "</td><td colspan=\"3\">";
+
+
+ // 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 "</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");
$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);
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:
?>