### Eclipse Workspace Patch 1.0
#P 19stable
Index: lang/en_utf8/forum.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lang/en_utf8/forum.php,v
retrieving revision 1.21.4.15
diff -u -r1.21.4.15 forum.php
--- lang/en_utf8/forum.php	30 Jan 2009 11:30:21 -0000	1.21.4.15
+++ lang/en_utf8/forum.php	20 Feb 2009 01:25:31 -0000
@@ -23,6 +23,8 @@
 $string['allsubscribe'] = 'Subscribe to all forums';
 $string['allunsubscribe'] = 'Unsubscribe from all forums';
 $string['anyfile'] = 'Any file';
+$string['assessfinish'] = 'End ratings';
+$string['assessstart'] = 'Begin ratings';
 $string['attachment'] = 'Attachment';
 $string['blockafter'] = 'Post threshold for blocking';
 $string['blockperiod'] = 'Time period for blocking';
Index: mod/forum/lib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/mod/forum/lib.php,v
retrieving revision 1.609.2.87
diff -u -r1.609.2.87 lib.php
--- mod/forum/lib.php	30 Jan 2009 11:30:21 -0000	1.609.2.87
+++ mod/forum/lib.php	20 Feb 2009 01:25:32 -0000
@@ -26,6 +26,7 @@
 define ('FORUM_AGGREGATE_MIN', 4);
 define ('FORUM_AGGREGATE_SUM', 5);
 
+define("FORUM_MAX_EVENT_LENGTH", 5*24*60*60);   // 5 days maximum
 /// STANDARD FUNCTIONS ///////////////////////////////////////////////////////////
 
 /**
@@ -85,11 +86,13 @@
 
     $forum = stripslashes_recursive($forum);
     forum_grade_item_update($forum);
-
+    forum_after_add_or_update($forum);
+            
     return $forum->id;
 }
 
 
+
 /**
  * Given an object containing all the necessary data,
  * (defined by the form in mod.html) this function
@@ -169,6 +172,7 @@
 
     $forum = stripslashes_recursive($forum);
     forum_grade_item_update($forum);
+    forum_after_add_or_update($forum);
 
     return true;
 }
@@ -208,6 +212,14 @@
     }
 
     forum_grade_item_delete($forum);
+    
+    // remove related events
+    if ($events = get_records_select('event', "modulename = 'forum' and instance = '$forum->id'")) {
+        foreach($events as $event) {
+            delete_event($event->id);
+        }
+    }
+    
 
     return $result;
 }
@@ -6879,4 +6891,133 @@
     return array('moodle/site:accessallgroups', 'moodle/site:viewfullnames', 'moodle/site:trustcontent');
 }
 
+function forum_after_add_or_update($forum) {
+    // Update the events relating to this forum.
+    // This is slightly inefficient, deleting the old events and creating new ones. However,
+    // there are at most two events, and this keeps the code simpler.
+    if ($events = get_records_select('event', "modulename = 'forum' and instance = '$forum->id'")) {
+        foreach($events as $event) {
+            delete_event($event->id);
+        }
+    }
+    
+    if ($forum->assesstimestart and $forum->assesstimefinish) {
+                $event = new object();
+                $event->name        = $forum->name.' ('.get_string('assessstart','forum').')';
+                $event->description = $forum->intro;
+                $event->courseid    = $forum->course;
+                $event->groupid     = 0;
+                $event->userid      = 0;
+                $event->modulename  = 'forum';
+                $event->instance    = $forum->id;
+                $event->eventtype   = 'open';
+                $event->timestart   = $forum->assesstimestart;
+                $event->timeduration = 0;
+                $event->visible     = instance_is_visible('forum', $forum);
+                $event->timeduration = $forum->assesstimefinish - $forum->assesstimestart;
+    }
+
+    if ($forum->assesstimefinish and $forum->assesstimestart and $event->timeduration <= FORUM_MAX_EVENT_LENGTH) {
+        // Single event for the forum.
+        add_event($event);
+    } else {
+        // Separate start and end events.
+        $event->timeduration  = 0;
+        if ($forum->assesstimestart) {
+            add_event($event);
+            unset($event->id); // So we can use the same object for the close event.
+        }
+        if ($forum->assesstimefinish) {
+            $event->name      = $forum->name.' ('.get_string('assessfinish', 'forum').')';
+            $event->timestart = $forum->assesstimefinish;
+            $event->eventtype = 'close';
+            add_event($event);
+        }
+    }
+}
+
+function forum_refresh_events($courseid = 0) {
+// This standard function will check all instances of this module
+// and make sure there are up-to-date events created for each of them.
+// If courseid = 0, then every forum event in the site is checked, else
+// only forum events belonging to the course specified are checked.
+// This function is used, in its new format, by restore_refresh_events()
+// Currently for MDL-18300 just creates asssessment events, not availability
+
+    if ($courseid == 0) {
+        if (! $forums = get_records("forum")) {
+            return true;
+        }
+    } else {
+        if (! $forums = get_records("forum", "course", $courseid)) {
+            return true;
+        }
+    }
+    $moduleid = get_field('modules', 'id', 'name', 'forum');
+
+    foreach ($forums as $forum) {
+        $event = NULL;
+        $event2 = NULL;
+        $event2old = NULL;
+
+        // consider what the best order by would be in quiz it was timestart, keep in mind we will ultimately want to recreate assessment restrictions and availability restrictions
+        // here we are creating assessment events so assessstart seemed logical
+        if ($events = get_records_select('event', "modulename = 'forum' AND instance = '$forum->id' ORDER BY assessstart")) {
+            $event = array_shift($events);
+            if (!empty($events)) {
+                $event2old = array_shift($events);
+                if (!empty($events)) {
+                    foreach ($events as $badevent) {
+                        delete_records('event', 'id', $badevent->id);
+                    }
+                }
+            }
+        }
+
+        $event->name        = addslashes($forum->name.' ('.get_string('assessstart','forum').')');
+        $event->description = addslashes($forum->intro);
+        $event->courseid    = $forum->course;
+        $event->groupid     = 0;
+        $event->userid      = 0;
+        $event->modulename  = 'forum';
+        $event->instance    = $forum->id;
+        $event->visible     = instance_is_visible('forum', $forum);
+        $event->timestart   = $forum->assessstart;
+        $event->eventtype   = 'open';
+        $event->timeduration = ($forum->assessfinish - $forum->assessstart);
+
+        if ($event->timeduration > FORUM_MAX_EVENT_LENGTH) {  /// Set up two events
+
+            $event2 = $event;
+
+            $event->timeduration = 0;
+
+            $event2->name        = addslashes($forum->name.' ('.get_string('assessfinish', 'forum').')');
+            $event2->timestart   = $forum->assessfinish;
+            $event2->eventtype   = 'close';
+            $event2->timeduration = 0;
+
+            if (empty($event2old->id)) {
+                unset($event2->id);
+                add_event($event2);
+            } else {
+                $event2->id = $event2old->id;
+                update_event($event2);
+            }
+        } else if (!empty($event2old->id)) {
+            delete_event($event2old->id);
+        }
+
+        if (empty($event->id)) {
+            if (!empty($event->timestart)) {
+                add_event($event);
+            }
+        } else {
+            update_event($event);
+        }
+
+    }
+    return true;
+}
+
 ?>
