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