-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
289 lines (268 loc) · 10.6 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
<?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/>.
/**
* Giphy repository plugin.
*
* @package repository_giphy
* @copyright 2017 Andrei Bautu
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/repository/lib.php');
/**
* Giphy repository plugin implementation.
* @author Andrei Bautu
* @copyright 2017 Andrei Bautu
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class repository_giphy extends repository {
/**
* Retrieves a list of from giphy's API
* @see https://developers.giphy.com/explorer/
*
* @param string $id id of the image to retrieve data for
* @param string $search text to search for (use when id is null); if null, then trending images will be retrieve
* @param int $page the page of the listing to retrieve
* @param int $pagesize the number of items per page to retrieve
* @return array
*/
protected function get_files($id = null, $search = null, $page = 0, $pagesize = 0) {
if (empty($page)) {
$page = 1;
}
if (empty($pagesize)) {
$pagesize = (int)get_config('giphy', 'page_size');
}
$url = 'https://api.giphy.com/v1/gifs/';
if ($id) {
$url .= $id . '?';
} else if ($search) {
$url .= 'search?q=' . urlencode($search);
} else {
$url .= 'trending?';
}
$url .= '&offset=' . (($page - 1) * $pagesize);
$url .= '&limit=' . $pagesize;
$url .= '&api_key=' . get_config('giphy', 'api_key');
$url .= '&rating=' . get_config('giphy', 'rating');
$data = @file_get_contents($url);
$data = @json_decode($data);
if ($data->meta->status != 200) {
return null;
}
$data->pagination->pagesize = max($pagesize, $data->pagination->count);
$data->pagination->path = $id;
// Make the output uniform so format_files and format_folders have simpler structure.
if ($id) {
$data->data = array($data->data);
$data->pagination->page = 1;
$data->pagination->pages = 1;
} else {
$data->pagination->page = 1 + floor($data->pagination->offset / $data->pagination->pagesize);
$data->pagination->pages = ceil($data->pagination->total_count / $data->pagination->pagesize);
}
return $data;
}
/**
* Format files data as folders for filepicker.
*
* @param object $data object received from get_files
* @return array
* @see https://docs.moodle.org/dev/Repository_plugins
*/
protected function format_folders($data) {
$list = array();
foreach ($data->data as $item) {
$list[] = array(
'title' => ($item->title ? $item->title : $item->slug),
'shorttitle' => $item->slug,
'date' => strtotime($item->import_datetime),
'thumbnail' => $item->images->fixed_height_small->url,
'icon' => $item->images->fixed_height_small_still->url,
'children' => array(),
'path' => $item->id,
'size' => $item->images->original->size,
);
}
$result = array(
'nologin' => true,
'dynload' => true,
'page' => $data->pagination->page,
'pages' => $data->pagination->pages,
'list' => $list,
);
return $result;
}
/**
* Format files data as files for filepicker.
*
* @param object $data object received from get_files
* @return array
* @see https://docs.moodle.org/dev/Repository_plugins
*/
protected function format_files($data) {
$mimetypes = $this->options['mimetypes'];
if (is_string($mimetypes) && $mimetypes == '*') {
$mimetypes = array('.gif', '.mp4', '.webp');
}
$list = array();
$sources = array('url', 'mp4', 'webp');
foreach ($data->data as $item) {
// For each image, we have multiple formats.
foreach ($item->images as $format => $img) {
$thumbnail = strpos($format, '_still') ? 'fixed_height_small_still' : 'fixed_height_small';
$display = str_replace('_', ' ', $format);
$template = array(
'date' => strtotime($item->import_datetime),
'thumbnail' => $item->images->{$thumbnail}->url,
'thumbnail_width' => $item->images->{$thumbnail}->width,
'thumbnail_height' => $item->images->{$thumbnail}->height,
'icon' => $item->images->{$thumbnail}->url,
'author' => $item->user->display_name,
'image_height' => $img->height,
'image_width' => $img->width,
);
// For a particular format, we can have multiple files.
foreach ($sources as $urlfield) {
if (empty($img->$urlfield)) {
continue;
}
$url = $img->$urlfield;
$extension = strtolower(pathinfo($url, PATHINFO_EXTENSION));
if (!in_array('.' . $extension, $mimetypes)) {
continue;
}
$sizefield = $urlfield == 'url' ? 'size' : $urlfield .'_size';
$template['source'] = $url;
$template['url'] = $url;
$template['size'] = $img->$sizefield;
$template['title'] = ($item->title ? $item->title : $item->slug) . '.' . $extension;
$template['shorttitle'] = strtoupper($extension) . '@' . $img->width .'x'. $img->height . 'px '
. display_size($img->$sizefield) . ' - ' . $display;
$list[] = $template;
}
}
}
$result = array(
'path' => array(array('name' => 'Giphy', 'path' => ''), array('name' => $item->title, 'path' => $item->id)),
'nologin' => true,
'dynload' => false,
'page' => $data->pagination->page,
'pages' => $data->pagination->pages,
'list' => $list,
);
return $result;
}
/**
* Get file listing
*
* @param string $path path of the listing
* @param int $page page of listing
* @return mixed
*/
public function get_listing($path = '', $page = '') {
$data = $this->get_files($path, null, (int)$page);
if ($path) {
return $this->format_files($data);
}
return $this->format_folders($data);
}
/**
* Search files in repository
* When doing global search, $searchtext will be used as
* keyword.
*
* @param string $searchtext search key word
* @param int $page page
* @return mixed see {@see repository::get_listing()}
*/
public function search($searchtext, $page = '') {
$data = $this->get_files(null, $searchtext, (int)$page);
return $this->format_folders($data);
}
/**
* Tells how the file can be picked from this repository
*
* @return int
*/
public function supported_returntypes() {
return FILE_INTERNAL | FILE_EXTERNAL;
}
/**
* What kind of files will be in this repository?
*
* @return array return '*' means this repository support any files, otherwise
* return mimetypes of files, it can be an array
*/
public function supported_filetypes() {
return array('web_image', 'web_video');
}
/**
* Show the search screen, if required
*
* @return string
*/
public function print_search() {
global $CFG;
$str = parent::print_search();
$str .= html_writer::img("{$CFG->wwwroot}/repository/giphy/pix/Poweredby_100px-Black_VertLogo.png", "Giphy");
return $str;
}
/**
* Return names of the general options.
* By default: no general option name
*
* @return array
*/
public static function get_type_option_names() {
return array_merge(parent::get_type_option_names(), array(
'api_key',
'rating',
'page_size',
));
}
/**
* Edit/Create Admin Settings Moodle form
*
* @param MoodleQuickForm $mform Moodle form (passed by reference)
* @param string $classname repository class name
*/
public static function type_config_form($mform, $classname = 'repository') {
parent::type_config_form($mform, $classname);
$apikey = get_config('giphy', 'api_key');
$rating = get_config('giphy', 'rating');
$pagesize = (int)get_config('giphy', 'page_size');
$mform->addElement('text', 'api_key', get_string('api_key', 'repository_giphy'), array('size' => '32'));
$mform->addRule('api_key', get_string('required'), 'required', null, 'client');
$mform->setDefault('api_key', $apikey);
$mform->setType('api_key', PARAM_ALPHANUMEXT);
$ratings = array(
'' => get_string('any'),
'Y' => get_string('ratingY', 'repository_giphy'),
'PG-13' => get_string('ratingPG-13', 'repository_giphy'),
'PG' => get_string('ratingPG', 'repository_giphy'),
'R' => get_string('ratingR', 'repository_giphy'),
'G' => get_string('ratingG', 'repository_giphy'),
);
$mform->addElement('select', 'rating', get_string('rating', 'repository_giphy'), $ratings);
$mform->setDefault('rating', $rating);
$pagesizes = array(25, 50, 100, 250, 500, 1000);
$pagesizes = array_combine($pagesizes, $pagesizes);
$mform->addElement('select', 'page_size', get_string('page_size', 'repository_giphy'), $pagesizes);
$mform->addRule('page_size', get_string('required'), 'required', null, 'client');
$mform->setDefault('page_size', $pagesizes);
}
}