More robust variable checking
[moodle.git] / mod / forum / index.php
CommitLineData
f93f848a 1<?PHP // $Id$
2
b0e3a925 3 require_once("../../config.php");
4 require_once("lib.php");
f93f848a 5
6 optional_variable($id); // course
7
8 if ($id) {
9 if (! $course = get_record("course", "id", $id)) {
10 error("Course ID is incorrect");
11 }
12 } else {
f781b794 13 if (! $course = get_site()) {
f93f848a 14 error("Could not find a top-level course!");
15 }
16 }
17
18 if ($course->category) {
19 require_login($course->id);
20 }
21
501cdbd8 22 unset($SESSION->fromdiscussion);
f93f848a 23
f781b794 24 add_to_log($course->id, "forum", "view forums", "index.php?id=$course->id");
25
26 $strforums = get_string("forums", "forum");
97485d07 27 $strforum = get_string("forum", "forum");
28 $strdescription = get_string("description");
29 $strdiscussions = get_string("discussions", "forum");
30 $strsubscribed = get_string("subscribed", "forum");
31
32 $searchform = forum_print_search_form($course, "", true, "plain");
f93f848a 33
34 if ($course->category) {
f781b794 35 print_header("$course->shortname: $strforums", "$course->fullname",
dfc9ba9b 36 "<A HREF=../../course/view.php?id=$course->id>$course->shortname</A> -> $strforums",
97485d07 37 "", "", true, $searchform, navmenu($course));
f93f848a 38 } else {
97485d07 39 print_header("$course->shortname: $strforums", "$course->fullname", "$strforums",
40 "", "", true, $searchform, navmenu($course));
f93f848a 41 }
97485d07 42
f781b794 43
44 $table->head = array ($strforum, $strdescription, $strdiscussions);
45 $table->align = array ("LEFT", "LEFT", "CENTER");
46
47 $can_subscribe = (isstudent($course->id) or isteacher($course->id) or isadmin());
f93f848a 48
f93f848a 49 if ($can_subscribe) {
f781b794 50 $table->head[] = $strsubscribed;
51 $table->align[] = "CENTER";
f93f848a 52 }
f93f848a 53
54 if ($forums = get_records("forum", "course", $id, "name ASC")) {
71fb9630 55 foreach ($forums as $forum) {
37b15514 56 switch ($forum->type) {
0872b023 57 case "news":
58 case "social":
59 $generalforums[] = $forum;
37b15514 60 break;
61 case "teacher":
62 if (isteacher($course->id)) {
63 $generalforums[] = $forum;
64 }
65 break;
94361e02 66 default:
67 if (!$course->category) {
68 $generalforums[] = $forum;
69 }
70 break;
71fb9630 71 }
37b15514 72 }
73 }
71fb9630 74
37b15514 75 if ($generalforums) {
76 foreach ($generalforums as $forum) {
501cdbd8 77 $count = count_records("forum_discussions", "forum", "$forum->id");
71fb9630 78
79 if ($can_subscribe) {
501cdbd8 80 if (forum_is_forcesubscribed($forum->id)) {
f781b794 81 $sublink = get_string("yes");
71fb9630 82 } else {
501cdbd8 83 if (forum_is_subscribed($USER->id, $forum->id)) {
f781b794 84 $subscribed = get_string("yes");
85 $subtitle = get_string("unsubscribe", "forum");
501cdbd8 86 } else {
f781b794 87 $subscribed = get_string("no");
88 $subtitle = get_string("subscribe", "forum");
501cdbd8 89 }
f781b794 90 $sublink = "<A TITLE=\"$subtitle\" HREF=\"subscribe.php?id=$forum->id\">$subscribed</A>";
71fb9630 91 }
92 $table->data[] = array ("<A HREF=\"view.php?f=$forum->id\">$forum->name</A>",
501cdbd8 93 "$forum->intro", "$count", "$sublink");
71fb9630 94 } else {
95 $table->data[] = array ("<A HREF=\"view.php?f=$forum->id\">$forum->name</A>",
501cdbd8 96 "$forum->intro", "$count");
71fb9630 97 }
98 }
f781b794 99 print_heading(get_string("generalforums", "forum"));
37b15514 100 print_table($table);
101 unset($table->data);
102 }
71fb9630 103
94361e02 104 if ($course->category) { // Only real courses have learning forums
105 // Add extra field for section number, at the front
106 array_unshift($table->head, "");
107 array_unshift($table->align, "CENTER");
108
109 if ($learningforums = get_all_instances_in_course("forum", $course->id)) {
86c5e69a 110 foreach ($learningforums as $key => $forum) {
111 if ($forum->type == "news" or $forum->type == "social") {
112 unset($learningforums[$key]); // Remove these
113 }
114 }
115 }
116 if ($learningforums) {
94361e02 117 foreach ($learningforums as $forum) {
118 $count = count_records("forum_discussions", "forum", "$forum->id");
119
120 $forum->intro = forum_shorten_post($forum->intro);
121
122 if (!$forum->section) { // some forums are in the "0" section
123 $forum->section = "";
124 }
125
126 if ($can_subscribe) {
127 if (forum_is_forcesubscribed($forum->id)) {
128 $sublink = get_string("yes");
501cdbd8 129 } else {
94361e02 130 if (forum_is_subscribed($USER->id, $forum->id)) {
131 $subscribed = get_string("yes");
132 $subtitle = get_string("unsubscribe", "forum");
133 } else {
134 $subscribed = get_string("no");
135 $subtitle = get_string("subscribe", "forum");
136 }
137 $sublink = "<A TITLE=\"$subtitle\" HREF=\"subscribe.php?id=$forum->id\">$subscribed</A>";
501cdbd8 138 }
94361e02 139 $table->data[] = array ("$forum->section", "<A HREF=\"view.php?f=$forum->id\">$forum->name</A>",
140 "$forum->intro", "$count", "$sublink");
141 } else {
142 $table->data[] = array ("$forum->section", "<A HREF=\"view.php?f=$forum->id\">$forum->name</A>",
143 "$forum->intro", "$count");
f93f848a 144 }
f93f848a 145 }
94361e02 146 print_heading(get_string("learningforums", "forum"));
147 print_table($table);
f93f848a 148 }
149 }
150
f93f848a 151
152 print_footer($course);
153
154?>