Commit 97e96de0 authored by Aaron Wells's avatar Aaron Wells
Browse files

Refactor the Dwoo_Template_Mahara files

Bug 1231755

- Cleanup, documentation, and removing replicated code.

- Not checking for whether the file exists. We'll just make a list
of search paths and pass those through to Dwoo_Template_File, and
let it determine which path has the file.

- Eliminating the separate Dwoo_Template_Mahara classes for plugintypes,
and moving the custom logic for how plugintypes find their template files,
into the Plugin class itself.

Change-Id: I877a4221323333e8e8b6c6df54062a0f8bf2b817
parent 4c6b29de
......@@ -18,6 +18,10 @@ require_once(get_config('libroot') . 'group.php');
*/
abstract class PluginArtefact extends Plugin {
public static function get_plugintype_name() {
return 'artefact';
}
/**
* This function returns a list of classnames
* of artefact types this plugin provides.
......
......@@ -2403,6 +2403,10 @@ function auth_register_submit(Pieform $form, $values) {
class PluginAuth extends Plugin {
public static function get_plugintype_name() {
return 'auth';
}
public static function get_event_subscriptions() {
$subscriptions = array();
......
......@@ -18,6 +18,20 @@ defined('INTERNAL') || die();
*/
abstract class PluginBlocktype extends Plugin {
public static function get_plugintype_name() {
return 'blocktype';
}
public static function get_theme_path($pluginname) {
if (($artefactname = blocktype_artefactplugin($pluginname))) {
// Path for block plugins that sit under an artefact
return 'artefact/' . $artefactname . '/blocktype/' . $pluginname;
}
else {
return parent::get_theme_path($pluginname);
}
}
public static function extra_xmldb_substitution($xml) {
return str_replace(
'<!-- PLUGINTYPE_INSTALLED_EXTRAFIELDS -->',
......
......@@ -66,6 +66,29 @@ abstract class PluginExport extends Plugin {
*/
const MAX_FILENAME_LENGTH = 80;
public static function get_plugintype_name() {
return 'export';
}
/**
* Where the theme assets for export plugins live. Usually it's in the normal location,
* but they can also have assets that live under artefacts.
*
* @param string $pluginname
* @return string
*/
public static function get_theme_path($pluginname) {
if (strpos($pluginname, '/')) {
// Path for export plugins that sit under an artefact plugin
// i.e. "export:html/file:index.tpl"
list($exportname, $artefactname) = explode('/', $pluginname, 2);
return 'artefact/' . $artefactname . '/export/' . $exportname;
}
else {
return parent::get_theme_path($pluginname);
}
}
/**
* A human-readable title for the export
*/
......
......@@ -13,6 +13,10 @@ defined('INTERNAL') || die();
abstract class PluginGrouptype extends Plugin {
public static function get_plugintype_name() {
return 'grouptype';
}
public static function installgrouptype($type) {
$grouptype = new $type();
$grouptype->install();
......
......@@ -39,6 +39,10 @@ abstract class PluginImport extends Plugin {
/** the ImporterTransport object to use */
protected $importertransport;
public static function get_plugintype_name() {
return 'import';
}
/**
* @param int $id the queue record id
* @param stdclass $record (optional, pass this to save db queries)
......
......@@ -18,6 +18,10 @@ defined('INTERNAL') || die();
*/
abstract class PluginInteraction extends Plugin {
public static function get_plugintype_name() {
return 'interaction';
}
/**
* override this to add extra pieform elements to the edit instance form
*/
......
......@@ -11,11 +11,6 @@
// loading all dependencies
require 'Dwoo_Template_Mahara.php';
require 'Dwoo_Template_Mahara_Artefact.php';
require 'Dwoo_Template_Mahara_Blocktype.php';
require 'Dwoo_Template_Mahara_Export.php';
require 'Dwoo_Template_Mahara_Interaction.php';
require 'Dwoo_Template_Mahara_Search.php';
/**
* implements some of the Smarty interface to support old code
......@@ -64,11 +59,6 @@ class Dwoo_Mahara extends Dwoo {
// adds mahara resources and compiler factory
$this->setDefaultCompilerFactory('file', array($this, 'compilerFactory'));
$this->addResource('artefact', 'Dwoo_Template_Mahara_Artefact', array($this, 'compilerFactory'));
$this->addResource('blocktype', 'Dwoo_Template_Mahara_Blocktype', array($this, 'compilerFactory'));
$this->addResource('export', 'Dwoo_Template_Mahara_Export', array($this, 'compilerFactory'));
$this->addResource('interaction', 'Dwoo_Template_Mahara_Interaction', array($this, 'compilerFactory'));
$this->addResource('search', 'Dwoo_Template_Mahara_Search', array($this, 'compilerFactory'));
// set base data
$theme_list = array();
......@@ -121,13 +111,16 @@ class Dwoo_Mahara extends Dwoo {
* implements smarty api to render and return a template's ouptut
*/
public function fetch($file) {
$class = 'Dwoo_Template_File';
if (strpos($file, ':') !== false) {
$name = explode(':', $file, 2);
$class = 'Dwoo_Template_Mahara_'.$name[0];
$file = $name[1];
$parts = explode(':', $file, 2);
if (count($parts) == 2) {
list($type, $file) = $parts;
}
return $this->get(new $class($file, null, null, null, $this->template_dir), $this->_data);
else {
$type = 'file';
}
$template = $this->templateFactory($type, $file);
return $this->get($template, $this->_data);
}
/**
......@@ -143,6 +136,23 @@ class Dwoo_Mahara extends Dwoo {
return $compiler;
}
}
?>
/**
* [util function] fetches a template object of the given resource
*
* @param string $resourceName the resource name (i.e. file, string)
* @param string $resourceId the resource identifier (i.e. file path)
* @param int $cacheTime the cache time setting for this resource
* @param string $cacheId the unique cache identifier
* @param string $compileId the unique compiler identifier
* @return Dwoo_ITemplate
*/
public function templateFactory($resourceName, $resourceId, $cacheTime = null, $cacheId = null, $compileId = null, Dwoo_ITemplate $parentTemplate = null) {
if ($resourceName != 'file') {
return new Dwoo_Template_Mahara("{$resourceName}:{$resourceId}", $cacheTime, $cacheId, $compileId, $this->template_dir);
}
else {
return new Dwoo_Template_File($resourceId, $cacheTime, $cacheId, $compileId, $this->template_dir);
}
}
}
<?php
/**
* base resource class for all mahara types, it calls for file name resolution if required
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class Dwoo_Template_Mahara extends Dwoo_Template_File
{
public function __construct($file, $cacheTime = null, $cacheId = null, $compileId = null, $includePath = null)
{
global $THEME;
$name = explode(':', $file, 2);
// this is a mahara special resource, resolving path
if (count($name) == 2) {
list($file, $includePath) = $this->resolveFileName($name, $includePath);
}
parent::__construct($file, null, null, null, $includePath);
}
}
?>
\ No newline at end of file
<?php
/**
*
* @package mahara
* @subpackage dwoo
* @author Catalyst IT Ltd
* @author Jordi Boggiano <j.boggiano@seld.be>
* @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.
*/
/**
* This class is a Dwoo ITemplate class. It acts as a pass-through to the standard
* Dwoo_Template_File class, which reads template files. What this class does is take
* a template specifier for a Mahara plugin's template file, like
* "blocktype:creativecommons:statement.tpl", and translate that into the relative path
* to the template (statement.tpl) and say which directories to search for this relative
* path in.
*
* The actual code that translates the Dwoo identifier into a relative filesystem path, is in the
* "get_theme_path()" method in the plugin type's class. Most plugin types will not need to customize
* this and can simply inherit the implementation from Plugin. If the plugin can also have template
* files that live outside of the {$plugintype}/{$pluginname} directory, then it will need to provide
* its own implementation of get_theme_path().
*/
class Dwoo_Template_Mahara extends Dwoo_Template_File {
/**
* Convert a Mahara plugin template file path into a normal template file path with extra search paths.
*
* @param string $pluginfile The plugintype, name, and name of file, e.g. "blocktype:clippy:index.tpl"
* @param int $cacheTime Not used.
* @param int $cacheId Not used.
* @param int $compileId Not used.
* @param array $includePath The paths to look in.
* @throws MaharaException
*/
public function __construct($file, $cacheTime = null, $cacheId = null, $compileId = null, $includePath = null) {
global $THEME;
$parts = explode(':', $file, 3);
if (count($parts) !== 3) {
throw new SystemException("Invalid template path \"{$file}\"");
}
list($plugintype, $pluginname, $file) = $parts;
// Get the relative path for this particular plugin
require_once(get_config('docroot') . $plugintype . '/lib.php');
$pluginpath = call_static_method(generate_class_name($plugintype), 'get_theme_path', $pluginname);
// Because this is a plugin template file, we don't want to include any accidental matches against
// core template files with the same name.
$includePath = array();
foreach ($THEME->inheritance as $theme) {
$includePath[] = get_config('docroot') . "{$pluginpath}/theme/{$theme}";
}
// Now, we instantiate this as a standard Dwoo_Template_File class.
// We're passing in $file, which is the relative path to the file, and
// $includePath, which is an array of directories to search for $file in.
// We let Dwoo figure out which one actually has it.
parent::__construct($file, null, null, null, $includePath);
}
}
\ No newline at end of file
<?php
/**
* implements the Artefact resource type for custom plugins
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class Dwoo_Template_Mahara_Artefact extends Dwoo_Template_Mahara
{
protected function resolveFileName(array $name, array $includePath)
{
global $THEME;
$plugin_name = $name[0];
$plugin_path = $name[1];
$basedir = get_config('docroot') . 'artefact/' . $plugin_name . '/theme/';
foreach ($THEME->inheritance as $theme) {
$filename = $basedir . $theme . '/' . $plugin_path;
if (is_readable($filename)) {
array_unshift($includePath, $basedir . $theme . '/');
return array($plugin_path, $includePath);
}
}
throw new MaharaException('Artefact template could not be found : '.implode(':', $name));
}
}
?>
\ No newline at end of file
<?php
/**
* implements the Blocktype resource type for custom plugins
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class Dwoo_Template_Mahara_Blocktype extends Dwoo_Template_Mahara
{
protected static $_filePaths = array();
protected function resolveFileName(array $name, array $includePath)
{
global $THEME;
if (!isset(self::$_filePaths[$name[0]][$name[1]])) {
$artefactplugin = blocktype_artefactplugin($name[0]);
$template_path = $name[1];
$basedir = get_config('docroot');
if ($artefactplugin) {
$basedir .= 'artefact/' . $artefactplugin . '/blocktype/' . $name[0] . '/theme/';
}
else {
$basedir .= 'blocktype/' . $name[0] . '/theme/';
}
foreach ($THEME->inheritance as $theme) {
$filename = $basedir . $theme . '/' . $template_path;
if (is_readable($filename)) {
array_unshift($includePath, $basedir . $theme . '/');
return self::$_filePaths[$name[0]][$name[1]] = array($template_path, $includePath);
}
}
self::$_filePaths[$name[0]][$name[1]] = false;
}
if (!self::$_filePaths[$name[0]][$name[1]]) {
throw new MaharaException('Blocktype template could not be found : '.implode(':', $name));
}
return self::$_filePaths[$name[0]][$name[1]];
}
}
?>
\ No newline at end of file
<?php
/**
* implements the Export resource type
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class Dwoo_Template_Mahara_Export extends Dwoo_Template_Mahara
{
protected static $_filePaths = array();
protected function resolveFileName(array $name, array $includePath)
{
global $THEME;
if (!isset(self::$_filePaths[$name[0]][$name[1]])) {
$plugin = $name[0];
$template_path = $name[1];
$basedir = get_config('docroot');
$pluginbits = explode('/', $plugin);
if (count($pluginbits) == 2) {
$basedir .= 'artefact/' . $pluginbits[1] . '/export/' . $pluginbits[0] . '/theme/';
}
else {
$basedir .= 'export/' . $plugin . '/theme/';
}
foreach ($THEME->inheritance as $theme) {
$filename = $basedir . $theme . '/' . $template_path;
if (is_readable($filename)) {
array_unshift($includePath, $basedir . $theme . '/');
return self::$_filePaths[$name[0]][$name[1]] = array($template_path, $includePath);
}
}
self::$_filePaths[$name[0]][$name[1]] = false;
}
if (!self::$_filePaths[$name[0]][$name[1]]) {
throw new MaharaException('Blocktype template could not be found : '.implode(':', $name));
}
return self::$_filePaths[$name[0]][$name[1]];
}
}
?>
\ No newline at end of file
<?php
/**
* implements the Interaction resource type
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class Dwoo_Template_Mahara_Interaction extends Dwoo_Template_Mahara
{
protected function resolveFileName(array $name, array $includePath)
{
global $THEME;
$plugin_name = $name[0];
$plugin_path = $name[1];
$basedir = get_config('docroot') . 'interaction/' . $plugin_name . '/theme/';
foreach ($THEME->inheritance as $theme) {
$filename = $basedir . $theme . '/' . $plugin_path;
if (is_readable($filename)) {
array_unshift($includePath, $basedir . $theme . '/');
return array($plugin_path, $includePath);
}
}
throw new MaharaException('Artefact template could not be found : '.implode(':', $name));
}
}
?>
\ No newline at end of file
<?php
/**
*
* @package mahara
* @subpackage dwoo
* @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.
*
*/
/**
* Dwoo compiler for the Search plugin type. The only added functionality is the ability
* to search for dwoo templates under a Search plugin by prepending the path with "search:"
*/
class Dwoo_Template_Mahara_Search extends Dwoo_Template_Mahara
{
protected function resolveFileName(array $name, array $includePath) {
global $THEME;
$plugin_name = $name[0];
$plugin_path = $name[1];
$basedir = get_config('docroot') . 'search/' . $plugin_name . '/theme/';
foreach ($THEME->inheritance as $theme) {
$filename = $basedir . $theme . '/' . $plugin_path;
if (is_readable($filename)) {
array_unshift($includePath, $basedir . $theme . '/');
return array($plugin_path, $includePath);
}
}
throw new MaharaException('Search template could not be found : '.implode(':', $name));
}
}
\ No newline at end of file
......@@ -1730,7 +1730,13 @@ define('DEBUG_DEVELOPER', 'whocares');
/**
* Base class for all plugintypes.
*/
class Plugin {
abstract class Plugin {
/**
* The name of this plugintype. Used in directory names, table names, etc.
* @return string
*/
abstract public static function get_plugintype_name();
/**
* This function returns an array of crons it wants to have run
......@@ -1799,6 +1805,16 @@ class Plugin {
*/
public static function sanity_check() {
}
/**
* The relative path for this content's stuff in theme directories.
*
* @param string $pluginname The middle part in a dwoo. i.e. in "export:html/file:index.tpl", it's the "html/file".
* @return string
*/
public static function get_theme_path($pluginname) {
return static::get_plugintype_name() . '/' . $pluginname;
}
}
/**
......
......@@ -13,6 +13,10 @@ defined('INTERNAL') || die();
abstract class PluginNotification extends Plugin {
public static function get_plugintype_name() {
return 'notification';
}
/**
* Indicates if a plugin uses data from the activity table. If an activity type implements ActivityStreamable
* then users can choose any notification methods that use the activity table (as well as the other methods).
......
......@@ -17,6 +17,10 @@ defined('INTERNAL') || die();
*/
abstract class PluginSearch extends Plugin {
public static function get_plugintype_name() {
return 'search';
}
/**
* This function gets called when the sitewide search plugin is switched to
* this one. It's the chance for the plugin to do any post-configuration
......
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