Removed version (replaced by /version.php
[moodle.git] / mod / forum / lib.php
CommitLineData
f93f848a 1<?PHP // $Id$
2
3
0d22e7cc 4include_once("$CFG->dirroot/mod/discuss/lib.php");
f93f848a 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));
82aa0e8d 43 } else if ($forum->type == "teacher") {
44 return isteacher($forum->course);
e07635f4 45 } else if (isteacher($forum->course)) {
46 return true;
47 } else {
48 return $forum->open;
49 }
50}
51
52
53function get_all_topics($forum="0", $forum_sort="DESC", $user=0) {
54 if ($user) {
55 $userselect = " AND u.id = '$user' ";
56 } else {
57 $userselect = "";
58 }
f93f848a 59 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture, u.id as userid
60 FROM discuss d, discuss_posts p, user u
61 WHERE d.forum = '$forum' AND p.discuss = d.id AND
e07635f4 62 p.parent= 0 AND p.user = u.id $userselect
f93f848a 63 ORDER BY p.created $forum_sort");
64}
65
66
67function get_course_news_forum($courseid) {
68 if ($forum = get_record_sql("SELECT * from forum WHERE course = '$courseid' AND type = 'news'")) {
69 return $forum;
70 } else {
71 // Doesn't exist, so create one now.
72 $forum->course = $courseid;
73 $forum->type = "news";
74 $forum->name = "News";
75 $forum->intro= "General news about this course";
76 $forum->open = 0;
77 $forum->assessed = 0;
78 $forum->timemodified = time();
79 $forum->id = insert_record("forum", $forum);
80 return get_record_sql("SELECT * from forum WHERE id = '$forum->id'");
81 }
82}
83
84function get_course_social_forum($courseid) {
85 if ($forum = get_record_sql("SELECT * from forum WHERE course = '$courseid' AND type = 'social'")) {
86 return $forum;
87 } else {
88 // Doesn't exist, so create one now.
89 $forum->course = $courseid;
90 $forum->type = "social";
91 $forum->name = "Social";
92 $forum->intro= "A forum to socialise and talk about anything you like";
93 $forum->open = 1;
94 $forum->assessed = 0;
95 $forum->timemodified = time();
96 $forum->id = insert_record("forum", $forum);
97 return get_record_sql("SELECT * from forum WHERE id = '$forum->id'");
98 }
99}
100
101function get_course_discussion_forum($courseid) {
102 if ($forum = get_record_sql("SELECT * from forum WHERE course = '$courseid' AND type = 'discussion'")) {
103 return $forum;
104 } else {
105 // Doesn't exist, so create one now.
106 $forum->course = $courseid;
107 $forum->type = "discussion";
108 $forum->name = "Course Discussion";
71fb9630 109 $forum->intro= "General discussions about course topics";
f93f848a 110 $forum->open = 0;
111 $forum->assessed = 1;
112 $forum->timemodified = time();
113 $forum->id = insert_record("forum", $forum);
114 return get_record_sql("SELECT * from forum WHERE id = '$forum->id'");
115 }
116}
117
82aa0e8d 118function get_course_teacher_forum($courseid) {
119 if ($forum = get_record_sql("SELECT * from forum WHERE course = '$courseid' AND type = 'teacher'")) {
120 return $forum;
121 } else {
122 // Doesn't exist, so create one now.
123 $forum->course = $courseid;
124 $forum->type = "teacher";
125 $forum->name = "Teacher Forum";
126 $forum->intro= "For teacher-only notes and discussion";
127 $forum->open = 0;
128 $forum->assessed = 0;
129 $forum->timemodified = time();
130 $forum->id = insert_record("forum", $forum);
131 return get_record_sql("SELECT * from forum WHERE id = '$forum->id'");
132 }
133}
134
f93f848a 135
e07635f4 136function print_forum_latest_topics($forum_id=0, $forum_numtopics=5, $forum_style="plain", $forum_sort="DESC") {
f93f848a 137 global $CFG, $USER;
138
139 if ($forum_id) {
140 if (! $forum = get_record("forum", "id", $forum_id)) {
141 error("Forum ID was incorrect");
142 }
143 if (! $course = get_record("course", "id", $forum->course)) {
144 error("Could not find the course this forum belongs to!");
145 }
146
147 if ($course->category) {
148 require_login($course->id);
149 }
150
151 } else {
152 if (! $course = get_record("course", "category", 0)) {
153 error("Could not find a top-level course!");
154 }
155 if (! $forum = get_course_news_forum($course->id)) {
156 error("Could not find or create a main forum in this course (id $course->id)");
157 }
158 }
159
77305fe6 160 if (user_can_post_discussion($forum)) {
161 echo "<P ALIGN=right>";
162 echo "<A HREF=\"$CFG->wwwroot/mod/discuss/post.php?forum=$forum->id\">Add a new topic...</A>";
163 echo "</P>";
164 }
165
f93f848a 166 if (! $topics = get_all_topics($forum->id, $forum_sort) ) {
e07635f4 167 echo "<P ALIGN=CENTER><B>There are no discussion topics yet in this forum.</B></P>";
f93f848a 168
169 } else {
170
171 $replies = count_discussion_replies($forum->id);
172
173 $topiccount = 0;
174
175 foreach ($topics as $topic) {
176 $topiccount++;
177
178 if ($forum_numtopics && ($topiccount > $forum_numtopics)) {
ab001fe7 179 echo "<P ALIGN=right><A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id\">Older topics</A> ...</P>";
f93f848a 180 break;
181 }
182 if ($replies[$topic->discuss]) {
183 $topic->replies = $replies[$topic->discuss]->replies;
184 } else {
185 $topic->replies = 0;
186 }
187 $ownpost = ($topic->userid == $USER->id);
188 switch ($forum_style) {
189 case "minimal":
7a302afc 190 echo "<P><FONT COLOR=#555555>".userdate($topic->modified, "%e %B, %H:%M")."</FONT>";
f93f848a 191 echo "<BR>$topic->subject ";
192 echo "<A HREF=\"$CFG->wwwroot/mod/discuss/view.php?d=$topic->discuss\">more...</A>";
193 echo "</P>\n";
194 break;
195 default:
196 print_post($topic, $forum->course, $ownpost, $reply=0, $link=1, $assessed=false);
197 echo "<BR>\n";
198 break;
199 }
200 }
201 }
f93f848a 202}
203
204
205?>