MDL-57446 block_myoverview: add course summary renderable
authorSimey Lameze <simey@moodle.com>
Tue, 14 Feb 2017 05:11:16 +0000 (13:11 +0800)
committerDamyon Wiese <damyon@moodle.com>
Mon, 3 Apr 2017 03:35:38 +0000 (11:35 +0800)
Part of MDL-55611 epic.

blocks/myoverview/block_myoverview.php
blocks/myoverview/classes/output/course_summary.php [new file with mode: 0644]
blocks/myoverview/classes/output/renderer.php [moved from blocks/myoverview/renderer.php with 79% similarity]

index 7ae6662..170aae1 100644 (file)
@@ -50,8 +50,12 @@ class block_myoverview extends block_base {
             return $this->content;
         }
 
+        $renderable = new \block_myoverview\output\course_summary();
+
+        $renderer = $this->page->get_renderer('block_myoverview');
+
         $this->content = new stdClass();
-        $this->content->text = $this->page->get_renderer('block_myoverview')->get_content();
+        $this->content->text = $renderer->render($renderable);
         $this->content->footer = '';
 
         return $this->content;
diff --git a/blocks/myoverview/classes/output/course_summary.php b/blocks/myoverview/classes/output/course_summary.php
new file mode 100644 (file)
index 0000000..a28dd6b
--- /dev/null
@@ -0,0 +1,56 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * Class containing data for my overview block.
+ *
+ * @package    block_myoverview
+ * @copyright  2017 Simey Lameze <simey@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+namespace block_myoverview\output;
+defined('MOODLE_INTERNAL') || die();
+
+use core_course\external\course_summary_exporter;
+use renderable;
+use renderer_base;
+use templatable;
+/**
+ * Class containing data for my overview block.
+ *
+ * @copyright  2017 Simey Lameze <simey@moodle.com>
+ * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class course_summary implements renderable, templatable {
+
+    /**
+     * Export this data so it can be used as the context for a mustache template.
+     *
+     * @param \renderer_base $output
+     * @return stdClass
+     */
+    public function export_for_template(renderer_base $output) {
+        $courses = enrol_get_my_courses('*');
+
+        $data = [];
+        foreach ($courses as $courseid => $value) {
+            $context = \context_course::instance($courseid);
+            $exporter = new course_summary_exporter($courses[$courseid], array('context' => $context));
+            $data[] = $exporter->export($output);
+        }
+        return $data;
+    }
+}
similarity index 79%
rename from blocks/myoverview/renderer.php
rename to blocks/myoverview/classes/output/renderer.php
index 248bc54..11909e4 100644 (file)
  * @copyright  2016 Ryan Wyllie <ryan@moodle.com>
  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
+namespace block_myoverview\output;
 defined('MOODLE_INTERNAL') || die;
 
+use plugin_renderer_base;
+use renderable;
+
 /**
- * myoverview block rendrer
+ * myoverview block renderer
  *
  * @package    block_myoverview
  * @copyright  2016 Ryan Wyllie <ryan@moodle.com>
  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
-class block_myoverview_renderer extends plugin_renderer_base {
+class renderer extends plugin_renderer_base {
 
     /**
      * Return the main content for the block overview.
      *
-     * @return {string} HTML string
+     * @return string HTML string
      */
-    public function get_content() {
-        return $this->render_from_template('block_myoverview/main', []);
+    public function render_course_summary(course_summary $coursesummary) {
+        return $this->render_from_template('block_myoverview/main', ['courses' => $coursesummary->export_for_template($this)]);
     }
 }