Merge branch 'MDL-33122-master' of git://github.com/ankitagarwal/moodle
[moodle.git] / repository / youtube / lib.php
blob7a7c695ed5b02712d04f0cf30d6f7b2f13f68ddf
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.
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/>.
17 /**
18 * This plugin is used to access youtube videos
20 * @since 2.0
21 * @package repository_youtube
22 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once($CFG->dirroot . '/repository/lib.php');
27 /**
28 * repository_youtube class
30 * @since 2.0
31 * @package repository_youtube
32 * @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class repository_youtube extends repository {
37 /** @var int maximum number of thumbs per page */
38 const YOUTUBE_THUMBS_PER_PAGE = 27;
40 /**
41 * Youtube plugin constructor
42 * @param int $repositoryid
43 * @param object $context
44 * @param array $options
46 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
47 parent::__construct($repositoryid, $context, $options);
50 public function check_login() {
51 return !empty($this->keyword);
54 /**
55 * Return search results
56 * @param string $search_text
57 * @return array
59 public function search($search_text, $page = 0) {
60 global $SESSION;
61 $sort = optional_param('youtube_sort', '', PARAM_TEXT);
62 $sess_keyword = 'youtube_'.$this->id.'_keyword';
63 $sess_sort = 'youtube_'.$this->id.'_sort';
65 // This is the request of another page for the last search, retrieve the cached keyword and sort
66 if ($page && !$search_text && isset($SESSION->{$sess_keyword})) {
67 $search_text = $SESSION->{$sess_keyword};
69 if ($page && !$sort && isset($SESSION->{$sess_sort})) {
70 $sort = $SESSION->{$sess_sort};
72 if (!$sort) {
73 $sort = 'relevance'; // default
76 // Save this search in session
77 $SESSION->{$sess_keyword} = $search_text;
78 $SESSION->{$sess_sort} = $sort;
80 $this->keyword = $search_text;
81 $ret = array();
82 $ret['nologin'] = true;
83 $ret['page'] = (int)$page;
84 if ($ret['page'] < 1) {
85 $ret['page'] = 1;
87 $start = ($ret['page'] - 1) * self::YOUTUBE_THUMBS_PER_PAGE + 1;
88 $max = self::YOUTUBE_THUMBS_PER_PAGE;
89 $ret['list'] = $this->_get_collection($search_text, $start, $max, $sort);
90 $ret['norefresh'] = true;
91 $ret['nosearch'] = true;
92 $ret['pages'] = -1;
93 return $ret;
96 /**
97 * Private method to get youtube search results
98 * @param string $keyword
99 * @param int $start
100 * @param int $max max results
101 * @param string $sort
102 * @return array
104 private function _get_collection($keyword, $start, $max, $sort) {
105 $list = array();
106 $this->feed_url = 'http://gdata.youtube.com/feeds/api/videos?q=' . urlencode($keyword) . '&format=5&start-index=' . $start . '&max-results=' .$max . '&orderby=' . $sort;
107 $c = new curl(array('cache'=>true, 'module_cache'=>'repository'));
108 $content = $c->get($this->feed_url);
109 $xml = simplexml_load_string($content);
110 $media = $xml->entry->children('http://search.yahoo.com/mrss/');
111 $links = $xml->children('http://www.w3.org/2005/Atom');
112 foreach ($xml->entry as $entry) {
113 $media = $entry->children('http://search.yahoo.com/mrss/');
114 $title = $media->group->title;
115 $attrs = $media->group->thumbnail[2]->attributes();
116 $thumbnail = $attrs['url'];
117 $arr = explode('/', $entry->id);
118 $id = $arr[count($arr)-1];
119 $source = 'http://www.youtube.com/v/' . $id . '#' . $title;
120 $list[] = array(
121 'title'=>(string)$title,
122 'thumbnail'=>(string)$attrs['url'],
123 'thumbnail_width'=>150,
124 'thumbnail_height'=>120,
125 'size'=>'',
126 'date'=>'',
127 'source'=>$source
130 return $list;
134 * Youtube plugin doesn't support global search
136 public function global_search() {
137 return false;
140 public function get_listing($path='', $page = '') {
141 return array();
145 * Generate search form
147 public function print_login($ajax = true) {
148 $ret = array();
149 $search = new stdClass();
150 $search->type = 'text';
151 $search->id = 'youtube_search';
152 $search->name = 's';
153 $search->label = get_string('search', 'repository_youtube').': ';
154 $sort = new stdClass();
155 $sort->type = 'select';
156 $sort->options = array(
157 (object)array(
158 'value' => 'relevance',
159 'label' => get_string('sortrelevance', 'repository_youtube')
161 (object)array(
162 'value' => 'published',
163 'label' => get_string('sortpublished', 'repository_youtube')
165 (object)array(
166 'value' => 'rating',
167 'label' => get_string('sortrating', 'repository_youtube')
169 (object)array(
170 'value' => 'viewCount',
171 'label' => get_string('sortviewcount', 'repository_youtube')
174 $sort->id = 'youtube_sort';
175 $sort->name = 'youtube_sort';
176 $sort->label = get_string('sortby', 'repository_youtube').': ';
177 $ret['login'] = array($search, $sort);
178 $ret['login_btn_label'] = get_string('search');
179 $ret['login_btn_action'] = 'search';
180 $ret['allowcaching'] = true; // indicates that login form can be cached in filepicker.js
181 return $ret;
185 * file types supported by youtube plugin
186 * @return array
188 public function supported_filetypes() {
189 return array('web_video');
193 * Youtube plugin only return external links
194 * @return int
196 public function supported_returntypes() {
197 return FILE_EXTERNAL;