forked from johnkelsh/moodle-mod_lightboxgallery
-
Notifications
You must be signed in to change notification settings - Fork 37
/
locallib.php
230 lines (194 loc) · 8.01 KB
/
locallib.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
<?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 $gallery
* @param int $resize
* @access public
* @return void
*/
function lightboxgallery_add_images($files, $context, $cm, $gallery, $resize = 0) {
require_once(dirname(__FILE__).'/imageclass.php');
$fs = get_file_storage();
$images = array();
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 = array(
'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();
list($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);
}
function lightboxgallery_config_defaults() {
$defaults = array(
'disabledplugins' => '',
'enablerssfeeds' => 0,
);
$localcfg = get_config('lightboxgallery');
foreach ($defaults as $name => $value) {
if (! isset($localcfg->$name)) {
set_config($name, $value, 'lightboxgallery');
}
}
}
function lightboxgallery_edit_types($showall = false) {
$result = array();
$disabledplugins = explode(',', get_config('lightboxgallery', 'disabledplugins'));
// TODO: Remove this once crop functionality is working.
$disabledplugins[] = 'crop';
$edittypes = get_list_of_plugins('mod/lightboxgallery/edit');
foreach ($edittypes as $edittype) {
if ($showall || !in_array($edittype, $disabledplugins)) {
$result[$edittype] = get_string('edit_' . $edittype, 'lightboxgallery');
}
}
return $result;
}
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" value="'.get_string('search').'" />'.
' </fieldset>'.
'</form>'.
$heading.': ';
$tagarray = array();
foreach ($tags as $tag) {
$tagparams = array('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), array('class' => 'taglink'));
}
echo implode(', ', $tagarray);
echo $OUTPUT->box_end();
}
function lightboxgallery_resize_options() {
return array(1 => '1280x1024', 2 => '1024x768', 3 => '800x600', 4 => '640x480');
}
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) or $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 = $CFG->wwwroot.'/pluginfile.php/'.$context->id.'/mod_lightboxgallery/gallery_index/'.
$index->get_itemid().$index->get_filepath().$index->get_filename();
return '<img src="' . $path . '" alt="" ' . (! empty($imageid) ? 'id="' . $imageid . '"' : '' ) . ' />';
}
/**
* File browsing support class
*/
class lightboxgallery_content_file_info extends file_info_stored {
public function get_parent() {
if ($this->lf->get_filepath() === '/' and $this->lf->get_filename() === '.') {
return $this->browser->get_file_info($this->context);
}
return parent::get_parent();
}
public function get_visible_name() {
if ($this->lf->get_filepath() === '/' and $this->lf->get_filename() === '.') {
return $this->topvisiblename;
}
return parent::get_visible_name();
}
}