-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmod_forumng.php
More file actions
5840 lines (5343 loc) · 233 KB
/
mod_forumng.php
File metadata and controls
5840 lines (5343 loc) · 233 KB
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/>.
defined('MOODLE_INTERNAL') || die();
require_once(dirname(__FILE__).'/mod_forumng_utils.php');
require_once(dirname(__FILE__).'/mod_forumng_discussion.php');
require_once(dirname(__FILE__).'/mod_forumng_discussion_list.php');
require_once(dirname(__FILE__).'/mod_forumng_post.php');
require_once(dirname(__FILE__).'/mod_forumng_draft.php');
require_once(dirname(__FILE__).'/type/forumngtype.php');
require_once(dirname(__FILE__).'/feature/forumngfeature.php');
/**
* Represents a forum. This class contains:
* 1. A constructor and methods for handling information about a specific forum,
* such as obtaining a list of discussions.
* 2. Static methods related to multiple forums across the course or site, or
* to forums in general.
* @see mod_forumng_discussion_list
* @see mod_forumng_discussion
* @see mod_forumng_post
* @package mod
* @subpackage forumng
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class mod_forumng {
// Constants
/*//////////*/
/** Subscription: Nobody is allowed to subscribe to the forum. */
const SUBSCRIPTION_NOT_PERMITTED = 0;
/** Subscription: Anyone who can see the forum can choose to subscribe to it. */
const SUBSCRIPTION_PERMITTED = 1;
/** Subscription: Anybody who can see the forum can choose to subscribe to it,
and users with certain roles are automatically subscribed (but can
unsubscribe). */
const SUBSCRIPTION_INITIALLY_SUBSCRIBED = 2;
/** Subscription: Anyone who can see the forum can choose to subscribe to it.
and users with certain roles are forced to be subscribed (and cannot
unsubsribe). */
const SUBSCRIPTION_FORCED = 3;
/** NOT_SUBSCRIBED, PARTIALLY_SUBSCRIBED and FULLY_SUBSCRIBED are only used in
a none group mode or all group mode
FULLY_SUBSCRIBED_GROUPMODE (view a group page when fully subscribed),
THIS_GROUP_PARTIALLY_SUBSCRIBED(subscribed some discussions in this group),
THIS_GROUP_SUBSCRIBED, THIS_GROUP_NOT_SUBSCRIBED are only used in individual group mode.*/
const NOT_SUBSCRIBED = 0;
const PARTIALLY_SUBSCRIBED = 1;
const FULLY_SUBSCRIBED = 2;
const FULLY_SUBSCRIBED_GROUPMODE = 3;
const THIS_GROUP_PARTIALLY_SUBSCRIBED = 4;
const THIS_GROUP_SUBSCRIBED = 5;
const THIS_GROUP_NOT_SUBSCRIBED = 6;
/** Grading: No grade for this activity. */
const GRADING_NONE = 0;
/** Grading: Average of ratings. */
const GRADING_AVERAGE = 1;
/** Grading: Count of ratings. */
const GRADING_COUNT = 2;
/** Grading: Max rating. */
const GRADING_MAX = 3;
/** Grading: Min rating. */
const GRADING_MIN = 4;
/** Grading: Sum of ratings. */
const GRADING_SUM = 5;
/** Grading: Teacher grades students */
const GRADING_MANUAL = 6;
/** Feed type: No feeds provided. */
const FEEDTYPE_NONE = 0;
/** Feed type: Feed contains only the posts that start discussions. */
const FEEDTYPE_DISCUSSIONS = 1;
/** Feed type: Feed contains all forum posts. */
const FEEDTYPE_ALL_POSTS = 2;
/** Feed format: Atom */
const FEEDFORMAT_ATOM = 1;
/** Feed format: RSS */
const FEEDFORMAT_RSS = 2;
/** Mail state: Post not mailed yet. */
const MAILSTATE_NOT_MAILED = 0;
/** Mail state: Post not mailed (and is set to mail now). */
const MAILSTATE_NOW_NOT_MAILED = 4;
/** Mail state: Post already mailed. */
const MAILSTATE_MAILED = 1;
/** Mail state: Post sent in digests. */
const MAILSTATE_DIGESTED = 2;
/** Constant referring to posts from all groups. */
const ALL_GROUPS = null;
/** @var int Constant indicating that after login, require_view should get the group id */
const GET_GROUP_AFTER_LOGIN = -2;
/**
* Special constant indicating that groups are not used (does not apply
* to posts).
*/
const NO_GROUPS = -1;
/** Discussion sort: by date. */
const SORT_DATE = 0;
/** Discussion sort: by subject. */
const SORT_SUBJECT = 1;
/** Discussion sort: by author. */
const SORT_AUTHOR = 2;
/** Discussion sort: by replies. */
const SORT_POSTS = 3;
/** Discussion sort: by unread replies. */
const SORT_UNREAD = 4;
/** Discussion sort: by group. */
const SORT_GROUP = 5;
/** Obtain no unread info */
const UNREAD_NONE = 0;
/** Obtain binary (yes there are unread messages) unread info */
const UNREAD_BINARY = 1;
/** Obtain the count of unread discussions */
const UNREAD_DISCUSSIONS = 2;
/** Constant used if there is no post quota in effect */
const QUOTA_DOES_NOT_APPLY = -1;
/** Link constant: HTML link (&) */
const PARAM_HTML = 1;
/** Link constant: standard link (&) */
const PARAM_PLAIN = 2;
/** Link constant: HTML form input fields */
const PARAM_FORM = 3;
/** Link bitfield: HTML link (&) with 'guess' for clone */
const PARAM_UNKNOWNCLONE = 16;
/**
* Special parameter used when requesting a forum 'directly' from a course
* (so that we know it will either have no clone id, or the clone id will
* be the same as the cmid).
*/
const CLONE_DIRECT = -1;
/**
* Special parameter used when requesting a forum in a situation where we
* do not know what is the appropriate clone to use. In that case the
* system will 'guess' based on the user's access permissions
*/
const CLONE_GUESS = -2;
/** Discussion moderator post identity: standard post.*/
const ASMODERATOR_NO = 0;
/** Discussion moderator post identity: self as moderator.*/
const ASMODERATOR_IDENTIFY = 1;
/** Discussion moderator post identity: anonymously as moderator.*/
const ASMODERATOR_ANON = 2;
// Constants defining grading options.
const FORUMNG_NO_RATING = 0;// No grade (default).
const FORUMNG_RATING_OBSOLETE = 1;// Forumng ratings (obsolete).
const FORUMNG_STANDARD_RATING = 2;// Ratings (standard).
// Constants for web services.
const IPUD_SHORTEN_LENGTH = 160;
/** Post as normal.*/
const CANPOSTAON_NORMAL = 0;
/** Allow moderators to post anonymously. */
const CANPOSTANON_MODERATOR = 1;
/** Non-moderators always post anonymously */
const CANPOSTATON_NONMODERATOR = 2;
// Static methods
/*///////////////*/
/**
* Obtains list of available per-forum subscription type options.
* @return array Array from subscription constant (integer) => description
* in current language
*/
public static function get_subscription_options() {
return array(
self::SUBSCRIPTION_PERMITTED =>get_string('subscription_permitted', 'forumng'),
self::SUBSCRIPTION_FORCED =>get_string('subscription_forced', 'forumng'),
self::SUBSCRIPTION_INITIALLY_SUBSCRIBED =>
get_string('subscription_initially_subscribed', 'forumng'),
self::SUBSCRIPTION_NOT_PERMITTED =>
get_string('subscription_not_permitted', 'forumng'));
}
/**
* Obtains list of available per-forum feed type options.
* @return array Array from feedtype constant (integer) => description
* in current language
*/
public static function get_feedtype_options() {
return array(
self::FEEDTYPE_NONE=>get_string('feedtype_none', 'forumng'),
self::FEEDTYPE_DISCUSSIONS=>get_string('feedtype_discussions', 'forumng'),
self::FEEDTYPE_ALL_POSTS=>get_string('feedtype_all_posts', 'forumng')
);
}
/**
* Obtains list of available per-forum feed item count options.
* @return array Array from feed item value (integer) => description
* in current language (probably just the same integer)
*/
public static function get_feeditems_options() {
return array(
1=>1,
2=>2,
3=>3,
4=>4,
5=>5,
10=>10,
15=>15,
20=>20,
25=>25,
30=>30,
40=>40,
50=>50);
}
/**
* @param bool $midsentence True if the result is being used in the middle
* of a sentence (then we use 'day' rather than '1 day')
* @return array Array of available post-period options (keys) to the text
* versions of those options (values).
*/
public static function get_max_posts_period_options($midsentence = false) {
$options = array();
$options[60*60*24] = ($midsentence ? '' : '1 ') . get_string('day');
$options[60*60*24*2] = '2 '.get_string('days');
$options[60*60*24*7] = '7 '.get_string('days');
$options[60*60*24*14] = '14 '.get_string('days');
return $options;
}
/**
* @param bool $text True if we want in text format not number
* @param bool $midsentence True if the result is being used in the middle
* of a sentence (then we use 'day' rather than '1 day')
* @return mixed The number (seconds) or text description of the max-posts
* period of the current foru (only valid if there is one)
*/
public function get_max_posts_period($text = false, $midsentence = false) {
if ($text) {
$options = self::get_max_posts_period_options($midsentence);
return $options[$this->forumfields->maxpostsperiod];
} else {
return $this->forumfields->maxpostsperiod;
}
}
/**
* @return array Array of grading option => description
*/
public static function get_grading_options() {
return array (
self::GRADING_NONE => get_string('grading_none', 'forumng'),
self::GRADING_MANUAL => get_string('teacher_grades_students', 'forumng'),
self::GRADING_AVERAGE => get_string('grading_average', 'forumng'),
self::GRADING_COUNT => get_string('grading_count', 'forumng'),
self::GRADING_MAX => get_string('grading_max', 'forumng'),
self::GRADING_MIN => get_string('grading_min', 'forumng'),
self::GRADING_SUM => get_string('grading_sum', 'forumng'));
}
/**
* Options for select box canpostanon
*
* @return array
* @throws coding_exception
*/
public static function get_canpostanon_options() {
return [
self::CANPOSTAON_NORMAL => get_string('canpostanon_normal', 'forumng'),
self::CANPOSTANON_MODERATOR => get_string('canpostanon_moderator', 'forumng'),
self::CANPOSTATON_NONMODERATOR => get_string('canpostanon_nonmoderator', 'forumng'),
];
}
/** @return bool True if read-tracking is enabled */
public static function enabled_read_tracking() {
global $CFG;
return $CFG->forumng_trackreadposts ? true : false;
}
/** @return int Number of days that read-tracking data is kept for */
public static function get_read_tracking_days() {
global $CFG;
return $CFG->forumng_readafterdays;
}
/** @return int The oldest time (seconds since epoch) for which
* read-tracking data should be kept */
public static function get_read_tracking_deadline() {
return time()-self::get_read_tracking_days()*24*3600;
}
/**
* @return bool True if the current user has the option selected to
* automatically mark discussions as read
*/
public static function mark_read_automatically($userid = 0) {
$userid = mod_forumng_utils::get_real_userid($userid);
return !get_user_preferences('forumng_manualmark', '0', $userid);
}
/**
* @param int $sort SORT_xx constant
* @return string 'Sort by xxx' text in current language
*/
public static function get_sort_title($sort) {
return get_string('sortby', 'forumng', self::get_sort_field($sort));
}
/**
* @param int $sort SORT_xx constant
* @return string Title (in lower-case) of the field in current language
*/
public static function get_sort_field($sort) {
switch ($sort) {
case self::SORT_DATE:
return get_string('lastpost', 'forumng');
case self::SORT_SUBJECT:
return get_string('discussion', 'forumng');
case self::SORT_AUTHOR:
return get_string('startedby', 'forumng');
case self::SORT_POSTS:
return get_string('posts', 'forumng');
case self::SORT_UNREAD:
return get_string('unread', 'forumng');
case self::SORT_GROUP:
return get_string('group', 'forumng');
default:
throw new coding_exception("Unknown sort constant: $sort");
}
}
/**
* @param int $sort SORT_xx constant
* @return string Letter used to identify this sort type
*/
public static function get_sort_letter($sort) {
switch ($sort) {
case self::SORT_DATE: return 'd';
case self::SORT_SUBJECT: return 's';
case self::SORT_AUTHOR: return 'a';
case self::SORT_POSTS: return 'p';
case self::SORT_UNREAD: return 'u';
case self::SORT_GROUP: return 'g';
default:
throw new coding_exception("Unknown sort constant: $sort");
}
}
/**
* @param string $letter Letter used to identify sort type
* @return int SORT_xx constant
*/
public static function get_sort_code($letter) {
switch ($letter) {
case 'd' : return self::SORT_DATE;
case 's' : return self::SORT_SUBJECT;
case 'a' : return self::SORT_AUTHOR;
case 'p' : return self::SORT_POSTS;
case 'u' : return self::SORT_UNREAD;
case 'g' : return self::SORT_GROUP;
default:
throw new coding_exception("Unknown sort letter: $letter");
}
}
/**
* Obtains currently selected group for an activity, in the format that
* forum methods want. (Which is slightly different to standard Moodle.)
*
* Note: This function should not be called before require_login.
*
* @param object $cm Course-module
* @param bool $update If true, updates group based on URL parameter
* @return int Group ID; ALL_GROUPS if all groups; NO_GROUPS if no groups used
*/
public static function get_activity_group($cm, $update=false) {
$result = groups_get_activity_group($cm, $update);
if ($result === false) {
return self::NO_GROUPS;
} else if ($result === 0) {
return self::ALL_GROUPS;
} else {
return $result;
}
}
/**
* Obtains the forum type based on its 'info' object in modinfo (e.g. from
* $modinfo->instances['forumng'][1234]). Usually this comes from the
* custom data in the cm_info object.
*
* @param object $info Info object (either cm_info or something else)
* @return string Forum type
*/
private static function get_type_from_modinfo_info(cm_info $info) {
if (isset($info->forumtype)) {
// Only set when using get_modinfo_special for shared activity modules.
return $info->forumtype;
}
return $info->customdata->type;
}
/**
* Can anonymous posts
*
* 0: Normal 1: Moderator 2: Non moderator
* @return int Anonymous posts
*/
public function get_can_post_anon() {
return $this->forumfields->canpostanon;
}
// Object variables and accessors
/*///////////////////////////////*/
private $course, $cm, $context, $clonecourse, $clonecm, $clonecontext,
$forumfields, $type, $cache;
/** @return bool True if ratings are enabled */
public function has_ratings() {
return $this->forumfields->ratingscale!=0;
}
/**
* @param int $created Date that post was created; use 0 to obtain
* a 'general' value supposing that posts are in range
* @return bool True if current user can rate a post in this forum
*/
public function can_rate($created=0) {
return $this->has_ratings()
&& ($created == 0 || $created > $this->forumfields->ratingfrom)
&& ($created == 0 || $this->forumfields->ratinguntil==0
|| $created<$this->forumfields->ratinguntil)
&& has_capability('mod/forumng:rate', $this->get_context());
}
/**
* @return bool True if current user can grade a user
*/
public function can_grade() {
return $this->get_grading() == self::GRADING_MANUAL
&& has_capability('mod/forumng:grade', $this->get_context());
}
/** @return int ID of course that contains this forum */
public function get_course_id() {
return $this->forumfields->course;
}
/**
* Obtains course object. For non-shared forums this is
* straightforward. For shared forums this usually returns the course
* of the *clone* forum that is currently relevant, not directly of the
* original forum.
* @param bool $forcereal If set, always returns the course of the
* original forum and not of any clone
* @return object Course object
*/
public function get_course($forcereal = false) {
global $DB;
if ($this->is_shared() && !$forcereal) {
if (!$this->clonecourse) {
$cm = $this->get_course_module();
$this->clonecourse = $DB->get_record('course', array('id' => $cm->course));
if (!$this->clonecourse) {
throw new coding_exception('Cannot find clone course ' .
$cm->course);
}
}
return $this->clonecourse;
}
return $this->course;
}
/**
* Obtains course-module id. For non-shared forums this is
* straightforward. For shared forums this usually returns the id
* of the *clone* forum that is currently relevant, not directly of the
* original forum.
* @param bool $forcereal If set, always returns the id of the
* original forum and not of any clone
* @return int ID of course-module instance
*/
public function get_course_module_id($forcereal = false) {
return $this->get_course_module($forcereal)->id;
}
/**
* Obtains course-module instance. For non-shared forums this is
* straightforward. For shared forums this usually returns the course-module
* of the *clone* forum that is currently relevant, not directly of the
* original forum.
* @param bool $forcereal If set, always returns the course-module of the
* original forum and not of any clone
* @return cm_info Course-module instance
*/
public function get_course_module($forcereal = false) {
global $CFG, $SESSION;
if (empty($this->cm)) {
throw new coding_exception('Course-module not set for this forum');
}
if ($this->is_shared() && !$forcereal) {
if (!$this->clonecm) {
throw new coding_exception('Clone reference not defined');
}
return $this->clonecm;
}
return $this->cm;
}
/**
* Retrieves basic details for all the clones of this forum. (If any.)
* @return array Array of objects (each one has ->context, ->courseid,
* ->courseshortname, ->forumname, and ->sectionid) for clones of this
* forum
*/
public function get_clone_details() {
global $DB;
$recs = $DB->get_records_sql("
SELECT
x.*, c.id AS courseid, c.shortname AS courseshortname, f.name AS forumname,
f.id AS cloneforumngid, cm.section AS sectionid
FROM
{forumng} f
INNER JOIN {course_modules} cm ON f.id = cm.instance
INNER JOIN {course} c ON cm.course = c.id
INNER JOIN {modules} m ON cm.module = m.id
INNER JOIN {context} x ON x.instanceid = cm.id
WHERE
f.originalcmid = ?
AND m.name = 'forumng'
AND x.contextlevel = 70
ORDER BY
c.shortname, f.name", array($this->cm->id));
$contexts = array();
foreach ($recs as $id => $rec) {
$context = (object)array('courseid' => $rec->courseid,
'courseshortname' => $rec->courseshortname, 'forumname' => $rec->forumname,
'cloneforumngid' => $rec->cloneforumngid, 'sectionid' => $rec->sectionid);
$context->context = mod_forumng_context_access::create_instance_from_record_public($rec);
$contexts[$id] = $context;
}
return $contexts;
}
/**
* Sets up the clone reference. The clone reference is used for shared
* forums only. If a forum is a shared forum, you can access it from several
* different course-module instances. The id of these instances is known as
* the 'clone id'. We store the clone course-module in the forum object
* so that when displaying links etc., these can retain the clone
* information.
* @param int $cloneid Clone id
* @param object $clonecourse Optional clone course object (improves
* performance in cases where it needs to get the cm entry)
*/
public function set_clone_reference($cloneid, $clonecourse=null) {
global $SESSION, $DB;
if ($cloneid == $this->cm->id || $cloneid == self::CLONE_DIRECT) {
$this->clonecm = $this->cm;
return;
}
if ($cloneid == self::CLONE_GUESS) {
// We had better cache guesses in session because this is
// time-consuming
if (!isset($SESSION->forumng_cache)) {
$SESSION->forumng_cache = new stdClass;
}
if (!isset($SESSION->forumng_cache->guesses)) {
$SESSION->forumng_cache->guesses = array();
}
if (isset($SESSION->forumng_cache->guesses[$this->get_id()])) {
return $SESSION->forumng_cache->guesses[$this->get_id()];
}
// Okay, no cached guess. First let's see if they can write to the
// original forum because if so let's just use that
if (has_capability('mod/forumng:replypost', $this->get_context(true))) {
$this->clonecm = $this->cm;
return;
}
// See if they can write to any context
$clones = $this->get_clone_details();
foreach ($clones as $clone) {
if (has_capability('mod/forumng:replypost', $clone->context)) {
$this->clonecm = self::get_modinfo_cm(
$clone->context->instanceid);
break;
}
}
// No? Well see if they can read to one
if (!$this->clonecm) {
foreach ($clones as $clone) {
if (has_capability('moodle/course:view', $clone->context)) {
$this->clonecm = self::get_modinfo_cm($clone->context->instanceid);
break;
}
}
}
// Default, just use original
if (!$this->clonecm) {
$this->clonecm = $this->cm;
}
// Cache guess
$SESSION->forumng_cache->guesses[$this->get_id()] = $this->clonecm;
return;
} else {
// Get course-module record
$this->clonecm = self::get_modinfo_cm($cloneid);
// Security check that specifed cm is indeed a clone of this forum
if ($DB->get_field('forumng', 'originalcmid', array('id' =>
$this->clonecm->instance)) != $this->cm->id) {
throw new coding_exception("Not a clone of this forum: $cloneid");
}
}
}
/**
* Gets a course-module object using get_fast_modinfo (so that it includes
* additional data not in the actual table).
* @param int $cmid ID of course-module
* @param object $course Optional $course object to improve performance
* @return cm_info Course-module object
* @throws mod_forumng_exception If the cm isn't found or not in that course
*/
private static function get_modinfo_cm($cmid, $course=null) {
global $DB;
if (!$course) {
$course = $DB->get_record_sql("
SELECT
c.*
FROM
{course_modules} cm
INNER JOIN {course} c ON c.id = cm.course
WHERE
cm.id = ?", array($cmid), MUST_EXIST);
}
$modinfo = get_fast_modinfo($course);
return $modinfo->get_cm($cmid);
}
/**
* Obtains context object. For non-shared forums this is
* straightforward. For shared forums this usually returns the context
* of the *clone* forum that is currently relevant, not directly of the
* original forum.
* @param bool $forcereal If set, always returns the context of the
* original forum and not of any clone
* @return context Context object
*/
public function get_context($forcereal = false) {
if ($this->is_shared() && !$forcereal) {
if (!$this->clonecontext) {
$this->clonecontext = context_module::instance($this->get_course_module_id());
}
return $this->clonecontext;
}
return $this->context;
}
/** @return int ID of this forum */
public function get_id() {
return $this->forumfields->id;
}
/** @return Name of forum */
public function get_name() {
return $this->forumfields->name;
}
/** @return reporting email of form */
public function get_reportingemail() {
return $this->forumfields->reportingemail;
}
/** @return array of reporting emails of forum */
public function get_reportingemails() {
global $CFG;
$recipients = $this->get_reportingemail();
if (!empty($recipients)) {
$recipients = explode(';', $recipients);
} else {
$recipients = array();
}
if (!empty($CFG->forumng_reportunacceptable)) {
// Check to see whether global forum report e-mail is already in recipients.
if (!in_array($CFG->forumng_reportunacceptable, $recipients)) {
// Add global recipient address to recipents array.
$recipients[] = $CFG->forumng_reportunacceptable;
}
}
return $recipients;
}
/** @return posting from of form */
public function get_postingfrom() {
return $this->forumfields->postingfrom;
}
/** @return posting until of form */
public function get_postinguntil() {
return $this->forumfields->postinguntil;
}
/**
* @param $abbreviated If true, cuts down the length
* @return string Introduction text
*/
public function get_introduction() {
return $this->forumfields->introduction;
}
/**
* @return int FORMAT_xx constant for introduction format
*/
public function get_introduction_format() {
return $this->forumfields->introductionformat;
}
/** @return int GRADING_xx constant */
public function get_grading() {
return $this->forumfields->grading;
}
/**
* @return int Scale used for ratings; 0 = disable,
* positive integer = 0..N scale, negative integer = defined scale
*/
public function get_grading_scale() {
return $this->forumfields->gradingscale;
}
/**
* @return int Scale used for ratings; 0 = disable,
* positive integer = 0..N scale, negative integer = defined scale
*/
public function get_rating_scale() {
return $this->forumfields->ratingscale;
}
/**
* @return array Array (in choose_from_menu format) of available rating
* options as value=>text
*/
public function get_rating_options() {
return mod_forumng_utils::make_grades_menu($this->forumfields->ratingscale);
}
/**
* @return int Number of ratings a post must have in order to 'count'
*/
public function get_rating_threshold() {
return $this->forumfields->ratingthreshold;
}
/**
* @return bool True if this forum is shared (has the 'allow sharing' flag
* set)
*/
public function is_shared() {
return $this->forumfields->shared ? true : false;
}
/**
* @return bool True if this forum is a clone (has the 'original cmid'
* value set)
*/
public function is_clone() {
return $this->forumfields->originalcmid != null;
}
/**
* If this forum is a clone, obtains the real one; otherwise just returns
* this again.
* @return mod_forumng Forum object (same or different)
*/
public function get_real_forum() {
if ($this->is_clone()) {
return self::get_from_cmid($this->forumfields->originalcmid, $this->cm->id);
} else {
return $this;
}
}
/**
* @return int Number of discussions containing unread posts
*/
public function get_num_unread_discussions() {
if (!isset($this->forumfields->numunreaddiscussions)) {
throw new coding_exception('Unread discussion count not obtained');
}
return $this->forumfields->numunreaddiscussions;
}
/**
* @return int Number of discussions
*/
public function get_num_discussions() {
if (!isset($this->forumfields->numdiscussions)) {
throw new coding_exception('Discussion count not obtained');
}
return $this->forumfields->numdiscussions;
}
/**
* @return bool True if any discussions have unread posts
*/
public function has_unread_discussions() {
if (isset($this->forumfields->numunreaddiscussions)) {
return $this->forumfields->numunreaddiscussions > 0;
} else if (isset($this->forumfields->hasunreaddiscussions)) {
return $this->forumfields->hasunreaddiscussions > 0;
} else {
throw new coding_exception('Unread discussion flag not obtained');
}
}
/**
* Get mod/forumng:ignorefilesizelimits setting value.
*
* @return bool
*/
public function is_ignore_filesize_limit(): bool {
return has_capability('mod/forumng:ignorefilesizelimits', $this->get_context());
}
/**
* It seems a bit of a problem that we chose -1 to represent no attachments (which seems a mistake)
* when this means unlimited size (USER_CAN_IGNORE_FILE_SIZE_LIMITS).
*
* @return int Max bytes for attachments or -1 if upload is prevented
*/
public function get_max_bytes(): int {
if (!$this->is_ignore_filesize_limit() && $this->forumfields->attachmentmaxbytes) {
if ($this->forumfields->attachmentmaxbytes == -1) {
return -1;
} else {
return get_user_max_upload_file_size($this->get_context(),
$this->forumfields->attachmentmaxbytes);
}
} else {
return get_user_max_upload_file_size($this->get_context(),
$this->get_course()->maxbytes);
}
}
/**
* @return int Activity group mode; may be VISIBLEGROUPS, SEPARATEGROUPS, or 0
*/
public function get_group_mode() {
if ($this->forumfields->shared) {
// Performance up: shared forums never have groups
return 0;
}
return groups_get_activity_groupmode($this->get_course_module(),
$this->get_course());
}
/**
* @return int Grouping in use for this activity; 0 for 'all groupings'
*/
public function get_grouping() {
return $this->get_course_module()->groupingid;
}
/** @return bool True if either site level or forum level reporting email is not null */
public function has_reporting_email() {
global $CFG;
return $this->forumfields->reportingemail!= null ||
(!empty($CFG->forumng_reportunacceptable) &&
validate_email($CFG->forumng_reportunacceptable));
}
/**
* Use to obtain link parameters when linking to any page that has anything
* to do with forums.
* @return string e.g. 'id=1234'
*/
public function get_link_params($type) {
if ($type == self::PARAM_FORM) {
$id = '<input type="hidden" name="id" value="' . $this->cm->id . '" />';
} else {
$id = 'id=' . $this->cm->id;
}
return $id . $this->get_clone_param($type);
}
/**
* Use to obtain link parameters as an array instead of as a string.
* @return array e.g. ('id'=>123)
*/
public function get_link_params_array() {
$result = array('id' => $this->cm->id);
$this->add_clone_param_array($result);
return $result;
}
/**
* Adds the clone parameter to an array of parameters, if it is necessary.
* @param array $result Array that may have key 'clone' set
*/
public function add_clone_param_array(&$result) {
if ($this->is_shared()) {
$result['clone'] = $this->get_course_module_id();
}
}
/**
* @param int $type PARAMS_xx constant
* @return string Full URL to this forum
*/
public function get_url($type) {
global $CFG;
return $CFG->wwwroot . '/mod/forumng/view.php?' .
$this->get_link_params($type);
}
/**
* @param int $type Parameter type (whether you want it escaped or not)
* @return Either empty string or some variant of '&clone=N'
*/
public function get_clone_param($type) {
if (!$this->is_shared()) {
return '';
}
if ($type & self::PARAM_UNKNOWNCLONE) {
$cloneid = -2; // Special 'guess' vale
} else {
$cloneid = $this->get_course_module_id();
}
if ($type == self::PARAM_FORM) {
return '<input type="hidden" name="clone" value="' .
$cloneid . '" />';
}
if (($type & 0xf) == self::PARAM_HTML) {
$params = '&';
} else {
$params = '&';
}
return $params . 'clone=' . $cloneid;
}
/** @return int forum ratings enabled */
public function get_enableratings() {
return $this->forumfields->enableratings;
}
/** @return int forum ratings from */
public function get_ratingfrom() {
return $this->forumfields->ratingfrom;
}
/** @return int forum ratings until */
public function get_ratinguntil() {
return $this->forumfields->ratinguntil;
}
// Factory methods
/*////////////////*/
/**
* Creates a forum object and all related data from a single forum ID.
* Note this is a forum ID and not a course-module ID.
* @param int $id ID of forum
* @param int $cloneid Clone identifier (0 if not a shared forum) or
* CLONE_DIRECT constant
* @param bool $requirecm True if we require that the forum object
* has a valid course-module and context; false if the forum has only
* just been created so it doesn't have one yet
* @param object $passcm Optional $cm object. Can be used in cases where
* get_fast_modinfo will fail (during course deletion).
* @param int $userid Use particular user for modinfo call
* @return mod_forumng Forum object
*/