From 83da8221f73fbbc2b5cf5ecc5ee2eefd9d46d5c3 Mon Sep 17 00:00:00 2001
From: David Mudrak <david.mudrak@gmail.com>
Date: Tue, 7 Jul 2009 13:40:33 +0200
Subject: [PATCH] MDL-19731 get_renderer() accepts optional subtype

---
 lib/deprecatedlib.php            |    4 +-
 lib/outputlib.php                |   41 +++++++++++++-------
 lib/simpletest/testoutputlib.php |   76 +++++++++++++++++++++++++++++++++-----
 3 files changed, 94 insertions(+), 27 deletions(-)

diff --git a/lib/deprecatedlib.php b/lib/deprecatedlib.php
index 833ca42..77e7c2a 100644
--- a/lib/deprecatedlib.php
+++ b/lib/deprecatedlib.php
@@ -1757,11 +1757,11 @@ class custom_corners_renderer_factory extends standard_renderer_factory {
         require_once($CFG->themedir . '/custom_corners/renderers.php');
     }
     /* Implement the subclass method. */
-    public function get_renderer($module, $page) {
+    public function get_renderer($module, $page, $subtype=null) {
         if ($module == 'core') {
             return new custom_corners_core_renderer($page);
         }
-        return parent::get_renderer($module, $page);
+        return parent::get_renderer($module, $page, $subtype);
     }
 }
 
diff --git a/lib/outputlib.php b/lib/outputlib.php
index 1a05003..5a2782a 100644
--- a/lib/outputlib.php
+++ b/lib/outputlib.php
@@ -59,9 +59,10 @@ interface renderer_factory {
      *
      * @param string $component name such as 'core', 'mod_forum' or 'qtype_multichoice'.
      * @param moodle_page $page the page the renderer is outputting content for.
+     * @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news'
      * @return object an object implementing the requested renderer interface.
      */
-    public function get_renderer($component, $page);
+    public function get_renderer($component, $page, $subtype=null);
 }
 
 
