Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 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   * This file keeps track of upgrades to the tag_youtube block
  19   *
  20   * @package block_tag_youtube
  21   * @copyright 2020 Mihail Geshoski <mihail@moodle.com>
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  /**
  28   * Upgrade code for the Tag Youtube block.
  29   *
  30   * @param int $oldversion
  31   */
  32  function xmldb_block_tag_youtube_upgrade($oldversion) {
  33      global $DB, $CFG, $OUTPUT;
  34  
  35      // Automatically generated Moodle v3.10.0 release upgrade line.
  36      // Put any upgrade step following this.
  37  
  38      if ($oldversion < 2021052501) {
  39          // We need to fix every tag_youtube block instance that has used a legacy category name as a category config.
  40          // The category config needs to store the category ID instead.
  41  
  42          // If tag_youtube block instances exist.
  43          if ($blockinstances = $DB->get_records('block_instances', ['blockname' => 'tag_youtube'])) {
  44              $categories = [];
  45              // The block tag youtube needs to be configured and have a valid API key in order to obtain the video
  46              // category list.
  47              if ($apikey = get_config('block_tag_youtube', 'apikey')) {
  48                  require_once($CFG->libdir . '/google/lib.php');
  49                  $client = get_google_client();
  50                  $client->setDeveloperKey($apikey);
  51                  $client->setScopes(array(Google_Service_YouTube::YOUTUBE_READONLY));
  52                  $service = new Google_Service_YouTube($client);
  53  
  54                  try {
  55                      // Get the video category list.
  56                      $response = $service->videoCategories->listVideoCategories('snippet', ['regionCode' => 'us']);
  57  
  58                      // Return an array of categories, where the key is the category name and the value is the
  59                      // category ID.
  60                      $categories = array_reduce($response['modelData']['items'], function ($categories, $category) {
  61                          $categoryid = $category['id'];
  62                          $categoryname = $category['snippet']['title'];
  63                          // If videos can be associated with this category, add it to the categories list.
  64                          if ($category['snippet']['assignable']) {
  65                              $categories[$categoryname] = $categoryid;
  66                          }
  67                          return $categories;
  68                      }, []);
  69                  } catch (Exception $e) {
  70                      $warn = "Due to the following error the youtube video categories were not obtained through the API:
  71                      '{$e->getMessage()}'. Therefore, any legacy values used as a category setting in Tag Youtube
  72                      block instances cannot be properly mapped and updated. All legacy values used as category setting
  73                      will still be updated and set by default to 'Any category'.";
  74                      echo $OUTPUT->notification($warn, 'notifyproblem');
  75                  }
  76              } else {
  77                  $warn = "The API key is missing in the Tag Youtube block configuration. Therefore, the youtube video
  78                  categories cannot be obtained and mapped with the legacy values used as category setting. All legacy
  79                  values used as category setting will still be updated and set by default to 'Any category'.";
  80                  echo $OUTPUT->notification($warn, 'notifyproblem');
  81              }
  82  
  83              // Array that maps the old category names to the current category names.
  84              $categorynamemap = [
  85                  'Film' => 'Film & Animation',
  86                  'Autos' => 'Autos & Vehicles',
  87                  'Comedy' => 'Comedy',
  88                  'Entertainment' => 'Entertainment',
  89                  'Music' => 'Music',
  90                  'News' => 'News & Politics',
  91                  'People' => 'People & Blogs',
  92                  'Animals' => 'Pets & Animals',
  93                  'Howto' => 'Howto & Style',
  94                  'Sports' => 'Sports',
  95                  'Travel' => 'Travel & Events',
  96                  'Games' => 'Gaming',
  97                  'Education' => 'Education',
  98                  'Tech' => 'Tech'
  99              ];
 100  
 101              // If the block uses a legacy category name, update it to use the current category ID instead.
 102              foreach ($blockinstances as $blockinstance) {
 103                  $blockconfig = unserialize(base64_decode($blockinstance->configdata));
 104  
 105                  // Skip if the block does not have a specific category set.
 106                  if (!isset($blockconfig->category)) {
 107                      continue;
 108                  }
 109  
 110                  $blockcategoryconfig = $blockconfig->category;
 111                  // The block is using a legacy category name as a category config.
 112                  if (array_key_exists($blockcategoryconfig, $categorynamemap)) {
 113                      if (!empty($categories)) { // The categories were successfully obtained through the API call.
 114                          // Get the current category name.
 115                          $currentcategoryname = $categorynamemap[$blockcategoryconfig];
 116                          // Add the category ID as a new category config for this block instance.
 117                          $blockconfig->category = $categories[$currentcategoryname];
 118                      } else { // The categories were not obtained through the API call.
 119                          // If the categories were not obtained through the API call, we are not able to map the
 120                          // current legacy category name with the category ID. Therefore, we should default the category
 121                          // config value to 0 ('Any category') to at least enable the block to function properly. The
 122                          // user can later manually select the desired category and re-save the config through the UI.
 123                          $blockconfig->category = 0;
 124                      }
 125  
 126                      $blockinstance->configdata = base64_encode(serialize($blockconfig));
 127                      $DB->update_record('block_instances', $blockinstance);
 128                  }
 129              }
 130          }
 131  
 132          upgrade_block_savepoint(true, 2021052501, 'tag_youtube', false);
 133      }
 134  
 135      // Automatically generated Moodle v4.0.0 release upgrade line.
 136      // Put any upgrade step following this.
 137  
 138      // Automatically generated Moodle v4.1.0 release upgrade line.
 139      // Put any upgrade step following this.
 140  
 141      // Automatically generated Moodle v4.2.0 release upgrade line.
 142      // Put any upgrade step following this.
 143  
 144      // Automatically generated Moodle v4.3.0 release upgrade line.
 145      // Put any upgrade step following this.
 146  
 147      return true;
 148  }