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   * License manager helper class.
  19   *
  20   * @package    tool_licensemanager
  21   * @copyright  2019 Tom Dickman <tomdickman@catalyst-au.net>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace tool_licensemanager;
  26  
  27  use moodle_url;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  /**
  32   * License manager helper class.
  33   *
  34   * @package    tool_licensemanager
  35   * @copyright  2019 Tom Dickman <tomdickman@catalyst-au.net>
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class helper {
  39  
  40      /**
  41       * Moodle relative path to the licenses manager.
  42       */
  43      const MANAGER_PATH = '/admin/tool/licensemanager/index.php';
  44  
  45      /**
  46       * Get the URL for viewing the license manager interface.
  47       *
  48       * @return \moodle_url
  49       */
  50      public static function get_licensemanager_url() : moodle_url {
  51          global $CFG;
  52  
  53          $url = new moodle_url($CFG->wwwroot . self::MANAGER_PATH,
  54              ['sesskey' => sesskey()]);
  55  
  56          return $url;
  57      }
  58  
  59      /**
  60       * Get the URL for endpoint enabling a license.
  61       *
  62       * @param string $licenseshortname the shortname of license to enable.
  63       *
  64       * @return \moodle_url
  65       */
  66      public static function get_enable_license_url(string $licenseshortname) : moodle_url {
  67          $url = new moodle_url(self::MANAGER_PATH,
  68              ['action' => manager::ACTION_ENABLE, 'license' => $licenseshortname, 'sesskey' => sesskey()]);
  69  
  70          return $url;
  71      }
  72  
  73      /**
  74       * Get the URL for endpoint disabling a license.
  75       *
  76       * @param string $licenseshortname the shortname of license to disable.
  77       *
  78       * @return \moodle_url
  79       */
  80      public static function get_disable_license_url(string $licenseshortname) : moodle_url {
  81          $url = new moodle_url(self::MANAGER_PATH,
  82              ['action' => manager::ACTION_DISABLE, 'license' => $licenseshortname, 'sesskey' => sesskey()]);
  83  
  84          return $url;
  85      }
  86  
  87      /**
  88       * Get the URL endpoint to create a new license.
  89       *
  90       * @return \moodle_url
  91       */
  92      public static function get_create_license_url() : moodle_url {
  93          $url = new moodle_url(self::MANAGER_PATH,
  94              ['action' => manager::ACTION_CREATE, 'sesskey' => sesskey()]);
  95  
  96          return $url;
  97      }
  98  
  99      /**
 100       * Get the URL endpoint to update an existing license.
 101       *
 102       * @param string $licenseshortname the shortname of license to update.
 103       *
 104       * @return \moodle_url
 105       */
 106      public static function get_update_license_url(string $licenseshortname) : moodle_url {
 107          $url = new moodle_url(self::MANAGER_PATH,
 108              ['action' => manager::ACTION_UPDATE, 'license' => $licenseshortname, 'sesskey' => sesskey()]);
 109  
 110          return $url;
 111      }
 112  
 113      /**
 114       * Get the URL endpoint to move a license up order.
 115       *
 116       * @param string $licenseshortname the shortname of license to move up.
 117       *
 118       * @return \moodle_url
 119       */
 120      public static function get_moveup_license_url(string $licenseshortname) : moodle_url {
 121          $url = new moodle_url(self::MANAGER_PATH,
 122              ['action' => manager::ACTION_MOVE_UP, 'license' => $licenseshortname, 'sesskey' => sesskey()]);
 123  
 124          return $url;
 125      }
 126  
 127      /**
 128       * Get the URL endpoint to move a license down order.
 129       *
 130       * @param string $licenseshortname the shortname of license to move down.
 131       *
 132       * @return \moodle_url
 133       */
 134      public static function get_movedown_license_url(string $licenseshortname) : moodle_url {
 135          $url = new moodle_url(self::MANAGER_PATH,
 136              ['action' => manager::ACTION_MOVE_DOWN, 'license' => $licenseshortname, 'sesskey' => sesskey()]);
 137  
 138          return $url;
 139      }
 140  
 141      /**
 142       * Convert a license version number string to a UNIX epoch.
 143       *
 144       * @param string $version
 145       *
 146       * @return int $epoch
 147       */
 148      public static function convert_version_to_epoch(string $version) : int {
 149          $date = substr($version, 0, 8);
 150          $epoch = strtotime($date);
 151  
 152          return $epoch;
 153      }
 154  }