forked from aspark21/moodle-mod_hsuforum
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrenderer.php
2079 lines (1847 loc) · 84.2 KB
/
renderer.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 file contains a custom renderer class used by the forum module.
*
* @package mod_hsuforum
* @copyright 2009 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @copyright Copyright (c) 2012 Open LMS (https://www.openlms.net)
* @author Mark Nielsen
*/
use mod_hsuforum\local;
use mod_hsuforum\renderables\advanced_editor;
require_once(__DIR__.'/lib/discussion/subscribe.php');
require_once($CFG->dirroot.'/lib/formslib.php');
require_once($CFG->dirroot . '/grade/grading/lib.php');
/**
* A custom renderer class that extends the plugin_renderer_base and
* is used by the forum module.
*
* @package mod_hsuforum
* @copyright 2009 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @copyright Copyright (c) 2012 Open LMS (https://www.openlms.net)
* @author Mark Nielsen
**/
class mod_hsuforum_renderer extends plugin_renderer_base {
/**
* @param $course
* @param $cm
* @param $forum
* @param context_module $context
* @author Mark Nielsen
*/
public function view($course, $cm, $forum, context_module $context) {
global $USER, $DB, $OUTPUT;
require_once(__DIR__.'/lib/discussion/sort.php');
$config = get_config('hsuforum');
$mode = optional_param('mode', 0, PARAM_INT); // Display mode (for single forum)
$page = optional_param('page', 0, PARAM_INT); // which page to show
$forumicon = "<img src='".$OUTPUT->image_url('icon', 'hsuforum')."' alt='' class='iconlarge activityicon'/> ";
echo '<div id="hsuforum-header"><h2>'.$forumicon.format_string($forum->name).'</h2>';
if (!empty($forum->intro)) {
echo '<div class="hsuforum_introduction">'.format_module_intro('hsuforum', $forum, $cm->id).'</div>';
}
echo "</div>";
// Update activity group mode changes here.
groups_get_activity_group($cm, true);
$dsort = hsuforum_lib_discussion_sort::get_from_session($forum, $context);
$dsort->set_key(optional_param('dsortkey', $dsort->get_key(), PARAM_ALPHA));
hsuforum_lib_discussion_sort::set_to_session($dsort);
if (!empty($forum->blockafter) && !empty($forum->blockperiod)) {
$a = new stdClass();
$a->blockafter = $forum->blockafter;
$a->blockperiod = get_string('secondstotime'.$forum->blockperiod);
echo $OUTPUT->notification(get_string('thisforumisthrottled', 'hsuforum', $a));
}
if ($forum->type == 'qanda' && !has_capability('moodle/course:manageactivities', $context)) {
echo $OUTPUT->notification(get_string('qandanotify','hsuforum'));
}
switch ($forum->type) {
case 'blog':
hsuforum_print_latest_discussions($course, $forum, -1, 'd.pinned DESC, p.created DESC', -1, -1, $page, $config->manydiscussions, $cm);
break;
case 'eachuser':
if (hsuforum_user_can_post_discussion($forum, null, -1, $cm)) {
echo '<p class="mdl-align">';
print_string("allowsdiscussions", "hsuforum");
echo '</p>';
}
// Fall through to following cases.
default:
hsuforum_print_latest_discussions($course, $forum, -1, $dsort->get_sort_sql(), -1, -1, $page, $config->manydiscussions, $cm);
break;
}
}
/**
* Render all discussions view, including add discussion button, etc...
*
* @param stdClass $forum - forum row
* @return string
*/
public function render_discussionsview($forum) {
global $CFG, $DB, $PAGE, $SESSION, $USER;
ob_start(); // YAK! todo, fix this rubbish.
require_once($CFG->dirroot.'/mod/hsuforum/lib.php');
require_once($CFG->libdir.'/completionlib.php');
require_once($CFG->libdir.'/accesslib.php');
$output = '';
$modinfo = get_fast_modinfo($forum->course);
$forums = $modinfo->get_instances_of('hsuforum');
if (!isset($forums[$forum->id])) {
print_error('invalidcoursemodule');
}
$cm = $forums[$forum->id];
$id = $cm->id; // Forum instance id (id in course modules table)
$f = $forum->id; // Forum ID
$config = get_config('hsuforum');
if ($id) {
if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
print_error('coursemisconf');
}
} else if ($f) {
if (! $course = $DB->get_record("course", array("id" => $forum->course))) {
print_error('coursemisconf');
}
// move require_course_login here to use forced language for course
// fix for MDL-6926
require_course_login($course, true, $cm);
} else {
print_error('missingparameter');
}
$context = \context_module::instance($cm->id);
if (!empty($CFG->enablerssfeeds) && !empty($config->enablerssfeeds) && $forum->rsstype && $forum->rssarticles) {
require_once("$CFG->libdir/rsslib.php");
$rsstitle = format_string($course->shortname, true, array('context' => \context_course::instance($course->id))) . ': ' . format_string($forum->name);
rss_add_http_header($context, 'mod_hsuforum', $forum, $rsstitle);
}
// Mark viewed if required
$completion = new \completion_info($course);
$completion->set_module_viewed($cm);
/// Some capability checks.
if (empty($cm->visible) and !has_capability('moodle/course:viewhiddenactivities', $context)) {
notice(get_string("activityiscurrentlyhidden"));
}
if (!has_capability('mod/hsuforum:viewdiscussion', $context)) {
notice(get_string('noviewdiscussionspermission', 'hsuforum'));
}
// Mark viewed and trigger the course_module_viewed event.
hsuforum_view($forum, $course, $cm, $context);
if (!defined(AJAX_SCRIPT) || !AJAX_SCRIPT) {
// Return here if we post or set subscription etc (but not if we are calling this via ajax).
$SESSION->fromdiscussion = qualified_me();
}
$PAGE->requires->js_init_call('M.mod_hsuforum.init', null, false, $this->get_js_module());
$output .= $this->svg_sprite();
$this->view($course, $cm, $forum, $context);
// Don't allow non logged in users, or guest to try to manage subscriptions.
if (isloggedin() && !isguestuser()) {
$forumobject = $DB->get_record("hsuforum", ["id" => $PAGE->cm->instance]);
// Url's for different options in the discussion.
$manageforumsubscriptionsurl = new \moodle_url('/mod/hsuforum/index.php', ['id' => $course->id]);
$exporturl = new \moodle_url('/mod/hsuforum/route.php', ['contextid' => $context->id, 'action' => 'export']);
$viewpostersurl = new \moodle_url('/mod/hsuforum/route.php', ['contextid' => $context->id, 'action' => 'viewposters']);
$subscribeforumurl = new \moodle_url('/mod/hsuforum/subscribe.php', ['id' => $forum->id, 'sesskey' => sesskey()]);
// Strings for the Url's.
$manageforumsubscriptions = get_string('manageforumsubscriptions', 'mod_hsuforum');
$exportdiscussions = get_string('export', 'mod_hsuforum');
$viewposters = get_string('viewposters', 'mod_hsuforum');
if (!hsuforum_is_subscribed($USER->id, $forumobject)) {
$subscribe = get_string('subscribe', 'hsuforum');
} else {
$subscribe = get_string('unsubscribe', 'hsuforum');
}
// We need to verify that these outputs only appears for Snap, Boost will only display the manage forum subscriptions link.
if (get_config('core', 'theme') == 'snap') {
// Outputs for the Url's inside divs to have a correct position inside the page.
$output .= '<div class="text-right"><hr>';
$output .= '<div class="managesubscriptions-url">';
$output .= \html_writer::link($manageforumsubscriptionsurl, $manageforumsubscriptions, ['class' => 'btn btn-link']);
$output .= '</div>';
$output .= '<div class="exportdiscussions-url">';
$output .= \html_writer::link($exporturl, $exportdiscussions, ['class' => 'btn btn-link']);
$output .= '</div>';
$output .= '<div class="viewposters-url">';
$output .= \html_writer::link($viewpostersurl, $viewposters, ['class' => 'btn btn-link']);
$output .= '</div>';
$output .= '<div class="subscribeforum-url">';
$output .= \html_writer::link($subscribeforumurl, $subscribe, ['class' => 'btn btn-link']);
$output .= '</div>';
$output .= '</div>';
} else {
$output .= '<div class="text-right"><hr>';
$output .= '<div class="managesubscriptions-url">';
$output .= \html_writer::link($manageforumsubscriptionsurl, $manageforumsubscriptions, ['class' => 'btn btn-link']);
$output .= '</div>';
$output .= '</div>';
}
}
if (!empty($CFG->mod_hsuforum_grading_interface)) {
$gradingmanager = get_grading_manager($context, 'mod_hsuforum', 'posts');
$gradingcontrollerpreview = '';
if ($gradingmethod = $gradingmanager->get_active_method()) {
$controller = $gradingmanager->get_controller($gradingmethod);
if ($controller->is_form_defined()) {
$gradingcontrollerpreview = $controller->render_preview($PAGE);
if ($gradingcontrollerpreview) {
$output .= '<div class="text-right">';
$output .= \html_writer::link('#hsuforum_gradingcriteria', get_string('gradingmethodpreview', 'hsuforum'),
['class' => 'btn btn-link text-right', 'data-toggle' => 'collapse', 'role' => 'button', 'aria-expanded' => 'false',
'aria-controls' => 'hsuforum_gradingcriteria']);
$output .= '</div>';
$output .= '<div class="row">
<div class="col">
<div class="collapse multi-collapse" id="hsuforum_gradingcriteria">
<div class="card card-body">
'. $gradingcontrollerpreview .'
</div>
</div>
</div>
</div>';
}
}
}
}
$output = ob_get_contents().$output;
ob_end_clean();
return ($output);
}
/**
* Render a list of discussions
*
* @param \stdClass $cm The forum course module
* @param array $discussions A list of discussion and discussion post pairs, EG: array(array($discussion, $post), ...)
* @param array $options Display options and information, EG: total discussions, page number and discussions per page
* @return string
*/
public function discussions($cm, array $discussions, array $options) {
$output = '<div class="hsuforum-new-discussion-target"></div>';
foreach ($discussions as $discussionpost) {
list($discussion, $post) = $discussionpost;
$output .= $this->discussion($cm, $discussion, $post, false, array(), null, true);
}
// TODO - this is confusing code
return $this->notification_area().
$this->output->container('', 'hsuforum-add-discussion-target').
html_writer::tag('section', $output, array('role' => 'region', 'aria-label' => get_string('discussions', 'hsuforum'), 'class' => 'hsuforum-threads-wrapper', 'tabindex' => '-1')).
$this->article_assets($cm);
}
/**
* Render a single, stand alone discussion
*
* This is very similar to discussion(), but allows for
* wrapping a single discussion in extra renderings
* when the discussion is the only thing being viewed
* on the page.
*
* @param \stdClass $cm The forum course module
* @param \stdClass $discussion The discussion to render
* @param \stdClass $post The discussion's post to render
* @param \stdClass[] $posts The discussion posts
* @param null|boolean $canreply If the user can reply or not (optional)
* @return string
*/
public function discussion_thread($cm, $discussion, $post, array $posts, $canreply = null) {
$output = $this->discussion($cm, $discussion, $post, true, $posts, $canreply);
$output .= $this->article_assets($cm);
return $output;
}
/**
* Render a single discussion
*
* Optionally also render the discussion's posts
*
* @param \stdClass $cm The forum course module
* @param \stdClass $discussion The discussion to render
* @param \stdClass $post The discussion's post to render
* @param \stdClass[] $posts The discussion posts (optional)
* @param null|boolean $canreply If the user can reply or not (optional)
* @param null|boolean $hidethreadcontent for main view(optional)
* @return string
*/
public function discussion($cm, $discussion, $post, $fullthread, array $posts = array(), $canreply = null, $hidethreadcontent = null) {
global $DB, $PAGE, $USER;
$forum = hsuforum_get_cm_forum($cm);
$postuser = hsuforum_extract_postuser($post, $forum, context_module::instance($cm->id));
$postuser->user_picture->size = 100;
$course = hsuforum_get_cm_course($cm);
if (is_null($canreply)) {
$canreply = hsuforum_user_can_post($forum, $discussion, null, $cm, $course, context_module::instance($cm->id));
}
// Meta properties, sometimes don't exist.
if (!property_exists($discussion, 'replies')) {
if (!empty($posts)) {
$discussion->replies = count($posts) - 1;
} else {
$discussion->replies = 0;
}
} else if (empty($discussion->replies)) {
$discussion->replies = 0;
}
if (!property_exists($discussion, 'unread') or empty($discussion->unread)) {
$discussion->unread = '-';
}
$format = get_string('articledateformat', 'hsuforum');
$groups = groups_get_all_groups($course->id, 0, $cm->groupingid);
$group = '';
if (groups_get_activity_groupmode($cm, $course) > 0 && isset($groups[$discussion->groupid])) {
$group = $groups[$discussion->groupid];
$group = format_string($group->name);
}
$data = new stdClass;
$data->context = context_module::instance($cm->id);
$data->id = $discussion->id;
$data->postid = $post->id;
$data->unread = $discussion->unread;
$data->fullname = $postuser->fullname;
$data->subject = $this->raw_post_subject($post);
$data->message = $this->post_message($post, $cm);
$data->created = userdate($post->created, $format);
$data->modified = userdate($discussion->timemodified, $format);
$data->pinned = $discussion->pinned;
$data->replies = $discussion->replies;
$data->replyavatars = array();
if ($data->replies > 0) {
// Get actual replies
$userfieldsapi = \core_user\fields::for_userpic();
$fields = $userfieldsapi->get_sql('u', false, '', '', false)->selects;
$sql = "SELECT $fields, hp.max
FROM {user} u
JOIN (
SELECT userid, max(modified) as max
FROM {hsuforum_posts}
WHERE privatereply = 0 AND discussion = ?
GROUP BY userid
) hp ON hp.userid = u.id
ORDER BY hp.max DESC";
$replyusers = $DB->get_records_sql($sql, array($discussion->id));
if (!empty($replyusers) && !$forum->anonymous) {
foreach ($replyusers as $replyuser) {
if ($replyuser->id === $postuser->id) {
continue; // Don't show the posters avatar in the reply section.
}
$replyuser->imagealt = fullname($replyuser);
$data->replyavatars[] = $this->output->user_picture($replyuser, array('link' => false, 'size' => 100));
}
}
}
$data->group = $group;
$data->imagesrc = $postuser->user_picture->get_url($this->page)->out();
$data->userurl = $this->get_post_user_url($cm, $postuser);
$data->viewurl = new moodle_url('/mod/hsuforum/discuss.php', array('d' => $discussion->id));
$data->tools = implode(' ', $this->post_get_commands($post, $discussion, $cm, $canreply));
$data->postflags = implode(' ',$this->post_get_flags($post, $cm, $discussion->id));
$data->subscribe = '';
$data->posts = '';
$data->fullthread = $fullthread;
$data->revealed = false;
$data->rawcreated = $post->created;
$data->rawmodified = $discussion->timemodified;
if ($forum->anonymous
&& $postuser->id === $USER->id
&& $post->reveal) {
$data->revealed = true;
}
if ($fullthread && $canreply) {
$data->replyform = html_writer::tag(
'div', $this->simple_edit_post($cm, false, $post->id), array('class' => 'hsuforum-footer-reply')
);
} else {
$data->replyform = '';
}
if ($fullthread) {
$data->posts = $this->posts($cm, $discussion, $posts, $canreply);
}
$subscribe = new hsuforum_lib_discussion_subscribe($forum, context_module::instance($cm->id));
$data->subscribe = $this->discussion_subscribe_link($cm, $discussion, $subscribe) ;
$config = get_config('hsuforum');
$timeddiscussion = !empty($config->enabletimedposts) && ($discussion->timestart || $discussion->timeend);
$timedoutsidewindow = ($timeddiscussion && ($discussion->timestart > time() || ($discussion->timeend != 0 && $discussion->timeend < time())));
$canviewhiddentimedposts = has_capability('mod/hsuforum:viewhiddentimedposts', context_module::instance($cm->id));
$canalwaysseetimedpost = ($USER->id == $postuser->id) || $canviewhiddentimedposts;
if ($timeddiscussion && $canalwaysseetimedpost) {
$data->timed = $PAGE->get_renderer('mod_hsuforum')->timed_discussion_tooltip($discussion, empty($timedoutsidewindow));
} else {
$data->timed = '';
}
return $this->discussion_template($data, $forum->type, $hidethreadcontent);
}
public function article_assets($cm) {
$context = context_module::instance($cm->id);
$this->article_js($context);
if (!isloggedin()) {
return '';
}
$output = html_writer::tag(
'script',
$this->simple_edit_post($cm),
array('type' => 'text/template', 'id' => 'hsuforum-reply-template')
);
$output .= html_writer::tag(
'script',
$this->simple_edit_discussion($cm),
array('type' => 'text/template', 'id' => 'hsuforum-discussion-template')
);
return $output;
}
/**
* Render a single post
*
* @param \stdClass $cm The forum course module
* @param \stdClass $discussion The post's discussion
* @param \stdClass $post The post to render
* @param bool $canreply
* @param null|object $parent Optional, parent post
* @param array $commands Override default post commands
* @param int $depth Depth of the post
* @return string
*/
public function post($cm, $discussion, $post, $canreply = false, $parent = null, $commands = array(), $depth = 0, $search = '') {
global $USER, $CFG, $DB;
$forum = hsuforum_get_cm_forum($cm);
if (!hsuforum_user_can_see_post($forum, $discussion, $post, null, $cm)) {
// Return a message about why you cannot see the post
return "<div class='hsuforum-post-content-hidden'>".get_string('forumbodyhidden','hsuforum')."</div>";
}
if ($commands === false){
$commands = array();
} else if (empty($commands)) {
$commands = $this->post_get_commands($post, $discussion, $cm, $canreply, false);
} else if (!is_array($commands)){
throw new coding_exception('$commands must be false, empty or populated array');
}
$postuser = hsuforum_extract_postuser($post, $forum, context_module::instance($cm->id));
$postuser->user_picture->size = 100;
// $post->breadcrumb comes from search btw.
$data = new stdClass;
$data->id = $post->id;
$data->discussionid = $discussion->id;
$data->fullname = $postuser->fullname;
$data->subject = property_exists($post, 'breadcrumb') ? $post->breadcrumb : $this->raw_post_subject($post);
$data->message = $this->post_message($post, $cm, $search);
$data->created = userdate($post->created, get_string('articledateformat', 'hsuforum'));
$data->rawcreated = $post->created;
$data->privatereply = $post->privatereply;
$data->imagesrc = $postuser->user_picture->get_url($this->page)->out();
$data->userurl = $this->get_post_user_url($cm, $postuser);
$data->unread = empty($post->postread) ? true : false;
$data->permalink = new moodle_url('/mod/hsuforum/discuss.php#p'.$post->id, array('d' => $discussion->id));
$data->isreply = false;
$data->parentfullname = '';
$data->parentuserurl = '';
$data->tools = implode(' ', $commands);
$data->postflags = implode(' ',$this->post_get_flags($post, $cm, $discussion->id, false));
$data->depth = $depth;
$data->revealed = false;
if ($forum->anonymous
&& $postuser->id === $USER->id
&& $post->reveal) {
$data->revealed = true;
}
if (!empty($post->children)) {
$post->replycount = count($post->children);
}
$data->replycount = '';
// Only show reply count if replies and not first post
if(!empty($post->replycount) && $post->replycount > 0 && $post->parent) {
$data->replycount = hsuforum_xreplies($post->replycount);
}
// Mark post as read.
if ($data->unread) {
hsuforum_mark_post_read($USER->id, $post, $forum->id);
}
if (!empty($parent)) {
$parentuser = hsuforum_extract_postuser($parent, $forum, context_module::instance($cm->id));
$data->parenturl = $CFG->wwwroot.'/mod/hsuforum/discuss.php?d='.$parent->discussion.'#p'.$parent->id;
$data->parentfullname = $parentuser->fullname;
if (!empty($parentuser->user_picture)) {
$parentuser->user_picture->size = 100;
$data->parentuserurl = $this->get_post_user_url($cm, $parentuser);
$data->parentuserpic = $this->output->user_picture($parentuser,
array('link' => false, 'size' => 100, 'alttext' => false));
}
}
if ($depth > 0) {
// Top level responses don't count.
$data->isreply = true;
}
return $this->post_template($data);
}
public function discussion_template($d, $forumtype, $hidethreadcontent = null) {
global $PAGE;
$replies = '';
if(!empty($d->replies)) {
$xreplies = hsuforum_xreplies($d->replies);
$replies = "<span class='hsuforum-replycount'>$xreplies</span>";
}
if (!empty($d->userurl)) {
$byuser = html_writer::link($d->userurl, $d->fullname);
} else {
$byuser = html_writer::tag('span', $d->fullname);
}
$unread = '';
$unreadclass = '';
$attrs = '';
if ($d->unread != '-') {
$new = get_string('unread', 'hsuforum');
$unread = "<a class='hsuforum-unreadcount disable-router' href='$d->viewurl#unread'>$new</a>";
$attrs = 'data-isunread="true"';
$unreadclass = 'hsuforum-post-unread';
}
$author = s(strip_tags($d->fullname));
$group = '';
if (!empty($d->group)) {
$group = '<br>'.$d->group;
}
$latestpost = '';
if (!empty($d->modified) && !empty($d->replies)) {
$latestpost = '<small class="hsuforum-thread-replies-meta">'.get_string('lastposttimeago', 'hsuforum', hsuforum_relative_time($d->rawmodified)).'</small>';
}
$participants = '<div class="hsuforum-thread-participants">'.implode(' ',$d->replyavatars).'</div>';
$datecreated = hsuforum_relative_time($d->rawcreated, array('class' => 'hsuforum-thread-pubdate'));
$threadtitle = $d->subject;
if ($d->pinned) {
$pinnedstr = get_string('discussionpinned', 'hsuforum');
$threadtitle = $this->pix_icon('i/pinned', $pinnedstr, 'mod_hsuforum') . ' ' . $threadtitle;
}
if (!$d->fullthread) {
$threadtitle = "<a class='disable-router' href='$d->viewurl'>$threadtitle</a>";
}
$options = get_string('options', 'hsuforum');
$threadmeta =
'<div class="hsuforum-thread-meta">'
.$replies
.$unread
.$participants
.$latestpost
.'<div class="hsuforum-thread-flags">'."{$d->subscribe} $d->postflags $d->timed</div>"
.'</div>';
if ($d->fullthread) {
$tools = '<div role="region" class="hsuforum-tools hsuforum-thread-tools" aria-label="'.$options.'">'.$d->tools.'</div>';
$blogmeta = '';
$blogreplies = '';
} else {
$blogreplies = hsuforum_xreplies($d->replies);
$tools = "<a class='disable-router hsuforum-replycount-link' href='$d->viewurl'>$blogreplies</a>";
$blogmeta = $threadmeta;
}
$revealed = "";
if ($d->revealed) {
$nonanonymous = get_string('nonanonymous', 'mod_hsuforum');
$revealed = '<span class="label label-danger">'.$nonanonymous.'</span>';
}
$threadcontent = '';
if (!$hidethreadcontent) {
$threadcontent = '<div class="hsuforum-thread-content" tabindex="0">' . $d->message . '</div>';
}
$threadheader = <<<HTML
<div class="hsuforum-thread-header">
<div class="hsuforum-thread-title">
<h4 id='thread-title-{$d->id}' role="heading" aria-level="4">
$threadtitle
</h4>
<small>$datecreated</small>
</div>
$threadmeta
</div>
HTML;
return <<<HTML
<article id="p{$d->postid}" class="hsuforum-thread hsuforum-post-target clearfix" role="article"
data-discussionid="$d->id" data-postid="$d->postid" data-author="$author" data-isdiscussion="true" $attrs>
<header id="h{$d->postid}" class="clearfix $unreadclass">
<div class="hsuforum-thread-author">
<img class="userpicture img-circle" src="{$d->imagesrc}" alt="" />
<p class="hsuforum-thread-byline">
$byuser $group $revealed
</p>
</div>
$threadheader
$threadcontent
$tools
</header>
<div id="hsuforum-thread-{$d->id}" class="hsuforum-thread-body">
<!-- specific to blog style -->
$blogmeta
$d->posts
$d->replyform
</div>
</article>
HTML;
}
/**
* Render a list of posts
*
* @param \stdClass $cm The forum course module
* @param \stdClass $discussion The discussion for the posts
* @param \stdClass[] $posts The posts to render
* @param bool $canreply
* @throws coding_exception
* @return string
*/
public function posts($cm, $discussion, $posts, $canreply = false) {
global $USER;
$items = '';
$count = 0;
if (!empty($posts)) {
if (!array_key_exists($discussion->firstpost, $posts)) {
throw new coding_exception('Missing discussion post');
}
$parent = $posts[$discussion->firstpost];
$items .= $this->post_walker($cm, $discussion, $posts, $parent, $canreply, $count);
// Mark post as read.
if (empty($parent->postread)) {
$forum = hsuforum_get_cm_forum($cm);
hsuforum_mark_post_read($USER->id, $parent, $forum->id);
}
}
$output = "<h5 role='heading' aria-level='5'>".hsuforum_xreplies($count)."</h5>";
if (!empty($count)) {
$output .= "<ol class='hsuforum-thread-replies-list'>".$items."</ol>";
}
return "<div class='hsuforum-thread-replies'>".$output."</div>";
}
/**
* Internal method to walk over a list of posts, rendering
* each post and their children.
*
* @param object $cm
* @param object $discussion
* @param array $posts
* @param object $parent
* @param bool $canreply
* @param int $count Keep track of the number of posts actually rendered
* @param int $depth
* @return string
*/
protected function post_walker($cm, $discussion, $posts, $parent, $canreply, &$count, $depth = 0) {
$output = '';
foreach ($posts as $post) {
if ($post->parent != $parent->id) {
continue;
}
$html = $this->post($cm, $discussion, $post, $canreply, $parent, array(), $depth);
if (!empty($html)) {
$count++;
$output .= "<li class='hsuforum-post depth$depth' data-depth='$depth' data-count='$count'>".$html."</li>";
if (!empty($post->children)) {
$output .= $this->post_walker($cm, $discussion, $posts, $post, $canreply, $count, ($depth + 1));
}
}
}
return $output;
}
/**
* Return html for individual post
*
* 3 use cases:
* 1. Standard post
* 2. Reply to user
* 3. Private reply to user
*
* @param object $p
* @return string
*/
public function post_template($p) {
global $PAGE;
$byuser = $p->fullname;
if (!empty($p->userurl)) {
$byuser = html_writer::link($p->userurl, $p->fullname);
}
$byline = get_string('postbyx', 'hsuforum', $byuser);
if ($p->isreply) {
$parent = $p->parentfullname;
if (!empty($p->parentuserurl)) {
$parent = html_writer::link($p->parentuserurl, $p->parentfullname);
}
if (empty($p->parentuserpic)) {
$byline = get_string('replybyx', 'hsuforum', $byuser);
} else {
$byline = get_string('postbyxinreplytox', 'hsuforum', array(
'parent' => $p->parentuserpic.$parent,
'author' => $byuser,
'parentpost' => "<a title='".get_string('parentofthispost', 'hsuforum')."' class='hsuforum-parent-post-link disable-router' href='$p->parenturl'><span class='accesshide'>".get_string('parentofthispost', 'hsuforum')."</span>↑</a>"
));
}
if (!empty($p->privatereply)) {
if (empty($p->parentuserpic)) {
$byline = get_string('privatereplybyx', 'hsuforum', $byuser);
} else {
$byline = get_string('postbyxinprivatereplytox', 'hsuforum', array(
'author' => $byuser,
'parent' => $p->parentuserpic.$parent
));
}
}
} else if (!empty($p->privatereply)) {
$byline = get_string('privatereplybyx', 'hsuforum', $byuser);
}
$author = s(strip_tags($p->fullname));
$unread = '';
$unreadclass = '';
if ($p->unread) {
$unread = "<span class='hsuforum-unreadcount'>".get_string('unread', 'hsuforum')."</span>";
$unreadclass = "hsuforum-post-unread";
}
$options = get_string('options', 'hsuforum');
$datecreated = hsuforum_relative_time($p->rawcreated, array('class' => 'hsuforum-post-pubdate'));
$postreplies = '';
if($p->replycount) {
$postreplies = "<div class='post-reply-count accesshide'>$p->replycount</div>";
}
$newwindow = '';
if ($PAGE->pagetype === 'local-joulegrader-view') {
$newwindow = ' target="_blank"';
}
$revealed = "";
if ($p->revealed) {
$nonanonymous = get_string('nonanonymous', 'mod_hsuforum');
$revealed = '<span class="label label-danger">'.$nonanonymous.'</span>';
}
return <<<HTML
<div class="hsuforum-post-wrapper hsuforum-post-target clearfix $unreadclass" id="p$p->id" data-postid="$p->id" data-discussionid="$p->discussionid" data-author="$author" data-ispost="true" tabindex="-1">
<div class="hsuforum-post-figure">
<img class="userpicture" src="{$p->imagesrc}" alt="">
</div>
<div class="hsuforum-post-body">
<h6 role="heading" aria-level="6" class="hsuforum-post-byline" id="hsuforum-post-$p->id">
$unread $byline $revealed
</h6>
<small class='hsuform-post-date'><a href="$p->permalink" class="disable-router"$newwindow>$datecreated</a></small>
<div class="hsuforum-post-content">
<div class="hsuforum-post-title">$p->subject</div>
$p->message
</div>
<div role="region" class='hsuforum-tools' aria-label='$options'>
<div class="hsuforum-postflagging">$p->postflags</div>
$p->tools
</div>
$postreplies
</div>
</div>
HTML;
}
/**
* This method is used to generate HTML for a subscriber selection form that
* uses two user_selector controls
*
* @param user_selector_base $existinguc
* @param user_selector_base $potentialuc
* @return string
*/
public function subscriber_selection_form(user_selector_base $existinguc, user_selector_base $potentialuc) {
$output = '';
$formattributes = array();
$formattributes['id'] = 'subscriberform';
$formattributes['action'] = '';
$formattributes['method'] = 'post';
$output .= html_writer::start_tag('form', $formattributes);
$output .= html_writer::empty_tag('input', array('type'=>'hidden', 'name'=>'sesskey', 'value'=>sesskey()));
$existingcell = new html_table_cell();
$existingcell->text = $existinguc->display(true);
$existingcell->attributes['class'] = 'existing';
$actioncell = new html_table_cell();
$actioncell->text = html_writer::start_tag('div', array());
$actioncell->text .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'subscribe', 'value'=>$this->page->theme->larrow.' '.get_string('add'), 'class'=>'actionbutton'));
$actioncell->text .= html_writer::empty_tag('br', array());
$actioncell->text .= html_writer::empty_tag('input', array('type'=>'submit', 'name'=>'unsubscribe', 'value'=>$this->page->theme->rarrow.' '.get_string('remove'), 'class'=>'actionbutton'));
$actioncell->text .= html_writer::end_tag('div', array());
$actioncell->attributes['class'] = 'actions';
$potentialcell = new html_table_cell();
$potentialcell->text = $potentialuc->display(true);
$potentialcell->attributes['class'] = 'potential';
$table = new html_table();
$table->attributes['class'] = 'subscribertable boxaligncenter';
$table->data = array(new html_table_row(array($existingcell, $actioncell, $potentialcell)));
$output .= html_writer::table($table);
$output .= html_writer::end_tag('form');
return $output;
}
/**
* This function generates HTML to display a subscriber overview, primarily used on
* the subscribers page if editing was turned off
*
* @param array $users
* @param string $entityname
* @param object $forum
* @param object $course
* @return string
*/
public function subscriber_overview($users, $entityname, $forum, $course) {
$output = '';
$modinfo = get_fast_modinfo($course);
if (!$users || !is_array($users) || count($users)===0) {
$output .= $this->output->heading(get_string("nosubscribers", "hsuforum"));
} else if (!isset($modinfo->instances['hsuforum'][$forum->id])) {
$output .= $this->output->heading(get_string("invalidmodule", "error"));
} else {
$cm = $modinfo->instances['hsuforum'][$forum->id];
$canviewemail = in_array('email', \core_user\fields::get_identity_fields(context_module::instance($cm->id), false));
$strparams = new stdclass();
$strparams->name = format_string($forum->name);
$strparams->count = count($users);
$output .= $this->output->heading(get_string("subscriberstowithcount", "hsuforum", $strparams));
$table = new html_table();
$table->cellpadding = 5;
$table->cellspacing = 5;
$table->tablealign = 'center';
$table->data = array();
foreach ($users as $user) {
$info = array($this->output->user_picture($user, array('courseid'=>$course->id)), fullname($user));
if ($canviewemail) {
array_push($info, $user->email);
}
$table->data[] = $info;
}
$output .= html_writer::table($table);
}
return $output;
}
/**
* This is used to display a control containing all of the subscribed users so that
* it can be searched
*
* @param user_selector_base $existingusers
* @return string
*/
public function subscribed_users(user_selector_base $existingusers) {
$output = $this->output->box_start('subscriberdiv boxaligncenter');
$output .= html_writer::tag('p', get_string('forcessubscribe', 'hsuforum'));
$output .= $existingusers->display(true);
$output .= $this->output->box_end();
return $output;
}
/**
* Generate the HTML for an icon to be displayed beside the subject of a timed discussion.
*
* @param object $discussion
* @param bool $visiblenow Indicicates that the discussion is currently
* visible to all users.
* @return string
*/
public function timed_discussion_tooltip($discussion, $visiblenow) {
$dates = array();
if ($discussion->timestart) {
$dates[] = get_string('displaystart', 'mod_hsuforum').': '.userdate($discussion->timestart);
}
if ($discussion->timeend) {
$dates[] = get_string('displayend', 'mod_hsuforum').': '.userdate($discussion->timeend);
}
$str = $visiblenow ? 'timedvisible' : 'timedhidden';
$dates[] = get_string($str, 'mod_hsuforum');
$tooltip = implode("\n", $dates);
return $this->pix_icon('i/calendar', $tooltip, 'moodle', array('class' => 'smallicon timedpost'));
}
/**
* Display a forum post in the relevant context.
*
* @param \mod_hsuforum\output\hsuforum_post $post The post to display.
* @return string
*/
public function render_hsuforum_post_email(\mod_hsuforum\output\hsuforum_post_email $post) {
$data = $post->export_for_template($this, $this->target === RENDERER_TARGET_TEXTEMAIL);
return $this->render_from_template('mod_hsuforum/' . $this->hsuforum_post_template(), $data);
}
/**
* The template name for this renderer.
*
* @return string
*/
public function hsuforum_post_template() {
return 'hsuforum_post';
}
/**
* The javascript module used by the presentation layer
*
* @return array
* @author Mark Nielsen
*/
public function get_js_module() {
return array(
'name' => 'mod_hsuforum',
'fullpath' => '/mod/hsuforum/module.js',
'requires' => array(
'base',
'node',
'event',
'anim',
'panel',
'dd-plugin',
'io-base',
'json',