Skip to content

Commit

Permalink
Merge pull request #18 from oat-sa/features/fragment-prefixes
Browse files Browse the repository at this point in the history
Provides a way to client code to render PHP fragment with prefixes.
  • Loading branch information
Jérôme Bogaerts committed Oct 14, 2015
2 parents 59100fc + c8374de commit 692b8e1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "qtism/qtism",
"description": "OAT QTI Software Module Library",
"description": "OAT QTI-SDK",
"type": "library",
"authors": [
{
Expand Down
36 changes: 30 additions & 6 deletions src/qtism/runtime/rendering/markup/MarkupPostRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2013-2014 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
* Copyright (c) 2013-2015 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
*
* @author Jérôme Bogaerts <[email protected]>
* @license GPLv2
*
*
*
*/

namespace qtism\runtime\rendering\markup;
Expand Down Expand Up @@ -58,6 +56,13 @@ class MarkupPostRenderer implements Renderable
* @var array
*/
private $fragments;

/**
* Template fragments path prefix.
*
* @var string
*/
private $fragmentPrefix = '';

/**
* Create a new MarkupPostRenderer object.
Expand Down Expand Up @@ -160,6 +165,24 @@ protected function setFragments(array $fragments)
{
$this->fragments = $fragments;
}

/**
* Set the prefix to be used for fragment file names.
*
* @param string $fragmentPrefix.
*/
public function setFragmentPrefix($fragmentPrefix)
{
$this->fragmentPrefix = $fragmentPrefix;
}

/**
* Get the prefix to be used for fragment file names.
*/
protected function getFragmentPrefix()
{
return $this->fragmentPrefix;
}

public function render($document)
{
Expand Down Expand Up @@ -187,7 +210,7 @@ public function render($document)
}

/*
* 2. Transform qtism-if, qtism-printVariable statements into PHP statements.
* 2. Transform qtism-if, qtism-printVariable, qtism-include statements into PHP statements.
*/
if ($this->isTemplateOriented() === true) {
$output = preg_replace('/<!--\s+qtism-if\s*\((.+?)\)\s*:\s+-->/iu', '<?php if (\1): ?>', $output);
Expand All @@ -198,12 +221,13 @@ public function render($document)
$output = preg_replace('/<!--\s+qtism-printVariable\((.+?)\)\s+-->/iu', $call, $output);

$matches = array();
$fragmentPrefix = $this->getFragmentPrefix();
if (($c = preg_match_all('/<!--\s+(?:qtism-include)\s*\((?:(\$.+?), ([0-9]+), "(.+?)", ([0-9]+?))\)\s*:\s+-->(.+?)<!--\s+qtism-endinclude\s+-->/ius', $output, $matches)) > 0) {
$fragments = $this->getFragments();
for ($i = 0; $i < $c; $i++) {
$output = str_replace($matches[0][$i], '<?php include(dirname(__FILE__) . "/' . $matches[2][$i] . '-" . ' . $matches[1][$i] . '->getShuffledChoiceIdentifierAt(' . $matches[2][$i] . ', ' . $matches[4][$i] . ') . ".phtml"); ?>', $output);
$output = str_replace($matches[0][$i], '<?php include(dirname(__FILE__) . "/' . $fragmentPrefix . $matches[2][$i] . '-" . ' . $matches[1][$i] . '->getShuffledChoiceIdentifierAt(' . $matches[2][$i] . ', ' . $matches[4][$i] . ') . ".phtml"); ?>', $output);
$fragments[] = array(
'path' => $matches[2][$i] . '-' . $matches[3][$i] . '.phtml',
'path' => $fragmentPrefix . $matches[2][$i] . '-' . $matches[3][$i] . '.phtml',
'content' => $matches[5][$i]
);
}
Expand Down

0 comments on commit 692b8e1

Please sign in to comment.