@@ -431,8 +432,9 @@ class theme_config {
      * @param string $module the name of part of moodle. E.g. 'core', 'quiz', 'qtype_multichoice'.
      * @param moodle_page $page the page we are rendering
      * @return moodle_renderer_base the requested renderer.
+     * @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news'
      */
-    public function get_renderer($module, $page) {
+    public function get_renderer($module, $page, $subtype=null) {
         if (is_null($this->rf)) {
             if (CLI_SCRIPT) {
                 $classname = 'cli_renderer_factory';
@@ -442,7 +444,7 @@ class theme_config {
             $this->rf = new $classname($this);
         }
 
-        return $this->rf->get_renderer($module, $page);
+        return $this->rf->get_renderer($module, $page, $subtype);
     }
 
     /**
@@ -882,16 +884,21 @@ abstract class renderer_factory_base implements renderer_factory {
      * the class definition of the default renderer has been loaded.
      *
      * @param string $component name such as 'core', 'mod_forum' or 'qtype_multichoice'.
+     * @param string $subtype optional subtype such as 'news' resulting to 'mod_forum_news'
      * @return string the name of the standard renderer class for that module.
      */
-    protected function standard_renderer_class_for_module($component) {
+    protected function standard_renderer_class_for_module($component, $subtype=null) {
         if ($component != 'core') {
             $pluginrenderer = get_component_directory($component) . '/renderer.php';
             if (file_exists($pluginrenderer)) {
                 include_once($pluginrenderer);
             }
         }
-        $class = 'moodle_' . $component . '_renderer';
+        if (is_null($subtype)) {
+            $class = 'moodle_' . $component . '_renderer';
+        } else {
+            $class = 'moodle_' . $component . '_' . $subtype . '_renderer';
+        }
         if (!class_exists($class)) {
             throw new coding_exception('Request for an unknown renderer class ' . $class);
         }
@@ -910,11 +917,11 @@ abstract class renderer_factory_base implements renderer_factory {
  */
 class standard_renderer_factory extends renderer_factory_base {
     /* Implement the subclass method. */
-    public function get_renderer($module, $page) {
+    public function get_renderer($module, $page, $subtype=null) {
         if ($module == 'core') {
             return new moodle_core_renderer($page);
         } else {
-            $class = $this->standard_renderer_class_for_module($module);
+            $class = $this->standard_renderer_class_for_module($module, $subtype);
             return new $class($page, $this->get_renderer('core', $page));
         }
     }
@@ -930,11 +937,11 @@ class standard_renderer_factory extends renderer_factory_base {
  */
 class cli_renderer_factory extends standard_renderer_factory {
     /* Implement the subclass method. */
-    public function get_renderer($module, $page) {
+    public function get_renderer($module, $page, $subtype=null) {
         if ($module == 'core') {
             return new cli_core_renderer($page);
         } else {
-            parent::get_renderer($module, $page);
+            parent::get_renderer($module, $page, $subtype);
         }
     }
 }
@@ -984,9 +991,13 @@ class theme_overridden_renderer_factory extends standard_renderer_factory {
     }
 
     /* Implement the subclass method. */
-    public function get_renderer($module, $page) {
+    public function get_renderer($module, $page, $subtype=null) {
         foreach ($this->prefixes as $prefix) {
-            $classname = $prefix . $module . '_renderer';
+            if (is_null($subtype)) {
+                $classname = $prefix . $module . '_renderer';
+            } else {
+                $classname = $prefix . $module . '_' . $subtype . '_renderer';
+            }
             if (class_exists($classname)) {
                 if ($module == 'core') {
                     return new $classname($page);
@@ -995,7 +1006,7 @@ class theme_overridden_renderer_factory extends standard_renderer_factory {
                 }
             }
         }
-        return parent::get_renderer($module, $page);
+        return parent::get_renderer($module, $page, $subtype);
     }
 }
 
@@ -1057,7 +1068,7 @@ class template_renderer_factory extends renderer_factory_base {
     }
 
     /* Implement the subclass method. */
-    public function get_renderer($module, $page) {
+    public function get_renderer($module, $page, $subtype=null) {
         // Refine the list of search paths for this module.
         $searchpaths = array();
         foreach ($this->searchpaths as $rootpath) {
@@ -1068,7 +1079,7 @@ class template_renderer_factory extends renderer_factory_base {
         }
 
         // Create a template_renderer that copies the API of the standard renderer.
-        $copiedclass = $this->standard_renderer_class_for_module($module);
+        $copiedclass = $this->standard_renderer_class_for_module($module, $subtype);
         return new template_renderer($copiedclass, $searchpaths, $page);
     }
 }
@@ -2590,4 +2601,4 @@ function output_css_for_css_edit($files, $toreplace) {
         }
         echo "/* @end */\n\n";
     }
-}
\ No newline at end of file
+}
diff --git a/lib/simpletest/testoutputlib.php b/lib/simpletest/testoutputlib.php
index dfa5f0e..42e9bd8 100644
--- a/lib/simpletest/testoutputlib.php
+++ b/lib/simpletest/testoutputlib.php
@@ -92,13 +92,15 @@ class testable_renderer_factory extends renderer_factory_base {
         parent::__construct(null);
     }
 
-    public function get_renderer($module, $page) {
-        $this->createcalls[] = $module;
+    public function get_renderer($module, $page, $subtype=null) {
+        if (!in_array(array($module, $subtype), $this->createcalls)) {
+            $this->createcalls[] = array($module, $subtype);
+        }
         return new moodle_core_renderer($page);
     }
 
-    public function standard_renderer_class_for_module($module) {
-        return parent::standard_renderer_class_for_module($module);
+    public function standard_renderer_class_for_module($module, $subtype=null) {
+        return parent::standard_renderer_class_for_module($module, $subtype);
     }
 }
 
@@ -137,6 +139,23 @@ class moodle_mod_test_renderer extends moodle_core_renderer {
 
 
 /**
+ * Renderer class for testing subrendering feature
+ *
+ * @copyright 2009 David Mudrak
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class moodle_mod_test_subtype_renderer extends moodle_core_renderer {
+    public function __construct($containerstack, $page) {
+        parent::__construct($containerstack, $page, null);
+    }
+
+    public function signature($user = 'Administrator') {
+        return '<div class="signature">Best regards, ' . $user . '</div>';
+    }
+}
+
+
+/**
  * Unit tests for the requriement_base base class.
  *
  * @copyright 2009 Tim Hunt
@@ -150,9 +169,12 @@ class renderer_factory_base_test extends UnitTestCase {
         // Set up.
         $factory = new testable_renderer_factory();
         // Exercise SUT.
-        $renderer = $factory->get_renderer('modulename', new moodle_page);
+        $renderer    = $factory->get_renderer('modulename', new moodle_page);
+        $subrenderer = $factory->get_renderer('modulename', new moodle_page, 'subtype');
+        $cached      = $factory->get_renderer('modulename', new moodle_page);
         // Verify outcome
-        $this->assertEqual(array('modulename'), $factory->createcalls);
+        $this->assertEqual(array(array('modulename', null), array('modulename', 'subtype')), $factory->createcalls);
+
     }
 
     public function test_standard_renderer_class_for_module_core() {
@@ -173,6 +195,15 @@ class renderer_factory_base_test extends UnitTestCase {
         $this->assertEqual('moodle_mod_test_renderer', $classname);
     }
 
+    public function test_standard_renderer_class_for_module_test_with_subtype() {
+        // Set up.
+        $factory = new testable_renderer_factory();
+        // Exercise SUT.
+        $classname = $factory->standard_renderer_class_for_module('mod_test', 'subtype');
+        // Verify outcome
+        $this->assertEqual('moodle_mod_test_subtype_renderer', $classname);
+    }
+
     public function test_standard_renderer_class_for_module_unknown() {
         // Set up.
         $factory = new testable_renderer_factory();
@@ -211,6 +242,11 @@ class standard_renderer_factory_test extends UnitTestCase {
         $renderer = $this->factory->get_renderer('mod_test', new moodle_page);
         $this->assertIsA($renderer, 'moodle_mod_test_renderer');
     }
+
+    public function test_get_test_subtype_renderer() {
+        $renderer = $this->factory->get_renderer('mod_test', new moodle_page, 'subtype');
+        $this->assertIsA($renderer, 'moodle_mod_test_subtype_renderer');
+    }
 }
 
 
@@ -242,6 +278,11 @@ class custom_corners_renderer_factory_test extends UnitTestCase {
         $renderer = $this->factory->get_renderer('mod_test', new moodle_page);
         $this->assertIsA($renderer, 'moodle_mod_test_renderer');
     }
+
+    public function test_get_test_subtype_renderer() {
+        $renderer = $this->factory->get_renderer('mod_test', new moodle_page, 'subtype');
+        $this->assertIsA($renderer, 'moodle_mod_test_subtype_renderer');
+    }
 }
 
 
@@ -351,10 +392,12 @@ class theme_overridden_renderer_factory_test extends UnitTestCase {
         $factory = new testable_theme_overridden_renderer_factory($theme, $this->page);
 
         // Exercise SUT.
-        $renderer = $factory->get_renderer('mod_test', new moodle_page);
+        $renderer    = $factory->get_renderer('mod_test', new moodle_page);
+        $subrenderer = $factory->get_renderer('mod_test', new moodle_page, 'subtype');
 
         // Verify outcome
         $this->assertIsA($renderer, 'moodle_mod_test_renderer');
+        $this->assertIsA($subrenderer, 'moodle_mod_test_subtype_renderer');
     }
 
     public function test_get_renderer_overridden() {
@@ -366,10 +409,12 @@ class theme_overridden_renderer_factory_test extends UnitTestCase {
         $factory = new testable_theme_overridden_renderer_factory($theme, $this->page);
 
         // Exercise SUT.
-        $renderer = $factory->get_renderer('mod_test', new moodle_page);
+        $renderer    = $factory->get_renderer('mod_test', new moodle_page);
+        $subrenderer = $factory->get_renderer('mod_test', new moodle_page, 'subtype');
 
         // Verify outcome
         $this->assertIsA($renderer, 'testrenderertheme_mod_test_renderer');
+        $this->assertIsA($subrenderer, 'moodle_mod_test_subtype_renderer');
     }
 
     public function test_get_renderer_overridden_in_parent() {
@@ -551,13 +596,18 @@ class template_renderer_factory_test extends UnitTestCase {
         $this->make_theme_template_dir('standardtemplate');
                 
         // Exercise SUT.
-        $factory = new testable_template_renderer_factory($theme, $this->page);
+        $factory    = new testable_template_renderer_factory($theme, $this->page);
+        $subfactory = new testable_template_renderer_factory($theme, $this->page, 'subtype');
 
         // Verify outcome
         $this->assertEqual(array(
                 $CFG->themedir . '/mytheme/templates',
                 $CFG->themedir . '/standardtemplate/templates'),
                 $factory->get_search_paths());
+        $this->assertEqual(array(
+                $CFG->themedir . '/mytheme/templates',
+                $CFG->themedir . '/standardtemplate/templates'),
+                $subfactory->get_search_paths());
     }
 
     public function test_get_renderer() {
@@ -571,7 +621,8 @@ class template_renderer_factory_test extends UnitTestCase {
         $factory = new testable_template_renderer_factory($theme);
 
         // Exercise SUT.
-        $renderer = $factory->get_renderer('mod_test', $this->page);
+        $renderer    = $factory->get_renderer('mod_test', $this->page);
+        $subrenderer = $factory->get_renderer('mod_test', $this->page, 'subtype');
 
         // Verify outcome
         $this->assertEqual('moodle_mod_test_renderer', $renderer->get_copied_class());
@@ -579,6 +630,11 @@ class template_renderer_factory_test extends UnitTestCase {
                 $CFG->themedir . '/parenttheme/templates/mod_test',
                 $CFG->themedir . '/standardtemplate/templates/mod_test'),
                 $renderer->get_search_paths());
+        $this->assertEqual('moodle_mod_test_subtype_renderer', $subrenderer->get_copied_class());
+        $this->assertEqual(array(
+                $CFG->themedir . '/parenttheme/templates/mod_test',
+                $CFG->themedir . '/standardtemplate/templates/mod_test'),
+                $subrenderer->get_search_paths());
     }
 }
 
-- 
1.6.0.6

