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  namespace core_question\local\bank;
  18  
  19  /**
  20   * Converts contextlevels to strings and back to help with reading/writing contexts to/from import/export files.
  21   *
  22   * @package   core_question
  23   * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
  24   * @author    2021 Safat Shahin <safatshahin@catalyst-au.net>
  25   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  class context_to_string_translator {
  28  
  29      /**
  30       * @var array used to translate between contextids and strings for this context.
  31       */
  32      protected $contexttostringarray = [];
  33  
  34      /**
  35       * context_to_string_translator constructor.
  36       *
  37       * @param \context[] $contexts
  38       */
  39      public function __construct($contexts) {
  40          $this->generate_context_to_string_array($contexts);
  41      }
  42  
  43      /**
  44       * Context to string.
  45       *
  46       * @param int $contextid
  47       * @return mixed
  48       */
  49      public function context_to_string($contextid) {
  50          return $this->contexttostringarray[$contextid];
  51      }
  52  
  53      /**
  54       * String to context.
  55       *
  56       * @param string $contextname
  57       * @return false|int|string
  58       */
  59      public function string_to_context($contextname) {
  60          return array_search($contextname, $this->contexttostringarray);
  61      }
  62  
  63      /**
  64       * Generate context to array.
  65       *
  66       * @param \context[] $contexts
  67       */
  68      protected function generate_context_to_string_array($contexts) {
  69          if (!$this->contexttostringarray) {
  70              $catno = 1;
  71              /** @var \context $context */
  72              foreach ($contexts as $context) {
  73                  switch ($context->contextlevel) {
  74                      case CONTEXT_MODULE :
  75                          $contextstring = 'module';
  76                          break;
  77                      case CONTEXT_COURSE :
  78                          $contextstring = 'course';
  79                          break;
  80                      case CONTEXT_COURSECAT :
  81                          $contextstring = "cat$catno";
  82                          $catno++;
  83                          break;
  84                      case CONTEXT_SYSTEM :
  85                          $contextstring = 'system';
  86                          break;
  87                      default:
  88                          throw new \coding_exception('Unexpected context level ' .
  89                                  \context_helper::get_level_name($context->contextlevel) . ' for context ' .
  90                                  $context->id . ' in generate_context_to_string_array. ' .
  91                                  'Questions can never exist in this type of context.');
  92                  }
  93                  $this->contexttostringarray[$context->id] = $contextstring;
  94              }
  95          }
  96      }
  97  
  98  }