forked from jed15/socialwiki
-
Notifications
You must be signed in to change notification settings - Fork 3
/
locallib.php
executable file
·1744 lines (1577 loc) · 53.5 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
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?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/>.
/**
* This contains functions and classes that will be used by scripts in wiki module
*
* @package mod_socialwiki
* @copyright 2009 Marc Alier, Jordi Piguillem marc.alier@upc.edu
* @copyright 2009 Universitat Politecnica de Catalunya http://www.upc.edu
*
* @author Jordi Piguillem
* @author Marc Alier
* @author David Jimenez
* @author Josep Arus
* @author Daniel Serrano
* @author Kenneth Riba
*
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->dirroot . '/mod/socialwiki/lib.php');
require_once($CFG->dirroot . '/mod/socialwiki/parser/parser.php');
require_once($CFG->dirroot . '/tag/lib.php');
define('SOCIALFORMAT_CREOLE', '37');
define('SOCIALFORMAT_NWIKI', '38');
/**
* Get a wiki instance.
*
* @param int $wid The wiki ID.
* @return stdClass
*/
function socialwiki_get_wiki($wid) {
global $DB;
return $DB->get_record('socialwiki', array('id' => $wid));
}
/**
* Get subwiki instances with same wiki ID.
*
* @param int $wid The wiki ID.
* @return stdClass
*/
function socialwiki_get_subwikis($wid) {
global $DB;
return $DB->get_records('socialwiki_subwikis', array('wikiid' => $wid));
}
/**
* Get a subwiki instance by wiki ID and group ID.
*
* @param int $wid The wiki ID.
* @param int $gid The group ID.
* @param int $uid The user ID.
* @return stdClass
*/
function socialwiki_get_subwiki_by_group($wid, $gid, $uid = 0) {
global $DB;
return $DB->get_record('socialwiki_subwikis', array('wikiid' => $wid, 'groupid' => $gid, 'userid' => $uid));
}
/**
* Get a subwiki by ID.
*
* @param int $swid The subwiki ID.
* @return stdClass
*/
function socialwiki_get_subwiki($swid) {
global $DB;
return $DB->get_record('socialwiki_subwikis', array('id' => $swid));
}
/**
* Add a new sub wiki instance.
*
* @param int $wid The wiki ID.
* @param int $gid The group ID.
* @param int $uid The first user ID.
* @return int
*/
function socialwiki_add_subwiki($wid, $gid, $uid = 0) {
global $DB;
$record = new stdClass();
$record->wikiid = $wid;
$record->groupid = $gid;
$record->userid = $uid;
$insertid = $DB->insert_record('socialwiki_subwikis', $record);
return $insertid;
}
/**
* Get a wiki instance by page ID.
*
* @param int $pid The page ID.
* @return stdClass
*/
function socialwiki_get_wiki_from_pageid($pid) {
global $DB;
$sql = "SELECT w.* FROM {socialwiki} w, {socialwiki_subwikis} s, {socialwiki_pages} p
WHERE p.id = ? AND p.subwikiid = s.id AND s.wikiid = w.id";
return $DB->get_record_sql($sql, array($pid));
}
/**
* Get a wiki page by page ID.
*
* @param int $pid The page ID.
* @return stdClass
*/
function socialwiki_get_page($pid) {
global $DB;
return $DB->get_record('socialwiki_pages', array('id' => $pid));
}
/**
* Get all pages for a user.
*
* @param int $uid The user ID.
* @param int $swid The subwiki ID.
* @return stdClass[]
*/
function socialwiki_get_pages_from_userid($uid, $swid) {
Global $DB;
$select = 'userid=? And subwikiid=?';
return $DB->get_records_select('socialwiki_pages', $select, array($uid, $swid));
}
/**
* Get page section.
*
* @param stdClass $page
* @param string $section
*/
function socialwiki_get_section_page($page, $section) {
return socialwiki_parser_proxy::get_section($page->content, $page->format, $section);
}
/**
* Get a wiki page by page title.
*
* @param int $swid The subwiki ID.
* @param string $title The page title.
* @return stdClass
*/
function socialwiki_get_page_by_title($swid, $title) {
global $DB, $USER;
$records = $DB->get_records('socialwiki_pages', array('subwikiid' => $swid, 'title' => $title));
if (count($records) > 0) {
foreach ($records as $r) {
if (socialwiki_is_user_favourite($USER->id, $r->id, $swid)) {
return $r;
}
}
// The user has no fave.
return $records[max(array_keys($records))];
} else {
return $records;
}
}
/**
* Save a page section.
*
* @param stdClass $page The page that is modified.
* @param string $sectiontitle The title of the section.
* @param string $sectioncontent The content in the section.
* @param int $uid The ID of the user.
* @return bool|array
*/
function socialwiki_save_section($page, $sectiontitle, $sectioncontent, $uid) {
$wiki = socialwiki_get_wiki_from_pageid($page->id);
$cm = get_coursemodule_from_instance('socialwiki', $wiki->id);
$context = context_module::instance($cm->id);
if (has_capability('mod/socialwiki:editpage', $context)) {
$content = socialwiki_parser_proxy::get_section($page->content, $page->format, $sectiontitle, true);
$newcontent = $content[0] . $sectioncontent . $content[2];
return socialwiki_save_page($page, $newcontent, $uid);
} else {
return false;
}
}
/**
* Save page content.
*
* @param stdClass $page The page to modify.
* @param string $newcontent The content in the page.
* @param int $uid The ID of the user.
* @return bool|array
*/
function socialwiki_save_page($page, $newcontent, $uid) {
global $DB;
$wiki = socialwiki_get_wiki_from_pageid($page->id);
$cm = get_coursemodule_from_instance('socialwiki', $wiki->id);
$context = context_module::instance($cm->id);
if (has_capability('mod/socialwiki:editpage', $context)) {
$page->userid = $uid;
$page->content = $newcontent;
$DB->update_record('socialwiki_pages', $page);
$options = array('swid' => $page->subwikiid, 'pageid' => $page->id, 'navi' => 0);
$parseroutput = socialwiki_parse_content($page->format, $newcontent, $options);
return array('page' => $page, 'sections' => $parseroutput['repeated_sections']);
} else {
return false;
}
}
/**
* Restore a page.
*
* @param stdClass $page The page to modify.
* @param string $newcontent The content in the page.
* @param int $uid The ID of the user.
* @return stdClass
*/
function socialwiki_restore_page($page, $newcontent, $uid) {
$return = socialwiki_save_page($page, $newcontent, $uid);
return $return['page'];
}
/**
* Create a new wiki page, if the page exists, return existing pageid.
*
* @param int $swid The subwiki ID.
* @param string $title The page title.
* @param string $format The format type.
* @param int $uid The user ID.
* @param int $parent The parent page ID.
* @return int
*/
function socialwiki_create_page($swid, $title, $format, $uid, $parent = null) {
global $DB;
$subwiki = socialwiki_get_subwiki($swid);
$cm = get_coursemodule_from_instance('socialwiki', $subwiki->wikiid);
$context = context_module::instance($cm->id);
require_capability('mod/socialwiki:editpage', $context);
// Creating a new empty page.
$page = new stdClass();
$page->subwikiid = $swid;
$page->title = $title;
$page->content = "";
$page->timecreated = time();
$page->format = $format;
$page->userid = $uid;
$page->pageviews = 0;
$page->parent = $parent;
$pid = $DB->insert_record('socialwiki_pages', $page);
return $pid;
}
/**
* Get pages list in wiki
* @param int $swid subwiki ID.
* @param bool $filter0likes Whether to skip the pages without likes.
* @return stdClass[]
*/
function socialwiki_get_page_list($swid, $filter0likes = false) {
global $DB;
if ($filter0likes) {
$sql = "SELECT DISTINCT p.* FROM {socialwiki_pages}
AS p INNER JOIN {socialwiki_likes}
AS l ON p.id=l.pageid WHERE p.subwikiid=?";
$records = $DB->get_records_sql($sql, array("subwikiid" => $swid));
return $records;
} else {
$records = $DB->get_records('socialwiki_pages', array('subwikiid' => $swid), 'title ASC');
return $records;
}
}
/**
* Get the list of topics.
*
* @param int $swid The subwiki ID.
* @return stdClass[]
*/
function socialwiki_get_topics($swid) {
$records = socialwiki_get_page_list($swid);
$pages = array();
foreach ($records as $r) {
if (!array_key_exists($r->title, $pages)) {
$pages[$r->title] = array();
$pages[$r->title]["Views"] = 0;
$pages[$r->title]["Likes"] = 0;
$pages[$r->title]["Versions"] = 0;
}
$pages[$r->title]["Views"] += intval($r->pageviews);
$pages[$r->title]["Likes"] += intval(socialwiki_numlikes($r->id));
$pages[$r->title]["Versions"] ++;
}
return $pages;
}
/**
* Get a list of the user's pages.
*
* @param int $uid The user ID.
* @param int $swid The subwiki ID.
* @return stdClass[]
*/
function socialwiki_get_user_page_list($uid, $swid) {
global $DB;
$records = $DB->get_records('socialwiki_pages', array('subwikiid' => $swid, 'userid' => $uid), 'title ASC');
return $records;
}
/**
* Get a list of the user's topics.
*
* @param int $uid The user ID.
* @param int $swid The subwiki ID.
* @return stdClass[]
*/
function socialwiki_get_user_topics($uid, $swid) {
$records = socialwiki_get_user_page_list($uid, $swid);
$pages = array();
foreach ($records as $r) {
if (!array_key_exists($r->title, $pages)) {
$pages[$r->title] = array();
$pages[$r->title]["Views"] = 0;
$pages[$r->title]["Likes"] = 0;
$pages[$r->title]["Versions"] = 0;
}
$pages[$r->title]["Views"] += intval($r->pageviews);
$pages[$r->title]["Likes"] += intval(socialwiki_numlikes($r->id));
$pages[$r->title]["Versions"] ++;
}
return $pages;
}
/**
* Gets all related pages to the title.
*
* @param int $swid The subwiki ID.
* @param string $title The title to search for.
* @return stdClass[]
*/
function socialwiki_get_related_pages($swid, $title) {
global $DB;
$sql = "SELECT p.id, p.title FROM {socialwiki_pages} p WHERE p.subwikiid = ? AND p.title = ?";
return $DB->get_records_sql($sql, array($swid, $title));
}
/**
* Search wiki title and or content.
*
* @param int $swid The subwiki ID.
* @param string $search What to search for.
* @param bool $searchtitle Search page titles.
* @param bool $searchcontent Search page contents.
* @param bool $exact Only an exact title match if true.
* @return stdClass[]
*/
function socialwiki_search($swid, $search, $searchtitle = true, $searchcontent = true, $exact = false) {
global $DB;
// Return nothing if not searching the title or content.
if (!$searchtitle && !$searchcontent) {
return [];
}
// If not exact then allow for characters on either side.
if (!$exact) {
$search = '%' . $search . '%';
}
// Search SQL.
$sql = "SELECT * FROM {socialwiki_pages}
WHERE subwikiid=? AND (";
if ($searchtitle && $searchcontent) {
// If looking for title and content then search by both.
$sql .= "content LIKE ? OR title LIKE ?";
$params = array($swid, $search, $search);
} else {
// Only place the correct term if title or content are turned off.
$sql .= $searchtitle ? "title LIKE ?" : "content LIKE ?";
$params = array($swid, $search);
}
// Group the pages with likes together.
$sql .= ")";
return $DB->get_records_sql($sql, $params);
}
/**
* Get user data.
*
* @param int $uid The user ID.
* @return stdClass
*/
function socialwiki_get_user_info($uid) {
global $DB;
if ($uid == -1) {
$user = new stdClass();
$user->id = $uid;
$user->firstname = get_string('unknownfirstname', 'socialwiki');
$user->lastname = get_string('unknownlastname', 'socialwiki');
$user->middlename = $user->alternatename = $user->firstnamephonetic = $user->lastnamephonetic = "";
$user->picture = 0;
$user->imagealt = $user->email = "";
return $user;
}
return $DB->get_record('user', array('id' => $uid));
}
/**
* Increase page view number.
*
* @param stdClass $page Database record.
*/
function socialwiki_increment_pageviews($page) {
global $DB;
$page->pageviews++;
$DB->update_record('socialwiki_pages', $page);
}
/**
* Increase page view number for given user.
* If this is the first time the user has viewed the page, a new entry will be added.
*
* @param int $uid The user ID.
* @param int $pid The page ID.
*/
function socialwiki_increment_user_views($uid, $pid) {
global $DB;
$result = $DB->get_record('socialwiki_user_views', array('userid' => $uid, 'pageid' => $pid));
if (!$result) {
$DB->insert_record("socialwiki_user_views",
array('userid' => $uid, 'pageid' => $pid, 'latestview' => time(), 'viewcount' => 1), true, false);
} else {
$userview = array(
'id' => $result->id,
'userid' => $result->userid,
'pageid' => $result->pageid,
'latestview' => time(),
'viewcount' => $result->viewcount + 1,
);
$DB->update_record("socialwiki_user_views", $userview, false);
}
}
// ----------------------------------------------------------
// ----------------------------------------------------------
/**
* Style formats.
*
* @return string[]
*/
function socialwiki_get_styles() {
return array('classic'); // Style 'modern' removed for now.
}
/**
* Text format supported by wiki module.
*
* @return string[]
*/
function socialwiki_get_formats() {
return array('html', 'creole', 'nwiki');
}
/**
* Parses a string with the wiki markup language.
*
* @author Josep Arús Pous
* @param string $markup The wiki markup language.
* @param string $pagecontent The page content.
* @param array $options Extra options.
* @return bool|array False when something wrong has happened.
*/
function socialwiki_parse_content($markup, $pagecontent, $options = array()) {
$subwiki = socialwiki_get_subwiki($options['swid']);
$cm = get_coursemodule_from_instance("socialwiki", $subwiki->wikiid);
$context = context_module::instance($cm->id);
$parseroptions = array(
'link_callback' => '/mod/socialwiki/locallib.php:socialwiki_parser_link',
'link_callback_args' => array('swid' => $options['swid'], 'navi' => $options['navi']),
'table_callback' => '/mod/socialwiki/locallib.php:socialwiki_parser_table',
'real_path_callback' => '/mod/socialwiki/locallib.php:socialwiki_parser_real_path',
'real_path_callback_args' => array(
'context' => $context,
'component' => 'mod_socialwiki',
'filearea' => 'attachments',
'subwikiid' => $subwiki->id,
'pageid' => $options['pageid']
),
'pageid' => $options['pageid'],
'pretty_print' => (isset($options['pretty_print']) && $options['pretty_print']),
'printable' => (isset($options['printable']) && $options['printable'])
);
return socialwiki_parser_proxy::parse($pagecontent, $markup, $parseroptions);
}
/**
* This function is the parser callback to parse wiki links.
*
* It returns the necessary information to print a link.
*
* NOTE: Empty pages and non-existent pages must be print in red color.
*
* !!!!!! IMPORTANT !!!!!!
* It is critical that you call format_string on the content before it is used.
*
* @param string|page_socialwiki $link Name of a page.
* @param array $options Extra options.
* @return array Array('content' => string, 'url' => string, 'new' => bool, 'link_info' => array)
*/
function socialwiki_parser_link($link, $options = null) {
global $CFG, $PAGE;
$matches = array();
$swid = $options['swid'];
$star = '';
if (is_object($link)) { // If the fn is passed a page_socialwiki object as 1st argument.
$parsedlink = array('content' => $link->title, 'url' => $CFG->wwwroot . '/mod/socialwiki/view.php?pageid='
. $link->id, 'new' => false, 'link_info' => array('link' => $link->title, 'pageid' => $link->id, 'new' => false));
} else { // Standard case, wikilink shortcut in text.
$specific = false;
if (preg_match('/@(([0-9]+)|(\.))/', $link, $matches)) { // Check if getting a specific version.
$link = preg_replace('/@(([0-9]+)|(\.))/', "", $link);
$specific = true;
} else if ($options['navi'] !== 0) {
if ($options['navi'] === -1) {
$matches[1] = '.';
$specific = true;
} else {
$fav = socialwiki_get_title_favourite($options['navi'], $link, $swid);
if ($fav !== false) {
$matches[1] = $fav;
$star = ' ★';
$specific = true;
}
}
}
if ($page = socialwiki_get_page_by_title($swid, $link)) { // Check if there is a page with that name.
if ($specific) { // Going to a specific page (no search).
if ($matches[1] == '.') { // Get the most recent version with the title.
$parsedlink = array('content' => $link, 'new' => false, 'url' => $CFG->wwwroot
. "/mod/socialwiki/view.php?pageid={$page->id}",
'link_info' => array('link' => $link, 'pageid' => $page->id, 'new' => false));
} else { // Get the page at the ID.
if (socialwiki_get_page($matches[1])) { // Page found and linked.
$parsedlink = array('content' => $link . $star, 'new' => false, 'url' => $CFG->wwwroot
. "/mod/socialwiki/view.php?pageid=$matches[1]",
'link_info' => array('link' => $link, 'pageid' => $matches[1], 'new' => false));
} else { // The page wasn't found, do a search instead.
$currentpage = optional_param('pageid', 0, PARAM_INT);
$parsedlink = array('content' => $link, 'new' => false, 'url' => $CFG->wwwroot
. "/mod/socialwiki/search.php?searchstring=$link&pageid=$currentpage&id={$PAGE->cm->id}&exact=1",
'link_info' => array('link' => $link, 'pageid' => -$page->id, 'new' => false));
}
}
} else { // Make a search for pages based on the title.
$currentpage = optional_param('pageid', 0, PARAM_INT);
$parsedlink = array('content' => $link, 'url' => $CFG->wwwroot
. '/mod/socialwiki/search.php?searchstring=' . $link . '&pageid=' . $currentpage
. '&id=' . $PAGE->cm->id . '&exact=1', 'new' => false,
'link_info' => array('link' => $link, 'pageid' => -$page->id, 'new' => false));
}
} else { // A page with that title doesn't exist.
$parsedlink = array('content' => $link, 'url' => $CFG->wwwroot . '/mod/socialwiki/create.php?swid='
. $swid . '&title=' . urlencode($link) . '&action=new', 'new' => true,
'link_info' => array('link' => $link, 'new' => true, 'pageid' => 0));
}
}
return $parsedlink;
}
/**
* Returns the table fully parsed (HTML).
*
* @param array $table Table Data.
* @return string for the table $table
* @author Josep Arús Pous
*
*/
function socialwiki_parser_table($table) {
$htmltable = new html_table();
$headers = $table[0];
$htmltable->head = array();
foreach ($headers as $h) {
$htmltable->head[] = $h[1];
}
array_shift($table);
$htmltable->data = array();
foreach ($table as $row) {
$rowdata = array();
foreach ($row as $r) {
$rowdata[] = $r[1];
}
$htmltable->data[] = $rowdata;
}
return html_writer::table($htmltable);
}
/**
* Returns an absolute path link, unless there is no such link.
*
* @param string $url Link's URL or filename.
* @param stdClass $context filearea params.
* @param string $component The component the file is associated with.
* @param string $filearea The filearea the file is stored in.
* @param int $swid The subwiki ID.
*
* @return string URL for files full path
*/
function socialwiki_parser_real_path($url, $context, $component, $filearea, $swid) {
global $CFG;
if (preg_match("/^(?:http|ftp)s?\:\/\// ", $url)) {
return $url;
} else {
$file = 'pluginfile.php';
if (!$CFG->slasharguments) {
$file = $file . '?file=';
}
$baseurl = "$CFG->wwwroot/$file/{$context->id}/$component/$filearea/$swid/";
// It is a file in current file area.
return $baseurl . $url;
}
}
/**
* Returns the token used by a wiki language to represent a given tag or "object" (bold -> **).
*
* @param string $markup The markup language.
* @param string $name Type to check.
* @return A string when it has only one token at the beginning (f. ex. lists).
* An array composed by 2 strings when it has 2 tokens, one at the beginning
* and one at the end (f. ex. italics). Returns false otherwise.
* @author Josep Arús Pous
*/
function socialwiki_parser_get_token($markup, $name) {
return socialwiki_parser_proxy::get_token($name, $markup);
}
/**
* Checks if current user can view a subwiki.
*
* @param stdClass $subwiki The subwiki ID.
* @return bool
*/
function socialwiki_user_can_view($subwiki) {
$wiki = socialwiki_get_wiki($subwiki->wikiid);
$cm = get_coursemodule_from_instance('socialwiki', $wiki->id);
$context = context_module::instance($cm->id);
// Working depending on activity groupmode.
switch (groups_get_activity_groupmode($cm)) {
case NOGROUPS:
if ($wiki->wikimode == 'collaborative') {
// Collaborative Mode:
// There is one wiki for all the class.
// Only view capbility needed.
return has_capability('mod/socialwiki:viewpage', $context);
} else {
// Error.
return false;
}
case SEPARATEGROUPS:
// Collaborative and Individual Mode
// Collaborative Mode: There is one wiki per group.
// Individual Mode: Each person owns a wiki.
if ($wiki->wikimode == 'collaborative' || $wiki->wikimode == 'individual') {
// Only members of subwiki group could view that wiki.
if (groups_is_member($subwiki->groupid)) {
// Only view capability needed.
return has_capability('mod/socialwiki:viewpage', $context);
} else {
/*
* User is not part of that group.
* User must have: mod/wiki:managewiki capability
* or moodle/site:accessallgroups capability
* and mod/wiki:viewpage capability.
*/
$view = has_capability('mod/socialwiki:viewpage', $context);
$manage = has_capability('mod/socialwiki:managewiki', $context);
$access = has_capability('moodle/site:accessallgroups', $context);
return ($manage || $access) && $view;
}
} else {
// Error.
return false;
}
case VISIBLEGROUPS:
// Collaborative and Individual Mode
// Collaborative Mode: There is one wiki per group.
// Individual Mode: Each person owns a wiki.
if ($wiki->wikimode == 'collaborative' || $wiki->wikimode == 'individual') {
// Everybody can read all wikis.
// Only view capability needed.
return has_capability('mod/socialwiki:viewpage', $context);
} else {
// Error.
return false;
}
default: // Error.
return false;
}
}
/**
* Checks if current user can edit a subwiki.
*
* @param stdClass $subwiki The subwiki ID.
* @return bool
*/
function socialwiki_user_can_edit($subwiki) {
$wiki = socialwiki_get_wiki($subwiki->wikiid);
$cm = get_coursemodule_from_instance('socialwiki', $wiki->id);
$context = context_module::instance($cm->id);
// Working depending on activity groupmode.
switch (groups_get_activity_groupmode($cm)) {
case NOGROUPS:
if ($wiki->wikimode == 'collaborative') {
// Collaborative Mode: There is a wiki for all the class.
// Only edit capbility needed.
return has_capability('mod/socialwiki:editpage', $context);
} else {
// Error.
return false;
}
case SEPARATEGROUPS:
if ($wiki->wikimode == 'collaborative') {
// Collaborative Mode: There is one wiki per group.
// Only members of subwiki group could edit that wiki.
if ($subwiki->groupid == groups_get_activity_group($cm)) {
// Only edit capability needed.
return has_capability('mod/socialwiki:editpage', $context);
} else {
/*
* User is not part of that group.
* User must have: mod/wiki:managewiki capability
* or moodle/site:accessallgroups capability
* and mod/wiki:editpage capability.
*/
$manage = has_capability('mod/socialwiki:managewiki', $context);
$access = has_capability('moodle/site:accessallgroups', $context);
$edit = has_capability('mod/socialwiki:editpage', $context);
return $manage && $access && $edit;
}
} else {
// Error.
return false;
}
case VISIBLEGROUPS:
if ($wiki->wikimode == 'collaborative') {
// Collaborative Mode: There is one wiki per group.
// Only members of subwiki group could edit that wiki.
if (groups_is_member($subwiki->groupid)) {
// Only edit capability needed.
return has_capability('mod/socialwiki:editpage', $context);
} else {
/*
* User is not part of that group.
* User must have: mod/wiki:managewiki capability
* and mod/wiki:editpage capability.
*/
$manage = has_capability('mod/socialwiki:managewiki', $context);
$edit = has_capability('mod/socialwiki:editpage', $context);
return $manage && $edit;
}
} else {
// Error.
return false;
}
default: // Error.
return false;
}
}
/**
* Delete pages and all related data.
*
* @param mixed $context Context in which page needs to be deleted.
* @param mixed $pages Pages to be deleted.
* @param int $swid ID of the subwiki for which all pages should be deleted.
*/
function socialwiki_delete_pages($context, $pages = null, $swid = null) {
global $DB;
if (!empty($pages) && is_int($pages)) {
$pages = array($pages);
} else if (!empty($swid)) {
$pages = socialwiki_get_page_list($swid);
}
// If there is no pageid then return as we can't delete anything.
if (empty($pages)) {
return;
}
// Delete page and all it's relevant data.
foreach ($pages as $p) {
if (!is_object($p)) {
$p = socialwiki_get_page($p);
}
// Delete page comments.
$comments = socialwiki_get_comments($context->id, $p->id);
foreach ($comments as $commentid => $commentvalue) {
socialwiki_delete_comment($commentid, $context, $p->id);
}
// Delete page likes.
socialwiki_delete_page_likes($p->id);
// Delete page tags.
$tags = tag_get_tags_array('socialwiki_pages', $p->id);
foreach ($tags as $tagid => $tagvalue) {
tag_delete_instance('socialwiki_pages', $p->id, $tagid);
}
// Fix parent pages.
$records = $DB->get_records('socialwiki_pages', array('parent' => $p->id));
foreach ($records as $rec) {
$rec->parent = $p->parent;
$DB->update_record('socialwiki_pages', $rec);
}
// Delete page.
$DB->delete_records('socialwiki_pages', array('id' => $p->id));
}
}
/**
* Get a comment.
*
* @param int $commentid The comment ID.
* @return stdClass
*/
function socialwiki_get_comment($commentid) {
global $DB;
return $DB->get_record('comments', array('id' => $commentid));
}
/**
* Returns all comments by context and pageid.
*
* @param int $contextid Current context ID.
* @param int $pid Current page ID.
* @return stdClass[]
*/
function socialwiki_get_comments($contextid, $pid) {
global $DB;
return $DB->get_records('comments', array('contextid' => $contextid, 'itemid' => $pid, 'commentarea' => 'socialwiki_page'));
}
/**
* Add comments to database.
*
* @param stdClass $context Current context.
* @param int $pid Current page ID.
* @param string $content Content of the comment.
* @param string $editor Version of editor we are using.
*/
function socialwiki_add_comment($context, $pid, $content, $editor) {
global $CFG;
require_once($CFG->dirroot . '/comment/lib.php');
list($contextid, $course, $cm) = get_context_info_array($context->id);
$cmt = new stdClass();
$cmt->context = $contextid;
$cmt->itemid = $pid;
$cmt->area = 'socialwiki_page';
$cmt->course = $course;
$cmt->component = 'mod_socialwiki';
$manager = new comment($cmt);
if ($editor == 'creole') {
$manager->add($content, SOCIALFORMAT_CREOLE);
} else if ($editor == 'html') {
$manager->add($content, FORMAT_HTML);
} else if ($editor == 'nwiki') {
$manager->add($content, SOCIALFORMAT_NWIKI);
}
}
/**
* Delete comments from database.
*
* @param int $commentid ID of comment which will be deleted.
* @param stdClass $context Current context.
* @param int $pid Current page ID.
*/
function socialwiki_delete_comment($commentid, $context, $pid) {
global $CFG;
require_once($CFG->dirroot . '/comment/lib.php');
list($contextid, $course, $cm) = get_context_info_array($context->id);
$cmt = new stdClass();
$cmt->context = $contextid;
$cmt->itemid = $pid;
$cmt->area = 'socialwiki_page';
$cmt->course = $course;
$cmt->component = 'mod_socialwiki';
$manager = new comment($cmt);
$manager->delete($commentid);
}
/**
* Delete all comments from the wiki
*/
function socialwiki_delete_comments_wiki() {
global $PAGE, $DB;
$cm = $PAGE->cm;
$context = context_module::instance($cm->id);
$table = 'comments';
$select = 'contextid = ?';
$DB->delete_records_select($table, $select, array($context->id));
}
/**
* Print the page content.
*
* @param stdClass $page The current page.
* @param stdClass $context The current context.
* @param int $swid The subwiki ID.
* @param int $navigation The link nav type.
*/
function socialwiki_print_page_content($page, $context, $swid) {
global $PAGE, $USER, $SESSION;
// Only increment page view when linked, not refreshed.
$pagerefreshed = (null !== filter_input(INPUT_SERVER, 'HTTP_CACHE_CONTROL'))
&& filter_input(INPUT_SERVER, 'HTTP_CACHE_CONTROL') === 'max-age=0';
if (!$pagerefreshed) {
socialwiki_increment_pageviews($page);
socialwiki_increment_user_views($USER->id, $page->id);
}
$content = socialwiki_parse_content(
$page->format, $page->content, array('swid' => $swid, 'pageid' => $page->id, 'navi' => $SESSION->mod_socialwiki->navi));
$html = file_rewrite_pluginfile_urls($content['parsed_text'], 'pluginfile.php',
$context->id, 'mod_socialwiki', 'attachments', $swid);
$wikioutput = $PAGE->get_renderer('mod_socialwiki');
// This is where the page content, from the title down, is rendered!
return $wikioutput->viewing_area(
$content['toc'] . format_text($html, FORMAT_MOODLE, array('overflowdiv' => true, 'allowid' => true)), $page);
}
/**
* Prints default edit form fields and buttons.
*
* @param string $format Edit form format (ex. creole).
* @param bool $upload
* @param array $deleteuploads
*/
function socialwiki_print_edit_form_default_fields($format, $upload = false, $deleteuploads = array()) {
global $CFG, $PAGE, $OUTPUT;
// Hidden values.