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.

Differences Between: [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   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   * Typical crappy helper class with tiny functions.
  19   *
  20   * @package   tool_analytics
  21   * @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace tool_analytics\output;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Helper class with general purpose tiny functions.
  31   *
  32   * @package   tool_analytics
  33   * @copyright 2017 David Monllao {@link http://www.davidmonllao.com}
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class helper {
  37  
  38      /**
  39       * Converts a class full name to a select option key
  40       *
  41       * @param string $class
  42       * @return string
  43       */
  44      public static function class_to_option($class) {
  45          // Form field is PARAM_ALPHANUMEXT and we are sending fully qualified class names
  46          // as option names, but replacing the backslash for a string that is really unlikely
  47          // to ever be part of a class name.
  48          return str_replace('\\', '__', $class);
  49      }
  50  
  51      /**
  52       * option_to_class
  53       *
  54       * @param string $option
  55       * @return string
  56       */
  57      public static function option_to_class($option) {
  58          // Really unlikely but yeah, I'm a bad booyyy.
  59          return str_replace('__', '\\', $option);
  60      }
  61  
  62      /**
  63       * Sets an analytics > analytics models > $title breadcrumb.
  64       *
  65       * @param string $title
  66       * @param \moodle_url $url
  67       * @param \context|null $context Defaults to context_system
  68       * @return null
  69       */
  70      public static function set_navbar(string $title, \moodle_url $url, ?\context $context = null) {
  71          global $PAGE;
  72  
  73          if (!$context) {
  74              $context = \context_system::instance();
  75          }
  76  
  77          $PAGE->set_context($context);
  78          $PAGE->set_url($url);
  79  
  80          if ($siteadmin = $PAGE->settingsnav->find('root', \navigation_node::TYPE_SITE_ADMIN)) {
  81              $PAGE->navbar->add($siteadmin->get_content(), $siteadmin->action());
  82          }
  83          if ($analytics = $PAGE->settingsnav->find('analytics', \navigation_node::TYPE_SETTING)) {
  84              $PAGE->navbar->add($analytics->get_content(), $analytics->action());
  85          }
  86          if ($analyticmodels = $PAGE->settingsnav->find('analyticmodels', \navigation_node::TYPE_SETTING)) {
  87              $PAGE->navbar->add($analyticmodels->get_content(), $analyticmodels->action());
  88          }
  89          $PAGE->navbar->add($title);
  90  
  91          $PAGE->set_pagelayout('report');
  92          $PAGE->set_title($title);
  93          $PAGE->set_heading($title);
  94      }
  95  
  96      /**
  97       * Resets the current page.
  98       *
  99       * Note that this function can only be used by analytics pages that work at the system context.
 100       *
 101       * @return null
 102       */
 103      public static function reset_page() {
 104          global $PAGE;
 105          $PAGE->reset_theme_and_output();
 106          $PAGE->set_context(\context_system::instance());
 107      }
 108      /**
 109       * Convert a list of contexts to an associative array where the value is the context name.
 110       *
 111       * @param  array            $contexts
 112       * @param  \moodle_url      $url
 113       * @param  \renderer_base   $output
 114       * @param  int|null         $selected
 115       * @param  bool             $includeall
 116       * @param  bool             $shortentext
 117       * @return \stdClass
 118       */
 119      public static function prediction_context_selector(array $contexts, \moodle_url $url, \renderer_base $output,
 120              ?int $selected = null, ?bool $includeall = false, ?bool $shortentext = true): \stdClass {
 121  
 122          foreach ($contexts as $contextid => $unused) {
 123              // We prepare this to be used as single_select template options.
 124              $context = \context::instance_by_id($contextid);
 125  
 126              // Special name for system level predictions as showing "System is not visually nice".
 127              if ($contextid == SYSCONTEXTID) {
 128                  $contextname = get_string('allpredictions', 'tool_analytics');
 129              } else {
 130                  if ($shortentext) {
 131                      $contextname = shorten_text($context->get_context_name(false, true), 40);
 132                  } else {
 133                      $contextname = $context->get_context_name(false, true);
 134                  }
 135              }
 136              $contexts[$contextid] = $contextname;
 137          }
 138  
 139          if ($includeall) {
 140              $contexts[0] = get_string('all');
 141              $nothing = '';
 142          } else {
 143              $nothing = array('' => 'choosedots');
 144          }
 145  
 146          \core_collator::asort($contexts);
 147  
 148          if (!$selected) {
 149              $selected = '';
 150          }
 151          $singleselect = new \single_select($url, 'contextid', $contexts, $selected, $nothing);
 152          return $singleselect->export_for_template($output);
 153      }
 154  }