PageRenderTime 39ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/repository/youtube/lib.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 339 lines | 181 code | 35 blank | 123 comment | 16 complexity | 97eef65d23b5c1156f69f61d414b2f87 MD5 | raw file
  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 plugin is used to access youtube videos
  18. *
  19. * @since Moodle 2.0
  20. * @package repository_youtube
  21. * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. require_once($CFG->dirroot . '/repository/lib.php');
  25. /**
  26. * repository_youtube class
  27. *
  28. * @since Moodle 2.0
  29. * @package repository_youtube
  30. * @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
  31. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32. */
  33. class repository_youtube extends repository {
  34. /** @var int maximum number of thumbs per page */
  35. const YOUTUBE_THUMBS_PER_PAGE = 27;
  36. /**
  37. * API key for using the YouTube Data API.
  38. * @var mixed
  39. */
  40. private $apikey;
  41. /**
  42. * Google Client.
  43. * @var Google_Client
  44. */
  45. private $client = null;
  46. /**
  47. * YouTube Service.
  48. * @var Google_Service_YouTube
  49. */
  50. private $service = null;
  51. /**
  52. * Youtube plugin constructor
  53. * @param int $repositoryid
  54. * @param object $context
  55. * @param array $options
  56. */
  57. public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
  58. parent::__construct($repositoryid, $context, $options);
  59. $this->apikey = $this->get_option('apikey');
  60. // Without an API key, don't show this repo to users as its useless without it.
  61. if (empty($this->apikey)) {
  62. $this->disabled = true;
  63. }
  64. }
  65. /**
  66. * Init all the youtube client service stuff.
  67. *
  68. * Instead of instantiating the service in the constructor, we delay
  69. * it until really neeed because it's really memory hungry (2MB). That
  70. * way the editor or any other artifact requiring repository instantiation
  71. * can do it in a cheap way. Sort of lazy loading the plugin.
  72. */
  73. private function init_youtube_service() {
  74. global $CFG;
  75. if (!isset($this->service)) {
  76. require_once($CFG->libdir . '/google/lib.php');
  77. $this->client = get_google_client();
  78. $this->client->setDeveloperKey($this->apikey);
  79. $this->client->setScopes(array(Google_Service_YouTube::YOUTUBE_READONLY));
  80. $this->service = new Google_Service_YouTube($this->client);
  81. }
  82. }
  83. /**
  84. * Save apikey in config table.
  85. * @param array $options
  86. * @return boolean
  87. */
  88. public function set_option($options = array()) {
  89. if (!empty($options['apikey'])) {
  90. set_config('apikey', trim($options['apikey']), 'youtube');
  91. }
  92. unset($options['apikey']);
  93. return parent::set_option($options);
  94. }
  95. /**
  96. * Get apikey from config table.
  97. *
  98. * @param string $config
  99. * @return mixed
  100. */
  101. public function get_option($config = '') {
  102. if ($config === 'apikey') {
  103. return trim(get_config('youtube', 'apikey'));
  104. } else {
  105. $options['apikey'] = trim(get_config('youtube', 'apikey'));
  106. }
  107. return parent::get_option($config);
  108. }
  109. public function check_login() {
  110. return !empty($this->keyword);
  111. }
  112. /**
  113. * Return search results
  114. * @param string $search_text
  115. * @return array
  116. */
  117. public function search($search_text, $page = 0) {
  118. global $SESSION;
  119. $sort = optional_param('youtube_sort', '', PARAM_TEXT);
  120. $sess_keyword = 'youtube_'.$this->id.'_keyword';
  121. $sess_sort = 'youtube_'.$this->id.'_sort';
  122. // This is the request of another page for the last search, retrieve the cached keyword and sort
  123. if ($page && !$search_text && isset($SESSION->{$sess_keyword})) {
  124. $search_text = $SESSION->{$sess_keyword};
  125. }
  126. if ($page && !$sort && isset($SESSION->{$sess_sort})) {
  127. $sort = $SESSION->{$sess_sort};
  128. }
  129. if (!$sort) {
  130. $sort = 'relevance'; // default
  131. }
  132. // Save this search in session
  133. $SESSION->{$sess_keyword} = $search_text;
  134. $SESSION->{$sess_sort} = $sort;
  135. $this->keyword = $search_text;
  136. $ret = array();
  137. $ret['nologin'] = true;
  138. $ret['page'] = (int)$page;
  139. if ($ret['page'] < 1) {
  140. $ret['page'] = 1;
  141. }
  142. $start = ($ret['page'] - 1) * self::YOUTUBE_THUMBS_PER_PAGE + 1;
  143. $max = self::YOUTUBE_THUMBS_PER_PAGE;
  144. $ret['list'] = $this->_get_collection($search_text, $start, $max, $sort);
  145. $ret['norefresh'] = true;
  146. $ret['nosearch'] = true;
  147. // If the number of results is smaller than $max, it means we reached the last page.
  148. $ret['pages'] = (count($ret['list']) < $max) ? $ret['page'] : -1;
  149. return $ret;
  150. }
  151. /**
  152. * Private method to get youtube search results
  153. * @param string $keyword
  154. * @param int $start
  155. * @param int $max max results
  156. * @param string $sort
  157. * @throws moodle_exception If the google API returns an error.
  158. * @return array
  159. */
  160. private function _get_collection($keyword, $start, $max, $sort) {
  161. global $SESSION;
  162. // The new API doesn't use "page" numbers for browsing through results.
  163. // It uses a prev and next token in each set that you need to use to
  164. // request the next page of results.
  165. $sesspagetoken = 'youtube_'.$this->id.'_nextpagetoken';
  166. $pagetoken = '';
  167. if ($start > 1 && isset($SESSION->{$sesspagetoken})) {
  168. $pagetoken = $SESSION->{$sesspagetoken};
  169. }
  170. $list = array();
  171. $error = null;
  172. try {
  173. $this->init_youtube_service(); // About to use the service, ensure it's loaded.
  174. $response = $this->service->search->listSearch('id,snippet', array(
  175. 'q' => $keyword,
  176. 'maxResults' => $max,
  177. 'order' => $sort,
  178. 'pageToken' => $pagetoken,
  179. 'type' => 'video',
  180. 'videoEmbeddable' => 'true',
  181. ));
  182. // Track the next page token for the next request (when a user
  183. // scrolls down in the file picker for more videos).
  184. $SESSION->{$sesspagetoken} = $response['nextPageToken'];
  185. foreach ($response['items'] as $result) {
  186. $title = $result->snippet->title;
  187. $source = 'http://www.youtube.com/v/' . $result->id->videoId . '#' . $title;
  188. $thumb = $result->snippet->getThumbnails()->getDefault();
  189. $list[] = array(
  190. 'shorttitle' => $title,
  191. 'thumbnail_title' => $result->snippet->description,
  192. 'title' => $title.'.avi', // This is a hack so we accept this file by extension.
  193. 'thumbnail' => $thumb->url,
  194. 'thumbnail_width' => (int)$thumb->width,
  195. 'thumbnail_height' => (int)$thumb->height,
  196. 'size' => '',
  197. 'date' => '',
  198. 'source' => $source,
  199. );
  200. }
  201. } catch (Google_Service_Exception $e) {
  202. // If we throw the google exception as-is, we may expose the apikey
  203. // to end users. The full message in the google exception includes
  204. // the apikey param, so we take just the part pertaining to the
  205. // actual error.
  206. $error = $e->getErrors()[0]['message'];
  207. throw new moodle_exception('apierror', 'repository_youtube', '', $error);
  208. }
  209. return $list;
  210. }
  211. /**
  212. * Youtube plugin doesn't support global search
  213. */
  214. public function global_search() {
  215. return false;
  216. }
  217. public function get_listing($path='', $page = '') {
  218. return array();
  219. }
  220. /**
  221. * Generate search form
  222. */
  223. public function print_login($ajax = true) {
  224. $ret = array();
  225. $search = new stdClass();
  226. $search->type = 'text';
  227. $search->id = 'youtube_search';
  228. $search->name = 's';
  229. $search->label = get_string('search', 'repository_youtube').': ';
  230. $sort = new stdClass();
  231. $sort->type = 'select';
  232. $sort->options = array(
  233. (object)array(
  234. 'value' => 'relevance',
  235. 'label' => get_string('sortrelevance', 'repository_youtube')
  236. ),
  237. (object)array(
  238. 'value' => 'date',
  239. 'label' => get_string('sortpublished', 'repository_youtube')
  240. ),
  241. (object)array(
  242. 'value' => 'rating',
  243. 'label' => get_string('sortrating', 'repository_youtube')
  244. ),
  245. (object)array(
  246. 'value' => 'viewCount',
  247. 'label' => get_string('sortviewcount', 'repository_youtube')
  248. )
  249. );
  250. $sort->id = 'youtube_sort';
  251. $sort->name = 'youtube_sort';
  252. $sort->label = get_string('sortby', 'repository_youtube').': ';
  253. $ret['login'] = array($search, $sort);
  254. $ret['login_btn_label'] = get_string('search');
  255. $ret['login_btn_action'] = 'search';
  256. $ret['allowcaching'] = true; // indicates that login form can be cached in filepicker.js
  257. return $ret;
  258. }
  259. /**
  260. * file types supported by youtube plugin
  261. * @return array
  262. */
  263. public function supported_filetypes() {
  264. return array('video');
  265. }
  266. /**
  267. * Youtube plugin only return external links
  268. * @return int
  269. */
  270. public function supported_returntypes() {
  271. return FILE_EXTERNAL;
  272. }
  273. /**
  274. * Is this repository accessing private data?
  275. *
  276. * @return bool
  277. */
  278. public function contains_private_data() {
  279. return false;
  280. }
  281. /**
  282. * Add plugin settings input to Moodle form.
  283. * @param object $mform
  284. * @param string $classname
  285. */
  286. public static function type_config_form($mform, $classname = 'repository') {
  287. parent::type_config_form($mform, $classname);
  288. $apikey = get_config('youtube', 'apikey');
  289. if (empty($apikey)) {
  290. $apikey = '';
  291. }
  292. $mform->addElement('text', 'apikey', get_string('apikey', 'repository_youtube'), array('value' => $apikey, 'size' => '40'));
  293. $mform->setType('apikey', PARAM_RAW_TRIMMED);
  294. $mform->addRule('apikey', get_string('required'), 'required', null, 'client');
  295. $mform->addElement('static', null, '', get_string('information', 'repository_youtube'));
  296. }
  297. /**
  298. * Names of the plugin settings
  299. * @return array
  300. */
  301. public static function get_type_option_names() {
  302. return array('apikey', 'pluginname');
  303. }
  304. }