]> git.mjollnir.org Git - moodle.git/commitdiff
Mass change related to separating forum from discuss and making it a
authormartin <martin>
Thu, 20 Jun 2002 15:15:22 +0000 (15:15 +0000)
committermartin <martin>
Thu, 20 Jun 2002 15:15:22 +0000 (15:15 +0000)
separate module (again).  This frees up forums for some interesting
developments.  Note that database tables have moved around.

15 files changed:
course/social.php
course/topics.php
course/unenrol.php
course/weeks.php
index.php
lib/moodlelib.php
mod/forum/db/mysql.sql [new file with mode: 0644]
mod/forum/icon.gif [new file with mode: 0755]
mod/forum/index.php [new file with mode: 0644]
mod/forum/lib.php [new file with mode: 0644]
mod/forum/mod.html [new file with mode: 0644]
mod/forum/mod.php [new file with mode: 0644]
mod/forum/module.php [new file with mode: 0644]
mod/forum/subscribe.php [new file with mode: 0644]
mod/forum/view.php [new file with mode: 0644]

index 3db47cac8424d6a9dfda366e43458029e64fda4f..7a9a72ffa7e574318cfbe33f285c39a33258281c 100644 (file)
@@ -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"); 
 ?>
 
index a8f641c1cc0956fc23c81e847838d59d2d389695..8a499815a9cc0bb0b5634604a146643194686203 100644 (file)
@@ -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.
index be0fd6b984eb5a9c4838b7d6a5c0b08844b12bf7..36f3102743a48a33b457d6c78fe6a3391c4c0e3a 100644 (file)
@@ -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");
 
index 1581bfb682a6165fb01324da3d251e6ae6e4b0c7..891bf1a66d6e5f7440a6c91901787c216747f136 100644 (file)
@@ -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.
index 85ed014516220d5c12136be1b1bbabfa29264015..6015ff6f4b1fb6cb0ff246f4ab4942caea6af540 100644 (file)
--- 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/");
index 366f192fd9f872d67b128f2f0a55eecc247d2b44..4918127d6eb24c2cbb8fea203b0d93415fe23597 100644 (file)
@@ -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 <BR>";
+    }
+}
+
+
 
 /// USER DATABASE ////////////////////////////////////////////////
 
