forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
locallib.php
1854 lines (1605 loc) · 60.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
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_wiki
* @copyright 2009 Marc Alier, Jordi Piguillem [email protected]
* @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
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/mod/wiki/lib.php');
require_once($CFG->dirroot . '/mod/wiki/parser/parser.php');
require_once($CFG->libdir . '/filelib.php');
define('WIKI_REFRESH_CACHE_TIME', 30); // @TODO: To be deleted.
define('FORMAT_CREOLE', '37');
define('FORMAT_NWIKI', '38');
define('NO_VALID_RATE', '-999');
define('IMPROVEMENT', '+');
define('EQUAL', '=');
define('WORST', '-');
define('LOCK_TIMEOUT', 30);
/**
* Get a wiki instance
* @param int $wikiid the instance id of wiki
*/
function wiki_get_wiki($wikiid) {
global $DB;
return $DB->get_record('wiki', array('id' => $wikiid));
}
/**
* Get sub wiki instances with same wiki id
* @param int $wikiid
*/
function wiki_get_subwikis($wikiid) {
global $DB;
return $DB->get_records('wiki_subwikis', array('wikiid' => $wikiid));
}
/**
* Get a sub wiki instance by wiki id and group id
* @param int $wikiid
* @param int $groupid
* @return object
*/
function wiki_get_subwiki_by_group($wikiid, $groupid, $userid = 0) {
global $DB;
return $DB->get_record('wiki_subwikis', array('wikiid' => $wikiid, 'groupid' => $groupid, 'userid' => $userid));
}
/**
* Get a sub wiki instace by instance id
* @param int $subwikiid
* @return object
*/
function wiki_get_subwiki($subwikiid) {
global $DB;
return $DB->get_record('wiki_subwikis', array('id' => $subwikiid));
}
/**
* Add a new sub wiki instance
* @param int $wikiid
* @param int $groupid
* @return int $insertid
*/
function wiki_add_subwiki($wikiid, $groupid, $userid = 0) {
global $DB;
$record = new StdClass();
$record->wikiid = $wikiid;
$record->groupid = $groupid;
$record->userid = $userid;
$insertid = $DB->insert_record('wiki_subwikis', $record);
return $insertid;
}
/**
* Get a wiki instance by pageid
* @param int $pageid
* @return object
*/
function wiki_get_wiki_from_pageid($pageid) {
global $DB;
$sql = "SELECT w.*
FROM {wiki} w, {wiki_subwikis} s, {wiki_pages} p
WHERE p.id = ? AND
p.subwikiid = s.id AND
s.wikiid = w.id";
return $DB->get_record_sql($sql, array($pageid));
}
/**
* Get a wiki page by pageid
* @param int $pageid
* @return object
*/
function wiki_get_page($pageid) {
global $DB;
return $DB->get_record('wiki_pages', array('id' => $pageid));
}
/**
* Get latest version of wiki page
* @param int $pageid
* @return object
*/
function wiki_get_current_version($pageid) {
global $DB;
// @TODO: Fix this query
$sql = "SELECT *
FROM {wiki_versions}
WHERE pageid = ?
ORDER BY version DESC";
$records = $DB->get_records_sql($sql, array($pageid), 0, 1);
return array_pop($records);
}
/**
* Alias of wiki_get_current_version
* @TODO, does the exactly same thing as wiki_get_current_version, should be removed
* @param int $pageid
* @return object
*/
function wiki_get_last_version($pageid) {
return wiki_get_current_version($pageid);
}
/**
* Get page section
* @param int $pageid
* @param string $section
*/
function wiki_get_section_page($page, $section) {
$version = wiki_get_current_version($page->id);
return wiki_parser_proxy::get_section($version->content, $version->contentformat, $section);
}
/**
* Get a wiki page by page title
* @param int $swid, sub wiki id
* @param string $title
* @return object
*/
function wiki_get_page_by_title($swid, $title) {
global $DB;
return $DB->get_record('wiki_pages', array('subwikiid' => $swid, 'title' => $title));
}
/**
* Get a version record by record id
* @param int $versionid, the version id
* @return object
*/
function wiki_get_version($versionid) {
global $DB;
return $DB->get_record('wiki_versions', array('id' => $versionid));
}
/**
* Get first page of wiki instace
* @param int $subwikiid
* @param int $module, wiki instance object
*/
function wiki_get_first_page($subwikid, $module = null) {
global $DB, $USER;
$sql = "SELECT p.*
FROM {wiki} w, {wiki_subwikis} s, {wiki_pages} p
WHERE s.id = ? AND
s.wikiid = w.id AND
w.firstpagetitle = p.title AND
p.subwikiid = s.id";
return $DB->get_record_sql($sql, array($subwikid));
}
function wiki_save_section($wikipage, $sectiontitle, $sectioncontent, $userid) {
$wiki = wiki_get_wiki_from_pageid($wikipage->id);
$cm = get_coursemodule_from_instance('wiki', $wiki->id);
$context = context_module::instance($cm->id);
if (has_capability('mod/wiki:editpage', $context)) {
$version = wiki_get_current_version($wikipage->id);
$content = wiki_parser_proxy::get_section($version->content, $version->contentformat, $sectiontitle, true);
$newcontent = $content[0] . $sectioncontent . $content[2];
return wiki_save_page($wikipage, $newcontent, $userid);
} else {
return false;
}
}
/**
* Save page content
* @param object $wikipage
* @param string $newcontent
* @param int $userid
*/
function wiki_save_page($wikipage, $newcontent, $userid) {
global $DB;
$wiki = wiki_get_wiki_from_pageid($wikipage->id);
$cm = get_coursemodule_from_instance('wiki', $wiki->id);
$context = context_module::instance($cm->id);
if (has_capability('mod/wiki:editpage', $context)) {
$version = wiki_get_current_version($wikipage->id);
$version->content = $newcontent;
$version->userid = $userid;
$version->version++;
$version->timecreated = time();
$version->id = $DB->insert_record('wiki_versions', $version);
$wikipage->timemodified = $version->timecreated;
$wikipage->userid = $userid;
$return = wiki_refresh_cachedcontent($wikipage, $newcontent);
$event = \mod_wiki\event\page_updated::create(
array(
'context' => $context,
'objectid' => $wikipage->id,
'relateduserid' => $userid,
'other' => array(
'newcontent' => $newcontent
)
));
$event->add_record_snapshot('wiki', $wiki);
$event->add_record_snapshot('wiki_pages', $wikipage);
$event->add_record_snapshot('wiki_versions', $version);
$event->trigger();
return $return;
} else {
return false;
}
}
function wiki_refresh_cachedcontent($page, $newcontent = null) {
global $DB;
$version = wiki_get_current_version($page->id);
if (empty($version)) {
return null;
}
if (!isset($newcontent)) {
$newcontent = $version->content;
}
$options = array('swid' => $page->subwikiid, 'pageid' => $page->id);
$parseroutput = wiki_parse_content($version->contentformat, $newcontent, $options);
$page->cachedcontent = $parseroutput['toc'] . $parseroutput['parsed_text'];
$page->timerendered = time();
$DB->update_record('wiki_pages', $page);
wiki_refresh_page_links($page, $parseroutput['link_count']);
return array('page' => $page, 'sections' => $parseroutput['repeated_sections'], 'version' => $version->version);
}
/**
* Restore a page with specified version.
*
* @param stdClass $wikipage wiki page record
* @param stdClass $version wiki page version to restore
* @param context_module $context context of wiki module
* @return stdClass restored page
*/
function wiki_restore_page($wikipage, $version, $context) {
$return = wiki_save_page($wikipage, $version->content, $version->userid);
$event = \mod_wiki\event\page_version_restored::create(
array(
'context' => $context,
'objectid' => $version->id,
'other' => array(
'pageid' => $wikipage->id
)
));
$event->add_record_snapshot('wiki_versions', $version);
$event->trigger();
return $return['page'];
}
function wiki_refresh_page_links($page, $links) {
global $DB;
$DB->delete_records('wiki_links', array('frompageid' => $page->id));
foreach ($links as $linkname => $linkinfo) {
$newlink = new stdClass();
$newlink->subwikiid = $page->subwikiid;
$newlink->frompageid = $page->id;
if ($linkinfo['new']) {
$newlink->tomissingpage = $linkname;
} else {
$newlink->topageid = $linkinfo['pageid'];
}
try {
$DB->insert_record('wiki_links', $newlink);
} catch (dml_exception $e) {
debugging($e->getMessage());
}
}
}
/**
* Create a new wiki page, if the page exists, return existing pageid
* @param int $swid
* @param string $title
* @param string $format
* @param int $userid
*/
function wiki_create_page($swid, $title, $format, $userid) {
global $DB;
$subwiki = wiki_get_subwiki($swid);
$cm = get_coursemodule_from_instance('wiki', $subwiki->wikiid);
$context = context_module::instance($cm->id);
require_capability('mod/wiki:editpage', $context);
// if page exists
if ($page = wiki_get_page_by_title($swid, $title)) {
return $page->id;
}
// Creating a new empty version
$version = new stdClass();
$version->content = '';
$version->contentformat = $format;
$version->version = 0;
$version->timecreated = time();
$version->userid = $userid;
$versionid = null;
$versionid = $DB->insert_record('wiki_versions', $version);
// Createing a new empty page
$page = new stdClass();
$page->subwikiid = $swid;
$page->title = $title;
$page->cachedcontent = '';
$page->timecreated = $version->timecreated;
$page->timemodified = $version->timecreated;
$page->timerendered = $version->timecreated;
$page->userid = $userid;
$page->pageviews = 0;
$page->readonly = 0;
$pageid = $DB->insert_record('wiki_pages', $page);
// Setting the pageid
$version->id = $versionid;
$version->pageid = $pageid;
$DB->update_record('wiki_versions', $version);
$event = \mod_wiki\event\page_created::create(
array(
'context' => $context,
'objectid' => $pageid
)
);
$event->trigger();
wiki_make_cache_expire($page->title);
return $pageid;
}
function wiki_make_cache_expire($pagename) {
global $DB;
$sql = "UPDATE {wiki_pages}
SET timerendered = 0
WHERE id IN ( SELECT l.frompageid
FROM {wiki_links} l
WHERE l.tomissingpage = ?
)";
$DB->execute ($sql, array($pagename));
}
/**
* Get a specific version of page
* @param int $pageid
* @param int $version
*/
function wiki_get_wiki_page_version($pageid, $version) {
global $DB;
return $DB->get_record('wiki_versions', array('pageid' => $pageid, 'version' => $version));
}
/**
* Get version list
* @param int $pageid
* @param int $limitfrom
* @param int $limitnum
*/
function wiki_get_wiki_page_versions($pageid, $limitfrom, $limitnum) {
global $DB;
return $DB->get_records('wiki_versions', array('pageid' => $pageid), 'version DESC', '*', $limitfrom, $limitnum);
}
/**
* Count the number of page version
* @param int $pageid
*/
function wiki_count_wiki_page_versions($pageid) {
global $DB;
return $DB->count_records('wiki_versions', array('pageid' => $pageid));
}
/**
* Get linked from page
* @param int $pageid
*/
function wiki_get_linked_to_pages($pageid) {
global $DB;
return $DB->get_records('wiki_links', array('frompageid' => $pageid));
}
/**
* Get linked from page
* @param int $pageid
*/
function wiki_get_linked_from_pages($pageid) {
global $DB;
return $DB->get_records('wiki_links', array('topageid' => $pageid));
}
/**
* Get pages which user have been edited
* @param int $swid
* @param int $userid
*/
function wiki_get_contributions($swid, $userid) {
global $DB;
$sql = "SELECT v.*
FROM {wiki_versions} v, {wiki_pages} p
WHERE p.subwikiid = ? AND
v.pageid = p.id AND
v.userid = ?";
return $DB->get_records_sql($sql, array($swid, $userid));
}
/**
* Get missing or empty pages in wiki
* @param int $swid sub wiki id
*/
function wiki_get_missing_or_empty_pages($swid) {
global $DB;
$sql = "SELECT DISTINCT p.title, p.id, p.subwikiid
FROM {wiki} w, {wiki_subwikis} s, {wiki_pages} p
WHERE s.wikiid = w.id and
s.id = ? and
w.firstpagetitle != p.title and
p.subwikiid = ? and
1 = (SELECT count(*)
FROM {wiki_versions} v
WHERE v.pageid = p.id)
UNION
SELECT DISTINCT l.tomissingpage as title, 0 as id, l.subwikiid
FROM {wiki_links} l
WHERE l.subwikiid = ? and
l.topageid = 0";
return $DB->get_records_sql($sql, array($swid, $swid, $swid));
}
/**
* Get pages list in wiki
* @param int $swid sub wiki id
* @param string $sort How to sort the pages. By default, title ASC.
*/
function wiki_get_page_list($swid, $sort = 'title ASC') {
global $DB;
$records = $DB->get_records('wiki_pages', array('subwikiid' => $swid), $sort);
return $records;
}
/**
* Return a list of orphaned wikis for one specific subwiki
* @global object
* @param int $swid sub wiki id
*/
function wiki_get_orphaned_pages($swid) {
global $DB;
$sql = "SELECT p.id, p.title
FROM {wiki_pages} p, {wiki} w , {wiki_subwikis} s
WHERE p.subwikiid = ?
AND s.id = ?
AND w.id = s.wikiid
AND p.title != w.firstpagetitle
AND p.id NOT IN (SELECT topageid FROM {wiki_links} WHERE subwikiid = ?)";
return $DB->get_records_sql($sql, array($swid, $swid, $swid));
}
/**
* Search wiki title
* @param int $swid sub wiki id
* @param string $search
*/
function wiki_search_title($swid, $search) {
global $DB;
return $DB->get_records_select('wiki_pages', "subwikiid = ? AND title LIKE ?", array($swid, '%'.$search.'%'));
}
/**
* Search wiki content
* @param int $swid sub wiki id
* @param string $search
*/
function wiki_search_content($swid, $search) {
global $DB;
return $DB->get_records_select('wiki_pages', "subwikiid = ? AND cachedcontent LIKE ?", array($swid, '%'.$search.'%'));
}
/**
* Search wiki title and content
* @param int $swid sub wiki id
* @param string $search
*/
function wiki_search_all($swid, $search) {
global $DB;
return $DB->get_records_select('wiki_pages', "subwikiid = ? AND (cachedcontent LIKE ? OR title LIKE ?)", array($swid, '%'.$search.'%', '%'.$search.'%'));
}
/**
* Get user data
*/
function wiki_get_user_info($userid) {
global $DB;
return $DB->get_record('user', array('id' => $userid));
}
/**
* Increase page view nubmer
* @param int $page, database record
*/
function wiki_increment_pageviews($page) {
global $DB;
$page->pageviews++;
$DB->update_record('wiki_pages', $page);
}
//----------------------------------------------------------
//----------------------------------------------------------
/**
* Text format supported by wiki module
*/
function wiki_get_formats() {
return array('html', 'creole', 'nwiki');
}
/**
* Parses a string with the wiki markup language in $markup.
*
* @return Array or false when something wrong has happened.
*
* Returned array contains the following fields:
* 'parsed_text' => String. Contains the parsed wiki content.
* 'unparsed_text' => String. Constains the original wiki content.
* 'link_count' => Array of array('destination' => ..., 'new' => "is new?"). Contains the internal wiki links found in the wiki content.
* 'deleted_sections' => the list of deleted sections.
* '' =>
*
* @author Josep Arús Pous
**/
function wiki_parse_content($markup, $pagecontent, $options = array()) {
global $PAGE;
$subwiki = wiki_get_subwiki($options['swid']);
$cm = get_coursemodule_from_instance("wiki", $subwiki->wikiid);
$context = context_module::instance($cm->id);
$parser_options = array(
'link_callback' => '/mod/wiki/locallib.php:wiki_parser_link',
'link_callback_args' => array('swid' => $options['swid']),
'table_callback' => '/mod/wiki/locallib.php:wiki_parser_table',
'real_path_callback' => '/mod/wiki/locallib.php:wiki_parser_real_path',
'real_path_callback_args' => array(
'context' => $context,
'component' => 'mod_wiki',
'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 wiki_parser_proxy::parse($pagecontent, $markup, $parser_options);
}
/**
* This function is the parser callback to parse wiki links.
*
* It returns the necesary 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_wiki $link name of a page
* @param array $options
* @return array Array('content' => string, 'url' => string, 'new' => bool, 'link_info' => array)
*
* @TODO Doc return and options
*/
function wiki_parser_link($link, $options = null) {
global $CFG;
if (is_object($link)) {
$parsedlink = array('content' => $link->title, 'url' => $CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $link->id, 'new' => false, 'link_info' => array('link' => $link->title, 'pageid' => $link->id, 'new' => false));
$version = wiki_get_current_version($link->id);
if ($version->version == 0) {
$parsedlink['new'] = true;
}
return $parsedlink;
} else {
$swid = $options['swid'];
if ($page = wiki_get_page_by_title($swid, $link)) {
$parsedlink = array('content' => $link, 'url' => $CFG->wwwroot . '/mod/wiki/view.php?pageid=' . $page->id, 'new' => false, 'link_info' => array('link' => $link, 'pageid' => $page->id, 'new' => false));
$version = wiki_get_current_version($page->id);
if ($version->version == 0) {
$parsedlink['new'] = true;
}
return $parsedlink;
} else {
return array('content' => $link, 'url' => $CFG->wwwroot . '/mod/wiki/create.php?swid=' . $swid . '&title=' . urlencode($link) . '&action=new', 'new' => true, 'link_info' => array('link' => $link, 'new' => true, 'pageid' => 0));
}
}
}
/**
* Returns the table fully parsed (HTML)
*
* @return HTML for the table $table
* @author Josep Arús Pous
*
**/
function wiki_parser_table($table) {
global $OUTPUT;
$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) {
$row_data = array();
foreach ($row as $r) {
$row_data[] = $r[1];
}
$htmltable->data[] = $row_data;
}
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 Sub wiki id
*
* @return string URL for files full path
*/
function wiki_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 -> **)
*
* @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 wiki_parser_get_token($markup, $name) {
return wiki_parser_proxy::get_token($name, $markup);
}
/**
* Checks if current user can view a subwiki
*
* @param stdClass $subwiki usually record from {wiki_subwikis}. Must contain fields 'wikiid', 'groupid', 'userid'.
* If it also contains fields 'course' and 'groupmode' from table {wiki} it will save extra DB query.
* @param stdClass $wiki optional wiki object if known
* @return bool
*/
function wiki_user_can_view($subwiki, $wiki = null) {
global $USER;
if (empty($wiki) || $wiki->id != $subwiki->wikiid) {
$wiki = wiki_get_wiki($subwiki->wikiid);
}
$modinfo = get_fast_modinfo($wiki->course);
if (!isset($modinfo->instances['wiki'][$subwiki->wikiid])) {
// Module does not exist.
return false;
}
$cm = $modinfo->instances['wiki'][$subwiki->wikiid];
if (!$cm->uservisible) {
// The whole module is not visible to the current user.
return false;
}
$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/wiki:viewpage', $context);
} else if ($wiki->wikimode == 'individual') {
// Individual Mode:
// Each person owns a wiki.
if ($subwiki->userid == $USER->id) {
// Only the owner of the wiki can view it
return has_capability('mod/wiki:viewpage', $context);
} else { // User has special capabilities
// User must have:
// mod/wiki:viewpage capability
// and
// mod/wiki:managewiki capability
$view = has_capability('mod/wiki:viewpage', $context);
$manage = has_capability('mod/wiki:managewiki', $context);
return $view && $manage;
}
} 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 (in_array($subwiki->groupid, $modinfo->get_groups($cm->groupingid))) {
// Only view capability needed
return has_capability('mod/wiki: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/wiki:viewpage', $context);
$manage = has_capability('mod/wiki: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/wiki:viewpage', $context);
} else {
//Error
return false;
}
default: // Error
return false;
}
}
/**
* Checks if current user can edit a subwiki
*
* @param $subwiki
*/
function wiki_user_can_edit($subwiki) {
global $USER;
$wiki = wiki_get_wiki($subwiki->wikiid);
$cm = get_coursemodule_from_instance('wiki', $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/wiki:editpage', $context);
} else if ($wiki->wikimode == 'individual') {
// Individual Mode
// There is a wiki per user
// Only the owner of that wiki can edit it
if ($subwiki->userid == $USER->id) {
return has_capability('mod/wiki:editpage', $context);
} else { // Current user is not the owner of that wiki.
// User must have:
// mod/wiki:editpage capability
// and
// mod/wiki:managewiki capability
$edit = has_capability('mod/wiki:editpage', $context);
$manage = has_capability('mod/wiki:managewiki', $context);
return $edit && $manage;
}
} 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 (groups_is_member($subwiki->groupid)) {
// Only edit capability needed
return has_capability('mod/wiki:editpage', $context);
} else { // User is not part of that group
// User must have:
// mod/wiki:managewiki capability
// and
// moodle/site:accessallgroups capability
// and
// mod/wiki:editpage capability
$manage = has_capability('mod/wiki:managewiki', $context);
$access = has_capability('moodle/site:accessallgroups', $context);
$edit = has_capability('mod/wiki:editpage', $context);
return $manage && $access && $edit;
}
} else if ($wiki->wikimode == 'individual') {
// Individual Mode:
// Each person owns a wiki.
//
// Only the owner of that wiki can edit it
if ($subwiki->userid == $USER->id) {
return has_capability('mod/wiki:editpage', $context);
} else { // Current user is not the owner of that wiki.
// User must have:
// mod/wiki:managewiki capability
// and
// moodle/site:accessallgroups capability
// and
// mod/wiki:editpage capability
$manage = has_capability('mod/wiki:managewiki', $context);
$access = has_capability('moodle/site:accessallgroups', $context);
$edit = has_capability('mod/wiki: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/wiki: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/wiki:managewiki', $context);
$edit = has_capability('mod/wiki:editpage', $context);
return $manage && $edit;
}
} else if ($wiki->wikimode == 'individual') {
// Individual Mode:
// Each person owns a wiki.
//
// Only the owner of that wiki can edit it
if ($subwiki->userid == $USER->id) {
return has_capability('mod/wiki:editpage', $context);
} else { // Current user is not the owner of that wiki.
// User must have:
// mod/wiki:managewiki capability
// and
// mod/wiki:editpage capability
$manage = has_capability('mod/wiki:managewiki', $context);
$edit = has_capability('mod/wiki:editpage', $context);
return $manage && $edit;
}
} else {
//Error
return false;
}
default: // Error
return false;
}
}
//----------------
// Locks
//----------------
/**
* Checks if a page-section is locked.