Merge branch 'm20_MDL-22449_wiki_help_buttons' of git://github.com/dongsheng/moodle
[moodle.git] / repository / youtube / lib.php
CommitLineData
9d833292 1<?php
10d53fd3
DC
2
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
9d833292 18/**
19 * repository_youtube class
20 *
10d53fd3 21 * @since 2.0
d078f6d3
PS
22 * @package repository
23 * @subpackage youtube
24 * @copyright 2009 Dongsheng Cai
25 * @author Dongsheng Cai <dongsheng@moodle.com>
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
9d833292 27 */
28
29class repository_youtube extends repository {
447c7a19 30 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
3d1df2fe 31 $this->keyword = optional_param('youtube_keyword', '', PARAM_RAW);
32 $this->start =1;
33 $this->max = 27;
34 $this->sort = 'published';
9d833292 35 parent::__construct($repositoryid, $context, $options);
9d833292 36 }
37
38 public function check_login() {
65df7713 39 return !empty($this->keyword);
9d833292 40 }
41
42 public function search($search_text) {
97977aa8 43 $ret = array();
44 $ret['nologin'] = true;
45 $ret['list'] = $this->_get_collection($this->keyword, $this->start, $this->max, $this->sort);
65df7713 46 return $ret;
9d833292 47 }
48
49 private function _get_collection($keyword, $start, $max, $sort) {
50 $list = array();
3d1df2fe 51 $this->feed_url = 'http://gdata.youtube.com/feeds/api/videos?q=' . urlencode($keyword) . '&format=5&start-index=' . $start . '&max-results=' .$max . '&orderby=' . $sort;
5430f05b 52 $c = new curl(array('cache'=>true, 'module_cache'=>'repository'));
9d833292 53 $content = $c->get($this->feed_url);
4454447d 54 $xml = simplexml_load_string($content);
9d833292 55 $media = $xml->entry->children('http://search.yahoo.com/mrss/');
4454447d 56 $links = $xml->children('http://www.w3.org/2005/Atom');
9d833292 57 foreach ($xml->entry as $entry) {
58 $media = $entry->children('http://search.yahoo.com/mrss/');
59 $title = $media->group->title;
60 $attrs = $media->group->thumbnail->attributes();
61 $thumbnail = $attrs['url'];
62 $arr = explode('/', $entry->id);
63 $id = $arr[count($arr)-1];
662688bd 64 $source = 'http://www.youtube.com/v/' . $id . '#' . $title;
9d833292 65 $list[] = array(
66 'title'=>(string)$title,
67 'thumbnail'=>(string)$attrs['url'],
1c4a1552 68 'thumbnail_width'=>150,
69 'thumbnail_height'=>120,
9d833292 70 'size'=>'',
71 'date'=>'',
ea789897 72 'source'=>$source
9d833292 73 );
4317f92f 74 }
9d833292 75 return $list;
76 }
77
9d833292 78 public function global_search() {
79 return false;
80 }
5fa358d1 81 public function get_listing($path='', $page = '') {
9d833292 82 $ret = array();
65df7713 83 $ret['nologin'] = true;
65df7713 84 $ret['list'] = $this->_get_collection($this->keyword, $this->start, $this->max, $this->sort);
9d833292 85 return $ret;
86 }
87
88 public function print_login($ajax = true) {
97977aa8 89 $ret = array();
6bdfef5d 90 $search = new stdClass();
97977aa8 91 $search->type = 'text';
92 $search->id = 'youtube_search';
93 $search->name = 'youtube_keyword';
94 $search->label = get_string('search', 'repository_youtube').': ';
95 $ret['login'] = array($search);
96 $ret['login_btn_label'] = get_string('search');
da2946c9 97 $ret['login_btn_action'] = 'search';
97977aa8 98 return $ret;
9d833292 99 }
014c1ca0 100 public function supported_filetypes() {
101 return array('web_video');
102 }
41076c58
DC
103 public function supported_returntypes() {
104 return FILE_EXTERNAL;
105 }
9d833292 106}