diff --git a/mod/forum/db/mysql.sql b/mod/forum/db/mysql.sql
new file mode 100644 (file)
index 0000000..7506d3b
--- /dev/null
@@ -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 (executable)
index 0000000..d5855c8
Binary files /dev/null and b/mod/forum/icon.gif differ
diff --git a/mod/forum/index.php b/mod/forum/index.php
new file mode 100644 (file)
index 0000000..99c887d
--- /dev/null
@@ -0,0 +1,67 @@
+<?PHP  // $Id$
+
+    require("../../config.php");
+    require("lib.php");
+
+    optional_variable($id);          // course
+
+    if ($id) {
+        if (! $course = get_record("course", "id", $id)) {
+            error("Course ID is incorrect");
+        }
+    } else {
+        if (! $course = get_record("course", "category", 0)) {
+            error("Could not find a top-level course!");
+        }
+    }
+
+    if ($course->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",
+                    "<A HREF=../../course/view.php?id=$course->id>$course->shortname</A> -> 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 ("<A HREF=\"view.php?f=$forum->id\">$forum->name</A>", 
+                                  "$forum->intro", 
+                                  "$count",
+                                  "<A HREF=\"subscribe.php?id=$forum->id\">$subscribed</A>");
+            } else {
+                $table->data[] = array ("<A HREF=\"view.php?f=$forum->id\">$forum->name</A>", 
+                                  "$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 (file)
index 0000000..c97366b
--- /dev/null
@@ -0,0 +1,159 @@
+<?PHP  // $Id$
+
+
+include("$CFG->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 "<P><B>There are no discussion topics yet in this forum.</B></P>";
+
+    } else {
+
+        $replies = count_discussion_replies($forum->id);
+
+        $topiccount = 0;
+
+        foreach ($topics as $topic) {
+            $topiccount++;
+
+            if ($forum_numtopics && ($topiccount > $forum_numtopics)) {
+                echo "<P ALIGN=right><A HREF=\"$CFG->wwwroot/mod/discuss/index.php?forum=$forum->id\">Older topics</A> ...</P>";
+                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 "<P><FONT COLOR=#555555>".userdate($topic->modified, "j M H:i")."</FONT>";
+                    echo "<BR>$topic->subject ";
+                    echo "<A HREF=\"$CFG->wwwroot/mod/discuss/view.php?d=$topic->discuss\">more...</A>";
+                    echo "</P>\n";
+                break;
+                default:
+                    print_post($topic, $forum->course, $ownpost, $reply=0, $link=1, $assessed=false);
+                    echo "<BR>\n";
+                break;
+            }
+        }
+    }
+    if ($forum->open || $USER->editing) {
+        echo "<P ALIGN=right>";
+        echo "<A HREF=\"$CFG->wwwroot/mod/discuss/post.php?forum=$forum->id\">Add a new topic...</A>";
+        echo "</P>";
+    }
+
+}
+
+
+?>
diff --git a/mod/forum/mod.html b/mod/forum/mod.html
new file mode 100644 (file)
index 0000000..45ee5dd
--- /dev/null
@@ -0,0 +1,54 @@
+<form name="form" method="post" action="<?=$ME ?>">
+<table cellpadding=5>
+<tr>
+<tr valign=top>
+    <td align=right><P><B>Forum Name:</B></P></TD>
+    <td>
+        <input type="text" name="name" size=30 value="<? p($form->name) ?>">
+    </td>
+</tr>
+<tr valign=top>
+    <td align=right><P><B>Type of Forum:</B></P></TD>
+    <td>
+    <?
+       $student = strtolower($course->student);
+       require("$CFG->dirroot/mod/forum/lib.php");
+       asort($FORUM_TYPE);
+       choose_from_menu($FORUM_TYPE, "type", $form->type);
+    ?>
+
+    </td>
+</tr>
+<tr valign=top>
+    <td align=right><P><B>Forum Introduction:</B></P></TD>
+    <td>
+        <textarea name="intro" rows=4 cols=50 wrap="virtual"><? p($form->intro) ?></textarea>
+    </td>
+</tr>
+
+<tr valign=top>
+    <td align=right><P><B>Can <?=$course->student?>s start new discussions?:</B></P></TD>
+    <td>
+        <input type=radio name=open value=1 <? if ($form->open == 1) echo "CHECKED"; ?> >Yes
+        <input type=radio name=open value=0 <? if ($form->open == 0) echo "CHECKED"; ?> >No
+    </td>
+</tr>
+<tr>
+    <td align=right><P><B>Will this forum contain assessments?:</B></P></TD>
+    <td>
+        <input type=radio name=assessed value=1 <? if ($form->assessed == 1) echo "CHECKED"; ?> >Yes
+        <input type=radio name=assessed value=0 <? if ($form->assessed == 0) echo "CHECKED"; ?> >No
+    </td>
+</tr>
+</table>
+<CENTER>
+<input type="hidden" name=course     value="<? p($form->course) ?>">
+<input type="hidden" name=coursemodule  value="<? p($form->coursemodule) ?>">
+<input type="hidden" name=week       value="<? p($form->week) ?>">
+<input type="hidden" name=module     value="<? p($form->module) ?>">
+<input type="hidden" name=modulename value="<? p($form->modulename) ?>">
+<input type="hidden" name=instance   value="<? p($form->instance) ?>">
+<input type="hidden" name=mode       value="<? p($form->mode) ?>">
+<input type="submit" value="Save these settings">
+</CENTER>
+</FORM>
diff --git a/mod/forum/mod.php b/mod/forum/mod.php
new file mode 100644 (file)
index 0000000..6e9f6ba
--- /dev/null
@@ -0,0 +1,71 @@
+<?PHP  // $Id$
+
+/////////////////////////////////////////////////////////////
+//
+// MOD.PHP - contains functions to add, update and delete
+//           an instance of this module
+//           
+//           Generally called from /course/mod.php
+//
+/////////////////////////////////////////////////////////////
+
+function add_instance($forum) {
+// Given an object containing all the necessary data, 
+// (defined by the form in mod.html) this function 
+// will create a new instance and return the id number 
+// of the new instance.
+
+    $forum->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 (file)
index 0000000..65f9fd4
--- /dev/null
@@ -0,0 +1,14 @@
+<?PHP // $Id$
+
+////////////////////////////////////////////////////////////////////////////////
+//  Code fragment to define the module version etc.
+//  This fragment is called by /admin/index.php
+////////////////////////////////////////////////////////////////////////////////
+
+    $module->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 (file)
index 0000000..987b20b
--- /dev/null
@@ -0,0 +1,47 @@
+<?PHP // $Id$
+
+//  Subscribe to or unsubscribe from a forum.
+
+    require("../../config.php");
+    require("lib.php");
+
+    require_variable($id);  // The forum to subscribe or unsubscribe to
+
+    if (isguest()) {
+        error("Guests are not allowed to subscribe to posts.", $HTTP_REFERER);
+    }
+
+    if (! $forum = get_record("forum", "id", $id)) {
+        error("Forum ID was incorrect");
+    }
+
+    if (! $course = get_record("course", "id", $forum->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 (file)
index 0000000..6f9a0f2
--- /dev/null
@@ -0,0 +1,60 @@
+<?PHP  // $Id$
+
+    require("../../config.php");
+    require("lib.php");
+
+    optional_variable($id);      // Course Module ID
+    optional_variable($f);       // Forum ID
+
+
+    if ($id) {
+        if (! $cm = get_record("course_modules", "id", $id)) {
+            error("Course Module ID was incorrect");
+        }
+        if (! $course = get_record("course", "id", $cm->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 = "<A HREF=\"../../course/view.php?id=$course->id\">$course->shortname</A> ->
+                       <A HREF=\"index.php?id=$course->id\">Forums</A> ->";
+    }
+
+    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 "<DIV ALIGN=RIGHT><FONT SIZE=1><A HREF=\"subscribe.php?id=$forum->id\">$subtext</A></FONT></P>";
+    }
+
+    forum_latest_topics($forum->id, 0);
+
+    print_footer($course);
+
+?>