-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
executable file
·328 lines (279 loc) · 12.1 KB
/
lib.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
<?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/>.
/**
* This plugin is used to access botr videos
* requirement: installed moodle_filter_botr and setup.
*
* @since 2.0
* @package repository
* @subpackage botr (BitsOnTheRun.com)
* @copyright 2013 Guido Hornig based on the botr repository of 2009 Dongsheng Cai
* @author Guido Hornig [email protected], Dongsheng Cai <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later except the botr-API
*/
require_once($CFG->dirroot . '/repository/lib.php');
require_once($CFG->dirroot . '/repository/botr/botrapi/api.php');
/**
* repository_botr class
*
* @package repository
* @subpackage botr (BitsOnTheRun.com)
* @copyright 2013 Guido Hornig based on the botr repository of 2009 Dongsheng Cai
* @author Guido Hornig [email protected], Dongsheng Cai <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later except the botr-API
*/
class repository_botr extends repository {
/** @var int maximum number of thumbs per page */
const BOTR_THUMBS_PER_PAGE = 20;
/**
* botr plugin constructor
* @param int $repositoryid
* @param object $context
* @param array $options
*/
public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
// options['mimetypes'] =>'video';
parent::__construct($repositoryid, $context, $options);
}
public function check_login() {
return !empty($this->keyword);
}
/**
* Return search results
* @param string $search_text
* @param int $page
* @return array
*/
public function search($search_text, $page = 0) {
global $SESSION;
$sort = optional_param('botr_sort', '', PARAM_TEXT);
$sess_keyword = 'botr_'.$this->id.'_keyword';
$sess_sort = 'botr_'.$this->id.'_sort';
// This is the request of another page for the last search, retrieve the cached keyword and sort
if ($page && !$search_text && isset($SESSION->{$sess_keyword})) {
$search_text = $SESSION->{$sess_keyword};
}
if ($page && !$sort && isset($SESSION->{$sess_sort})) {
$sort = $SESSION->{$sess_sort};
}
if (!$sort) {
$sort = 'title'; // default
}
// Save this search in session
$SESSION->{$sess_keyword} = $search_text;
$SESSION->{$sess_sort} = $sort;
$this->keyword = $search_text;
$ret = array();
$ret['nologin'] = true;
// $ret['help'] = get_string('helpURL','repository_botr');
// $ret['manage'] = get_string('manageURL','repository_botr');
$ret['page'] = (int)$page;
if ($ret['page'] < 1) {
$ret['page'] = 1;
}
$start = ($ret['page'] - 1) * self::BOTR_THUMBS_PER_PAGE + 1;
$max = self::BOTR_THUMBS_PER_PAGE;
$ret['list'] = $this->_get_collection($search_text, $start, $max, $sort);
$ret['norefresh'] = false;
$ret['nosearch'] = false;
$ret['dynloading'] = true;
// $ret['object'] = array('type'=>'txt/html','url'=>'http://lern-net.de/agb/botr/');
if (!empty($ret['list'])) {
$ret['pages'] = -1; // means we don't know exactly how many pages there are but we can always jump to the next page
} else if ($ret['page'] > 1) {
$ret['pages'] = $ret['page']; // no images available on this page, this is the last page
} else {
$ret['pages'] = 0; // no paging
}
// $ret['upload'] = array ('label'=>'uptitle','id'=>'uptitleid');
return $ret;
}
/**
* Private method to get botr search results
* @param string $keyword
* @param int $start
* @param int $max max results
* @param string $sort
* @return array
*/
private function _get_collection($keyword, $start, $max, $sort) {
global $CFG;
// get connected to the botr-PLatfrom via botr_api
// the credentials can be entered in the moodle_filter_botr plugin
$botr_api = new BotrAPI($CFG->botr_key,$CFG->botr_secret);
$list = array();
$response = array();
// We get all videos for the specified owner
$params = array(
'result_limit'=>(int)$max,
'result_offset'=> (int)($start-1), //botr api counts from zero
// 'tags_mode'=>'any', // all the owner tags must be in the list
// 'tags'=>$keyword, // with the owner tags, you could select by tags and give a owner a tag
'search:custom.owner' => repository::get_option('owner') , //not here, because we get all and do not search
'order_by'=>$sort
);
// if a search term is specified select those from the search:
/* if (!empty($keyword)) {
$params['text'] = $keyword;
$params['search'] = '*'; // here could be something more efficient See http://developer.longtailvideo.com/botr/system-api/methods/videos/list.html
}
*/
// get all videos that fits $params search
$response = $botr_api->call("/videos/list",$params);
if ($response['status'] == "error") {
die(json_encode(array('e'=>get_string('botrApiProblem', 'repository_botr').$response['message'])));
}
for ($i = 0; $i < sizeof($response['videos']); $i++) { // walk though all found videos
$video = $response['videos'][$i];
/* if (!empty($keyword)) {
if (!(stripos(json_encode($video),$keyword) === false)) { $keyword = '';}
}
*/
// if (in_array($keyword,$video,true)) {
$pos = false;
if ( !empty($keyword)) {
$alltext = $video['title']." ".$video['description']." ".$video['author']." ".$video['key']." ".$video['tags'];
$pos = stripos($alltext,$keyword);
}
if ((empty($keyword)) or !($pos === false)) {
// if (isset($video['custom']['owner'])) {
// if ($video['custom']['owner'] === repository::get_option('owner')) {
# calculate the duration in format
$Sekundenzahl = round($video['duration']);
$duration = sprintf("%02d:%02d:%02d", ($Sekundenzahl / 60 / 60) % 24, ($Sekundenzahl / 60) % 60, $Sekundenzahl % 60);
// check for player option and add a '-' if the player is given
$player = repository::get_option('player');
if (!empty($player)) {
$player = "-" . $player;
}
//print_object($player);
$list[] = array( // get all video data from the api into the file picker list
'shorttitle' => $video['title'],
'thumbnail_title' => $video['description'] . "\n ⌛ " . $duration . "\n ▷ " . $video['views'],
'title' => $video['title'] . " .mp4", // hack to get it through the filepicker: we pretend to be a video mime type
'thumbnail' => "http://$CFG->botr_dnsmask/thumbs/" . $video['key'] . "-120.jpg", // thumbs are prepared at botr platform
'thumbnail_width' => 110, // try to fit 5 in a row
'thumbnail_height' => "50px", //
'size' => $video['size'],
'date' => $video['date'],
'tags' => $video['tags'],
'source' => "[botr " . $video['key'] . $player . "]",
'url' => $video['key'],
'author' => $video['author']
);
// }
}
}
return $list;
}
/**
* botr plugin doesn't support global search
*/
public function global_search() {
return false;
}
public function get_listing($path='', $page = '') {
return array();
}
/**
* Generate search form
*/
public function print_login($ajax = true) {
$ret = array();
$search = new stdClass();
$search->type = 'text';
$search->id = 'botr_search';
$search->name = 's';
$search->label = get_string('search', 'repository_botr').': ';
$sort = new stdClass();
$sort->type = 'select';
$sort->options = array(
(object)array(
'value' => 'title',
'label' => get_string('sorttitle', 'repository_botr')
),
(object)array(
'value' => 'date',
'label' => get_string('sortdate', 'repository_botr')
),
(object)array(
'value' => 'author',
'label' => get_string('sortauthor', 'repository_botr')
),
(object)array(
'value' => 'views',
'label' => get_string('sortviews', 'repository_botr')
)
);
$sort->id = 'botr_sort';
$sort->name = 'botr_sort';
$sort->label = get_string('sortby', 'repository_botr').': ';
$ret['login'] = array($search, $sort);
$ret['login_btn_label'] = get_string('search');
$ret['login_btn_action'] = 'search';
$ret['allowcaching'] = true; // indicates that login form can be cached in filepicker.js
return $ret;
}
/**
* file types supported by botr plugin
* @return array
*/
public function supported_filetypes() {
return array('video');
}
/**
* botr plugin only return external links
* @return int
*/
public function supported_returntypes() {
return FILE_EXTERNAL;
}
public static function get_type_option_names() {
return array('pluginname');
}
/* public static function type_config_form($mform) {
parent::type_config_form($mform);
$mform->addHelpButton('pluginname','pluginname','repository_botr'); // not necessary
}/**/
public static function get_instance_option_names() {
return array('owner','player'); // From repository_filesystem
}
public static function instance_config_form($mform) {
$mform->addElement('text', 'owner', get_string('owner', 'repository_botr'));
$mform->addElement('text', 'player', get_string('player', 'repository_botr'), array('size' => '8'));
// here we should test for real tags
// $mform->addRule('owner', get_string('callback'), 'callback', an api call to botr_api, 'client');
$mform->addRule('player', get_string('playerrule','repository_botr'), 'nopunctuation', null, 'client');
$mform->addHelpButton('owner','owner','repository_botr');
$mform->addHelpButton('player','player','repository_botr');
$mform->addHelpButton('name','defaultname','repository_botr');
}/**/
public static function plugin_init() {
//here we create a default repository instance. The last parameter is 1 in order to set the instance as readonly.
/** @noinspection PhpDeprecationInspection */
$id = repository::static_function('botr','create', 'botr', 0, get_system_context(),
array('name' => get_string('defaultname','repository_botr'),
'owner' => null,
'player' => null,
),
1);
if (empty($id)) {
return false;
} else {
return true;
}
}
}