Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is 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   * Class containing data for a user learning plans list page.
  19   *
  20   * @package    tool_lp
  21   * @copyright  2015 David Monllao
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace tool_lp\output;
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  use renderable;
  28  use templatable;
  29  use renderer_base;
  30  use stdClass;
  31  use single_button;
  32  use moodle_url;
  33  use core_competency\api;
  34  use core_competency\external\plan_exporter;
  35  use core_competency\plan;
  36  use core_competency\user_evidence;
  37  use context_user;
  38  
  39  /**
  40   * Class containing data for a user learning plans list page.
  41   *
  42   * @copyright  2015 David Monllao
  43   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  44   */
  45  class plans_page implements renderable, templatable {
  46  
  47      /** @var array $navigation List of links to display on the page. Each link contains a url and a title. */
  48      protected $navigation = array();
  49  
  50      /** @var array|\core_competency\plan[] $plans List of plans. */
  51      protected $plans = array();
  52  
  53      /** @var context_user|null $context context.  */
  54      protected $context = null;
  55  
  56      /** @var int|null $userid Userid. */
  57      protected $userid = null;
  58  
  59      /**
  60       * Construct this renderable.
  61       *
  62       * @param int $userid
  63       */
  64      public function __construct($userid) {
  65          $this->userid = $userid;
  66          $this->plans = api::list_user_plans($userid);
  67          $this->context = context_user::instance($userid);
  68  
  69          if (plan::can_manage_user($userid) || plan::can_manage_user_draft($userid)) {
  70              $addplan = new single_button(
  71                  new moodle_url('/admin/tool/lp/editplan.php', array('userid' => $userid)),
  72                  get_string('addnewplan', 'tool_lp'), 'get'
  73              );
  74              $this->navigation[] = $addplan;
  75          }
  76      }
  77  
  78      /**
  79       * Export this data so it can be used as the context for a mustache template.
  80       *
  81       * @param renderer_base $output
  82       * @return stdClass
  83       */
  84      public function export_for_template(renderer_base $output) {
  85          $data = new stdClass();
  86          $data->userid = $this->userid;
  87          $data->pluginbaseurl = (new moodle_url('/admin/tool/lp'))->out(true);
  88          $data->canreaduserevidence = user_evidence::can_read_user($this->userid);
  89          $data->canmanageuserplans = plan::can_manage_user($this->userid);
  90  
  91          // Attach standard objects as mustache can not parse \core_competency\plan objects.
  92          $data->plans = array();
  93          if ($this->plans) {
  94              foreach ($this->plans as $plan) {
  95                  $exporter = new plan_exporter($plan, array('template' => $plan->get_template()));
  96                  $record = $exporter->export($output);
  97                  $data->plans[] = $record;
  98              }
  99          }
 100  
 101          $data->navigation = array();
 102          foreach ($this->navigation as $button) {
 103              $data->navigation[] = $output->render($button);
 104          }
 105  
 106          return $data;
 107      }
 108  }