2008-10-23 04:10:59 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* repository_youtube class
|
|
|
|
*
|
|
|
|
* @author Dongsheng Cai <dongsheng@moodle.com>
|
|
|
|
* @version $Id$
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
|
|
|
|
*/
|
|
|
|
|
|
|
|
class repository_youtube extends repository {
|
|
|
|
public function __construct($repositoryid, $context = SITEID, $options = array()) {
|
|
|
|
$options['keyword'] = optional_param('youtube_keyword', '', PARAM_RAW);
|
2008-10-23 07:11:47 +00:00
|
|
|
$options['start'] =1;
|
|
|
|
$options['max'] = 25;
|
|
|
|
$options['sort'] = 'relevance';
|
2008-10-23 04:10:59 +00:00
|
|
|
parent::__construct($repositoryid, $context, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function check_login() {
|
2008-10-23 07:11:47 +00:00
|
|
|
return !empty($this->keyword);
|
2008-10-23 04:10:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function search($search_text) {
|
2008-10-23 07:11:47 +00:00
|
|
|
$ret = array();
|
|
|
|
$ret['list'] = $this->_get_collection($search_text, $this->start, $this->max, $this->sort);
|
|
|
|
return $ret;
|
2008-10-23 04:10:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private function _get_collection($keyword, $start, $max, $sort) {
|
|
|
|
$list = array();
|
2008-11-19 01:54:35 +00:00
|
|
|
$this->feed_url = 'http://gdata.youtube.com/feeds/api/videos?vq=' . urlencode($keyword) . '&format=5&start-index=' . $start . '&max-results=' .$max . '&orderby=' . $sort;
|
2008-10-23 04:10:59 +00:00
|
|
|
$c = new curl(array('cache'=>true));
|
|
|
|
$content = $c->get($this->feed_url);
|
|
|
|
$xml = simplexml_load_string($content);
|
|
|
|
$media = $xml->entry->children('http://search.yahoo.com/mrss/');
|
|
|
|
$links = $xml->children('http://www.w3.org/2005/Atom');
|
|
|
|
foreach ($xml->entry as $entry) {
|
|
|
|
$media = $entry->children('http://search.yahoo.com/mrss/');
|
|
|
|
$title = $media->group->title;
|
|
|
|
$attrs = $media->group->thumbnail->attributes();
|
|
|
|
$thumbnail = $attrs['url'];
|
|
|
|
$arr = explode('/', $entry->id);
|
|
|
|
$id = $arr[count($arr)-1];
|
|
|
|
$source = 'http://www.youtube.com/v/'.$id;
|
|
|
|
$list[] = array(
|
|
|
|
'title'=>(string)$title,
|
|
|
|
'thumbnail'=>(string)$attrs['url'],
|
2008-11-27 03:32:48 +00:00
|
|
|
'thumbnail_width'=>150,
|
|
|
|
'thumbnail_height'=>120,
|
2008-10-23 04:10:59 +00:00
|
|
|
'size'=>'',
|
|
|
|
'date'=>'',
|
|
|
|
'source'=>$source
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return $list;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_file($url, $title) {
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
public function global_search() {
|
|
|
|
return false;
|
|
|
|
}
|
2009-02-18 06:52:54 +00:00
|
|
|
public function get_listing($path='', $page = '') {
|
2008-10-23 07:11:47 +00:00
|
|
|
global $CFG;
|
2008-10-23 04:10:59 +00:00
|
|
|
$list = array();
|
|
|
|
$ret = array();
|
2008-10-23 07:11:47 +00:00
|
|
|
$ret['nologin'] = true;
|
2008-10-23 04:10:59 +00:00
|
|
|
$ret['path'] = array(array('name'=>'Root', 'path'=>0));
|
2008-10-23 07:11:47 +00:00
|
|
|
$ret['list'] = $this->_get_collection($this->keyword, $this->start, $this->max, $this->sort);
|
2008-10-23 04:10:59 +00:00
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function print_login($ajax = true) {
|
|
|
|
if ($ajax) {
|
|
|
|
$ret = array();
|
|
|
|
$search = new stdclass;
|
|
|
|
$search->type = 'text';
|
|
|
|
$search->id = 'youtube_search';
|
|
|
|
$search->name = 'youtube_keyword';
|
|
|
|
$search->label = get_string('search', 'repository_youtube').': ';
|
|
|
|
$ret['login'] = array($search);
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
}
|
2008-12-08 05:19:09 +00:00
|
|
|
public function supported_return_value() {
|
|
|
|
// return 'link';
|
|
|
|
// return 'ref_id';
|
|
|
|
return 'link';
|
|
|
|
}
|
|
|
|
public function supported_filetypes() {
|
|
|
|
return array('web_video');
|
|
|
|
}
|
2008-10-23 04:10:59 +00:00
|
|
|
}
|