Commit 54d47060 authored by Aaron Wells's avatar Aaron Wells
Browse files

Load block contents by AJAX

Bug 1419399

Change-Id: Ia22577980bb67862f540a2ffc76dba54fc824aa5
parent 05efeba4
......@@ -12,6 +12,11 @@
defined ('INTERNAL') || die();
class PluginBlocktypeComment extends SystemBlocktype {
public static function should_ajaxify() {
// TinyMCE doesn't play well with loading by ajax
return false;
}
public static function single_only() {
return true;
}
......
......@@ -13,6 +13,13 @@ defined('INTERNAL') || die();
class PluginBlocktypeImage extends PluginBlocktype {
public static function should_ajaxify() {
// Most of the load time for an image block is waiting for
// the image file to get served up, which is already
// a separate client HTTP request. So no need to ajaxify.
return false;
}
public static function get_title() {
return get_string('title', 'blocktype.file/image');
}
......
<?php
/**
*
* @package mahara
* @subpackage blocktype
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
* @copyright For copyright information on Mahara, please see the README file distributed with this software.
*
*/
define('INTERNAL', 1);
require(dirname(dirname(__FILE__)) . '/init.php');
require($CFG->docroot.'/blocktype/lib.php');
// Close the session to prevent session locking.
session_write_close();
$blockid = param_integer('blockid');
$block = new BlockInstance($blockid);
if (!can_view_view($block->get('view'))) {
throw new AccessDeniedException(get_string('accessdenied', 'error'));
}
safe_require_plugin('blocktype', $block->get('blocktype'));
echo call_static_method(generate_class_name('blocktype', $block->get('blocktype')), 'render_instance', $block);
......@@ -118,6 +118,22 @@ abstract class PluginBlocktype extends Plugin implements IPluginBlocktype {
return false;
}
/**
* Indicates whether this block can be loaded by Ajax after the page is done. This
* improves page-load times by allowing blocks to be rendered in parallel instead
* of in serial.
*
* You might want to disable this for:
* - Blocks with particularly finicky Javascript contents
* - Blocks that need to write to the session (the Ajax loader uses the session in read-only)
* - Blocks that won't take long to render (static content, external content)
*
* @return boolean
*/
public static function should_ajaxify() {
return true;
}
/**
* Allows block types to override the instance's title.
*
......@@ -813,20 +829,25 @@ class BlockInstance {
return;
}
$classname = generate_class_name('blocktype', $this->get('blocktype'));
try {
$content = call_static_method($classname, 'render_instance', $this);
}
catch (NotFoundException $e) {
// Whoops - where did the image go? There is possibly a bug
// somewhere else that meant that this blockinstance wasn't
// told that the image was previously deleted. But the block
// instance is not allowed to treat this as a failure
log_debug('Artefact not found when rendering a block instance. '
. 'There might be a bug with deleting artefacts of this type? '
. 'Original error follows:');
log_debug($e->getMessage());
if (call_static_method($classname, 'should_ajaxify')) {
$content = '';
}
else {
try {
$content = call_static_method($classname, 'render_instance', $this);
}
catch (NotFoundException $e) {
// Whoops - where did the image go? There is possibly a bug
// somewhere else that meant that this blockinstance wasn't
// told that the image was previously deleted. But the block
// instance is not allowed to treat this as a failure
log_debug('Artefact not found when rendering a block instance. '
. 'There might be a bug with deleting artefacts of this type? '
. 'Original error follows:');
log_debug($e->getMessage());
$content = '';
}
}
$smarty = smarty_core();
$smarty->assign('id', $this->get('id'));
......
......@@ -16,8 +16,15 @@
--></h2>
<span class="cb"></span>
</div>{/if}
<div class="blockinstance-content{if $retractable && $retractedonload} js-hidden{/if}">
{$content|safe}
<div id="blockinstance-content-{$id}" class="blockinstance-content{if $retractable && $retractedonload} js-hidden{/if}">
{if $content}
{$content|safe}
{else}
<img src="{theme_url filename="images/loading.gif"}" />
<script type="text/javascript">
jQuery("div#blockinstance-content-{$id}").load("{$WWWROOT}blocktype/blocktype.ajax.php?blockid={$id}");
</script>
{/if}
</div>
</div>
{if $retractable}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment