PageRenderTime 24ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/blocks/tag_youtube/db/upgrade.php

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