Polish and New translation.
[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
2e2e71a8 7define("FORUM_MODE_FLATOLDEST", 1);
8define("FORUM_MODE_FLATNEWEST", -1);
9define("FORUM_MODE_THREADED", 2);
10define("FORUM_MODE_NESTED", 3);
11
12$FORUM_LAYOUT_MODES = array ( FORUM_MODE_FLATOLDEST => get_string("modeflatoldestfirst", "forum"),
13 FORUM_MODE_FLATNEWEST => get_string("modeflatnewestfirst", "forum"),
14 FORUM_MODE_THREADED => get_string("modethreaded", "forum"),
15 FORUM_MODE_NESTED => get_string("modenested", "forum") );
f93f848a 16
11b0c469 17// These are course content forums that can be added to the course manually
ffe11640 18$FORUM_TYPES = array ("general" => get_string("generalforum", "forum"),
19 "eachuser" => get_string("eachuserforum", "forum"),
20 "single" => get_string("singleforum", "forum") );
f93f848a 21
70c476a7 22$FORUM_OPEN_MODES = array ("2" => get_string("openmode2", "forum"),
23 "1" => get_string("openmode1", "forum"),
24 "0" => get_string("openmode0", "forum") );
25
5be7800c 26if (!isset($CFG->forum_displaymode)) {
2e2e71a8 27 set_config("forum_displaymode", FORUM_MODE_NESTED);
65b0e537 28}
73bb0835 29
5be7800c 30if (!isset($CFG->forum_shortpost)) {
31 set_config("forum_shortpost", 300); // Less non-HTML characters than this is short
65b0e537 32}
4d871a72 33
5be7800c 34if (!isset($CFG->forum_longpost)) {
35 set_config("forum_longpost", 600); // More non-HTML characters than this is long
65b0e537 36}
4d871a72 37
5be7800c 38if (!isset($CFG->forum_manydiscussions)) {
39 set_config("forum_manydiscussions", 100); // Number of discussions on a page
65b0e537 40}
e07635f4 41
4909e176 42if (!isset($CFG->forum_maxbytes)) {
43 set_config("forum_maxbytes", 512000); // Default maximum size for all forums
65b0e537 44}
4909e176 45
1246ea56 46if (!isset($CFG->forum_replytouser)) {
47 set_config("forum_replytouser", true); // Default maximum size for all forums
48}
49
4909e176 50
e07635f4 51
caadf009 52/// STANDARD FUNCTIONS ///////////////////////////////////////////////////////////
53
54function forum_add_instance($forum) {
65b0e537 55// Given an object containing all the necessary data,
56// (defined by the form in mod.html) this function
57// will create a new instance and return the id number
caadf009 58// of the new instance.
59
60 global $CFG;
61
62 $forum->timemodified = time();
274e05db 63 $forum->intro = clean_text($forum->intro);
caadf009 64
f2f56406 65 if (!$forum->userating) {
66 $forum->assessed = 0;
67 }
68
98914efd 69 if (!empty($forum->ratingtime)) {
65b0e537 70 $forum->assesstimestart = make_timestamp($forum->startyear, $forum->startmonth, $forum->startday,
98914efd 71 $forum->starthour, $forum->startminute, 0);
65b0e537 72 $forum->assesstimefinish = make_timestamp($forum->finishyear, $forum->finishmonth, $forum->finishday,
98914efd 73 $forum->finishhour, $forum->finishminute, 0);
74 } else {
75 $forum->assesstimestart = 0;
76 $forum->assesstimefinish = 0;
77 }
caadf009 78
cb9a975f 79 if (! $forum->id = insert_record("forum", $forum)) {
80 return false;
81 }
82
98914efd 83 if ($forum->type == "single") { // Create related discussion.
caadf009 84 $discussion->course = $forum->course;
85 $discussion->forum = $forum->id;
86 $discussion->name = $forum->name;
87 $discussion->intro = $forum->intro;
88 $discussion->assessed = $forum->assessed;
89
90 if (! forum_add_discussion($discussion)) {
91 error("Could not add the discussion for this forum");
92 }
93 }
caadf009 94
95 return $forum->id;
96}
97
98
99function forum_update_instance($forum) {
65b0e537 100// Given an object containing all the necessary data,
101// (defined by the form in mod.html) this function
caadf009 102// will update an existing instance with new data.
103
104 $forum->timemodified = time();
105 $forum->id = $forum->instance;
106
f2f56406 107 if (!$forum->userating) {
108 $forum->assessed = 0;
109 }
110
98914efd 111 if (!empty($forum->ratingtime)) {
65b0e537 112 $forum->assesstimestart = make_timestamp($forum->startyear, $forum->startmonth, $forum->startday,
98914efd 113 $forum->starthour, $forum->startminute, 0);
65b0e537 114 $forum->assesstimefinish = make_timestamp($forum->finishyear, $forum->finishmonth, $forum->finishday,
98914efd 115 $forum->finishhour, $forum->finishminute, 0);
116 } else {
117 $forum->assesstimestart = 0;
118 $forum->assesstimefinish = 0;
119 }
120
caadf009 121 if ($forum->type == "single") { // Update related discussion and post.
122 if (! $discussion = get_record("forum_discussions", "forum", $forum->id)) {
123 if ($discussions = get_records("forum_discussions", "forum", $forum->id, "timemodified ASC")) {
124 notify("Warning! There is more than one discussion in this forum - using the most recent");
125 $discussion = array_pop($discussions);
126 } else {
127 error("Could not find the discussion in this forum");
128 }
129 }
130 if (! $post = get_record("forum_posts", "id", $discussion->firstpost)) {
131 error("Could not find the first post in this forum discussion");
132 }
133
134 $post->subject = $forum->name;
135 $post->message = $forum->intro;
136 $post->modified = $forum->timemodified;
137
138 if (! update_record("forum_posts", $post)) {
139 error("Could not update the first post");
140 }
141
142 $discussion->name = $forum->name;
143
144 if (! update_record("forum_discussions", $discussion)) {
145 error("Could not update the discussion");
146 }
147 }
148
69d79bc3 149 return update_record("forum", $forum);
caadf009 150}
151
152
153function forum_delete_instance($id) {
65b0e537 154// Given an ID of an instance of this module,
155// this function will permanently delete the instance
156// and any data that depends on it.
caadf009 157
158 if (! $forum = get_record("forum", "id", "$id")) {
159 return false;
160 }
161
162 $result = true;
163
164 if ($discussions = get_records("forum_discussions", "forum", $forum->id)) {
165 foreach ($discussions as $discussion) {
166 if (! forum_delete_discussion($discussion)) {
167 $result = false;
168 }
169 }
170 }
171
172 if (! delete_records("forum_subscriptions", "forum", "$forum->id")) {
173 $result = false;
174 }
175
176 if (! delete_records("forum", "id", "$forum->id")) {
177 $result = false;
178 }
179
180 return $result;
181}
182
183
184function forum_cron () {
edffca15 185/// Function to be run periodically according to the moodle cron
186/// Finds all posts that have yet to be mailed out, and mails them
187/// out to all subscribers
caadf009 188
aaf7a9dc 189 static $strforums = NULL;
190 if($strforums === NULL) {
191 $strforums = get_string('forums', 'forum');
192 }
193
a0330747 194 global $CFG, $USER, $THEME;
caadf009 195
ec2137ba 196 if (!empty($USER)) { // Remember real USER account if necessary
197 $realuser = $USER;
198 }
199
caadf009 200 $cutofftime = time() - $CFG->maxeditingtime;
201
1f48942e 202 if ($posts = forum_get_unmailed_posts($cutofftime)) {
caadf009 203
65b0e537 204 /// Mark them all now as being mailed. It's unlikely but possible there
205 /// might be an error later so that a post is NOT actually mailed out,
edffca15 206 /// but since mail isn't crucial, we can accept this risk. Doing it now
207 /// prevents the risk of duplicated mails, which is a worse problem.
208
65b0e537 209 foreach ($posts as $key => $post) {
edffca15 210 if (! set_field("forum_posts", "mailed", "1", "id", "$post->id")) {
211 echo "Error marking post id post->id as being mailed. This post will not be mailed.\n";
212 unset($posts[$key]);
213 }
214 }
215
caadf009 216 $timenow = time();
217
ca8e8a10 218 @set_time_limit(0); /// so that script does not get timed out when posting to many users
aaf7a9dc 219
caadf009 220 foreach ($posts as $post) {
221
edffca15 222 echo "\n";
caadf009 223 print_string("processingpost", "forum", $post->id);
edffca15 224 echo "\n";
caadf009 225
ebc3bd2b 226 if (! $userfrom = get_record("user", "id", "$post->userid")) {
227 echo "Could not find user $post->userid\n";
caadf009 228 continue;
229 }
230
49d9b738 231 $userfrom->precedence = "bulk"; // This gets added to the email header
232
caadf009 233 if (! $discussion = get_record("forum_discussions", "id", "$post->discussion")) {
234 echo "Could not find discussion $post->discussion\n";
235 continue;
236 }
237
238 if (! $forum = get_record("forum", "id", "$discussion->forum")) {
239 echo "Could not find forum $discussion->forum\n";
240 continue;
241 }
242
243 if (! $course = get_record("course", "id", "$forum->course")) {
244 echo "Could not find course $forum->course\n";
245 continue;
246 }
247
e1fb2e6e 248 if (!empty($course->lang)) {
249 $CFG->courselang = $course->lang;
250 } else {
251 unset($CFG->courselang);
252 }
9c84314e 253
9197e147 254 $groupmode = false;
255 if ($cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
2862b309 256 if ($groupmode = groupmode($course, $cm) and $discussion->groupid > 0) { // Groups are being used
2f9c977e 257 if (!$group = get_record("groups", "id", $discussion->groupid)) { // Can't find group
9197e147 258 continue; // Be safe and don't send it to anyone
259 }
260 }
69d79bc3 261 } else {
262 $cm->id = 0;
263 }
9197e147 264
caadf009 265 if ($users = forum_subscribed_users($course, $forum)) {
caadf009 266
267 $mailcount=0;
3d94772d 268 $errorcount=0;
caadf009 269 foreach ($users as $userto) {
9197e147 270 if ($groupmode) { // Look for a reason not to send this email
271 if (!isteacheredit($course->id, $userto->id)) {
2862b309 272 if (!empty($group->id)) {
273 if (!ismember($group->id, $userto->id)) {
274 continue;
275 }
9197e147 276 }
277 }
278 }
65b0e537 279
aaf7a9dc 280 if ($userto->maildigest > 0) {
281 // This user wants the mails to be in digest form
282 $queue = New stdClass;
283 $queue->userid = $userto->id;
284 $queue->discussionid = $discussion->id;
285 $queue->postid = $post->id;
286 if(!insert_record('forum_queue', $queue)) {
287 echo "Error: mod/forum/cron.php: Could not queue for digest mail for id $post->id to user $userto->id ($userto->email) .. not trying again.\n";
288 }
289 continue;
290 }
65b0e537 291
292 /// Override the language and timezone of the "current" user, so that
ec2137ba 293 /// mail is customised for the receiver.
294 $USER->lang = $userto->lang;
295 $USER->timezone = $userto->timezone;
296
caadf009 297 $postsubject = "$course->shortname: $post->subject";
aaf7a9dc 298 $posttext = forum_make_mail_text($course, $forum, $discussion, $post, $userfrom, $userto);
299 $posthtml = forum_make_mail_html($course, $forum, $discussion, $post, $userfrom, $userto);
caadf009 300
50a26903 301 if (!$mailresult = email_to_user($userto, $userfrom, $postsubject, $posttext,
302 $posthtml, '', '', $CFG->forum_replytouser)) {
303 echo "Error: mod/forum/cron.php: Could not send out mail for id $post->id to user $userto->id".
304 " ($userto->email) .. not trying again.\n";
305 add_to_log($course->id, 'forum', 'mail error', "discuss.php?d=$discussion->id#$post->id",
306 substr($post->subject,0,30), $cm->id, $userto->id);
aaf7a9dc 307 $errorcount++;
b6268a0e 308 } else if ($mailresult === 'emailstop') {
50a26903 309 add_to_log($course->id, 'forum', 'mail blocked', "discuss.php?d=$discussion->id#$post->id",
310 substr($post->subject,0,30), $cm->id, $userto->id);
caadf009 311 } else {
aaf7a9dc 312 $mailcount++;
caadf009 313 }
aaf7a9dc 314 }
315
316 echo ".... mailed to $mailcount users.\n";
317 if ($errorcount) {
318 set_field("forum_posts", "mailed", "2", "id", "$post->id");
319 }
320 }
321 }
322 }
323
6e6a64b3 324 unset($CFG->courselang);
325
aaf7a9dc 326 /// Now see if there are any digest mails waiting to be sent, and if we should send them
aaf7a9dc 327
ca8e8a10 328 if (!isset($CFG->digestmailtimelast)) { // To catch the first time
329 set_config('digestmailtimelast', 0);
330 }
331
332 $timenow = time();
333 $digesttime = usergetmidnight($timenow) + ($CFG->digestmailtime * 3600);
334
335 if ($CFG->digestmailtimelast < $digesttime and $timenow > $digesttime) {
336 set_config('digestmailtimelast', $timenow);
aaf7a9dc 337
aaf7a9dc 338 $digestposts = get_records('forum_queue');
339 if(!empty($digestposts)) {
340
341 // We have work to do
342 $usermailcount = 0;
343 $site = get_site();
344
345 $users = array();
346 $posts = array();
347 $discussions = array();
348 $discussionposts = array();
349 $userdiscussions = array();
350 foreach($digestposts as $digestpost) {
351 if(!isset($users[$digestpost->userid])) {
352 $users[$digestpost->userid] = get_record('user', 'id', $digestpost->userid);
353 }
354 if(!isset($discussions[$digestpost->discussionid])) {
355 $discussions[$digestpost->discussionid] = get_record('forum_discussions', 'id', $digestpost->discussionid);
356 }
357 if(!isset($posts[$digestpost->postid])) {
358 $posts[$digestpost->postid] = get_record('forum_posts', 'id', $digestpost->postid);
359 }
360 $userdiscussions[$digestpost->userid][$digestpost->discussionid] = $digestpost->discussionid;
361 $discussionposts[$digestpost->discussionid][$digestpost->postid] = $digestpost->postid;
362 }
363
364 // Data collected, start sending out emails to each user
365
366 foreach($userdiscussions as $userid => $thesediscussions) {
367
368 echo "\n".get_string('processingdigest', 'forum', $userid).'... ';
369
370 // First of all delete all the queue entries for this user
371 delete_records('forum_queue', 'userid', $userid);
372 $userto = $users[$userid];
373
374 /// Override the language and timezone of the "current" user, so that
375 /// mail is customised for the receiver.
a0330747 376 $USER->lang = $userto->lang;
377 $USER->timezone = $userto->timezone;
aaf7a9dc 378
379
380 $postsubject = get_string('digestmailsubject', 'forum', $site->shortname);
381 $headerdata = New stdClass;
382 $headerdata->sitename = $site->fullname;
383 $headerdata->userprefs = $CFG->wwwroot.'/user/edit.php?id='.$userid.'&course='.$site->id;
384
385 $posttext = get_string('digestmailheader', 'forum', $headerdata)."\n\n";
386 $headerdata->userprefs = '<a target="_blank" href="'.$headerdata->userprefs.'">'.get_string('digestmailprefs', 'forum').'</a>';
9c674431 387
388 $posthtml = "<head><link rel=\"stylesheet\" type=\"text/css\" href=\"".$CFG->stylesheet."\" /></head>\n";
389 $posthtml .= "<body bgcolor=\"$THEME->cellcontent2\">";
390 $posthtml .= "<style> <!--"; /// Inline styles for autolinks
391 $posthtml .= "a.autolink:link {text-decoration: none; color: black; background-color: $THEME->autolink}\n";
392 $posthtml .= "a.autolink:visited {text-decoration: none; color: black; background-color: $THEME->autolink}\n";
393 $posthtml .= "a.autolink:hover {text-decoration: underline; color: red}\n";
394 $posthtml .= "--> </style>\n\n";
a0330747 395 $posthtml .= '<p>'.get_string('digestmailheader', 'forum', $headerdata).'</p><br /><hr size="1" noshade="noshade" />';
aaf7a9dc 396
397 foreach($thesediscussions as $discussionid) {
398 $discussion = $discussions[$discussionid];
399 if(empty($discussion)) {
400 echo "Could not find discussion $discussionid\n";
401 continue;
caadf009 402 }
aaf7a9dc 403
404 if (! $forum = get_record("forum", "id", "$discussion->forum")) {
405 echo "Could not find forum $discussion->forum\n";
406 continue;
f690562f 407 }
aaf7a9dc 408 if (! $course = get_record("course", "id", "$forum->course")) {
409 echo "Could not find course $forum->course\n";
410 continue;
caadf009 411 }
65b0e537 412
aaf7a9dc 413 $canunsubscribe = ! forum_is_forcesubscribed($forum->id);
414 $canreply = forum_user_can_post($forum, $userto);
caadf009 415
caadf009 416
aaf7a9dc 417 $posttext .= "\n \n";
418 $posttext .= '=====================================================================';
419 $posttext .= "\n \n";
420 $posttext .= "$course->shortname -> $strforums -> $forum->name";
421 if ($discussion->name != $forum->name) {
422 $posttext .= " -> $discussion->name";
caadf009 423 }
aaf7a9dc 424 $posttext .= "\n";
65b0e537 425
aaf7a9dc 426 $posthtml .= "<p><font face=\"sans-serif\">".
427 "<a target=\"_blank\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> -> ".
428 "<a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/index.php?id=$course->id\">$strforums</a> -> ".
429 "<a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id\">$forum->name</a>";
430 if ($discussion->name == $forum->name) {
431 $posthtml .= "</font></p>";
caadf009 432 } else {
aaf7a9dc 433 $posthtml .= " -> <a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->id\">$discussion->name</a></font></p>";
caadf009 434 }
aaf7a9dc 435 $posthtml .= '<p>';
436
e1c6dde1 437 $postsarray = $discussionposts[$discussionid];
438 sort($postsarray);
439
440 foreach($postsarray as $postid) {
aaf7a9dc 441 if (! $post = get_record("forum_posts", "id", "$postid")) {
442 echo "Could not find post $postid\n";
443 continue;
444 }
445 if (! $userfrom = get_record("user", "id", "$post->userid")) {
446 echo "Could not find user $post->userid\n";
447 continue;
448 }
449
450 $userfrom->precedence = "bulk"; // This gets added to the email header
451 if($userto->maildigest == 2) {
452 // Subjects only
453 $by = New stdClass;
454 $by->name = fullname($userfrom);
455 $by->date = userdate($post->modified);
456 $posttext .= "\n".$post->subject.' '.get_string("bynameondate", "forum", $by);
457 $posttext .= "\n---------------------------------------------------------------------";
458
459 $by->name = "<a target=\"_blank\" href=\"$CFG->wwwroot/user/view.php?id=$userfrom->id&course=$course->id\">$by->name</a>";
460 $posthtml .= '<div><a target="_blank" href="'.$CFG->wwwroot.'/mod/forum/discuss.php?d='.$discussion->id.'#'.$post->id.'">'.$post->subject.'</a> '.get_string("bynameondate", "forum", $by).'</div>';
461 }
462 else {
463 // The full treatment
464 $posttext .= forum_make_mail_text($course, $forum, $discussion, $post, $userfrom, $userto, true);
465 $posthtml .= forum_make_mail_post($post, $userfrom, $userto, $course, false, $canreply, false, false);
466 }
467 }
468 if ($canunsubscribe) {
469 $posthtml .= "\n<div align=\"right\"><font size=\"1\"><a href=\"$CFG->wwwroot/mod/forum/subscribe.php?id=$forum->id\">".get_string("unsubscribe", "forum")."</a></font></div>";
470 }
471 else {
472 $posthtml .= "\n<div align=\"right\"><font size=\"1\">".get_string("everyoneissubscribed", "forum")."</font></div>";
473 }
474 $posthtml .= '<hr size="1" noshade="noshade" /></p>';
caadf009 475 }
a0330747 476 $posthtml .= '</body>';
caadf009 477
50a26903 478 if (!$mailresult = email_to_user($userto, $site->shortname, $postsubject, $posttext, $posthtml,
479 '', '', $CFG->forum_replytouser)) {
aaf7a9dc 480 echo "ERROR!\n";
481 echo "Error: mod/forum/cron.php: Could not send out digest mail to user $userto->id ($userto->email)... not trying again.\n";
482 add_to_log($course->id, 'forum', 'mail digest error', '', '', $cm->id, $userto->id);
b6268a0e 483 } else if ($mailresult === 'emailstop') {
50a26903 484 add_to_log($course->id, 'forum', 'mail digest blocked', '', '', $cm->id, $userto->id);
aaf7a9dc 485 } else {
486 echo "success.\n";
487 $usermailcount++;
3d94772d 488 }
aaf7a9dc 489
caadf009 490 }
aaf7a9dc 491
caadf009 492 }
493 }
494
d658167f 495 if(!empty($usermailcount)) {
aaf7a9dc 496 echo "\n".get_string('digestsentusers', 'forum', $usermailcount)."\n";
497 }
498
ec2137ba 499 if (!empty($realuser)) { // Restore real USER if necessary
500 $USER = $realuser;
501 }
502
caadf009 503 return true;
504}
505
aaf7a9dc 506function forum_make_mail_text($course, $forum, $discussion, $post, $userfrom, $userto, $bare = false) {
507 global $CFG;
508
aaf7a9dc 509 $by = New stdClass;
510 $by->name = fullname($userfrom, isteacher($course->id, $userto->id));
511 $by->date = userdate($post->modified, "", $userto->timezone);
512
513 $strbynameondate = get_string('bynameondate', 'forum', $by);
514
64762ddc 515 $strforums = get_string('forums', 'forum');
516
aaf7a9dc 517 $canunsubscribe = ! forum_is_forcesubscribed($forum->id);
518 $canreply = forum_user_can_post($forum, $userto);
519
520 $posttext = '';
521
522 if(!$bare) {
523 $posttext = "$course->shortname -> $strforums -> $forum->name";
524
525 if ($discussion->name != $forum->name) {
526 $posttext .= " -> $discussion->name";
527 }
528 }
529
530 $posttext .= "\n---------------------------------------------------------------------\n";
531 $posttext .= $post->subject;
532 if($bare) {
533 $posttext .= " ($CFG->wwwroot/mod/forum/discuss.php?d=$discussion->id#$post->id)";
534 }
535 $posttext .= "\n".$strbynameondate."\n";
536 $posttext .= "---------------------------------------------------------------------\n";
537 $posttext .= format_text_email($post->message, $post->format);
538 $posttext .= "\n\n";
539 if ($post->attachment) {
540 $post->course = $course->id;
541 $post->forum = $forum->id;
542 $posttext .= forum_print_attachments($post, "text");
543 }
544 if (!$bare && $canreply) {
545 $posttext .= "---------------------------------------------------------------------\n";
546 $posttext .= get_string("postmailinfo", "forum", $course->shortname)."\n";
547 $posttext .= "$CFG->wwwroot/mod/forum/post.php?reply=$post->id\n";
548 }
549 if (!$bare && $canunsubscribe) {
550 $posttext .= "\n---------------------------------------------------------------------\n";
551 $posttext .= get_string("unsubscribe", "forum");
552 $posttext .= ": $CFG->wwwroot/mod/forum/subscribe.php?id=$forum->id\n";
553 }
554
555 return $posttext;
556}
557
558function forum_make_mail_html($course, $forum, $discussion, $post, $userfrom, $userto) {
559 global $CFG;
560
aaf7a9dc 561 if ($userto->mailformat == 1) { // HTML
562
64762ddc 563 $strforums = get_string('forums', 'forum');
aaf7a9dc 564 $canreply = forum_user_can_post($forum, $userto);
565 $canunsubscribe = ! forum_is_forcesubscribed($forum->id);
566
9c674431 567 $posthtml = '';
568 $posthtml .= "<head><link rel=\"stylesheet\" type=\"text/css\" href=\"".$CFG->stylesheet."\" /></head>\n";
569 $posthtml .= "<body><style> <!--"; /// Inline styles for autolinks
570 $posthtml .= "a.autolink:link {text-decoration: none; color: black; background-color: $THEME->autolink}\n";
571 $posthtml .= "a.autolink:visited {text-decoration: none; color: black; background-color: $THEME->autolink}\n";
572 $posthtml .= "a.autolink:hover {text-decoration: underline; color: red}\n";
573 $posthtml .= "--> </style>\n\n";
574
575 $posthtml .= "<p><font face=\"sans-serif\">".
576 "<a target=\"_blank\" href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> &raquo; ".
577 "<a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/index.php?id=$course->id\">$strforums</a> &raquo; ".
aaf7a9dc 578 "<a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id\">$forum->name</a>";
579 if ($discussion->name == $forum->name) {
580 $posthtml .= "</font></p>";
581 } else {
9c674431 582 $posthtml .= " &raquo; <a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->id\">$discussion->name</a></font></p>";
aaf7a9dc 583 }
584 $posthtml .= forum_make_mail_post($post, $userfrom, $userto, $course, false, $canreply, false, false);
585
586 if ($canunsubscribe) {
587 $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>";
588 }
589
590 return $posthtml;
591
592 } else {
64762ddc 593 return '';
aaf7a9dc 594 }
595}
596
caadf009 597function forum_user_outline($course, $user, $mod, $forum) {
598
1f48942e 599 if ($posts = forum_get_user_posts($forum->id, $user->id)) {
caadf009 600 $result->info = get_string("numposts", "forum", count($posts));
601
602 $lastpost = array_pop($posts);
603 $result->time = $lastpost->modified;
604 return $result;
605 }
606 return NULL;
607}
608
609
610function forum_user_complete($course, $user, $mod, $forum) {
611 global $CFG;
612
1f48942e 613 if ($posts = forum_get_user_posts($forum->id, $user->id)) {
caadf009 614 foreach ($posts as $post) {
497588fe 615 forum_print_post($post, $course->id, $ownpost=false, $reply=false, $link=false, $rate=false);
caadf009 616 }
617
618 } else {
619 echo "<P>".get_string("noposts", "forum")."</P>";
620 }
caadf009 621}
622
1b5910c4 623function forum_print_recent_activity($course, $isteacher, $timestart) {
65b0e537 624/// Given a course and a date, prints a summary of all the new
cc3655a2 625/// messages posted in the course since that date
626
3d891989 627 global $CFG;
caadf009 628
629 $heading = false;
630 $content = false;
631
1b5910c4 632 if (!$logs = get_records_select("log", "time > '$timestart' AND ".
633 "course = '$course->id' AND ".
634 "module = 'forum' AND ".
635 "action LIKE 'add %' ", "time ASC")){
636 return false;
637 }
638
dcde9f02 639 $strftimerecent = get_string("strftimerecent");
640
d05956ac 641 $isteacheredit = isteacheredit($course->id);
642 $mygroupid = mygroupid($course->id);
643
644 $groupmode = array(); /// To cache group modes
645
1b5910c4 646 foreach ($logs as $log) {
647 //Get post info, I'll need it later
648 $post = forum_get_post_from_log($log);
65b0e537 649
1b5910c4 650 //Create a temp valid module structure (course,id)
651 $tempmod->course = $log->course;
652 $tempmod->id = $post->forum;
653 //Obtain the visible property from the instance
b91d6dcd 654 $modvisible = instance_is_visible($log->module, $tempmod);
65b0e537 655
1b5910c4 656 //Only if the mod is visible
657 if ($modvisible) {
658 if ($post) {
b91d6dcd 659 /// Check whether this is for teachers only
1b5910c4 660 $teacheronly = "";
b91d6dcd 661 if ($forum = get_record("forum", "id", $post->forum)) {
1b5910c4 662 if ($forum->type == "teacher") {
663 if ($isteacher) {
664 $teacheronly = "class=\"teacheronly\"";
ad08fdc2 665 } else {
1b5910c4 666 continue;
ad08fdc2 667 }
caadf009 668 }
669 }
65b0e537 670 /// Check whether this is belongs to a discussion in a group that
d05956ac 671 /// should NOT be accessible to the current user
672
2862b309 673 if (!$isteacheredit and $post->groupid != -1) { /// Editing teachers or open discussions
d05956ac 674 if (!isset($cm[$post->forum])) {
675 $cm[$forum->id] = get_coursemodule_from_instance("forum", $forum->id, $course->id);
676 $groupmode[$forum->id] = groupmode($course, $cm[$forum->id]);
677 }
c3fcf3f3 678 if ($groupmode[$forum->id]) {
2f9c977e 679 if ($mygroupid != $post->groupid) {
d05956ac 680 continue;
b91d6dcd 681 }
682 }
683 }
684
1b5910c4 685 if (! $heading) {
686 print_headline(get_string("newforumposts", "forum").":");
687 $heading = true;
688 $content = true;
689 }
690 $date = userdate($post->modified, $strftimerecent);
1b26d5e7 691 $fullname = fullname($post, $isteacher);
692 echo "<p $teacheronly><font size=1>$date - $fullname<br>";
1b5910c4 693 echo "\"<a href=\"$CFG->wwwroot/mod/forum/$log->url\">";
436a7cae 694 if (!empty($CFG->filterall)) {
34e934a9 695 $post->subject = filter_text("<nolink>$post->subject</nolink>", $course->id);
436a7cae 696 }
1b5910c4 697 if ($log->action == "add discussion") {
698 echo "<b>$post->subject</b>";
699 } else {
700 echo "$post->subject";
701 }
702 echo "</a>\"</font></p>";
caadf009 703 }
704 }
705 }
1b5910c4 706
caadf009 707 return $content;
708}
709
710
711function forum_grades($forumid) {
712/// Must return an array of grades, indexed by user, and a max grade.
caadf009 713
4db9d14d 714 if (!$forum = get_record("forum", "id", $forumid)) {
715 return false;
716 }
717 if (!$forum->assessed) {
718 return false;
719 }
d6bdd9d5 720 $scalemenu = make_grades_menu($forum->scale);
02ebf404 721
722 $currentuser = 0;
723 $ratingsuser = array();
724
1f48942e 725 if ($ratings = forum_get_user_grades($forumid)) {
02ebf404 726 foreach ($ratings as $rating) { // Ordered by user
727 if ($currentuser and $rating->userid != $currentuser) {
728 if (!empty($ratingsuser)) {
d6bdd9d5 729 if ($forum->scale < 0) {
730 $return->grades[$currentuser] = forum_get_ratings_mean(0, $scalemenu, $ratingsuser);
731 $return->grades[$currentuser] .= "<br />".forum_get_ratings_summary(0, $scalemenu, $ratingsuser);
732 } else {
733 $total = 0;
734 $count = 0;
735 foreach ($ratingsuser as $ra) {
736 $total += $ra;
737 $count ++;
738 }
739 $return->grades[$currentuser] = format_float($total/$count, 2);
740 }
02ebf404 741 } else {
742 $return->grades[$currentuser] = "";
743 }
744 $ratingsuser = array();
caadf009 745 }
02ebf404 746 $ratingsuser[] = $rating->rating;
747 $currentuser = $rating->userid;
caadf009 748 }
02ebf404 749 if (!empty($ratingsuser)) {
d6bdd9d5 750 if ($forum->scale < 0) {
751 $return->grades[$currentuser] = forum_get_ratings_mean(0, $scalemenu, $ratingsuser);
752 $return->grades[$currentuser] .= "<br />".forum_get_ratings_summary(0, $scalemenu, $ratingsuser);
753 } else {
754 $total = 0;
755 $count = 0;
756 foreach ($ratingsuser as $ra) {
757 $total += $ra;
758 $count ++;
759 }
760 $return->grades[$currentuser] = format_float((float)$total/(float)$count, 2);
761 }
02ebf404 762 } else {
763 $return->grades[$currentuser] = "";
caadf009 764 }
765 } else {
766 $return->grades = array();
767 }
768
d6bdd9d5 769 if ($forum->scale < 0) {
770 $return->maxgrade = "";
771 } else {
772 $return->maxgrade = $forum->scale;
773 }
caadf009 774 return $return;
775}
776
05855091 777function forum_get_participants($forumid) {
778//Returns the users with data in one forum
65b0e537 779//(users with records in forum_subscriptions, forum_posts and forum_ratings, students)
05855091 780
781 global $CFG;
782
783 //Get students from forum_subscriptions
784 $st_subscriptions = get_records_sql("SELECT DISTINCT u.*
785 FROM {$CFG->prefix}user u,
786 {$CFG->prefix}forum_subscriptions s
787 WHERE s.forum = '$forumid' and
65b0e537 788 u.id = s.userid");
05855091 789 //Get students from forum_posts
790 $st_posts = get_records_sql("SELECT DISTINCT u.*
791 FROM {$CFG->prefix}user u,
792 {$CFG->prefix}forum_discussions d,
793 {$CFG->prefix}forum_posts p
794 WHERE d.forum = '$forumid' and
795 p.discussion = d.id and
796 u.id = p.userid");
797
798 //Get students from forum_ratings
799 $st_ratings = get_records_sql("SELECT DISTINCT u.*
800 FROM {$CFG->prefix}user u,
801 {$CFG->prefix}forum_discussions d,
802 {$CFG->prefix}forum_posts p,
803 {$CFG->prefix}forum_ratings r
804 WHERE d.forum = '$forumid' and
805 p.discussion = d.id and
806 r.post = p.id and
807 u.id = r.userid");
808
809 //Add st_posts to st_subscriptions
810 if ($st_posts) {
811 foreach ($st_posts as $st_post) {
812 $st_subscriptions[$st_post->id] = $st_post;
813 }
814 }
815 //Add st_ratings to st_subscriptions
816 if ($st_ratings) {
817 foreach ($st_ratings as $st_rating) {
818 $st_subscriptions[$st_rating->id] = $st_rating;
819 }
820 }
821 //Return st_subscriptions array (it contains an array of unique users)
822 return ($st_subscriptions);
823}
caadf009 824
0f1a97c2 825function forum_scale_used ($forumid,$scaleid) {
826//This function returns if a scale is being used by one forum
65b0e537 827
0f1a97c2 828 $return = false;
65b0e537 829
0f1a97c2 830 $rec = get_record("forum","id","$forumid","scale","-$scaleid");
65b0e537 831
832 if (!empty($rec) && !empty($scaleid)) {
0f1a97c2 833 $return = true;
834 }
65b0e537 835
0f1a97c2 836 return $return;
837}
838
9fa49e22 839/// SQL FUNCTIONS ///////////////////////////////////////////////////////////
840
1f48942e 841function forum_get_post_full($postid) {
842/// Gets a post with all info ready for forum_print_post
843 global $CFG;
844
ebc3bd2b 845 return get_record_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture
65b0e537 846 FROM {$CFG->prefix}forum_posts p,
847 {$CFG->prefix}user u
848 WHERE p.id = '$postid'
ebc3bd2b 849 AND p.userid = u.id");
1f48942e 850}
851
852function forum_get_discussion_posts($discussion, $sort) {
853/// Gets posts with all info ready for forum_print_post
854 global $CFG;
855
ebc3bd2b 856 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture
65b0e537 857 FROM {$CFG->prefix}forum_posts p,
858 {$CFG->prefix}user u
859 WHERE p.discussion = $discussion
860 AND p.parent > 0
ebc3bd2b 861 AND p.userid = u.id $sort");
1f48942e 862}
863
864function forum_get_child_posts($parent) {
865/// Gets posts with all info ready for forum_print_post
866 global $CFG;
867
ebc3bd2b 868 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture
65b0e537 869 FROM {$CFG->prefix}forum_posts p,
870 {$CFG->prefix}user u
871 WHERE p.parent = '$parent'
ebc3bd2b 872 AND p.userid = u.id
1f48942e 873 ORDER BY p.created ASC");
874}
875
876
8b9c7aa0 877function forum_search_posts($searchterms, $courseid, $page=0, $recordsperpage=50, &$totalcount) {
878/// Returns a list of posts found using an array of search terms
879/// eg word +word -word
880///
881
9fa49e22 882 global $CFG;
883
884 if (!isteacher($courseid)) {
885 $notteacherforum = "AND f.type <> 'teacher'";
4cabd355 886
887 $forummodule = get_record("modules", "name", "forum");
888 $onlyvisible = " AND f.id = cm.instance AND cm.visible = 1 AND cm.module = $forummodule->id";
889 $onlyvisibletable = ", {$CFG->prefix}course_modules cm";
9fa49e22 890 } else {
891 $notteacherforum = "";
4cabd355 892
893 $onlyvisible = "";
894 $onlyvisibletable = "";
9fa49e22 895 }
896
c2a96d6b 897 switch ($CFG->dbtype) {
898 case "mysql":
899 $limit = "LIMIT $page,$recordsperpage";
900 break;
901 case "postgres7":
902 $limit = "LIMIT $recordsperpage OFFSET ".($page * $recordsperpage);
903 break;
65b0e537 904 default:
c2a96d6b 905 $limit = "LIMIT $recordsperpage,$page";
97485d07 906 }
907
63d99fcb 908 /// Some differences in syntax for PostgreSQL
909 if ($CFG->dbtype == "postgres7") {
a74f950c 910 $LIKE = "ILIKE"; // case-insensitive
2b4c64e9 911 $NOTLIKE = "NOT ILIKE"; // case-insensitive
912 $REGEXP = "~*";
913 $NOTREGEXP = "!~*";
b800ac5a 914 } else {
a74f950c 915 $LIKE = "LIKE";
2b4c64e9 916 $NOTLIKE = "NOT LIKE";
a74f950c 917 $REGEXP = "REGEXP";
2b4c64e9 918 $NOTREGEXP = "NOT REGEXP";
c4c57597 919 }
920
b800ac5a 921 $messagesearch = "";
922 $subjectsearch = "";
923
b800ac5a 924
925 foreach ($searchterms as $searchterm) {
8b9c7aa0 926 if (strlen($searchterm) < 2) {
927 continue;
928 }
b800ac5a 929 if ($messagesearch) {
930 $messagesearch .= " AND ";
931 }
b800ac5a 932 if ($subjectsearch) {
933 $subjectsearch .= " AND ";
934 }
63d99fcb 935
a74f950c 936 if (substr($searchterm,0,1) == "+") {
937 $searchterm = substr($searchterm,1);
63d99fcb 938 $messagesearch .= " p.message $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
939 $subjectsearch .= " p.subject $REGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
a74f950c 940 } else if (substr($searchterm,0,1) == "-") {
941 $searchterm = substr($searchterm,1);
2b4c64e9 942 $messagesearch .= " p.message $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
943 $subjectsearch .= " p.subject $NOTREGEXP '(^|[^a-zA-Z0-9])$searchterm([^a-zA-Z0-9]|$)' ";
63d99fcb 944 } else {
945 $messagesearch .= " p.message $LIKE '%$searchterm%' ";
946 $subjectsearch .= " p.subject $LIKE '%$searchterm%' ";
947 }
b800ac5a 948 }
949
65b0e537 950 $selectsql = "{$CFG->prefix}forum_posts p,
951 {$CFG->prefix}forum_discussions d,
952 {$CFG->prefix}user u,
4cabd355 953 {$CFG->prefix}forum f $onlyvisibletable
8b9c7aa0 954 WHERE ($messagesearch OR $subjectsearch)
65b0e537 955 AND p.userid = u.id
956 AND p.discussion = d.id
957 AND d.course = '$courseid'
4cabd355 958 AND d.forum = f.id $notteacherforum $onlyvisible";
65b0e537 959
8b9c7aa0 960 $totalcount = count_records_sql("SELECT COUNT(*) FROM $selectsql");
b800ac5a 961
8b9c7aa0 962 return get_records_sql("SELECT p.*,u.firstname,u.lastname,u.email,u.picture FROM
963 $selectsql ORDER BY p.modified DESC $limit");
9fa49e22 964}
965
63d99fcb 966
9fa49e22 967function forum_get_ratings($postid, $sort="u.firstname ASC") {
968/// Returns a list of ratings for a particular post - sorted.
969 global $CFG;
65b0e537 970 return get_records_sql("SELECT u.*, r.rating, r.time
971 FROM {$CFG->prefix}forum_ratings r,
9fa49e22 972 {$CFG->prefix}user u
65b0e537 973 WHERE r.post = '$postid'
974 AND r.userid = u.id
9fa49e22 975 ORDER BY $sort");
976}
977
1f48942e 978function forum_get_unmailed_posts($cutofftime) {
979/// Returns a list of all new posts that have not been mailed yet
980 global $CFG;
65b0e537 981 return get_records_sql("SELECT p.*, d.course
982 FROM {$CFG->prefix}forum_posts p,
1f48942e 983 {$CFG->prefix}forum_discussions d
65b0e537 984 WHERE p.mailed = 0
985 AND p.created < '$cutofftime'
1f48942e 986 AND p.discussion = d.id");
987}
988
989function forum_get_user_posts($forumid, $userid) {
990/// Get all the posts for a user in a forum suitable for forum_print_post
991 global $CFG;
992
ebc3bd2b 993 return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture
65b0e537 994 FROM {$CFG->prefix}forum f,
995 {$CFG->prefix}forum_discussions d,
996 {$CFG->prefix}forum_posts p,
997 {$CFG->prefix}user u
998 WHERE f.id = '$forumid'
999 AND d.forum = f.id
1f48942e 1000 AND p.discussion = d.id
65b0e537 1001 AND p.userid = '$userid'
ebc3bd2b 1002 AND p.userid = u.id
1f48942e 1003 ORDER BY p.modified ASC");
1004}
1005
1006function forum_get_post_from_log($log) {
1007/// Given a log entry, return the forum post details for it.
1008 global $CFG;
1009
1010 if ($log->action == "add post") {
1011
2f9c977e 1012 return get_record_sql("SELECT p.*, d.forum, d.groupid, u.firstname, u.lastname, u.email, u.picture
65b0e537 1013 FROM {$CFG->prefix}forum_discussions d,
1014 {$CFG->prefix}forum_posts p,
1015 {$CFG->prefix}user u
1016 WHERE p.id = '$log->info'
1017 AND d.id = p.discussion
1018 AND p.userid = u.id
1f48942e 1019 AND u.deleted <> '1'");
1020
1021
1022 } else if ($log->action == "add discussion") {
1023
2f9c977e 1024 return get_record_sql("SELECT p.*, d.forum, d.groupid, u.firstname, u.lastname, u.email, u.picture
65b0e537 1025 FROM {$CFG->prefix}forum_discussions d,
1026 {$CFG->prefix}forum_posts p,
1027 {$CFG->prefix}user u
1028 WHERE d.id = '$log->info'
1029 AND d.firstpost = p.id
1030 AND p.userid = u.id
1f48942e 1031 AND u.deleted <> '1'");
1032 }
1033 return NULL;
1034}
1035
d05956ac 1036function forum_get_firstpost_from_discussion($discussionid) {
1037/// Given a discussion id, return the first post from the discussion
1038 global $CFG;
1039
1040 return get_record_sql("SELECT p.*
65b0e537 1041 FROM {$CFG->prefix}forum_discussions d,
d05956ac 1042 {$CFG->prefix}forum_posts p
65b0e537 1043 WHERE d.id = '$discussionid'
d05956ac 1044 AND d.firstpost = p.id ");
1045}
1046
1f48942e 1047
1048function forum_get_user_grades($forumid) {
1049/// Get all user grades for a forum
1050 global $CFG;
1051
ebc3bd2b 1052 return get_records_sql("SELECT r.id, p.userid, r.rating
65b0e537 1053 FROM {$CFG->prefix}forum_discussions d,
1054 {$CFG->prefix}forum_posts p,
1f48942e 1055 {$CFG->prefix}forum_ratings r
65b0e537 1056 WHERE d.forum = '$forumid'
1f48942e 1057 AND p.discussion = d.id
02ebf404 1058 AND r.post = p.id
1059 ORDER by p.userid ");
1f48942e 1060}
1061
1062
1063function forum_count_discussion_replies($forum="0") {
1064// Returns an array of counts of replies to each discussion (optionally in one forum)
1065 global $CFG;
1066
1067 if ($forum) {
1068 $forumselect = " AND d.forum = '$forum'";
1069 }
1070 return get_records_sql("SELECT p.discussion, (count(*)) as replies
65b0e537 1071 FROM {$CFG->prefix}forum_posts p,
1f48942e 1072 {$CFG->prefix}forum_discussions d
65b0e537 1073 WHERE p.parent > 0
1074 AND p.discussion = d.id
1f48942e 1075 GROUP BY p.discussion");
1076}
1077
1078function forum_count_unrated_posts($discussionid, $userid) {
1079// How many unrated posts are in the given discussion for a given user?
1080 global $CFG;
1081 if ($posts = get_record_sql("SELECT count(*) as num
1082 FROM {$CFG->prefix}forum_posts
65b0e537 1083 WHERE parent > 0
1084 AND discussion = '$discussionid'
ebc3bd2b 1085 AND userid <> '$userid' ")) {
1f48942e 1086
65b0e537 1087 if ($rated = get_record_sql("SELECT count(*) as num
1088 FROM {$CFG->prefix}forum_posts p,
1f48942e 1089 {$CFG->prefix}forum_ratings r
1090 WHERE p.discussion = '$discussionid'
65b0e537 1091 AND p.id = r.post
ebc3bd2b 1092 AND r.userid = '$userid'")) {
1f48942e 1093 $difference = $posts->num - $rated->num;
1094 if ($difference > 0) {
1095 return $difference;
1096 } else {
1097 return 0; // Just in case there was a counting error
1098 }
1099 } else {
1100 return $posts->num;
1101 }
1102 } else {
1103 return 0;
1104 }
1105}
1106
65b0e537 1107function forum_get_discussions($forum="0", $forumsort="d.timemodified DESC",
2862b309 1108 $user=0, $fullpost=true, $visiblegroups=-1) {
1f48942e 1109/// Get all discussions in a forum
1110 global $CFG;
1111
1112 if ($user) {
1113 $userselect = " AND u.id = '$user' ";
1114 } else {
1115 $userselect = "";
1116 }
2862b309 1117
1118
1119 if ($visiblegroups == -1) {
02509fe6 1120 $groupselect = "";
2862b309 1121 } else {
1122 $groupselect = " AND (d.groupid = '$visiblegroups' OR d.groupid = '-1') ";
02509fe6 1123 }
2862b309 1124
29507631 1125 if (empty($forumsort)) {
1126 $forumsort = "d.timemodified DESC";
1127 }
2ab968e9 1128 if (empty($fullpost)) {
b879effb 1129 $postdata = "p.id,p.subject,p.modified,p.discussion,p.userid";
2ab968e9 1130 } else {
1131 $postdata = "p.*";
1132 }
9197e147 1133
cf84431b 1134 return get_records_sql("SELECT $postdata, d.name, d.timemodified, d.usermodified,
016cd6af 1135 u.firstname, u.lastname, u.email, u.picture
65b0e537 1136 FROM {$CFG->prefix}forum_discussions d,
9197e147 1137 {$CFG->prefix}forum_posts p,
65b0e537 1138 {$CFG->prefix}user u
1139 WHERE d.forum = '$forum'
1140 AND p.discussion = d.id
1141 AND p.parent = 0
9197e147 1142 AND p.userid = u.id $groupselect $userselect
29507631 1143 ORDER BY $forumsort");
1f48942e 1144}
1145
1146
1147
b656e2a9 1148function forum_get_user_discussions($courseid, $userid, $groupid=0) {
1149/// Get all discussions started by a particular user in a course (or group)
1f48942e 1150 global $CFG;
1151
b656e2a9 1152 if ($groupid) {
1153 $groupselect = " AND d.groupid = '$groupid' ";
1154 } else {
1155 $groupselect = "";
1156 }
1157
2862b309 1158 return get_records_sql("SELECT p.*, d.groupid, u.firstname, u.lastname, u.email, u.picture,
ebc3bd2b 1159 f.type as forumtype, f.name as forumname, f.id as forumid
65b0e537 1160 FROM {$CFG->prefix}forum_discussions d,
1161 {$CFG->prefix}forum_posts p,
1162 {$CFG->prefix}user u,
1f48942e 1163 {$CFG->prefix}forum f
65b0e537 1164 WHERE d.course = '$courseid'
1165 AND p.discussion = d.id
1166 AND p.parent = 0
1167 AND p.userid = u.id
1168 AND u.id = '$userid'
b656e2a9 1169 AND d.forum = f.id $groupselect
b8bf90c5 1170 ORDER BY p.created DESC");
1f48942e 1171}
1172
1173
6673d7bd 1174function forum_subscribed_users($course, $forum, $groupid=0) {
1f48942e 1175/// Returns list of user objects that are subscribed to this forum
1176 global $CFG;
1177
6673d7bd 1178 if ($groupid) {
1179 $grouptables = ", {$CFG->prefix}groups_members g";
1180 $groupselect = " AND g.groupid = '$groupid' AND u.id = g.userid";
1181 } else {
1182 $grouptables = "";
1183 $groupselect = "";
1184 }
1185
adaf3928 1186 if ($forum->forcesubscribe) {
1187 if ($course->category) {
170a9c46 1188 if ($forum->type == "teacher") {
1189 return get_course_teachers($course->id); // Only teachers can be subscribed to teacher forums
1190 } else {
1191 return get_course_users($course->id); // Otherwise get everyone in the course
1192 }
adaf3928 1193 } else {
2d0b30a0 1194 return get_site_users();
1f48942e 1195 }
1196 }
aaf7a9dc 1197 return get_records_sql("SELECT u.id, u.username, u.firstname, u.lastname, u.maildisplay, u.mailformat, u.maildigest, u.emailstop,
0351b1f9 1198 u.email, u.city, u.country, u.lastaccess, u.lastlogin, u.picture, u.timezone, u.lang
65b0e537 1199 FROM {$CFG->prefix}user u,
6673d7bd 1200 {$CFG->prefix}forum_subscriptions s $grouptables
1f48942e 1201 WHERE s.forum = '$forum->id'
65b0e537 1202 AND s.userid = u.id
6673d7bd 1203 AND u.deleted <> 1 $groupselect
7c81b6e2 1204 ORDER BY u.email ASC");
1f48942e 1205}
9fa49e22 1206
caadf009 1207/// OTHER FUNCTIONS ///////////////////////////////////////////////////////////
f93f848a 1208
1209
11b0c469 1210function forum_get_course_forum($courseid, $type) {
1211// How to set up special 1-per-course forums
a6fcdf98 1212 global $CFG;
1213
29cbd93a 1214 if ($forums = get_records_select("forum", "course = '$courseid' AND type = '$type'", "id ASC")) {
65b0e537 1215 // There should always only be ONE, but with the right combination of
29cbd93a 1216 // errors there might be more. In this case, just return the oldest one (lowest ID).
1217 foreach ($forums as $forum) {
1218 return $forum; // ie the first one
11b0c469 1219 }
8daaf761 1220 }
e6874d9f 1221
8daaf761 1222 // Doesn't exist, so create one now.
1223 $forum->course = $courseid;
1224 $forum->type = "$type";
1225 switch ($forum->type) {
1226 case "news":
65a3ef30 1227 $forum->name = addslashes(get_string("namenews", "forum"));
1228 $forum->intro = addslashes(get_string("intronews", "forum"));
8daaf761 1229 $forum->forcesubscribe = 1;
8daaf761 1230 $forum->open = 1; // 0 - no, 1 - posts only, 2 - discuss and post
1231 $forum->assessed = 0;
1232 if ($site = get_site()) {
1233 if ($courseid == $site->id) {
1234 $forum->name = get_string("sitenews");
1235 $forum->forcesubscribe = 0;
1236 }
a6fcdf98 1237 }
8daaf761 1238 break;
1239 case "social":
65a3ef30 1240 $forum->name = addslashes(get_string("namesocial", "forum"));
1241 $forum->intro = addslashes(get_string("introsocial", "forum"));
8daaf761 1242 $forum->open = 2; // 0 - no, 1 - posts only, 2 - discuss and post
1243 $forum->assessed = 0;
1244 $forum->forcesubscribe = 0;
1245 break;
1246 case "teacher":
65a3ef30 1247 $forum->name = addslashes(get_string("nameteacher", "forum"));
1248 $forum->intro = addslashes(get_string("introteacher", "forum"));
a796d0b8 1249 $forum->open = 2; // 0 - no, 1 - posts only, 2 - discuss and post
8daaf761 1250 $forum->assessed = 0;
1251 $forum->forcesubscribe = 0;
1252 break;
1253 default:
1254 notify("That forum type doesn't exist!");
1255 return false;
1256 break;
1257 }
1258
1259 $forum->timemodified = time();
1260 $forum->id = insert_record("forum", $forum);
1261
1262 if ($forum->type != "teacher") {
1263 if (! $module = get_record("modules", "name", "forum")) {
1264 notify("Could not find forum module!!");
1265 return false;
1266 }
1267 $mod->course = $courseid;
1268 $mod->module = $module->id;
1269 $mod->instance = $forum->id;
1270 $mod->section = 0;
1271 if (! $mod->coursemodule = add_course_module($mod) ) { // assumes course/lib.php is loaded
1272 notify("Could not add a new course module to the course '$course->fullname'");
1273 return false;
1274 }
1275 if (! $sectionid = add_mod_to_section($mod) ) { // assumes course/lib.php is loaded
1276 notify("Could not add the new course module to that section");
1277 return false;
1278 }
1279 if (! set_field("course_modules", "section", $sectionid, "id", $mod->coursemodule)) {
1280 notify("Could not update the course module with the correct section");
1281 return false;
1282 }
1283 include_once("$CFG->dirroot/course/lib.php");
d769d2ee 1284 rebuild_course_cache($courseid);
82aa0e8d 1285 }
65b0e537 1286
8daaf761 1287 return get_record("forum", "id", "$forum->id");
82aa0e8d 1288}
1289
f93f848a 1290
65b0e537 1291function forum_make_mail_post(&$post, $user, $touser, $course,
11b0c469 1292 $ownpost=false, $reply=false, $link=false, $rate=false, $footer="") {
65b0e537 1293// Given the data about a posting, builds up the HTML to display it and
501cdbd8 1294// returns the HTML in a string. This is designed for sending via HTML email.
1295
1296 global $THEME, $CFG;
1297
0d851f90 1298 static $formattedtext; // Cached version of formatted text for a post
1299 static $formattedtextid; // The ID number of the post
1300
1301 if (empty($formattedtextid) or $formattedtextid != $post->id) { // Recalculate the formatting
1302 $formattedtext = format_text($post->message, $post->format, NULL, $course->id);
1303 $formattedtextid = $post->id;
1304 }
1305
501cdbd8 1306 $output = "";
1307
9c674431 1308 $output .= '<table border="0" cellpadding="3" cellspacing="0" class="forumpost">';
501cdbd8 1309
9c674431 1310 $output .= "<tr><td bgcolor=\"$THEME->cellcontent2\" width=\"35\" valign=\"top\" class=\"forumpostpicture\">";
501cdbd8 1311 $output .= print_user_picture($user->id, $course->id, $user->picture, false, true);
72d497d4 1312 $output .= "</td>";
501cdbd8 1313
1314 if ($post->parent) {
9c674431 1315 $output .= "<td nowrap bgcolor=\"$THEME->cellheading\" class=\"forumpostheader\">";
501cdbd8 1316 } else {
9c674431 1317 $output .= "<td nowrap bgcolor=\"$THEME->cellheading2\" class=\"forumpostheadertopic\">";
501cdbd8 1318 }
72d497d4 1319 $output .= "<p>";
1320 $output .= "<font size=3><b>$post->subject</b></font><br />";
1321 $output .= "<font size=2>";
1b26d5e7 1322
1323 $fullname = fullname($user, isteacher($course->id));
1324 $by->name = "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id\">$fullname</a>";
d62413e8 1325 $by->date = userdate($post->modified, "", $touser->timezone);
ffe11640 1326 $output .= get_string("bynameondate", "forum", $by);
1b26d5e7 1327
72d497d4 1328 $output .= "</font></p></td></tr>";
9c674431 1329 $output .= "<tr><td bgcolor=\"$THEME->cellcontent2\" width=\"10\" class=\"forumpostside\">";
501cdbd8 1330 $output .= "&nbsp;";
9c674431 1331 $output .= "</td><td bgcolor=\"$THEME->cellcontent\" class=\"forumpostmessage\">\n";
501cdbd8 1332
7f6689e4 1333 if ($post->attachment) {
1334 $post->course = $course->id;
1335 $post->forum = get_field("forum_discussions", "forum", "id", $post->discussion);
72d497d4 1336 $output .= "<div align=right>";
7f6689e4 1337 $output .= forum_print_attachments($post, "html");
72d497d4 1338 $output .= "</div>";
7f6689e4 1339 }
1340
0d851f90 1341 $output .= $formattedtext;
501cdbd8 1342
72d497d4 1343 $output .= "<p align=right><font size=-1>";
501cdbd8 1344
2e2e71a8 1345 if ($post->parent) {
ce45515e 1346 $output .= "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion&parent=$post->parent\">".get_string("parent", "forum")."</a> | ";
2e2e71a8 1347 }
ce45515e 1348
501cdbd8 1349 $age = time() - $post->created;
1350 if ($ownpost) {
14eef464 1351 $output .= "<a href=\"$CFG->wwwroot/mod/forum/post.php?delete=$post->id\">".get_string("delete", "forum")."</a>";
501cdbd8 1352 if ($reply) {
cf84431b 1353 $output .= "<a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/post.php?reply=$post->id\">".get_string("replyforum", "forum")."</a>";
501cdbd8 1354 }
1355 $output .= "&nbsp;&nbsp;";
1356 } else {
1357 if ($reply) {
02509fe6 1358 $output .= "<a target=\"_blank\" href=\"$CFG->wwwroot/mod/forum/post.php?reply=$post->id\">".get_string("replyforum", "forum")."</a>&nbsp;&nbsp;";
501cdbd8 1359 }
1360 }
1361
72d497d4 1362 $output .= "</p>";
1363 $output .= "<div align=right><p align=right>";
65b0e537 1364
501cdbd8 1365 if ($link) {
1366 if ($post->replies == 1) {
ffe11640 1367 $replystring = get_string("repliesone", "forum", $post->replies);
501cdbd8 1368 } else {
ffe11640 1369 $replystring = get_string("repliesmany", "forum", $post->replies);
501cdbd8 1370 }
72d497d4 1371 $output .= "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\"><B>".get_string("discussthistopic", "forum")."</b></a> ($replystring)&nbsp;&nbsp;";
501cdbd8 1372 }
72d497d4 1373 $output .= "</p></div>";
501cdbd8 1374 if ($footer) {
72d497d4 1375 $output .= "<p>$footer</p>";
501cdbd8 1376 }
72d497d4 1377 $output .= "</td></tr></table>\n\n";
501cdbd8 1378
1379 return $output;
1380}
1381
1382
65b0e537 1383function forum_print_post(&$post, $courseid, $ownpost=false, $reply=false, $link=false,
98914efd 1384 $ratings=NULL, $footer="", $highlight="") {
74f5d1e3 1385
c585fa17 1386 global $THEME, $USER, $CFG;
501cdbd8 1387
b8be40ce 1388 static $stredit, $strdelete, $strreply, $strparent, $threadedmode, $isteacher, $adminedit;
2e2e71a8 1389
1390 if (empty($stredit)) {
1391 $stredit = get_string("edit", "forum");
1392 $strdelete = get_string("delete", "forum");
1393 $strreply = get_string("reply", "forum");
1394 $strparent = get_string("parent", "forum");
1395 $threadedmode = (!empty($USER->mode) and ($USER->mode == FORUM_MODE_THREADED));
3bd98ad4 1396 $isteacher = isteacher($courseid);
b8be40ce 1397 $adminedit = (isadmin() and !empty($CFG->admineditalways));
2e2e71a8 1398 }
1399
b22b0e61 1400 echo "<a name=\"$post->id\"></a>";
501cdbd8 1401 if ($post->parent) {
72d497d4 1402 echo '<table border="0" cellpadding="3" cellspacing="0" class="forumpost">';
501cdbd8 1403 } else {
72d497d4 1404 echo '<table border="0" cellpadding="3" cellspacing="0" class="forumpost" width="100%">';
501cdbd8 1405 }
1406
72d497d4 1407 echo "<tr><td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostpicture\" width=\"35\" valign=\"top\">";
501cdbd8 1408 print_user_picture($post->userid, $courseid, $post->picture);
72d497d4 1409 echo "</td>";
501cdbd8 1410
1411 if ($post->parent) {
834f1c7f 1412 echo "<td bgcolor=\"$THEME->cellheading\" class=\"forumpostheader\" width=\"100%\">";
501cdbd8 1413 } else {
834f1c7f 1414 echo "<td bgcolor=\"$THEME->cellheading2\" class=\"forumpostheadertopic\" width=\"100%\">";
501cdbd8 1415 }
83ec9098 1416
1417 if (!empty($CFG->filterall)) { /// Put the subject through the filters
34e934a9 1418 $post->subject = filter_text("<nolink>$post->subject</nolink>", $courseid);
83ec9098 1419 }
72d497d4 1420 echo "<p>";
d7143408 1421 echo "<font size=3><b>$post->subject</b></font><br />";
834f1c7f 1422 echo "<font size=2>";
1b26d5e7 1423
3bd98ad4 1424 $fullname = fullname($post, $isteacher);
1b26d5e7 1425 $by->name = "<a href=\"$CFG->wwwroot/user/view.php?id=$post->userid&course=$courseid\">$fullname</a>";
d62413e8 1426 $by->date = userdate($post->modified);
ffe11640 1427 print_string("bynameondate", "forum", $by);
1b26d5e7 1428
72d497d4 1429 echo "</font></p></td></tr>";
507407a7 1430 echo "<tr><td bgcolor=\"$THEME->cellcontent2\" valign=\"top\" class=\"forumpostside\" width=\"10\">";
1431 if ($group = user_group($courseid, $post->userid)) {
a4fbb0b2 1432 print_group_picture($group, $courseid, false, false, true);
507407a7 1433 } else {
1434 echo "&nbsp;";
1435 }
72d497d4 1436 echo "</td><td bgcolor=\"$THEME->cellcontent\" class=\"forumpostmessage\">\n";
501cdbd8 1437
7f6689e4 1438 if ($post->attachment) {
1439 $post->course = $courseid;
1440 $post->forum = get_field("forum_discussions", "forum", "id", $post->discussion);
72d497d4 1441 echo "<div align=\"right\">";
1442 $attachedimages = forum_print_attachments($post);
1443 echo "</div>";
e9c2dc1f 1444 } else {
1445 $attachedimages = "";
7f6689e4 1446 }
1447
5be7800c 1448 if ($link and (strlen(strip_tags($post->message)) > $CFG->forum_longpost)) {
aa153f29 1449 // Print shortened version
71767204 1450 echo format_text(forum_shorten_post($post->message), $post->format, NULL, $courseid);
c585fa17 1451 $numwords = count_words(strip_tags($post->message));
72d497d4 1452 echo "<p><a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\">";
ffe11640 1453 echo get_string("readtherest", "forum");
72d497d4 1454 echo "</a> (".get_string("numwords", "", $numwords).")...</p>";
501cdbd8 1455 } else {
aa153f29 1456 // Print whole message
88438a58 1457 if ($highlight) {
71767204 1458 echo highlight($highlight, format_text($post->message, $post->format, NULL, $courseid));
88438a58 1459 } else {
71767204 1460 echo format_text($post->message, $post->format, NULL, $courseid);
88438a58 1461 }
65b0e537 1462 echo $attachedimages;
501cdbd8 1463 }
1464
72d497d4 1465 echo "<p align=right><font size=-1>";
501cdbd8 1466
2e2e71a8 1467 if ($post->parent) {
1468 if ($threadedmode) {
1469 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion&parent=$post->parent\">$strparent</a> | ";
1470 } else {
1471 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion#$post->parent\">$strparent</a> | ";
1472 }
1473 }
1474
501cdbd8 1475 $age = time() - $post->created;
b8be40ce 1476 if ($ownpost or $adminedit) {
1477 if (($age < $CFG->maxeditingtime) or $adminedit) {
2e2e71a8 1478 echo "<a href=\"$CFG->wwwroot/mod/forum/post.php?edit=$post->id\">$stredit</a> | ";
501cdbd8 1479 }
64eacd6f 1480 }
aaf7a9dc 1481
cf84431b 1482 if (isteacheredit($courseid) and $post->parent) {
1483 echo "<a href=\"$CFG->wwwroot/mod/forum/post.php?prune=$post->id\" title=\"".get_string('pruneheading', 'forum').'">'.
1484 get_string("prune", "forum")."</a> | ";
1485 }
aaf7a9dc 1486
3bd98ad4 1487 if ($ownpost or $isteacher) {
14eef464 1488 echo "<a href=\"$CFG->wwwroot/mod/forum/post.php?delete=$post->id\">$strdelete</a>";
1489 if ($reply) {
1490 echo " | ";
1491 } else {
1492 echo "&nbsp;&nbsp;";
501cdbd8 1493 }
64eacd6f 1494 }
1495 if ($reply) {
2e2e71a8 1496 echo "<a href=\"$CFG->wwwroot/mod/forum/post.php?reply=$post->id\">$strreply</a>";
501cdbd8 1497 echo "&nbsp;&nbsp;";
501cdbd8 1498 }
72d497d4 1499 echo "</p>";
501cdbd8 1500
72d497d4 1501 echo "<div align=right><p align=right>";
74f5d1e3 1502
1503 $ratingsmenuused = false;
02ebf404 1504 if (!empty($ratings) and !empty($USER->id)) {
98914efd 1505 $useratings = true;
1506 if ($ratings->assesstimestart and $ratings->assesstimefinish) {
1507 if ($post->created < $ratings->assesstimestart or $post->created > $ratings->assesstimefinish) {
1508 $useratings = false;
1509 }
1510 }
1511 if ($useratings) {
3bd98ad4 1512 $mypost = ($USER->id == $post->userid);
1513
1514 if (($isteacher or $ratings->assesspublic) and !$mypost) {
1515 forum_print_ratings_mean($post->id, $ratings->scale, $isteacher);
d395046a 1516 if (!empty($ratings->allow)) {
1517 forum_print_rating_menu($post->id, $USER->id, $ratings->scale);
1518 $ratingsmenuused = true;
1519 }
3bd98ad4 1520
1521 } else if ($mypost) {
1522 forum_print_ratings_mean($post->id, $ratings->scale, true);
1523
98914efd 1524 } else if (!empty($ratings->allow) ) {
1525 forum_print_rating_menu($post->id, $USER->id, $ratings->scale);
74f5d1e3 1526 $ratingsmenuused = true;
2a3cda19 1527 }
501cdbd8 1528 }
1529 }
65b0e537 1530
501cdbd8 1531 if ($link) {
1532 if ($post->replies == 1) {
ffe11640 1533 $replystring = get_string("repliesone", "forum", $post->replies);
501cdbd8 1534 } else {
ffe11640 1535 $replystring = get_string("repliesmany", "forum", $post->replies);
501cdbd8 1536 }
74f5d1e3 1537 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\"><b>".
1538 get_string("discussthistopic", "forum")."</b></a> ($replystring)&nbsp;&nbsp;";
501cdbd8 1539 }
72d497d4 1540 echo "</p>";
501cdbd8 1541 if ($footer) {
72d497d4 1542 echo "<p>$footer</p>";
501cdbd8 1543 }
72d497d4 1544 echo "</div>";
1545 echo "</td></tr>\n</table>\n\n";
74f5d1e3 1546
1547 return $ratingsmenuused;
501cdbd8 1548}
1549
3335f6fb 1550
a796d0b8 1551function forum_print_discussion_header(&$post, $forum, $datestring="") {
c585fa17 1552 global $THEME, $USER, $CFG;
3335f6fb 1553
436a7cae 1554 if (!empty($CFG->filterall)) {
a796d0b8 1555 $post->subject = filter_text("<nolink>$post->subject</nolink>", $forum->course);
436a7cae 1556 }
1557
29507631 1558 echo "<tr class=\"forumpostheader\">";
3335f6fb 1559
29507631 1560 // Topic
546be6dd 1561 echo "<td bgcolor=\"$THEME->cellheading2\" class=\"forumpostheadertopic\" width=\"100%\">";
29507631 1562 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\">$post->subject</a>";
1563 echo "</td>\n";
1564
1565 // Picture
96e301a7 1566 echo "<td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostheaderpicture\" width=35>";
a796d0b8 1567 print_user_picture($post->userid, $forum->course, $post->picture);
29507631 1568 echo "</td>\n";
3335f6fb 1569
29507631 1570 // User name
a796d0b8 1571 $fullname = fullname($post, isteacher($forum->course));
96e301a7 1572 echo "<td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostheadername\" align=left nowrap>";
a796d0b8 1573 echo "<a href=\"$CFG->wwwroot/user/view.php?id=$post->userid&course=$forum->course\">$fullname</a>";
29507631 1574 echo "</td>\n";
1575
a796d0b8 1576 if ($forum->open or $forum->type == "teacher") { // Show the column with replies
1577 echo "<td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostheaderreplies\" align=center nowrap>";
1578 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion\">$post->replies</a>";
1579 echo "</td>\n";
1580 }
3335f6fb 1581
96e301a7 1582 echo "<td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostheaderdate\" align=right nowrap>";
29507631 1583 if (!empty($post->timemodified)) {
2ab968e9 1584 echo userdate($post->timemodified, $datestring);
3335f6fb 1585 } else {
2ab968e9 1586 echo userdate($post->modified, $datestring);
3335f6fb 1587 }
29507631 1588 echo "</td>\n";
1589
1590 echo "</tr>\n";
3335f6fb 1591
3335f6fb 1592}
1593
1594
aa153f29 1595function forum_shorten_post($message) {
c585fa17 1596// Given a post object that we already know has a long message
65b0e537 1597// this function truncates the message nicely to the first
5be7800c 1598// sane place between $CFG->forum_longpost and $CFG->forum_shortpost
1599
1600 global $CFG;
c585fa17 1601
1602 $i = 0;
1603 $tag = false;
1604 $length = strlen($message);
1605 $count = 0;
1606 $stopzone = false;
1607 $truncate = 0;
1608
1609 for ($i=0; $i<$length; $i++) {
a8afb411 1610 $char = $message[$i];
c585fa17 1611
1612 switch ($char) {
65b0e537 1613 case "<":
c585fa17 1614 $tag = true;
1615 break;
65b0e537 1616 case ">":
c585fa17 1617 $tag = false;
1618 break;
1619 default:
1620 if (!$tag) {
1621 if ($stopzone) {
67f0b4cc 1622 if ($char == ".") {
a8afb411 1623 $truncate = $i+1;
c585fa17 1624 break 2;
1625 }
1626 }
1627 $count++;
1628 }
a8afb411 1629 break;
c585fa17 1630 }
1631 if (!$stopzone) {
5be7800c 1632 if ($count > $CFG->forum_shortpost) {
c585fa17 1633 $stopzone = true;
1634 }
1635 }
1636 }
aa153f29 1637
c585fa17 1638 if (!$truncate) {
a8afb411 1639 $truncate = $i;
c585fa17 1640 }
1641
67f0b4cc 1642 return substr($message, 0, $truncate);
aa153f29 1643}
1644
501cdbd8 1645
3bd98ad4 1646function forum_print_ratings_mean($postid, $scale, $link=true) {
02ebf404 1647/// Print the multiple ratings on a post given to the current user by others.
1648/// Scale is an array of ratings
1649
1650 static $strrate;
05c47ef7 1651
1652 $mean = forum_get_ratings_mean($postid, $scale);
65b0e537 1653
05c47ef7 1654 if ($mean !== "") {
02ebf404 1655
1656 if (empty($strratings)) {
1657 $strratings = get_string("ratings", "forum");
501cdbd8 1658 }
501cdbd8 1659
02ebf404 1660 echo "$strratings: ";
3bd98ad4 1661 if ($link) {
1662 link_to_popup_window ("/mod/forum/report.php?id=$postid", "ratings", $mean, 400, 600);
1663 } else {
1664 echo "$mean ";
1665 }
501cdbd8 1666 }
1667}
1668
501cdbd8 1669
0761d83f 1670function forum_get_ratings_mean($postid, $scale, $ratings=NULL) {
1671/// Return the mean rating of a post given to the current user by others.
02ebf404 1672/// Scale is an array of possible ratings in the scale
1673/// Ratings is an optional simple array of actual ratings (just integers)
1674
1675 if (!$ratings) {
1676 $ratings = array();
1677 if ($rates = get_records("forum_ratings", "post", $postid)) {
1678 foreach ($rates as $rate) {
1679 $ratings[] = $rate->rating;
1680 }
501cdbd8 1681 }
501cdbd8 1682 }
02ebf404 1683
0761d83f 1684 $count = count($ratings);
1685
1686 if ($count == 0) {
02ebf404 1687 return "";
02ebf404 1688
0761d83f 1689 } else if ($count == 1) {
1690 return $scale[$ratings[0]];
1691
02ebf404 1692 } else {
0761d83f 1693 $total = 0;
1694 foreach ($ratings as $rating) {
1695 $total += $rating;
1696 }
1697 $mean = round( ((float)$total/(float)$count) + 0.001); // Little fudge factor so that 0.5 goes UP
65b0e537 1698
0761d83f 1699 if (isset($scale[$mean])) {
1700 return $scale[$mean]." ($count)";
1701 } else {
1702 return "$mean ($count)"; // Should never happen, hopefully
1703 }
02ebf404 1704 }
1705}
1706
1707function forum_get_ratings_summary($postid, $scale, $ratings=NULL) {
1708/// Return a summary of post ratings given to the current user by others.
1709/// Scale is an array of possible ratings in the scale
1710/// Ratings is an optional simple array of actual ratings (just integers)
1711
1712 if (!$ratings) {
1713 $ratings = array();
1714 if ($rates = get_records("forum_ratings", "post", $postid)) {
1715 foreach ($rates as $rate) {
1716 $rating[] = $rate->rating;
1717 }
1718 }
1719 }
1720
1721
1722 if (!$count = count($ratings)) {
1723 return "";
1724 }
1725
1726
1727 foreach ($scale as $key => $scaleitem) {
1728 $sumrating[$key] = 0;
1729 }
1730
1731 foreach ($ratings as $rating) {
1732 $sumrating[$rating]++;
1733 }
1734
1735 $summary = "";
1736 foreach ($scale as $key => $scaleitem) {
1737 $summary = $sumrating[$key].$summary;
1738 if ($key > 1) {
1739 $summary = "/$summary";
1740 }
1741 }
1742 return $summary;
1743}
1744
1745function forum_print_rating_menu($postid, $userid, $scale) {
65b0e537 1746/// Print the menu of ratings as part of a larger form.
02ebf404 1747/// If the post has already been - set that value.
1748/// Scale is an array of ratings
1749
1750 static $strrate;
1751
1752 if (!$rating = get_record("forum_ratings", "userid", $userid, "post", $postid)) {
1753 $rating->rating = 0;
1754 }
1755
1756 if (empty($strrate)) {
1757 $strrate = get_string("rate", "forum");
1758 }
1759
1760 choose_from_menu($scale, $postid, $rating->rating, "$strrate...");
501cdbd8 1761}
1762
7a12aab4 1763function forum_print_mode_form($discussion, $mode) {
1764 GLOBAL $FORUM_LAYOUT_MODES;
501cdbd8 1765
8b9c7aa0 1766 echo "<center><p>";
7a12aab4 1767 popup_form("discuss.php?d=$discussion&mode=", $FORUM_LAYOUT_MODES, "mode", $mode, "");
8b9c7aa0 1768 echo "</p></center>\n";
501cdbd8 1769}
1770
97485d07 1771function forum_print_search_form($course, $search="", $return=false, $type="") {
501cdbd8 1772 global $CFG;
1773
97485d07 1774 if ($type == "plain") {
8b9c7aa0 1775 $output = "<table border=0 cellpadding=0 cellspacing=0><tr><td nowrap>";
1776 $output .= "<form name=search action=\"$CFG->wwwroot/mod/forum/search.php\">";
1777 $output .= "<font size=\"-1\">";
2862b309 1778 $output .= "<input name=search type=text size=15 value=\"$search\">";
8b9c7aa0 1779 $output .= "<input value=\"".get_string("searchforums", "forum")."\" type=submit>";
1780 $output .= "</font>";
1781 $output .= "<input name=id type=hidden value=\"$course->id\">";
1782 $output .= "</form>";
1783 $output .= "</td></tr></table>";
97485d07 1784 } else {
8b9c7aa0 1785 $output = "<table border=0 cellpadding=10 cellspacing=0><tr><td align=center>";
1786 $output .= "<form name=search action=\"$CFG->wwwroot/mod/forum/search.php\">";
1787 $output .= "<font size=\"-1\">";
2862b309 1788 $output .= "<input name=search type=text size=15 value=\"$search\"><br>";
8b9c7aa0 1789 $output .= "<input value=\"".get_string("searchforums", "forum")."\" type=submit>";
1790 $output .= "</font>";
1791 $output .= "<input name=id type=hidden value=\"$course->id\">";
1792 $output .= "</form>";
1793 $output .= "</td></tr></table>";
97485d07 1794 }
5e367a2d 1795
1796 if ($return) {
1797 return $output;
1798 }
1799 echo $output;
501cdbd8 1800}
1801
1802
11b0c469 1803function forum_set_return() {
607809b3 1804 global $CFG, $SESSION;
501cdbd8 1805
28e1e8b9 1806 if (! isset($SESSION->fromdiscussion)) {
48d38fad 1807 if (!empty($_SERVER['HTTP_REFERER'])) {
1808 $referer = $_SERVER['HTTP_REFERER'];
1809 } else {
1810 $referer = "";
1811 }
28e1e8b9 1812 // If the referer is NOT a login screen then save it.
48d38fad 1813 if (! strncasecmp("$CFG->wwwroot/login", $referer, 300)) {
607809b3 1814 $SESSION->fromdiscussion = $_SERVER["HTTP_REFERER"];
28e1e8b9 1815 }
501cdbd8 1816 }
1817}
1818
1819
11b0c469 1820function forum_go_back_to($default) {
501cdbd8 1821 global $SESSION;
1822
9c9f7d77 1823 if (!empty($SESSION->fromdiscussion)) {
501cdbd8 1824 $returnto = $SESSION->fromdiscussion;
1825 unset($SESSION->fromdiscussion);
1826 return $returnto;
1827 } else {
1828 return $default;
1829 }
1830}
1831
7f6689e4 1832function forum_file_area_name($post) {
1833// Creates a directory file name, suitable for make_upload_directory()
1834 global $CFG;
1835
1836 return "$post->course/$CFG->moddata/forum/$post->forum/$post->id";
1837}
1838
1839function forum_file_area($post) {
1840 return make_upload_directory( forum_file_area_name($post) );
1841}
1842
1843function forum_delete_old_attachments($post, $exception="") {
1844// Deletes all the user files in the attachments area for a post
1845// EXCEPT for any file named $exception
1846
1847 if ($basedir = forum_file_area($post)) {
1848 if ($files = get_directory_list($basedir)) {
1849 foreach ($files as $file) {
1850 if ($file != $exception) {
1851 unlink("$basedir/$file");
1852 notify("Existing file '$file' has been deleted!");
1853 }
1854 }
1855 }
1856 if (!$exception) { // Delete directory as well, if empty
1857 rmdir("$basedir");
1858 }
1859 }
1860}
1861
cc2b7ea5 1862function forum_move_attachments($discussion, $forumid) {
65b0e537 1863/// Given a discussion object that is being moved to forumid,
1864/// this function checks all posts in that discussion
1865/// for attachments, and if any are found, these are
cc2b7ea5 1866/// moved to the new forum directory.
1867
1868 global $CFG;
1869
1870 $return = true;
1871
1872 if ($posts = get_records_select("forum_posts", "discussion = '$discussion->id' AND attachment <> ''")) {
1873 foreach ($posts as $oldpost) {
1874 $oldpost->course = $discussion->course;
1875 $oldpost->forum = $discussion->forum;
1876 $oldpostdir = "$CFG->dataroot/".forum_file_area_name($oldpost);
1877 if (is_dir($oldpostdir)) {
1878 $newpost = $oldpost;
1879 $newpost->forum = $forumid;
7f0c3a44 1880 $newpostdir = forum_file_area($newpost);
cc2b7ea5 1881 if (! @rename($oldpostdir, $newpostdir)) {
1882 $return = false;
1883 }
1884 }
1885 }
1886 }
1887 return $return;
1888}
1889
7f6689e4 1890function forum_print_attachments($post, $return=NULL) {
1891// if return=html, then return a html string.
1892// if return=text, then return a text-only string.
72d497d4 1893// otherwise, print HTML for non-images, and return image HTML
7f6689e4 1894
1895 global $CFG;
1896
1897 $filearea = forum_file_area_name($post);
1898
72d497d4 1899 $imagereturn = "";
1900 $output = "";
1901
7f6689e4 1902 if ($basedir = forum_file_area($post)) {
1903 if ($files = get_directory_list($basedir)) {
1904 $strattachment = get_string("attachment", "forum");
6afe8558 1905 $strpopupwindow = get_string("popupwindow");
7f6689e4 1906 foreach ($files as $file) {
1907 $icon = mimeinfo("icon", $file);
1908 if ($CFG->slasharguments) {
1909 $ffurl = "file.php/$filearea/$file";
1910 } else {
1911 $ffurl = "file.php?file=/$filearea/$file";
1912 }
096b5432 1913 $image = "<img border=\"0\" src=\"$CFG->pixpath/f/$icon\" height=\"16\" width=\"16\" alt=\"$strpopupwindow\">";
7f6689e4 1914
1915 if ($return == "html") {
6afe8558 1916 $output .= "<a href=\"$CFG->wwwroot/$ffurl\">$image</a> ";
1917 $output .= "<a href=\"$CFG->wwwroot/$ffurl\">$file</a><br />";
7f6689e4 1918
1919 } else if ($return == "text") {
1920 $output .= "$strattachment $file:\n$CFG->wwwroot/$ffurl\n";
1921
1922 } else {
72d497d4 1923 if ($icon == "image.gif") { // Image attachments don't get printed as links
1924 $imagereturn .= "<br /><img src=\"$CFG->wwwroot/$ffurl\">";
1925 } else {
1926 link_to_popup_window("/$ffurl", "attachment", $image, 500, 500, $strattachment);
1927 echo "<a href=\"$CFG->wwwroot/$ffurl\">$file</a>";
1928 echo "<br />";
1929 }
7f6689e4 1930 }
1931 }
1932 }
1933 }
72d497d4 1934
7f6689e4 1935 if ($return) {
1936 return $output;
1937 }
72d497d4 1938
1939 return $imagereturn;
7f6689e4 1940}
1941
1942function forum_add_attachment($post, $newfile) {
1943// $post is a full post record, including course and forum
3b7c1de9 1944// $newfile is a full upload array from $_FILES
7f6689e4 1945// If successful, this function returns the name of the file
1946
9dd0b378 1947 global $CFG;
1948
3b7c1de9 1949 if (empty($newfile['name'])) {
7f6689e4 1950 return "";
1951 }
1952
4909e176 1953 if (!$forum = get_record("forum", "id", $post->forum)) {
1954 return "";
1955 }
1956
1957 if (!$course = get_record("course", "id", $forum->course)) {
1958 return "";
1959 }
1960
1961 $maxbytes = get_max_upload_file_size($CFG->maxbytes, $course->maxbytes, $forum->maxbytes);
1962
7f6689e4 1963 $newfile_name = clean_filename($newfile['name']);
1964
1965 if (valid_uploaded_file($newfile)) {
4909e176 1966 if ($maxbytes and $newfile['size'] > $maxbytes) {
1967 return "";
1968 }
7f6689e4 1969 if (! $newfile_name) {
1970 notify("This file had a wierd filename and couldn't be uploaded");
1971
1972 } else if (! $dir = forum_file_area($post)) {
1973 notify("Attachment could not be stored");
1974 $newfile_name = "";
1975
1976 } else {
1977 if (move_uploaded_file($newfile['tmp_name'], "$dir/$newfile_name")) {
9dd0b378 1978 chmod("$dir/$newfile_name", $CFG->directorypermissions);
7f6689e4 1979 forum_delete_old_attachments($post, $newfile_name);
1980 } else {
1981 notify("An error happened while saving the file on the server");
1982 $newfile_name = "";
1983 }
1984 }
1985 } else {
1986 $newfile_name = "";
1987 }
1988
1989 return $newfile_name;
1990}
501cdbd8 1991
11b0c469 1992function forum_add_new_post($post) {
501cdbd8 1993
ffe11640 1994 $post->created = $post->modified = time();
501cdbd8 1995 $post->mailed = "0";
1996
7f6689e4 1997 $newfile = $post->attachment;
1998 $post->attachment = "";
1999
65b0e537 2000 if (! $post->id = insert_record("forum_posts", $post)) {
7f6689e4 2001 return false;
2002 }
2003
2004 if ($post->attachment = forum_add_attachment($post, $newfile)) {
2005 set_field("forum_posts", "attachment", $post->attachment, "id", $post->id);
2006 }
29507631 2007
2008 // Update discussion modified date
2009 set_field("forum_discussions", "timemodified", $post->modified, "id", $post->discussion);
016cd6af 2010 set_field("forum_discussions", "usermodified", $post->userid, "id", $post->discussion);
65b0e537 2011
7f6689e4 2012 return $post->id;
501cdbd8 2013}
2014
11b0c469 2015function forum_update_post($post) {
501cdbd8 2016
ffe11640 2017 $post->modified = time();
501cdbd8 2018
0ab85112 2019 if (!$post->parent) { // Post is a discussion starter - update discussion title too
2020 set_field("forum_discussions", "name", $post->subject, "id", $post->discussion);
2021 }
29507631 2022
7f6689e4 2023 if ($newfilename = forum_add_attachment($post, $post->attachment)) {
2024 $post->attachment = $newfilename;
2025 } else {
2026 unset($post->attachment);
2027 }
29507631 2028
2029 // Update discussion modified date
2030 set_field("forum_discussions", "timemodified", $post->modified, "id", $post->discussion);
016cd6af 2031 set_field("forum_discussions", "usermodified", $post->userid, "id", $post->discussion);
29507631 2032
ffe11640 2033 return update_record("forum_posts", $post);
501cdbd8 2034}
2035
2036function forum_add_discussion($discussion) {
65b0e537 2037// Given an object containing all the necessary data,
501cdbd8 2038// create a new discussion and return the id
2039
2040 GLOBAL $USER;
2041
2042 $timenow = time();
2043
65b0e537 2044 // The first post is stored as a real post, and linked
501cdbd8 2045 // to from the discuss entry.
2046
2047 $post->discussion = 0;
2048 $post->parent = 0;
ebc3bd2b 2049 $post->userid = $USER->id;
501cdbd8 2050 $post->created = $timenow;
2051 $post->modified = $timenow;
2052 $post->mailed = 0;
2053 $post->subject = $discussion->name;
2054 $post->message = $discussion->intro;
7f6689e4 2055 $post->attachment = "";
2056 $post->forum = $discussion->forum;
2057 $post->course = $discussion->course;
9f0b8269 2058 $post->format = $discussion->format;
501cdbd8 2059
2060 if (! $post->id = insert_record("forum_posts", $post) ) {
2061 return 0;
2062 }
2063
7f6689e4 2064 if ($post->attachment = forum_add_attachment($post, $discussion->attachment)) {
2065 set_field("forum_posts", "attachment", $post->attachment, "id", $post->id); //ignore errors
2066 }
2067
65b0e537 2068 // Now do the main entry for the discussion,
02509fe6 2069 // linking to this first post
04eba58f 2070
caadf009 2071 $discussion->firstpost = $post->id;
2072 $discussion->timemodified = $timenow;
016cd6af 2073 $discussion->usermodified = $post->userid;
04eba58f 2074
caadf009 2075 if (! $discussion->id = insert_record("forum_discussions", $discussion) ) {
2076 delete_records("forum_posts", "id", $post->id);
2077 return 0;
04eba58f 2078 }
2079
caadf009 2080 // Finally, set the pointer on the post.
2081 if (! set_field("forum_posts", "discussion", $discussion->id, "id", $post->id)) {
2082 delete_records("forum_posts", "id", $post->id);
2083 delete_records("forum_discussions", "id", $discussion->id);
2084 return 0;
04eba58f 2085 }
04eba58f 2086
caadf009 2087 return $discussion->id;
2088}
04eba58f 2089
04eba58f 2090
caadf009 2091function forum_delete_discussion($discussion) {
2092// $discussion is a discussion record object
04eba58f 2093
2094 $result = true;
2095
caadf009 2096 if ($posts = get_records("forum_posts", "discussion", $discussion->id)) {
2097 foreach ($posts as $post) {
2098 $post->course = $discussion->course;
2099 $post->forum = $discussion->forum;
2100 if (! delete_records("forum_ratings", "post", "$post->id")) {
2101 $result = false;
2102 }
2103 if (! forum_delete_post($post)) {
04eba58f 2104 $result = false;
2105 }
2106 }
2107 }
2108
caadf009 2109 if (! delete_records("forum_discussions", "id", "$discussion->id")) {
04eba58f 2110 $result = false;
2111 }
2112
2113 return $result;
2114}
2115
2116
caadf009 2117function forum_delete_post($post) {
2118 if (delete_records("forum_posts", "id", $post->id)) {
2119 delete_records("forum_ratings", "post", $post->id); // Just in case
2120 if ($post->attachment) {
2121 $discussion = get_record("forum_discussions", "id", $post->discussion);
2122 $post->course = $discussion->course;
2123 $post->forum = $discussion->forum;
2124 forum_delete_old_attachments($post);
2125 }
2126 return true;
2127 }
2128 return false;
2129}
501cdbd8 2130
501cdbd8 2131
b656e2a9 2132function forum_print_user_discussions($courseid, $userid, $groupid=0) {
caadf009 2133 global $CFG, $USER;
501cdbd8 2134
b8bf90c5 2135 $maxdiscussions = 10;
2136 $countdiscussions = 0;
2137
65b0e537 2138 $visible = array();
1f48942e 2139
2862b309 2140 $course = get_record("course", "id", $courseid);
2141
2142 $currentgroup = get_current_group($courseid);
2143 $isteacheredit = isteacheredit($courseid);
2144
b656e2a9 2145 if ($discussions = forum_get_user_discussions($courseid, $userid, $groupid=0)) {
2862b309 2146
2147 $user = get_record("user", "id", $userid);
1b26d5e7 2148 $fullname = fullname($user, isteacher($courseid));
2862b309 2149
caadf009 2150 $replies = forum_count_discussion_replies();
2862b309 2151
2152 echo "<hr />";
2153
2154 print_heading( get_string("discussionsstartedbyrecent", "forum", $fullname) );
2155
caadf009 2156 foreach ($discussions as $discussion) {
b8bf90c5 2157 $countdiscussions++;
2158 if ($countdiscussions > $maxdiscussions) {
2159 break;
2160 }
caadf009 2161 if (($discussion->forumtype == "teacher") and !isteacher($courseid)) {
ffe11640 2162 continue;
2163 }
65b0e537 2164 if(!isset($visible[$discussion->forumid])) {
2165 $mod = New stdClass;
2166 $mod->course = $courseid;
2167 $mod->id = $discussion->forumid;
2168 $visible[$discussion->forumid] = instance_is_visible('forum', $mod);
2169 }
2170 if(!$visible[$discussion->forumid] && !isteacheredit($courseid, $USER->id)) {
2171 continue;
2172 }
2862b309 2173
2174 /// Check whether this is belongs to a discussion in a group that
2175 /// should NOT be accessible to the current user
2176
2177 if (!$isteacheredit and $discussion->groupid != -1) { /// Editing teachers or open discussions
2178 if (!isset($cm[$discussion->forum])) {
2179 $cm[$discussion->forum] = get_coursemodule_from_instance("forum", $discussion->forum, $courseid);
2180 $groupmode[$discussion->forum] = groupmode($course, $cm[$discussion->forum]);
2181 }
2182 if ($groupmode[$discussion->forum] == SEPARATEGROUPS) {
2183 if ($currentgroup != $discussion->groupid) {
2184 continue;
2185 }
2186 }
2187 }
2188
47f1da80 2189 if (!empty($replies[$discussion->discussion])) {
caadf009 2190 $discussion->replies = $replies[$discussion->discussion]->replies;
2191 } else {
2192 $discussion->replies = 0;
501cdbd8 2193 }
caadf009 2194 $inforum = get_string("inforum", "forum", "<A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$discussion->forumid\">$discussion->forumname</A>");
2195 $discussion->subject .= " ($inforum)";
61e96406 2196 if (!empty($USER->id)) {
2197 $ownpost = ($discussion->userid == $USER->id);
2198 } else {
2199 $ownpost = false;
2200 }
caadf009 2201 forum_print_post($discussion, $courseid, $ownpost, $reply=0, $link=1, $assessed=false);
2202 echo "<BR>\n";
501cdbd8 2203 }
2204 }
501cdbd8 2205}
2206
501cdbd8 2207function forum_forcesubscribe($forumid, $value=1) {
2208 return set_field("forum", "forcesubscribe", $value, "id", $forumid);
2209}
2210
2211function forum_is_forcesubscribed($forumid) {
2212 return get_field("forum", "forcesubscribe", "id", $forumid);
2213}
2214
2215function forum_is_subscribed($userid, $forumid) {
2216 if (forum_is_forcesubscribed($forumid)) {
2217 return true;
2218 }
ebc3bd2b 2219 return record_exists("forum_subscriptions", "userid", $userid, "forum", $forumid);
86970225 2220}
2221
501cdbd8 2222function forum_subscribe($userid, $forumid) {
9fa49e22 2223/// Adds user to the subscriber list
2224
6673d7bd 2225 if (record_exists("forum_subscriptions", "userid", $userid, "forum", $forumid)) {
2226 return true;
2227 }
2228
ebc3bd2b 2229 $sub->userid = $userid;
9fa49e22 2230 $sub->forum = $forumid;
501cdbd8 2231
9fa49e22 2232 return insert_record("forum_subscriptions", $sub);
501cdbd8 2233}
2234
2235function forum_unsubscribe($userid, $forumid) {
9fa49e22 2236/// Removes user from the subscriber list
ebc3bd2b 2237 return delete_records("forum_subscriptions", "userid", $userid, "forum", $forumid);
501cdbd8 2238}
2239
0a9f61b5 2240function forum_post_subscription($post) {
2241/// Given a new post, subscribes or unsubscribes as appropriate.
2242/// Returns some text which describes what happened.
2243
2244 global $USER;
2245
2246 if (empty($post->subscribe) and empty($post->unsubscribe)) {
2247 return "";
2248 }
2249
2250 if (!$forum = get_record("forum", "id", $post->forum)) {
2251 return "";
2252 }
2253
2254 $info->name = "$USER->firstname $USER->lastname";
2255 $info->forum = $forum->name;
2256
2257 if (!empty($post->subscribe)) {
2258 forum_subscribe($USER->id, $post->forum);
2259 return "<p>".get_string("nowsubscribed", "forum", $info)."</p>";
2260 }
2261
2262 forum_unsubscribe($USER->id, $post->forum);
2263 return "<p>".get_string("nownotsubscribed", "forum", $info)."</p>";
2264}
2265
501cdbd8 2266
11b0c469 2267function forum_user_has_posted_discussion($forumid, $userid) {
29507631 2268 if ($discussions = forum_get_discussions($forumid, "", $userid)) {
501cdbd8 2269 return true;
2270 } else {
2271 return false;
2272 }
2273}
2274
9197e147 2275function forum_user_can_post_discussion($forum, $currentgroup=false) {
501cdbd8 2276// $forum is an object
2277 global $USER;
2278
2279 if ($forum->type == "eachuser") {
11b0c469 2280 return (! forum_user_has_posted_discussion($forum->id, $USER->id));
501cdbd8 2281 } else if ($forum->type == "teacher") {
2282 return isteacher($forum->course);
9197e147 2283 } else if ($currentgroup) {
d591b40f 2284 return (isteacheredit($forum->course) or (ismember($currentgroup) and $forum->open == 2));
501cdbd8 2285 } else if (isteacher($forum->course)) {
2286 return true;
2287 } else {
70c476a7 2288 return ($forum->open == 2);
501cdbd8 2289 }
2290}
2291
f690562f 2292function forum_user_can_post($forum, $user=NULL) {
2293// $forum, $user are objects
2294
2295 if ($user) {
2296 $isteacher = isteacher($forum->course, $user->id);
2297 } else {
2298 $isteacher = isteacher($forum->course);
2299 }
70c476a7 2300
2301 if ($forum->type == "teacher") {
f690562f 2302 return $isteacher;
2303 } else if ($isteacher) {
70c476a7 2304 return true;
2305 } else {
2306 return $forum->open;
2307 }
2308}
501cdbd8 2309
501cdbd8 2310
65b0e537 2311function forum_print_latest_discussions($forum_id=0, $forum_numdiscussions=5,
2312 $forum_style="plain", $forum_sort="",
2862b309 2313 $currentgroup=0, $groupmode=-1) {
c585fa17 2314 global $CFG, $USER;
65b0e537 2315
f93f848a 2316 if ($forum_id) {
2317 if (! $forum = get_record("forum", "id", $forum_id)) {
2318 error("Forum ID was incorrect");
2319 }
2320 if (! $course = get_record("course", "id", $forum->course)) {
2321 error("Could not find the course this forum belongs to!");
2322 }
2323
2324 if ($course->category) {
2325 require_login($course->id);
2326 }
2327
2328 } else {
2329 if (! $course = get_record("course", "category", 0)) {
2330 error("Could not find a top-level course!");
2331 }
7beb45d8 2332 if (! $forum = forum_get_course_forum($course->id, "news")) {
f93f848a 2333 error("Could not find or create a main forum in this course (id $course->id)");
2334 }
2335 }
2336
2862b309 2337 if ($groupmode == -1) { /// We need to reconstruct groupmode because none was given
2338 if ($cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
2339 $groupmode = groupmode($course, $cm);
2340 } else {
2341 $groupmode = SEPARATEGROUPS;
2342 }
2343 }
2344
2345
9197e147 2346 if (forum_user_can_post_discussion($forum, $currentgroup)) {
b879effb 2347 echo "<p align=center>";
2348 echo "<a href=\"$CFG->wwwroot/mod/forum/post.php?forum=$forum->id\">";
0351b1f9 2349 if ($forum->type == "news") {
2350 echo get_string("addanewtopic", "forum")."</a>...";
2351 } else {
2352 echo get_string("addanewdiscussion", "forum")."</a>...";
2353 }
b879effb 2354 echo "</p>\n";
77305fe6 2355 }
2356
65b0e537 2357 if ((!$forum_numdiscussions) && ($forum_style == "plain")) {
3c8a606d 2358 $forum_style = "header"; // Abbreviate display by default
3335f6fb 2359 }
f93f848a 2360
65b0e537 2361 if ($forum_style == "minimal") {
2ab968e9 2362 $forum_sort = "p.modified DESC";
2363 }
2364
2365 $fullpost = false;
65b0e537 2366 if ($forum_style == "plain") {
2ab968e9 2367 $fullpost = true;
2368 }
2369
2862b309 2370
2371/// Decides if current user is allowed to see ALL the current discussions or not
2372
2373 if (!$currentgroup and ($groupmode != SEPARATEGROUPS or isteacheredit($forum->course)) ) {
2374 $visiblegroups = -1;
2375 } else {
2376 $visiblegroups = $currentgroup;
2377 }
2378
2379/// Get all the recent discussions we're allowed to see
2380
2381 if (! $discussions = forum_get_discussions($forum->id, $forum_sort, 0, $fullpost, $visiblegroups) ) {
0351b1f9 2382 if ($forum->type == "news") {
2383 echo "<p align=center><b>(".get_string("nonews", "forum").")</b></p>";
2384 } else {
2385 echo "<p align=center><b>(".get_string("nodiscussions", "forum").")</b></p>";
2386 }
2ab968e9 2387 return;
2388 }
65b0e537 2389
3335f6fb 2390 $replies = forum_count_discussion_replies($forum->id);
f93f848a 2391
3260de67 2392 $canreply = forum_user_can_post($forum);
2393
3335f6fb 2394 $discussioncount = 0;
c20b762a 2395 $olddiscussionlink = false;
2ab968e9 2396 $strdatestring = get_string("strftimedaydatetime");
f93f848a 2397
dcde9f02 2398 if ($forum_style == "minimal") {
2399 $strftimerecent = get_string("strftimerecent");
2400 $strmore = get_string("more", "forum");
2401 }
2402
29507631 2403 if ($forum_style == "header") {
ba1ce4c0 2404 echo "<table width=\"100%\" border=0 cellpadding=3 cellspacing=1 class=\"forumheaderlist\">";
03aa0dfa 2405 echo "<tr class=\"forumpostheader\">";
65b0e537 2406 echo "<th>".get_string("discussion", "forum")."</th>";
29507631 2407 echo "<th colspan=2>".get_string("startedby", "forum")."</th>";
a796d0b8 2408 if ($forum->open or $forum->type == "teacher") {
2409 echo "<th>".get_string("replies", "forum")."</th>";
2410 }
29507631 2411 echo "<th>".get_string("lastpost", "forum")."</th>";
2412 echo "</tr>";
2413 }
2414
3335f6fb 2415 foreach ($discussions as $discussion) {
2416 $discussioncount++;
f93f848a 2417
3335f6fb 2418 if ($forum_numdiscussions && ($discussioncount > $forum_numdiscussions)) {
c20b762a 2419 $olddiscussionlink = true;
3335f6fb 2420 break;
2421 }
9c9f7d77 2422 if (!empty($replies[$discussion->discussion])) {
3335f6fb 2423 $discussion->replies = $replies[$discussion->discussion]->replies;
2424 } else {
2425 $discussion->replies = 0;
2426 }
f7477444 2427 if (!empty($USER->id)) {
2428 $ownpost = ($discussion->userid == $USER->id);
2429 } else {
2430 $ownpost=false;
2431 }
cf84431b 2432 // Use discussion name instead of subject of first post
2433 $discussion->subject = $discussion->name;
2434
3335f6fb 2435 switch ($forum_style) {
2436 case "minimal":
436a7cae 2437 if (!empty($CFG->filterall)) {
2438 $discussion->subject = filter_text($discussion->subject, $forum->course);
2439 }
b656e2a9 2440 echo "<p><span class=\"smallinfohead\">".
2441 userdate($discussion->modified, $strftimerecent).
2442 " - ".
2443 fullname($discussion).
2444 "</span><br />";
00363389 2445 echo "<span class=\"smallinfo\">$discussion->subject ";
b879effb 2446 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->discussion\">";
00363389 2447 echo $strmore."...</a></span>";
b879effb 2448 echo "</p>\n";
3335f6fb 2449 break;
2450 case "header":
a796d0b8 2451 forum_print_discussion_header($discussion, $forum, $strdatestring);
3335f6fb 2452 break;
2453 default:
3260de67 2454 if ($canreply or $discussion->replies) {
2455 $link = true;
2456 } else {
2457 $link = false;
2458 }
2459 forum_print_post($discussion, $forum->course, $ownpost, $reply=0, $link, $assessed=false);
b879effb 2460 echo "<br>\n";
3335f6fb 2461 break;
f93f848a 2462 }
2463 }
29507631 2464
2465 if ($forum_style == "header") {
2466 echo "</table>";
2467 }
c20b762a 2468
2469 if ($olddiscussionlink) {
e2d41062 2470 if ($forum_style == "minimal") {
2471 echo '<p align="center">';
2472 } else {
2473 echo '<p align="right">';
2474 }
2475 echo "<a href=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id&showall=1\">";
c20b762a 2476 echo get_string("olderdiscussions", "forum")."</a> ...</p>";
2477 }
f93f848a 2478}
2479
c6d691dc 2480function forum_print_discussion($course, $forum, $discussion, $post, $mode, $canreply=NULL) {
501cdbd8 2481
2482 global $USER;
2483
61e96406 2484 if (!empty($USER->id)) {
2485 $ownpost = ($USER->id == $post->userid);
2486 } else {
2487 $ownpost = false;
2488 }
c6d691dc 2489 if ($canreply === NULL) {
2490 $reply = forum_user_can_post($forum);
2491 } else {
2492 $reply = $canreply;
2493 }
501cdbd8 2494
02ebf404 2495 $ratings = NULL;
74f5d1e3 2496 $ratingsmenuused = false;
61e96406 2497 if ($forum->assessed and !empty($USER->id)) {
d6bdd9d5 2498 if ($ratings->scale = make_grades_menu($forum->scale)) {
3bd98ad4 2499 $ratings->assesspublic = $forum->assesspublic;
98914efd 2500 $ratings->assesstimestart = $forum->assesstimestart;
2501 $ratings->assesstimefinish = $forum->assesstimefinish;
3bd98ad4 2502 $ratings->allow = ($forum->assessed != 2 or isteacher($course->id));
2503
02ebf404 2504 echo "<form name=form method=post action=rate.php>";
2505 echo "<input type=hidden name=id value=\"$course->id\">";
9d1b97c5 2506 }
2507 }
2508
74f5d1e3 2509 if (forum_print_post($post, $course->id, $ownpost, $reply, $link=false, $ratings)) {
2510 $ratingsmenuused = true;
2511 }
501cdbd8 2512
2513 switch ($mode) {
2e2e71a8 2514 case FORUM_MODE_FLATOLDEST :
2515 case FORUM_MODE_FLATNEWEST :
65b0e537 2516 default:
02ebf404 2517 echo "<ul>";
74f5d1e3 2518 if (forum_print_posts_flat($post->discussion, $course->id, $mode, $ratings, $reply)) {
2519 $ratingsmenuused = true;
2520 }
02ebf404 2521 echo "</ul>";
501cdbd8 2522 break;
2523
2e2e71a8 2524 case FORUM_MODE_THREADED :
74f5d1e3 2525 if (forum_print_posts_threaded($post->id, $course->id, 0, $ratings, $reply)) {
2526 $ratingsmenuused = true;
2527 }
501cdbd8 2528 break;
2529
2e2e71a8 2530 case FORUM_MODE_NESTED :
74f5d1e3 2531 if (forum_print_posts_nested($post->id, $course->id, $ratings, $reply)) {
2532 $ratingsmenuused = true;
2533 }
501cdbd8 2534 break;
2535 }
2536
74f5d1e3 2537 if ($ratingsmenuused) {
02ebf404 2538 echo "<center><input type=\"submit\" value=\"".get_string("sendinratings", "forum")."\">";
fa01ae8b 2539 if ($forum->scale < 0) {
2540 if ($scale = get_record("scale", "id", abs($forum->scale))) {
2541 print_scale_menu_helpbutton($course->id, $scale );
2542 }
2543 }
02ebf404 2544 echo "</center>";
2545 echo "</form>";
501cdbd8 2546 }
2547}
2548
65b0e537 2549function forum_print_posts_flat($discussion, $course, $direction, $ratings, $reply) {
501cdbd8 2550 global $USER;
2551
501cdbd8 2552 $link = false;
74f5d1e3 2553 $ratingsmenuused = false;
501cdbd8 2554
2555 if ($direction < 0) {
2556 $sort = "ORDER BY created DESC";
2557 } else {
2558 $sort = "ORDER BY created ASC";
2559 }
2560
1f48942e 2561 if ($posts = forum_get_discussion_posts($discussion, $sort)) {
501cdbd8 2562 foreach ($posts as $post) {
ebc3bd2b 2563 $ownpost = ($USER->id == $post->userid);
74f5d1e3 2564 if (forum_print_post($post, $course, $ownpost, $reply, $link, $ratings)) {
2565 $ratingsmenuused = true;
2566 }
501cdbd8 2567 }
501cdbd8 2568 }
74f5d1e3 2569
2570 return $ratingsmenuused;
501cdbd8 2571}
2572
65b0e537 2573function forum_print_posts_threaded($parent, $course, $depth, $ratings, $reply) {
501cdbd8 2574 global $USER;
2575
501cdbd8 2576 $link = false;
74f5d1e3 2577 $ratingsmenuused = false;
501cdbd8 2578
1f48942e 2579 if ($posts = forum_get_child_posts($parent)) {
501cdbd8 2580 foreach ($posts as $post) {
2581
f95c2a73 2582 echo "<ul>";
501cdbd8 2583 if ($depth > 0) {
ebc3bd2b 2584 $ownpost = ($USER->id == $post->userid);
74f5d1e3 2585 if (forum_print_post($post, $course, $ownpost, $reply, $link, $ratings)) {
2586 $ratingsmenuused = true;
2587 }
f95c2a73 2588 echo "<br />";
501cdbd8 2589 } else {
1b26d5e7 2590 $by->name = fullname($post, isteacher($course->id));
d62413e8 2591 $by->date = userdate($post->modified);
f95c2a73 2592 echo "<li><p><a name=\"$post->id\"></a><font size=-1><b><a href=\"discuss.php?d=$post->discussion&parent=$post->id\">$post->subject</a></b> ";
8c3c8481 2593 print_string("bynameondate", "forum", $by);
f95c2a73 2594 echo "</font></p></li>";
501cdbd8 2595 }
2596
74f5d1e3 2597 if (forum_print_posts_threaded($post->id, $course, $depth-1, $ratings, $reply)) {
2598 $ratingsmenuused = true;
2599 }
f95c2a73 2600 echo "</ul>\n";
501cdbd8 2601 }
501cdbd8 2602 }
74f5d1e3 2603 return $ratingsmenuused;
501cdbd8 2604}
2605
65b0e537 2606function forum_print_posts_nested($parent, $course, $ratings, $reply) {
501cdbd8 2607 global $USER;
2608
501cdbd8 2609 $link = false;
74f5d1e3 2610 $ratingsmenuused = false;
501cdbd8 2611
1f48942e 2612 if ($posts = forum_get_child_posts($parent)) {
501cdbd8 2613 foreach ($posts as $post) {
2614
61e96406 2615 if (empty($USER->id)) {
2616 $ownpost = false;
2617 } else {
2618 $ownpost = ($USER->id == $post->userid);
2619 }
501cdbd8 2620
74f5d1e3 2621 echo "<ul>";
2622 if (forum_print_post($post, $course, $ownpost, $reply, $link, $ratings)) {
2623 $ratingsmenuused = true;
2624 }
2625 echo "<br />";
2626 if (forum_print_posts_nested($post->id, $course, $ratings, $reply)) {
2627 $ratingsmenuused = true;
2628 }
2629 echo "</ul>\n";
501cdbd8 2630 }
501cdbd8 2631 }
74f5d1e3 2632 return $ratingsmenuused;
501cdbd8 2633}
2634
2862b309 2635function forum_get_recent_mod_activity(&$activities, &$index, $sincetime, $courseid, $cmid="0", $user="", $groupid="") {
a57a7794 2636// Returns all forum posts since a given time. If forum is specified then
2637// this restricts the results
2638
2639 global $CFG;
2640
2862b309 2641 if ($cmid) {
2642 $forumselect = " AND cm.id = '$cmid'";
a57a7794 2643 } else {
2644 $forumselect = "";
2645 }
2646
2647 if ($user) {
2648 $userselect = " AND u.id = '$user'";
2649 } else {
2650 $userselect = "";
2651 }
2652
a57a7794 2653 $posts = get_records_sql("SELECT p.*, d.name, u.firstname, u.lastname,
2654 u.picture, d.groupid, cm.instance, f.name, cm.section
2655 FROM {$CFG->prefix}forum_posts p,
2656 {$CFG->prefix}forum_discussions d,
2657 {$CFG->prefix}user u,
2658 {$CFG->prefix}course_modules cm,
2659 {$CFG->prefix}forum f
2660 WHERE p.modified > '$sincetime' $forumselect
2661 AND p.userid = u.id $userselect
2662 AND d.course = '$courseid'
2862b309 2663 AND p.discussion = d.id
a57a7794 2664 AND cm.instance = f.id
2665 AND cm.course = d.course
2666 AND cm.course = f.course
2667 AND f.id = d.forum
2668 ORDER BY d.id");
2669
2862b309 2670 if (empty($posts)) {
2671 return;
2672 }
2673
2674 $isteacheredit = isteacheredit($courseid);
a57a7794 2675
90708fc1 2676 foreach ($posts as $post) {
a57a7794 2677
2862b309 2678 if ($groupid and ($post->groupid != -1 and $groupid != $post->groupid and !$isteacheredit)) {
2679 continue;
2680 }
a57a7794 2681
2862b309 2682 $tmpactivity->type = "forum";
2683 $tmpactivity->defaultindex = $index;
2684 $tmpactivity->instance = $post->instance;
2685 $tmpactivity->name = $post->name;
2686 $tmpactivity->section = $post->section;
65b0e537 2687
2862b309 2688 $tmpactivity->content->id = $post->id;
2689 $tmpactivity->content->discussion = $post->discussion;
2690 $tmpactivity->content->subject = $post->subject;
2691 $tmpactivity->content->parent = $post->parent;
a57a7794 2692
2862b309 2693 $tmpactivity->user->userid = $post->userid;
2694 $tmpactivity->user->fullname = fullname($post);
2695 $tmpactivity->user->picture = $post->picture;
65b0e537 2696
2862b309 2697 $tmpactivity->timestamp = $post->modified;
2698 $activities[] = $tmpactivity;
2699
2700 $index++;
90708fc1 2701 }
a57a7794 2702
90708fc1 2703 return;
a57a7794 2704}
2705
2706function forum_print_recent_mod_activity($activity, $course, $detail=false) {
2707
2708 global $CFG;
2709
2710 echo '<table border="0" cellpadding="3" cellspacing="0">';
2711
2712 if ($activity->content->parent) {
2713 $openformat = "<font size=\"2\"><i>";
2714 $closeformat = "</i></font>";
2715 } else {
2716 $openformat = "<b>";
2717 $closeformat = "</b>";
2718 }
2719
2720 echo "<tr><td bgcolor=\"$THEME->cellcontent2\" class=\"forumpostpicture\" width=\"35\" valign=\"top\">";
2721 print_user_picture($activity->user->userid, $course, $activity->user->picture);
2722 echo "</td><td>$openformat";
2723
2724 if ($detail) {
2725 echo "<img src=\"$CFG->modpixpath/$activity->type/icon.gif\" ".
2726 "height=16 width=16 alt=\"$activity->name\"> ";
2727 }
2728 echo "<a href=\"$CFG->wwwroot/mod/forum/discuss.php?d=" . $activity->content->discussion
2729 . "#" . $activity->content->id . "\">";
2730
2731 echo $activity->content->subject;
2732 echo "</a>$closeformat";
2733
2734 echo "<br><font size=\"2\">";
2735 echo "<a href=\"$CFG->wwwroot/user/view.php?id=" . $activity->user->userid . "&course=" . "$course\">"
2736 . $activity->user->fullname . "</a>";
2737 echo " - " . userdate($activity->timestamp) . "</font></td></tr>";
2738 echo "</table>";
2739
2740 return;
2741}
2742
cf84431b 2743function forum_change_discussionid($postid, $discussionid) {
2744/// recursively sets the discussion field to $discussionid on $postid and all its children
2745/// used when pruning a post
2746 set_field('forum_posts', 'discussion', $discussionid, 'id', $postid);
2747 if ($posts = get_records('forum_posts', 'parent', $postid)) {
2748 foreach ($posts as $post) {
2749 forum_change_discussionid($post->id, $discussionid);
2750 }
2751 }
2752 return true;
2753}
2754
f93f848a 2755?>