-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.php
142 lines (118 loc) · 4.5 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
<?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/>.
/**
* @package local_vimeoapi
* @copyright Nicholas Yang
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/local/vimeoapi/locallib.php');
/**
* Gets an album's duration. Checks Moodle Cache first. If it's not there,
* get it through the Vimeo API.
*
* @param stdClass $albumid Vimeo album ID.
* @param string $format
* hms (X hrs Y mins Z secs)
* :: (X:Y:Z)
* seconds (Z secs)
* @return string Formatted string showing the album's duration.
*/
function local_vimeoapi_get_album_duration($albumid, $format = 'hms') {
if (empty($albumid))
return;
// First check Moodle Cache
$cache = cache::make('local_vimeoapi', 'albumdurations');
$duration = $cache->get($albumid);
$gotcached = true;
// Override cache retrieval if disabled in settings
if (!get_config('local_vimeoapi', 'caching') || local_vimeoapi_force_cache_update_from_queryparam()) {
unset($duration);
}
// Connect to Vimeo only if duration hasn't been Moodle-cached
if (empty($duration)) {
$duration = local_vimeoapi_get_album_field($albumid, 'duration');
// Cache the duration for this album
$cache->set($albumid, $duration);
$gotcached = false;
}
return local_vimeoapi_format_seconds($duration, $format) . ' ' . local_vimeoapi_display_origin($gotcached);
}
/**
* Gets a video's duration. Checks Moodle Cache first. If it's not there,
* get it through the Vimeo API.
*
* @param stdClass $videoid Vimeo video ID.
* @param string $format
* hms (X hrs Y mins Z secs)
* :: (X:Y:Z)
* seconds (Z secs)
* @return string Formatted string showing the video's duration.
*/
function local_vimeoapi_get_video_duration($videoid, $format = 'hms') {
if (empty($videoid))
return;
// First check Moodle Cache
$cache = cache::make('local_vimeoapi', 'videodurations');
$duration = $cache->get($videoid);
$gotcached = true;
// Override cache retrieval if disabled in settings
if (!get_config('local_vimeoapi', 'caching') || local_vimeoapi_force_cache_update_from_queryparam()) {
unset($duration);
}
// Connect to Vimeo only if duration hasn't been Moodle-cached
if (empty($duration)) {
$duration = local_vimeoapi_get_video_field($videoid, 'duration');
// Cache the duration for this video
$cache->set($videoid, $duration);
$gotcached = false;
}
return local_vimeoapi_format_seconds($duration, $format) . ' ' . local_vimeoapi_display_origin($gotcached);
}
/**
* Gets a video's thumbnail image link. Checks Moodle Cache first. If it's not
* there, get it through the Vimeo API.
*
* @param stdClass $videoid Vimeo video ID.
* @param string $size Requested thumbnail size: small/medium/large
* @param bool $displayorigin Override displaying of origin.
* @return string Image link of video thumbnail.
*/
function local_vimeoapi_get_video_thumb($videoid, $size = 'large', $displayorigin = true) {
if (empty($videoid))
return;
// First check Moodle Cache
$cache = cache::make('local_vimeoapi', 'videothumbs');
$thumb = $cache->get($videoid);
$gotcached = true;
// Override cache retrieval if disabled in settings
if (!get_config('local_vimeoapi', 'caching') || local_vimeoapi_force_cache_update_from_queryparam()) {
unset($thumb);
}
// Connect to Vimeo only if thumb hasn't been Moodle-cached
if (empty($thumb)) {
$thumb = local_vimeoapi_get_video_thumb_helper($videoid, $size);
// Cache the thumb for this video
$cache->set($videoid, $thumb);
$gotcached = false;
}
if ($displayorigin) {
return $thumb . ' ' . local_vimeoapi_display_origin($gotcached);
}
else {
return $thumb;
}
}