Merge branch 'MDL-47182-27' of git://github.com/danpoltawski/moodle into MOODLE_27_STABLE
[moodle.git] / repository / youtube / lib.php
blobb780128c0944bd49550d880755b4a5ec27ed2b6d
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 Moodle 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 Moodle 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 // If the number of results is smaller than $max, it means we reached the last page.
93 $ret['pages'] = (count($ret['list']) < $max) ? $ret['page'] : -1;
94 return $ret;
97 /**
98 * Private method to get youtube search results
99 * @param string $keyword
100 * @param int $start
101 * @param int $max max results
102 * @param string $sort
103 * @return array
105 private function _get_collection($keyword, $start, $max, $sort) {
106 $list = array();
107 $this->feed_url = 'http://gdata.youtube.com/feeds/api/videos?q=' . urlencode($keyword) . '&format=5&start-index=' . $start . '&max-results=' .$max . '&orderby=' . $sort;
108 $c = new curl(array('cache'=>true, 'module_cache'=>'repository'));
109 $content = $c->get($this->feed_url);
110 $xml = simplexml_load_string($content);
111 $media = $xml->entry->children('http://search.yahoo.com/mrss/');
112 $links = $xml->children('http://www.w3.org/2005/Atom');
113 foreach ($xml->entry as $entry) {
114 $media = $entry->children('http://search.yahoo.com/mrss/');
115 $title = (string)$media->group->title;
116 $description = (string)$media->group->description;
117 if (empty($description)) {
118 $description = $title;
120 $attrs = $media->group->thumbnail[2]->attributes();
121 $thumbnail = $attrs['url'];
122 $arr = explode('/', $entry->id);
123 $id = $arr[count($arr)-1];
124 $source = 'http://www.youtube.com/v/' . $id . '#' . $title;
125 $list[] = array(
126 'shorttitle'=>$title,
127 'thumbnail_title'=>$description,
128 'title'=>$title.'.avi', // this is a hack so we accept this file by extension
129 'thumbnail'=>(string)$attrs['url'],
130 'thumbnail_width'=>(int)$attrs['width'],
131 'thumbnail_height'=>(int)$attrs['height'],
132 'size'=>'',
133 'date'=>'',
134 'source'=>$source
137 return $list;
141 * Youtube plugin doesn't support global search
143 public function global_search() {
144 return false;
147 public function get_listing($path='', $page = '') {
148 return array();
152 * Generate search form
154 public function print_login($ajax = true) {
155 $ret = array();
156 $search = new stdClass();
157 $search->type = 'text';
158 $search->id = 'youtube_search';
159 $search->name = 's';
160 $search->label = get_string('search', 'repository_youtube').': ';
161 $sort = new stdClass();
162 $sort->type = 'select';
163 $sort->options = array(
164 (object)array(
165 'value' => 'relevance',
166 'label' => get_string('sortrelevance', 'repository_youtube')
168 (object)array(
169 'value' => 'published',
170 'label' => get_string('sortpublished', 'repository_youtube')
172 (object)array(
173 'value' => 'rating',
174 'label' => get_string('sortrating', 'repository_youtube')
176 (object)array(
177 'value' => 'viewCount',
178 'label' => get_string('sortviewcount', 'repository_youtube')
181 $sort->id = 'youtube_sort';
182 $sort->name = 'youtube_sort';
183 $sort->label = get_string('sortby', 'repository_youtube').': ';
184 $ret['login'] = array($search, $sort);
185 $ret['login_btn_label'] = get_string('search');
186 $ret['login_btn_action'] = 'search';
187 $ret['allowcaching'] = true; // indicates that login form can be cached in filepicker.js
188 return $ret;
192 * file types supported by youtube plugin
193 * @return array
195 public function supported_filetypes() {
196 return array('video');
200 * Youtube plugin only return external links
201 * @return int
203 public function supported_returntypes() {
204 return FILE_EXTERNAL;
208 * Is this repository accessing private data?
210 * @return bool
212 public function contains_private_data() {
213 return false;