This repository has been archived by the owner on Jul 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
locallib.php
299 lines (257 loc) · 12.7 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
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
294
295
296
297
298
299
<?php
defined('MOODLE_INTERNAL') || die();
require_once("$CFG->libdir/filelib.php");
define('GALLERY_IMAGES_FILEAREA','gallery_images');
define('GALLERY_IMAGE_THUMBS_FILEAREA','gallery_thumbs');
define('GALLERY_IMAGE_PREVIEWS_FILEAREA','gallery_previews');
define('GALLERY_IMAGE_DRAFTS_FILEAREA','gallery_drafts');
define('GALLERY_IMAGE_ATTACHMENTS_FILEAREA','gallery_attachments');
function gallery_get_completion_status_addimages(gallery $gallery, $userid = 0) {
global $USER, $DB;
if($userid == 0)
$userid = $USER->id;
// If completion option is enabled, evaluate it and return true/false
if($gallery->completionaddimages() > 0)
if($DB->count_records('gallery_images', array('gallery'=>$gallery->id(),'user'=>$userid)) >= $gallery->completionaddimages())
return true;
return false;
}
function gallery_get_completion_status_addcomments(gallery $gallery, $context, $userid = 0) {
global $USER, $DB;
if($userid == 0)
$userid = $USER->id;
if($gallery->completionaddcomments() > 0)
if($DB->count_records('comments', array('userid'=>$userid,'contextid'=>$context->id)) >= $gallery->completionaddcomments())
return true;
return false;
}
function gallery_process_completion(gallery $gallery, $context, $course, $cm, $userid = 0) {
global $CFG;
require_once($CFG->dirroot."/lib/completionlib.php");
$completedImages = gallery_get_completion_status_addimages($gallery, $userid);
$completedComments = gallery_get_completion_status_addcomments($gallery, $context, $userid);
$completion=new completion_info($course);
if($completion->is_enabled($cm)) {
if(($gallery->completionaddimages() > 0) && ($gallery->completionaddcomments() > 0) && $completedImages && $completedComments)
$completion->update_state($cm,COMPLETION_COMPLETE);
else
$completion->update_state($cm,COMPLETION_INCOMPLETE);
if(($gallery->completionaddimages() > 0) && ($gallery->completionaddcomments() == 0) && $completedImages)
$completion->update_state($cm,COMPLETION_COMPLETE);
else
$completion->update_state($cm,COMPLETION_INCOMPLETE);
if(($gallery->completionaddimages() == 0) && ($gallery->completionaddcomments() > 0) && $completedComments)
$completion->update_state($cm,COMPLETION_COMPLETE);
else
$completion->update_state($cm,COMPLETION_INCOMPLETE);
}
}
function gallery_process_editing($edit, $context) {
global $USER;
if (has_capability('mod/gallery:manage', $context) ||
has_capability('mod/gallery:editallimages', $context) ||
has_capability('mod/gallery:editownimages', $context) ||
has_capability('mod/gallery:deleteallimages', $context) ||
has_capability('mod/gallery:deleteownimages', $context) ||
has_capability('mod/gallery:addimages', $context)) {
if ($edit != -1 and confirm_sesskey())
$USER->editing = $edit;
} else
$USER->editing = 0;
if(!isset($USER->editing))
$USER->editing = 0;
}
function gallery_process_drafts($context, $gallery) {
global $CFG, $USER, $PAGE;
require_once($CFG->dirroot.'/mod/gallery/image.class.php');
$fs = get_file_storage();
$fs->delete_area_files($context->id, 'mod_gallery', GALLERY_IMAGE_DRAFTS_FILEAREA, $gallery->id());
$fs->delete_area_files($context->id, 'mod_gallery', 'unpacktemp');
$draftid = file_get_submitted_draft_itemid('images');
if (!$files = $fs->get_area_files(
get_context_instance(CONTEXT_USER, $USER->id)->id, 'user', 'draft', $draftid, 'filename ASC', false)) {
redirect($PAGE->url);
}
$preloaded_images = array();
$i = 1;
foreach($files as $file) {
if(!$file->is_valid_image()) {
$packer = get_file_packer($file->get_mimetype());
if($packer) {
$file->extract_to_storage($packer, $context->id, 'mod_gallery', 'unpacktemp', $i, '/');
$unpackedFiles = $fs->get_area_files($context->id, 'mod_gallery', 'unpacktemp', $i,'filename ASC');
$preloaded_images = array_merge($preloaded_images, $unpackedFiles);
$file->delete();
$i++;
}
}
$preloaded_images[] = $file;
}
$images = array();
foreach ($preloaded_images as $file) {
$data = gallery_image::get_initial_data();
$data->name = $file->get_filename();
if ($file->is_valid_image()) {
$fileinfo = array(
'contextid' => $context->id,
'component' => 'mod_gallery',
'filearea' => GALLERY_IMAGE_DRAFTS_FILEAREA,
'itemid' => $gallery->id(),
'filepath' => '/',
'filename' => $file->get_filename()
);
if (!$fs->get_file($context->id, 'mod_gallery', GALLERY_IMAGE_DRAFTS_FILEAREA, $gallery->id(), '/', $file->get_filename())) {
$file = $fs->create_file_from_storedfile($fileinfo, $file);
$images[] = new gallery_image($data,$file,null,false);
}
}
}
$fs->delete_area_files($context->id, 'mod_gallery', 'unpacktemp');
return $images;
}
function gallery_get_draft_images($context, $gallery) {
global $PAGE, $CFG;
$fs = get_file_storage();
if (!$files = $fs->get_area_files(
$context->id, 'mod_gallery', GALLERY_IMAGE_DRAFTS_FILEAREA, $gallery->id(), 'filename ASC', false)) {
redirect($PAGE->url);
}
require_once($CFG->dirroot.'/mod/gallery/image.class.php');
$images = array();
foreach($files as $file) {
$data = gallery_image::get_initial_data();
$data->name = $file->get_filename();
$images[] = new gallery_image($data,$file,null,false);
}
return $images;
}
function gallery_process_image_drats_save($data, $context, $gallery, $files) {
global $CFG;
require_once($CFG->dirroot.'/mod/gallery/image.class.php');
require_once($CFG->dirroot.'/mod/gallery/imagemanager.class.php');
$fs = get_file_storage();
foreach ($files as $file) {
$uId = clean_param($file->stored_file()->get_filename(), PARAM_ALPHANUM);
$imgData = gallery_image::from_form_data($uId, $data);
$imgData->gallery = $gallery->id();
$imgData->type = strtolower(pathinfo($file->stored_file()->get_filename(), PATHINFO_EXTENSION));
$image_data = gallery_imagemanager::create_image($imgData);
$filename = $image_data->id.'.'. strtolower(pathinfo($file->stored_file()->get_filename(), PATHINFO_EXTENSION));
$fileinfo = array(
'contextid' => $context->id,
'component' => 'mod_gallery',
'filearea' => GALLERY_IMAGES_FILEAREA,
'itemid' => $gallery->id(),
'filepath' => '/',
'filename' => $filename
);
if (!$fs->get_file($context->id, 'mod_gallery', GALLERY_IMAGES_FILEAREA, $gallery->id(), '/', $filename)) {
$file = $fs->create_file_from_storedfile($fileinfo, $file->stored_file());
$image = new gallery_image($image_data, $file, $context, true, false, $gallery->previewheight());
if($gallery->imageattachments()) {
$attachmentsStr = 'attachments-'.$uId;
file_save_draft_area_files($data->$attachmentsStr, $context->id, 'mod_gallery', GALLERY_IMAGE_ATTACHMENTS_FILEAREA, $image->id(), array('subdirs' => 0));
}
}
}
$fs->delete_area_files($context->id, 'mod_gallery', GALLERY_IMAGE_DRAFTS_FILEAREA, $gallery->id());
}
function gallery_process_images_save($data, $images,$context,$gallery) {
global $CFG;
require_once($CFG->dirroot.'/mod/gallery/imagemanager.class.php');
require_once($CFG->dirroot.'/mod/gallery/image.class.php');
foreach($images as $image) {
$imgData = $image->from_form($data);
if($image->data()->name != $imgData->name ||
$image->data()->description != $imgData->description ||
$image->data()->descriptionformat != $imgData->descriptionformat ||
$image->data()->sourcetype != $imgData->sourcetype ||
($image->data()->sourcetype == GALLERY_IMAGE_SOURCE_TEXT && $image->data()->sourcetext != $imgData->sourcetext)) {
gallery_imagemanager::update_image($imgData);
}
if($gallery->imageattachments()) {
$attachmentsStr = 'attachments-'.$image->id();
file_save_draft_area_files($data->$attachmentsStr, $context->id, 'mod_gallery', GALLERY_IMAGE_ATTACHMENTS_FILEAREA, $image->id(), array('subdirs' => 0));
}
}
}
function gallery_load_images($gallery, $context, $iid = false) {
global $CFG;
require_once($CFG->dirroot.'/mod/gallery/image.class.php');
require_once($CFG->dirroot.'/mod/gallery/imagemanager.class.php');
$images_db = gallery_imagemanager::get_images($gallery);
$images = array();
$fs = get_file_storage();
foreach($images_db as $idb) {
if($iid && $idb->id == $iid) {
$images[$idb->id] = new gallery_image($idb, $fs->get_file($context->id, 'mod_gallery', GALLERY_IMAGES_FILEAREA, $gallery->id(), '/',
$idb->id.'.'.$idb->type),$context,true,true,$gallery->previewheight());
} else {
$images[$idb->id] = new gallery_image($idb, $fs->get_file($context->id, 'mod_gallery', GALLERY_IMAGES_FILEAREA, $gallery->id(), '/',
$idb->id.'.'.$idb->type),$context,true,false,$gallery->previewheight());
}
}
return $images;
}
function gallery_load_image($context,$image_db, $previewheight) {
global $CFG;
require_once($CFG->dirroot.'/mod/gallery/image.class.php');
require_once($CFG->dirroot.'/mod/gallery/imagemanager.class.php');
$fs = get_file_storage();
return new gallery_image($image_db, $fs->get_file($context->id, 'mod_gallery', GALLERY_IMAGES_FILEAREA, $image_db->gallery, '/',
$image_db->id.'.'.$image_db->type),$context,true,true,$previewheight);
}
function gallery_process_rotate_image($direction,$image) {
$angle = 90;
if($direction == 'right')
$angle = 270;
$image->rotate($angle);
}
function gallery_process_delete_image($img, $context, $gallery) {
global $CFG;
require_once($CFG->dirroot.'/mod/gallery/image.class.php');
require_once($CFG->dirroot.'/mod/gallery/imagemanager.class.php');
require_once($CFG->dirroot.'/comment/lib.php');
$fs = get_file_storage();
$img->delete();
$fs->delete_area_files($context->id, 'mod_gallery', GALLERY_IMAGE_ATTACHMENTS_FILEAREA, $gallery->id());
gallery_imagemanager::delete_image($img->id());
comment::delete_comments(array('contextid'=>$context->id,'commentarea'=>'gallery_image_comments','itemid'=>$img->id()));
}
function gallery_get_packed_images($images, $gallery, $context) {
global $USER, $DB;
$packer = get_file_packer('application/zip');
$fs = get_file_storage();
$fs->delete_area_files($context->id,'mod_gallery','gallery_packed_images');
$preparedFiles = array();
$users = array();
foreach($images as $img) {
if(!isset($users[$img->data()->user]))
$users[$img->data()->user] = $DB->get_record('user',array('id'=>$img->data()->user));
$preparedFiles[fullname($users[$img->data()->user]).' - img'.$img->stored_file()->get_filename()] = $img->stored_file();
}
return $packer->archive_to_storage($preparedFiles, $context->id, 'mod_gallery', 'gallery_packed_images', $gallery->id(), '/', $gallery->id().'-'.$gallery->name().'.zip', $USER->id);
}
function gallery_load_batch_images($gallery, $context) {
$images = gallery_load_images($gallery, $context);
$batchImages = array();
foreach($images as $image) {
if(isset($_POST['mod-gallery-batch-'.$image->id()]))
$batchImages[$image->id()] = $image;
}
return $batchImages;
}
class gallery_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();
}
}