Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Rearranged functions to match other modules, and added forum_grades()

  • Loading branch information...
commit caadf0090909a590dace290d30f37c3c967bf08f 1 parent fa452aa
moodler authored

Showing 1 changed file with 423 additions and 393 deletions. Show diff stats Hide diff stats

  1. +423 393 mod/forum/lib.php
816 mod/forum/lib.php
@@ -32,7 +32,366 @@
32 32 $FORUM_MANY_DISCUSSIONS = 10;
33 33
34 34
35   -/// FUNCTIONS ///////////////////////////////////////////////////////////
  35 +/// STANDARD FUNCTIONS ///////////////////////////////////////////////////////////
  36 +
  37 +function forum_add_instance($forum) {
  38 +// Given an object containing all the necessary data,
  39 +// (defined by the form in mod.html) this function
  40 +// will create a new instance and return the id number
  41 +// of the new instance.
  42 +
  43 + global $CFG;
  44 +
  45 + $forum->timemodified = time();
  46 +
  47 + if (! $forum->id = insert_record("forum", $forum)) {
  48 + return false;
  49 + }
  50 +
  51 + if ($forum->type == "single") { // Create related discussion.
  52 +
  53 + $discussion->course = $forum->course;
  54 + $discussion->forum = $forum->id;
  55 + $discussion->name = $forum->name;
  56 + $discussion->intro = $forum->intro;
  57 + $discussion->assessed = $forum->assessed;
  58 +
  59 + if (! forum_add_discussion($discussion)) {
  60 + error("Could not add the discussion for this forum");
  61 + }
  62 + }
  63 + add_to_log($forum->course, "forum", "add", "index.php?f=$forum->id", "$forum->id");
  64 +
  65 + return $forum->id;
  66 +}
  67 +
  68 +
  69 +function forum_update_instance($forum) {
  70 +// Given an object containing all the necessary data,
  71 +// (defined by the form in mod.html) this function
  72 +// will update an existing instance with new data.
  73 +
  74 + $forum->timemodified = time();
  75 + $forum->id = $forum->instance;
  76 +
  77 + if ($forum->type == "single") { // Update related discussion and post.
  78 + if (! $discussion = get_record("forum_discussions", "forum", $forum->id)) {
  79 + if ($discussions = get_records("forum_discussions", "forum", $forum->id, "timemodified ASC")) {
  80 + notify("Warning! There is more than one discussion in this forum - using the most recent");
  81 + $discussion = array_pop($discussions);
  82 + } else {
  83 + error("Could not find the discussion in this forum");
  84 + }
  85 + }
  86 + if (! $post = get_record("forum_posts", "id", $discussion->firstpost)) {
  87 + error("Could not find the first post in this forum discussion");
  88 + }
  89 +
  90 + $post->subject = $forum->name;
  91 + $post->message = $forum->intro;
  92 + $post->modified = $forum->timemodified;
  93 +
  94 + if (! update_record("forum_posts", $post)) {
  95 + error("Could not update the first post");
  96 + }
  97 +
  98 + $discussion->name = $forum->name;
  99 +
  100 + if (! update_record("forum_discussions", $discussion)) {
  101 + error("Could not update the discussion");
  102 + }
  103 + }
  104 +
  105 + if (update_record("forum", $forum)) {
  106 + add_to_log($forum->course, "forum", "update", "index.php?f=$forum->id", "$forum->id");
  107 + return true;
  108 + } else {
  109 + return false;
  110 + }
  111 +}
  112 +
  113 +
  114 +function forum_delete_instance($id) {
  115 +// Given an ID of an instance of this module,
  116 +// this function will permanently delete the instance
  117 +// and any data that depends on it.
  118 +
  119 + if (! $forum = get_record("forum", "id", "$id")) {
  120 + return false;
  121 + }
  122 +
  123 + $result = true;
  124 +
  125 + if ($discussions = get_records("forum_discussions", "forum", $forum->id)) {
  126 + foreach ($discussions as $discussion) {
  127 + if (! forum_delete_discussion($discussion)) {
  128 + $result = false;
  129 + }
  130 + }
  131 + }
  132 +
  133 + if (! delete_records("forum_subscriptions", "forum", "$forum->id")) {
  134 + $result = false;
  135 + }
  136 +
  137 + if (! delete_records("forum", "id", "$forum->id")) {
  138 + $result = false;
  139 + }
  140 +
  141 + return $result;
  142 +}
  143 +
  144 +
  145 +function forum_cron () {
  146 +// Function to be run periodically according to the moodle cron
  147 +// Finds all posts that have yet to be mailed out, and mails them
  148 +
  149 + global $CFG, $USER;
  150 +
  151 + $cutofftime = time() - $CFG->maxeditingtime;
  152 +
  153 + if ($posts = get_records_sql("SELECT p.*, d.course FROM forum_posts p, forum_discussions d
  154 + WHERE p.mailed = '0' AND p.created < '$cutofftime' AND p.discussion = d.id")) {
  155 +
  156 + $timenow = time();
  157 +
  158 + foreach ($posts as $post) {
  159 +
  160 + print_string("processingpost", "forum", $post->id);
  161 + echo " ... ";
  162 +
  163 + if (! $userfrom = get_record("user", "id", "$post->user")) {
  164 + echo "Could not find user $post->user\n";
  165 + continue;
  166 + }
  167 +
  168 + if (! $discussion = get_record("forum_discussions", "id", "$post->discussion")) {
  169 + echo "Could not find discussion $post->discussion\n";
  170 + continue;
  171 + }
  172 +
  173 + if (! $forum = get_record("forum", "id", "$discussion->forum")) {
  174 + echo "Could not find forum $discussion->forum\n";
  175 + continue;
  176 + }
  177 +
  178 + if (! $course = get_record("course", "id", "$forum->course")) {
  179 + echo "Could not find course $forum->course\n";
  180 + continue;
  181 + }
  182 +
  183 + if ($users = forum_subscribed_users($course, $forum)) {
  184 + $canunsubscribe = ! forum_is_forcesubscribed($forum->id);
  185 +
  186 + $mailcount=0;
  187 + foreach ($users as $userto) {
  188 + $USER->lang = $userto->lang; // Affects the language of get_string
  189 +
  190 +
  191 + $by->name = "$userfrom->firstname $userfrom->lastname";
  192 + $by->date = userdate($post->created, "", $userto->timezone);
  193 + $strbynameondate = get_string("bynameondate", "forum", $by);
  194 +
  195 + $strforums = get_string("forums", "forum");
  196 +
  197 + $postsubject = "$course->shortname: $post->subject";
  198 + $posttext = "$course->shortname -> $strforums -> $forum->name";
  199 +
  200 + if ($discussion->name == $forum->name) {
  201 + $posttext .= "\n";
  202 + } else {
  203 + $posttext .= " -> $discussion->name\n";
  204 + }
  205 + $posttext .= "---------------------------------------------------------------------\n";
  206 + $posttext .= "$post->subject\n";
  207 + $posttext .= $strbynameondate."\n";
  208 + $posttext .= "---------------------------------------------------------------------\n";
  209 + $posttext .= strip_tags($post->message);
  210 + $posttext .= "\n\n";
  211 + if ($post->attachment) {
  212 + $post->course = $course->id;
  213 + $post->forum = $forum->id;
  214 + $posttext .= forum_print_attachments($post, "text");
  215 + }
  216 + $posttext .= "---------------------------------------------------------------------\n";
  217 + $posttext .= get_string("postmailinfo", "forum", $course->shortname)."\n";
  218 + $posttext .= "$CFG->wwwroot/mod/forum/post.php?reply=$post->id\n";
  219 + if ($canunsubscribe) {
  220 + $posttext .= "\n---------------------------------------------------------------------\n";
  221 + $posttext .= get_string("unsubscribe", "forum");
  222 + $posttext .= ": $CFG->wwwroot/mod/forum/subscribe.php?id=$forum->id\n";
  223 + }
  224 +
  225 + if ($userto->mailformat == 1) { // HTML
  226 + $posthtml = "<P><FONT FACE=sans-serif>".
  227 + "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A> -> ".
  228 + "<A HREF=\"$CFG->wwwroot/mod/forum/index.php?id=$course->id\">$strforums</A> -> ".
  229 + "<A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id\">$forum->name</A>";
  230 + if ($discussion->name == $forum->name) {
  231 + $posthtml .= "</FONT></P>";
  232 + } else {
  233 + $posthtml .= " -> <A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->id\">$discussion->name</A></FONT></P>";
  234 + }
  235 + $posthtml .= forum_make_mail_post($post, $userfrom, $userto, $course, false, true, false, false);
  236 +
  237 + if ($canunsubscribe) {
  238 + $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>";
  239 + }
  240 +
  241 + } else {
  242 + $posthtml = "";
  243 + }
  244 +
  245 + if (! email_to_user($userto, $userfrom, $postsubject, $posttext, $posthtml)) {
  246 + echo "Error: mod/forum/cron.php: Could not send out mail for id $post->id to user $userto->id ($userto->email)\n";
  247 + } else {
  248 + $mailcount++;
  249 + }
  250 + }
  251 + echo "mailed to $mailcount users ...";
  252 + }
  253 +
  254 + if (! set_field("forum_posts", "mailed", "1", "id", "$post->id")) {
  255 + echo "Could not update the mailed field for id $post->id\n";
  256 + }
  257 + echo "\n";
  258 + }
  259 + }
  260 +
  261 + return true;
  262 +}
  263 +
  264 +function forum_user_outline($course, $user, $mod, $forum) {
  265 +
  266 + if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
  267 + FROM forum f, forum_discussions d, forum_posts p, user u
  268 + WHERE f.id = '$forum->id' AND d.forum = f.id AND p.discussion = d.id
  269 + AND p.user = '$user->id' AND p.user = u.id
  270 + ORDER BY p.modified ASC")) {
  271 +
  272 + $result->info = get_string("numposts", "forum", count($posts));
  273 +
  274 + $lastpost = array_pop($posts);
  275 + $result->time = $lastpost->modified;
  276 + return $result;
  277 + }
  278 + return NULL;
  279 +}
  280 +
  281 +
  282 +function forum_user_complete($course, $user, $mod, $forum) {
  283 + global $CFG;
  284 +
  285 + if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
  286 + FROM forum f, forum_discussions d, forum_posts p, user u
  287 + WHERE f.id = '$forum->id' AND d.forum = f.id AND p.discussion = d.id
  288 + AND p.user = '$user->id' AND p.user = u.id
  289 + ORDER BY p.modified ASC")) {
  290 +
  291 + foreach ($posts as $post) {
  292 + if ($post->parent) {
  293 + $footer = "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion&parent=$post->parent\">".
  294 + get_string("parentofthispost", "forum")."</A>";
  295 + } else {
  296 + $footer = "";
  297 + }
  298 +
  299 + forum_print_post($post, $course->id, $ownpost=false, $reply=false, $link=false, $rate=false, $footer);
  300 + }
  301 +
  302 + } else {
  303 + echo "<P>".get_string("noposts", "forum")."</P>";
  304 + }
  305 +
  306 +}
  307 +
  308 +function forum_print_recent_activity(&$logs, $isteacher=false) {
  309 + global $CFG, $COURSE_TEACHER_COLOR;
  310 +
  311 + $heading = false;
  312 + $content = false;
  313 +
  314 + foreach ($logs as $log) {
  315 + if ($log->module == "forum") {
  316 + $post = NULL;
  317 +
  318 + if ($log->action == "add post") {
  319 + $post = get_record_sql("SELECT p.*, d.forum, u.firstname, u.lastname,
  320 + u.email, u.picture, u.id as userid
  321 + FROM forum_discussions d, forum_posts p, user u
  322 + WHERE p.id = '$log->info' AND d.id = p.discussion
  323 + AND p.user = u.id and u.deleted <> '1'");
  324 +
  325 + } else if ($log->action == "add discussion") {
  326 + $post = get_record_sql("SELECT p.*, d.forum, u.firstname, u.lastname,
  327 + u.email, u.picture, u.id as userid
  328 + FROM forum_discussions d, forum_posts p, user u
  329 + WHERE d.id = '$log->info' AND d.firstpost = p.id
  330 + AND p.user = u.id and u.deleted <> '1'");
  331 + }
  332 +
  333 + if ($post) {
  334 + $teacherpost = "";
  335 + if ($forum = get_record("forum", "id", $post->forum) ) {
  336 + if ($forum->type == "teacher") {
  337 + if ($isteacher) {
  338 + $teacherpost = "COLOR=$COURSE_TEACHER_COLOR";
  339 + } else {
  340 + continue;
  341 + }
  342 + }
  343 + }
  344 + if (! $heading) {
  345 + print_headline(get_string("newforumposts", "forum").":");
  346 + $heading = true;
  347 + $content = true;
  348 + }
  349 + $date = userdate($post->modified, "%e %b, %H:%M");
  350 + echo "<P><FONT SIZE=1 $teacherpost>$date - $post->firstname $post->lastname<BR>";
  351 + echo "\"<A HREF=\"$CFG->wwwroot/mod/forum/$log->url\">";
  352 + if ($log->action == "add") {
  353 + echo "<B>$post->subject</B>";
  354 + } else {
  355 + echo "$post->subject";
  356 + }
  357 + echo "</A>\"</FONT></P>";
  358 + }
  359 + }
  360 + }
  361 + return $content;
  362 +}
  363 +
  364 +
  365 +function forum_grades($forumid) {
  366 +/// Must return an array of grades, indexed by user, and a max grade.
  367 + global $FORUM_POST_RATINGS;
  368 +
  369 + if ($ratings = get_records_sql_menu("SELECT p.user, r.rating
  370 + FROM forum_discussions d, forum_posts p, forum_ratings r
  371 + WHERE d.forum = '$forumid'
  372 + AND p.discussion = d.id
  373 + AND r.post = p.id")) {
  374 + foreach ($ratings as $user => $rating) {
  375 + if (!isset($sumrating[$user])) {
  376 + $sumrating[$user][1] = 0;
  377 + $sumrating[$user][2] = 0;
  378 + $sumrating[$user][3] = 0;
  379 + }
  380 + $sumrating[$user][$rating] += 1;
  381 + }
  382 + foreach ($sumrating as $user => $rating) {
  383 + $return->grades[$user] = $rating[1]."s/".$rating[2]."/".$rating[3]."c";
  384 + }
  385 + } else {
  386 + $return->grades = array();
  387 + }
  388 +
  389 + $return->maxgrade = "";
  390 + return $return;
  391 +}
  392 +
  393 +
  394 +/// OTHER FUNCTIONS ///////////////////////////////////////////////////////////
36 395
37 396
38 397 function forum_get_course_forum($courseid, $type) {
@@ -621,249 +980,46 @@ function forum_add_discussion($discussion) {
621 980 set_field("forum_posts", "attachment", $post->attachment, "id", $post->id); //ignore errors
622 981 }
623 982
624   - // Now do the real module entry
625   -
626   - $discussion->firstpost = $post->id;
627   - $discussion->timemodified = $timenow;
628   -
629   - if (! $discussion->id = insert_record("forum_discussions", $discussion) ) {
630   - delete_records("forum_posts", "id", $post->id);
631   - return 0;
632   - }
633   -
634   - // Finally, set the pointer on the post.
635   - if (! set_field("forum_posts", "discussion", $discussion->id, "id", $post->id)) {
636   - delete_records("forum_posts", "id", $post->id);
637   - delete_records("forum_discussions", "id", $discussion->id);
638   - return 0;
639   - }
640   -
641   - return $discussion->id;
642   -}
643   -
644   -
645   -function forum_delete_discussion($discussion) {
646   -// $discussion is a discussion record object
647   -
648   - $result = true;
649   -
650   - if ($posts = get_records("forum_posts", "discussion", $discussion->id)) {
651   - foreach ($posts as $post) {
652   - $post->course = $discussion->course;
653   - $post->forum = $discussion->forum;
654   - if (! delete_records("forum_ratings", "post", "$post->id")) {
655   - $result = false;
656   - }
657   - if (! forum_delete_post($post)) {
658   - $result = false;
659   - }
660   - }
661   - }
662   -
663   - if (! delete_records("forum_discussions", "id", "$discussion->id")) {
664   - $result = false;
665   - }
666   -
667   - return $result;
668   -}
669   -
670   -
671   -function forum_delete_post($post) {
672   - if (delete_records("forum_posts", "id", $post->id)) {
673   - delete_records("forum_ratings", "post", $post->id); // Just in case
674   - if ($post->attachment) {
675   - $discussion = get_record("forum_discussions", "id", $post->discussion);
676   - $post->course = $discussion->course;
677   - $post->forum = $discussion->forum;
678   - forum_delete_old_attachments($post);
679   - }
680   - return true;
681   - }
682   - return false;
683   -}
684   -
685   -
686   -function forum_print_user_discussions($courseid, $userid) {
687   - global $CFG, $USER;
688   -
689   - $discussions = get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture,
690   - u.id as userid, f.type as forumtype, f.name as forumname, f.id as forumid
691   - FROM forum_discussions d, forum_posts p, user u, forum f
692   - WHERE d.course = '$courseid' AND p.discussion = d.id AND
693   - p.parent = 0 AND p.user = u.id AND u.id = '$userid' AND
694   - d.forum = f.id
695   - ORDER BY p.created ASC");
696   -
697   - if ($discussions) {
698   - $user = get_record("user", "id", $userid);
699   - echo "<HR>";
700   - print_heading( get_string("discussionsstartedby", "forum", "$user->firstname $user->lastname") );
701   - $replies = forum_count_discussion_replies();
702   - foreach ($discussions as $discussion) {
703   - if (($discussion->forumtype == "teacher") and !isteacher($courseid)) {
704   - continue;
705   - }
706   - if ($replies[$discussion->discussion]) {
707   - $discussion->replies = $replies[$discussion->discussion]->replies;
708   - } else {
709   - $discussion->replies = 0;
710   - }
711   - $inforum = get_string("inforum", "forum", "<A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$discussion->forumid\">$discussion->forumname</A>");
712   - $discussion->subject .= " ($inforum)";
713   - $ownpost = ($discussion->userid == $USER->id);
714   - forum_print_post($discussion, $courseid, $ownpost, $reply=0, $link=1, $assessed=false);
715   - echo "<BR>\n";
716   - }
717   - }
718   -}
719   -
720   -
721   -function forum_user_outline($course, $user, $mod, $forum) {
722   -
723   - if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
724   - FROM forum f, forum_discussions d, forum_posts p, user u
725   - WHERE f.id = '$forum->id' AND d.forum = f.id AND p.discussion = d.id
726   - AND p.user = '$user->id' AND p.user = u.id
727   - ORDER BY p.modified ASC")) {
728   -
729   - $result->info = get_string("numposts", "forum", count($posts));
730   -
731   - $lastpost = array_pop($posts);
732   - $result->time = $lastpost->modified;
733   - return $result;
734   - }
735   - return NULL;
736   -}
737   -
738   -
739   -function forum_user_complete($course, $user, $mod, $forum) {
740   - global $CFG;
741   -
742   - if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture
743   - FROM forum f, forum_discussions d, forum_posts p, user u
744   - WHERE f.id = '$forum->id' AND d.forum = f.id AND p.discussion = d.id
745   - AND p.user = '$user->id' AND p.user = u.id
746   - ORDER BY p.modified ASC")) {
747   -
748   - foreach ($posts as $post) {
749   - if ($post->parent) {
750   - $footer = "<A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$post->discussion&parent=$post->parent\">".
751   - get_string("parentofthispost", "forum")."</A>";
752   - } else {
753   - $footer = "";
754   - }
755   -
756   - forum_print_post($post, $course->id, $ownpost=false, $reply=false, $link=false, $rate=false, $footer);
757   - }
758   -
759   - } else {
760   - echo "<P>".get_string("noposts", "forum")."</P>";
761   - }
762   -
763   -}
764   -
765   -
766   -function forum_add_instance($forum) {
767   -// Given an object containing all the necessary data,
768   -// (defined by the form in mod.html) this function
769   -// will create a new instance and return the id number
770   -// of the new instance.
771   -
772   - global $CFG;
773   -
774   - $forum->timemodified = time();
775   -
776   - if (! $forum->id = insert_record("forum", $forum)) {
777   - return false;
778   - }
779   -
780   - if ($forum->type == "single") { // Create related discussion.
781   -
782   - $discussion->course = $forum->course;
783   - $discussion->forum = $forum->id;
784   - $discussion->name = $forum->name;
785   - $discussion->intro = $forum->intro;
786   - $discussion->assessed = $forum->assessed;
787   -
788   - if (! forum_add_discussion($discussion)) {
789   - error("Could not add the discussion for this forum");
790   - }
791   - }
792   - add_to_log($forum->course, "forum", "add", "index.php?f=$forum->id", "$forum->id");
793   -
794   - return $forum->id;
795   -}
796   -
797   -
798   -function forum_update_instance($forum) {
799   -// Given an object containing all the necessary data,
800   -// (defined by the form in mod.html) this function
801   -// will update an existing instance with new data.
802   -
803   - $forum->timemodified = time();
804   - $forum->id = $forum->instance;
805   -
806   - if ($forum->type == "single") { // Update related discussion and post.
807   - if (! $discussion = get_record("forum_discussions", "forum", $forum->id)) {
808   - if ($discussions = get_records("forum_discussions", "forum", $forum->id, "timemodified ASC")) {
809   - notify("Warning! There is more than one discussion in this forum - using the most recent");
810   - $discussion = array_pop($discussions);
811   - } else {
812   - error("Could not find the discussion in this forum");
813   - }
814   - }
815   - if (! $post = get_record("forum_posts", "id", $discussion->firstpost)) {
816   - error("Could not find the first post in this forum discussion");
817   - }
818   -
819   - $post->subject = $forum->name;
820   - $post->message = $forum->intro;
821   - $post->modified = $forum->timemodified;
822   -
823   - if (! update_record("forum_posts", $post)) {
824   - error("Could not update the first post");
825   - }
  983 + // Now do the real module entry
826 984
827   - $discussion->name = $forum->name;
  985 + $discussion->firstpost = $post->id;
  986 + $discussion->timemodified = $timenow;
828 987
829   - if (! update_record("forum_discussions", $discussion)) {
830   - error("Could not update the discussion");
831   - }
  988 + if (! $discussion->id = insert_record("forum_discussions", $discussion) ) {
  989 + delete_records("forum_posts", "id", $post->id);
  990 + return 0;
832 991 }
833 992
834   - if (update_record("forum", $forum)) {
835   - add_to_log($forum->course, "forum", "update", "index.php?f=$forum->id", "$forum->id");
836   - return true;
837   - } else {
838   - return false;
  993 + // Finally, set the pointer on the post.
  994 + if (! set_field("forum_posts", "discussion", $discussion->id, "id", $post->id)) {
  995 + delete_records("forum_posts", "id", $post->id);
  996 + delete_records("forum_discussions", "id", $discussion->id);
  997 + return 0;
839 998 }
840   -}
841 999
  1000 + return $discussion->id;
  1001 +}
842 1002
843   -function forum_delete_instance($id) {
844   -// Given an ID of an instance of this module,
845   -// this function will permanently delete the instance
846   -// and any data that depends on it.
847 1003
848   - if (! $forum = get_record("forum", "id", "$id")) {
849   - return false;
850   - }
  1004 +function forum_delete_discussion($discussion) {
  1005 +// $discussion is a discussion record object
851 1006
852 1007 $result = true;
853 1008
854   - if ($discussions = get_records("forum_discussions", "forum", $forum->id)) {
855   - foreach ($discussions as $discussion) {
856   - if (! forum_delete_discussion($discussion)) {
  1009 + if ($posts = get_records("forum_posts", "discussion", $discussion->id)) {
  1010 + foreach ($posts as $post) {
  1011 + $post->course = $discussion->course;
  1012 + $post->forum = $discussion->forum;
  1013 + if (! delete_records("forum_ratings", "post", "$post->id")) {
  1014 + $result = false;
  1015 + }
  1016 + if (! forum_delete_post($post)) {
857 1017 $result = false;
858 1018 }
859 1019 }
860 1020 }
861 1021
862   - if (! delete_records("forum_subscriptions", "forum", "$forum->id")) {
863   - $result = false;
864   - }
865   -
866   - if (! delete_records("forum", "id", "$forum->id")) {
  1022 + if (! delete_records("forum_discussions", "id", "$discussion->id")) {
867 1023 $result = false;
868 1024 }
869 1025
@@ -871,126 +1027,55 @@ function forum_delete_instance($id) {
871 1027 }
872 1028
873 1029
874   -function forum_cron () {
875   -// Function to be run periodically according to the moodle cron
876   -// Finds all posts that have yet to be mailed out, and mails them
877   -
878   - global $CFG, $USER;
879   -
880   - $cutofftime = time() - $CFG->maxeditingtime;
881   -
882   - if ($posts = get_records_sql("SELECT p.*, d.course FROM forum_posts p, forum_discussions d
883   - WHERE p.mailed = '0' AND p.created < '$cutofftime' AND p.discussion = d.id")) {
884   -
885   - $timenow = time();
886   -
887   - foreach ($posts as $post) {
888   -
889   - print_string("processingpost", "forum", $post->id);
890   - echo " ... ";
891   -
892   - if (! $userfrom = get_record("user", "id", "$post->user")) {
893   - echo "Could not find user $post->user\n";
894   - continue;
895   - }
  1030 +function forum_delete_post($post) {
  1031 + if (delete_records("forum_posts", "id", $post->id)) {
  1032 + delete_records("forum_ratings", "post", $post->id); // Just in case
  1033 + if ($post->attachment) {
  1034 + $discussion = get_record("forum_discussions", "id", $post->discussion);
  1035 + $post->course = $discussion->course;
  1036 + $post->forum = $discussion->forum;
  1037 + forum_delete_old_attachments($post);
  1038 + }
  1039 + return true;
  1040 + }
  1041 + return false;
  1042 +}
896 1043
897   - if (! $discussion = get_record("forum_discussions", "id", "$post->discussion")) {
898   - echo "Could not find discussion $post->discussion\n";
899   - continue;
900   - }
901 1044
902   - if (! $forum = get_record("forum", "id", "$discussion->forum")) {
903   - echo "Could not find forum $discussion->forum\n";
904   - continue;
905   - }
  1045 +function forum_print_user_discussions($courseid, $userid) {
  1046 + global $CFG, $USER;
906 1047
907   - if (! $course = get_record("course", "id", "$forum->course")) {
908   - echo "Could not find course $forum->course\n";
  1048 + $discussions = get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture,
  1049 + u.id as userid, f.type as forumtype, f.name as forumname, f.id as forumid
  1050 + FROM forum_discussions d, forum_posts p, user u, forum f
  1051 + WHERE d.course = '$courseid' AND p.discussion = d.id AND
  1052 + p.parent = 0 AND p.user = u.id AND u.id = '$userid' AND
  1053 + d.forum = f.id
  1054 + ORDER BY p.created ASC");
  1055 +
  1056 + if ($discussions) {
  1057 + $user = get_record("user", "id", $userid);
  1058 + echo "<HR>";
  1059 + print_heading( get_string("discussionsstartedby", "forum", "$user->firstname $user->lastname") );
  1060 + $replies = forum_count_discussion_replies();
  1061 + foreach ($discussions as $discussion) {
  1062 + if (($discussion->forumtype == "teacher") and !isteacher($courseid)) {
909 1063 continue;
910 1064 }
911   -
912   - if ($users = forum_subscribed_users($course, $forum)) {
913   - $canunsubscribe = ! forum_is_forcesubscribed($forum->id);
914   -
915   - $mailcount=0;
916   - foreach ($users as $userto) {
917   - $USER->lang = $userto->lang; // Affects the language of get_string
918   -
919   -
920   - $by->name = "$userfrom->firstname $userfrom->lastname";
921   - $by->date = userdate($post->created, "", $userto->timezone);
922   - $strbynameondate = get_string("bynameondate", "forum", $by);
923   -
924   - $strforums = get_string("forums", "forum");
925   -
926   - $postsubject = "$course->shortname: $post->subject";
927   - $posttext = "$course->shortname -> $strforums -> $forum->name";
928   -
929   - if ($discussion->name == $forum->name) {
930   - $posttext .= "\n";
931   - } else {
932   - $posttext .= " -> $discussion->name\n";
933   - }
934   - $posttext .= "---------------------------------------------------------------------\n";
935   - $posttext .= "$post->subject\n";
936   - $posttext .= $strbynameondate."\n";
937   - $posttext .= "---------------------------------------------------------------------\n";
938   - $posttext .= strip_tags($post->message);
939   - $posttext .= "\n\n";
940   - if ($post->attachment) {
941   - $post->course = $course->id;
942   - $post->forum = $forum->id;
943   - $posttext .= forum_print_attachments($post, "text");
944   - }
945   - $posttext .= "---------------------------------------------------------------------\n";
946   - $posttext .= get_string("postmailinfo", "forum", $course->shortname)."\n";
947   - $posttext .= "$CFG->wwwroot/mod/forum/post.php?reply=$post->id\n";
948   - if ($canunsubscribe) {
949   - $posttext .= "\n---------------------------------------------------------------------\n";
950   - $posttext .= get_string("unsubscribe", "forum");
951   - $posttext .= ": $CFG->wwwroot/mod/forum/subscribe.php?id=$forum->id\n";
952   - }
953   -
954   - if ($userto->mailformat == 1) { // HTML
955   - $posthtml = "<P><FONT FACE=sans-serif>".
956   - "<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A> -> ".
957   - "<A HREF=\"$CFG->wwwroot/mod/forum/index.php?id=$course->id\">$strforums</A> -> ".
958   - "<A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$forum->id\">$forum->name</A>";
959   - if ($discussion->name == $forum->name) {
960   - $posthtml .= "</FONT></P>";
961   - } else {
962   - $posthtml .= " -> <A HREF=\"$CFG->wwwroot/mod/forum/discuss.php?d=$discussion->id\">$discussion->name</A></FONT></P>";
963   - }
964   - $posthtml .= forum_make_mail_post($post, $userfrom, $userto, $course, false, true, false, false);
965   -
966   - if ($canunsubscribe) {
967   - $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>";
968   - }
969   -
970   - } else {
971   - $posthtml = "";
972   - }
973   -
974   - if (! email_to_user($userto, $userfrom, $postsubject, $posttext, $posthtml)) {
975   - echo "Error: mod/forum/cron.php: Could not send out mail for id $post->id to user $userto->id ($userto->email)\n";
976   - } else {
977   - $mailcount++;
978   - }
979   - }
980   - echo "mailed to $mailcount users ...";
981   - }
982   -
983   - if (! set_field("forum_posts", "mailed", "1", "id", "$post->id")) {
984   - echo "Could not update the mailed field for id $post->id\n";
  1065 + if ($replies[$discussion->discussion]) {
  1066 + $discussion->replies = $replies[$discussion->discussion]->replies;
  1067 + } else {
  1068 + $discussion->replies = 0;
985 1069 }
986   - echo "\n";
  1070 + $inforum = get_string("inforum", "forum", "<A HREF=\"$CFG->wwwroot/mod/forum/view.php?f=$discussion->forumid\">$discussion->forumname</A>");
  1071 + $discussion->subject .= " ($inforum)";
  1072 + $ownpost = ($discussion->userid == $USER->id);
  1073 + forum_print_post($discussion, $courseid, $ownpost, $reply=0, $link=1, $assessed=false);
  1074 + echo "<BR>\n";
987 1075 }
988 1076 }
989   -
990   - return true;
991 1077 }
992 1078
993   -
994 1079 function forum_forcesubscribe($forumid, $value=1) {
995 1080 return set_field("forum", "forcesubscribe", $value, "id", $forumid);
996 1081 }
@@ -1304,60 +1389,5 @@ function forum_set_display_mode($mode=0) {
1304 1389 }
1305 1390 }
1306 1391
1307   -function forum_print_recent_activity(&$logs, $isteacher=false) {
1308   - global $CFG, $COURSE_TEACHER_COLOR;
1309   -
1310   - $heading = false;
1311   - $content = false;
1312   -
1313   - foreach ($logs as $log) {
1314   - if ($log->module == "forum") {
1315   - $post = NULL;
1316   -
1317   - if ($log->action == "add post") {
1318   - $post = get_record_sql("SELECT p.*, d.forum, u.firstname, u.lastname,
1319   - u.email, u.picture, u.id as userid
1320   - FROM forum_discussions d, forum_posts p, user u
1321   - WHERE p.id = '$log->info' AND d.id = p.discussion
1322   - AND p.user = u.id and u.deleted <> '1'");
1323   -
1324   - } else if ($log->action == "add discussion") {
1325   - $post = get_record_sql("SELECT p.*, d.forum, u.firstname, u.lastname,
1326   - u.email, u.picture, u.id as userid
1327   - FROM forum_discussions d, forum_posts p, user u
1328   - WHERE d.id = '$log->info' AND d.firstpost = p.id
1329   - AND p.user = u.id and u.deleted <> '1'");
1330   - }
1331   -
1332   - if ($post) {
1333   - $teacherpost = "";
1334   - if ($forum = get_record("forum", "id", $post->forum) ) {
1335   - if ($forum->type == "teacher") {
1336   - if ($isteacher) {
1337   - $teacherpost = "COLOR=$COURSE_TEACHER_COLOR";
1338   - } else {
1339   - continue;
1340   - }
1341   - }
1342   - }
1343   - if (! $heading) {
1344   - print_headline(get_string("newforumposts", "forum").":");
1345   - $heading = true;
1346   - $content = true;
1347   - }
1348   - $date = userdate($post->modified, "%e %b, %H:%M");
1349   - echo "<P><FONT SIZE=1 $teacherpost>$date - $post->firstname $post->lastname<BR>";
1350   - echo "\"<A HREF=\"$CFG->wwwroot/mod/forum/$log->url\">";
1351   - if ($log->action == "add") {
1352   - echo "<B>$post->subject</B>";
1353   - } else {
1354   - echo "$post->subject";
1355   - }
1356   - echo "</A>\"</FONT></P>";
1357   - }
1358   - }
1359   - }
1360   - return $content;
1361   -}
1362 1392
1363 1393 ?>

0 comments on commit caadf00

Please sign in to comment.
Something went wrong with that request. Please try again.