forked from open-lms-open-source/moodle-mod_lightboxgallery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
locallib.php
244 lines (200 loc) · 8.91 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?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("$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);
function lightboxgallery_add_images($stored_file, $context, $cm, $gallery, $resize = 0) {
require_once(dirname(__FILE__).'/imageclass.php');
$fs = get_file_storage();
$images = array();
if ($stored_file->get_mimetype() == 'application/zip') {
// Unpack.
$packer = get_file_packer('application/zip');
$fs->delete_area_files($context->id, 'mod_lightboxgallery', 'unpacktemp', 0);
$stored_file->extract_to_storage($packer, $context->id, 'mod_lightboxgallery', 'unpacktemp', 0, '/');
$images = $fs->get_area_files($context->id, 'mod_lightboxgallery', 'unpacktemp', 0);
$stored_file->delete();
} else {
$images[] = $stored_file;
}
foreach ($images as $stored_file) {
if ($stored_file->is_valid_image()) {
$filename = $stored_file->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)) {
$stored_file = $fs->create_file_from_storedfile($fileinfo, $stored_file);
$image = new lightboxgallery_image($stored_file, $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) {
global $CFG;
$result = array();
$disabledplugins = explode(',', get_config('lightboxgallery', 'disabledplugins'));
$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_comment($comment, $context) {
global $DB, $CFG, $COURSE, $OUTPUT;
$user = $DB->get_record('user', array('id' => $comment->userid));
$deleteurl = new moodle_url('/mod/lightboxgallery/comment.php', array('id' => $comment->gallery, 'delete' => $comment->id));
echo '<table cellspacing="0" width="50%" class="boxaligncenter datacomment forumpost">'.
'<tr class="header"><td class="picture left">'.$OUTPUT->user_picture($user, array('courseid' => $COURSE->id)).'</td>'.
'<td class="topic starter" align="left"><a name="c'.$comment->id.'"></a><div class="author">'.
'<a href="'.$CFG->wwwroot.'/user/view.php?id='.$user->id.'&course='.$COURSE->id.'">'.
fullname($user, has_capability('moodle/site:viewfullnames', $context)).'</a> - '.userdate($comment->timemodified).
'</div></td></tr>'.
'<tr><td class="left side">'.
// TODO: user_group picture?
'</td><td class="content" align="left">'.
format_text($comment->comment, FORMAT_MOODLE).
'<div class="commands">'.
(has_capability('mod/lightboxgallery:edit', $context) ? html_writer::link($deleteurl, get_string('delete')) : '').
'</div>'.
'</td></tr></table>';
}
function lightboxgallery_print_tags($heading, $tags, $courseid, $galleryid) {
global $CFG, $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="l" 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' => urlencode(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_label($label) {
return lightboxgallery_resize_text($label, MAX_IMAGE_LABEL);
}
function lightboxgallery_resize_options() {
return array(1 => '1280x1024', 2 => '1024x768', 3 => '800x600', 4 => '640x480');
}
function lightboxgallery_resize_text($text, $length) {
$textlib = new textlib();
return ($textlib->strlen($text) > $length ? $textlib->substr($text, 0, $length) . '...' : $text);
}
function lightboxgallery_rss_enabled() {
global $CFG;
return ($CFG->enablerssfeeds && get_config('lightboxgallery', 'enablerssfeeds'));
}
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 = get_context_instance(CONTEXT_MODULE, $cm->id);
$imageid = 'Gallery Index Image';
$fs = get_file_storage();
$stored_file = $fs->get_file($context->id, 'mod_lightboxgallery', 'gallery_index', '0', '/', 'index.png');
if (!is_null($newimage) && is_object($stored_file)) { // Delete any existing index.
$stored_file->delete();
}
if (is_object($stored_file) && is_null($newimage)) {
// Grab the index.
$index = $stored_file;
} else {
// Get first image and create an index for that.
if (is_null($newimage)) {
$files = $fs->get_area_files($context->id, 'mod_lightboxgallery', 'gallery_images');
$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();
}
$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();
}
}