Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * myprofile renderer.
  19   *
  20   * @package    core_user
  21   * @copyright  2015 onwards Ankit Agarwal <ankit.agrr@gmail.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_user\output\myprofile;
  26  defined('MOODLE_INTERNAL') || die;
  27  /**
  28   * Report log renderer's for printing reports.
  29   *
  30   * @since      Moodle 2.9
  31   * @package    core_user
  32   * @copyright  2015 onwards Ankit Agarwal <ankit.agrr@gmail.com>
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class renderer extends \plugin_renderer_base {
  36      /**
  37       * Render the whole tree.
  38       *
  39       * @param tree $tree
  40       *
  41       * @return string
  42       */
  43      public function render_tree(tree $tree) {
  44          $return = \html_writer::start_tag('div', array('class' => 'profile_tree'));
  45          $categories = $tree->categories;
  46          foreach ($categories as $category) {
  47              $return .= $this->render($category);
  48          }
  49          $return .= \html_writer::end_tag('div');
  50          return $return;
  51      }
  52  
  53      /**
  54       * Render a category.
  55       *
  56       * @param category $category
  57       *
  58       * @return string
  59       */
  60      public function render_category(category $category) {
  61          $classes = $category->classes;
  62          if (empty($classes)) {
  63              $return = \html_writer::start_tag('section',
  64                  array('class' => 'node_category card d-inline-block w-100 mb-3'));
  65              $return .= \html_writer::start_tag('div', array('class' => 'card-body'));
  66          } else {
  67              $return = \html_writer::start_tag('section',
  68                  array('class' => 'node_category card d-inline-block w-100 mb-3' . $classes));
  69              $return .= \html_writer::start_tag('div', array('class' => 'card-body'));
  70          }
  71          $return .= \html_writer::tag('h3', $category->title, array('class' => 'lead'));
  72          $nodes = $category->nodes;
  73          if (empty($nodes)) {
  74              // No nodes, nothing to render.
  75              return '';
  76          }
  77          $return .= \html_writer::start_tag('ul');
  78          foreach ($nodes as $node) {
  79              $return .= $this->render($node);
  80          }
  81          $return .= \html_writer::end_tag('ul');
  82          $return .= \html_writer::end_tag('div');
  83          $return .= \html_writer::end_tag('section');
  84          return $return;
  85      }
  86  
  87      /**
  88       * Render a node.
  89       *
  90       * @param node $node
  91       *
  92       * @return string
  93       */
  94      public function render_node(node $node) {
  95          $return = '';
  96          if (is_object($node->url)) {
  97              $header = \html_writer::link($node->url, $node->title);
  98          } else {
  99              $header = $node->title;
 100          }
 101          $icon = $node->icon;
 102          if (!empty($icon)) {
 103              $header .= $this->render($icon);
 104          }
 105          $content = $node->content;
 106          $classes = $node->classes;
 107          if (!empty($content)) {
 108              if ($header) {
 109                  // There is some content to display below this make this a header.
 110                  $return = \html_writer::tag('dt', $header);
 111                  $return .= \html_writer::tag('dd', $content);
 112  
 113                  $return = \html_writer::tag('dl', $return);
 114              } else {
 115                  $return = \html_writer::span($content);
 116              }
 117              if ($classes) {
 118                  $return = \html_writer::tag('li', $return, array('class' => 'contentnode ' . $classes));
 119              } else {
 120                  $return = \html_writer::tag('li', $return, array('class' => 'contentnode'));
 121              }
 122          } else {
 123              $return = \html_writer::span($header);
 124              $return = \html_writer::tag('li', $return, array('class' => $classes));
 125          }
 126  
 127          return $return;
 128      }
 129  }