-
Notifications
You must be signed in to change notification settings - Fork 2
/
lib.php
281 lines (238 loc) · 9.61 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
<?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/>.
/**
* repository_soundcloud class
*
* @since 2.0
* @package repository
* @subpackage soundcloud
* @copyright 2011 Troy Williams
* @author Troy Williams <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class repository_soundcloud extends repository {
private $pagelimit = 50;
public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
global $CFG;
// class may be loaded through filter, apparently require_once only looks at paths
if (! class_exists('Services_Soundcloud', false)) {
require_once($CFG->dirroot.'/repository/soundcloud/soundcloudapi.php');
}
// TODO check if empty?
$this->clientid = get_config('soundcloud', 'clientid');
$this->clientsecret = get_config('soundcloud', 'clientsecret');
$this->redirecturi = $CFG->wwwroot.'/repository/repository_callback.php?repo_id='.$repositoryid;
$this->accesstoken = get_user_preferences('soundcloud_accesstoken', '');
// load up te php soundcloud wrapper
$this->soundcloudapi = new Services_Soundcloud($this->clientid, $this->clientsecret, $this->redirecturi);
if ($this->accesstoken) {
$this->soundcloudapi->setAccessToken($this->accesstoken);
}
parent::__construct($repositoryid, $context, $options);
}
public function check_login() {
return !empty($this->accesstoken);
}
public function callback() {
global $CFG;
$code = optional_param('code', false, PARAM_RAW);
$error = optional_param('error', false, PARAM_RAW);
$errordescription = optional_param('error_description', false, PARAM_RAW);
// throw exception, id, secret or redirect probably wrong
if ($error) {
throw new moodle_exception($errordescription, 'soundcloud');
}
if ($code) {
$response = $this->soundcloudapi->accessToken($code); // response returned as array
set_user_preference('soundcloud_accesstoken', $response['access_token']);
}
}
public function global_search() {
return false;
}
/**
*
* @param type $trackid
* @param type $filename
* @return type
*/
public function get_file($url, $filename = '') {
$track = $this->soundcloudapi->get('me/tracks/'.$trackid);
$track = json_decode($track);
// Check downloads remaining. TODO - how does this function under Pro accounts
if ($track->downloads_remaining === 0) {
throw new moodle_exception('download count for this track has exceeded', 'soundcloud');
}
$path = $this->prepare_file($filename);
try {
$data = $this->soundcloudapi->download($track->id);
file_put_contents($path, $data);
} catch (Exception $e) {
throw new moodle_exception($e->getMessage(), 'soundcloud');
}
return array('path'=>$path, 'url'=>$track->uri);
}
/**
*
* @param type $trackid
* @return string
*/
public function get_link($trackid) {
$track = $this->soundcloudapi->get('me/tracks/'.$trackid);
$track = json_decode($track);
$baseurl = $track->permalink_url;
$url = $baseurl.'#'.$track->title; // use title for nice naming
if ($track->sharing === 'private') {
$url = $baseurl.'/'.$track->secret_token.'#'.$track->title; // private stream, use title for nice naming
}
return $url;
}
/**
*
* @global type $CFG
* @global type $OUTPUT
* @param type $path
* @param type $page
* @return int
*/
public function get_listing($path='', $page=1) {
global $CFG, $OUTPUT;
$ret = array();
// log out link will be displayed
$ret['nologin'] = false;
$ret['nosearch'] = true;
// Get user, this will contain track totals
$me = $this->soundcloudapi->get('me');
$me = json_decode($me);
// Pagination
$ret['perpage'] = $this->pagelimit;
$ret['total'] = (int)$me->track_count + (int)$me->private_tracks_count;
$ret['pages'] = ceil($ret['total']/$ret['perpage']);
if (empty($page)) {
$page = 1;
}
if ($page <= $ret['pages']) {
$ret['page'] = (int)$page;
} else {
$ret['page'] = 1;
}
$offset = ($ret['page']-1)*$ret['perpage']; // Soundcloud offset
// Get users tracks
$tracks = $this->soundcloudapi->get('me/tracks', array('limit'=> $ret['perpage'], 'offset'=>$offset));
$tracks = json_decode($tracks);
if ($tracks) {
foreach($tracks as $track) {
$filename = $track->title. '.' . $track->original_format; // download will be original file, so use original extension
if (empty($track->artwork_url)){
$thumbnail = $OUTPUT->pix_url(file_extension_icon('.'.$track->original_format, 32));
} else {
$thumbnail = $track->artwork_url;
}
$list[] = array(
'title'=>(string)$filename,
'thumbnail'=>(string)$thumbnail,
'thumbnail_width'=>64,
'thumbnail_height'=>64,
'size'=>'',
'license'=>$track->license,
'date'=>'',
'source'=>$track->id
);
}
$ret['list'] = $list;
}
return $ret;
}
/**
*
* @param string $ajax
* @return array
*/
public function print_login($ajax = true){
global $CFG;
if ($ajax) {
$ret = array();
$popup_btn = new stdClass();
$popup_btn->type = 'popup';
$popup_btn->url = $this->soundcloudapi->getAuthorizeUrl(array('scope'=>'non-expiring'));
$ret['login'] = array($popup_btn);
return $ret;
}
}
/**
*
* @return mixed
*/
public function logout() {
set_user_preference('soundcloud_accesstoken', '');
return parent::logout();
}
/**
* Add Plugin settings input to Moodle form
* @param object $mform
*/
public static function type_config_form($mform, $classname = 'repository') {
global $CFG;
$clientid = get_config('soundcloud', 'clientid');
$clientsecret = get_config('soundcloud', 'clientsecret');
if (empty($clientid)) {
$clientid = '';
}
if (empty($clientsecret)) {
$clientsecret = '';
}
$strrequired = get_string('required');
$mform->addElement('text', 'clientid', get_string('clientid', 'repository_soundcloud'), array('value'=>$clientid, 'size'=>'40'));
$mform->setType( 'clientid', PARAM_RAW);
$mform->addElement('text', 'clientsecret', get_string('clientsecret', 'repository_soundcloud'), array('value'=>$clientsecret, 'size'=>'40'));
$mform->setType( 'clientsecret', PARAM_RAW);
// get Soundcloud instances
$params = array();
$params['onlyvisible'] = false;
$params['type'] = 'soundcloud';
$instances = repository::get_instances($params);
if (empty($instances)) {
$redirecturi = get_string('redirecturiwarning', 'repository_soundcloud');
$mform->addElement('static', null, '', $redirecturi);
} else {
$instance = array_shift($instances);
$redirecturi = $CFG->wwwroot.'/repository/repository_callback.php?repo_id='.$instance->id;
$mform->addElement('static', 'redirect_uri', '', get_string('redirecturitext', 'repository_soundcloud', $redirecturi));
}
$mform->addRule('clientid', $strrequired, 'required', null, 'client');
$mform->addRule('clientsecret', $strrequired, 'required', null, 'client');
}
public static function type_form_validation($mform, $data, $errors) {
//if (!is_dir($data['rootpath'])) {
// $errors['rootpath'] = get_string('invalidrootpath', 'repository_someplugin');
//}
$errors = array();
return $errors;
}
/**
* Need for Soundcloud configuration variables.
* @return type
*/
public static function get_type_option_names() {
return array_merge(parent::get_type_option_names(), array('clientid', 'clientsecret', 'pluginname'));
}
public function supported_returntypes() {
return FILE_EXTERNAL;
}
public function supported_filetypes() {
return '*';
}
}