Index: common/db/round.php
===================================================================
--- common/db/round.php	(revision 1130)
+++ common/db/round.php	(working copy)
@@ -226,11 +226,13 @@
     $tasks = array_diff($tasks, $common_tasks);
 
     // delete round-task relations
-    $query = sprintf("DELETE FROM ia_round_task
-                      WHERE round_id = %s AND task_id IN (%s)",
-                     db_quote($round_id),
-                     implode(',', array_map("db_quote", $old_tasks)));
-    db_query($query);
+    if (count($old_tasks) > 0) {
+        $query = sprintf("DELETE FROM ia_round_task
+                          WHERE round_id = %s AND task_id IN (%s)",
+                         db_quote($round_id),
+                         implode(',', array_map("db_quote", $old_tasks)));
+        db_query($query);
+    }
 
     foreach($old_tasks as $task) {
         // Update parent round cache for old tasks
Index: common/db/round_task.php
===================================================================
--- common/db/round_task.php	(revision 1129)
+++ common/db/round_task.php	(working copy)
@@ -1,6 +1,7 @@
 <?php
 
 require_once(IA_ROOT_DIR."common/db/db.php");
+require_once(IA_ROOT_DIR."common/db/task.php");
 
 // Updates the `order_id` for a (round, task) pair.
 function round_task_update_order_id($round_id, $task_id, $order_id) {
@@ -23,12 +24,20 @@
     $order_id = 0;
     foreach ($res as $row) {
         $order_id += 1;
-        $query = sprintf(
-            "UPDATE ia_round_task
-             SET order_id = %s
-             WHERE round_id = %s AND task_id = %s",
-             db_quote($order_id), db_quote($round_id),
-             db_quote($row['task_id']));
-        db_query($query);
+
+        if ($order_id != $row['order_id']) {
+            $query = sprintf(
+                "UPDATE ia_round_task
+                 SET order_id = %s
+                 WHERE round_id = %s AND task_id = %s",
+                 db_quote($order_id), db_quote($round_id),
+                 db_quote($row['task_id']));
+            db_query($query);
+
+            // update forum if necessary
+            if ($round_id == 'arhiva') {
+                task_update_forum_topic($row['task_id']);
+            }
+        }
     }
 }
Index: common/db/task.php
===================================================================
--- common/db/task.php	(revision 1129)
+++ common/db/task.php	(working copy)
@@ -262,4 +262,86 @@
 
     return $tasks;
 }
+
+// Updates the forum topic associated with a task.
+function task_update_forum_topic($task_id, $round_id = "arhiva") {
+    if (!is_task_id($task_id)) {
+        log_error("Invalid task id");
+    }
+
+    // Get task info
+    $query = "SELECT title, page_name FROM ia_task
+              WHERE id = " . db_quote($task_id);
+    $task = db_fetch($query);
+
+    // Get the forum topic
+    $query = "SELECT forum_topic
+              FROM ia_textblock
+              WHERE name = " . db_quote($task['page_name']);
+    $res = db_fetch($query);
+    $topic_id = $res['forum_topic'];
+
+    // Check the textblock has an associated forum topic.
+    if (is_null($topic_id)) {
+        return;
+    }
+
+    // Get the first message from the topic
+    $query = "SELECT ID_FIRST_MSG AS `msg_id`
+              FROM ia_smf_topics
+              WHERE ID_TOPIC = " . db_quote($topic_id);
+    $res = db_fetch($query);
+    // Topic id doesn't exist
+    if (is_null($res)) {
+        return;
+    }
+    $message_id = $res['msg_id'];
+
+    // Get the subject and the body of the message
+    $query = "SELECT subject, body FROM ia_smf_messages
+              WHERE ID_MSG = " . db_quote($message_id);
+    $message = db_fetch($query);
+
+    // Find the number associated with the (task, round) pair.
+    $query = sprintf(
+        "SELECT order_id FROM ia_round_task
+         WHERE round_id = %s AND task_id = %s",
+         db_quote($round_id), db_quote($task_id));
+    $res = db_fetch($query);
+    $task_number = sprintf("%03d", $res['order_id'] - 1);
+
+    // New info
+    $new_subject = $task_number . " " . $task['title'];
+    $body_start = mb_substr($message['body'], 0, 35);
+    if ($body_start != "Aici puteti discuta despre problema" &&
+        $body_start != "Aici puteÈ›i discuta despre prob" &&
+        $body_start != "Aici puteÅ£i discuta despre probl") {
+        $new_body = 'Aici puteÅ£i discuta despre problema ' .
+                    '[url=http://infoarena.ro/problema/' . $task_id . ']' .
+                    $task['title'] . '[/url].';
+    } else {
+        $new_body = $message['body'];
+    }
+
+    // Finally, update the message
+    $query = sprintf(
+        "UPDATE ia_smf_messages
+         SET subject = %s, body = %s
+         WHERE ID_MSG = %s",
+         db_quote($new_subject), db_quote($new_body), db_quote($message_id));
+    db_query($query);
+
+    // Not finished yet, must change replies too.
+    // This is extremely time consuming.
+    if ($new_subject != $message['subject']) {
+        $query = sprintf(
+            "UPDATE ia_smf_messages
+             SET subject = %s
+             WHERE subject LIKE %s",
+             db_quote("RÄƒspuns: " . $new_subject),
+             db_quote("%" . $message['subject']));
+        db_query($query);
+    }
+}
+
 ?>
Index: scripts/add-round-task-order
===================================================================
--- scripts/add-round-task-order	(revision 1129)
+++ scripts/add-round-task-order	(working copy)
@@ -2,7 +2,7 @@
 <?php
 
 require_once(dirname($argv[0])."/utilities.php");
-require_once(dirname($argv[0])."/../common/task.php");
+require_once(dirname($argv[0])."/../common/db/task.php");
 
 db_connect();
 
@@ -43,4 +43,12 @@
     }
 }
 
+// Update forum topics
+$query = "SELECT task_id FROM ia_round_task
+          WHERE round_id = 'arhiva'";
+$res = db_fetch_all($query);
+foreach ($res as $task) {
+    task_update_forum_topic($task['task_id']);
+}
+
 ?>
Index: www/controllers/round.php
===================================================================
--- www/controllers/round.php	(revision 1129)
+++ www/controllers/round.php	(working copy)
@@ -264,7 +264,7 @@
 
             if ($current_id != $order_id) {
                 round_task_update_order_id($round_id, $task_id, $current_id);
-                //task_update_forum_topic($task_id);
+                task_update_forum_topic($task_id);
             }
 
             $current_id += 1;
