diff --git a/admin/index.php b/admin/index.php index 0309fc3dd73..711753e8266 100644 --- a/admin/index.php +++ b/admin/index.php @@ -12,9 +12,12 @@ print_header("Setting up database", "Setting up database", "Setting up databases for the first time", ""); if (file_exists("$CFG->libdir/db/$CFG->dbtype.sql")) { + $db->debug = true; if (modify_database("$CFG->libdir/db/$CFG->dbtype.sql")) { + $db->debug = false; notify("Main databases set up successfully"); } else { + $db->debug = false; error("Error: Main databases NOT set up successfully"); } } else { @@ -32,7 +35,9 @@ if ($dversion = get_field("config", "value", "name", "version")) { if ($version > $dversion) { // upgrade notify("Upgrading databases from version $dversion to $version..."); + $db->debug=true; if (upgrade_moodle($dversion)) { + $db->debug=false; if (set_field("config", "value", "$version", "name", "version")) { notify("Databases were successfully upgraded"); print_heading("Continue"); @@ -41,6 +46,7 @@ notify("Upgrade failed! (Could not update version in config table)"); } } else { + $db->debug=false; notify("Upgrade failed! See /version.php"); } } else if ($version < $dversion) { @@ -55,14 +61,17 @@ print_heading("Continue"); die; } else { + $db->debug=true; if (upgrade_moodle(0)) { print_heading("Continue"); } else { error("A problem occurred inserting current version into databases"); } + $db->debug=false; } } + // Find and check all modules and load them up. $dir = opendir("$CFG->dirroot/mod"); while ($mod = readdir($dir)) { @@ -77,7 +86,7 @@ unset($module); - include_once("$CFG->dirroot/mod/$mod/module.php"); # defines $module + include_once("$CFG->dirroot/mod/$mod/version.php"); # defines $module with version etc if (!isset($module)) { continue; @@ -92,7 +101,9 @@ notify("$module->name module needs upgrading"); $upgrade_function = $module->name."_upgrade"; if (function_exists($upgrade_function)) { + $db->debug=true; if ($upgrade_function($currmodule->version, $module)) { + $db->debug=false; // OK so far, now update the modules record $module->id = $currmodule->id; if (! update_record("modules", $module)) { @@ -100,6 +111,7 @@ } notify("$module->name module was successfully upgraded"); } else { + $db->debug=false; notify("Upgrading $module->name from $currmodule->version to $module->version FAILED!"); } } diff --git a/course/lib.php b/course/lib.php index f707420ad64..37714df2999 100644 --- a/course/lib.php +++ b/course/lib.php @@ -276,10 +276,6 @@ function print_recent_activity($course) { $info = split(" ", $log->info); $modname = get_field($info[0], "name", "id", $info[1]); - if ($info[0] == "discuss") { - $info[0] = "discussion"; // nasty hack, really. - } - switch ($log->action) { case "add mod": $changelist["$log->info"] = array ("operation" => "add", "text" => "Added a ".$info[0].":
wwwroot/course/$log->url\">$modname"); @@ -322,19 +318,19 @@ function print_recent_activity($course) { $heading = false; foreach ($logs as $log) { - if ($log->module == "discuss") { + if ($log->module == "forum") { $post = NULL; - if ($log->action == "add post") { + if ($log->action == "add post" or $log->action == "add discussion") { $post = get_record_sql("SELECT p.*, d.forum, u.firstname, u.lastname, u.email, u.picture, u.id as userid - FROM discuss d, discuss_posts p, user u - WHERE p.id = '$log->info' AND d.id = p.discuss AND p.user = u.id"); + FROM forum_discussions d, forum_posts p, user u + WHERE p.id = '$log->info' AND d.id = p.discussion AND p.user = u.id"); } else if ($log->action == "add") { $post = get_record_sql("SELECT p.*, d.forum, u.firstname, u.lastname, u.email, u.picture, u.id as userid - FROM discuss d, discuss_posts p, user u + FROM forum_discussions d, forum_posts p, user u WHERE d.id = '$log->info' AND d.firstpost = p.id AND p.user = u.id"); } @@ -356,7 +352,7 @@ function print_recent_activity($course) { $content = true; } echo "

$post->firstname $post->lastname:
"; - echo "\"wwwroot/mod/discuss/$log->url\">"; + echo "\"wwwroot/mod/forum/$log->url\">"; if ($log->action == "add") { echo "$post->subject"; } else { diff --git a/course/mod.php b/course/mod.php index e05a06e1600..c8db9c45d4e 100644 --- a/course/mod.php +++ b/course/mod.php @@ -97,20 +97,29 @@ error("This course doesn't exist"); } - if (! $module = get_record("modules", "id", $cm->module)) { - error("This module doesn't exist"); - } - - if (! $instance = get_record($module->name, "id", $cm->instance)) { - error("The required instance of this module doesn't exist"); - } - require_login($course->id); if (!isteacher($course->id)) { error("You can't modify this course!"); } + if (! $module = get_record("modules", "id", $cm->module)) { + error("This module doesn't exist"); + } + + if (! $instance = get_record($module->name, "id", $cm->instance)) { + // Delete this module from the course right away + if (! delete_course_module($cm->id)) { + notify("Could not delete the $module->name (coursemodule)"); + } + if (! delete_mod_from_section($cm->id, $cm->section)) { + notify("Could not delete the $module->name from that section"); + } + error("The required instance of this module didn't exist. Module deleted.", + "$CFG->wwwroot/course/view.php?id=$course->id"); + } + + $form->coursemodule = $cm->id; $form->section = $cm->section; $form->course = $cm->course; diff --git a/course/social.php b/course/social.php index 8efa334f790..56584da80d8 100644 --- a/course/social.php +++ b/course/social.php @@ -46,10 +46,10 @@ echo ""; echo "
"; - // Print a form to search discussions + // Print a form to search forums print_simple_box("Search Discussions", $align="CENTER", $width="100%", $color="$THEME->cellheading"); echo "

"; - print_discussion_search_form($course); + print_forum_search_form($course); echo "
"; // Print Admin links for teachers and admin. @@ -81,7 +81,7 @@ error("Could not find or create a social forum here"); } - $SESSION->fromdiscuss = "$CFG->wwwdir/course/view.php?id=$course->id"; + $SESSION->fromdiscussion = "$CFG->wwwdir/course/view.php?id=$course->id"; if (is_subscribed($USER->id, $social->id)) { $subtext = "Unsubscribe"; } else { diff --git a/course/topics.php b/course/topics.php index 4a5b1741930..e271a6493ce 100644 --- a/course/topics.php +++ b/course/topics.php @@ -70,10 +70,10 @@ print_simple_box("Activities", $align="CENTER", $width="100%", $color="$THEME->cellheading"); print_side_block("", $moddata, "", $modicon); - // Print a form to search discussions + // Print a form to search forums print_simple_box("Search Discussions", $align="CENTER", $width="100%", $color="$THEME->cellheading"); echo "
"; - print_discussion_search_form($course); + print_forum_search_form($course); echo "
"; // Admin links and controls diff --git a/course/view.php b/course/view.php index 3e709553122..cfc202196c5 100644 --- a/course/view.php +++ b/course/view.php @@ -54,10 +54,6 @@ get_all_mods($course->id, $mods, $modtype); - if (isset($modtype["forum"]) and isset($modtype["discuss"])) { // Only need to display one - unset($modtype["discuss"]); - } - switch ($course->format) { case "weeks": include("weeks.php"); diff --git a/course/weeks.php b/course/weeks.php index b73bcd5d4cf..2692c03c052 100644 --- a/course/weeks.php +++ b/course/weeks.php @@ -52,10 +52,10 @@ print_simple_box("Activities", $align="CENTER", $width="100%", $color="$THEME->cellheading"); print_side_block("", $moddata, "", $modicon); - // Print a form to search discussions + // Print a form to search forums print_simple_box("Search Discussions", $align="CENTER", $width="100%", $color="$THEME->cellheading"); echo "
"; - print_discussion_search_form($course); + print_forum_search_form($course); echo "
"; // Admin links and controls diff --git a/doc/install.html b/doc/install.html index e7e5b002df6..2729865505f 100755 --- a/doc/install.html +++ b/doc/install.html @@ -28,7 +28,7 @@ 4.0.6 or better), including the
GD library for manipulating images.
  • a working database server (MySQL, PostgreSQL, - MSSQL, Oracle, Interbase, Foxpro, Access, ADO, Sybase, DB2 or ODBC)
  • + MSSQL, Oracle, Interbase, Foxpro, Access, ADO, Sybase, DB2 or ODBC).

    The quickest way to satisfy these requirements on Windows platforms is to download PHPTriad or FoxServ, diff --git a/index.php b/index.php index 7be9cf89389..abf8edf32fb 100644 --- a/index.php +++ b/index.php @@ -76,7 +76,7 @@ } if (isset($USER->id)) { - $SESSION->fromdiscuss = "$CFG->wwwroot"; + $SESSION->fromdiscussion = "$CFG->wwwroot"; if (is_subscribed($USER->id, $newsforum->id)) { $subtext = get_string("unsubscribe", "forum"); } else { diff --git a/lib/moodlelib.php b/lib/moodlelib.php index def4c7c1b86..46c9bf3b29b 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -458,10 +458,10 @@ function execute_sql($command) { $result = $db->Execute("$command"); if ($result) { - echo "

    SUCCESS: $command

    "; + echo "

    SUCCESS

    "; return true; } else { - echo "

    ERROR: $command

    "; + echo "

    ERROR

    "; return false; } } @@ -1351,7 +1351,7 @@ function add_to_log($course, $module, $action, $url="", $info="") { // any particular student has been doing. // // course = the course id -// module = discuss, journal, reading, course, user etc +// module = forum, journal, reading, course, user etc // action = view, edit, post (often but not always the same as the file.php) // url = the file and parameters used to see the results of the action // info = additional description information diff --git a/mod/assignment/module.php b/mod/assignment/version.php similarity index 100% rename from mod/assignment/module.php rename to mod/assignment/version.php diff --git a/mod/choice/module.php b/mod/choice/version.php similarity index 100% rename from mod/choice/module.php rename to mod/choice/version.php diff --git a/mod/forum/db/mysql.sql b/mod/forum/db/mysql.sql index cb4e2b2b9da..8432de56e47 100644 --- a/mod/forum/db/mysql.sql +++ b/mod/forum/db/mysql.sql @@ -13,7 +13,56 @@ CREATE TABLE forum ( timemodified int(10) unsigned NOT NULL default '0', PRIMARY KEY (id), UNIQUE KEY id (id) -) TYPE=MyISAM COMMENT='Discussion Forums'; +) COMMENT='Forums contain and structure discussion'; +# -------------------------------------------------------- + +# +# Table structure for table `forum_discussions` +# + +CREATE TABLE forum_discussions ( + id int(10) unsigned NOT NULL auto_increment, + course int(10) unsigned NOT NULL default '0', + forum int(10) unsigned NOT NULL default '0', + name varchar(255) NOT NULL default '', + firstpost int(10) unsigned NOT NULL default '0', + assessed tinyint(1) NOT NULL default '1', + timemodified int(10) unsigned NOT NULL default '0', + PRIMARY KEY (id) +) COMMENT='Forums are composed of discussions'; +# -------------------------------------------------------- + +# +# Table structure for table `forum_posts` +# + +CREATE TABLE forum_posts ( + id int(10) unsigned NOT NULL auto_increment, + discuss int(10) unsigned NOT NULL default '0', + parent int(10) unsigned NOT NULL default '0', + user int(10) unsigned NOT NULL default '0', + created int(10) unsigned NOT NULL default '0', + modified int(10) unsigned NOT NULL default '0', + mailed tinyint(1) unsigned NOT NULL default '0', + subject varchar(255) NOT NULL default '', + message text NOT NULL, + totalscore tinyint(4) NOT NULL default '0', + PRIMARY KEY (id) +) COMMENT='All posts are stored in this table'; +# -------------------------------------------------------- + +# +# Table structure for table `forum_ratings` +# + +CREATE TABLE forum_ratings ( + id int(10) unsigned NOT NULL auto_increment, + user int(10) unsigned NOT NULL default '0', + post int(10) unsigned NOT NULL default '0', + time int(10) unsigned NOT NULL default '0', + rating tinyint(4) NOT NULL default '0', + PRIMARY KEY (id) +) COMMENT='Contains user ratings for individual posts'; # -------------------------------------------------------- # @@ -26,13 +75,18 @@ CREATE TABLE forum_subscriptions ( forum int(10) unsigned NOT NULL default '0', PRIMARY KEY (id), UNIQUE KEY id (id) -) TYPE=MyISAM COMMENT='keeps track of who is subscribed to what forum'; - +) COMMENT='Keeps track of who is subscribed to what forum'; +# -------------------------------------------------------- # # Dumping data for table `log_display` # +INSERT INTO log_display VALUES ('forum', 'add', 'forum', 'name'); +INSERT INTO log_display VALUES ('forum', 'add discussion', 'forum_discuss', 'name'); +INSERT INTO log_display VALUES ('forum', 'add post', 'forum_posts', 'subject'); +INSERT INTO log_display VALUES ('forum', 'update post', 'forum_posts', 'subject'); +INSERT INTO log_display VALUES ('forum', 'view discussion', 'forum_discuss', 'name'); INSERT INTO log_display VALUES ('forum', 'view forum', 'forum', 'name'); INSERT INTO log_display VALUES ('forum', 'subscribe', 'forum', 'name'); INSERT INTO log_display VALUES ('forum', 'unsubscribe', 'forum', 'name'); diff --git a/mod/forum/discuss.php b/mod/forum/discuss.php new file mode 100644 index 00000000000..c50114d4afb --- /dev/null +++ b/mod/forum/discuss.php @@ -0,0 +1,73 @@ +course)) { + error("Course ID is incorrect - discussion is faulty"); + } + + if (! $forum = get_record("forum", "id", $discussion->forum)) { + notify("Bad forum ID stored in this discussion"); + } + + if ($course->category) { + require_login($course->id); + } + + add_to_log($course->id, "forum", "view discussion", "view.php?".$_SERVER["QUERY_STRING"], "$discussion->id"); + + unset($SESSION->fromdiscussion); + + forum_set_display_mode($mode); + + if (abs($USER->mode) == 1) { // If flat display then display the lot. + $parent = 0; + } + + if (!$parent) { + $parent = $discussion->firstpost; + $navtail = "$discussion->name"; + } + + if (! $post = get_forum_post_full($parent)) { + error("Discussion no longer exists", "$CFG->wwwroot/mod/forum/view.php?f=$forum->id"); + } + + if (!$navtail) { + $navtail = "id\">$discussion->name -> $post->subject"; + } + + $navmiddle = "id\">Forums -> id\">$forum->name"; + + if ($cm->id) { + $updatebutton = update_module_icon($cm->id, $course->id); + } else { + $updatebutton = ""; + } + + if ($course->category) { + print_header("$course->shortname: $discussion->name", "$course->fullname", + "id>$course->shortname -> + $navmiddle -> $navtail", "", "", true, $updatebutton); + } else { + print_header("$course->shortname: $discussion->name", "$course->fullname", + "$navmiddle -> $navtail", "", "", true, $updatebutton); + } + + print_discussion($course, $discussion, $post, $USER->mode); + + print_footer($course); + +?> diff --git a/mod/forum/icon.gif b/mod/forum/icon.gif index c84041d803b..9caa242e141 100755 Binary files a/mod/forum/icon.gif and b/mod/forum/icon.gif differ diff --git a/mod/forum/index.php b/mod/forum/index.php index 29b966a1698..2c5c11ebace 100644 --- a/mod/forum/index.php +++ b/mod/forum/index.php @@ -19,7 +19,7 @@ require_login($course->id); } - unset($SESSION->fromdiscuss); + unset($SESSION->fromdiscussion); add_to_log($course->id, "forum", "view forums", "index.php?id=$course->id", ""); @@ -42,7 +42,7 @@ if ($forums = get_records("forum", "course", $id, "name ASC")) { foreach ($forums as $forum) { switch ($forum->type) { - case "discussion": + case "single": case "general": case "eachuser": $contentforums[] = $forum; @@ -61,22 +61,24 @@ if ($generalforums) { foreach ($generalforums as $forum) { - $count = count_records("discuss", "forum", "$forum->id"); + $count = count_records("forum_discussions", "forum", "$forum->id"); if ($can_subscribe) { - if (is_subscribed($USER->id, $forum->id)) { - $subscribed = "YES"; + if (forum_is_forcesubscribed($forum->id)) { + $sublink = "YES"; } else { - $subscribed = "NO"; + if (forum_is_subscribed($USER->id, $forum->id)) { + $subscribed = "YES"; + } else { + $subscribed = "NO"; + } + $sublink = "id\">$subscribed"; } $table->data[] = array ("id\">$forum->name", - "$forum->intro", - "$count", - "id\">$subscribed"); + "$forum->intro", "$count", "$sublink"); } else { $table->data[] = array ("id\">$forum->name", - "$forum->intro", - "$count"); + "$forum->intro", "$count"); } } print_heading("General Forums"); @@ -86,22 +88,24 @@ if ($contentforums) { foreach ($contentforums as $forum) { - $count = count_records("discuss", "forum", "$forum->id"); + $count = count_records("forum_discussions", "forum", "$forum->id"); if ($can_subscribe) { - if (is_subscribed($USER->id, $forum->id)) { - $subscribed = "YES"; + if (forum_is_forcesubscribed($forum->id)) { + $sublink = "YES"; } else { - $subscribed = "NO"; + if (forum_is_subscribed($USER->id, $forum->id)) { + $subscribed = "YES"; + } else { + $subscribed = "NO"; + } + $sublink = "id\">$subscribed"; } $table->data[] = array ("id\">$forum->name", - "$forum->intro", - "$count", - "id\">$subscribed"); + "$forum->intro", "$count", "$sublink"); } else { $table->data[] = array ("id\">$forum->name", - "$forum->intro", - "$count"); + "$forum->intro", "$count"); } } print_heading("Course content"); @@ -109,7 +113,7 @@ } echo "
    "; - print_discussion_search_form($course, $search); + print_forum_search_form($course, $search); echo "
    "; print_footer($course); diff --git a/mod/forum/lib.php b/mod/forum/lib.php index a7c716c33e2..0b5f4313757 100644 --- a/mod/forum/lib.php +++ b/mod/forum/lib.php @@ -1,28 +1,627 @@ dirroot/mod/discuss/lib.php"); +$FORUM_DEFAULT_DISPLAY_MODE = 3; -// These are non-special forum types ie the ones that aren't automatically created -$FORUM_TYPE = array ("general" => "General Forum", - "eachuser" => "Each $student posts a topic"); +$FORUM_DISCUSS_MODES = array ( "1" => "Display replies flat, with oldest first", + "-1" => "Display replies flat, wth newest first", + "2" => "Display replies in threaded form", + "3" => "Display replies in nested form"); -function is_subscribed($user, $forum) { - global $db; +$FORUM_TYPE = array ("general" => "General forum", + "eachuser" => "Each $student posts a topic", + "single" => "A single simple discussion"); - return record_exists_sql("SELECT * FROM forum_subscriptions WHERE user='$user' AND forum='$forum'"); +$FORUM_POST_RATINGS = array ("3" => "Outstanding", + "2" => "Satisfactory", + "1" => "Not satisfactory"); + +$FORUM_LONG_POST = 600; + + +/// FUNCTIONS /////////////////////////////////////////////////////////// + +// How to set up special 1-per-course forums + +function get_course_news_forum($courseid) { + if ($forum = get_record_sql("SELECT * from forum WHERE course = '$courseid' AND type = 'news'")) { + return $forum; + } else { + // Doesn't exist, so create one now. + $forum->course = $courseid; + $forum->type = "news"; + $forum->name = "News"; + $forum->intro= "General news about this course"; + $forum->open = 0; + $forum->assessed = 0; + $forum->forcesubscribe = 1; + $forum->timemodified = time(); + $forum->id = insert_record("forum", $forum); + return get_record_sql("SELECT * from forum WHERE id = '$forum->id'"); + } } -function forum_subscribe($user, $forum) { - global $db; - return $db->Execute("INSERT INTO forum_subscriptions SET user = '$user', forum = '$forum'"); +function get_course_social_forum($courseid) { + if ($forum = get_record_sql("SELECT * from forum WHERE course = '$courseid' AND type = 'social'")) { + return $forum; + } else { + // Doesn't exist, so create one now. + $forum->course = $courseid; + $forum->type = "social"; + $forum->name = "Social"; + $forum->intro= "A forum for general socialising. Talk about anything you like!"; + $forum->open = 1; + $forum->assessed = 0; + $forum->forcesubscribe = 0; + $forum->timemodified = time(); + $forum->id = insert_record("forum", $forum); + return get_record_sql("SELECT * from forum WHERE id = '$forum->id'"); + } } -function forum_unsubscribe($user, $forum) { + +function get_course_teacher_forum($courseid) { + if ($forum = get_record_sql("SELECT * from forum WHERE course = '$courseid' AND type = 'teacher'")) { + return $forum; + } else { + // Doesn't exist, so create one now. + $forum->course = $courseid; + $forum->type = "teacher"; + $forum->name = "Teacher Forum"; + $forum->intro= "For teacher-only notes and discussion"; + $forum->open = 0; + $forum->assessed = 0; + $forum->forcesubscribe = 0; + $forum->timemodified = time(); + $forum->id = insert_record("forum", $forum); + return get_record_sql("SELECT * from forum WHERE id = '$forum->id'"); + } +} + + +function make_mail_post(&$post, $user, $touser, $course, $ownpost=false, $reply=false, $link=false, $rate=false, $footer="") { +// Given the data about a posting, builds up the HTML to display it and +// returns the HTML in a string. This is designed for sending via HTML email. + + global $THEME, $CFG; + + $output = ""; + + if ($post->parent) { + $output .= "
    "; + $output .= ""; + } else { + $output .= "
    "; + $output .= ""; + } + + $output .= ""; + + if ($post->parent) { + $output .= ""; + $output .= "
    body\" WIDTH=35 VALIGN=TOP>"; + $output .= print_user_picture($user->id, $course->id, $user->picture, false, true); + $output .= "cellheading\">"; + } else { + $output .= "cellheading2\">"; + } + $output .= "

    "; + $output .= "$post->subject
    "; + $output .= "by wwwroot/user/view.php?id=$user->id&course=$course->id\">$user->firstname $user->lastname"; + $output .= " on ".userdate($post->created, "", $touser->timezone); + $output .= "

    body\" WIDTH=10>"; + $output .= " "; + $output .= "\n"; + + $output .= text_to_html($post->message); + + $output .= "

    "; + + $age = time() - $post->created; + if ($ownpost) { + $output .= "wwwroot/mod/forum/post.php?delete=$post->id\">Delete"; + if ($reply) { + $output .= "| wwwroot/mod/forum/post.php?reply=$post->id\">Reply"; + } + $output .= "  "; + } else { + if ($reply) { + $output .= "wwwroot/mod/forum/post.php?reply=$post->id\">Reply  "; + } + } + + $output .= "

    "; + + if ($link) { + if ($post->replies == 1) { + $replystring = "reply"; + } else { + $replystring = "replies"; + } + $output .= "wwwroot/mod/forum/discuss.php?d=$post->discussion\">Discuss this topic ($post->replies $replystring so far)  "; + } + $output .= "

    "; + if ($footer) { + $output .= "

    $footer

    "; + } + $output .= "
    \n"; + $output .= "
    \n\n"; + + return $output; +} + + +function print_post(&$post, $courseid, $ownpost=false, $reply=false, $link=false, $rate=false, $footer="") { + global $THEME, $USER, $CFG, $FORUM_LONG_POST; + + if ($post->parent) { + echo " + + + +
    "; + echo ""; + } else { + echo "
    \n
    "; + echo ""; + } + + echo ""; + + if ($post->parent) { + echo ""; + echo "
    body\" WIDTH=35 VALIGN=TOP>"; + print_user_picture($post->userid, $courseid, $post->picture); + echo "cellheading\">"; + } else { + echo "cellheading2\">"; + } + echo "

    "; + echo "$post->subject
    "; + echo "by wwwroot/user/view.php?id=$post->userid&course=$courseid\">$post->firstname $post->lastname"; + echo " on ".userdate($post->created); + echo "

    body\" WIDTH=10>"; + echo " "; + echo "\n"; + + if ($link && (strlen($post->message) > $FORUM_LONG_POST)) { + // Look for the first return between 50 and $FORUM_LONG_POST + $shortmessage = substr($post->message, 50, $FORUM_LONG_POST); + if ($pos = strpos($shortmessage, "\n")) { + $shortmessage = substr($post->message, 0, 50 + $pos); + } else { + $shortmessage = substr($post->message, 0, $FORUM_LONG_POST). "..."; + } + echo text_to_html($shortmessage); + $numwords = count_words($post->message); + echo "wwwroot/mod/forum/discuss.php?d=$post->discussion\">Read the rest of this topic ($numwords words)..."; + } else { + // Just print the whole thing + echo text_to_html($post->message); + } + + echo "

    "; + + $age = time() - $post->created; + if ($ownpost) { + if ($age < $CFG->maxeditingtime) { + echo "wwwroot/mod/forum/post.php?edit=$post->id\">Edit | "; + } + echo "wwwroot/mod/forum/post.php?delete=$post->id\">Delete"; + if ($reply) { + echo "| wwwroot/mod/forum/post.php?reply=$post->id\">Reply"; + } + echo "  "; + } else { + if ($reply) { + echo "wwwroot/mod/forum/post.php?reply=$post->id\">Reply  "; + } + } + + + echo "

    "; + if ($rate && $USER->id) { + if ($USER->id == $post->userid) { + print_ratings($post->id); + } else { + print_rating($post->id, $USER->id); + } + } + + if ($link) { + if ($post->replies == 1) { + $replystring = "reply"; + } else { + $replystring = "replies"; + } + echo "wwwroot/mod/forum/discuss.php?d=$post->discussion\">Discuss this topic ($post->replies $replystring so far)  "; + } + echo "

    "; + if ($footer) { + echo "

    $footer

    "; + } + echo "
    "; + echo "
    "; + echo "
    \n\n"; +} + + +function print_ratings($post) { + + global $CFG, $PHPSESSID; + + $notsatisfactory = 0; + $satisfactory = 0; + $outstanding = 0; + if ($ratings = get_records_sql("SELECT * from forum_ratings WHERE post='$post'")) { + foreach ($ratings as $rating) { + switch ($rating->rating) { + case 1: $notsatisfactory++; break; + case 2: $satisfactory++; break; + case 3: $outstanding++; break; + } + } + $summary = "$outstanding/$satisfactory/$notsatisfactory"; + + echo "Ratings: "; + link_to_popup_window ("/mod/forum/report.php?id=$post", "ratings", $summary, 400, 550); + + } else { + echo ""; + } +} + +function print_rating($post, $user) { + global $FORUM_POST_RATINGS; + + if ($rs = get_record_sql("SELECT rating from forum_ratings WHERE user='$user' AND post='$post'")) { + echo "You rated this: "; + if ($FORUM_POST_RATINGS[$rs->rating]) { + echo $FORUM_POST_RATINGS[$rs->rating]; + } else { + echo "Error"; + } + echo ""; + + } else { + choose_from_menu($FORUM_POST_RATINGS, $post, "", "Rate..."); + } +} + +function print_mode_form($discussion, $mode) { + GLOBAL $FORUM_DISCUSS_MODES; + + echo "

    "; + popup_form("discuss.php?d=$discussion&mode=", $FORUM_DISCUSS_MODES, "mode", $mode, ""); + echo "

    \n"; +} + +function print_forum_search_form($course, $search="") { + global $CFG; + + echo "
    "; + echo "
    wwwroot/mod/forum/search.php\">"; + echo "
    "; + echo ""; + echo "id\">"; + echo "
    "; + echo "
    "; +} + + +function count_discussion_replies($forum="0") { + if ($forum) { + $forumselect = " AND d.forum = '$forum'"; + } + return get_records_sql("SELECT p.discussion, (count(*)) as replies + FROM forum_posts p, forum_discussions d + WHERE p.parent > 0 AND p.discussion = d.id + GROUP BY p.discussion"); +} + + +function set_fromdiscussion() { + global $SESSION, $HTTP_REFERER; + + if (! $SESSION->fromdiscussion) { + $SESSION->fromdiscussion = $HTTP_REFERER; + } +} + + +function go_back_to($default) { + global $SESSION; + + if ($SESSION->fromdiscussion) { + $returnto = $SESSION->fromdiscussion; + unset($SESSION->fromdiscussion); + return $returnto; + } else { + return $default; + } +} + +function get_forum_post_full($postid) { + return get_record_sql("SELECT p.*, u.firstname, u.lastname, + u.email, u.picture, u.id as userid + FROM forum_posts p, user u + WHERE p.id = '$postid' AND p.user = u.id"); +} + + +function add_new_post_to_database($post) { + + $timenow = time(); + $post->created = $timenow; + $post->modified = $timenow; + $post->mailed = "0"; + + return insert_record("forum_posts", $post); +} + +function update_post_in_database($post) { global $db; - return $db->Execute("DELETE FROM forum_subscriptions WHERE user = '$user' AND forum = '$forum'"); + $timenow = time(); + + $rs = $db->Execute("UPDATE forum_posts + SET message='$post->message', subject='$post->subject', modified='$timenow' + WHERE id = '$post->id'"); + return $rs; +} + +function forum_add_discussion($discussion) { +// Given an object containing all the necessary data, +// create a new discussion and return the id + + GLOBAL $USER; + + $timenow = time(); + + // The first post is stored as a real post, and linked + // to from the discuss entry. + + $post->discussion = 0; + $post->parent = 0; + $post->user = $USER->id; + $post->created = $timenow; + $post->modified = $timenow; + $post->mailed = 0; + $post->subject = $discussion->name; + $post->message = $discussion->intro; + + if (! $post->id = insert_record("forum_posts", $post) ) { + return 0; + } + + // Now do the real module entry + + $discussion->firstpost = $post->id; + $discussion->timemodified = $timenow; + + if (! $discussion->id = insert_record("forum_discussions", $discussion) ) { + return 0; + } + + // Finally, set the pointer on the post. + if (! set_field("forum_posts", "discussion", $discussion->id, "id", $post->id)) { + return 0; + } + + return $discussion->id; +} + + +function forum_delete_discussion($discussion) { +// $discussion is a discussion record object + + $result = true; + + if ($posts = get_records("forum_posts", "discussion", $discussion->id)) { + foreach ($posts as $post) { + if (! delete_records("forum_ratings", "post", "$post->id")) { + $result = false; + } + } + } + + if (! delete_records("forum_posts", "discussion", "$discussion->id")) { + $result = false; + } + + if (! delete_records("forum_discussions", "id", "$discussion->id")) { + $result = false; + } + + return $result; +} + + + +function print_user_discussions($course, $user) { + global $CFG; + + $topics = get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture, u.id as userid + FROM forum_discussions d, forum_posts p, user u, forum f + WHERE d.course = '$course->id' AND p.discussion = d.id AND + p.parent = 0 AND p.user = u.id AND u.id = '$user->id' + AND d.forum = f.id AND f.type = 'eachuser' + ORDER BY p.created DESC"); + + if ($topics) { + echo "
    "; + print_heading("Discussion topics"); + $replies = count_discussion_replies(); + foreach ($topics as $topic) { + if ($replies[$topic->discussion]) { + $topic->replies = $replies[$topic->discussion]->replies; + } else { + $topic->replies = 0; + } + $ownpost = ($topic->userid == $USER->id); + print_post($topic, $course->id, $ownpost, $reply=0, $link=1, $assessed=false); + echo "
    \n"; + } + } +} + + +function forum_user_summary($course, $user, $mod, $forum) { + global $CFG; +} + + +function forum_user_outline($course, $user, $mod, $forum) { + +///XXX + global $CFG; + if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture + FROM forum_posts p, user u + WHERE p.discussion='$discussion->id' AND p.user = '$user->id' AND p.user = u.id + ORDER BY p.modified ASC")) { + + $result->info = count($posts)." posts"; + + $lastpost = array_pop($posts); + $result->time = $lastpost->modified; + return $result; + } + return NULL; +} + + +function forum_user_complete($course, $user, $mod, $forum) { + global $CFG; + +///XXX + if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture + FROM forum_posts p, user u + WHERE p.discussion='$discussion->id' AND p.user = '$user->id' AND p.user = u.id + ORDER BY p.modified")) { + + foreach ($posts as $post) { + if ($post->parent) { + $footer = "wwwroot/mod/forum/discuss.php?d=$post->discussion&parent=$post->parent\">Parent of this post"; + } else { + $footer = ""; + } + + print_post($post, $course->id, $ownpost=false, $reply=false, $link=false, $rate=false, $footer); + } + + } else { + echo "

    No posts

    "; + } + +} + +function forum_cron () { +// Function to be run periodically according to the moodle cron +// Finds all posts that have yet to be mailed out, and mails them + + global $CFG; + + echo "Processing posts...\n"; + + $cutofftime = time() - $CFG->maxeditingtime; + + if ($posts = get_records_sql("SELECT p.*, d.course FROM forum_posts p, forum_discussions d + WHERE p.mailed = '0' AND p.created < '$cutofftime' AND p.discussion = d.id")) { + + $timenow = time(); + + foreach ($posts as $post) { + + echo "Processing post $post->id\n"; + + if (! $userfrom = get_record("user", "id", "$post->user")) { + echo "Could not find user $post->user\n"; + continue; + } + + if (! $course = get_record("course", "id", "$post->course")) { + echo "Could not find course $post->course\n"; + continue; + } + + if (! $discussion = get_record("forum_discussions", "id", "$post->discussion")) { + echo "Could not find discussion $post->discussion\n"; + continue; + } + + if (! $forum = get_record("forum", "id", "$discussion->forum")) { + echo "Could not find forum $discussion->forum\n"; + continue; + } + + + if ($users = get_records_sql("SELECT u.* FROM user u, forum_subscriptions s + WHERE s.user = u.id AND s.forum = '$discussion->forum'")) { + + foreach ($users as $userto) { + $postsubject = "$course->shortname: $post->subject"; + $posttext = "$course->shortname -> Forums -> $forum->name -> $discussion->name\n"; + $posttext .= "---------------------------------------------------------------------\n"; + $posttext .= "$post->subject\n"; + $posttext .= "by $userfrom->firstname $userfrom->lastname, on ".userdate($post->created, "", $userto->timezone)."\n"; + $posttext .= "---------------------------------------------------------------------\n"; + $posttext .= strip_tags($post->message); + $posttext .= "\n\n"; + $posttext .= "---------------------------------------------------------------------\n"; + $posttext .= "This is a copy of a message posted on the $course->shortname website.\n"; + $posttext .= "To add your reply via the website, click on this link:\n"; + $posttext .= "$CFG->wwwroot/mod/forum/post.php?reply=$post->id"; + + if ($userto->mailformat == 1) { // HTML + $posthtml = "

    ". + "wwwroot/course/view.php?id=$course->id\">$course->shortname ->". + "wwwroot/mod/forum/index.php?id=$course->id\">Forums ->". + "wwwroot/mod/forum/view.php?f=$forum->id\">$forum->name ->". + "wwwroot/mod/forum/discuss.php?d=$discussion->id\">$discussion->name

    "; + $posthtml .= make_mail_post($post, $userfrom, $userto, $course, false, true, false, false); + } else { + $posthtml = ""; + } + + if (! email_to_user($userto, $userfrom, $postsubject, $posttext, $posthtml)) { + echo "Error: mod/forum/cron.php: Could not send out mail for id $post->id to user $userto->id ($userto->email)\n"; + } + } + } + + if (! set_field("forum_posts", "mailed", "1", "id", "$post->id")) { + echo "Could not update the mailed field for id $post->id\n"; + } + } + } + + return true; +} + + +function forum_forcesubscribe($forumid, $value=1) { + return set_field("forum", "forcesubscribe", $value, "id", $forumid); +} + +function forum_is_forcesubscribed($forumid) { + return get_field("forum", "forcesubscribe", "id", $forumid); +} + +function forum_is_subscribed($userid, $forumid) { + if (forum_is_forcesubscribed($forumid)) { + return true; + } + return record_exists_sql("SELECT * FROM forum_subscriptions WHERE user='$userid' AND forum='$forumid'"); +} + +function forum_subscribe($userid, $forumid) { + global $db; + + return $db->Execute("INSERT INTO forum_subscriptions SET user = '$userid', forum = '$forumid'"); +} + +function forum_unsubscribe($userid, $forumid) { + global $db; + + return $db->Execute("DELETE FROM forum_subscriptions WHERE user = '$userid' AND forum = '$forumid'"); } @@ -57,81 +656,13 @@ function get_all_topics($forum="0", $forum_sort="DESC", $user=0) { $userselect = ""; } return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture, u.id as userid - FROM discuss d, discuss_posts p, user u - WHERE d.forum = '$forum' AND p.discuss = d.id AND + FROM forum_discussions d, forum_posts p, user u + WHERE d.forum = '$forum' AND p.discussion = d.id AND p.parent= 0 AND p.user = u.id $userselect ORDER BY p.created $forum_sort"); } -function get_course_news_forum($courseid) { - if ($forum = get_record_sql("SELECT * from forum WHERE course = '$courseid' AND type = 'news'")) { - return $forum; - } else { - // Doesn't exist, so create one now. - $forum->course = $courseid; - $forum->type = "news"; - $forum->name = "News"; - $forum->intro= "General news about this course"; - $forum->open = 0; - $forum->assessed = 0; - $forum->timemodified = time(); - $forum->id = insert_record("forum", $forum); - return get_record_sql("SELECT * from forum WHERE id = '$forum->id'"); - } -} - -function get_course_social_forum($courseid) { - if ($forum = get_record_sql("SELECT * from forum WHERE course = '$courseid' AND type = 'social'")) { - return $forum; - } else { - // Doesn't exist, so create one now. - $forum->course = $courseid; - $forum->type = "social"; - $forum->name = "Social"; - $forum->intro= "A forum to socialise and talk about anything you like"; - $forum->open = 1; - $forum->assessed = 0; - $forum->timemodified = time(); - $forum->id = insert_record("forum", $forum); - return get_record_sql("SELECT * from forum WHERE id = '$forum->id'"); - } -} - -function get_course_discussion_forum($courseid) { - if ($forum = get_record_sql("SELECT * from forum WHERE course = '$courseid' AND type = 'discussion'")) { - return $forum; - } else { - // Doesn't exist, so create one now. - $forum->course = $courseid; - $forum->type = "discussion"; - $forum->name = "Course Discussion"; - $forum->intro= "General discussions about course topics"; - $forum->open = 0; - $forum->assessed = 1; - $forum->timemodified = time(); - $forum->id = insert_record("forum", $forum); - return get_record_sql("SELECT * from forum WHERE id = '$forum->id'"); - } -} - -function get_course_teacher_forum($courseid) { - if ($forum = get_record_sql("SELECT * from forum WHERE course = '$courseid' AND type = 'teacher'")) { - return $forum; - } else { - // Doesn't exist, so create one now. - $forum->course = $courseid; - $forum->type = "teacher"; - $forum->name = "Teacher Forum"; - $forum->intro= "For teacher-only notes and discussion"; - $forum->open = 0; - $forum->assessed = 0; - $forum->timemodified = time(); - $forum->id = insert_record("forum", $forum); - return get_record_sql("SELECT * from forum WHERE id = '$forum->id'"); - } -} - function print_forum_latest_topics($forum_id=0, $forum_numtopics=5, $forum_style="plain", $forum_sort="DESC") { global $CFG, $USER; @@ -159,8 +690,8 @@ function print_forum_latest_topics($forum_id=0, $forum_numtopics=5, $forum_style if (user_can_post_discussion($forum)) { echo "

    "; - echo "wwwroot/mod/discuss/post.php?forum=$forum->id\">Add a new topic..."; - echo "

    "; + echo "wwwroot/mod/forum/post.php?forum=$forum->id\">Add a new topic..."; + echo "

    \n"; } if (! $topics = get_all_topics($forum->id, $forum_sort) ) { @@ -179,8 +710,8 @@ function print_forum_latest_topics($forum_id=0, $forum_numtopics=5, $forum_style echo "

    wwwroot/mod/forum/view.php?f=$forum->id\">Older topics ...

    "; break; } - if ($replies[$topic->discuss]) { - $topic->replies = $replies[$topic->discuss]->replies; + if ($replies[$topic->discussion]) { + $topic->replies = $replies[$topic->discussion]->replies; } else { $topic->replies = 0; } @@ -189,7 +720,7 @@ function print_forum_latest_topics($forum_id=0, $forum_numtopics=5, $forum_style case "minimal": echo "

    ".userdate($topic->modified, "%e %B, %H:%M").""; echo "
    $topic->subject "; - echo "wwwroot/mod/discuss/view.php?d=$topic->discuss\">more..."; + echo "wwwroot/mod/forum/discuss.php?d=$topic->discussion\">more..."; echo "

    \n"; break; default: @@ -201,5 +732,133 @@ function print_forum_latest_topics($forum_id=0, $forum_numtopics=5, $forum_style } } +function print_discussion($course, $discussion, $post, $mode) { + + global $USER; + + $ownpost = ($USER->id == $post->user); + + print_post($post, $course->id, $ownpost, $reply=true, $link=false, $rate=false); + + print_mode_form($discussion->id, $mode); + + if ($discussion->assessed && $USER->id) { + echo "
    "; + echo "id\">"; + } + + switch ($mode) { + case 1 : // Flat ascending + case -1 : // Flat descending + default: + echo "
      "; + print_posts_flat($post->discussion, $course->id, $mode, $discussion->assessed); + echo "
    "; + break; + + case 2 : // Threaded + print_posts_threaded($post->id, $course->id, 0, $discussion->assessed); + break; + + case 3 : // Nested + print_posts_nested($post->id, $course->id, $discussion->assessed); + break; + } + + if ($discussion->assessed && $USER->id) { + echo "

    "; + echo "
    "; + } +} + +function print_posts_flat($discussion, $course, $direction, $assessed) { + global $USER; + + $reply = true; + $link = false; + + if ($direction < 0) { + $sort = "ORDER BY created DESC"; + } else { + $sort = "ORDER BY created ASC"; + } + + if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture + FROM forum_posts p, user u + WHERE p.discussion = $discussion AND p.parent > 0 AND p.user = u.id $sort")) { + + foreach ($posts as $post) { + $ownpost = ($USER->id == $post->user); + print_post($post, $course, $ownpost, $reply, $link, $assessed); + } + } else { + return; + } +} + +function print_posts_threaded($parent, $course, $depth, $assessed) { + global $USER; + + $reply = true; + $link = false; + + if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture + FROM forum_posts p, user u + WHERE p.parent = '$parent' AND p.user = u.id")) { + + foreach ($posts as $post) { + + echo "
      "; + if ($depth > 0) { + $ownpost = ($USER->id == $post->user); + print_post($post, $course, $ownpost, $reply, $link, $assessed); // link=true? + echo "
      "; + } else { + echo "
    • discussion&parent=$post->id\">$post->subject by $post->firstname $post->lastname, ".userdate($post->created)."

      "; + } + + print_posts_threaded($post->id, $course, $depth-1, $assessed); + echo "
    \n"; + } + } else { + return; + } +} + +function print_posts_nested($parent, $course, $assessed) { + global $USER; + + $reply = true; + $link = false; + + if ($posts = get_records_sql("SELECT p.*, u.id as userid, u.firstname, u.lastname, u.email, u.picture + FROM forum_posts p, user u + WHERE p.parent = $parent AND p.user = u.id + ORDER BY p.created ASC ")) { + + foreach ($posts as $post) { + + $ownpost = ($USER->id == $post->user); + + echo "
      "; + print_post($post, $course, $ownpost, $reply, $link, $assessed); + echo "
      "; + print_posts_nested($post->id, $course, $assessed); + echo "
    \n"; + } + } else { + return; + } +} + +function forum_set_display_mode($mode=0) { + global $USER; + + if ($mode) { + $USER->mode = $mode; + } else if (!$USER->mode) { + $USER->mode = $FORUM_DEFAULT_DISPLAY_MODE; + } +} ?> diff --git a/mod/forum/mod.html b/mod/forum/mod.html index f402f785164..0ea1e13599a 100644 --- a/mod/forum/mod.html +++ b/mod/forum/mod.html @@ -40,6 +40,13 @@ assessed == 0) echo "CHECKED"; ?> >No

    Force everyone to be subscribed to this forum?:

    + forcesubscribe == 1) echo "CHECKED"; ?> >Yes + forcesubscribe == 0) echo "CHECKED"; ?> >No +
    diff --git a/mod/forum/mod.php b/mod/forum/mod.php index 6e9f6ba6861..66399e82706 100644 --- a/mod/forum/mod.php +++ b/mod/forum/mod.php @@ -15,9 +15,29 @@ function add_instance($forum) { // will create a new instance and return the id number // of the new instance. + global $CFG; + $forum->timemodified = time(); - return insert_record("forum", $forum); + if (! $forum->id = insert_record("forum", $forum)) { + return false; + } + + if ($forum->type == "single") { // Create related discussion. + include_once("$CFG->dirroot/mod/forum/lib.php"); + + $discussion->course = $forum->course; + $discussion->forum = $forum->id; + $discussion->name = $forum->name; + $discussion->intro = $forum->intro; + $discussion->assessed = $forum->assessed; + + if (! forum_add_discussion($discussion)) { + error("Could not add the discussion for this forum"); + } + } + + return $forum->id; } @@ -29,6 +49,34 @@ function update_instance($forum) { $forum->timemodified = time(); $forum->id = $forum->instance; + if ($forum->type == "single") { // Update related discussion and post. + if (! $discussion = get_record("forum_discussions", "forum", $forum->id)) { + if ($discussions = get_records("forum_discussions", "forum", $forum->id, "timemodified ASC")) { + notify("Warning! There is more than one discussion in this forum - using the most recent"); + $discussion = array_pop($discussions); + } else { + error("Could not find the discussion in this forum"); + } + } + if (! $post = get_record("forum_posts", "id", $discussion->firstpost)) { + error("Could not find the first post in this forum discussion"); + } + + $post->subject = $forum->name; + $post->message = $forum->intro; + $post->modified = $forum->timemodified; + + if (! update_record("forum_posts", $post)) { + error("Could not update the first post"); + } + + $discussion->name = $forum->name; + + if (! update_record("forum_discussions", $discussion)) { + error("Could not update the discussion"); + } + } + return update_record("forum", $forum); } @@ -40,7 +88,7 @@ function delete_instance($id) { global $CFG; - include("$CFG->dirroot/mod/discuss/lib.php"); + include_once("$CFG->dirroot/mod/forum/lib.php"); if (! $forum = get_record("forum", "id", "$id")) { return false; @@ -48,9 +96,9 @@ function delete_instance($id) { $result = true; - if ($discussions = get_records("discuss", "forum", $forum->id)) { - foreach ($discussions as $discuss) { - if (! delete_discussion($discuss)) { + if ($discussions = get_records("forum_discussions", "forum", $forum->id)) { + foreach ($discussions as $discussion) { + if (! forum_delete_discussion($discussion)) { $result = false; } } diff --git a/mod/forum/module.php b/mod/forum/module.php deleted file mode 100644 index 966b3306736..00000000000 --- a/mod/forum/module.php +++ /dev/null @@ -1,22 +0,0 @@ -fullname = "Forum"; -$module->version = 20020801; -$module->cron = 0; -$module->search = ""; - -function forum_upgrade($oldversion) { -// This function does anything necessary to upgrade -// older versions to match current functionality - - return true; -} - - -?> - diff --git a/mod/forum/post.html b/mod/forum/post.html new file mode 100644 index 00000000000..3e59b6e2f86 --- /dev/null +++ b/mod/forum/post.html @@ -0,0 +1,33 @@ +
    + + + + + + + + + + + +

    :

    + + +   +

    :

    + +

    + + + + + + + +
    + + +
    +
    +
    +
    diff --git a/mod/forum/post.php b/mod/forum/post.php new file mode 100644 index 00000000000..c1ed084fb5d --- /dev/null +++ b/mod/forum/post.php @@ -0,0 +1,277 @@ +subject = strip_tags($post->subject); // Strip all tags + $post->message = cleantext($post->message); // Clean up any bad tags + + require_login(); + + if ($post->edit) { // Updating a post + $post->id = $post->edit; + if (update_post_in_database($post) ) { + add_to_log($post->course, "forum", "update post", "discuss.php?d=$post->discussion&parent=$post->id", "$post->id"); + redirect(go_back_to("discuss.php?d=$post->discussion"), "Your post was updated", 1); + } else { + error("Could not update your post due to an unknown error"); + } + } else if ($post->discussion) { // Adding a new post to an existing discussion + if ($post->id = add_new_post_to_database($post)) { + if ( ! forum_is_subscribed($USER->id, $post->forum) ) { + forum_subscribe($USER->id, $post->forum); + } + + add_to_log($post->course, "forum", "add post", "discuss.php?d=$post->discussion&parent=$post->id", "$post->id"); + redirect(go_back_to("discuss.php?d=$post->discussion"), + "Your post was successfully added.

    You have ".format_time($CFG->maxeditingtime)." to edit it if you want to make any changes.", 3); + } else { + error("Could not add the post due to an unknown error"); + } + } else { // Adding a new discussion + $discussion = $post; + $discussion->name = $post->subject; + $discussion->intro = $post->message; + if ($discussion->id = forum_add_discussion($discussion)) { + if ( ! forum_is_subscribed($USER->id, $post->forum) ) { + forum_subscribe($USER->id, $post->forum); + } + add_to_log($post->course, "forum", "add discussion", "discuss.php?d=$discussion->id", "$discussion->id"); + redirect(go_back_to("view.php?f=$post->forum"), + "Your post was successfully added.

    You have ".format_time($CFG->maxeditingtime)." to edit it if you want to make any changes.", 5); + } else { + error("Could not insert the new discussion."); + } + } + die; + } + + + + if (isset($forum)) { // User is starting a new discussion in a forum + + $SESSION->fromurl = $HTTP_REFERER; + + if (! $forum = get_record("forum", "id", $forum)) { + error("The forum number was incorrect ($forum)"); + } + if (! $course = get_record("course", "id", $forum->course)) { + error("The course number was incorrect ($forum)"); + } + + if (! user_can_post_discussion($forum)) { + error("Sorry, but you can not post a new discussion in this forum."); + } + + // Load up the $post variable. + + $post->course = $course->id; + $post->forum = $forum->id; + $post->discussion = 0; // ie discussion # not defined yet + $post->parent = 0; + $post->subject = ""; + $post->user = $USER->id; + $post->message = ""; + + set_fromdiscussion(); + + } else if (isset($reply)) { // User is writing a new reply + + if (! $parent = get_forum_post_full($reply)) { + error("Parent post ID was incorrect ($reply)"); + } + if (! $discussion = get_record("forum_discussions", "id", $parent->discussion)) { + error("This post is not part of a discussion! ($reply)"); + } + if (! $forum = get_record("forum", "id", $discussion->forum)) { + error("The forum number was incorrect ($discussion->forum)"); + } + if (! $course = get_record("course", "id", $discussion->course)) { + error("The course number was incorrect ($discussion->course)"); + } + // Load up the $post variable. + + $post->course = $course->id; + $post->forum = $forum->id; + $post->discussion = $parent->discussion; + $post->parent = $parent->id; + $post->subject = $parent->subject; + $post->user = $USER->id; + $post->message = ""; + + if (!(substr($post->subject, 0, 3) == "Re:")) { + $post->subject = "Re: ".$post->subject; + } + + set_fromdiscussion(); + + } else if (isset($edit)) { // User is editing their own post + + if (! $post = get_forum_post_full($edit)) { + error("Post ID was incorrect"); + } + if ($post->user <> $USER->id) { + error("You can't edit other people's posts!"); + } + if ((time() - $post->created) > $CFG->maxeditingtime) { + error("Sorry, but the maximum time for editing this post (".format_time($CFG->maxeditingtime).") has passed!"); + } + if ($post->parent) { + if (! $parent = get_forum_post_full($post->parent)) { + error("Parent post ID was incorrect ($post->parent)"); + } + } + if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) { + error("This post is not part of a discussion! ($reply)"); + } + if (! $forum = get_record("forum", "id", $discussion->forum)) { + error("The forum number was incorrect ($discussion->forum)"); + } + if (! $course = get_record("course", "id", $discussion->course)) { + error("The course number was incorrect ($discussion->course)"); + } + + // Load up the $post variable. + + $post->edit = $edit; + + $post->course = $course->id; + $post->forum = $forum->id; + + set_fromdiscussion(); + + + } else if (isset($delete)) { // User is deleting a post + + if (! $post = get_forum_post_full($delete)) { + error("Post ID was incorrect"); + } + if ($post->user <> $USER->id) { + error("You can't delete other people's posts!"); + } + if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) { + error("This post is not part of a discussion!"); + } + + if (isset($confirm)) { // User has confirmed the delete + + if ($post->totalscore) { + notice("Sorry, that cannot be deleted as people have already rated it", + go_back_to("discuss.php?d=$post->discussion")); + + } else if (record_exists("forum_posts", "parent", $delete)) { + error("Sorry, that cannot be deleted as people have + already responded to it", + go_back_to("discuss.php?id=$post->discussion")); + + } else { + if (! $post->parent) { // post is a discussion topic as well, so delete discussion + forum_delete_discussion($discussion); + + add_to_log($discussion->course, "forum", "delete discussion", "view.php?id=$discussion->forum", "$post->id"); + redirect("view.php?f=$discussion->forum", + "Your discussion topic was deleted", 1); + + } else if (delete_records("forum_posts", "id", $post->id)) { + + add_to_log($discussion->course, "forum", "delete post", "discuss.php?d=$post->discussion", "$post->id"); + redirect(go_back_to("discuss.php?d=$post->discussion"), + "Your post was deleted", 1); + } else { + error("An error occurred while deleting record $post->id"); + } + } + + + } else { // User just asked to delete something + + set_fromdiscussion(); + + print_header(); + notice_yesno("Are you sure you want to delete this post?", + "post.php?delete=$delete&confirm=$delete", + $HTTP_REFERER); + + echo "


    "; + print_post($post, 0, $ownpost=false, $reply=false, $link=false); + + } + + die; + + + } else { + error("No operation specified"); + + } + + + // To get here they need to edit a post, and the $post + // variable will be loaded with all the particulars, + // so bring up the form. + + // $course, $forum are defined. $discussion is for edit and reply only. + + require_login($course->id); + + if ($post->discussion) { + if (! $toppost = get_record_sql("SELECT * FROM forum_posts + WHERE discussion='$post->discussion' + AND parent = 0")) { + error("Could not find top parent of post $post->id"); + } + } else { + $toppost->subject = "New discussion topic"; + } + + if ($post->subject) { + $formstart = "form.message"; + } else { + $formstart = "form.subject"; + } + + if ($post->parent) { + $navtail = "id\">$toppost->subject -> Editing"; + } else { + $navtail = "$toppost->subject"; + } + + $navmiddle = "id\">Forums -> id\">$forum->name"; + + if ($course->category) { + print_header("$course->shortname: $discussion->name: $toppost->subject", "$course->fullname", + "id>$course->shortname -> + $navmiddle -> $navtail", "$forumstart"); + } else { + print_header("$course->shortname: $discussion->name: $toppost->subject", "$course->fullname", + "$navmiddle -> $navtail", ""); + + } + + echo "
    "; + if (isset($parent)) { + print_post($parent, $course->id, $ownpost=false, $reply=false, $link=false); + echo "

    Your reply:

    "; + } else { + echo "

    Your new discussion topic:

    "; + } + echo "
    "; + + print_simple_box_start("center", "", "$THEME->cellheading"); + require("post.html"); + print_simple_box_end(); + + print_footer($course); + + +?> diff --git a/mod/forum/rate.php b/mod/forum/rate.php new file mode 100644 index 00000000000..740c63c72a6 --- /dev/null +++ b/mod/forum/rate.php @@ -0,0 +1,51 @@ +id); + + if (isset($HTTP_POST_VARS)) { // form submitted + + foreach ($HTTP_POST_VARS as $post => $rating) { + if ($post == "id") { + continue; + } + if ($rating) { + if ($check = get_record_sql("SELECT COUNT(*) as count FROM forum_ratings + WHERE user='$USER->id' AND post='$post'")){ + if ($check->count == 0) { + $timenow = time(); + if (!$rs = $db->Execute("INSERT DELAYED INTO forum_ratings + SET user='$USER->id', post='$post', time='$timenow', rating='$rating'")){ + error("Could not insert a new rating ($post = $rating)"); + } + + } else { + error("You've rated this question before ($post)"); + } + } + } + } + redirect($HTTP_REFERER, "Ratings saved"); + + } else { + error("This page was not accessed correctly"); + } + +?> diff --git a/mod/forum/report.php b/mod/forum/report.php new file mode 100644 index 00000000000..755bc19010b --- /dev/null +++ b/mod/forum/report.php @@ -0,0 +1,61 @@ +discussion)) { + error("Forum ID was incorrect"); + } + + if ($USER->id <> $post->user) { + error("You can only look at results for posts you own"); + } + + if (!isset($sort)) { + $sort = "r.time"; + } + + print_header("(Ratings for) $post->subject"); + + if (!$ratings = get_records_sql("SELECT u.*, r.rating, r.time FROM forum_ratings r, user u + WHERE r.post='$post->id' AND r.user=u.id ORDER BY $sort")) { + echo "No ratings for this post: \"$post->subject\""; + die; + } else { + echo ""; + echo ""; + echo ""; + echo "cellcontent2\">"; + } else { + echo "cellcontent\">"; + } + echo "\n"; + } + echo "
     id&sort=u.firstname>Name"; + echo "id&sort=r.rating>Rating"; + echo "id&sort=r.time>Date"; + foreach ($ratings as $rating) { + if (isteacher($discussion->course, $rating->id)) { + echo "
    "; + if ($rating->picture) { + echo "wwwroot/user/pix.php/".$rating->picture."/f2.jpg\" ALIGN=left BORDER=0>"; + } else { + echo "wwwroot/user/pix.php/0/f2.jpg\" ALIGN=left BORDER=0>"; + } + echo "

    $rating->firstname $rating->lastname

    "; + echo "

    ".$FORUM_POST_RATINGS[$rating->rating]."

    "; + echo "

    ".userdate($rating->time)."

    "; + echo "
    "; + } + + close_window_button(); + +?> diff --git a/mod/forum/search.php b/mod/forum/search.php new file mode 100644 index 00000000000..cb60507a337 --- /dev/null +++ b/mod/forum/search.php @@ -0,0 +1,77 @@ +id); + + add_to_log($course->id, "course", "search", "search.php?id=$course->id&search=$search", "$search"); + + if ($search) { + print_header("$course->shortname: Search Results", "$course->fullname", + "id\">$course->shortname -> + id\">Forums -> + id\">Search -> \"$search\"", "search.search"); + } else { + print_header("$course->shortname: Search", "$course->fullname", + "id\">$course->shortname -> + id\">Forums -> Search", "search.search"); + } + + echo "
    "; + print_forum_search_form($course, $search); + echo "
    "; + + if ($search) { + + if (!isteacher($course->id)) { + $notteacherforum = "AND f.type <> 'teacher'"; + } else { + $notteacherforum = ""; + } + + $posts = get_records_sql("SELECT p.*,u.firstname,u.lastname,u.email,u.picture,u.id as userid + FROM forum_posts p, forum_discussions d, user u, forum f + WHERE message LIKE '%$search%' AND p.user = u.id + AND p.discussion = d.id AND d.course = '$course->id' + AND d.forum = f.id $notteacherforum + ORDER BY p.modified DESC LIMIT 0, 50 "); + + if (!$posts) { + print_heading("
    No posts found containing \"$search\""); + + } else { + foreach ($posts as $post) { + if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) { + error("Discussion ID was incorrect"); + } + if (! $forum = get_record("forum", "id", "$discussion->forum")) { + error("Could not find forum $discussion->forum"); + } + $post->subject = "id&forum=$forum->id\">$forum->name -> ". + "id\">$discussion->name -> ". + "discussion&parent=$post->id\">$post->subject"; + + $post->message = highlight("$search", $post->message); + + $fulllink = "

    discussion&parent=$post->id\">See this post in context

    "; + print_post($post, $course->id, false, false, false, false, $fulllink); + + echo "
    "; + } + } + } + + print_footer($course); + +?> + diff --git a/mod/forum/subscribe.php b/mod/forum/subscribe.php index 926213b8164..40f22fe1d6b 100644 --- a/mod/forum/subscribe.php +++ b/mod/forum/subscribe.php @@ -5,7 +5,8 @@ require("../../config.php"); require("lib.php"); - require_variable($id); // The forum to subscribe or unsubscribe to + require_variable($id); // The forum to subscribe or unsubscribe to + optional_variable($force); // Force everyone to be subscribed to this forum? if (isguest()) { error("Guests are not allowed to subscribe to posts.", $HTTP_REFERER); @@ -31,10 +32,23 @@ } } - $returnto = go_back_to("index.php?id=$course->id"); - if ( is_subscribed($USER->id, $forum->id) ) { + if ($force and isteacher($course->id)) { + if (forum_is_forcesubscribed($forum->id)) { + forum_forcesubscribe($forum->id, 0); + redirect($returnto, "Everyone can choose their own subscription to this forum", 1); + } else { + forum_forcesubscribe($forum->id, 1); + redirect($returnto, "Everyone is now subscribed to this forum", 1); + } + } + + if (forum_is_forcesubscribed($forum->id)) { + redirect($returnto, "Everyone is subscribed to this forum", 1); + } + + if ( forum_is_subscribed($USER->id, $forum->id) ) { if (forum_unsubscribe($USER->id, $forum->id) ) { add_to_log($course->id, "forum", "unsubscribe", "index.php?id=$course->id", "$forum->id"); redirect($returnto, "You are now NOT subscribed to receive '$forum->name' by email.", 1); diff --git a/mod/forum/subscribers.php b/mod/forum/subscribers.php index 2fc88866dba..955e956b53e 100644 --- a/mod/forum/subscribers.php +++ b/mod/forum/subscribers.php @@ -19,7 +19,7 @@ error("This page is for teachers only"); } - unset($SESSION->fromdiscuss); + unset($SESSION->fromdiscussion); add_to_log($course->id, "forum", "view subscribers", "subscribers.php?id=$forum->id", ""); @@ -45,7 +45,7 @@ echo ""; $count = 0; foreach ($users as $user) { - if (is_subscribed($user->id, $forum->id)) { + if (forum_is_subscribed($user->id, $forum->id)) { echo "
    "; print_user_picture($user->id, $course->id, $user->picture); echo ""; diff --git a/mod/forum/version.php b/mod/forum/version.php new file mode 100644 index 00000000000..1956511859d --- /dev/null +++ b/mod/forum/version.php @@ -0,0 +1,46 @@ +fullname = "Forum"; +$module->version = 2002073008; +$module->cron = 60; +$module->search = ""; + +function forum_upgrade($oldversion) { +// This function does anything necessary to upgrade +// older versions to match current functionality + + if ($oldversion < 2002073008) { + execute_sql("DELETE FROM modules WHERE name = 'discuss' "); + execute_sql("ALTER TABLE `discuss` RENAME `forum_discussions` "); + execute_sql("ALTER TABLE `discuss_posts` RENAME `forum_posts` "); + execute_sql("ALTER TABLE `discuss_ratings` RENAME `forum_ratings` "); + execute_sql("ALTER TABLE `forum` CHANGE `intro` `intro` TEXT NOT NULL "); + execute_sql("ALTER TABLE `forum` ADD `forcesubscribe` TINYINT(1) UNSIGNED DEFAULT '0' NOT NULL AFTER `assessed`"); + execute_sql("ALTER TABLE `forum` CHANGE `type` `type` ENUM( 'single', 'news', 'social', 'general', + 'eachuser', 'teacher' ) DEFAULT 'general' NOT NULL "); + execute_sql("ALTER TABLE `forum_posts` CHANGE `discuss` `discussion` INT( 10 ) UNSIGNED DEFAULT '0' NOT NULL "); + execute_sql("INSERT INTO log_display VALUES ('forum', 'add', 'forum', 'name') "); + execute_sql("INSERT INTO log_display VALUES ('forum', 'add discussion', 'forum_discussions', 'name') "); + execute_sql("INSERT INTO log_display VALUES ('forum', 'add post', 'forum_posts', 'subject') "); + execute_sql("INSERT INTO log_display VALUES ('forum', 'update post', 'forum_posts', 'subject') "); + execute_sql("INSERT INTO log_display VALUES ('forum', 'view discussion', 'forum_discussions', 'name') "); + execute_sql("DELETE FROM log_display WHERE module = 'discuss' "); + execute_sql("UPDATE log SET action = 'view discussion' WHERE module = 'discuss' AND action = 'view' "); + execute_sql("UPDATE log SET action = 'add discussion' WHERE module = 'discuss' AND action = 'add' "); + execute_sql("UPDATE log SET module = 'forum' WHERE module = 'discuss' "); + notify("Renamed all the old discuss tables (now part of forum) and created new forum_types"); + } + + return true; + +} + + + +?> + diff --git a/mod/forum/view.php b/mod/forum/view.php index 7b96547146b..c67b7874d63 100644 --- a/mod/forum/view.php +++ b/mod/forum/view.php @@ -5,6 +5,7 @@ optional_variable($id); // Course Module ID optional_variable($f); // Forum ID + optional_variable($mode); // Display mode (for single forum) if ($id) { @@ -50,29 +51,62 @@ } } - add_to_log($course->id, "forum", "view forum", "view.php?f=$forum->id", "$forum->id"); print_header("$course->shortname: $forum->name", "$course->fullname", "$navigation $forum->name", "", "", true, $buttontext); if ($USER) { - $SESSION->fromdiscuss = "$FULLME"; - if (is_subscribed($USER->id, $forum->id)) { - $subtext = "Unsubscribe me from this forum"; + $SESSION->fromdiscussion = "$FULLME"; + if (forum_is_forcesubscribed($forum->id)) { + $subtext = "Everyone is subscribed to this forum"; + if (isteacher($course->id)) { + echo "
    "; + echo "id&force=no\">$subtext"; + echo "
    "; + } else { + echo "
    $subtext
    "; + } + } else { - $subtext = "Subscribe me to this forum"; - } - echo ""; - if (isteacher($course->id)) { - echo ""; + $subtext = "Everyone can choose to be subscribed"; + if (isteacher($course->id)) { + echo "
    "; + echo "id&force=yes\">$subtext"; + echo "
    "; + $subtext = "id\">Show subscribers"; + echo "
    $subtext
    "; + } + if (forum_is_subscribed($USER->id, $forum->id)) { + $subtext = "Unsubscribe me"; + } else { + $subtext = "Subscribe me"; + } + $subtext = "id\">$subtext"; + echo "
    $subtext
    "; } } - print_simple_box(text_to_html($forum->intro), "CENTER"); switch ($forum->type) { + case "single": + if (! $discussion = get_record("forum_discussions", "forum", $forum->id)) { + if ($discussions = get_records("forum_discussions", "forum", $forum->id, "timemodified ASC")) { + notify("Warning! There is more than one discussion in this forum - using the most recent"); + $discussion = array_pop($discussions); + } else { + error("Could not find the discussion in this forum"); + } + } + if (! $post = get_forum_post_full($discussion->firstpost)) { + error("Could not find the first post in this forum"); + } + forum_set_display_mode($mode); + print_discussion($course, $discussion, $post, $USER->mode); + break; + case "eachuser": + print_simple_box(text_to_html($forum->intro), "CENTER"); echo "

    "; if (user_can_post_discussion($forum)) { echo "This forum allows one discussion topic to be posted per person."; @@ -80,14 +114,16 @@ echo " "; } echo "

    "; + print_forum_latest_topics($forum->id, 0); break; default: + print_simple_box(text_to_html($forum->intro), "CENTER"); echo "

     

    "; + print_forum_latest_topics($forum->id, 0); break; } - print_forum_latest_topics($forum->id, 0); print_footer($course); diff --git a/mod/journal/module.php b/mod/journal/version.php similarity index 100% rename from mod/journal/module.php rename to mod/journal/version.php diff --git a/mod/survey/module.php b/mod/survey/version.php similarity index 100% rename from mod/survey/module.php rename to mod/survey/version.php diff --git a/user/view.php b/user/view.php index 21704ed0d8c..1a810dee993 100644 --- a/user/view.php +++ b/user/view.php @@ -4,8 +4,8 @@ require("../config.php"); require("../lib/countries.php"); + require("../mod/forum/lib.php"); require("lib.php"); - require("../mod/discuss/lib.php"); require_variable($id); require_variable($course); diff --git a/version.php b/version.php index 3d820f01adc..69c86ff58e6 100644 --- a/version.php +++ b/version.php @@ -17,7 +17,7 @@ // If there's something it cannot do itself, it // will tell you what you need to do. -$version = 2002072801; +$version = 2002073100; function upgrade_moodle($oldversion=0) { @@ -29,13 +29,14 @@ function upgrade_moodle($oldversion=0) { `value` varchar(255) NOT NULL default '', PRIMARY KEY (`id`), UNIQUE KEY `name` (`name`) - ) TYPE=MyISAM COMMENT='Moodle configuration variables';"); + ) COMMENT='Moodle configuration variables';"); notify("Created a new table 'config' to hold configuration data"); } - if ($oldversion < 2002072801) { - + if ($oldversion < 2002073100) { + execute_sql("DELETE FROM `modules` WHERE `name` = 'chat' "); } + return true; }