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   * Summary renderable.
  19   *
  20   * @package    block_lp
  21   * @copyright  2016 Frédéric Massart - FMCorz.net
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace block_lp\output;
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  use core_competency\api;
  29  use core_competency\external\competency_exporter;
  30  use core_competency\external\plan_exporter;
  31  use core_competency\external\user_competency_exporter;
  32  use core_user\external\user_summary_exporter;
  33  use core_competency\plan;
  34  use core_competency\url;
  35  use renderable;
  36  use renderer_base;
  37  use templatable;
  38  use required_capability_exception;
  39  
  40  /**
  41   * Summary renderable class.
  42   *
  43   * @package    block_lp
  44   * @copyright  2016 Frédéric Massart - FMCorz.net
  45   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  46   */
  47  class summary implements renderable, templatable {
  48  
  49      /** @var array Active plans. */
  50      protected $activeplans = array();
  51      /** @var array Competencies to review. */
  52      protected $compstoreview = array();
  53      /** @var array Plans to review. */
  54      protected $planstoreview = array();
  55      /** @var array Plans. */
  56      protected $plans = array();
  57      /** @var stdClass The user. */
  58      protected $user;
  59  
  60      /**
  61       * Constructor.
  62       * @param stdClass $user The user.
  63       */
  64      public function __construct($user = null) {
  65          global $USER;
  66          if (!$user) {
  67              $user = $USER;
  68          }
  69          $this->user = $user;
  70  
  71          // Get the plans.
  72          try {
  73              $this->plans = api::list_user_plans($this->user->id);
  74          } catch (required_capability_exception $e) {
  75              $this->plans = [];
  76          }
  77  
  78          // Get the competencies to review.
  79          $this->compstoreview = api::list_user_competencies_to_review(0, 3);
  80  
  81          // Get the plans to review.
  82          $this->planstoreview = api::list_plans_to_review(0, 3);
  83      }
  84  
  85      public function export_for_template(renderer_base $output) {
  86          $plans = array();
  87          foreach ($this->plans as $plan) {
  88              if (count($plans) >= 3) {
  89                  break;
  90              }
  91              if ($plan->get('status') == plan::STATUS_ACTIVE) {
  92                  $plans[] = $plan;
  93              }
  94          }
  95          $activeplans = array();
  96          foreach ($plans as $plan) {
  97              $planexporter = new plan_exporter($plan, array('template' => $plan->get_template()));
  98              $activeplans[] = $planexporter->export($output);
  99          }
 100  
 101          $compstoreview = array();
 102          foreach ($this->compstoreview['competencies'] as $compdata) {
 103              $ucexporter = new user_competency_exporter($compdata->usercompetency,
 104                  array('scale' => $compdata->competency->get_scale()));
 105              $compexporter = new competency_exporter($compdata->competency,
 106                  array('context' => $compdata->competency->get_context()));
 107              $userexporter = new user_summary_exporter($compdata->user);
 108              $compstoreview[] = array(
 109                  'usercompetency' => $ucexporter->export($output),
 110                  'competency' => $compexporter->export($output),
 111                  'user' => $userexporter->export($output),
 112              );
 113          }
 114  
 115          $planstoreview = array();
 116          foreach ($this->planstoreview['plans'] as $plandata) {
 117              $planexporter = new plan_exporter($plandata->plan, array('template' => $plandata->template));
 118              $userexporter = new user_summary_exporter($plandata->owner);
 119              $planstoreview[] = array(
 120                  'plan' => $planexporter->export($output),
 121                  'user' => $userexporter->export($output),
 122              );
 123          }
 124  
 125          $data = array(
 126              'hasplans' => !empty($this->plans),
 127              'hasactiveplans' => !empty($activeplans),
 128              'hasmoreplans' => count($this->plans) > count($activeplans),
 129              'activeplans' => $activeplans,
 130  
 131              'compstoreview' => $compstoreview,
 132              'hascompstoreview' => $this->compstoreview['count'] > 0,
 133              'hasmorecompstoreview' => $this->compstoreview['count'] > 3,
 134  
 135              'planstoreview' => $planstoreview,
 136              'hasplanstoreview' => $this->planstoreview['count'] > 0,
 137              'hasmoreplanstoreview' => $this->planstoreview['count'] > 3,
 138  
 139              'plansurl' => url::plans($this->user->id)->out(false),
 140              'pluginbaseurl' => (new \moodle_url('/blocks/lp'))->out(false),
 141              'userid' => $this->user->id,
 142          );
 143  
 144          return $data;
 145      }
 146  
 147      /**
 148       * Returns whether there is content in the summary.
 149       *
 150       * @return boolean
 151       */
 152      public function has_content() {
 153          return !empty($this->plans) || $this->planstoreview['count'] > 0 || $this->compstoreview['count'] > 0;
 154      }
 155  
 156  }