Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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.
Backported from 19_STABLE
  • Loading branch information...
commit 0004ac64a5440c5b73322c853396fc5a5ee610cb 1 parent 9cd77bd
@stronk7 stronk7 authored
Showing with 27 additions and 48 deletions.
  1. +3 −18 backup/backuplib.php
  2. +24 −30 backup/restorelib.php
View
21 backup/backuplib.php
@@ -82,8 +82,8 @@ 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.
- //WARNING: It returns only NEEDED users, not every
+ //plus users needed to backup messages
+ //WARNING: It returns only NEEDED users, not every
// every student and teacher in the course, so it
//must be merged with backup_get_enrrolled_users !!
@@ -119,21 +119,6 @@ function backup_get_needed_users ($courseid, $includemessages=false) {
}
}
- //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");
@@ -163,7 +148,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
54 backup/restorelib.php
@@ -2189,7 +2189,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
@@ -2205,16 +2205,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
@@ -2231,42 +2226,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;
- }
- $sca_db = get_record("scale","scale",$sca->scale,"courseid",$course_to_search);
- //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
+ // 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.
+ $compare_scale_clause = sql_compare_text('scale') . "=" . sql_compare_text("'" . $sca->scale . "'");
+ // 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);
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.