-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathindex.php
435 lines (364 loc) · 16 KB
/
index.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
<?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/>.
/**
* @package mod_hsuforum
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @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
*/
require_once(__DIR__ . '/../../config.php');
require_once($CFG->dirroot . '/course/lib.php');
require_once($CFG->dirroot . '/mod/hsuforum/lib.php');
require_once($CFG->libdir . '/rsslib.php');
$id = optional_param('id', 0, PARAM_INT); // Course id
$subscribe = optional_param('subscribe', null, PARAM_INT); // Subscribe/Unsubscribe all forums
$config = get_config('hsuforum');
$url = new moodle_url('/mod/hsuforum/index.php', array('id' => $id));
if ($subscribe !== null) {
require_sesskey();
$url->param('subscribe', $subscribe);
}
$PAGE->set_url($url);
if ($id) {
if (!$course = $DB->get_record('course', array('id' => $id))) {
throw new \moodle_exception('invalidcourseid');
}
} else {
$course = get_site();
}
require_course_login($course);
$PAGE->set_pagelayout('incourse');
$coursecontext = context_course::instance($course->id);
unset($SESSION->fromdiscussion);
$params = array(
'context' => context_course::instance($course->id),
);
$event = \mod_hsuforum\event\course_module_instance_list_viewed::create($params);
$event->add_record_snapshot('course', $course);
$event->trigger();
$strforums = get_string('forums', 'hsuforum');
$strforum = get_string('forum', 'hsuforum');
$strdescription = get_string('description');
$strdiscussions = get_string('discussions', 'hsuforum');
$strsubscribed = get_string('subscribed', 'hsuforum');
$strunreadposts = get_string('unreadposts', 'hsuforum');
$strmarkallread = get_string('markallread', 'hsuforum');
$strsubscribe = get_string('subscribe', 'hsuforum');
$strunsubscribe = get_string('unsubscribe', 'hsuforum');
$stryes = get_string('yes');
$strno = get_string('no');
$strrss = get_string('rss');
$stremaildigest = get_string('emaildigest');
$searchform = hsuforum_search_form($course);
// Retrieve the list of forum digest options for later.
$digestoptions = hsuforum_get_user_digest_options();
$digestoptions_selector = new single_select(new moodle_url('/mod/hsuforum/maildigest.php',
array(
'backtoindex' => 1,
)),
'maildigest',
$digestoptions,
null,
'');
$digestoptions_selector->method = 'post';
// Start of the table for General Forums.
$generaltable = new html_table();
$generaltable->head = array ($strforum, $strdescription, $strdiscussions);
$generaltable->align = array ('left', 'left', 'center');
$generaltable->head[] = $strunreadposts;
$generaltable->align[] = 'center';
$subscribed_forums = hsuforum_get_subscribed_forums($course);
$can_subscribe = is_enrolled($coursecontext);
if ($can_subscribe) {
$generaltable->head[] = $strsubscribed;
$generaltable->align[] = 'center';
$generaltable->head[] = $stremaildigest . ' ' . $OUTPUT->help_icon('emaildigesttype', 'mod_hsuforum');
$generaltable->align[] = 'center';
}
if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
isset($CFG->enablerssfeeds) && isset($config->enablerssfeeds) &&
$CFG->enablerssfeeds && $config->enablerssfeeds)) {
$generaltable->head[] = $strrss;
$generaltable->align[] = 'center';
}
$usesections = course_format_uses_sections($course->format);
$table = new html_table();
// Parse and organise all the forums. Most forums are course modules but
// some special ones are not. These get placed in the general forums
// category with the forums in section 0.
$forums = $DB->get_records_sql("
SELECT f.*,
d.maildigest
FROM {hsuforum} f
LEFT JOIN {hsuforum_digests} d ON d.forum = f.id AND d.userid = ?
WHERE f.course = ?
", array($USER->id, $course->id));
$generalforums = array();
$learningforums = array();
$modinfo = get_fast_modinfo($course);
$showsubscriptioncolumns = false;
foreach ($modinfo->get_instances_of('hsuforum') as $forumid => $cm) {
if (!$cm->uservisible or !isset($forums[$forumid])) {
continue;
}
$forum = $forums[$forumid];
if (!$context = context_module::instance($cm->id, IGNORE_MISSING)) {
// Shouldn't happen.
continue;
}
if (!has_capability('mod/hsuforum:viewdiscussion', $context)) {
// User can't view this one - skip it.
continue;
}
// Determine whether subscription options should be displayed.
$forum->cansubscribe = mod_hsuforum\subscriptions::is_subscribable($forum);
$forum->cansubscribe = $forum->cansubscribe || has_capability('mod/hsuforum:managesubscriptions', $context);
$forum->issubscribed = mod_hsuforum\subscriptions::is_subscribed($USER->id, $forum, null, $cm);
$showsubscriptioncolumns = $showsubscriptioncolumns || $forum->issubscribed || $forum->cansubscribe;
// Fill two type array - order in modinfo is the same as in course.
if ($forum->type == 'news' or $forum->type == 'social') {
$generalforums[$forum->id] = $forum;
} else if ($course->id == SITEID or empty($cm->sectionnum)) {
$generalforums[$forum->id] = $forum;
} else {
$learningforums[$forum->id] = $forum;
}
}
/// Do course wide subscribe/unsubscribe
if (!is_null($subscribe) && !isguestuser()) {
foreach ($modinfo->get_instances_of('hsuforum') as $forumid=>$cm) {
$forum = $forums[$forumid];
$modcontext = context_module::instance($cm->id);
$cansub = false;
if (has_capability('mod/hsuforum:viewdiscussion', $modcontext)) {
$cansub = true;
}
if ($cansub && $cm->visible == 0 &&
!has_capability('mod/hsuforum:managesubscriptions', $modcontext))
{
$cansub = false;
}
if (!hsuforum_is_forcesubscribed($forum)) {
$subscribed = hsuforum_is_subscribed($USER->id, $forum);
if ((has_capability('moodle/course:manageactivities', $coursecontext, $USER->id) || $forum->forcesubscribe != HSUFORUM_DISALLOWSUBSCRIBE) && $subscribe && !$subscribed && $cansub) {
hsuforum_subscribe($USER->id, $forumid, $modcontext);
} else if (!$subscribe && $subscribed) {
hsuforum_unsubscribe($USER->id, $forumid, $modcontext);
}
}
}
$returnto = hsuforum_go_back_to(new moodle_url('/mod/hsuforum/index.php', array('id' => $course->id)));
$shortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
if ($subscribe) {
redirect(
$returnto,
get_string('nowallsubscribed', 'hsuforum', $shortname),
null,
\core\output\notification::NOTIFY_SUCCESS
);
} else {
redirect(
$returnto,
get_string('nowallunsubscribed', 'hsuforum', $shortname),
null,
\core\output\notification::NOTIFY_SUCCESS
);
}
}
if ($generalforums) {
// Process general forums.
foreach ($generalforums as $forum) {
$cm = $modinfo->instances['hsuforum'][$forum->id];
$context = context_module::instance($cm->id);
$count = hsuforum_count_discussions($forum, $cm, $course);
if ($unread = hsuforum_count_forum_unread_posts($cm, $course)) {
$unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'#unread">'.$unread.'</a>';
} else {
$unreadlink = '<span class="read">0</span>';
}
$forum->intro = shorten_text(format_module_intro('hsuforum', $forum, $cm->id), $config->shortpost);
$forumname = format_string($forum->name, true);
if ($cm->visible) {
$style = '';
} else {
$style = 'class="dimmed"';
}
$forumlink = "<a href=\"view.php?f=$forum->id\" $style>".format_string($forum->name,true)."</a>";
$discussionlink = "<a href=\"view.php?f=$forum->id\" $style>".$count."</a>";
$row = array ($forumlink, $forum->intro, $discussionlink, $unreadlink);
if ($showsubscriptioncolumns) {
$row[] = hsuforum_get_subscribe_link($forum, $context, array('subscribed' => $stryes,
'unsubscribed' => $strno, 'forcesubscribed' => $stryes,
'cantsubscribe' => '-'), false, false, true);
$row[] = hsuforum_index_get_forum_subscription_selector($forum);
}
// If this forum has RSS activated, calculate it.
if ($show_rss) {
if ($forum->rsstype && $forum->rssarticles) {
//Calculate the tooltip text
if ($forum->rsstype == 1) {
$tooltiptext = get_string('rsssubscriberssdiscussions', 'hsuforum');
} else {
$tooltiptext = get_string('rsssubscriberssposts', 'hsuforum');
}
if (!isloggedin() && $course->id == SITEID) {
$userid = guest_user()->id;
} else {
$userid = $USER->id;
}
//Get html code for RSS link
$row[] = rss_get_link($context->id, $userid, 'mod_hsuforum', $forum->id, $tooltiptext);
} else {
$row[] = ' ';
}
}
$generaltable->data[] = $row;
}
}
// Start of the table for Learning Forums
$learningtable = new html_table();
$learningtable->head = array ($strforum, $strdescription, $strdiscussions);
$learningtable->align = array ('left', 'left', 'center');
$learningtable->head[] = $strunreadposts;
$learningtable->align[] = 'center';
if ($showsubscriptioncolumns) {
$learningtable->head[] = $strsubscribed;
$learningtable->align[] = 'center';
$learningtable->head[] = $stremaildigest . ' ' . $OUTPUT->help_icon('emaildigesttype', 'mod_hsuforum');
$learningtable->align[] = 'center';
}
if ($show_rss = (($can_subscribe || $course->id == SITEID) &&
isset($CFG->enablerssfeeds) && isset($config->enablerssfeeds) &&
$CFG->enablerssfeeds && $config->enablerssfeeds)) {
$learningtable->head[] = $strrss;
$learningtable->align[] = 'center';
}
/// Now let's process the learning forums
if ($course->id != SITEID) { // Only real courses have learning forums
// 'format_.'$course->format only applicable when not SITEID (format_site is not a format)
$strsectionname = get_string('sectionname', 'format_'.$course->format);
// Add extra field for section number, at the front
array_unshift($learningtable->head, $strsectionname);
array_unshift($learningtable->align, 'center');
if ($learningforums) {
$currentsection = '';
foreach ($learningforums as $forum) {
$cm = $modinfo->instances['hsuforum'][$forum->id];
$context = context_module::instance($cm->id);
$count = hsuforum_count_discussions($forum, $cm, $course);
if ($unread = hsuforum_count_forum_unread_posts($cm, $course)) {
$unreadlink = '<span class="unread"><a href="view.php?f='.$forum->id.'#unread">'.$unread.'</a>';
} else {
$unreadlink = '<span class="read">0</span>';
}
$forum->intro = shorten_text(format_module_intro('hsuforum', $forum, $cm->id), $config->shortpost);
if ($cm->sectionnum != $currentsection) {
$printsection = get_section_name($course, $cm->sectionnum);
if ($currentsection) {
$learningtable->data[] = 'hr';
}
$currentsection = $cm->sectionnum;
} else {
$printsection = '';
}
$forumname = format_string($forum->name,true);
if ($cm->visible) {
$style = '';
} else {
$style = 'class="dimmed"';
}
$forumlink = "<a href=\"view.php?f=$forum->id\" $style>".format_string($forum->name,true)."</a>";
$discussionlink = "<a href=\"view.php?f=$forum->id\" $style>".$count."</a>";
$row = array ($printsection, $forumlink, $forum->intro, $discussionlink, $unreadlink);
if ($can_subscribe) {
$row[] = hsuforum_get_subscribe_link($forum, $context, array('subscribed' => $stryes,
'unsubscribed' => $strno, 'forcesubscribed' => $stryes,
'cantsubscribe' => '-'), false, false, true, $subscribed_forums);
$digestoptions_selector->url->param('id', $forum->id);
if ($forum->maildigest === null) {
$digestoptions_selector->selected = -1;
} else {
$digestoptions_selector->selected = $forum->maildigest;
}
$row[] = $OUTPUT->render($digestoptions_selector);
}
//If this forum has RSS activated, calculate it
if ($show_rss) {
if ($forum->rsstype && $forum->rssarticles) {
//Calculate the tolltip text
if ($forum->rsstype == 1) {
$tooltiptext = get_string('rsssubscriberssdiscussions', 'hsuforum');
} else {
$tooltiptext = get_string('rsssubscriberssposts', 'hsuforum');
}
//Get html code for RSS link
$row[] = rss_get_link($context->id, $USER->id, 'mod_hsuforum', $forum->id, $tooltiptext);
} else {
$row[] = ' ';
}
}
$learningtable->data[] = $row;
}
}
}
/// Output the page
$PAGE->navbar->add($strforums);
$PAGE->set_title("$course->shortname: $strforums");
$PAGE->set_heading($course->fullname);
$PAGE->set_button($searchform);
echo $OUTPUT->header();
if (!isguestuser() && isloggedin()) {
echo $OUTPUT->box_start('subscription');
echo html_writer::tag('div',
html_writer::link(new moodle_url('/mod/hsuforum/index.php', array('id'=>$course->id, 'subscribe'=>1, 'sesskey'=>sesskey())),
get_string('allsubscribe', 'hsuforum')),
array('class'=>'helplink'));
echo html_writer::tag('div',
html_writer::link(new moodle_url('/mod/hsuforum/index.php', array('id'=>$course->id, 'subscribe'=>0, 'sesskey'=>sesskey())),
get_string('allunsubscribe', 'hsuforum')),
array('class'=>'helplink'));
echo $OUTPUT->box_end();
echo $OUTPUT->box(' ', 'clearer');
}
if ($generalforums) {
echo $OUTPUT->heading(get_string('generalforums', 'hsuforum'), 2);
echo html_writer::table($generaltable);
}
if ($learningforums) {
echo $OUTPUT->heading(get_string('learningforums', 'hsuforum'), 2);
echo html_writer::table($learningtable);
}
echo $OUTPUT->footer();
/**
* Get the content of the forum subscription options for this forum.
*
* @param stdClass $forum The forum to return options for
* @return string
*/
function hsuforum_index_get_forum_subscription_selector($forum) {
global $OUTPUT, $PAGE;
if ($forum->cansubscribe || $forum->issubscribed) {
if ($forum->maildigest === null) {
$forum->maildigest = -1;
}
$renderer = $PAGE->get_renderer('mod_hsuforum');
return $OUTPUT->render($renderer->render_digest_options($forum, $forum->maildigest));
} else {
// This user can subscribe to some forums. Add the empty fields.
return '';
}
};