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   * Performance helper.
  19   *
  20   * @package    core_competency
  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 core_competency\external;
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  use core_competency\competency;
  29  use core_competency\competency_framework;
  30  
  31  /**
  32   * Performance helper class.
  33   *
  34   * This tool keeps a local cache of certain items, which means that subsequent
  35   * calls to get the resource will not query the database. You will want to use
  36   * this when many resources could be shared and need to be queried in a loop.
  37   *
  38   * Note that some of these improvements can only be achieved by knowing the
  39   * logic deeper in other modules. For instance we know that a competency's context
  40   * is the one of its framework. This tool must be kept in sync with those APIs.
  41   *
  42   * @package    core_competency
  43   * @copyright  2016 Frédéric Massart - FMCorz.net
  44   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  45   */
  46  class performance_helper {
  47  
  48      /** @var \context Cache of contexts by framework ID. */
  49      protected $frameworkscontexts = [];
  50  
  51      /** @var competency_framework Cache of frameworks by framework ID. */
  52      protected $frameworks = [];
  53  
  54      /** @var \grade_scale[] Cache of scales by scale ID. */
  55      protected $scales = [];
  56  
  57      /**
  58       * Get the context of a competency.
  59       *
  60       * @param competency $competency The competency.
  61       * @return \context
  62       */
  63      public function get_context_from_competency(competency $competency) {
  64          $frameworkid = $competency->get('competencyframeworkid');
  65          if (!isset($this->frameworkscontexts[$frameworkid])) {
  66              $framework = $this->get_framework_from_competency($competency);
  67              $this->frameworkscontexts[$frameworkid] = $framework->get_context();
  68          }
  69          return $this->frameworkscontexts[$frameworkid];
  70      }
  71  
  72      /**
  73       * Get the framework of a competency.
  74       *
  75       * @param competency $competency The competency.
  76       * @return competency_framework
  77       */
  78      public function get_framework_from_competency(competency $competency) {
  79          $frameworkid = $competency->get('competencyframeworkid');
  80          if (!isset($this->frameworks[$frameworkid])) {
  81              $this->frameworks[$frameworkid] = $competency->get_framework();
  82          }
  83          return $this->frameworks[$frameworkid];
  84      }
  85  
  86      /**
  87       * Get the scale of a competency.
  88       *
  89       * /!\ Make sure that this is always kept in sync with:
  90       *  - core_competency\competency::get_scale()
  91       *  - core_competency\competency_framework::get_scale()
  92       *
  93       * @param competency $competency The competency.
  94       * @return \grade_scale
  95       */
  96      public function get_scale_from_competency(competency $competency) {
  97          $scaleid = $competency->get('scaleid');
  98          if ($scaleid !== null && !isset($this->scales[$scaleid])) {
  99              $this->scales[$scaleid] = $competency->get_scale();
 100  
 101          } else if ($scaleid === null) {
 102              $framework = $this->get_framework_from_competency($competency);
 103              $scaleid = $framework->get('scaleid');
 104              if (!isset($this->scales[$scaleid])) {
 105                  $this->scales[$scaleid] = $framework->get_scale();
 106              }
 107          }
 108  
 109          return $this->scales[$scaleid];
 110      }
 111  
 112      /**
 113       * Ingest a framework to avoid additional fetching.
 114       *
 115       * @param competency_framework $framework The framework.
 116       * @return void
 117       */
 118      public function ingest_framework(competency_framework $framework) {
 119          $id = $framework->get('id');
 120          $this->frameworks[$id] = $framework;
 121      }
 122  
 123  }