function restore_create_scales($restore, $xml_file) { global $CFG, $db; $status = true; //Check it exists if (!file_exists($xml_file)) { $status = false; } //Get info from xml if ($status) { //scales will contain the old_id of every scale //in backup_ids->info will be the real info (serialized) $scales = restore_read_xml_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 $info = $data->info; //traverse_xmlize($info); //Debug //print_object ($GLOBALS['traverse_array']); //Debug //$GLOBALS['traverse_array']=""; //Debug //Now build the SCALE record structure $sca = new object(); $sca->courseid = backup_todb($info['SCALE']['#']['COURSEID']['0']['#']); $sca->userid = backup_todb($info['SCALE']['#']['USERID']['0']['#']); $sca->name = backup_todb($info['SCALE']['#']['NAME']['0']['#']); $sca->scale = backup_todb($info['SCALE']['#']['SCALETEXT']['0']['#']); $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; } // 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 . "'"); $sca_db = get_record_sql("SELECT * FROM {$CFG->prefix}scale\n WHERE {$compare_scale_clause}\n 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 $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; } //The structure is equal to the db, so insert the scale $newid = insert_record("scale", $sca); } 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); } } } } } else { $status = false; } return $status; }
function restore_create_scales($restore, $xml_file) { global $CFG, $USER, $db; $status = true; //Check it exists if (!file_exists($xml_file)) { $status = false; } //Get info from xml if ($status) { //scales will contain the old_id of every scale //in backup_ids->info will be the real info (serialized) $scales = restore_read_xml_scales($restore, $xml_file); } //Now, if we have anything in scales, we have to restore that //scales if ($scales) { 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); if ($data) { //Now get completed xmlized object $info = $data->info; //traverse_xmlize($info); //Debug //print_object ($GLOBALS['traverse_array']); //Debug //$GLOBALS['traverse_array']=""; //Debug //Now build the SCALE record structure $sca = new object(); $sca->courseid = backup_todb($info['SCALE']['#']['COURSEID']['0']['#']); $sca->userid = backup_todb($info['SCALE']['#']['USERID']['0']['#']); $sca->name = backup_todb($info['SCALE']['#']['NAME']['0']['#']); $sca->scale = backup_todb($info['SCALE']['#']['SCALETEXT']['0']['#']); $sca->description = backup_todb($info['SCALE']['#']['DESCRIPTION']['0']['#']); $sca->timemodified = backup_todb($info['SCALE']['#']['TIMEMODIFIED']['0']['#']); // 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 *\n FROM {$CFG->prefix}scale\n WHERE {$compare_scale_clause}\n AND courseid IN (0, {$restore->course_id})\n 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 { $sca->userid = $USER->id; } // If course scale, recode the course field if ($sca->courseid != 0) { $sca->courseid = $restore->course_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 { $newid = $sca_db->id; } if ($newid) { //We have the newid, update backup_ids backup_putid($restore->backup_unique_code, "scale", $scale->id, $newid); } } } } } else { $status = false; } return $status; }