/
lib.php
498 lines (428 loc) · 17.4 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
<?php
/**
* Mahara: Electronic portfolio, weblog, resume builder and social networking
* Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
* http://wiki.mahara.org/Contributors
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package mahara
* @subpackage blocktype-groupviewsimage
* @author Shen Zhang / Jawyei Wong, AUT University, Code adapted from artefact-browse plugin by Mike Kelly UAL m.f.kelly@arts.ac.uk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
*
*/
defined('INTERNAL') || die();
require_once('group.php');
require_once('view.php'); //20140626 JW might be needed for pagniation
class PluginBlocktypeGroupViewsImage extends SystemBlocktype {
public static function get_title() {
return get_string('title', 'blocktype.groupviewsimage');
}
public static function get_description() {
return get_string('description', 'blocktype.groupviewsimage');
}
public static function single_only() {
return false;
}
public static function get_categories() {
return array('general');
}
public static function get_viewtypes() {
return array('grouphomepage');
}
public static function hide_title_on_empty_content() {
return true;
}
public static function has_instance_config() {
return true;
}
public static function get_instance_title() {
return get_string('title', 'blocktype.groupviewsimage');
}
public static function instance_config_form($instance) {
$configdata = $instance->get('configdata');
return array(
'showsharedviews' => array(
'type' => 'radio',
'title' => get_string('displaysharedviews', 'blocktype.groupviewsimage'),
'description' => get_string('displaysharedviewsdesc', 'blocktype.groupviewsimage'),
'options' => array(
1 => get_string('yes'),
0 => get_string('no'),
),
'separator' => '<br>',
'defaultvalue' => isset($configdata['showsharedviews']) ? $configdata['showsharedviews'] : 1,
),
);
}
public static function default_copy_type() {
return 'shallow';
}
public static function render_instance(BlockInstance $instance, $editing=false) {
//Default limit and offset. This was copied from Mike Kelly's code
$offset = 0;
$limit = 10;
$groupid = $instance->get_view()->get('group');
if (!$groupid) {
return '';
}
//returns an array of all views for a group id. This uses the groupviews method that was copied acrossed
$items = self::get_data($groupid, $offset, $limit);
//calls Mike Kelly's method to build the objects to be displayed
self::build_browse_list_html($items);
if(empty($items) || !isset($items)){
//20150723 JW changed the return message so if there are no pages to display it says that rather you don't have access
return "No pages to display";
}
//Not sure what this does
$js = <<< EOF
addLoadEvent(function () {
{$items['pagination_js']}
});
EOF;
//neededJS and neededCSS are files required to render the page properly. They are done this way so it is easier to read
$neededJS = array(
get_config('wwwroot').'blocktype/groupviewsimage/js/chosen.jquery.js',
get_config('wwwroot').'blocktype/groupviewsimage/js/browse.js'
);
$neededCSS = array(
get_config('wwwroot')."blocktype/groupviewsimage/js/jquery-ui/css/custom-theme/jquery-ui-1.8.20.custom.css",
get_config('wwwroot')."blocktype/groupviewsimage/theme/raw/static/style/chosen.css",
get_config('wwwroot')."blocktype/groupviewsimage/theme/raw/static/style/style.css"
);
//calls smarty and feeds the data to be render via the tpl file
$dwoo = smarty_core();
//adds the javascript and css files into the template to be rendered
$dwoo->assign('JAVASCRIPT', $neededJS);
$dwoo->assign('STYLESHEETLIST', $neededCSS);
$dwoo->assign('INLINEJAVASCRIPT', $js); //????
//20150723 JW added extra entry to check if jquery is loaded, if not then load the below jquery
$dwoo->assign('JQUERYPATH', get_config('wwwroot').'blocktype/groupviewsimage/js/jquery-ui/js/jquery-ui-1.8.19.custom.min.js');
//adds the items into the templated to be rendered
$dwoo->assign_by_ref('items', $items);
//returns the template as text so mahara can render the block
return $dwoo->fetch('blocktype:groupviewsimage:index.tpl');
}
/** Code copied from blocktype::groupviews **/
public static function get_data($groupid, $offset=0, $limit=20) {
global $USER;
$texttitletrim = 20;
$items2 = null;
if(!defined('GROUP')) {
define('GROUP', $groupid);
}
// get the currently requested group
$group = group_current_group();
$role = group_user_access($group->id);
if ($role) {
// For group members, display a list of views that others have shared to the group
// Params for get_sharedviews_data is $limit=0,$offset=0, $groupid
$data['sharedviews'] = View::get_sharedviews_data($limit, $offset, $group->id);
/*
foreach ($data['sharedviews']->data as &$view) {
if (isset($view['template']) && $view['template']) {
$view['form'] = pieform(create_view_form($group, null, $view->id));
}
}
*/
//the array that is returned from View::get_sharedviews_data($limit, $offset, $group->id)
//contains a few other things but we just wanted to focus on sharedviews
//the foreach below will loop through all views shared with the group
foreach($data['sharedviews'] as $aView){
if(is_array($aView)){
//20140909 JW sort the array returned by View::get_sharedviews_data($limit, $offset, $group->id)
//by ctime, this requires the query within get_sharedviews_data to have ctime in its select string
$aView = self::array_msort($aView, array('ctime'=>SORT_DESC));
//the foreach below will loop through all the properties for a view (returned by get_data method) and assigns them to the required variables
foreach($aView as $aViewProperty){
//get the view
$viewID = $aViewProperty['id']; //the page shared
$fullurl = $aViewProperty['fullurl']; //full url of the page shared
$viewTitle = str_shorten_text($aViewProperty['displaytitle'], $texttitletrim, true); //view's title
//get the owner of the view
$viewOwnerName = $aViewProperty['user']->firstname." ".$aViewProperty['user']->lastname; //owner of the view's name
$userobj = new User();
$userobj->find_by_id($aViewProperty['user']->id);
$profileurl = profile_url($userobj); //owner of the view's proflie page
$avatarurl = profile_icon_url($aViewProperty['user']->id,50,50); //owner of the view's profile picture
//get the artefact id of an image in the view
$theView = new View($aViewProperty['id']); //create the view
$artefactInView = $theView->get_artefact_instances(); //get all artefacts in the view 20170224 JW v16.10.2 uses get_artefact_instances()
foreach($artefactInView as $anArtefact){ //for each artefact
//20170224 JW v16.10.2 uses get_artefact_instances() which means all fields need to use the get('field_name') to get value
if($anArtefact->get('artefacttype') == 'image'){
$artefactID = $anArtefact->get('id'); //if it is an image artefact assign the id and break the loop
break;
}
//20150331 JW added that if page contains a folder with images (galleries count as folders)
//it will pull an image from that folder and use it as the cover
if($anArtefact->get('artefacttype') == 'folder'){
$query = "SELECT id FROM {artefact} where parent = ? AND artefacttype = 'image'";
$imagesInAFolder = get_records_sql_array($query,array($anArtefact->get('id')));
//only assign the id of an image if the folder contains at least 1 image
if(!empty($imagesInAFolder)){
$artefactID = $imagesInAFolder[0]->id;
break;
}
}
//20140903 JW if there are no images on the page then set to artefactID to 0
//this way, when display each page, instead of a blank box it will show a place holder image
$artefactID = 0;
}
//the items variable below requires the contents array to be in this format
$contents['photos'][] = array(
"image" => array (
"id" => $artefactID,
"view" => $viewID
),
"type" => "photo",
"page" => array(
"url" => $fullurl,
"title" => $viewTitle
),
"owner" => array(
"name" => $viewOwnerName,
"profileurl" => $profileurl,
"avatarurl" => $avatarurl
)
);
}
}
}
//20150723 JW added the if statement to check if $content is set, if not then skip
if(isset($contents)){
$items2 = array(
'count' => $data['sharedviews']->count,
'data' => $contents,
'offset' => $offset,
'limit' => $limit,
);
}
}
//$data['group'] = $group;
return $items2;
}
/** End of code copied from blocktype::groupviews **/
/**
* 20140909 JW
* array_msort copied from http://php.net/manual/en/function.array-multisort.php
* This is used to sort array returned by View::get_sharedviews_data($limit, $offset, $group->id)
*/
private static function array_msort($array, $cols){
$colarr = array();
foreach ($cols as $col => $order) {
$colarr[$col] = array();
foreach ($array as $k => $row) { $colarr[$col]['_'.$k] = strtolower($row[$col]); }
}
$eval = 'array_multisort(';
foreach ($cols as $col => $order) {
$eval .= '$colarr[\''.$col.'\'],'.$order.',';
}
$eval = substr($eval,0,-1).');';
eval($eval);
$ret = array();
foreach ($colarr as $col => $arr) {
foreach ($arr as $k => $v) {
$k = substr($k,1);
if (!isset($ret[$k])) $ret[$k] = $array[$k];
$ret[$k][$col] = $array[$k][$col];
}
}
return $ret;
}
/**
* Start of Mike Kelly's code
*/
public static function build_browse_list_html(&$items) {
//20140627 JW added if set so it does not display anything if the user is not a group member
if(isset($items)){
$smarty = smarty_core();
$smarty->assign_by_ref('items', $items);
$smarty->assign('wwwroot', get_config('wwwroot'));
$items['tablerows'] = $smarty->fetch('blocktype:groupviewsimage:browselist.tpl'); // the 'tablerows' naming is required for pagination script
$pagination = self::build_browse_pagination(array(
'id' => 'browselist_pagination',
'url' => get_config('wwwroot') . 'blocktype/groupviewsimage/lib.php',
'jsonscript' => 'blocktype/groupviewsimage/browse.json.php',
'datatable' => 'browselist', // the pagination script expects a table with this id
'count' => $items['count'],
'limit' => $items['limit'],
'offset' => $items['offset'],
'firsttext' => '',
'previoustext' => '',
'nexttext' => '',
'lasttext' => '',
'numbersincludefirstlast' => false,
'resultcounttextsingular' => 'Item', //get_string('plan', 'artefact.plans'),
'resultcounttextplural' => 'Items', //get_string('plans', 'artefact.plans'),
));
$items['pagination'] = $pagination['html'];
$items['pagination_js'] = $pagination['javascript'];
}
}
/**
* Builds pagination links for HTML display.
*
* @param array $params Options for the pagination
*/
function build_browse_pagination($params) {
// Bail if the required attributes are not present
$required = array('url', 'count', 'limit', 'offset');
foreach ($required as $option) {
if (!isset($params[$option])) {
throw new ParameterException('You must supply option "' . $option . '" to build_pagination');
}
}
// Work out default values for parameters
if (!isset($params['id'])) {
$params['id'] = substr(md5(microtime()), 0, 4);
}
$params['offsetname'] = (isset($params['offsetname'])) ? $params['offsetname'] : 'offset';
if (isset($params['forceoffset']) && !is_null($params['forceoffset'])) {
$params['offset'] = (int) $params['forceoffset'];
}
else if (!isset($params['offset'])) {
$params['offset'] = param_integer($params['offsetname'], 0);
}
// Correct for odd offsets
$params['offset'] -= $params['offset'] % $params['limit'];
$params['firsttext'] = (isset($params['firsttext'])) ? $params['firsttext'] : get_string('first');
$params['previoustext'] = (isset($params['previoustext'])) ? $params['previoustext'] : get_string('previous');
$params['nexttext'] = (isset($params['nexttext'])) ? $params['nexttext'] : get_string('next');
$params['resultcounttextsingular'] = (isset($params['resultcounttextsingular'])) ? $params['resultcounttextsingular'] : get_string('result');
$params['resultcounttextplural'] = (isset($params['resultcounttextplural'])) ? $params['resultcounttextplural'] : get_string('results');
if (!isset($params['numbersincludefirstlast'])) {
$params['numbersincludefirstlast'] = true;
}
if (!isset($params['numbersincludeprevnext'])) {
$params['numbersincludeprevnext'] = true;
}
if (!isset($params['extradata'])) {
$params['extradata'] = null;
}
// Begin building the output
$output = '<div id="' . $params['id'] . '" class="pagination';
if (isset($params['class'])) {
$output .= ' ' . hsc($params['class']);
}
$output .= '">';
//20140630 JW removed <= and added <
if ($params['limit'] < $params['count']) {
$pages = ceil($params['count'] / $params['limit']);
$page = $params['offset'] / $params['limit'];
$last = $pages - 1;
if (!empty($params['lastpage'])) {
$page = $last;
}
$next = min($last, $page + 1);
$prev = max(0, $page - 1);
// Build a list of what pagenumbers will be put between the previous/next links
$pagenumbers = array();
if ($params['numbersincludefirstlast']) {
$pagenumbers[] = 0;
}
if ($params['numbersincludeprevnext']) {
$pagenumbers[] = $prev;
}
$pagenumbers[] = $page;
if ($params['numbersincludeprevnext']) {
$pagenumbers[] = $next;
}
if ($params['numbersincludefirstlast']) {
$pagenumbers[] = $last;
}
$pagenumbers = array_unique($pagenumbers);
// Build the first/previous links
$isfirst = $page == 0;
$setlimit = true;
$output .= self::build_browse_pagination_pagelink('first',
$params['url'],
$setlimit,
$params['limit'],
0,
'« First ' . $params['firsttext'],
get_string('firstpage'),
$isfirst,
$params['offsetname']
);
$output .= self::build_browse_pagination_pagelink('prev',
$params['url'],
$setlimit,
$params['limit'],
$params['limit'] * $prev,
'← Previous ' . $params['previoustext'],
get_string('prevpage'),
$isfirst,
$params['offsetname']
);
// Build the pagenumbers in the middle
foreach ($pagenumbers as $k => $i) {
if ($k != 0 && $prevpagenum < $i - 1) {
$output .= 'É';
}
if ($i == $page) {
$output .= '<span class="selected">' . ($i + 1) . '</span>';
}
else {
$output .= self::build_browse_pagination_pagelink('',
$params['url'],
$setlimit,
$params['limit'],
$params['limit'] * $i,
$i + 1,
'',
false,
$params['offsetname']
);
}
$prevpagenum = $i;
}
// Build the next/last links
$islast = $page == $last;
$output .= self::build_browse_pagination_pagelink('next',
$params['url'],
$setlimit,
$params['limit'],
$params['limit'] * $next,
$params['nexttext'] . ' Next →',
get_string('nextpage'),
$islast, $params['offsetname']
);
}
$js = '';
// Close the container div
$output .= '</div>';
return array('html' => $output, 'javascript' => $js);
}
function build_browse_pagination_pagelink($class, $url, $setlimit, $limit, $offset, $text, $title, $disabled=false, $offsetname='offset') {
$url = "javascript:Browse.filtercontent('recentwork'," . $limit . "," . $offset . ");";
//$url = "javascript:groupviewsimage.filtercontent('recentwork'," . $limit . "," . $offset . ");";
if ($disabled) {
//20140627 JW display nothing if the button is disabled
//$return = '<span class="pagination';
//$return .= ($class) ? " $class" : '';
//$return .= ' disabled">' . $text . '</span>';
}
else {
$return = '<span class="pagination';
$return .= ($class) ? " $class" : '';
$return .= '">'
. '<a href="' . $url . '" title="' . $title
. '">' . $text . '</a></span>';
}
return $return;
}
/** End of Mike Kelly's code **/
}