forked from johnkelsh/moodle-mod_lightboxgallery
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathlocallib.php
More file actions
293 lines (256 loc) · 9.27 KB
/
locallib.php
File metadata and controls
293 lines (256 loc) · 9.27 KB
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
<?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/>.
/**
* Internal library of functions for module lightboxgallery
*
* All the newmodule specific functions, needed to implement the module
* logic, should go here. Never include this file from your lib.php!
*
* @package mod_lightboxgallery
* @copyright 2011 NetSpot Pty Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once(dirname(__FILE__) . '/lib.php');
require_once("$CFG->libdir/filelib.php");
define('THUMB_WIDTH', 150);
define('THUMB_HEIGHT', 150);
define('MAX_IMAGE_LABEL', 13);
define('MAX_COMMENT_PREVIEW', 20);
define('AUTO_RESIZE_SCREEN', 1);
define('AUTO_RESIZE_UPLOAD', 2);
define('AUTO_RESIZE_BOTH', 3);
/**
* Add a set of uploaded files to the gallery.
*
* @param array $files A list of stored_file objects.
* @param context $context
* @param cm_info $cm
* @param stdClass $gallery
* @param int $resize
* @return void
*/
function lightboxgallery_add_images($files, $context, $cm, $gallery, $resize = 0) {
require_once(dirname(__FILE__) . '/imageclass.php');
$fs = get_file_storage();
$images = [];
foreach ($files as $storedfile) {
if ($storedfile->get_mimetype() == 'application/zip') {
// Unpack.
$packer = get_file_packer('application/zip');
$fs->delete_area_files($context->id, 'mod_lightboxgallery', 'unpacktemp', 0);
$storedfile->extract_to_storage($packer, $context->id, 'mod_lightboxgallery', 'unpacktemp', 0, '/');
$images = $fs->get_area_files($context->id, 'mod_lightboxgallery', 'unpacktemp', 0);
$storedfile->delete();
} else {
$images[] = $storedfile;
}
}
foreach ($images as $storedfile) {
if ($storedfile->is_valid_image()) {
$filename = $storedfile->get_filename();
$fileinfo = [
'contextid' => $context->id,
'component' => 'mod_lightboxgallery',
'filearea' => 'gallery_images',
'itemid' => 0,
'filepath' => '/',
'filename' => $filename,
];
if (!$fs->get_file($context->id, 'mod_lightboxgallery', 'gallery_images', 0, '/', $filename)) {
$storedfile = $fs->create_file_from_storedfile($fileinfo, $storedfile);
$image = new lightboxgallery_image($storedfile, $gallery, $cm);
if ($resize > 0) {
$resizeoptions = lightboxgallery_resize_options();
[$width, $height] = explode('x', $resizeoptions[$resize]);
$image->resize_image($width, $height);
}
$image->set_caption($filename);
}
}
}
$fs->delete_area_files($context->id, 'mod_lightboxgallery', 'unpacktemp', 0);
}
/**
* Set default configuration values for the lightboxgallery module.
*
* @return void
* @throws dml_exception
*/
function lightboxgallery_config_defaults() {
$defaults = [
'disabledplugins' => '',
'enablerssfeeds' => 0,
];
$localcfg = get_config('lightboxgallery');
foreach ($defaults as $name => $value) {
if (! isset($localcfg->$name)) {
set_config($name, $value, 'lightboxgallery');
}
}
}
/**
* Get the list of editing plugins.
*
* @param bool|null $showall
* @param stdClass|null $image
* @return array
* @throws coding_exception
* @throws dml_exception
*/
function lightboxgallery_edit_types($showall = false, $image = null) {
$result = [];
$disabledplugins = explode(',', get_config('lightboxgallery', 'disabledplugins'));
// phpcs:disable moodle.Commenting.TodoComment
// TODO: Remove this once crop functionality is working.
$disabledplugins[] = 'crop';
$edittypes = get_list_of_plugins('mod/lightboxgallery/edit');
if ($image !== null && !$showall) {
$edittypes = array_intersect($image->get_editing_options(), $edittypes);
}
foreach ($edittypes as $edittype) {
if ($showall || !in_array($edittype, $disabledplugins)) {
$result[$edittype] = get_string('edit_' . $edittype, 'lightboxgallery');
}
}
return $result;
}
/**
* Print the tags for a gallery.
*
* @param string $heading
* @param array $tags
* @param int $courseid
* @param int $galleryid
* @return void
* @throws \core\exception\moodle_exception
* @throws coding_exception
*/
function lightboxgallery_print_tags($heading, $tags, $courseid, $galleryid) {
global $OUTPUT;
echo $OUTPUT->box_start();
echo '<form action="search.php" style="float: right; margin-left: 4px;">' .
' <fieldset class="invisiblefieldset">' .
' <input type="hidden" name="id" value="' . $courseid . '" />' .
' <input type="hidden" name="gallery" value="' . $galleryid . '" />' .
' <input type="text" name="search" size="8" />' .
' <input type="submit" class="btn btn-secondary" value="' . get_string('search') . '" />' .
' </fieldset>' .
'</form>' .
$heading . ': ';
$tagarray = [];
foreach ($tags as $tag) {
$tagparams = ['id' => $courseid, 'gallery' => $galleryid, 'search' => stripslashes($tag->description)];
$tagurl = new moodle_url('/mod/lightboxgallery/search.php', $tagparams);
$tagarray[] = html_writer::link($tagurl, s($tag->description), ['class' => 'taglink']);
}
echo implode(', ', $tagarray);
echo $OUTPUT->box_end();
}
/**
* Get the list of resize options.
*
* @return string[]
*/
function lightboxgallery_resize_options() {
return [1 => '1280x1024', 2 => '1024x768', 3 => '800x600', 4 => '640x480'];
}
/**
* Get the list of thumbnail sizes.
*
* @param int $courseid
* @param stdClass $gallery
* @param stdClass|null $newimage
* @return string
* @throws coding_exception
* @throws file_exception
* @throws stored_file_creation_exception
*/
function lightboxgallery_index_thumbnail($courseid, $gallery, $newimage = null) {
global $CFG;
require_once(dirname(__FILE__) . '/imageclass.php');
$cm = get_coursemodule_from_instance("lightboxgallery", $gallery->id, $courseid);
$context = context_module::instance($cm->id);
$imageid = 'Gallery Index Image';
$fs = get_file_storage();
$storedfile = $fs->get_file($context->id, 'mod_lightboxgallery', 'gallery_index', '0', '/', 'index.png');
if (!is_null($newimage) && is_object($storedfile)) { // Delete any existing index.
$storedfile->delete();
}
if (is_object($storedfile) && is_null($newimage)) {
// Grab the index.
$index = $storedfile;
} else if (!is_null($newimage) || $files = $fs->get_area_files($context->id, 'mod_lightboxgallery', 'gallery_images')) {
// Get first image and create an index for that.
if (is_null($newimage)) {
$file = array_shift($files);
while (substr($file->get_mimetype(), 0, 6) != 'image/') {
$file = array_shift($files);
}
$image = new lightboxgallery_image($file, $gallery, $cm);
} else {
$image = $newimage;
}
$index = $image->create_index();
} else {
$fileinfo = [
'contextid' => $context->id,
'component' => 'mod_lightboxgallery',
'filearea' => 'gallery_index',
'itemid' => 0,
'filepath' => '/',
'filename' => 'index.png',
];
$index = $fs->create_file_from_pathname($fileinfo, $CFG->dirroot . '/mod/lightboxgallery/pix/index.png');
}
$path = moodle_url::make_pluginfile_url(
$context->id,
'mod_lightboxgallery',
'gallery_index',
$index->get_itemid(),
$index->get_filepath(),
$index->get_filename(),
);
$path->param('mtime', $index->get_timemodified());
return '<img src="' . $path . '" alt="" ' . (! empty($imageid) ? 'id="' . $imageid . '"' : '' ) . ' />';
}
/**
* File browsing support class
*/
class lightboxgallery_content_file_info extends file_info_stored {
/**
* Get the parent file.
*
* @return file_info|null
*/
public function get_parent() {
if ($this->lf->get_filepath() === '/' && $this->lf->get_filename() === '.') {
return $this->browser->get_file_info($this->context);
}
return parent::get_parent();
}
/**
* Get the name of the file.
*
* @return string
*/
public function get_visible_name() {
if ($this->lf->get_filepath() === '/' && $this->lf->get_filename() === '.') {
return $this->topvisiblename;
}
return parent::get_visible_name();
}
}