From f93f848ab3846212d018fe47de883c598434bc22 Mon Sep 17 00:00:00 2001 From: martin Date: Thu, 20 Jun 2002 15:15:22 +0000 Subject: [PATCH] Mass change related to separating forum from discuss and making it a separate module (again). This frees up forums for some interesting developments. Note that database tables have moved around. --- course/social.php | 2 +- course/topics.php | 2 +- course/unenrol.php | 2 +- course/weeks.php | 2 +- index.php | 2 +- lib/moodlelib.php | 10 +++ mod/forum/db/mysql.sql | 38 ++++++++++ mod/forum/icon.gif | Bin 0 -> 130 bytes mod/forum/index.php | 67 +++++++++++++++++ mod/forum/lib.php | 159 ++++++++++++++++++++++++++++++++++++++++ mod/forum/mod.html | 54 ++++++++++++++ mod/forum/mod.php | 71 ++++++++++++++++++ mod/forum/module.php | 14 ++++ mod/forum/subscribe.php | 47 ++++++++++++ mod/forum/view.php | 60 +++++++++++++++ 15 files changed, 525 insertions(+), 5 deletions(-) create mode 100644 mod/forum/db/mysql.sql create mode 100755 mod/forum/icon.gif create mode 100644 mod/forum/index.php create mode 100644 mod/forum/lib.php create mode 100644 mod/forum/mod.html create mode 100644 mod/forum/mod.php create mode 100644 mod/forum/module.php create mode 100644 mod/forum/subscribe.php create mode 100644 mod/forum/view.php diff --git a/course/social.php b/course/social.php index 3db47cac84..7a9a72ffa7 100644 --- a/course/social.php +++ b/course/social.php @@ -2,7 +2,7 @@ // social.php - course format featuring social forum // included from view.php - include("../mod/discuss/lib.php"); + include("../mod/forum/lib.php"); include("../mod/reading/lib.php"); ?> diff --git a/course/topics.php b/course/topics.php index a8f641c1cc..8a499815a9 100644 --- a/course/topics.php +++ b/course/topics.php @@ -6,7 +6,7 @@ // courses that aren't so rigidly defined by time. // Included from "view.php" - include("../mod/discuss/lib.php"); + include("../mod/forum/lib.php"); if (! $rawweeks = get_records("course_weeks", "course", $course->id) ) { $week->course = $course->id; // Create a default week. diff --git a/course/unenrol.php b/course/unenrol.php index be0fd6b984..36f3102743 100644 --- a/course/unenrol.php +++ b/course/unenrol.php @@ -31,7 +31,7 @@ } // remove some other things - delete_records("discuss_subscriptions", "user", $user->id); + delete_records("forum_subscriptions", "user", $user->id); add_to_log($course->id, "course", "unenrol", "view.php?id=$course->id", "$user->id"); diff --git a/course/weeks.php b/course/weeks.php index 1581bfb682..891bf1a66d 100644 --- a/course/weeks.php +++ b/course/weeks.php @@ -2,7 +2,7 @@ // Display the whole course as "weeks" made of of modules // Included from "view.php" - include("../mod/discuss/lib.php"); + include("../mod/forum/lib.php"); if (! $rawweeks = get_records("course_weeks", "course", $course->id) ) { $week->course = $course->id; // Create a default week. diff --git a/index.php b/index.php index 85ed014516..6015ff6f4b 100644 --- a/index.php +++ b/index.php @@ -4,7 +4,7 @@ require("config.php"); include("course/lib.php"); include("mod/reading/lib.php"); - include("mod/discuss/lib.php"); + include("mod/forum/lib.php"); if (! $site = get_record("course", "category", 0)) { redirect("$CFG->wwwroot/admin/"); diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 366f192fd9..4918127d6e 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -702,6 +702,16 @@ function update_record($table, $dataobject) { } +function print_object($object) { +// Mostly just for debugging + + $array = (array)$object; + foreach ($array as $key => $item) { + echo "$key -> $item
"; + } +} + + /// USER DATABASE //////////////////////////////////////////////// diff --git a/mod/forum/db/mysql.sql b/mod/forum/db/mysql.sql new file mode 100644 index 0000000000..7506d3b2df --- /dev/null +++ b/mod/forum/db/mysql.sql @@ -0,0 +1,38 @@ +# +# Table structure for table `forum` +# + +CREATE TABLE forum ( + id int(10) unsigned NOT NULL auto_increment, + course int(10) unsigned NOT NULL default '0', + type enum('discussion','news','general','social') NOT NULL default 'general', + name varchar(255) NOT NULL default '', + intro tinytext NOT NULL, + open tinyint(1) unsigned NOT NULL default '0', + assessed tinyint(1) unsigned NOT NULL default '0', + timemodified int(10) unsigned NOT NULL default '0', + PRIMARY KEY (id), + UNIQUE KEY id (id) +) TYPE=MyISAM COMMENT='Discussion Forums'; +# -------------------------------------------------------- + +# +# Table structure for table `forum_subscriptions` +# + +CREATE TABLE forum_subscriptions ( + id int(10) unsigned NOT NULL auto_increment, + user int(10) unsigned NOT NULL default '0', + 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'; + + +# +# Dumping data for table `log_display` +# + +INSERT INTO log_display VALUES ('discuss', 'view forum', 'forum', 'name'); +INSERT INTO log_display VALUES ('discuss', 'subscribe', 'forum', 'name'); +INSERT INTO log_display VALUES ('discuss', 'unsubscribe', 'forum', 'name'); diff --git a/mod/forum/icon.gif b/mod/forum/icon.gif new file mode 100755 index 0000000000000000000000000000000000000000..d5855c8c072f3bea236b0b9a6e1c053b2f9391c4 GIT binary patch literal 130 zcmZ?wbhEHb6krfwSi}GV|Ns9#KHWOam||U|O$3e;K>b1#}8=o)uz{JYZ$ZF}T!gi;1Rg{i|W=!df*jUS}4D5WzmUA}c$h2=< eev@Ht!category) { + require_login($course->id); + } + + unset($SESSION->fromdiscuss); + + add_to_log($course->id, "forum", "view forums", "index.php?id=$course->id", ""); + + if ($course->category) { + print_header("$course->shortname: Forums", "$course->fullname", + "id>$course->shortname -> Forums", ""); + } else { + print_header("$course->shortname: Forums", "$course->fullname", "Forums", ""); + } + + $can_subscribe = (isstudent($course->id) || isteacher($course->id) || isadmin()); + if ($can_subscribe) { + $table->head = array ("Forum", "Description", "Topics", "Subscribed"); + } else { + $table->head = array ("Forum", "Description", "Topics"); + } + $table->align = array ("LEFT", "LEFT", "CENTER", "CENTER"); + + if ($forums = get_records("forum", "course", $id, "name ASC")) { + foreach ($forums as $forum) { + $count = count_records("discuss", "forum", "$forum->id"); + + if ($can_subscribe) { + if (is_subscribed($USER->id, $forum->id)) { + $subscribed = "YES"; + } else { + $subscribed = "NO"; + } + $table->data[] = array ("id\">$forum->name", + "$forum->intro", + "$count", + "id\">$subscribed"); + } else { + $table->data[] = array ("id\">$forum->name", + "$forum->intro", + "$count"); + } + } + } + + print_table($table); + + print_footer($course); + +?> diff --git a/mod/forum/lib.php b/mod/forum/lib.php new file mode 100644 index 0000000000..c97366b093 --- /dev/null +++ b/mod/forum/lib.php @@ -0,0 +1,159 @@ +dirroot/mod/discuss/lib.php"); + +// 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"); + +function is_subscribed($user, $forum) { + global $db; + + return record_exists_sql("SELECT * FROM forum_subscriptions WHERE user='$user' AND forum='$forum'"); +} + +function forum_subscribe($user, $forum) { + global $db; + + return $db->Execute("INSERT INTO forum_subscriptions SET user = '$user', forum = '$forum'"); +} + +function forum_unsubscribe($user, $forum) { + global $db; + + return $db->Execute("DELETE FROM forum_subscriptions WHERE user = '$user' AND forum = '$forum'"); +} + + +function get_all_topics($forum="0", $forum_sort="DESC") { + 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 + p.parent= 0 AND p.user = u.id + 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= "Discussions about course content"; + $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 forum_latest_topics($forum_id=0, $forum_numtopics=5, $forum_style="plain", $forum_sort="DESC") { + global $CFG, $USER; + + if ($forum_id) { + if (! $forum = get_record("forum", "id", $forum_id)) { + error("Forum ID was incorrect"); + } + if (! $course = get_record("course", "id", $forum->course)) { + error("Could not find the course this forum belongs to!"); + } + + if ($course->category) { + require_login($course->id); + } + + } else { + if (! $course = get_record("course", "category", 0)) { + error("Could not find a top-level course!"); + } + if (! $forum = get_course_news_forum($course->id)) { + error("Could not find or create a main forum in this course (id $course->id)"); + } + } + + if (! $topics = get_all_topics($forum->id, $forum_sort) ) { + echo "

There are no discussion topics yet in this forum.

"; + + } else { + + $replies = count_discussion_replies($forum->id); + + $topiccount = 0; + + foreach ($topics as $topic) { + $topiccount++; + + if ($forum_numtopics && ($topiccount > $forum_numtopics)) { + echo "

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

"; + break; + } + if ($replies[$topic->discuss]) { + $topic->replies = $replies[$topic->discuss]->replies; + } else { + $topic->replies = 0; + } + $ownpost = ($topic->userid == $USER->id); + switch ($forum_style) { + case "minimal": + echo "

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

\n"; + break; + default: + print_post($topic, $forum->course, $ownpost, $reply=0, $link=1, $assessed=false); + echo "
\n"; + break; + } + } + } + if ($forum->open || $USER->editing) { + echo "

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

"; + } + +} + + +?> diff --git a/mod/forum/mod.html b/mod/forum/mod.html new file mode 100644 index 0000000000..45ee5ddd18 --- /dev/null +++ b/mod/forum/mod.html @@ -0,0 +1,54 @@ +
+ + + + + + + + + + + + + + + + + + + + + + + +

Forum Name:

+ +

Type of Forum:

+ student); + require("$CFG->dirroot/mod/forum/lib.php"); + asort($FORUM_TYPE); + choose_from_menu($FORUM_TYPE, "type", $form->type); + ?> + +

Forum Introduction:

+ +

Can student?>s start new discussions?:

+ open == 1) echo "CHECKED"; ?> >Yes + open == 0) echo "CHECKED"; ?> >No +

