mirrored from git://git.moodle.org/moodle.git
/
lib.php
2337 lines (1958 loc) · 85.7 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
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 // $Id$
/// Library of functions and constants for module glossary
/// (replace glossary with the name of your module and delete this line)
require_once($CFG->libdir.'/filelib.php');
define("GLOSSARY_SHOW_ALL_CATEGORIES", 0);
define("GLOSSARY_SHOW_NOT_CATEGORISED", -1);
define("GLOSSARY_NO_VIEW", -1);
define("GLOSSARY_STANDARD_VIEW", 0);
define("GLOSSARY_CATEGORY_VIEW", 1);
define("GLOSSARY_DATE_VIEW", 2);
define("GLOSSARY_AUTHOR_VIEW", 3);
define("GLOSSARY_ADDENTRY_VIEW", 4);
define("GLOSSARY_IMPORT_VIEW", 5);
define("GLOSSARY_EXPORT_VIEW", 6);
define("GLOSSARY_APPROVAL_VIEW", 7);
/// STANDARD FUNCTIONS ///////////////////////////////////////////////////////////
function glossary_add_instance($glossary) {
/// Given an object containing all the necessary data,
/// (defined by the form in mod.html) this function
/// will create a new instance and return the id number
/// of the new instance.
if (empty($glossary->userating)) {
$glossary->assessed = 0;
}
if (empty($glossary->ratingtime) or empty($glossary->assessed)) {
$glossary->assesstimestart = 0;
$glossary->assesstimefinish = 0;
}
if (empty($glossary->globalglossary) ) {
$glossary->globalglossary = 0;
}
if (!has_capability('mod/glossary:manageentries', get_context_instance(CONTEXT_SYSTEM))) {
$glossary->globalglossary = 0;
}
$glossary->timecreated = time();
$glossary->timemodified = $glossary->timecreated;
//Check displayformat is a valid one
$formats = get_list_of_plugins('mod/glossary/formats','TEMPLATE');
if (!in_array($glossary->displayformat, $formats)) {
error("This format doesn't exist!");
}
if ($returnid = insert_record("glossary", $glossary)) {
$glossary->id = $returnid;
$glossary = stripslashes_recursive($glossary);
glossary_grade_item_update($glossary);
}
return $returnid;
}
function glossary_update_instance($glossary) {
/// Given an object containing all the necessary data,
/// (defined by the form in mod.html) this function
/// will update an existing instance with new data.
global $CFG;
if (empty($glossary->globalglossary)) {
$glossary->globalglossary = 0;
}
if (!has_capability('mod/glossary:manageentries', get_context_instance(CONTEXT_SYSTEM))) {
// keep previous
unset($glossary->globalglossary);
}
$glossary->timemodified = time();
$glossary->id = $glossary->instance;
if (empty($glossary->userating)) {
$glossary->assessed = 0;
}
if (empty($glossary->ratingtime) or empty($glossary->assessed)) {
$glossary->assesstimestart = 0;
$glossary->assesstimefinish = 0;
}
//Check displayformat is a valid one
$formats = get_list_of_plugins('mod/glossary/formats','TEMPLATE');
if (!in_array($glossary->displayformat, $formats)) {
error("This format doesn't exist!");
}
if ($return = update_record("glossary", $glossary)) {
if ($glossary->defaultapproval) {
execute_sql("update {$CFG->prefix}glossary_entries SET approved = 1 where approved != 1 and glossaryid = " . $glossary->id,false);
}
$glossary = stripslashes_recursive($glossary);
glossary_grade_item_update($glossary);
}
return $return;
}
function glossary_delete_instance($id) {
/// Given an ID of an instance of this module,
/// this function will permanently delete the instance
/// and any data that depends on it.
if (! $glossary = get_record("glossary", "id", "$id")) {
return false;
}
$result = true;
# Delete any dependent records here #
if (! delete_records("glossary", "id", "$glossary->id")) {
$result = false;
} else {
if ($categories = get_records("glossary_categories","glossaryid",$glossary->id)) {
$cats = "";
foreach ( $categories as $cat ) {
$cats .= "$cat->id,";
}
$cats = substr($cats,0,-1);
if ($cats) {
delete_records_select("glossary_entries_categories", "categoryid in ($cats)");
delete_records("glossary_categories", "glossaryid", $glossary->id);
}
}
if ( $entries = get_records("glossary_entries", "glossaryid", $glossary->id) ) {
$ents = "";
foreach ( $entries as $entry ) {
if ( $entry->sourceglossaryid ) {
$entry->glossaryid = $entry->sourceglossaryid;
$entry->sourceglossaryid = 0;
update_record("glossary_entries",$entry);
} else {
$ents .= "$entry->id,";
}
}
$ents = substr($ents,0,-1);
if ($ents) {
delete_records_select("glossary_comments", "entryid in ($ents)");
delete_records_select("glossary_alias", "entryid in ($ents)");
delete_records_select("glossary_ratings", "entryid in ($ents)");
}
}
glossary_delete_attachments($glossary);
delete_records("glossary_entries", "glossaryid", "$glossary->id");
}
glossary_grade_item_delete($glossary);
return $result;
}
function glossary_user_outline($course, $user, $mod, $glossary) {
/// Return a small object with summary information about what a
/// user has done with a given particular instance of this module
/// Used for user activity reports.
/// $return->time = the time they did it
/// $return->info = a short text description
if ($entries = glossary_get_user_entries($glossary->id, $user->id)) {
$result = new object();
$result->info = count($entries) . ' ' . get_string("entries", "glossary");
$lastentry = array_pop($entries);
$result->time = $lastentry->timemodified;
return $result;
}
return NULL;
}
function glossary_get_user_entries($glossaryid, $userid) {
/// Get all the entries for a user in a glossary
global $CFG;
return get_records_sql("SELECT e.*, u.firstname, u.lastname, u.email, u.picture
FROM {$CFG->prefix}glossary g,
{$CFG->prefix}glossary_entries e,
{$CFG->prefix}user u
WHERE g.id = '$glossaryid'
AND e.glossaryid = g.id
AND e.userid = '$userid'
AND e.userid = u.id
ORDER BY e.timemodified ASC");
}
function glossary_user_complete($course, $user, $mod, $glossary) {
/// Print a detailed representation of what a user has done with
/// a given particular instance of this module, for user activity reports.
global $CFG;
if ($entries = glossary_get_user_entries($glossary->id, $user->id)) {
echo '<table width="95%" border="0"><tr><td>';
foreach ($entries as $entry) {
$cm = get_coursemodule_from_instance("glossary", $glossary->id, $course->id);
glossary_print_entry($course, $cm, $glossary, $entry,"","",0);
echo '<p>';
}
echo '</td></tr></table>';
}
}
function glossary_print_recent_activity($course, $viewfullnames, $timestart) {
/// Given a course and a time, this module should find recent activity
/// that has occurred in glossary activities and print it out.
/// Return true if there was output, or false is there was none.
global $CFG, $USER;
//TODO: use timestamp in approved field instead of changing timemodified when approving in 2.0
$modinfo = get_fast_modinfo($course);
$ids = array();
foreach ($modinfo->cms as $cm) {
if ($cm->modname != 'glossary') {
continue;
}
if (!$cm->uservisible) {
continue;
}
$ids[$cm->instance] = $cm->instance;
}
if (!$ids) {
return false;
}
$glist = implode(',', $ids); // there should not be hundreds of glossaries in one course, right?
if (!$entries = get_records_sql("SELECT ge.id, ge.concept, ge.approved, ge.timemodified, ge.glossaryid,
ge.userid, u.firstname, u.lastname, u.email, u.picture
FROM {$CFG->prefix}glossary_entries ge
JOIN {$CFG->prefix}user u ON u.id = ge.userid
WHERE ge.glossaryid IN ($glist) AND ge.timemodified > $timestart
ORDER BY ge.timemodified ASC")) {
return false;
}
$editor = array();
foreach ($entries as $entryid=>$entry) {
if ($entry->approved) {
continue;
}
if (!isset($editor[$entry->glossaryid])) {
$editor[$entry->glossaryid] = has_capability('mod/glossary:approve', get_context_instance(CONTEXT_MODULE, $modinfo->instances['glossary'][$entry->glossaryid]->id));
}
if (!$editor[$entry->glossaryid]) {
unset($entries[$entryid]);
}
}
if (!$entries) {
return false;
}
print_headline(get_string('newentries', 'glossary').':');
$strftimerecent = get_string('strftimerecent');
foreach ($entries as $entry) {
$link = $CFG->wwwroot.'/mod/glossary/view.php?g='.$entry->glossaryid.'&mode=entry&hook='.$entry->id;
if ($entry->approved) {
$dimmed = '';
} else {
$dimmed = ' dimmed_text';
}
echo '<div class="head'.$dimmed.'">';
echo '<div class="date">'.userdate($entry->timemodified, $strftimerecent).'</div>';
echo '<div class="name">'.fullname($entry, $viewfullnames).'</div>';
echo '</div>';
echo '<div class="info"><a href="'.$link.'">'.format_text($entry->concept, true).'</a></div>';
}
return true;
}
function glossary_log_info($log) {
global $CFG;
return get_record_sql("SELECT e.*, u.firstname, u.lastname
FROM {$CFG->prefix}glossary_entries e,
{$CFG->prefix}user u
WHERE e.id = '$log->info'
AND u.id = '$log->userid'");
}
function glossary_cron () {
/// Function to be run periodically according to the moodle cron
/// This function searches for things that need to be done, such
/// as sending out mail, toggling flags etc ...
global $CFG;
return true;
}
/**
* Return grade for given user or all users.
*
* @param int $glossaryid id of glossary
* @param int $userid optional user id, 0 means all users
* @return array array of grades, false if none
*/
function glossary_get_user_grades($glossary, $userid=0) {
global $CFG;
$user = $userid ? "AND u.id = $userid" : "";
$sql = "SELECT u.id, u.id AS userid, avg(gr.rating) AS rawgrade
FROM {$CFG->prefix}user u, {$CFG->prefix}glossary_entries ge,
{$CFG->prefix}glossary_ratings gr
WHERE u.id = ge.userid AND ge.id = gr.entryid
AND gr.userid != u.id AND ge.glossaryid = $glossary->id
$user
GROUP BY u.id";
return get_records_sql($sql);
}
/**
* Update grades by firing grade_updated event
*
* @param object $glossary null means all glossaries
* @param int $userid specific user only, 0 mean all
*/
function glossary_update_grades($glossary=null, $userid=0, $nullifnone=true) {
global $CFG;
require_once($CFG->libdir.'/gradelib.php');
if ($glossary != null) {
if ($grades = glossary_get_user_grades($glossary, $userid)) {
glossary_grade_item_update($glossary, $grades);
} else if ($userid and $nullifnone) {
$grade = new object();
$grade->userid = $userid;
$grade->rawgrade = NULL;
glossary_grade_item_update($glossary, $grade);
} else {
glossary_grade_item_update($glossary);
}
} else {
$sql = "SELECT g.*, cm.idnumber as cmidnumber
FROM {$CFG->prefix}glossary g, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
WHERE m.name='glossary' AND m.id=cm.module AND cm.instance=g.id";
if ($rs = get_recordset_sql($sql)) {
while ($glossary = rs_fetch_next_record($rs)) {
if ($glossary->assessed) {
glossary_update_grades($glossary, 0, false);
} else {
glossary_grade_item_update($glossary);
}
}
rs_close($rs);
}
}
}
/**
* Create/update grade item for given glossary
*
* @param object $glossary object with extra cmidnumber
* @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
* @return int, 0 if ok, error code otherwise
*/
function glossary_grade_item_update($glossary, $grades=NULL) {
global $CFG;
if (!function_exists('grade_update')) { //workaround for buggy PHP versions
require_once($CFG->libdir.'/gradelib.php');
}
if(!empty($glossary->cmidnumber)){
$params = array('itemname'=>$glossary->name, 'idnumber'=>$glossary->cmidnumber);
}else{
// MDL-14303
$cm = get_coursemodule_from_instance('glossary', $glossary->id);
$params = array('itemname'=>$glossary->name, 'idnumber'=>$cm->id);
}
if (!$glossary->assessed or $glossary->scale == 0) {
$params['gradetype'] = GRADE_TYPE_NONE;
} else if ($glossary->scale > 0) {
$params['gradetype'] = GRADE_TYPE_VALUE;
$params['grademax'] = $glossary->scale;
$params['grademin'] = 0;
} else if ($glossary->scale < 0) {
$params['gradetype'] = GRADE_TYPE_SCALE;
$params['scaleid'] = -$glossary->scale;
}
if ($grades === 'reset') {
$params['reset'] = true;
$grades = NULL;
}
return grade_update('mod/glossary', $glossary->course, 'mod', 'glossary', $glossary->id, 0, $grades, $params);
}
/**
* Delete grade item for given glossary
*
* @param object $glossary object
*/
function glossary_grade_item_delete($glossary) {
global $CFG;
require_once($CFG->libdir.'/gradelib.php');
return grade_update('mod/glossary', $glossary->course, 'mod', 'glossary', $glossary->id, 0, NULL, array('deleted'=>1));
}
function glossary_get_participants($glossaryid) {
//Returns the users with data in one glossary
//(users with records in glossary_entries, students)
global $CFG;
//Get students
$students = get_records_sql("SELECT DISTINCT u.id, u.id
FROM {$CFG->prefix}user u,
{$CFG->prefix}glossary_entries g
WHERE g.glossaryid = '$glossaryid' and
u.id = g.userid");
//Return students array (it contains an array of unique users)
return ($students);
}
function glossary_scale_used ($glossaryid,$scaleid) {
//This function returns if a scale is being used by one glossary
$return = false;
$rec = get_record("glossary","id","$glossaryid","scale","-$scaleid");
if (!empty($rec) && !empty($scaleid)) {
$return = true;
}
return $return;
}
/**
* Checks if scale is being used by any instance of glossary
*
* This is used to find out if scale used anywhere
* @param $scaleid int
* @return boolean True if the scale is used by any glossary
*/
function glossary_scale_used_anywhere($scaleid) {
if ($scaleid and record_exists('glossary', 'scale', -$scaleid)) {
return true;
} else {
return false;
}
}
//////////////////////////////////////////////////////////////////////////////////////
/// Any other glossary functions go here. Each of them must have a name that
/// starts with glossary_
//This function return an array of valid glossary_formats records
//Everytime it's called, every existing format is checked, new formats
//are included if detected and old formats are deleted and any glossary
//using an invalid format is updated to the default (dictionary).
function glossary_get_available_formats() {
global $CFG;
//Get available formats (plugin) and insert (if necessary) them into glossary_formats
$formats = get_list_of_plugins('mod/glossary/formats', 'TEMPLATE');
$pluginformats = array();
foreach ($formats as $format) {
//If the format file exists
if (file_exists($CFG->dirroot.'/mod/glossary/formats/'.$format.'/'.$format.'_format.php')) {
include_once($CFG->dirroot.'/mod/glossary/formats/'.$format.'/'.$format.'_format.php');
//If the function exists
if (function_exists('glossary_show_entry_'.$format)) {
//Acummulate it as a valid format
$pluginformats[] = $format;
//If the format doesn't exist in the table
if (!$rec = get_record('glossary_formats','name',$format)) {
//Insert the record in glossary_formats
$gf = new object();
$gf->name = $format;
$gf->popupformatname = $format;
$gf->visible = 1;
insert_record("glossary_formats",$gf);
}
}
}
}
//Delete non_existent formats from glossary_formats table
$formats = get_records("glossary_formats");
foreach ($formats as $format) {
$todelete = false;
//If the format in DB isn't a valid previously detected format then delete the record
if (!in_array($format->name,$pluginformats)) {
$todelete = true;
}
if ($todelete) {
//Delete the format
delete_records('glossary_formats','name',$format->name);
//Reasign existing glossaries to default (dictionary) format
if ($glossaries = get_records('glossary','displayformat',$format->name)) {
foreach($glossaries as $glossary) {
set_field('glossary','displayformat','dictionary','id',$glossary->id);
}
}
}
}
//Now everything is ready in glossary_formats table
$formats = get_records("glossary_formats");
return $formats;
}
function glossary_debug($debug,$text,$br=1) {
if ( $debug ) {
echo '<font color="red">' . $text . '</font>';
if ( $br ) {
echo '<br />';
}
}
}
function glossary_get_entries($glossaryid, $entrylist, $pivot = "") {
global $CFG;
if ($pivot) {
$pivot .= ",";
}
return get_records_sql("SELECT $pivot id,userid,concept,definition,format
FROM {$CFG->prefix}glossary_entries
WHERE glossaryid = '$glossaryid'
AND id IN ($entrylist)");
}
function glossary_get_entries_search($concept, $courseid) {
global $CFG;
//Check if the user is an admin
$bypassadmin = 1; //This means NO (by default)
if (has_capability('moodle/course:viewhiddenactivities', get_context_instance(CONTEXT_SYSTEM))) {
$bypassadmin = 0; //This means YES
}
//Check if the user is a teacher
$bypassteacher = 1; //This means NO (by default)
if (has_capability('mod/glossary:manageentries', get_context_instance(CONTEXT_COURSE, $courseid))) {
$bypassteacher = 0; //This means YES
}
$conceptlower = moodle_strtolower(trim($concept));
return get_records_sql("SELECT e.*, g.name as glossaryname, cm.id as cmid, cm.course as courseid
FROM {$CFG->prefix}glossary_entries e,
{$CFG->prefix}glossary g,
{$CFG->prefix}course_modules cm,
{$CFG->prefix}modules m
WHERE m.name = 'glossary' AND
cm.module = m.id AND
(cm.visible = 1 OR cm.visible = $bypassadmin OR
(cm.course = '$courseid' AND cm.visible = $bypassteacher)) AND
g.id = cm.instance AND
e.glossaryid = g.id AND
( (e.casesensitive != 0 AND LOWER(concept) = '$conceptlower') OR
(e.casesensitive = 0 and concept = '$concept')) AND
(g.course = '$courseid' OR g.globalglossary = 1) AND
e.usedynalink != 0 AND
g.usedynalink != 0");
}
function glossary_get_entries_sorted($glossary, $where="", $orderby="", $pivot = "") {
global $CFG;
if ($where) {
$where = " and $where";
}
if ($orderby) {
$orderby = " ORDER BY $orderby";
}
if ($pivot) {
$pivot .= ",";
}
return get_records_sql("SELECT $pivot *
FROM {$CFG->prefix}glossary_entries
WHERE (glossaryid = $glossary->id or sourceglossaryid = $glossary->id) $where $orderby");
}
function glossary_get_entries_by_category($glossary, $hook, $where="", $orderby="", $pivot = "") {
global $CFG;
if ($where) {
$where = " and $where";
}
if ($orderby) {
$orderby = " ORDER BY $orderby";
}
if ($pivot) {
$pivot .= ",";
}
return get_records_sql("SELECT $pivot ge.*
FROM {$CFG->prefix}glossary_entries ge, {$CFG->prefix}glossary_entries_categories c
WHERE (ge.id = c.entryidid and c.categoryid = $hook) and
(ge.glossaryid = $glossary->id or ge.sourceglossaryid = $glossary->id) $where $orderby");
}
function glossary_print_entry($course, $cm, $glossary, $entry, $mode='',$hook='',$printicons = 1, $displayformat = -1, $ratings = NULL, $printview = false) {
global $USER, $CFG;
$return = false;
if ( $displayformat < 0 ) {
$displayformat = $glossary->displayformat;
}
if ($entry->approved or ($USER->id == $entry->userid) or ($mode == 'approval' and !$entry->approved) ) {
$formatfile = $CFG->dirroot.'/mod/glossary/formats/'.$displayformat.'/'.$displayformat.'_format.php';
if ($printview) {
$functionname = 'glossary_print_entry_'.$displayformat;
} else {
$functionname = 'glossary_show_entry_'.$displayformat;
}
if (file_exists($formatfile)) {
include_once($formatfile);
if (function_exists($functionname)) {
$return = $functionname($course, $cm, $glossary, $entry,$mode,$hook,$printicons,$ratings);
} else if ($printview) {
//If the glossary_print_entry_XXXX function doesn't exist, print default (old) print format
$return = glossary_print_entry_default($entry);
}
}
}
return $return;
}
//Default (old) print format used if custom function doesn't exist in format
function glossary_print_entry_default ($entry) {
echo '<h3>'. strip_tags($entry->concept) . ': </h3>';
$definition = $entry->definition;
// always detect and strip TRUSTTEXT marker before processing and add+strip it afterwards!
if (trusttext_present($definition)) {
$ttpresent = true;
$definition = trusttext_strip($definition);
} else {
$ttpresent = false;
}
$definition = '<span class="nolink">' . strip_tags($definition) . '</span>';
// reconstruct the TRUSTTEXT properly after processing
if ($ttpresent) {
$definition = trusttext_mark($definition);
} else {
$definition = trusttext_strip($definition); //make 100% sure TRUSTTEXT marker was not created
}
$options = new object();
$options->para = false;
$options->trusttext = true;
$definition = format_text($definition, $entry->format, $options);
echo ($definition);
echo '<br /><br />';
}
/**
* Print glossary concept/term as a heading <h3>
*/
function glossary_print_entry_concept($entry) {
$options = new object();
$options->para = false;
$text = format_text(print_heading($entry->concept, '', 3, 'nolink', true), FORMAT_MOODLE, $options);
if (!empty($entry->highlight)) {
$text = highlight($entry->highlight, $text);
}
echo $text;
}
function glossary_print_entry_definition($entry) {
$definition = $entry->definition;
// always detect and strip TRUSTTEXT marker before processing and add+strip it afterwards!
if (trusttext_present($definition)) {
$ttpresent = true;
$definition = trusttext_strip($definition);
} else {
$ttpresent = false;
}
$links = array();
$tags = array();
$urls = array();
$addrs = array();
//Calculate all the strings to be no-linked
//First, the concept
$term = preg_quote(trim($entry->concept),'/');
$pat = '/('.$term.')/is';
$doNolinks[] = $pat;
//Now the aliases
if ( $aliases = get_records('glossary_alias','entryid',$entry->id) ) {
foreach ($aliases as $alias) {
$term = preg_quote(trim($alias->alias),'/');
$pat = '/('.$term.')/is';
$doNolinks[] = $pat;
}
}
//Extract <a>..><a> tags from definition
preg_match_all('/<a[^>]+?>(.*?)<\/a>/is',$definition,$list_of_a);
//Save them into links array to use them later
foreach (array_unique($list_of_a[0]) as $key=>$value) {
$links['<#'.$key.'#>'] = $value;
}
//Take off every link from definition
if ( $links ) {
$definition = str_replace($links,array_keys($links),$definition);
}
//Extract all tags from definition
preg_match_all('/(<.*?>)/is',$definition,$list_of_tags);
//Save them into tags array to use them later
foreach (array_unique($list_of_tags[0]) as $key=>$value) {
$tags['<@'.$key.'@>'] = $value;
}
//Take off every tag from definition
if ( $tags ) {
$definition = str_replace($tags,array_keys($tags),$definition);
}
//Extract all URLS with protocol (http://domain.com) from definition
preg_match_all('/([[:space:]]|^|\(|\[)([[:alnum:]]+):\/\/([^[:space:]]*)([[:alnum:]#?\/&=])/is',$definition,$list_of_urls);
//Save them into urls array to use them later
foreach (array_unique($list_of_urls[0]) as $key=>$value) {
$urls['<*'.$key.'*>'] = $value;
}
//Take off every url from definition
if ( $urls ) {
$definition = str_replace($urls,array_keys($urls),$definition);
}
//Extract all WEB ADDRESSES (www.domain.com) from definition
preg_match_all('/([[:space:]]|^|\(|\[)www\.([^[:space:]]*)([[:alnum:]#?\/&=])/is',$definition,$list_of_addresses);
//Save them into addrs array to use them later
foreach (array_unique($list_of_addresses[0]) as $key=>$value) {
$addrs['<+'.$key.'+>'] = $value;
}
//Take off every addr from definition
if ( $addrs ) {
$definition = str_replace($addrs,array_keys($addrs),$definition);
}
//Put doNolinks (concept + aliases) enclosed by <nolink> tag
$definition= preg_replace($doNolinks,'<span class="nolink">$1</span>',$definition);
//Restore addrs
if ( $addrs ) {
$definition = str_replace(array_keys($addrs),$addrs,$definition);
}
//Restore urls
if ( $urls ) {
$definition = str_replace(array_keys($urls),$urls,$definition);
}
//Restore tags
if ( $tags ) {
$definition = str_replace(array_keys($tags),$tags,$definition);
}
//Restore links
if ( $links ) {
$definition = str_replace(array_keys($links),$links,$definition);
}
$options = new object();
$options->para = false;
$options->trusttext = true;
// reconstruct the TRUSTTEXT properly after processing
if ($ttpresent) {
$definition = trusttext_mark($definition);
} else {
$definition = trusttext_strip($definition); //make 100% sure TRUSTTEXT marker was not created
}
$text = format_text($definition, $entry->format, $options);
if (!empty($entry->highlight)) {
$text = highlight($entry->highlight, $text);
}
if (isset($entry->footer)) { // Unparsed footer info
$text .= $entry->footer;
}
echo $text;
}
function glossary_print_entry_aliases($course, $cm, $glossary, $entry,$mode='',$hook='', $type = 'print') {
$return = '';
if ( $aliases = get_records('glossary_alias','entryid',$entry->id) ) {
foreach ($aliases as $alias) {
if (trim($alias->alias)) {
if ($return == '') {
$return = '<select style="font-size:8pt">';
}
$return .= "<option>$alias->alias</option>";
}
}
if ($return != '') {
$return .= '</select>';
}
}
if ($type == 'print') {
echo $return;
} else {
return $return;
}
}
function glossary_print_entry_icons($course, $cm, $glossary, $entry, $mode='',$hook='', $type = 'print') {
global $USER, $CFG;
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
$output = false; //To decide if we must really return text in "return". Activate when needed only!
$importedentry = ($entry->sourceglossaryid == $glossary->id);
$ismainglossary = $glossary->mainglossary;
$return = '<span class="commands">';
// Differentiate links for each entry.
$altsuffix = ': '.strip_tags(format_text($entry->concept));
if (!$entry->approved) {
$output = true;
$return .= get_string('entryishidden','glossary');
}
$return .= glossary_print_entry_commentslink($course, $cm, $glossary, $entry,$mode,$hook,'html');
if (has_capability('mod/glossary:comment', $context) and $glossary->allowcomments) {
$output = true;
$return .= ' <a title="' . get_string('addcomment','glossary') . '" href="comment.php?action=add&eid='.$entry->id.'"><img src="comment.gif" class="iconsmall" alt="'.get_string('addcomment','glossary').$altsuffix.'" /></a>';
}
if (has_capability('mod/glossary:manageentries', $context) or (!empty($USER->id) and has_capability('mod/glossary:write', $context) and $entry->userid == $USER->id)) {
// only teachers can export entries so check it out
if (has_capability('mod/glossary:export', $context) and !$ismainglossary and !$importedentry) {
$mainglossary = get_record('glossary','mainglossary',1,'course',$course->id);
if ( $mainglossary ) { // if there is a main glossary defined, allow to export the current entry
$output = true;
$return .= ' <a title="'.get_string('exporttomainglossary','glossary') . '" href="exportentry.php?id='.$cm->id.'&entry='.$entry->id.'&mode='.$mode.'&hook='.$hook.'"><img src="export.gif" class="iconsmall" alt="'.get_string('exporttomainglossary','glossary').$altsuffix.'" /></a>';
}
}
if ( $entry->sourceglossaryid ) {
$icon = "minus.gif"; // graphical metaphor (minus) for deleting an imported entry
} else {
$icon = "$CFG->pixpath/t/delete.gif";
}
//Decide if an entry is editable:
// -It isn't a imported entry (so nobody can edit a imported (from secondary to main) entry)) and
// -The user is teacher or he is a student with time permissions (edit period or editalways defined).
$ineditperiod = ((time() - $entry->timecreated < $CFG->maxeditingtime) || $glossary->editalways);
if ( !$importedentry and (has_capability('mod/glossary:manageentries', $context) or ($entry->userid == $USER->id and ($ineditperiod and has_capability('mod/glossary:write', $context))))) {
$output = true;
$return .= " <a title=\"" . get_string("delete") . "\" href=\"deleteentry.php?id=$cm->id&mode=delete&entry=$entry->id&prevmode=$mode&hook=$hook\"><img src=\"";
$return .= $icon;
$return .= "\" class=\"iconsmall\" alt=\"" . get_string("delete") .$altsuffix."\" /></a> ";
$return .= " <a title=\"" . get_string("edit") . "\" href=\"edit.php?id=$cm->id&e=$entry->id&mode=$mode&hook=$hook\"><img src=\"$CFG->pixpath/t/edit.gif\" class=\"iconsmall\" alt=\"" . get_string("edit") .$altsuffix. "\" /></a>";
} elseif ( $importedentry ) {
$return .= " <font size=\"-1\">" . get_string("exportedentry","glossary") . "</font>";
}
}
$return .= " "; // just to make up a little the output in Mozilla ;)
$return .= '</span>';
//If we haven't calculated any REAL thing, delete result ($return)
if (!$output) {
$return = '';
}
//Print or get
if ($type == 'print') {
echo $return;
} else {
return $return;
}
}
function glossary_print_entry_commentslink($course, $cm, $glossary, $entry,$mode,$hook, $type = 'print') {
$return = '';
$count = count_records('glossary_comments','entryid',$entry->id);
if ($count) {
$return = '';
$return .= "<a href=\"comments.php?id=$cm->id&eid=$entry->id\">$count ";
if ($count == 1) {
$return .= get_string('comment', 'glossary');
} else {
$return .= get_string('comments', 'glossary');
}
$return .= '</a>';
}
if ($type == 'print') {
echo $return;
} else {
return $return;
}
}
function glossary_print_entry_lower_section($course, $cm, $glossary, $entry, $mode, $hook,$printicons,$ratings,$aliases=true) {
if ($aliases) {
$aliases = glossary_print_entry_aliases($course, $cm, $glossary, $entry, $mode, $hook,'html');
}
$icons = '';
$return = '';
if ( $printicons ) {
$icons = glossary_print_entry_icons($course, $cm, $glossary, $entry, $mode, $hook,'html');
}
if ($aliases || $icons || $ratings) {
echo '<table>';
if ( $aliases ) {
echo '<tr valign="top"><td class="aliases">' .
get_string('aliases','glossary').': '.$aliases . '</td></tr>';
}
if ($icons) {
echo '<tr valign="top"><td class="icons">'.$icons.'</td></tr>';
}
if ($ratings) {
echo '<tr valign="top"><td class="ratings">';
$return = glossary_print_entry_ratings($course, $entry, $ratings);
echo '</td></tr>';
}
echo '</table>';
}
return $return;
}
function glossary_print_entry_attachment($entry,$format=NULL,$align="right",$insidetable=true) {
/// valid format values: html : Return the HTML link for the attachment as an icon
/// text : Return the HTML link for tha attachment as text
/// blank : Print the output to the screen
if ($entry->attachment) {
$glossary = get_record("glossary","id",$entry->glossaryid);
$entry->course = $glossary->course; //used inside print_attachment
if ($insidetable) {
echo "<table border=\"0\" width=\"100%\" align=\"$align\"><tr><td align=\"$align\" nowrap=\"nowrap\">\n";
}
echo glossary_print_attachments($entry,$format,$align);
if ($insidetable) {
echo "</td></tr></table>\n";
}
}
}
function glossary_print_entry_approval($cm, $entry, $mode,$align="right",$insidetable=true) {
global $CFG;
if ( $mode == 'approval' and !$entry->approved ) {
if ($insidetable) {
echo '<table class="glossaryapproval" align="'.$align.'"><tr><td align="'.$align.'">';
}
echo '<a title="'.get_string('approve','glossary').'" href="approve.php?id='.$cm->id.'&eid='.$entry->id.'&mode='.$mode.'"><img align="'.$align.'" src="'.$CFG->pixpath.'/i/approve.gif" style="border:0px; width:34px; height:34px" alt="'.get_string('approve','glossary').'" /></a>';
if ($insidetable) {
echo '</td></tr></table>';
}
}
}
function glossary_search($course, $searchterms, $extended = 0, $glossary = NULL) {