New file
[moodle.git] / mod / forum / lib.php
CommitLineData
f93f848a 1<?PHP // $Id$
2
b0e3a925 3require_once("$CFG->dirroot/files/mimetypes.php");
7f6689e4 4
501cdbd8 5/// CONSTANTS ///////////////////////////////////////////////////////////
f93f848a 6
501cdbd8 7$FORUM_DEFAULT_DISPLAY_MODE = 3;
f93f848a 8
ffe11640 9$FORUM_LAYOUT_MODES = array ( "1" => get_string("modeflatoldestfirst", "forum"),
10 "-1" => get_string("modeflatnewestfirst", "forum"),
11 "2" => get_string("modethreaded", "forum"),
12 "3" => get_string("modenested", "forum") );
f93f848a 13
11b0c469 14// These are course content forums that can be added to the course manually
ffe11640 15$FORUM_TYPES = array ("general" => get_string("generalforum", "forum"),
16 "eachuser" => get_string("eachuserforum", "forum"),
17 "single" => get_string("singleforum", "forum") );
f93f848a 18
ffe11640 19$FORUM_POST_RATINGS = array ("3" => get_string("postrating3", "forum"),
20 "2" => get_string("postrating2", "forum"),
21 "1" => get_string("postrating1", "forum") );
f93f848a 22
70c476a7 23$FORUM_OPEN_MODES = array ("2" => get_string("openmode2", "forum"),
24 "1" => get_string("openmode1", "forum"),
25 "0" => get_string("openmode0", "forum") );
26
73bb0835 27
c585fa17 28define("FORUM_SHORT_POST", 300); // Less non-HTML characters than this is short
4d871a72 29
c585fa17 30define("FORUM_LONG_POST", 600); // More non-HTML characters than this is long
4d871a72 31
c585fa17 32define("FORUM_MANY_DISCUSSIONS", 10);
e07635f4 33
34
caadf009 35/// STANDARD FUNCTIONS ///////////////////////////////////////////////////////////
36
37function forum_add_instance($forum) {
38// Given an object containing all the necessary data,
39// (defined by the form in mod.html) this function
40// will create a new instance and return the id number
41// of the new instance.
42
43 global $CFG;
44
45 $forum->timemodified = time();
46
47 if (! $forum->id = insert_record("forum", $forum)) {
48 return false;
49 }
50
51 if ($forum->type == "single") { // Create related discussion.
52
53 $discussion->course = $forum->course;
54 $discussion->forum = $forum->id;
55 $discussion->name = $forum->name;
56 $discussion->intro = $forum->intro;
57 $discussion->assessed = $forum->assessed;
58
59 if (! forum_add_discussion($discussion)) {
60 error("Could not add the discussion for this forum");
61 }
62 }
63 add_to_log($forum->course, "forum", "add", "index.php?f=$forum->id", "$forum->id");
64
65 return $forum->id;
66}
67
68
69function forum_update_instance($forum) {
70// Given an object containing all the necessary data,
71// (defined by the form in mod.html) this function
72// will update an existing instance with new data.
73
74 $forum->timemodified = time();
75 $forum->id = $forum->instance;
76
77 if ($forum->type == "single") { // Update related discussion and post.
78 if (! $discussion = get_record("forum_discussions", "forum", $forum->id)) {
79 if ($discussions = get_records("forum_discussions", "forum", $forum->id, "timemodified ASC")) {
80 notify("Warning! There is more than one discussion in this forum - using the most recent");
81 $discussion = array_pop($discussions);
82 } else {
83 error("Could not find the discussion in this forum");
84 }
85 }
86 if (! $post = get_record("forum_posts", "id", $discussion->firstpost)) {
87 error("Could not find the first post in this forum discussion");
88 }
89
90 $post->subject = $forum->name;
91 $post->message = $forum->intro;
92 $post->modified = $forum->timemodified;
93
94 if (! update_record("forum_posts", $post)) {
95 error("Could not update the first post");
96 }
97
98 $discussion->name = $forum->name;
99
100 if (! update_record("forum_discussions", $discussion)) {
101 error("Could not update the discussion");
102 }
103 }
104
105 if (update_record("forum", $forum)) {
106 add_to_log($forum->course, "forum", "update", "index.php?f=$forum->id", "$forum->id");
107 return true;
108 } else {
109 return false;
110 }
111}
112
113
114function forum_delete_instance($id) {
115// Given an ID of an instance of this module,
116// this function will permanently delete the instance
117// and any data that depends on it.
118
119 if (! $forum = get_record("forum", "id", "$id")) {
120 return false;
121 }
122
123 $result = true;
124
125 if ($discussions = get_records("forum_discussions", "forum", $forum->id)) {
126 foreach ($discussions as $discussion) {
127 if (! forum_delete_discussion($discussion)) {
128 $result = false;
129 }
130 }
131 }
132
133 if (! delete_records("forum_subscriptions", "forum", "$forum->id")) {
134 $result = false;
135 }
136
137 if (! delete_records("forum", "id", "$forum->id")) {
138 $result = false;
139 }
140
141 return $result;
142}
143
144
145function forum_cron () {
146// Function to be run periodically according to the moodle cron
147// Finds all posts that have yet to be mailed out, and mails them
148
149 global $CFG, $USER;
150
151 $cutofftime = time() - $CFG->maxeditingtime;
152
1f48942e 153 if ($posts = forum_get_unmailed_posts($cutofftime)) {
caadf009 154
155 $timenow = time();
156
157 foreach ($posts as $post) {
158
159 print_string("processingpost", "forum", $post->id);
160 echo " ... ";
161
ebc3bd2b 162 if (! $userfrom = get_record("user", "id", "$post->userid")) {
163 echo "Could not find user $post->userid\n";
caadf009 164 continue;
165 }
166
167 if (! $discussion = get_record("forum_discussions", "id", "$post->discussion")) {
168 echo "Could not find discussion $post->discussion\n";
169 continue;
170 }
171
172 if (! $forum = get_record("forum", "id", "$discussion->forum")) {
173 echo "Could not find forum $discussion->forum\n";
174 continue;
175 }
176
177 if (! $course = get_record("course", "id", "$forum->course")) {
178 echo "Could not find course $forum->course\n";
179 continue;
180 }
181
182 if ($users = forum_subscribed_users($course, $forum)) {
183 $canunsubscribe = ! forum_is_forcesubscribed($forum->id);
184
185 $mailcount=0;
186 foreach ($users as $userto) {
187 $USER->lang = $userto->lang; // Affects the language of get_string
f690562f 188 $canreply = forum_user_can_post($forum, $userto);
caadf009 189
190
191 $by->name = "$userfrom->firstname $userfrom->lastname";
192 $by->date = userdate($post->created, "", $userto->timezone);
193 $strbynameondate = get_string("bynameondate", "forum", $by);
194
195 $strforums = get_string("forums", "forum");
196
197 $postsubject = "$course->shortname: $post->subject";
198 $posttext = "$course->shortname -> $strforums -> $forum->name";
199
200 if ($discussion->name == $forum->name) {
201 $posttext .= "\n";
202 } else {
203 $posttext .= " -> $discussion->name\n";
204 }
205 $posttext .= "---------------------------------------------------------------------\n";
206 $posttext .= "$post->subject\n";
207 $posttext .= $strbynameondate."\n";
208 $posttext .= "---------------------------------------------------------------------\n";
209 $posttext .= strip_tags($post->message);
210 $posttext .= "\n\n";
211 if ($post->attachment) {
212 $post->course = $course->id;
213 $post->forum = $forum->id;
214 $posttext .= forum_print_attachments($post, "text");
215 }
f690562f 216 if ($canreply) {
217 $posttext .= "---------------------------------------------------------------------\n";
218 $posttext .= get_string("postmailinfo", "forum", $course->shortname)."\n";
219 $posttext .= "$CFG->wwwroot/mod/forum/post.php?reply=$post->id\n";
220 }
caadf009 221 if ($canunsubscribe) {
222 $posttext .= "\n---------------------------------------------------------------------\n";
223 $posttext .= get_string("unsubscribe", "forum");
224 $posttext .= ": $CFG->wwwroot/mod/forum/subscribe.php?id=$forum->id\n";
225 }
226
227 if ($userto->mailformat == 1) { // HTML
228 $posthtml = "<P><FONT FACE=sans-serif>".
153cc356 229 "<A TARGET=\"_blank\" HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A> -> ".
230 "<A TARGET=\"_blank\" HREF=\"$CFG->wwwroot/mod/forum/index.php?id=$course->id\">$strforums</A> -> ".
231 "<A TARGET=\"_blank\" HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id\">$forum->name</A>";
caadf009 232 if ($discussion->name == $forum->name) {
233 $posthtml .= "</FONT></P>";
234 } else {
153cc356 235 $posthtml .= " -> <A TARGET=\"_blank\" HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->id\">$discussion->name</A></FONT></P>";
caadf009 236 }
f690562f 237 $posthtml .= forum_make_mail_post($post, $userfrom, $userto, $course, false, $canreply, false, false);
caadf009 238
239 if ($canunsubscribe) {
240 $posthtml .= "\n<BR><HR SIZE=1 NOSHADE><P ALIGN=RIGHT><FONT SIZE=1><A HREF=\"$CFG->wwwroot/mod/forum/subscribe.php?id=$forum->id\">".get_string("unsubscribe", "forum")."</A></FONT></P>";
241 }
242
243 } else {
244 $posthtml = "";
245 }
246
247 if (! email_to_user($userto, $userfrom, $postsubject, $posttext, $posthtml)) {
248 echo "Error: mod/forum/cron.php: Could not send out mail for id $post->id to user $userto->id ($userto->email)\n";
249 } else {
250 $mailcount++;
251 }
252 }
253 echo "mailed to $mailcount users ...";
254 }
255
256 if (! set_field("forum_posts", "mailed", "1", "id", "$post->id")) {
257 echo "Could not update the mailed field for id $post->id\n";
258 }
259 echo "\n";
260 }
261 }
262
263 return true;
264}
265
266function forum_user_outline($course, $user, $mod, $forum) {
267
1f48942e 268 if ($posts = forum_get_user_posts($forum->id, $user->id)) {
caadf009 269 $result->info = get_string("numposts", "forum", count($posts));
270
271 $lastpost = array_pop($posts);
272 $result->time = $lastpost->modified;
273 return $result;
274 }
275 return NULL;
276}
277
278
279function forum_user_complete($course, $user, $mod, $forum) {
280 global $CFG;
281
1f48942e 282 if ($posts = forum_get_user_posts($forum->id, $user->id)) {
caadf009 283 foreach ($posts as $post) {
284 if ($post->parent) {
285 $footer = "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion&parent=$post->parent\">".
286 get_string("parentofthispost", "forum")."</A>";
287 } else {
288 $footer = "";
289 }
290
291 forum_print_post($post, $course->id, $ownpost=false, $reply=false, $link=false, $rate=false, $footer);
292 }
293
294 } else {
295 echo "<P>".get_string("noposts", "forum")."</P>";
296 }
caadf009 297}
298
299function forum_print_recent_activity(&$logs, $isteacher=false) {
300 global $CFG, $COURSE_TEACHER_COLOR;
301
302 $heading = false;
303 $content = false;
304
dcde9f02 305 $strftimerecent = get_string("strftimerecent");
306
caadf009 307 foreach ($logs as $log) {
308 if ($log->module == "forum") {
caadf009 309
1f48942e 310 if ($post = forum_get_post_from_log($log)) {
caadf009 311 $teacherpost = "";
312 if ($forum = get_record("forum", "id", $post->forum) ) {
313 if ($forum->type == "teacher") {
314 if ($isteacher) {
315 $teacherpost = "COLOR=$COURSE_TEACHER_COLOR";
316 } else {
317 continue;
318 }
319 }
320 }
321 if (! $heading) {
322 print_headline(get_string("newforumposts", "forum").":");
323 $heading = true;
324 $content = true;
325 }
dcde9f02 326 $date = userdate($post->modified, $strftimerecent);
caadf009 327 echo "<P><FONT SIZE=1 $teacherpost>$date - $post->firstname $post->lastname<BR>";
328 echo "\"<A HREF=\"$CFG->wwwroot/mod/forum/$log->url\">";
329 if ($log->action == "add") {
330 echo "<B>$post->subject</B>";
331 } else {
332 echo "$post->subject";
333 }
334 echo "</A>\"</FONT></P>";
335 }
336 }
337 }
338 return $content;
339}
340
341
342function forum_grades($forumid) {
343/// Must return an array of grades, indexed by user, and a max grade.
344 global $FORUM_POST_RATINGS;
345
4db9d14d 346 if (!$forum = get_record("forum", "id", $forumid)) {
347 return false;
348 }
349 if (!$forum->assessed) {
350 return false;
351 }
1f48942e 352 if ($ratings = forum_get_user_grades($forumid)) {
36df40d6 353 foreach ($ratings as $rating) {
ebc3bd2b 354 $u = $rating->userid;
36df40d6 355 $r = $rating->rating;
356 if (!isset($sumrating[$u])) {
357 $sumrating[$u][1] = 0;
358 $sumrating[$u][2] = 0;
359 $sumrating[$u][3] = 0;
caadf009 360 }
36df40d6 361 $sumrating[$u][$r]++;
caadf009 362 }
363 foreach ($sumrating as $user => $rating) {
364 $return->grades[$user] = $rating[1]."s/".$rating[2]."/".$rating[3]."c";
365 }
366 } else {
367 $return->grades = array();
368 }
369
370 $return->maxgrade = "";
371 return $return;
372}
373
374
9fa49e22 375/// SQL FUNCTIONS ///////////////////////////////////////////////////////////
376
1f48942e 377function forum_get_post_full($postid) {
378/// Gets a post with all info ready for forum_print_post
379 global $CFG;
380
ebc3bd2b 381 return get_record_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture
1f48942e 382 FROM {$CFG->prefix}forum_posts p,
383 {$CFG->prefix}user u
384 WHERE p.id = '$postid'
ebc3bd2b 385 AND p.userid = u.id");
1f48942e 386}
387
388function forum_get_discussion_posts($discussion, $sort) {
389/// Gets posts with all info ready for forum_print_post
390 global $CFG;
391
ebc3bd2b 392 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture
1f48942e 393 FROM {$CFG->prefix}forum_posts p,
394 {$CFG->prefix}user u
395 WHERE p.discussion = $discussion
396 AND p.parent > 0
ebc3bd2b 397 AND p.userid = u.id $sort");
1f48942e 398}
399
400function forum_get_child_posts($parent) {
401/// Gets posts with all info ready for forum_print_post
402 global $CFG;
403
ebc3bd2b 404 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture
1f48942e 405 FROM {$CFG->prefix}forum_posts p,
406 {$CFG->prefix}user u
407 WHERE p.parent = '$parent'
ebc3bd2b 408 AND p.userid = u.id
1f48942e 409 ORDER BY p.created ASC");
410}
411
412
97485d07 413function forum_search_posts($search, $courseid, $page=0, $recordsperpage=50) {
9fa49e22 414/// Returns a list of posts that were found
415 global $CFG;
416
417 if (!isteacher($courseid)) {
418 $notteacherforum = "AND f.type <> 'teacher'";
419 } else {
420 $notteacherforum = "";
421 }
422
c2a96d6b 423 switch ($CFG->dbtype) {
424 case "mysql":
425 $limit = "LIMIT $page,$recordsperpage";
426 break;
427 case "postgres7":
428 $limit = "LIMIT $recordsperpage OFFSET ".($page * $recordsperpage);
429 break;
430 default:
431 $limit = "LIMIT $recordsperpage,$page";
97485d07 432 }
433
ebc3bd2b 434 return get_records_sql("SELECT p.*,u.firstname,u.lastname,u.email,u.picture
9fa49e22 435 FROM {$CFG->prefix}forum_posts p,
436 {$CFG->prefix}forum_discussions d,
437 {$CFG->prefix}user u,
438 {$CFG->prefix}forum f
439 WHERE (p.message LIKE '%$search%' OR p.subject LIKE '%$search%')
ebc3bd2b 440 AND p.userid = u.id
9fa49e22 441 AND p.discussion = d.id
442 AND d.course = '$courseid'
443 AND d.forum = f.id
444 $notteacherforum
97485d07 445 ORDER BY p.modified DESC $limit");
9fa49e22 446}
447
448function forum_get_ratings($postid, $sort="u.firstname ASC") {
449/// Returns a list of ratings for a particular post - sorted.
450 global $CFG;
451 return get_records_sql("SELECT u.*, r.rating, r.time
452 FROM {$CFG->prefix}forum_ratings r,
453 {$CFG->prefix}user u
1f48942e 454 WHERE r.post = '$postid'
ebc3bd2b 455 AND r.userid = u.id
9fa49e22 456 ORDER BY $sort");
457}
458
1f48942e 459function forum_get_unmailed_posts($cutofftime) {
460/// Returns a list of all new posts that have not been mailed yet
461 global $CFG;
462 return get_records_sql("SELECT p.*, d.course
463 FROM {$CFG->prefix}forum_posts p,
464 {$CFG->prefix}forum_discussions d
465 WHERE p.mailed = 0
466 AND p.created < '$cutofftime'
467 AND p.discussion = d.id");
468}
469
470function forum_get_user_posts($forumid, $userid) {
471/// Get all the posts for a user in a forum suitable for forum_print_post
472 global $CFG;
473
ebc3bd2b 474 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture
1f48942e 475 FROM {$CFG->prefix}forum f,
476 {$CFG->prefix}forum_discussions d,
477 {$CFG->prefix}forum_posts p,
478 {$CFG->prefix}user u
479 WHERE f.id = '$forumid'
480 AND d.forum = f.id
481 AND p.discussion = d.id
ebc3bd2b 482 AND p.userid = '$userid'
483 AND p.userid = u.id
1f48942e 484 ORDER BY p.modified ASC");
485}
486
487function forum_get_post_from_log($log) {
488/// Given a log entry, return the forum post details for it.
489 global $CFG;
490
491 if ($log->action == "add post") {
492
ebc3bd2b 493 return get_record_sql("SELECT p.*, d.forum, u.firstname, u.lastname, u.email, u.picture
1f48942e 494 FROM {$CFG->prefix}forum_discussions d,
495 {$CFG->prefix}forum_posts p,
496 {$CFG->prefix}user u
497 WHERE p.id = '$log->info'
498 AND d.id = p.discussion
ebc3bd2b 499 AND p.userid = u.id
1f48942e 500 AND u.deleted <> '1'");
501
502
503 } else if ($log->action == "add discussion") {
504
ebc3bd2b 505 return get_record_sql("SELECT p.*, d.forum, u.firstname, u.lastname, u.email, u.picture
1f48942e 506 FROM {$CFG->prefix}forum_discussions d,
507 {$CFG->prefix}forum_posts p,
508 {$CFG->prefix}user u
509 WHERE d.id = '$log->info'
510 AND d.firstpost = p.id
ebc3bd2b 511 AND p.userid = u.id
1f48942e 512 AND u.deleted <> '1'");
513 }
514 return NULL;
515}
516
517
518function forum_get_user_grades($forumid) {
519/// Get all user grades for a forum
520 global $CFG;
521
ebc3bd2b 522 return get_records_sql("SELECT r.id, p.userid, r.rating
1f48942e 523 FROM {$CFG->prefix}forum_discussions d,
524 {$CFG->prefix}forum_posts p,
525 {$CFG->prefix}forum_ratings r
526 WHERE d.forum = '$forumid'
527 AND p.discussion = d.id
528 AND r.post = p.id");
529}
530
531
532function forum_count_discussion_replies($forum="0") {
533// Returns an array of counts of replies to each discussion (optionally in one forum)
534 global $CFG;
535
536 if ($forum) {
537 $forumselect = " AND d.forum = '$forum'";
538 }
539 return get_records_sql("SELECT p.discussion, (count(*)) as replies
540 FROM {$CFG->prefix}forum_posts p,
541 {$CFG->prefix}forum_discussions d
542 WHERE p.parent > 0
543 AND p.discussion = d.id
544 GROUP BY p.discussion");
545}
546
547function forum_count_unrated_posts($discussionid, $userid) {
548// How many unrated posts are in the given discussion for a given user?
549 global $CFG;
550 if ($posts = get_record_sql("SELECT count(*) as num
551 FROM {$CFG->prefix}forum_posts
552 WHERE parent > 0
553 AND discussion = '$discussionid'
ebc3bd2b 554 AND userid <> '$userid' ")) {
1f48942e 555
556 if ($rated = get_record_sql("SELECT count(*) as num
557 FROM {$CFG->prefix}forum_posts p,
558 {$CFG->prefix}forum_ratings r
559 WHERE p.discussion = '$discussionid'
560 AND p.id = r.post
ebc3bd2b 561 AND r.userid = '$userid'")) {
1f48942e 562 $difference = $posts->num - $rated->num;
563 if ($difference > 0) {
564 return $difference;
565 } else {
566 return 0; // Just in case there was a counting error
567 }
568 } else {
569 return $posts->num;
570 }
571 } else {
572 return 0;
573 }
574}
575
576function forum_get_discussions($forum="0", $forum_sort="DESC", $user=0) {
577/// Get all discussions in a forum
578 global $CFG;
579
580 if ($user) {
581 $userselect = " AND u.id = '$user' ";
582 } else {
583 $userselect = "";
584 }
ebc3bd2b 585 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture
1f48942e 586 FROM {$CFG->prefix}forum_discussions d,
587 {$CFG->prefix}forum_posts p,
588 {$CFG->prefix}user u
589 WHERE d.forum = '$forum'
590 AND p.discussion = d.id
591 AND p.parent= 0
ebc3bd2b 592 AND p.userid = u.id $userselect
1f48942e 593 ORDER BY p.created $forum_sort");
594}
595
596
597
598function forum_get_user_discussions($courseid, $userid) {
599/// Get all discussions started by a particular user in a course
600 global $CFG;
601
602 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture,
ebc3bd2b 603 f.type as forumtype, f.name as forumname, f.id as forumid
1f48942e 604 FROM {$CFG->prefix}forum_discussions d,
605 {$CFG->prefix}forum_posts p,
606 {$CFG->prefix}user u,
607 {$CFG->prefix}forum f
608 WHERE d.course = '$courseid'
609 AND p.discussion = d.id
610 AND p.parent = 0
ebc3bd2b 611 AND p.userid = u.id
1f48942e 612 AND u.id = '$userid'
613 AND d.forum = f.id
614 ORDER BY p.created ASC");
615}
616
617
618function forum_subscribed_users($course, $forum) {
619/// Returns list of user objects that are subscribed to this forum
620 global $CFG;
621
adaf3928 622 if ($forum->forcesubscribe) {
623 if ($course->category) {
1f48942e 624 return get_course_users($course->id);
adaf3928 625 } else {
2d0b30a0 626 return get_site_users();
1f48942e 627 }
628 }
629 return get_records_sql("SELECT u.*
630 FROM {$CFG->prefix}user u,
631 {$CFG->prefix}forum_subscriptions s
632 WHERE s.forum = '$forum->id'
ebc3bd2b 633 AND s.userid = u.id
1f48942e 634 AND u.deleted <> 1");
635}
9fa49e22 636
637
caadf009 638/// OTHER FUNCTIONS ///////////////////////////////////////////////////////////
f93f848a 639
640
11b0c469 641function forum_get_course_forum($courseid, $type) {
642// How to set up special 1-per-course forums
a6fcdf98 643 global $CFG;
644
1f48942e 645 if ($forum = get_record("forum", "course", $courseid, "type", $type)) {
f93f848a 646 return $forum;
ffe11640 647
f93f848a 648 } else {
649 // Doesn't exist, so create one now.
650 $forum->course = $courseid;
11b0c469 651 $forum->type = "$type";
652 switch ($forum->type) {
653 case "news":
ffe11640 654 $forum->name = get_string("namenews", "forum");
adaf3928 655 $forum->forcesubscribe = 1;
656 $forum->intro = get_string("intronews", "forum");
657 $forum->open = 1; // 0 - no, 1 - posts only, 2 - discuss and post
658 $forum->assessed = 0;
24cd4546 659 if ($site = get_site()) {
660 if ($courseid == $site->id) {
661 $forum->name = get_string("sitenews");
adaf3928 662 $forum->forcesubscribe = 0;
24cd4546 663 }
664 }
11b0c469 665 break;
666 case "social":
ffe11640 667 $forum->name = get_string("namesocial", "forum");
668 $forum->intro = get_string("introsocial", "forum");
70c476a7 669 $forum->open = 2; // 0 - no, 1 - posts only, 2 - discuss and post
11b0c469 670 $forum->assessed = 0;
671 $forum->forcesubscribe = 0;
672 break;
673 case "teacher":
ffe11640 674 $forum->name = get_string("nameteacher", "forum");
675 $forum->intro = get_string("introteacher", "forum");
70c476a7 676 $forum->open = 0; // 0 - no, 1 - posts only, 2 - discuss and post
11b0c469 677 $forum->assessed = 0;
678 $forum->forcesubscribe = 0;
679 break;
680 default:
681 notify("That forum type doesn't exist!");
682 return false;
683 break;
f93f848a 684
11b0c469 685 }
82aa0e8d 686 $forum->timemodified = time();
687 $forum->id = insert_record("forum", $forum);
e6874d9f 688
689 if ($forum->type != "teacher") {
690 if (! $module = get_record("modules", "name", "forum")) {
691 notify("Could not find forum module!!");
692 return false;
693 }
694 $mod->course = $courseid;
695 $mod->module = $module->id;
696 $mod->instance = $forum->id;
697 $mod->section = 0;
698 if (! $mod->coursemodule = add_course_module($mod) ) { // assumes course/lib.php is loaded
699 notify("Could not add a new course module to the course '$course->fullname'");
700 return false;
701 }
702 if (! $sectionid = add_mod_to_section($mod) ) { // assumes course/lib.php is loaded
703 notify("Could not add the new course module to that section");
704 return false;
705 }
706 if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule)) {
707 notify("Could not update the course module with the correct section");
708 return false;
709 }
a6fcdf98 710 include_once("$CFG->dirroot/course/lib.php");
711 $modinfo = serialize(get_array_of_activities($courseid));
712 if (!set_field("course", "modinfo", $modinfo, "id", $courseid)) {
713 error("Could not cache module information!");
714 }
e6874d9f 715 }
716
717 return get_record("forum", "id", "$forum->id");
82aa0e8d 718 }
719}
720
f93f848a 721
11b0c469 722function forum_make_mail_post(&$post, $user, $touser, $course,
723 $ownpost=false, $reply=false, $link=false, $rate=false, $footer="") {
501cdbd8 724// Given the data about a posting, builds up the HTML to display it and
725// returns the HTML in a string. This is designed for sending via HTML email.
726
727 global $THEME, $CFG;
728
729 $output = "";
730
731 if ($post->parent) {
732 $output .= "<TABLE BORDER=0 CELLPADDING=1 CELLSPACING=1><TR><TD BGCOLOR=#888888>";
733 $output .= "<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0>";
734 } else {
735 $output .= "<TABLE BORDER=0 CELLPADDING=1 CELLSPACING=1 WIDTH=100%><TR><TD BGCOLOR=#888888>";
736 $output .= "<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0 WIDTH=100%>";
737 }
738
4499b2e2 739 $output .= "<TR><TD BGCOLOR=\"$THEME->cellcontent2\" WIDTH=35 VALIGN=TOP>";
501cdbd8 740 $output .= print_user_picture($user->id, $course->id, $user->picture, false, true);
741 $output .= "</TD>";
742
743 if ($post->parent) {
744 $output .= "<TD NOWRAP BGCOLOR=\"$THEME->cellheading\">";
745 } else {
746 $output .= "<TD NOWRAP BGCOLOR=\"$THEME->cellheading2\">";
747 }
748 $output .= "<P>";
749 $output .= "<FONT SIZE=3><B>$post->subject</B></FONT><BR>";
ffe11640 750 $output .= "<FONT SIZE=2>";
751 $by->name = "<A HREF=\"$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id\">$user->firstname $user->lastname</A>";
752 $by->date = userdate($post->created, "", $touser->timezone);
753 $output .= get_string("bynameondate", "forum", $by);
501cdbd8 754 $output .= "</FONT></P></TD></TR>";
4499b2e2 755 $output .= "<TR><TD BGCOLOR=\"$THEME->cellcontent2\" WIDTH=10>";
501cdbd8 756 $output .= "&nbsp;";
86970225 757 $output .= "</TD><TD BGCOLOR=\"$THEME->cellcontent\">\n";
501cdbd8 758
7f6689e4 759 if ($post->attachment) {
760 $post->course = $course->id;
761 $post->forum = get_field("forum_discussions", "forum", "id", $post->discussion);
762 $output .= "<DIV ALIGN=right>";
763 $output .= forum_print_attachments($post, "html");
764 $output .= "</DIV>";
765 }
766
73bb0835 767 $output .= format_text($post->message, $post->format);
501cdbd8 768
3260de67 769 $output .= "<P ALIGN=right><FONT SIZE=-1>";
501cdbd8 770
771 $age = time() - $post->created;
772 if ($ownpost) {
ffe11640 773 $output .= "<A HREF=\"$CFG->wwwroot/mod/forum/post.php?delete=$post->id\">".get_string("delete", "forum")."</A>";
501cdbd8 774 if ($reply) {
153cc356 775 $output .= " | <A TARGET=\"_blank\" HREF=\"$CFG->wwwroot/mod/forum/post.php?reply=$post->id\">".get_string("reply", "forum")."</A>";
501cdbd8 776 }
777 $output .= "&nbsp;&nbsp;";
778 } else {
779 if ($reply) {
153cc356 780 $output .= "<A TARGET=\"_blank\" HREF=\"$CFG->wwwroot/mod/forum/post.php?reply=$post->id\">".get_string("reply", "forum")."</A>&nbsp;&nbsp;";
501cdbd8 781 }
782 }
783
113bb2a2 784 $output .= "</P>";
501cdbd8 785 $output .= "<DIV ALIGN=right><P ALIGN=right>";
786
787 if ($link) {
788 if ($post->replies == 1) {
ffe11640 789 $replystring = get_string("repliesone", "forum", $post->replies);
501cdbd8 790 } else {
ffe11640 791 $replystring = get_string("repliesmany", "forum", $post->replies);
501cdbd8 792 }
ffe11640 793 $output .= "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\"><B>".get_string("discussthistopic", "forum")."</B></A> ($replystring)&nbsp;&nbsp;";
501cdbd8 794 }
795 $output .= "</P></DIV>";
796 if ($footer) {
797 $output .= "<P>$footer</P>";
798 }
799 $output .= "</TD></TR></TABLE>\n";
800 $output .= "</TD></TR></TABLE>\n\n";
801
802 return $output;
803}
804
805
11b0c469 806function forum_print_post(&$post, $courseid, $ownpost=false, $reply=false, $link=false, $rate=false, $footer="") {
c585fa17 807 global $THEME, $USER, $CFG;
501cdbd8 808
809 if ($post->parent) {
d34189e1 810 echo "<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0 CLASS=\"forumpost\">";
501cdbd8 811 } else {
d34189e1 812 echo "<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0 CLASS=\"forumpost\" WIDTH=100%>";
501cdbd8 813 }
814
d34189e1 815 echo "<TR><TD BGCOLOR=\"$THEME->cellcontent2\" CLASS=\"forumpostpicture\" WIDTH=35 VALIGN=TOP>";
501cdbd8 816 print_user_picture($post->userid, $courseid, $post->picture);
817 echo "</TD>";
818
819 if ($post->parent) {
d34189e1 820 echo "<TD NOWRAP BGCOLOR=\"$THEME->cellheading\" CLASS=\"forumpostheader\" WIDTH=\"100%\">";
501cdbd8 821 } else {
d34189e1 822 echo "<TD NOWRAP BGCOLOR=\"$THEME->cellheading2\" CLASS=\"forumpostheadertopic\" WIDTH=\"100%*\">";
501cdbd8 823 }
824 echo "<P>";
825 echo "<FONT SIZE=3><B>$post->subject</B></FONT><BR>";
ffe11640 826 echo "<FONT SIZE=2>";
827 $by->name = "<A HREF=\"$CFG->wwwroot/user/view.php?id=$post->userid&course=$courseid\">$post->firstname $post->lastname</A>";
828 $by->date = userdate($post->created);
829 print_string("bynameondate", "forum", $by);
501cdbd8 830 echo "</FONT></P></TD></TR>";
d34189e1 831 echo "<TR><TD BGCOLOR=\"$THEME->cellcontent2\" CLASS=\"forumpostside\" WIDTH=10>";
501cdbd8 832 echo "&nbsp;";
d34189e1 833 echo "</TD><TD BGCOLOR=\"$THEME->cellcontent\" CLASS=\"forumpostmessage\">\n";
501cdbd8 834
7f6689e4 835 if ($post->attachment) {
836 $post->course = $courseid;
837 $post->forum = get_field("forum_discussions", "forum", "id", $post->discussion);
838 echo "<DIV ALIGN=right>";
839 forum_print_attachments($post);
840 echo "</DIV>";
841 }
842
c585fa17 843 if ($link and (strlen(strip_tags($post->message)) > FORUM_LONG_POST)) {
aa153f29 844 // Print shortened version
73bb0835 845 echo format_text(forum_shorten_post($post->message), $post->format);
c585fa17 846 $numwords = count_words(strip_tags($post->message));
67f0b4cc 847 echo "<P><A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\">";
ffe11640 848 echo get_string("readtherest", "forum");
67f0b4cc 849 echo "</A> (".get_string("numwords", "", $numwords).")...</P>";
501cdbd8 850 } else {
aa153f29 851 // Print whole message
73bb0835 852 echo format_text($post->message, $post->format);
501cdbd8 853 }
854
e7b7951c 855 echo "<P ALIGN=right><FONT SIZE=-1>";
501cdbd8 856
857 $age = time() - $post->created;
858 if ($ownpost) {
859 if ($age < $CFG->maxeditingtime) {
ffe11640 860 echo "<A HREF=\"$CFG->wwwroot/mod/forum/post.php?edit=$post->id\">".get_string("edit", "forum")."</A> | ";
501cdbd8 861 }
64eacd6f 862 }
863 if ($ownpost or isteacher($courseid)) {
ffe11640 864 echo "<A HREF=\"$CFG->wwwroot/mod/forum/post.php?delete=$post->id\">".get_string("delete", "forum")."</A>";
501cdbd8 865 if ($reply) {
64eacd6f 866 echo "| ";
867 } else {
868 echo "&nbsp;&nbsp;";
501cdbd8 869 }
64eacd6f 870 }
871 if ($reply) {
872 echo "<A HREF=\"$CFG->wwwroot/mod/forum/post.php?reply=$post->id\">".get_string("reply", "forum")."</A>";
501cdbd8 873 echo "&nbsp;&nbsp;";
501cdbd8 874 }
113bb2a2 875 echo "</P>";
501cdbd8 876
877 echo "<DIV ALIGN=right><P ALIGN=right>";
878 if ($rate && $USER->id) {
2a3cda19 879 if (isteacher($courseid)) {
880 forum_print_ratings($post->id);
881 if ($USER->id != $post->userid) {
882 forum_print_rating($post->id, $USER->id);
883 }
884 } else if ($USER->id == $post->userid) {
7a12aab4 885 forum_print_ratings($post->id);
501cdbd8 886 } else {
7a12aab4 887 forum_print_rating($post->id, $USER->id);
501cdbd8 888 }
889 }
890
891 if ($link) {
892 if ($post->replies == 1) {
ffe11640 893 $replystring = get_string("repliesone", "forum", $post->replies);
501cdbd8 894 } else {
ffe11640 895 $replystring = get_string("repliesmany", "forum", $post->replies);
501cdbd8 896 }
ffe11640 897 echo "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\"><B>".get_string("discussthistopic", "forum")."</B></A> ($replystring)&nbsp;&nbsp;";
501cdbd8 898 }
899 echo "</P>";
900 if ($footer) {
901 echo "<P>$footer</P>";
902 }
903 echo "</DIV>";
501cdbd8 904 echo "</TD></TR>\n</TABLE>\n\n";
905}
906
3335f6fb 907
908function forum_print_post_header(&$post, $courseid, $ownpost=false, $reply=false, $link=false, $rate=false, $footer="") {
c585fa17 909 global $THEME, $USER, $CFG;
3335f6fb 910
911 if ($post->parent) {
d34189e1 912 echo "<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0 CLASS=\"forumpost\">";
3335f6fb 913 } else {
d34189e1 914 echo "<TABLE BORDER=0 CELLPADDING=3 CELLSPACING=0 CLASS=\"forumpost\" WIDTH=100%>";
3335f6fb 915 }
916
d34189e1 917 echo "<TR><TD BGCOLOR=\"$THEME->cellcontent2\" CLASS=\"forumpostpicture\" WIDTH=35 VALIGN=TOP>";
3335f6fb 918 print_user_picture($post->userid, $courseid, $post->picture);
919 echo "</TD>";
920
921 if ($post->parent) {
d34189e1 922 echo "<TD NOWRAP BGCOLOR=\"$THEME->cellheading\" CLASS=\"forumpostheader\">";
3335f6fb 923 } else {
d34189e1 924 echo "<TD NOWRAP BGCOLOR=\"$THEME->cellheading2\" CLASS=\"forumpostheadertopic\">";
3335f6fb 925 }
926 echo "<P>";
927 echo "<FONT SIZE=3><B>$post->subject</B></FONT><BR>";
928 echo "<FONT SIZE=2>";
929 $by->name = "<A HREF=\"$CFG->wwwroot/user/view.php?id=$post->userid&course=$courseid\">$post->firstname $post->lastname</A>";
930 $by->date = userdate($post->created);
931 print_string("bynameondate", "forum", $by);
932 echo "</FONT></P></TD>";
933
934 if ($post->parent) {
d34189e1 935 echo "<TD VALIGN=BOTTOM BGCOLOR=\"$THEME->cellheading\" CLASS=\"forumpostheader\">";
3335f6fb 936 } else {
d34189e1 937 echo "<TD VALIGN=BOTTOM BGCOLOR=\"$THEME->cellheading2\" CLASS=\"forumpostheadertopic\">";
3335f6fb 938 }
939 echo "<P ALIGN=right><FONT SIZE=-1>";
940
941 if ($link) {
942 if ($post->replies == 1) {
943 $replystring = get_string("repliesone", "forum", $post->replies);
944 } else {
945 $replystring = get_string("repliesmany", "forum", $post->replies);
946 }
947 echo "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\"><B>".get_string("discussthistopic", "forum")."</B></A> ($replystring)&nbsp;&nbsp;";
948 }
949 echo "</P>";
3335f6fb 950 echo "</TD></TR>\n</TABLE>\n\n";
951}
952
953
aa153f29 954function forum_shorten_post($message) {
c585fa17 955// Given a post object that we already know has a long message
956// this function truncates the message nicely to the first
957// sane place between FORUM_LONG_POST and FORUM_SHORT_POST
958
959 $i = 0;
960 $tag = false;
961 $length = strlen($message);
962 $count = 0;
963 $stopzone = false;
964 $truncate = 0;
965
966 for ($i=0; $i<$length; $i++) {
a8afb411 967 $char = $message[$i];
c585fa17 968
969 switch ($char) {
970 case "<":
971 $tag = true;
972 break;
973 case ">":
974 $tag = false;
975 break;
976 default:
977 if (!$tag) {
978 if ($stopzone) {
67f0b4cc 979 if ($char == ".") {
a8afb411 980 $truncate = $i+1;
c585fa17 981 break 2;
982 }
983 }
984 $count++;
985 }
a8afb411 986 break;
c585fa17 987 }
988 if (!$stopzone) {
989 if ($count > FORUM_SHORT_POST) {
990 $stopzone = true;
991 }
992 }
993 }
aa153f29 994
c585fa17 995 if (!$truncate) {
a8afb411 996 $truncate = $i;
c585fa17 997 }
998
67f0b4cc 999 return substr($message, 0, $truncate);
aa153f29 1000}
1001
501cdbd8 1002
7a12aab4 1003function forum_print_ratings($post) {
1f48942e 1004 if ($ratings = get_records("forum_ratings", "post", $post)) {
ffe11640 1005 $sumrating[1] = 0;
1006 $sumrating[2] = 0;
1007 $sumrating[3] = 0;
501cdbd8 1008 foreach ($ratings as $rating) {
ffe11640 1009 $sumrating[$rating->rating]++;
501cdbd8 1010 }
ffe11640 1011 $summary = $sumrating[1]."s/".$sumrating[2]."/".$sumrating[3]."c";
501cdbd8 1012
ffe11640 1013 echo get_string("ratings", "forum").": ";
501cdbd8 1014 link_to_popup_window ("/mod/forum/report.php?id=$post", "ratings", $summary, 400, 550);
501cdbd8 1015 }
1016}
1017
7a12aab4 1018function forum_print_rating($post, $user) {
501cdbd8 1019 global $FORUM_POST_RATINGS;
1020
ebc3bd2b 1021 if ($rating = get_record("forum_ratings", "userid", $user, "post", $post)) {
1f48942e 1022 if ($FORUM_POST_RATINGS[$rating->rating]) {
ffe11640 1023 echo "<FONT SIZE=-1>".get_string("youratedthis", "forum").": <FONT COLOR=green>";
1f48942e 1024 echo $FORUM_POST_RATINGS[$rating->rating];
ffe11640 1025 echo "</FONT></FONT>";
1026 return;
501cdbd8 1027 }
501cdbd8 1028 }
ffe11640 1029 choose_from_menu($FORUM_POST_RATINGS, $post, "", get_string("rate", "forum")."...");
501cdbd8 1030}
1031
7a12aab4 1032function forum_print_mode_form($discussion, $mode) {
1033 GLOBAL $FORUM_LAYOUT_MODES;
501cdbd8 1034
1035 echo "<CENTER><P>";
7a12aab4 1036 popup_form("discuss.php?d=$discussion&mode=", $FORUM_LAYOUT_MODES, "mode", $mode, "");
501cdbd8 1037 echo "</P></CENTER>\n";
1038}
1039
97485d07 1040function forum_print_search_form($course, $search="", $return=false, $type="") {
501cdbd8 1041 global $CFG;
1042
97485d07 1043 if ($type == "plain") {
1044 $output = "<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0><TR><TD NOWRAP>";
1045 $output .= "<FORM NAME=search ACTION=\"$CFG->wwwroot/mod/forum/search.php\">";
1046 $output .= "<FONT SIZE=\"-1\">";
1047 $output .= "<INPUT NAME=search TYPE=text SIZE=15 VALUE=\"$search\">";
1048 $output .= "<INPUT VALUE=\"".get_string("searchforums", "forum")."\" TYPE=submit>";
1049 $output .= "</FONT>";
1050 $output .= "<INPUT NAME=id TYPE=hidden VALUE=\"$course->id\">";
1051 $output .= "</FORM>";
1052 $output .= "</TD></TR></TABLE>";
1053 } else {
1054 $output = "<TABLE BORDER=0 CELLPADDING=10 CELLSPACING=0><TR><TD ALIGN=CENTER>";
1055 $output .= "<FORM NAME=search ACTION=\"$CFG->wwwroot/mod/forum/search.php\">";
1056 $output .= "<FONT SIZE=\"-1\">";
1057 $output .= "<INPUT NAME=search TYPE=text SIZE=15 VALUE=\"$search\"><BR>";
1058 $output .= "<INPUT VALUE=\"".get_string("searchforums", "forum")."\" TYPE=submit>";
1059 $output .= "</FONT>";
1060 $output .= "<INPUT NAME=id TYPE=hidden VALUE=\"$course->id\">";
1061 $output .= "</FORM>";
1062 $output .= "</TD></TR></TABLE>";
1063 }
5e367a2d 1064
1065 if ($return) {
1066 return $output;
1067 }
1068 echo $output;
501cdbd8 1069}
1070
1071
11b0c469 1072function forum_set_return() {
607809b3 1073 global $CFG, $SESSION;
501cdbd8 1074
28e1e8b9 1075 if (! isset($SESSION->fromdiscussion)) {
48d38fad 1076 if (!empty($_SERVER['HTTP_REFERER'])) {
1077 $referer = $_SERVER['HTTP_REFERER'];
1078 } else {
1079 $referer = "";
1080 }
28e1e8b9 1081 // If the referer is NOT a login screen then save it.
48d38fad 1082 if (! strncasecmp("$CFG->wwwroot/login", $referer, 300)) {
607809b3 1083 $SESSION->fromdiscussion = $_SERVER["HTTP_REFERER"];
28e1e8b9 1084 }
501cdbd8 1085 }
1086}
1087
1088
11b0c469 1089function forum_go_back_to($default) {
501cdbd8 1090 global $SESSION;
1091
9c9f7d77 1092 if (!empty($SESSION->fromdiscussion)) {
501cdbd8 1093 $returnto = $SESSION->fromdiscussion;
1094 unset($SESSION->fromdiscussion);
1095 return $returnto;
1096 } else {
1097 return $default;
1098 }
1099}
1100
7f6689e4 1101function forum_file_area_name($post) {
1102// Creates a directory file name, suitable for make_upload_directory()
1103 global $CFG;
1104
1105 return "$post->course/$CFG->moddata/forum/$post->forum/$post->id";
1106}
1107
1108function forum_file_area($post) {
1109 return make_upload_directory( forum_file_area_name($post) );
1110}
1111
1112function forum_delete_old_attachments($post, $exception="") {
1113// Deletes all the user files in the attachments area for a post
1114// EXCEPT for any file named $exception
1115
1116 if ($basedir = forum_file_area($post)) {
1117 if ($files = get_directory_list($basedir)) {
1118 foreach ($files as $file) {
1119 if ($file != $exception) {
1120 unlink("$basedir/$file");
1121 notify("Existing file '$file' has been deleted!");
1122 }
1123 }
1124 }
1125 if (!$exception) { // Delete directory as well, if empty
1126 rmdir("$basedir");
1127 }
1128 }
1129}
1130
1131function forum_print_attachments($post, $return=NULL) {
1132// if return=html, then return a html string.
1133// if return=text, then return a text-only string.
1134// otherwise, print HTML
1135
1136 global $CFG;
1137
1138 $filearea = forum_file_area_name($post);
1139
1140 if ($basedir = forum_file_area($post)) {
1141 if ($files = get_directory_list($basedir)) {
1142 $strattachment = get_string("attachment", "forum");
1143 foreach ($files as $file) {
1144 $icon = mimeinfo("icon", $file);
1145 if ($CFG->slasharguments) {
1146 $ffurl = "file.php/$filearea/$file";
1147 } else {
1148 $ffurl = "file.php?file=/$filearea/$file";
1149 }
1150 $image = "<IMG BORDER=0 SRC=\"$CFG->wwwroot/files/pix/$icon\" HEIGHT=16 WIDTH=16 ALT=\"File\">";
1151
1152 if ($return == "html") {
1153 $output .= "<A HREF=\"$CFG->wwwroot/$ffurl\">$image</A> ";
1154 $output .= "<A HREF=\"$CFG->wwwroot/$ffurl\">$file</A><BR>";
1155
1156 } else if ($return == "text") {
1157 $output .= "$strattachment $file:\n$CFG->wwwroot/$ffurl\n";
1158
1159 } else {
1160 link_to_popup_window("/$ffurl", "attachment", $image, 500, 500, $strattachment);
1161 echo "<A HREF=\"$CFG->wwwroot/$ffurl\">$file</A>";
1162 echo "<BR>";
1163 }
1164 }
1165 }
1166 }
1167 if ($return) {
1168 return $output;
1169 }
1170}
1171
1172function forum_add_attachment($post, $newfile) {
1173// $post is a full post record, including course and forum
3b7c1de9 1174// $newfile is a full upload array from $_FILES
7f6689e4 1175// If successful, this function returns the name of the file
1176
3b7c1de9 1177 if (empty($newfile['name'])) {
7f6689e4 1178 return "";
1179 }
1180
1181 $newfile_name = clean_filename($newfile['name']);
1182
1183 if (valid_uploaded_file($newfile)) {
1184 if (! $newfile_name) {
1185 notify("This file had a wierd filename and couldn't be uploaded");
1186
1187 } else if (! $dir = forum_file_area($post)) {
1188 notify("Attachment could not be stored");
1189 $newfile_name = "";
1190
1191 } else {
1192 if (move_uploaded_file($newfile['tmp_name'], "$dir/$newfile_name")) {
1193 forum_delete_old_attachments($post, $newfile_name);
1194 } else {
1195 notify("An error happened while saving the file on the server");
1196 $newfile_name = "";
1197 }
1198 }
1199 } else {
1200 $newfile_name = "";
1201 }
1202
1203 return $newfile_name;
1204}
501cdbd8 1205
11b0c469 1206function forum_add_new_post($post) {
501cdbd8 1207
ffe11640 1208 $post->created = $post->modified = time();
501cdbd8 1209 $post->mailed = "0";
1210
7f6689e4 1211 $newfile = $post->attachment;
1212 $post->attachment = "";
1213
1214 if (! $post->id = insert_record("forum_posts", $post)) {
1215 return false;
1216 }
1217
1218 if ($post->attachment = forum_add_attachment($post, $newfile)) {
1219 set_field("forum_posts", "attachment", $post->attachment, "id", $post->id);
1220 }
1221
1222 return $post->id;
501cdbd8 1223}
1224
11b0c469 1225function forum_update_post($post) {
501cdbd8 1226
ffe11640 1227 $post->modified = time();
501cdbd8 1228
0ab85112 1229 if (!$post->parent) { // Post is a discussion starter - update discussion title too
1230 set_field("forum_discussions", "name", $post->subject, "id", $post->discussion);
1231 }
7f6689e4 1232 if ($newfilename = forum_add_attachment($post, $post->attachment)) {
1233 $post->attachment = $newfilename;
1234 } else {
1235 unset($post->attachment);
1236 }
ffe11640 1237 return update_record("forum_posts", $post);
501cdbd8 1238}
1239
1240function forum_add_discussion($discussion) {
1241// Given an object containing all the necessary data,
1242// create a new discussion and return the id
1243
1244 GLOBAL $USER;
1245
1246 $timenow = time();
1247
1248 // The first post is stored as a real post, and linked
1249 // to from the discuss entry.
1250
1251 $post->discussion = 0;
1252 $post->parent = 0;
ebc3bd2b 1253 $post->userid = $USER->id;
501cdbd8 1254 $post->created = $timenow;
1255 $post->modified = $timenow;
1256 $post->mailed = 0;
1257 $post->subject = $discussion->name;
1258 $post->message = $discussion->intro;
7f6689e4 1259 $post->attachment = "";
1260 $post->forum = $discussion->forum;
1261 $post->course = $discussion->course;
501cdbd8 1262
1263 if (! $post->id = insert_record("forum_posts", $post) ) {
1264 return 0;
1265 }
1266
7f6689e4 1267 if ($post->attachment = forum_add_attachment($post, $discussion->attachment)) {
1268 set_field("forum_posts", "attachment", $post->attachment, "id", $post->id); //ignore errors
1269 }
1270
caadf009 1271 // Now do the real module entry
04eba58f 1272
caadf009 1273 $discussion->firstpost = $post->id;
1274 $discussion->timemodified = $timenow;
04eba58f 1275
caadf009 1276 if (! $discussion->id = insert_record("forum_discussions", $discussion) ) {
1277 delete_records("forum_posts", "id", $post->id);
1278 return 0;
04eba58f 1279 }
1280
caadf009 1281 // Finally, set the pointer on the post.
1282 if (! set_field("forum_posts", "discussion", $discussion->id, "id", $post->id)) {
1283 delete_records("forum_posts", "id", $post->id);
1284 delete_records("forum_discussions", "id", $discussion->id);
1285 return 0;
04eba58f 1286 }
04eba58f 1287
caadf009 1288 return $discussion->id;
1289}
04eba58f 1290
04eba58f 1291
caadf009 1292function forum_delete_discussion($discussion) {
1293// $discussion is a discussion record object
04eba58f 1294
1295 $result = true;
1296
caadf009 1297 if ($posts = get_records("forum_posts", "discussion", $discussion->id)) {
1298 foreach ($posts as $post) {
1299 $post->course = $discussion->course;
1300 $post->forum = $discussion->forum;
1301 if (! delete_records("forum_ratings", "post", "$post->id")) {
1302 $result = false;
1303 }
1304 if (! forum_delete_post($post)) {
04eba58f 1305 $result = false;
1306 }
1307 }
1308 }
1309
caadf009 1310 if (! delete_records("forum_discussions", "id", "$discussion->id")) {
04eba58f 1311 $result = false;
1312 }
1313
1314 return $result;
1315}
1316
1317
caadf009 1318function forum_delete_post($post) {
1319 if (delete_records("forum_posts", "id", $post->id)) {
1320 delete_records("forum_ratings", "post", $post->id); // Just in case
1321 if ($post->attachment) {
1322 $discussion = get_record("forum_discussions", "id", $post->discussion);
1323 $post->course = $discussion->course;
1324 $post->forum = $discussion->forum;
1325 forum_delete_old_attachments($post);
1326 }
1327 return true;
1328 }
1329 return false;
1330}
501cdbd8 1331
501cdbd8 1332
caadf009 1333function forum_print_user_discussions($courseid, $userid) {
1334 global $CFG, $USER;
501cdbd8 1335
1f48942e 1336
1337 if ($discussions = forum_get_user_discussions($courseid, $userid)) {
caadf009 1338 $user = get_record("user", "id", $userid);
1339 echo "<HR>";
1340 print_heading( get_string("discussionsstartedby", "forum", "$user->firstname $user->lastname") );
1341 $replies = forum_count_discussion_replies();
1342 foreach ($discussions as $discussion) {
1343 if (($discussion->forumtype == "teacher") and !isteacher($courseid)) {
ffe11640 1344 continue;
1345 }
47f1da80 1346 if (!empty($replies[$discussion->discussion])) {
caadf009 1347 $discussion->replies = $replies[$discussion->discussion]->replies;
1348 } else {
1349 $discussion->replies = 0;
501cdbd8 1350 }
caadf009 1351 $inforum = get_string("inforum", "forum", "<A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$discussion->forumid\">$discussion->forumname</A>");
1352 $discussion->subject .= " ($inforum)";
61e96406 1353 if (!empty($USER->id)) {
1354 $ownpost = ($discussion->userid == $USER->id);
1355 } else {
1356 $ownpost = false;
1357 }
caadf009 1358 forum_print_post($discussion, $courseid, $ownpost, $reply=0, $link=1, $assessed=false);
1359 echo "<BR>\n";
501cdbd8 1360 }
1361 }
501cdbd8 1362}
1363
501cdbd8 1364function forum_forcesubscribe($forumid, $value=1) {
1365 return set_field("forum", "forcesubscribe", $value, "id", $forumid);
1366}
1367
1368function forum_is_forcesubscribed($forumid) {
1369 return get_field("forum", "forcesubscribe", "id", $forumid);
1370}
1371
1372function forum_is_subscribed($userid, $forumid) {
1373 if (forum_is_forcesubscribed($forumid)) {
1374 return true;
1375 }
ebc3bd2b 1376 return record_exists("forum_subscriptions", "userid", $userid, "forum", $forumid);
86970225 1377}
1378
501cdbd8 1379function forum_subscribe($userid, $forumid) {
9fa49e22 1380/// Adds user to the subscriber list
1381
ebc3bd2b 1382 $sub->userid = $userid;
9fa49e22 1383 $sub->forum = $forumid;
501cdbd8 1384
9fa49e22 1385 return insert_record("forum_subscriptions", $sub);
501cdbd8 1386}
1387
1388function forum_unsubscribe($userid, $forumid) {
9fa49e22 1389/// Removes user from the subscriber list
ebc3bd2b 1390 return delete_records("forum_subscriptions", "userid", $userid, "forum", $forumid);
501cdbd8 1391}
1392
1393
11b0c469 1394function forum_user_has_posted_discussion($forumid, $userid) {
1395 if ($discussions = forum_get_discussions($forumid, "DESC", $userid)) {
501cdbd8 1396 return true;
1397 } else {
1398 return false;
1399 }
1400}
1401
11b0c469 1402function forum_user_can_post_discussion($forum) {
501cdbd8 1403// $forum is an object
1404 global $USER;
1405
1406 if ($forum->type == "eachuser") {
11b0c469 1407 return (! forum_user_has_posted_discussion($forum->id, $USER->id));
501cdbd8 1408 } else if ($forum->type == "teacher") {
1409 return isteacher($forum->course);
1410 } else if (isteacher($forum->course)) {
1411 return true;
1412 } else {
70c476a7 1413 return ($forum->open == 2);
501cdbd8 1414 }
1415}
1416
f690562f 1417function forum_user_can_post($forum, $user=NULL) {
1418// $forum, $user are objects
1419
1420 if ($user) {
1421 $isteacher = isteacher($forum->course, $user->id);
1422 } else {
1423 $isteacher = isteacher($forum->course);
1424 }
70c476a7 1425
1426 if ($forum->type == "teacher") {
f690562f 1427 return $isteacher;
1428 } else if ($isteacher) {
70c476a7 1429 return true;
1430 } else {
1431 return $forum->open;
1432 }
1433}
501cdbd8 1434
501cdbd8 1435
11b0c469 1436function forum_print_latest_discussions($forum_id=0, $forum_numdiscussions=5, $forum_style="plain", $forum_sort="DESC") {
c585fa17 1437 global $CFG, $USER;
f93f848a 1438
1439 if ($forum_id) {
1440 if (! $forum = get_record("forum", "id", $forum_id)) {
1441 error("Forum ID was incorrect");
1442 }
1443 if (! $course = get_record("course", "id", $forum->course)) {
1444 error("Could not find the course this forum belongs to!");
1445 }
1446
1447 if ($course->category) {
1448 require_login($course->id);
1449 }
1450
1451 } else {
1452 if (! $course = get_record("course", "category", 0)) {
1453 error("Could not find a top-level course!");
1454 }
7beb45d8 1455 if (! $forum = forum_get_course_forum($course->id, "news")) {
f93f848a 1456 error("Could not find or create a main forum in this course (id $course->id)");
1457 }
1458 }
1459
11b0c469 1460 if (forum_user_can_post_discussion($forum)) {
3b9af3dd 1461 echo "<P ALIGN=CENTER>";
ffe11640 1462 echo "<A HREF=\"$CFG->wwwroot/mod/forum/post.php?forum=$forum->id\">";
1463 echo get_string("addanewdiscussion", "forum")."</A>...";
501cdbd8 1464 echo "</P>\n";
77305fe6 1465 }
1466
11b0c469 1467 if (! $discussions = forum_get_discussions($forum->id, $forum_sort) ) {
ffe11640 1468 echo "<P ALIGN=CENTER><B>(".get_string("nodiscussions", "forum").")</B></P>";
3335f6fb 1469 return;
f93f848a 1470
3335f6fb 1471 }
1472
c585fa17 1473 if ((!$forum_numdiscussions) && ($forum_style == "plain") && (count($discussions) > FORUM_MANY_DISCUSSIONS) ) {
3335f6fb 1474 $forum_style = "header"; // Abbreviate display if it's going to be long.
1475 }
f93f848a 1476
3335f6fb 1477 $replies = forum_count_discussion_replies($forum->id);
f93f848a 1478
3260de67 1479 $canreply = forum_user_can_post($forum);
1480
3335f6fb 1481 $discussioncount = 0;
f93f848a 1482
dcde9f02 1483 if ($forum_style == "minimal") {
1484 $strftimerecent = get_string("strftimerecent");
1485 $strmore = get_string("more", "forum");
1486 }
1487
3335f6fb 1488 foreach ($discussions as $discussion) {
1489 $discussioncount++;
f93f848a 1490
3335f6fb 1491 if ($forum_numdiscussions && ($discussioncount > $forum_numdiscussions)) {
1492 echo "<P ALIGN=right><A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id\">";
1493 echo get_string("olderdiscussions", "forum")."</A> ...</P>";
1494 break;
1495 }
9c9f7d77 1496 if (!empty($replies[$discussion->discussion])) {
3335f6fb 1497 $discussion->replies = $replies[$discussion->discussion]->replies;
1498 } else {
1499 $discussion->replies = 0;
1500 }
f7477444 1501 if (!empty($USER->id)) {
1502 $ownpost = ($discussion->userid == $USER->id);
1503 } else {
1504 $ownpost=false;
1505 }
3335f6fb 1506 switch ($forum_style) {
1507 case "minimal":
dcde9f02 1508 echo "<P><FONT COLOR=#555555>".userdate($discussion->modified, $strftimerecent)." - $discussion->firstname</FONT>";
3335f6fb 1509 echo "<BR>$discussion->subject ";
1510 echo "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->discussion\">";
dcde9f02 1511 echo $strmore."...</A>";
3335f6fb 1512 echo "</P>\n";
1513 break;
1514 case "header":
1515 forum_print_post_header($discussion, $forum->course, $ownpost, $reply=0, $link=1, $assessed=false);
1516 break;
1517 default:
3260de67 1518 if ($canreply or $discussion->replies) {
1519 $link = true;
1520 } else {
1521 $link = false;
1522 }
1523 forum_print_post($discussion, $forum->course, $ownpost, $reply=0, $link, $assessed=false);
3335f6fb 1524 echo "<BR>\n";
1525 break;
f93f848a 1526 }
1527 }
f93f848a 1528}
1529
fe25fc9b 1530function forum_print_discussion($course, $forum, $discussion, $post, $mode) {
501cdbd8 1531
1532 global $USER;
1533
61e96406 1534 if (!empty($USER->id)) {
1535 $ownpost = ($USER->id == $post->userid);
1536 } else {
1537 $ownpost = false;
1538 }
70c476a7 1539 $reply = forum_user_can_post($forum);
501cdbd8 1540
70c476a7 1541 forum_print_post($post, $course->id, $ownpost, $reply, $link=false, $rate=false);
501cdbd8 1542
7a12aab4 1543 forum_print_mode_form($discussion->id, $mode);
501cdbd8 1544
9d1b97c5 1545 $ratingform = false;
61e96406 1546 if ($forum->assessed and !empty($USER->id)) {
9d1b97c5 1547 $unrated = forum_count_unrated_posts($discussion->id, $USER->id);
1548 if ($unrated > 0) {
1549 $ratingform = true;
1550 }
1551 }
1552
1553 if ($ratingform) {
501cdbd8 1554 echo "<FORM NAME=form METHOD=POST ACTION=rate.php>";
1555 echo "<INPUT TYPE=hidden NAME=id VALUE=\"$course->id\">";
1556 }
1557
1558 switch ($mode) {
1559 case 1 : // Flat ascending
1560 case -1 : // Flat descending
1561 default:
1562 echo "<UL>";
70c476a7 1563 forum_print_posts_flat($post->discussion, $course->id, $mode, $forum->assessed, $reply);
501cdbd8 1564 echo "</UL>";
1565 break;
1566
1567 case 2 : // Threaded
70c476a7 1568 forum_print_posts_threaded($post->id, $course->id, 0, $forum->assessed, $reply);
501cdbd8 1569 break;
1570
1571 case 3 : // Nested
70c476a7 1572 forum_print_posts_nested($post->id, $course->id, $forum->assessed, $reply);
501cdbd8 1573 break;
1574 }
1575
9d1b97c5 1576 if ($ratingform) {
31d160d3 1577 echo "<CENTER><P ALIGN=center><INPUT TYPE=submit VALUE=\"".get_string("sendinratings", "forum")."\">";
61ee082f 1578 helpbutton("ratings", get_string("separateandconnected"), "forum");
31d160d3 1579 echo "</P></CENTER>";
501cdbd8 1580 echo "</FORM>";
1581 }
1582}
1583
70c476a7 1584function forum_print_posts_flat($discussion, $course, $direction, $assessed, $reply) {
501cdbd8 1585 global $USER;
1586
501cdbd8 1587 $link = false;
1588
1589 if ($direction < 0) {
1590 $sort = "ORDER BY created DESC";
1591 } else {
1592 $sort = "ORDER BY created ASC";
1593 }
1594
1f48942e 1595 if ($posts = forum_get_discussion_posts($discussion, $sort)) {
501cdbd8 1596 foreach ($posts as $post) {
ebc3bd2b 1597 $ownpost = ($USER->id == $post->userid);
11b0c469 1598 forum_print_post($post, $course, $ownpost, $reply, $link, $assessed);
501cdbd8 1599 }
1600 } else {
1601 return;
1602 }
1603}
1604
70c476a7 1605function forum_print_posts_threaded($parent, $course, $depth, $assessed, $reply) {
501cdbd8 1606 global $USER;
1607
501cdbd8 1608 $link = false;
1609
1f48942e 1610 if ($posts = forum_get_child_posts($parent)) {
501cdbd8 1611 foreach ($posts as $post) {
1612
1613 echo "<UL>";
1614 if ($depth > 0) {
ebc3bd2b 1615 $ownpost = ($USER->id == $post->userid);
11b0c469 1616 forum_print_post($post, $course, $ownpost, $reply, $link, $assessed); // link=true?
501cdbd8 1617 echo "<BR>";
1618 } else {
8c3c8481 1619 $by->name = "$post->firstname $post->lastname";
1620 $by->date = userdate($post->created);
1621 echo "<LI><P><FONT SIZE=-1><B><A HREF=\"discuss.php?d=$post->discussion&parent=$post->id\">$post->subject</A></B> ";
1622 print_string("bynameondate", "forum", $by);
1623 echo "</FONT></P></LI>";
501cdbd8 1624 }
1625
70c476a7 1626 forum_print_posts_threaded($post->id, $course, $depth-1, $assessed, $reply);
501cdbd8 1627 echo "</UL>\n";
1628 }
1629 } else {
1630 return;
1631 }
1632}
1633
70c476a7 1634function forum_print_posts_nested($parent, $course, $assessed, $reply) {
501cdbd8 1635 global $USER;
1636
501cdbd8 1637 $link = false;
1638
1f48942e 1639 if ($posts = forum_get_child_posts($parent)) {
501cdbd8 1640 foreach ($posts as $post) {
1641
61e96406 1642 if (empty($USER->id)) {
1643 $ownpost = false;
1644 } else {
1645 $ownpost = ($USER->id == $post->userid);
1646 }
501cdbd8 1647
1648 echo "<UL>";
11b0c469 1649 forum_print_post($post, $course, $ownpost, $reply, $link, $assessed);
501cdbd8 1650 echo "<BR>";
70c476a7 1651 forum_print_posts_nested($post->id, $course, $assessed, $reply);
501cdbd8 1652 echo "</UL>\n";
1653 }
1654 } else {
1655 return;
1656 }
1657}
1658
1659function forum_set_display_mode($mode=0) {
5a83a0a8 1660 global $USER, $FORUM_DEFAULT_DISPLAY_MODE;
501cdbd8 1661
1662 if ($mode) {
1663 $USER->mode = $mode;
9c9f7d77 1664 } else if (empty($USER->mode)) {
501cdbd8 1665 $USER->mode = $FORUM_DEFAULT_DISPLAY_MODE;
1666 }
1667}
f93f848a 1668
3869a2ac 1669
f93f848a 1670?>