Mass change related to separating forum from discuss and making it a
[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
29function get_all_topics($forum="0", $forum_sort="DESC") {
30 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture, u.id as userid
31 FROM discuss d, discuss_posts p, user u
32 WHERE d.forum = '$forum' AND p.discuss = d.id AND
33 p.parent= 0 AND p.user = u.id
34 ORDER BY p.created $forum_sort");
35}
36
37
38function get_course_news_forum($courseid) {
39 if ($forum = get_record_sql("SELECT * from forum WHERE course = '$courseid' AND type = 'news'")) {
40 return $forum;
41 } else {
42 // Doesn't exist, so create one now.
43 $forum->course = $courseid;
44 $forum->type = "news";
45 $forum->name = "News";
46 $forum->intro= "General news about this course";
47 $forum->open = 0;
48 $forum->assessed = 0;
49 $forum->timemodified = time();
50 $forum->id = insert_record("forum", $forum);
51 return get_record_sql("SELECT * from forum WHERE id = '$forum->id'");
52 }
53}
54
55function get_course_social_forum($courseid) {
56 if ($forum = get_record_sql("SELECT * from forum WHERE course = '$courseid' AND type = 'social'")) {
57 return $forum;
58 } else {
59 // Doesn't exist, so create one now.
60 $forum->course = $courseid;
61 $forum->type = "social";
62 $forum->name = "Social";
63 $forum->intro= "A forum to socialise and talk about anything you like";
64 $forum->open = 1;
65 $forum->assessed = 0;
66 $forum->timemodified = time();
67 $forum->id = insert_record("forum", $forum);
68 return get_record_sql("SELECT * from forum WHERE id = '$forum->id'");
69 }
70}
71
72function get_course_discussion_forum($courseid) {
73 if ($forum = get_record_sql("SELECT * from forum WHERE course = '$courseid' AND type = 'discussion'")) {
74 return $forum;
75 } else {
76 // Doesn't exist, so create one now.
77 $forum->course = $courseid;
78 $forum->type = "discussion";
79 $forum->name = "Course Discussion";
80 $forum->intro= "Discussions about course content";
81 $forum->open = 0;
82 $forum->assessed = 1;
83 $forum->timemodified = time();
84 $forum->id = insert_record("forum", $forum);
85 return get_record_sql("SELECT * from forum WHERE id = '$forum->id'");
86 }
87}
88
89
90function forum_latest_topics($forum_id=0, $forum_numtopics=5, $forum_style="plain", $forum_sort="DESC") {
91 global $CFG, $USER;
92
93 if ($forum_id) {
94 if (! $forum = get_record("forum", "id", $forum_id)) {
95 error("Forum ID was incorrect");
96 }
97 if (! $course = get_record("course", "id", $forum->course)) {
98 error("Could not find the course this forum belongs to!");
99 }
100
101 if ($course->category) {
102 require_login($course->id);
103 }
104
105 } else {
106 if (! $course = get_record("course", "category", 0)) {
107 error("Could not find a top-level course!");
108 }
109 if (! $forum = get_course_news_forum($course->id)) {
110 error("Could not find or create a main forum in this course (id $course->id)");
111 }
112 }
113
114 if (! $topics = get_all_topics($forum->id, $forum_sort) ) {
115 echo "<P><B>There are no discussion topics yet in this forum.</B></P>";
116
117 } else {
118
119 $replies = count_discussion_replies($forum->id);
120
121 $topiccount = 0;
122
123 foreach ($topics as $topic) {
124 $topiccount++;
125
126 if ($forum_numtopics && ($topiccount > $forum_numtopics)) {
127 echo "<P ALIGN=right><A HREF=\"$CFG->wwwroot/mod/discuss/index.php?forum=$forum->id\">Older topics</A> ...</P>";
128 break;
129 }
130 if ($replies[$topic->discuss]) {
131 $topic->replies = $replies[$topic->discuss]->replies;
132 } else {
133 $topic->replies = 0;
134 }
135 $ownpost = ($topic->userid == $USER->id);
136 switch ($forum_style) {
137 case "minimal":
138 echo "<P><FONT COLOR=#555555>".userdate($topic->modified, "j M H:i")."</FONT>";
139 echo "<BR>$topic->subject ";
140 echo "<A HREF=\"$CFG->wwwroot/mod/discuss/view.php?d=$topic->discuss\">more...</A>";
141 echo "</P>\n";
142 break;
143 default:
144 print_post($topic, $forum->course, $ownpost, $reply=0, $link=1, $assessed=false);
145 echo "<BR>\n";
146 break;
147 }
148 }
149 }
150 if ($forum->open || $USER->editing) {
151 echo "<P ALIGN=right>";
152 echo "<A HREF=\"$CFG->wwwroot/mod/discuss/post.php?forum=$forum->id\">Add a new topic...</A>";
153 echo "</P>";
154 }
155
156}
157
158
159?>