mirrored from git://git.moodle.org/moodle.git
/
block_tag_youtube.php
401 lines (334 loc) · 13.5 KB
/
block_tag_youtube.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Tag youtube block
*
* @package block_tag_youtube
* @copyright 1999 onwards Martin Dougiamas (http://dougiamas.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('DEFAULT_NUMBER_OF_VIDEOS', 5);
class block_tag_youtube extends block_base {
/**
* @var Google_Service_Youtube
*/
protected $service = null;
function init() {
$this->title = get_string('pluginname','block_tag_youtube');
$this->config = new stdClass();
}
function applicable_formats() {
return array('tag' => true);
}
/**
* It can be configured.
*
* @return bool
*/
public function has_config() {
return true;
}
function specialization() {
$this->title = !empty($this->config->title) ? $this->config->title : get_string('pluginname', 'block_tag_youtube');
// Convert numeric categories (old YouTube API) to
// textual ones (new Google Data API)
$this->config->category = !empty($this->config->category) ? $this->category_map_old2new($this->config->category) : '0';
}
function instance_allow_multiple() {
return true;
}
function get_content() {
global $CFG;
//note: do NOT include files at the top of this file
require_once($CFG->dirroot.'/tag/lib.php');
require_once($CFG->libdir . '/filelib.php');
if ($this->content !== NULL) {
return $this->content;
}
$this->content = new stdClass();
$this->content->footer = '';
if (!$this->get_service()) {
$this->content->text = $this->get_error_message();
return $this->content;
}
$text = '';
if(!empty($this->config->playlist)){
//videos from a playlist
$text = $this->get_videos_by_playlist();
}
else{
if(!empty($this->config->category)){
//videos from category with tag
$text = $this->get_videos_by_tag_and_category();
}
else {
//videos with tag
$text = $this->get_videos_by_tag();
}
}
$this->content->text = $text;
return $this->content;
}
function get_videos_by_playlist(){
if (!$service = $this->get_service()) {
return $this->get_error_message();
}
$numberofvideos = DEFAULT_NUMBER_OF_VIDEOS;
if( !empty($this->config->numberofvideos)) {
$numberofvideos = $this->config->numberofvideos;
}
try {
$response = $service->playlistItems->listPlaylistItems('id,snippet', array(
'playlistId' => $this->config->playlist,
'maxResults' => $numberofvideos
));
} catch (Google_Service_Exception $e) {
debugging('Google service exception: ' . $e->getMessage(), DEBUG_DEVELOPER);
return $this->get_error_message(get_string('requesterror', 'block_tag_youtube'));
}
return $this->render_items($response);
}
function get_videos_by_tag(){
if (!$service = $this->get_service()) {
return $this->get_error_message();
}
$tagid = optional_param('id', 0, PARAM_INT); // tag id - for backware compatibility
$tag = optional_param('tag', '', PARAM_TAG); // tag
if ($tag) {
$tagobject = tag_get('name', $tag);
} else if ($tagid) {
$tagobject = tag_get('id', $tagid);
}
if (empty($tagobject)) {
return '';
}
$querytag = urlencode($tagobject->name);
$numberofvideos = DEFAULT_NUMBER_OF_VIDEOS;
if ( !empty($this->config->numberofvideos) ) {
$numberofvideos = $this->config->numberofvideos;
}
try {
$response = $service->search->listSearch('id,snippet', array(
'q' => $querytag,
'type' => 'video',
'maxResults' => $numberofvideos
));
} catch (Google_Service_Exception $e) {
debugging('Google service exception: ' . $e->getMessage(), DEBUG_DEVELOPER);
return $this->get_error_message(get_string('requesterror', 'block_tag_youtube'));
}
return $this->render_items($response);
}
function get_videos_by_tag_and_category(){
if (!$service = $this->get_service()) {
return $this->get_error_message();
}
$tagid = optional_param('id', 0, PARAM_INT); // tag id - for backware compatibility
$tag = optional_param('tag', '', PARAM_TAG); // tag
if ($tag) {
$tagobject = tag_get('name', $tag);
} else if ($tagid) {
$tagobject = tag_get('id', $tagid);
}
if (empty($tagobject)) {
return '';
}
$querytag = urlencode($tagobject->name);
$numberofvideos = DEFAULT_NUMBER_OF_VIDEOS;
if( !empty($this->config->numberofvideos)) {
$numberofvideos = $this->config->numberofvideos;
}
try {
$response = $service->search->listSearch('id,snippet', array(
'q' => $querytag,
'type' => 'video',
'maxResults' => $numberofvideos,
'videoCategoryId' => $this->config->category
));
} catch (Google_Service_Exception $e) {
debugging('Google service exception: ' . $e->getMessage(), DEBUG_DEVELOPER);
return $this->get_error_message(get_string('requesterror', 'block_tag_youtube'));
}
return $this->render_items($response);
}
/**
* Sends a request to fetch data.
*
* @see block_tag_youtube::service
* @deprecated since Moodle 2.8.8, 2.9.2 and 3.0 MDL-49085 - please do not use this function any more.
* @param string $request
* @throws coding_exception
*/
public function fetch_request($request) {
throw new coding_exception('Sorry, this function has been deprecated in Moodle 2.8.8, 2.9.2 and 3.0. Use block_tag_youtube::get_service instead.');
$c = new curl(array('cache' => true, 'module_cache'=>'tag_youtube'));
$c->setopt(array('CURLOPT_TIMEOUT' => 3, 'CURLOPT_CONNECTTIMEOUT' => 3));
$response = $c->get($request);
$xml = new SimpleXMLElement($response);
return $this->render_video_list($xml);
}
/**
* Renders the video list.
*
* @see block_tag_youtube::render_items
* @deprecated since Moodle 2.8.8, 2.9.2 and 3.0 MDL-49085 - please do not use this function any more.
* @param SimpleXMLElement $xml
* @throws coding_exception
*/
function render_video_list(SimpleXMLElement $xml){
throw new coding_exception('Sorry, this function has been deprecated in Moodle 2.8.8, 2.9.2 and 3.0. Use block_tag_youtube::render_items instead.');
}
/**
* Returns an error message.
*
* Useful when the block is not properly set or something goes wrong.
*
* @param string $message The message to display.
* @return string HTML
*/
protected function get_error_message($message = null) {
global $OUTPUT;
if (empty($message)) {
$message = get_string('apierror', 'block_tag_youtube');
}
return $OUTPUT->notification($message);
}
/**
* Gets the youtube service object.
*
* @return Google_Service_YouTube
*/
protected function get_service() {
global $CFG;
if (!$apikey = get_config('block_tag_youtube', 'apikey')) {
return false;
}
// Wrapped in an if in case we call different get_videos_* multiple times.
if (!isset($this->service)) {
require_once($CFG->libdir . '/google/lib.php');
$client = get_google_client();
$client->setDeveloperKey($apikey);
$client->setScopes(array(Google_Service_YouTube::YOUTUBE_READONLY));
$this->service = new Google_Service_YouTube($client);
}
return $this->service;
}
/**
* Renders the list of items.
*
* @param array $videosdata
* @return string HTML
*/
protected function render_items($videosdata) {
if (!$videosdata || empty($videosdata->items)) {
if (!empty($videosdata->error)) {
debugging('Error fetching data from youtube: ' . $videosdata->error->message, DEBUG_DEVELOPER);
}
return '';
}
// If we reach that point we already know that the API key is set.
$service = $this->get_service();
$text = html_writer::start_tag('ul', array('class' => 'yt-video-entry unlist img-text'));
foreach ($videosdata->items as $video) {
// Link to the video included in the playlist if listing a playlist.
if (!empty($video->snippet->resourceId)) {
$id = $video->snippet->resourceId->videoId;
$playlist = '&list=' . $video->snippet->playlistId;
} else {
$id = $video->id->videoId;
$playlist = '';
}
$thumbnail = $video->snippet->getThumbnails()->getDefault();
$url = 'http://www.youtube.com/watch?v=' . $id . $playlist;
$videodetails = $service->videos->listVideos('id,contentDetails', array('id' => $id));
if ($videodetails && !empty($videodetails->items)) {
// We fetch by id so we just use the first one.
$details = $videodetails->items[0];
$start = new DateTime('@0');
$start->add(new DateInterval($details->contentDetails->duration));
$seconds = $start->format('U');
}
$text .= html_writer::start_tag('li');
$imgattrs = array('class' => 'youtube-thumb', 'src' => $thumbnail->url, 'alt' => $video->snippet->title);
$thumbhtml = html_writer::empty_tag('img', $imgattrs);
$link = html_writer::tag('a', $thumbhtml, array('href' => $url));
$text .= html_writer::tag('div', $link, array('class' => 'clearfix'));
$text .= html_writer::tag('span', html_writer::tag('a', $video->snippet->title, array('href' => $url)));
if (!empty($seconds)) {
$text .= html_writer::tag('div', format_time($seconds));
}
$text .= html_writer::end_tag('li');
}
$text .= html_writer::end_tag('ul');
return $text;
}
function get_categories() {
// TODO: Right now using sticky categories from
// http://gdata.youtube.com/schemas/2007/categories.cat
// This should be performed from time to time by the block insead
// and cached somewhere, avoiding deprecated ones and observing regions
return array (
'0' => get_string('anycategory', 'block_tag_youtube'),
'Film' => get_string('filmsanimation', 'block_tag_youtube'),
'Autos' => get_string('autosvehicles', 'block_tag_youtube'),
'Music' => get_string('music', 'block_tag_youtube'),
'Animals'=> get_string('petsanimals', 'block_tag_youtube'),
'Sports' => get_string('sports', 'block_tag_youtube'),
'Travel' => get_string('travel', 'block_tag_youtube'),
'Games' => get_string('gadgetsgames', 'block_tag_youtube'),
'Comedy' => get_string('comedy', 'block_tag_youtube'),
'People' => get_string('peopleblogs', 'block_tag_youtube'),
'News' => get_string('newspolitics', 'block_tag_youtube'),
'Entertainment' => get_string('entertainment', 'block_tag_youtube'),
'Education' => get_string('education', 'block_tag_youtube'),
'Howto' => get_string('howtodiy', 'block_tag_youtube'),
'Tech' => get_string('scienceandtech', 'block_tag_youtube')
);
}
/**
* Provide conversion from old numeric categories available in youtube API
* to the new ones available in the Google API
*
* @param int $oldcat old category code
* @return mixed new category code or 0 (if no match found)
*
* TODO: Someday this should be applied on upgrade for all the existing
* block instances so we won't need the mapping any more. That would imply
* to implement restore handling to perform the conversion of old blocks.
*/
function category_map_old2new($oldcat) {
$oldoptions = array (
0 => '0',
1 => 'Film',
2 => 'Autos',
23 => 'Comedy',
24 => 'Entertainment',
10 => 'Music',
25 => 'News',
22 => 'People',
15 => 'Animals',
26 => 'Howto',
17 => 'Sports',
19 => 'Travel',
20 => 'Games'
);
if (array_key_exists($oldcat, $oldoptions)) {
return $oldoptions[$oldcat];
} else {
return $oldcat;
}
}
}