Prevent caching of these forms
[moodle.git] / mod / forum / lib.php
CommitLineData
f93f848a 1<?PHP // $Id$
2
3
4include("$CFG->dirroot/mod/discuss/lib.php");
5
6// These are non-special forum types ie the ones that aren't automatically created
7$FORUM_TYPE = array ("general" => "General Forum",
8 "eachuser" => "Each $student posts a topic");
9
10function is_subscribed($user, $forum) {
11 global $db;
12
13 return record_exists_sql("SELECT * FROM forum_subscriptions WHERE user='$user' AND forum='$forum'");
14}
15
16function forum_subscribe($user, $forum) {
17 global $db;
18
19 return $db->Execute("INSERT INTO forum_subscriptions SET user = '$user', forum = '$forum'");
20}
21
22function forum_unsubscribe($user, $forum) {
23 global $db;
24
25 return $db->Execute("DELETE FROM forum_subscriptions WHERE user = '$user' AND forum = '$forum'");
26}
27
28
e07635f4 29function user_has_posted_discussion($forumid, $userid) {
30 if ($topics = get_all_topics($forumid, "DESC", $userid)) {
31 return true;
32 } else {
33 return false;
34 }
35}
36
37function user_can_post_discussion($forum) {
38// $forum is an object
39 global $USER;
40
41 if ($forum->type == "eachuser") {
42 return (! user_has_posted_discussion($forum->id, $USER->id));
43 } else if (isteacher($forum->course)) {
44 return true;
45 } else {
46 return $forum->open;
47 }
48}
49
50
51function get_all_topics($forum="0", $forum_sort="DESC", $user=0) {
52 if ($user) {
53 $userselect = " AND u.id = '$user' ";
54 } else {
55 $userselect = "";
56 }
f93f848a 57 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture, u.id as userid
58 FROM discuss d, discuss_posts p, user u
59 WHERE d.forum = '$forum' AND p.discuss = d.id AND
e07635f4 60 p.parent= 0 AND p.user = u.id $userselect
f93f848a 61 ORDER BY p.created $forum_sort");
62}
63
64
65function get_course_news_forum($courseid) {
66 if ($forum = get_record_sql("SELECT * from forum WHERE course = '$courseid' AND type = 'news'")) {
67 return $forum;
68 } else {
69 // Doesn't exist, so create one now.
70 $forum->course = $courseid;
71 $forum->type = "news";
72 $forum->name = "News";
73 $forum->intro= "General news about this course";
74 $forum->open = 0;
75 $forum->assessed = 0;
76 $forum->timemodified = time();
77 $forum->id = insert_record("forum", $forum);
78 return get_record_sql("SELECT * from forum WHERE id = '$forum->id'");
79 }
80}
81
82function get_course_social_forum($courseid) {
83 if ($forum = get_record_sql("SELECT * from forum WHERE course = '$courseid' AND type = 'social'")) {
84 return $forum;
85 } else {
86 // Doesn't exist, so create one now.
87 $forum->course = $courseid;
88 $forum->type = "social";
89 $forum->name = "Social";
90 $forum->intro= "A forum to socialise and talk about anything you like";
91 $forum->open = 1;
92 $forum->assessed = 0;
93 $forum->timemodified = time();
94 $forum->id = insert_record("forum", $forum);
95 return get_record_sql("SELECT * from forum WHERE id = '$forum->id'");
96 }
97}
98
99function get_course_discussion_forum($courseid) {
100 if ($forum = get_record_sql("SELECT * from forum WHERE course = '$courseid' AND type = 'discussion'")) {
101 return $forum;
102 } else {
103 // Doesn't exist, so create one now.
104 $forum->course = $courseid;
105 $forum->type = "discussion";
106 $forum->name = "Course Discussion";
107 $forum->intro= "Discussions about course content";
108 $forum->open = 0;
109 $forum->assessed = 1;
110 $forum->timemodified = time();
111 $forum->id = insert_record("forum", $forum);
112 return get_record_sql("SELECT * from forum WHERE id = '$forum->id'");
113 }
114}
115
116
e07635f4 117function print_forum_latest_topics($forum_id=0, $forum_numtopics=5, $forum_style="plain", $forum_sort="DESC") {
f93f848a 118 global $CFG, $USER;
119
120 if ($forum_id) {
121 if (! $forum = get_record("forum", "id", $forum_id)) {
122 error("Forum ID was incorrect");
123 }
124 if (! $course = get_record("course", "id", $forum->course)) {
125 error("Could not find the course this forum belongs to!");
126 }
127
128 if ($course->category) {
129 require_login($course->id);
130 }
131
132 } else {
133 if (! $course = get_record("course", "category", 0)) {
134 error("Could not find a top-level course!");
135 }
136 if (! $forum = get_course_news_forum($course->id)) {
137 error("Could not find or create a main forum in this course (id $course->id)");
138 }
139 }
140
141 if (! $topics = get_all_topics($forum->id, $forum_sort) ) {
e07635f4 142 echo "<P ALIGN=CENTER><B>There are no discussion topics yet in this forum.</B></P>";
f93f848a 143
144 } else {
145
146 $replies = count_discussion_replies($forum->id);
147
148 $topiccount = 0;
149
150 foreach ($topics as $topic) {
151 $topiccount++;
152
153 if ($forum_numtopics && ($topiccount > $forum_numtopics)) {
154 echo "<P ALIGN=right><A HREF=\"$CFG->wwwroot/mod/discuss/index.php?forum=$forum->id\">Older topics</A> ...</P>";
155 break;
156 }
157 if ($replies[$topic->discuss]) {
158 $topic->replies = $replies[$topic->discuss]->replies;
159 } else {
160 $topic->replies = 0;
161 }
162 $ownpost = ($topic->userid == $USER->id);
163 switch ($forum_style) {
164 case "minimal":
7a302afc 165 echo "<P><FONT COLOR=#555555>".userdate($topic->modified, "%e %B, %H:%M")."</FONT>";
f93f848a 166 echo "<BR>$topic->subject ";
167 echo "<A HREF=\"$CFG->wwwroot/mod/discuss/view.php?d=$topic->discuss\">more...</A>";
168 echo "</P>\n";
169 break;
170 default:
171 print_post($topic, $forum->course, $ownpost, $reply=0, $link=1, $assessed=false);
172 echo "<BR>\n";
173 break;
174 }
175 }
176 }
e07635f4 177 if (user_can_post_discussion($forum)) {
f93f848a 178 echo "<P ALIGN=right>";
179 echo "<A HREF=\"$CFG->wwwroot/mod/discuss/post.php?forum=$forum->id\">Add a new topic...</A>";
180 echo "</P>";
181 }
182
183}
184
185
186?>