Will this forum contain assessments?:

+ assessed == 1) echo "CHECKED"; ?> >Yes + assessed == 0) echo "CHECKED"; ?> >No +
+
+ + + + + + + + +
+
diff --git a/mod/forum/mod.php b/mod/forum/mod.php new file mode 100644 index 0000000000..6e9f6ba686 --- /dev/null +++ b/mod/forum/mod.php @@ -0,0 +1,71 @@ +timemodified = time(); + + return insert_record("forum", $forum); +} + + +function update_instance($forum) { +// Given an object containing all the necessary data, +// (defined by the form in mod.html) this function +// will update an existing instance with new data. + + $forum->timemodified = time(); + $forum->id = $forum->instance; + + return update_record("forum", $forum); +} + + +function delete_instance($id) { +// Given an ID of an instance of this module, +// this function will permanently delete the instance +// and any data that depends on it. + + global $CFG; + + include("$CFG->dirroot/mod/discuss/lib.php"); + + if (! $forum = get_record("forum", "id", "$id")) { + return false; + } + + $result = true; + + if ($discussions = get_records("discuss", "forum", $forum->id)) { + foreach ($discussions as $discuss) { + if (! delete_discussion($discuss)) { + $result = false; + } + } + } + + if (! delete_records("forum_subscriptions", "forum", "$forum->id")) { + $result = false; + } + + if (! delete_records("forum", "id", "$forum->id")) { + $result = false; + } + + return $result; +} + + +?> diff --git a/mod/forum/module.php b/mod/forum/module.php new file mode 100644 index 0000000000..65f9fd4d2c --- /dev/null +++ b/mod/forum/module.php @@ -0,0 +1,14 @@ +fullname = "Forum"; + $module->version = "20020801"; + $module->cron = 0; + $module->search = ""; + +?> + diff --git a/mod/forum/subscribe.php b/mod/forum/subscribe.php new file mode 100644 index 0000000000..987b20bf0c --- /dev/null +++ b/mod/forum/subscribe.php @@ -0,0 +1,47 @@ +course)) { + error("Forum doesn't belong to a course!"); + } + + if ($course->category) { + require_login($forum->course); + } else { + require_login(); + } + + $returnto = go_back_to("index.php?id=$course->id"); + + if ( 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); + } else { + error("Could not unsubscribe you from that forum", "$HTTP_REFERER"); + } + + } else { // subscribe + if (forum_subscribe($USER->id, $forum->id) ) { + add_to_log($course->id, "forum", "subscribe", "index.php?id=$course->id", "$forum->id"); + redirect($returnto, "You are now subscribed to recieve '$forum->name' by email.", 1); + } else { + error("Could not subscribe you to that forum", "$HTTP_REFERER"); + } + } + +?> diff --git a/mod/forum/view.php b/mod/forum/view.php new file mode 100644 index 0000000000..6f9a0f2b45 --- /dev/null +++ b/mod/forum/view.php @@ -0,0 +1,60 @@ +course)) { + error("Course is misconfigured"); + } + if (! $forum = get_record("forum", "id", $cm->instance)) { + error("Forum ID was incorrect"); + } + $buttontext = update_module_icon($cm->id); + + } else if ($f) { + if (! $forum = get_record("forum", "id", $f)) { + error("Forum ID was incorrect"); + } + if (! $course = get_record("course", "id", $forum->course)) { + error("Forum is misconfigured - don't know what course it's from"); + } + $buttontext = ""; + + } else { + error("Must specify a course module or a forum ID"); + } + + if ($course->category) { + require_login($course->id); + $navigation = "id\">$course->shortname -> + id\">Forums ->"; + } + + add_to_log($course->id, "forum", "view forum", "index.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 from this forum"; + } else { + $subtext = "Subscribe to this forum"; + } + echo "
id\">$subtext

"; + } + + forum_latest_topics($forum->id, 0); + + print_footer($course); + +?> -- 2.39.5