Skip to content
This repository has been archived by the owner on Apr 8, 2022. It is now read-only.

Commit

Permalink
Merge branch 'MDL-43448' of git://github.com/timhunt/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
Damyon Wiese committed Jan 7, 2014
2 parents 21197e9 + 3d1c4e1 commit daaa692
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 8 deletions.
30 changes: 24 additions & 6 deletions backup/moodle2/backup_course_task.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,38 @@ public function build() {
/**
* Code the transformations to perform in the course in
* order to get transportable (encoded) links
* @param string $content content in which to encode links.
* @return string content with links encoded.
*/
static public function encode_content_links($content) {
global $CFG;

$base = preg_quote($CFG->wwwroot, '/');

// Link to the course main page (it also covers "&topic=xx" and "&week=xx"
// because they don't become transformed (section number) in backup/restore
$search = '/(' . $base . '\/course\/view.php\?id\=)([0-9]+)/';
$content= preg_replace($search, '$@COURSEVIEWBYID*$2@$', $content);
// because they don't become transformed (section number) in backup/restore.
$content = self::encode_links_helper($content, 'COURSEVIEWBYID', '/course/view.php?id=');

// A few other key course links.
$content = self::encode_links_helper($content, 'GRADEINDEXBYID', '/grade/index.php?id=');
$content = self::encode_links_helper($content, 'GRADEREPORTINDEXBYID', '/grade/report/index.php?id=');
$content = self::encode_links_helper($content, 'BADGESVIEWBYID', '/badges/view.php?type=2&id=');
$content = self::encode_links_helper($content, 'USERINDEXVIEWBYID', '/user/index.php?id=');

return $content;
}

/**
* Helper method, used by encode_content_links.
* @param string $content content in which to encode links.
* @param unknown_type $name the name of this type of encoded link.
* @param unknown_type $path the path that identifies this type of link, up
* to the ?paramname= bit.
* @return string content with one type of link encoded.
*/
static private function encode_links_helper($content, $name, $path) {
global $CFG;
$base = preg_quote($CFG->wwwroot . $path, '/');
return preg_replace('/(' . $base . ')([0-9]+)/', '$@' . $name . '*$2@$', $content);
}

// Protected API starts here

/**
Expand Down
11 changes: 9 additions & 2 deletions backup/moodle2/restore_course_task.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,17 @@ static public function define_decode_contents() {
static public function define_decode_rules() {
$rules = array();

$rules[] = new restore_decode_rule('COURSEVIEWBYID', '/course/view.php?id=$1', 'course');
// Link to the course main page (it also covers "&topic=xx" and "&week=xx"
// because they don't become transformed (section number) in backup/restore.
$rules[] = new restore_decode_rule('COURSEVIEWBYID', '/course/view.php?id=$1', 'course');

return $rules;
// A few other key course links.
$rules[] = new restore_decode_rule('GRADEINDEXBYID', '/grade/index.php?id=$1', 'course');
$rules[] = new restore_decode_rule('GRADEREPORTINDEXBYID', '/grade/report/index.php?id=$1', 'course');
$rules[] = new restore_decode_rule('BADGESVIEWBYID', '/badges/view.php?type=2&id=$1', 'course');
$rules[] = new restore_decode_rule('USERINDEXVIEWBYID', '/user/index.php?id=$1', 'course');

return $rules;
}

// Protected API starts here
Expand Down
56 changes: 56 additions & 0 deletions backup/util/helper/tests/backup_encode_content_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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/>.

/**
* @package core_backup
* @category phpunit
* @copyright 2013 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

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


global $CFG;
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
require_once($CFG->dirroot . '/backup/moodle2/backup_course_task.class.php');



/**
* Tests for encoding content links in backup_course_task.
*
* The code that this tests is acutally in backup/moodle2/backup_course_task.class.php,
* but there is no place for unit tests near there, and perhaps one day it will
* be refactored so it becomes more generic.
*/
class backup_course_task_testcase extends basic_testcase {

/**
* Test the encode_content_links method for course.
*/
public function test_course_encode_content_links() {
global $CFG;
$encoded = backup_course_task::encode_content_links(
$CFG->wwwroot . '/course/view.php?id=123, ' .
$CFG->wwwroot . '/grade/index.php?id=123, ' .
$CFG->wwwroot . '/grade/report/index.php?id=123, ' .
$CFG->wwwroot . '/badges/view.php?type=2&id=123 and ' .
$CFG->wwwroot . '/user/index.php?id=123.');
$this->assertEquals('$@COURSEVIEWBYID*123@$, $@GRADEINDEXBYID*123@$, ' .
'$@GRADEREPORTINDEXBYID*123@$, $@BADGESVIEWBYID*123@$ and $@USERINDEXVIEWBYID*123@$.', $encoded);
}
}

0 comments on commit daaa692

Please sign in to comment.