Skip to content
This repository has been archived by the owner on Feb 28, 2018. It is now read-only.

Restore works. #1

Merged
merged 4 commits into from
Apr 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions backup/moodle2/restore_side_bar_block_task.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Define all the backup steps that will be used by the backup_block_task
* @package block_side_bar
* @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @copyright 2016 Nelson Moller
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

/**
* Specialised restore task for the side bar block
* (using execute_after_tasks for recoding of target activity)
*
* @copyright 2003 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class restore_side_bar_block_task extends restore_block_task {

/**
* Define (add) particular settings this activity can have
*/
protected function define_my_settings() {
}

/**
* Define (add) particular steps this activity can have
*/
protected function define_my_steps() {
}

/**
* Define the associated file areas
*/
public function get_fileareas() {
return array(); // No associated fileareas.
}

/**
* Define special handling of configdata.
*/
public function get_configdata_encoded_attributes() {
return array(); // No special handling of configdata.
}

/**
* This function, executed after all the tasks in the plan
* have been executed, will perform the recode of the
* target activity for the block. This must be done here
* and not in normal execution so we are sure everything is
* at its finale place.
*/
public function after_restore() {
global $DB;

// Get the blockid.
$blockid = $this->get_blockid();

if ($configdata = $DB->get_field('block_instances', 'configdata', array('id' => $blockid))) {
$config = unserialize(base64_decode($configdata));
if (!empty($config->section_id)) {
// Get the mapping and replace it in config.
if ($mapping = restore_dbops::get_backup_ids_record($this->get_restoreid(),
'course_section', $config->section_id)) {

// Update the parent module id (the id from mdl_quiz etc...)
$config->section_id = $mapping->newitemid;

// Encode and save the config.
$configdata = base64_encode(serialize($config));
$DB->set_field('block_instances', 'configdata', $configdata, array('id' => $blockid));

// Arrange the replace link in the course_sections summary
$newsection = $DB->get_record('course_sections', array('id' => $mapping->newitemid));

// Update the Side Bar section with the required values to make it work
$reseturl = new moodle_url('/blocks/side_bar/reset.php?cid='.$newsection->course);
$newsection->name = get_string('sidebar', 'block_side_bar');
$newsection->summary = get_string('sectionsummary', 'block_side_bar', (string)html_writer::link($reseturl, $reseturl));
$newsection->summaryformat = FORMAT_HTML;
$newsection->visible = true;
$DB->update_record('course_sections', $newsection);

}
}
}
}

/**
* Define the contents in the activity that must be
* processed by the link decoder
*/
static public function define_decode_contents() {
return array();
}

/**
* Define the decoding rules for links belonging
* to the activity to be executed by the link decoder
*/
static public function define_decode_rules() {
return array();
}
}
116 changes: 116 additions & 0 deletions tests/side_bar_after_restore_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php
/**
* Created by PhpStorm.
* User: nmoller
* Date: 16-04-05
* Time: 09:50
*/

defined('MOODLE_INTERNAL') || die();

global $CFG;

require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');

require_once($CFG->dirroot.'/blocks/side_bar/locallib.php');
require_once($CFG->dirroot.'/blocks/side_bar/block_side_bar.php');

class side_bar_after_restore_testcase extends advanced_testcase {

/**
*
*
* @throws coding_exception
*/
public function test_after_restore() {
global $DB;

$this->resetAfterTest(true);
// To be able to have global $USER
$this->setAdminUser();
$dg = $this->getDataGenerator();
// Create test course data
$course = $dg->create_course(array('format' => 'topics', 'numsections' => 1));
$section = $dg->create_course_section(array('course' => $course->id, 'section' => 1));
// Setup the course section for the Side Bar block-managed activities
$sectioninfo = block_side_bar_create_section($course);

$page = $dg->create_module('page', array('course' => $course->id), array('section' => $sectioninfo->section));

// Setup the course section for the Side Bar block-managed activities
$block = new stdClass();
$ctx = context_course::instance($course->id);
$block->blockname = 'side_bar';
$block->parentcontextid = $ctx->id;
$block->patypepattern = 'course-view-*';
$block->defaultregion = 'side-post';
$block->defaultweight = 2;
$block->showinsubcontexts = 0;
$cfg = new stdClass();
$cfg->title = "Test";
$cfg->section_id = $sectioninfo->id;

$block->configdata = base64_encode(serialize($cfg));

$block_ins = $DB->insert_record('block_instances', $block);

$new_course_id = $this->backup_and_restore($course);

$reseturl = new moodle_url('/blocks/side_bar/reset.php?cid='.$new_course_id);

$newsection = new stdClass();
$newsection->name = get_string('sidebar', 'block_side_bar');
$newsection->summary = get_string('sectionsummary', 'block_side_bar', (string)html_writer::link($reseturl, $reseturl));
$newsection->summaryformat = FORMAT_HTML;
$newsection->visible = true;

$section = $DB->get_records('course_sections', array('name' => $newsection->name));
$section = array_pop($section);
$this->assertEquals($newsection->summary, $section->summary);
$block_instance = $DB->get_records('block_instances', array('blockname'=>'side_bar'));
$block_instance =array_pop($block_instance);
$new_config = $block_instance->configdata;
$new_config = unserialize(base64_decode($new_config));
$this->assertEquals($section->id, $new_config->section_id);

}

/**
* Backs a course up and restores it.
*
* @param stdClass $course Course object to backup
* @param int $newdate If non-zero, specifies custom date for new course
* @return int ID of newly restored course
*/
protected function backup_and_restore($course, $newdate = 0) {
global $USER, $CFG;

// Turn off file logging, otherwise it can't delete the file (Windows).
$CFG->backup_file_logger_level = backup::LOG_NONE;

// Do backup with default settings. MODE_IMPORT means it will just
// create the directory and not zip it.
$bc = new backup_controller(backup::TYPE_1COURSE, $course->id,
backup::FORMAT_MOODLE, backup::INTERACTIVE_NO, backup::MODE_IMPORT,
$USER->id);
$backupid = $bc->get_backupid();
$bc->execute_plan();
$bc->destroy();

// Do restore to new course with default settings.
$newcourseid = restore_dbops::create_new_course(
$course->fullname, $course->shortname . '_2', $course->category);
$rc = new restore_controller($backupid, $newcourseid,
backup::INTERACTIVE_NO, backup::MODE_GENERAL, $USER->id,
backup::TARGET_NEW_COURSE);
if ($newdate) {
$rc->get_plan()->get_setting('course_startdate')->set_value($newdate);
}
$this->assertTrue($rc->execute_precheck());
$rc->execute_plan();
$rc->destroy();

return $newcourseid;
}
}
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2016010700;
$plugin->version = 2016010701;
$plugin->requires = 2014051200;
$plugin->release = '2.7.1';
$plugin->component = 'block_side_bar';
Expand Down