Skip to content
Browse files

MDL-20939 backup & restore - avoid admins to be always included. Two …

…things

were causing this: wrong get_users_by_capability() call and standard scales.
Improved scales restore to observe the 'moodle/course:managescales' cap.
  • Loading branch information...
1 parent 02941fd commit dac27854a6bee4cf0ef475b04b0594cb5249fde3 @stronk7 stronk7 committed Nov 24, 2009
Showing with 23 additions and 52 deletions.
  1. +1 −17 backup/backuplib.php
  2. +22 −35 backup/restorelib.php
View
18 backup/backuplib.php
@@ -85,7 +85,6 @@ function user_check_backup($courseid,$backup_unique_code,$backup_users,$backup_m
//Returns every needed user (participant) in a course
//It uses the xxxx_get_participants() function
- //plus users needed to backup scales.
//Also it search for users having messages and
//users having blogs
//WARNING: It returns only NEEDED users, not every
@@ -124,21 +123,6 @@ function backup_get_needed_users ($courseid, $includemessages=false, $includeblo
}
}
- //Now, add scale users (from site and course scales)
- //Get users
- $scaleusers = get_records_sql("SELECT DISTINCT userid,userid
- FROM {$CFG->prefix}scale
- WHERE courseid = '0' or courseid = '$courseid'");
- //Add scale users to results
- if ($scaleusers) {
- foreach ($scaleusers as $scaleuser) {
- //If userid != 0
- if ($scaleuser->userid != 0) {
- $result[$scaleuser->userid]->id = $scaleuser->userid;
- }
- }
- }
-
//Now, add message users if necessary
if ($includemessages) {
include_once("$CFG->dirroot/message/lib.php");
@@ -184,7 +168,7 @@ function backup_get_enrolled_users ($courseid) {
// get all users with moodle/course:view capability, this will include people
// assigned at cat level, or site level
// but it should be ok if they have no direct assignment at course, mod, block level
- return get_users_by_capability(get_context_instance(CONTEXT_COURSE, $courseid), 'moodle/course:view');
+ return get_users_by_capability(get_context_instance(CONTEXT_COURSE, $courseid), 'moodle/course:view', '', '', '', '', '', '', false);
}
//Returns all users ids (every record in users table)
View
57 backup/restorelib.php
@@ -3273,7 +3273,7 @@ function restore_create_questions($restore,$xml_file) {
//This function creates all the scales
function restore_create_scales($restore,$xml_file) {
- global $CFG, $db;
+ global $CFG, $USER, $db;
$status = true;
//Check it exists
@@ -3289,16 +3289,11 @@ function restore_create_scales($restore,$xml_file) {
//Now, if we have anything in scales, we have to restore that
//scales
if ($scales) {
- //Get admin->id for later use
- $admin = get_admin();
- $adminid = $admin->id;
if ($scales !== true) {
//Iterate over each scale
foreach ($scales as $scale) {
//Get record from backup_ids
$data = backup_getid($restore->backup_unique_code,"scale",$scale->id);
- //Init variables
- $create_scale = false;
if ($data) {
//Now get completed xmlized object
@@ -3316,49 +3311,41 @@ function restore_create_scales($restore,$xml_file) {
$sca->description = backup_todb($info['SCALE']['#']['DESCRIPTION']['0']['#']);
$sca->timemodified = backup_todb($info['SCALE']['#']['TIMEMODIFIED']['0']['#']);
- //Now search if that scale exists (by scale field) in course 0 (Standar scale)
- //or in restore->course_id course (Personal scale)
- if ($sca->courseid == 0) {
- $course_to_search = 0;
- } else {
- $course_to_search = $restore->course_id;
- }
-
+ // Look for scale (by 'scale' both in standard (course=0) and current course
+ // with priority to standard scales (ORDER clause)
// scale is not course unique, use get_record_sql to suppress warning
- //Going to compare LOB columns so, use the cross-db sql_compare_text() in both sides.
+ // Going to compare LOB columns so, use the cross-db sql_compare_text() in both sides.
$compare_scale_clause = sql_compare_text('scale') . "=" . sql_compare_text("'" . $sca->scale . "'");
- $sca_db = get_record_sql("SELECT * FROM {$CFG->prefix}scale
- WHERE $compare_scale_clause
- AND courseid = $course_to_search", true);
-
- //If it doesn't exist, create
- if (!$sca_db) {
- $create_scale = true;
- }
- //If we must create the scale
- if ($create_scale) {
- //Me must recode the courseid if it's <> 0 (common scale)
- if ($sca->courseid != 0) {
- $sca->courseid = $restore->course_id;
- }
- //We must recode the userid
+ // Scale doesn't exist, create it
+ if (!$sca_db = get_record_sql("SELECT *
+ FROM {$CFG->prefix}scale
+ WHERE $compare_scale_clause
+ AND courseid IN (0, $restore->course_id)
+ ORDER BY courseid", true)) {
+
+ // Try to recode the user field, defaulting to current user if not found
$user = backup_getid($restore->backup_unique_code,"user",$sca->userid);
if ($user) {
$sca->userid = $user->new_id;
} else {
- //Assign it to admin
- $sca->userid = $adminid;
+ $sca->userid = $USER->id;
+ }
+ // If scale is standard, if user lacks perms to manage standar scales
+ // 'downgrade' them to course scales
+ if ($sca->courseid == 0 and !has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM), $sca->userid)) {
+ $sca->courseid = $restore->course_id;
}
//The structure is equal to the db, so insert the scale
$newid = insert_record ("scale",$sca);
+
+ // Scale exists, reuse it
} else {
- //get current scale id
$newid = $sca_db->id;
}
+
if ($newid) {
//We have the newid, update backup_ids
- backup_putid($restore->backup_unique_code,"scale",
- $scale->id, $newid);
+ backup_putid($restore->backup_unique_code,"scale", $scale->id, $newid);
}
}
}

0 comments on commit dac2785

Please sign in to comment.
Something went wrong with that request. Please try again.