Skip to content

Commit

Permalink
Merge branch 'MDL-40419-28-2nd' of git://github.com/FMCorz/moodle int…
Browse files Browse the repository at this point in the history
…o MOODLE_28_STABLE
  • Loading branch information
Sam Hemelryk authored and stronk7 committed Dec 4, 2014
2 parents 8f8ace5 + c83dc49 commit f401240
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 5 deletions.
51 changes: 46 additions & 5 deletions backup/moodle2/restore_stepslib.php
Expand Up @@ -3147,11 +3147,52 @@ public function process_block($data) {
}

if (!$bi->instance_allow_multiple()) {
if ($DB->record_exists_sql("SELECT bi.id
FROM {block_instances} bi
JOIN {block} b ON b.name = bi.blockname
WHERE bi.parentcontextid = ?
AND bi.blockname = ?", array($data->parentcontextid, $data->blockname))) {
// The block cannot be added twice, so we will check if the same block is already being
// displayed on the same page. For this, rather than mocking a page and using the block_manager
// we use a similar query to the one in block_manager::load_blocks(), this will give us
// a very good idea of the blocks already displayed in the context.
$params = array(
'blockname' => $data->blockname
);

// Context matching test.
$context = context::instance_by_id($data->parentcontextid);
$contextsql = 'bi.parentcontextid = :contextid';
$params['contextid'] = $context->id;

$parentcontextids = $context->get_parent_context_ids();
if ($parentcontextids) {
list($parentcontextsql, $parentcontextparams) =
$DB->get_in_or_equal($parentcontextids, SQL_PARAMS_NAMED);
$contextsql = "($contextsql OR (bi.showinsubcontexts = 1 AND bi.parentcontextid $parentcontextsql))";
$params = array_merge($params, $parentcontextparams);
}

// Page type pattern test.
$pagetypepatterns = matching_page_type_patterns_from_pattern($data->pagetypepattern);
list($pagetypepatternsql, $pagetypepatternparams) =
$DB->get_in_or_equal($pagetypepatterns, SQL_PARAMS_NAMED);
$params = array_merge($params, $pagetypepatternparams);

// Sub page pattern test.
$subpagepatternsql = 'bi.subpagepattern IS NULL';
if ($data->subpagepattern !== null) {
$subpagepatternsql = "($subpagepatternsql OR bi.subpagepattern = :subpagepattern)";
$params['subpagepattern'] = $data->subpagepattern;
}

$exists = $DB->record_exists_sql("SELECT bi.id
FROM {block_instances} bi
JOIN {block} b ON b.name = bi.blockname
WHERE bi.blockname = :blockname
AND $contextsql
AND bi.pagetypepattern $pagetypepatternsql
AND $subpagepatternsql", $params);
if ($exists) {
// There is at least one very similar block visible on the page where we
// are trying to restore the block. In these circumstances the block API
// would not allow the user to add another instance of the block, so we
// apply the same rule here.
return false;
}
}
Expand Down
24 changes: 24 additions & 0 deletions lib/blocklib.php
Expand Up @@ -1721,6 +1721,30 @@ function matching_page_type_patterns($pagetype) {
return $patterns;
}

/**
* Give an specific pattern, return all the page type patterns that would also match it.
*
* @param string $pattern the pattern, e.g. 'mod-forum-*' or 'mod-quiz-view'.
* @return array of all the page type patterns matching.
*/
function matching_page_type_patterns_from_pattern($pattern) {
$patterns = array($pattern);
if ($pattern === '*') {
return $patterns;
}

// Only keep the part before the star because we will append -* to all the bits.
$star = strpos($pattern, '-*');
if ($star !== false) {
$pattern = substr($pattern, 0, $star);
}

$patterns = array_merge($patterns, matching_page_type_patterns($pattern));
$patterns = array_unique($patterns);

return $patterns;
}

/**
* Given a specific page type, parent context and currect context, return all the page type patterns
* that might be used by this block.
Expand Down
50 changes: 50 additions & 0 deletions lib/tests/blocklib_test.php
Expand Up @@ -408,6 +408,56 @@ public function test_block_included_with_page_type_pattern() {
$blocks = $blockmanager->get_blocks_for_region($regionname);
$this->assertContainsBlocksOfType(array($blockname), $blocks);
}

public function test_matching_page_type_patterns_from_pattern() {
$pattern = '*';
$expected = array('*');
$this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));

$pattern = 'admin-*';
$expected = array('admin-*', 'admin', '*');
$this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));

$pattern = 'blog-index';
$expected = array('blog-index', 'blog-index-*', 'blog-*', '*');
$this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));

$pattern = 'course-index-*';
$expected = array('course-index-*', 'course-index', 'course-*', '*');
$this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));

$pattern = 'course-index-category';
$expected = array('course-index-category', 'course-index-category-*', 'course-index-*', 'course-*', '*');
$this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));

$pattern = 'mod-assign-view';
$expected = array('mod-assign-view', 'mod-*-view', 'mod-assign-view-*', 'mod-assign-*', 'mod-*', '*');
$this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));

$pattern = 'mod-assign-index';
$expected = array('mod-assign-index', 'mod-*-index', 'mod-assign-index-*', 'mod-assign-*', 'mod-*', '*');
$this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));

$pattern = 'mod-forum-*';
$expected = array('mod-forum-*', 'mod-forum', 'mod-*', '*');
$this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));

$pattern = 'mod-*-view';
$expected = array('mod-*-view', 'mod', 'mod-*', '*');
$this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));

$pattern = 'mod-*-index';
$expected = array('mod-*-index', 'mod', 'mod-*', '*');
$this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));

$pattern = 'my-index';
$expected = array('my-index', 'my-index-*', 'my-*', '*');
$this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));

$pattern = 'user-profile';
$expected = array('user-profile', 'user-profile-*', 'user-*', '*');
$this->assertEquals($expected, array_values(matching_page_type_patterns_from_pattern($pattern)));
}
}

/**
Expand Down

0 comments on commit f401240

Please sign in to comment.