]> git.mjollnir.org Git - moodle.git/commitdiff
merged from 14_STABLE - cleaned text from forms
authorskodak <skodak>
Sun, 10 Oct 2004 17:37:48 +0000 (17:37 +0000)
committerskodak <skodak>
Sun, 10 Oct 2004 17:37:48 +0000 (17:37 +0000)
mod/dialogue/dialogues.php
mod/dialogue/locallib.php

index 8abaca4ea8ce628602274067298816d8b8ab5c7e..357a90972e6aa6385b99c258c1c5d67b47528476 100644 (file)
-<?php  // $Id$
-
-/*************************************************
-    ACTIONS handled are:
-
-    closeconversation
-    confirmclose
-    getsubject
-    insertentries
-    openconversation
-    printdialogue
-    showdialogues
-    updatesubject
-    
-************************************************/
-
-    require_once("../../config.php");
-    require_once("lib.php");
-    require_once("locallib.php");
-
-    require_variable($id);    // Course Module 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 (! $dialogue = get_record("dialogue", "id", $cm->instance)) {
-        error("Course module dialogue is incorrect");
-    }
-
-    require_login($course->id);
-    
-    // set up some general variables
-    $usehtmleditor = can_use_html_editor();
-
-    $strdialogues = get_string("modulenameplural", "dialogue");
-    $strdialogue  = get_string("modulename", "dialogue");
-    
-    // ... print the header and...
-    print_header_simple("$dialogue->name", "",
-                 "<a href=\"index.php?id=$course->id\">$strdialogues</a> -> 
-                  <a hre=\"view.php?id=$cm->id\">$dialogue->name</a>", 
-                  "", "", true);
-
-
-    require_variable($action); // need something to do!
-    
-    /************** close conversation ************************************/
-    if ($action == 'closeconversation') {
-        if (empty($_GET['cid'])) {
-            error("Close dialogue: Missing conversation id");
-        }
-        else {
-            $conversationid = $_GET['cid'];
-        }
-        if (!set_field("dialogue_conversations", "closed", 1, "id", $conversationid)) {
-            error("Close dialogue: unable to set closed");
-        }
-        if (!set_field("dialogue_conversations", "lastid", $USER->id, "id", $conversationid)) {
-            error("Close dialogue: unable to set lastid");
-        }
-        $pane=$_GET['pane'];
-
-        add_to_log($course->id, "dialogue", "closed", "view.php?id=$cm->id", "$conversationid");
-        redirect("view.php?id=$cm->id&amp;pane=$pane", get_string("dialogueclosed", "dialogue"));
-    }
-    
-    
-    /****************** confirm close ************************************/
-    elseif ($action == 'confirmclose' ) {
-
-        if (empty($_GET['cid'])) {
-            error("Confirm Close: conversation id missing");
-        }
-        if (!$conversation = get_record("dialogue_conversations", "id", $_GET['cid'])) {
-            error("Confirm close: cannot get conversation record");
-        }
-        if ($conversation->userid == $USER->id) {
-            if (!$user = get_record("user", "id", $conversation->recipientid)) {
-                error("Confirm Close: cannot get recipient record");
-            }
-        }
-        else {
-            if (!$user = get_record("user", "id", $conversation->userid)) {
-                error("Confirm Close: cannot get user record");
-            }
-        }
-        $pane = $_GET['pane'];
-        notice_yesno(get_string("confirmclosure", "dialogue", fullname($user)), 
-             "dialogues.php?action=closeconversation&amp;id=$cm->id&amp;cid=$conversation->id&amp;pane=$pane", 
-             "view.php?id=$cm->id&amp;pane=$pane");
-    }
-    
-    /****************** get subject ************************************/
-    elseif ($action == 'getsubject' ) {
-
-        if (empty($_GET['cid'])) {
-            error("Confirm Close: conversation id missing");
-        }
-        print_heading(get_string("addsubject", "dialogue"));
-        echo "<form name=\"getsubjectform\" method=\"post\" action=\"dialogues.php\"/>\n";
-        echo "<input type=\"hidden\" name=\"action\" value=\"updatesubject\"/>\n";
-        echo "<input type=\"hidden\" name=\"id\" value=\"$_GET[id]\"/>\n";
-        echo "<input type=\"hidden\" name=\"cid\" value=\"$_GET[cid]\"/>\n";
-        echo "<input type=\"hidden\" name=\"pane\" value=\"$_GET[pane]\"/>\n";
-        echo "<table align=\"center\"border=\"1\" width=\"60%\">\n";
-        echo "<tr><td align=\"right\"><b>".get_string("subject", "dialogue")."</b></td>";
-        echo "<td><input type=\"text\" size=\"50\" maxsize=\"100\" name=\"subject\" 
-                value=\"\" /></td></tr>\n";
-        echo "<tr><td colspan=\"2\" align=\"center\"><input type=\"submit\" value=\"".
-            get_string("addsubject", "dialogue")."\" /></td></tr>\n";
-        echo "</table></form>\n";
-    }
-    
-        
-    /****************** insert conversation entries ******************************/
-    elseif ($action == 'insertentries' ) {
-
-        $timenow = time();
-        $n = 0;
-        // get all the open conversations for this user
-        if ($conversations = dialogue_get_conversations($dialogue, $USER, "closed = 0")) {
-            foreach ($conversations as $conversation) {
-                $textarea_name = "reply$conversation->id";
-                $stripped_text = '';
-                if (isset($_POST[$textarea_name])) {
-                    $stripped_text = strip_tags(trim($_POST[$textarea_name]));
-                }
-                if ($stripped_text) {
-                    unset($item);
-                    $item->dialogueid = $dialogue->id;
-                    $item->conversationid = $conversation->id;
-                    $item->userid = $USER->id;
-                    $item->timecreated = time(); 
-                    // reverse the dialogue mail default 
-                    $item->mailed = !$dialogue->maildefault;
-                    $item->text = $_POST[$textarea_name];
-                    if (!$item->id = insert_record("dialogue_entries", $item)) {
-                        error("Insert Entries: Could not insert dialogue record!");
-                    }
-                    if (!set_field("dialogue_conversations", "lastid", $USER->id, "id", $conversation->id)) {
-                        error("Insert Dialogue Entries: could not set lastid");
-                    }
-                    if (!set_field("dialogue_conversations", "timemodified", $timenow, "id", 
-                            $conversation->id)) {
-                        error("Insert Dialogue Entries: could not set lastid");
-                    }
-                    // reset seenon time
-                    if (!set_field("dialogue_conversations", "seenon", 0, "id", 
-                            $conversation->id)) {
-                        error("Insert Dialogue Entries: could not reset seenon");
-                    }
-                    add_to_log($course->id, "dialogue", "add entry", "view.php?id=$cm->id", "$item->id");
-                    $n++;
-                }
-            }
-        }
-        redirect("view.php?id=$cm->id&amp;pane={$_POST['pane']}", get_string("numberofentriesadded", 
-                    "dialogue", $n));
-    }
-    
-    /****************** list closed conversations *********************************/
-    elseif ($action == 'listclosed') {
-    
-        print_simple_box( text_to_html($dialogue->intro) , "center");
-        echo "<br />";
-        
-        dialogue_list_closed_conversations($dialogue);
-    }
-        
-    /****************** open conversation ************************************/
-    elseif ($action == 'openconversation' ) {
-
-        if (empty($_POST['recipientid'])) {
-            redirect("view.php?id=$cm->id", get_string("nopersonchosen", "dialogue"));
-        } else {
-            $recipientid = $_POST['recipientid'];
-            if (substr($recipientid, 0, 1) == 'g') { // it's a group
-                $groupid = intval(substr($recipientid, 1));
-                if ($groupid) { // it's a real group
-                    $recipients = get_records_sql("SELECT u.*
-                                FROM {$CFG->prefix}user u,
-                                     {$CFG->prefix}groups_members g
-                                WHERE g.groupid = $groupid and
-                                      u.id = g.userid");
-                } else { // it's all participants
-                    $recipients = get_course_students($course->id);
-                }
-            } else {
-                $recipients[$recipientid] = get_record("user", "id", $recipientid);
-            }
-            if ($recipients) {
-                $n = 0;
-                foreach ($recipients as $recipient) { 
-                    if ($recipient->id == $USER->id) { // teacher could be member of a group
-                        continue;
-                    }
-                    $stripped_text = strip_tags(trim($_POST['firstentry']));
-                    if (!$stripped_text) {
-                        redirect("view.php?id=$cm->id", get_string("notextentered", "dialogue"));
-                    }
-                    unset($conversation);
-                    $conversation->dialogueid = $dialogue->id;
-                    $conversation->userid = $USER->id;
-                    $conversation->recipientid = $recipient->id;
-                    $conversation->lastid = $USER->id; // this USER is adding an entry too
-                    $conversation->timemodified = time();
-                    $conversation->subject = $_POST['subject']; // may be blank
-                    if (!$conversation->id = insert_record("dialogue_conversations", $conversation)) {
-                        error("Open dialogue: Could not insert dialogue record!");
-                    }
-                    add_to_log($course->id, "dialogue", "open", "view.php?id=$cm->id", "$dialogue->id");
-
-                    // now add the entry
-                    unset($entry);
-                    $entry->dialogueid = $dialogue->id;
-                    $entry->conversationid = $conversation->id;
-                    $entry->userid = $USER->id;
-                    $entry->timecreated = time(); 
-                    // reverse the dialogue default value
-                    $entry->mailed = !$dialogue->maildefault;
-                    $entry->text = $_POST['firstentry'];
-                    if (!$entry->id = insert_record("dialogue_entries", $entry)) {
-                        error("Insert Entries: Could not insert dialogue record!");
-                    }
-                    add_to_log($course->id, "dialogue", "add entry", "view.php?id=$cm->id", "$entry->id");
-                    $n++;
-                }
-                print_heading(get_string("numberofentriesadded", "dialogue", $n));
-            } else {
-                redirect("view.php?id=$cm->id", get_string("noavailablepeople", "dialogue"));
-            }
-            if (isset($groupid)) {
-                if ($groupid) { // a real group
-                    if (!$group = get_record("groups", "id", $groupid)) {
-                        error("Dialogue open conversation: Group not found");
-                    }
-                    redirect("view.php?id=$cm->id", get_string("dialogueopened", "dialogue", $group->name));
-                } else { // all participants
-                    redirect("view.php?id=$cm->id", get_string("dialogueopened", "dialogue", 
-                                get_string("allparticipants")));
-                }
-            } else {
-                if (!$user =  get_record("user", "id", $conversation->recipientid)) {
-                    error("Open dialogue: user record not found");
-                }
-                redirect("view.php?id=$cm->id", get_string("dialogueopened", "dialogue", fullname($user) ));
-            }
-        }
-    }
-    
-
-    /****************** print dialogue (allowing new entry)********************/
-    elseif ($action == 'printdialogue') {
-    
-        if (!$conversation = get_record("dialogue_conversations", "id", $_GET['cid'])) {
-            error("Print Dialogue: can not get conversation record");
-        }
-            
-        print_simple_box( text_to_html($dialogue->intro) , "center");
-        echo "<br />";
-        
-        dialogue_print_conversation($dialogue, $conversation);
-    }
-    
-
-    /****************** show dialogues ****************************************/
-    elseif ($action == 'showdialogues') {
-    
-        if (!$conversation = get_record("dialogue_conversations", "id", $_GET['cid'])) {
-            error("Show Dialogue: can not get conversation record");
-        }
-            
-        print_simple_box( text_to_html($dialogue->intro) , "center");
-        echo "<br />";
-        
-        dialogue_show_conversation($dialogue, $conversation);
-        dialogue_show_other_conversations($dialogue, $conversation);
-    }
-    
-
-    /****************** update subject ****************************************/
-    elseif ($action == 'updatesubject') {
-    
-        if (!$conversation = get_record("dialogue_conversations", "id", $_POST['cid'])) {
-            error("Update Subject: can not get conversation record");
-        }
-            
-        if (!$_POST['subject']) {
-            redirect("view.php?id=$cm->id&amp;pane=$_POST[pane]", get_string("nosubject", "dialogue"));
-        } elseif (!set_field("dialogue_conversations", "subject", $_POST['subject'], "id", $_POST['cid'])) {
-            error("Update subject: could not update conversation record");
-        }
-        redirect("view.php?id=$cm->id&amp;pane=$_POST[pane]", get_string("subjectadded", "dialogue"));
-    }
-    
-
-    /*************** no man's land **************************************/
-    else {
-        error("Fatal Error: Unknown Action: ".$action."\n");
-    }
-
-    print_footer($course);
-
-?>
+<?php  // $Id$\r
+\r
+/*************************************************\r
+    ACTIONS handled are:\r
+\r
+    closeconversation\r
+    confirmclose\r
+    getsubject\r
+    insertentries\r
+    openconversation\r
+    printdialogue\r
+    showdialogues\r
+    updatesubject\r
+    \r
+************************************************/\r
+\r
+    require_once("../../config.php");\r
+    require_once("lib.php");\r
+    require_once("locallib.php");\r
+\r
+    require_variable($id);    // Course Module ID\r
+\r
+    if (! $cm = get_record("course_modules", "id", $id)) {\r
+        error("Course Module ID was incorrect");\r
+    }\r
+\r
+    if (! $course = get_record("course", "id", $cm->course)) {\r
+        error("Course is misconfigured");\r
+    }\r
+\r
+    if (! $dialogue = get_record("dialogue", "id", $cm->instance)) {\r
+        error("Course module dialogue is incorrect");\r
+    }\r
+\r
+    require_login($course->id);\r
+    \r
+    // set up some general variables\r
+    $usehtmleditor = can_use_html_editor();\r
+\r
+    $strdialogues = get_string("modulenameplural", "dialogue");\r
+    $strdialogue  = get_string("modulename", "dialogue");\r
+    \r
+    // ... print the header and...\r
+    print_header_simple("$dialogue->name", "",\r
+                 "<a href=\"index.php?id=$course->id\">$strdialogues</a> -> \r
+                  <a hre=\"view.php?id=$cm->id\">$dialogue->name</a>", \r
+                  "", "", true);\r
+\r
+\r
+    require_variable($action); // need something to do!\r
+    \r
+    /************** close conversation ************************************/\r
+    if ($action == 'closeconversation') {\r
+        if (empty($_GET['cid'])) {\r
+            error("Close dialogue: Missing conversation id");\r
+        }\r
+        else {\r
+            $conversationid = $_GET['cid'];\r
+        }\r
+        if (!set_field("dialogue_conversations", "closed", 1, "id", $conversationid)) {\r
+            error("Close dialogue: unable to set closed");\r
+        }\r
+        if (!set_field("dialogue_conversations", "lastid", $USER->id, "id", $conversationid)) {\r
+            error("Close dialogue: unable to set lastid");\r
+        }\r
+        $pane=$_GET['pane'];\r
+\r
+        add_to_log($course->id, "dialogue", "closed", "view.php?id=$cm->id", "$conversationid");\r
+        redirect("view.php?id=$cm->id&amp;pane=$pane", get_string("dialogueclosed", "dialogue"));\r
+    }\r
+    \r
+    \r
+    /****************** confirm close ************************************/\r
+    elseif ($action == 'confirmclose' ) {\r
+\r
+        if (empty($_GET['cid'])) {\r
+            error("Confirm Close: conversation id missing");\r
+        }\r
+        if (!$conversation = get_record("dialogue_conversations", "id", $_GET['cid'])) {\r
+            error("Confirm close: cannot get conversation record");\r
+        }\r
+        if ($conversation->userid == $USER->id) {\r
+            if (!$user = get_record("user", "id", $conversation->recipientid)) {\r
+                error("Confirm Close: cannot get recipient record");\r
+            }\r
+        }\r
+        else {\r
+            if (!$user = get_record("user", "id", $conversation->userid)) {\r
+                error("Confirm Close: cannot get user record");\r
+            }\r
+        }\r
+        $pane = $_GET['pane'];\r
+        notice_yesno(get_string("confirmclosure", "dialogue", fullname($user)), \r
+             "dialogues.php?action=closeconversation&amp;id=$cm->id&amp;cid=$conversation->id&amp;pane=$pane", \r
+             "view.php?id=$cm->id&amp;pane=$pane");\r
+    }\r
+    \r
+    /****************** get subject ************************************/\r
+    elseif ($action == 'getsubject' ) {\r
+\r
+        if (empty($_GET['cid'])) {\r
+            error("Confirm Close: conversation id missing");\r
+        }\r
+        print_heading(get_string("addsubject", "dialogue"));\r
+        echo "<form name=\"getsubjectform\" method=\"post\" action=\"dialogues.php\">\n";\r
+        echo "<input type=\"hidden\" name=\"action\" value=\"updatesubject\"/>\n";\r
+        echo "<input type=\"hidden\" name=\"id\" value=\"".p($_GET[id])."\"/>\n";\r
+        echo "<input type=\"hidden\" name=\"cid\" value=\"".p($_GET[cid])."\"/>\n";\r
+        echo "<input type=\"hidden\" name=\"pane\" value=\"".p($_GET[pane])."\"/>\n";\r
+        echo "<table align=\"center\" border=\"1\" width=\"60%\">\n";\r
+        echo "<tr><td align=\"right\"><b>".get_string("subject", "dialogue")."</b></td>";\r
+        echo "<td><input type=\"text\" size=\"50\" maxsize=\"100\" name=\"subject\" \r
+                value=\"\" /></td></tr>\n";\r
+        echo "<tr><td colspan=\"2\" align=\"center\"><input type=\"submit\" value=\"".\r
+            get_string("addsubject", "dialogue")."\" /></td></tr>\n";\r
+        echo "</table></form>\n";\r
+    }\r
+    \r
+        \r
+    /****************** insert conversation entries ******************************/\r
+    elseif ($action == 'insertentries' ) {\r
+\r
+        $timenow = time();\r
+        $n = 0;\r
+        // get all the open conversations for this user\r
+        if ($conversations = dialogue_get_conversations($dialogue, $USER, "closed = 0")) {\r
+            foreach ($conversations as $conversation) {\r
+                $textarea_name = "reply$conversation->id";\r
+                $stripped_text = '';\r
+                if (isset($_POST[$textarea_name])) {\r
+                    $stripped_text = strip_tags(trim($_POST[$textarea_name]));\r
+                }\r
+                if ($stripped_text) {\r
+                    unset($item);\r
+                    $item->dialogueid = $dialogue->id;\r
+                    $item->conversationid = $conversation->id;\r
+                    $item->userid = $USER->id;\r
+                    $item->timecreated = time(); \r
+                    // reverse the dialogue mail default \r
+                    $item->mailed = !$dialogue->maildefault;\r
+                    $item->text = clean_text($_POST[$textarea_name]);\r
+                    if (!$item->id = insert_record("dialogue_entries", $item)) {\r
+                        error("Insert Entries: Could not insert dialogue record!");\r
+                    }\r
+                    if (!set_field("dialogue_conversations", "lastid", $USER->id, "id", $conversation->id)) {\r
+                        error("Insert Dialogue Entries: could not set lastid");\r
+                    }\r
+                    if (!set_field("dialogue_conversations", "timemodified", $timenow, "id", \r
+                            $conversation->id)) {\r
+                        error("Insert Dialogue Entries: could not set lastid");\r
+                    }\r
+                    // reset seenon time\r
+                    if (!set_field("dialogue_conversations", "seenon", 0, "id", \r
+                            $conversation->id)) {\r
+                        error("Insert Dialogue Entries: could not reset seenon");\r
+                    }\r
+                    add_to_log($course->id, "dialogue", "add entry", "view.php?id=$cm->id", "$item->id");\r
+                    $n++;\r
+                }\r
+            }\r
+        }\r
+        redirect("view.php?id=$cm->id&amp;pane={$_POST['pane']}", get_string("numberofentriesadded", \r
+                    "dialogue", $n));\r
+    }\r
+    \r
+    /****************** list closed conversations *********************************/\r
+    elseif ($action == 'listclosed') {\r
+    \r
+        print_simple_box( text_to_html($dialogue->intro) , "center");\r
+        echo "<br />";\r
+        \r
+        dialogue_list_closed_conversations($dialogue);\r
+    }\r
+        \r
+    /****************** open conversation ************************************/\r
+    elseif ($action == 'openconversation' ) {\r
+\r
+        if (empty($_POST['recipientid'])) {\r
+            redirect("view.php?id=$cm->id", get_string("nopersonchosen", "dialogue"));\r
+        } else {\r
+            $recipientid = $_POST['recipientid'];\r
+            if (substr($recipientid, 0, 1) == 'g') { // it's a group\r
+                $groupid = intval(substr($recipientid, 1));\r
+                if ($groupid) { // it's a real group\r
+                    $recipients = get_records_sql("SELECT u.*\r
+                                FROM {$CFG->prefix}user u,\r
+                                     {$CFG->prefix}groups_members g\r
+                                WHERE g.groupid = $groupid and\r
+                                      u.id = g.userid");\r
+                } else { // it's all participants\r
+                    $recipients = get_course_students($course->id);\r
+                }\r
+            } else {\r
+                $recipients[$recipientid] = get_record("user", "id", $recipientid);\r
+            }\r
+            if ($recipients) {\r
+                $n = 0;\r
+                foreach ($recipients as $recipient) { \r
+                    if ($recipient->id == $USER->id) { // teacher could be member of a group\r
+                        continue;\r
+                    }\r
+                    $stripped_text = strip_tags(trim($_POST['firstentry']));\r
+                    if (!$stripped_text) {\r
+                        redirect("view.php?id=$cm->id", get_string("notextentered", "dialogue"));\r
+                    }\r
+                    unset($conversation);\r
+                    $conversation->dialogueid = $dialogue->id;\r
+                    $conversation->userid = $USER->id;\r
+                    $conversation->recipientid = $recipient->id;\r
+                    $conversation->lastid = $USER->id; // this USER is adding an entry too\r
+                    $conversation->timemodified = time();\r
+                    $conversation->subject = clean_text($_POST['subject']); // may be blank\r
+                    if (!$conversation->id = insert_record("dialogue_conversations", $conversation)) {\r
+                        error("Open dialogue: Could not insert dialogue record!");\r
+                    }\r
+                    add_to_log($course->id, "dialogue", "open", "view.php?id=$cm->id", "$dialogue->id");\r
+\r
+                    // now add the entry\r
+                    unset($entry);\r
+                    $entry->dialogueid = $dialogue->id;\r
+                    $entry->conversationid = $conversation->id;\r
+                    $entry->userid = $USER->id;\r
+                    $entry->timecreated = time(); \r
+                    // reverse the dialogue default value\r
+                    $entry->mailed = !$dialogue->maildefault;\r
+                    $entry->text = clean_text($_POST['firstentry']);\r
+                    if (!$entry->id = insert_record("dialogue_entries", $entry)) {\r
+                        error("Insert Entries: Could not insert dialogue record!");\r
+                    }\r
+                    add_to_log($course->id, "dialogue", "add entry", "view.php?id=$cm->id", "$entry->id");\r
+                    $n++;\r
+                }\r
+                print_heading(get_string("numberofentriesadded", "dialogue", $n));\r
+            } else {\r
+                redirect("view.php?id=$cm->id", get_string("noavailablepeople", "dialogue"));\r
+            }\r
+            if (isset($groupid)) {\r
+                if ($groupid) { // a real group\r
+                    if (!$group = get_record("groups", "id", $groupid)) {\r
+                        error("Dialogue open conversation: Group not found");\r
+                    }\r
+                    redirect("view.php?id=$cm->id", get_string("dialogueopened", "dialogue", $group->name));\r
+                } else { // all participants\r
+                    redirect("view.php?id=$cm->id", get_string("dialogueopened", "dialogue", \r
+                                get_string("allparticipants")));\r
+                }\r
+            } else {\r
+                if (!$user =  get_record("user", "id", $conversation->recipientid)) {\r
+                    error("Open dialogue: user record not found");\r
+                }\r
+                redirect("view.php?id=$cm->id", get_string("dialogueopened", "dialogue", fullname($user) ));\r
+            }\r
+        }\r
+    }\r
+    \r
+\r
+    /****************** print dialogue (allowing new entry)********************/\r
+    elseif ($action == 'printdialogue') {\r
+    \r
+        if (!$conversation = get_record("dialogue_conversations", "id", $_GET['cid'])) {\r
+            error("Print Dialogue: can not get conversation record");\r
+        }\r
+            \r
+        print_simple_box( text_to_html($dialogue->intro) , "center");\r
+        echo "<br />";\r
+        \r
+        dialogue_print_conversation($dialogue, $conversation);\r
+    }\r
+    \r
+\r
+    /****************** show dialogues ****************************************/\r
+    elseif ($action == 'showdialogues') {\r
+    \r
+        if (!$conversation = get_record("dialogue_conversations", "id", $_GET['cid'])) {\r
+            error("Show Dialogue: can not get conversation record");\r
+        }\r
+            \r
+        print_simple_box( text_to_html($dialogue->intro) , "center");\r
+        echo "<br />";\r
+        \r
+        dialogue_show_conversation($dialogue, $conversation);\r
+        dialogue_show_other_conversations($dialogue, $conversation);\r
+    }\r
+    \r
+\r
+    /****************** update subject ****************************************/\r
+    elseif ($action == 'updatesubject') {\r
+    \r
+        if (!$conversation = get_record("dialogue_conversations", "id", $_POST['cid'])) {\r
+            error("Update Subject: can not get conversation record");\r
+        }\r
+            \r
+        if (!$_POST['subject']) {\r
+            redirect("view.php?id=$cm->id&amp;pane=$_POST[pane]", get_string("nosubject", "dialogue"));\r
+        } elseif (!set_field("dialogue_conversations", "subject", clean_text($_POST['subject']), "id", $_POST['cid'])) {\r
+            error("Update subject: could not update conversation record");\r
+        }\r
+        redirect("view.php?id=$cm->id&amp;pane=$_POST[pane]", get_string("subjectadded", "dialogue"));\r
+    }\r
+    \r
+\r
+    /*************** no man's land **************************************/\r
+    else {\r
+        error("Fatal Error: Unknown Action: ".$action."\n");\r
+    }\r
+\r
+    print_footer($course);\r
+\r
+?>\r
index 2d8ffec06a7b32991e8c07068c87b795be09c84b..f7c0435bd304773b3539f1c3a0fa56c6747d6da2 100644 (file)
-<?php  // $Id$
-
-/// Library of extra functions for the dialogue module
-
-// SQL FUNCTIONS ///////////////////////////////////////////////////////////////////
-
-//////////////////////////////////////////////////////////////////////////////////////
-function dialogue_count_all_needing_replies_self($user = '') {
-// count [conversations] needing replies [from] self for all dialogues
-// function requested by Williams Castillo 17 Oct 2003 
-    global $USER;
-    
-    if ($user) {    
-        return count_records_select("dialogue_conversations", "(userid = $user->id OR 
-            recipientid = $user->id) AND lastid != $user->id AND closed = 0");
-    } else {
-        return count_records_select("dialogue_conversations", "(userid = $USER->id OR 
-            recipientid = $USER->id) AND lastid != $USER->id AND closed = 0");
-    }
-}
-
-
-//////////////////////////////////////////////////////////////////////////////////////
-function dialogue_count_closed($dialogue, $user) {
-    
-    return count_records_select("dialogue_conversations", "dialogueid = $dialogue->id AND 
-        (userid = $user->id OR recipientid = $user->id) AND closed = 1");
-    }
-
-
-//////////////////////////////////////////////////////////////////////////////////////
-function dialogue_count_open($dialogue, $user) {
-    
-    return count_records_select("dialogue_conversations", "dialogueid = $dialogue->id AND 
-        (userid = $user->id OR recipientid = $user->id) AND closed = 0");
-}
-
-
-//////////////////////////////////////////////////////////////////////////////////////
-function dialogue_count_needing_replies_other($dialogue, $user) {
-// count [conversations] needing replies [from] other [person]  
-    return count_records_select("dialogue_conversations", "dialogueid = $dialogue->id AND 
-        (userid = $user->id OR recipientid = $user->id) AND lastid = $user->id AND closed = 0");
-}
-
-
-//////////////////////////////////////////////////////////////////////////////////////
-function dialogue_count_needing_replies_self($dialogue, $user) {
-// count [conversations] needing replies [from] self
-    
-    return count_records_select("dialogue_conversations", "dialogueid = $dialogue->id AND 
-        (userid = $user->id OR recipientid = $user->id) AND lastid != $user->id AND closed = 0");
-}
-
-
-//////////////////////////////////////////////////////////////////////////////////////
-function dialogue_get_participants($dialogueid) {
-//Returns the users with data in one dialogue
-//(users with records in dialogue_conversations, creators and receivers)
-
-    global $CFG;
-
-    //Get conversation's creators
-    $creators = get_records_sql("SELECT DISTINCT u.*
-                                FROM {$CFG->prefix}user u,
-                                     {$CFG->prefix}dialogue_conversations c
-                                WHERE c.dialogueid = '$dialogueid' and
-                                      u.id = c.userid");
-
-    //Get conversation's receivers
-    $receivers = get_records_sql("SELECT DISTINCT u.*
-                                FROM {$CFG->prefix}user u,
-                                     {$CFG->prefix}dialogue_conversations c
-                                WHERE c.dialogueid = '$dialogueid' and
-                                      u.id = c.recipientid");
-
-    //Add receivers to creators
-    if ($receivers) {
-        foreach ($receivers as $receiver) {
-            $creators[$receiver->id] = $receiver;
-        }
-    }
-
-    //Return creators array (it contains an array of unique users, creators and receivers)
-    return ($creators);
-} 
-
-//////////////////////////////////////////////////////////////////////////////////////
-function dialogue_get_available_users($dialogue) {
-
-    if (! $course = get_record("course", "id", $dialogue->course)) {
-        error("Course is misconfigured");
-    }
-    switch ($dialogue->dialoguetype) {
-        case 0 : // teacher to student
-            if (isteacher($course->id)) {
-                return dialogue_get_available_students($dialogue);
-            }
-            else {
-                return dialogue_get_available_teachers($dialogue);
-            }
-        case 1: // student to student
-            if (isstudent($course->id)) {
-                return dialogue_get_available_students($dialogue);
-            }
-            else {
-                return;
-            }
-        case 2: // everyone
-            if ($teachers = dialogue_get_available_teachers($dialogue)) {
-                foreach ($teachers as $userid=>$name) {
-                    $names[$userid] = $name;
-                }
-                $names[-1] = "-------------";
-            }
-            if ($students = dialogue_get_available_students($dialogue)) {
-                foreach ($students as $userid=>$name) {
-                    $names[$userid] = $name;
-                }
-            }
-            if (isset($names)) {
-                return $names;
-            }
-            return;
-    }
-}
-
-                    
-//////////////////////////////////////////////////////////////////////////////////////
-function dialogue_get_available_students($dialogue) {
-global $USER;
-    
-    if (! $course = get_record("course", "id", $dialogue->course)) {
-        error("Course is misconfigured");
-    }
-    if (! $cm = get_coursemodule_from_instance("dialogue", $dialogue->id, $course->id)) {
-        error("Course Module ID was incorrect");
-    }
-    
-    $groupid = get_current_group($course->id);
-    // add current group before list of students if it's the teacher
-    if (isteacher($course->id) and groupmode($course, $cm)) {
-        // show teacher their current group
-        if ($groupid) {
-            if (!$group = get_record("groups", "id", $groupid)) {
-                error("Dialogue get available students: group not found");
-            }
-            $gnames["g$groupid"] = $group->name;
-        } else { // all participants
-            $gnames["g0"] = get_string("allparticipants");
-        }
-        $gnames["spacer"] = "------------";
-    }
-    // get the students on this course (default sort order)...
-    if ($users = get_course_students($course->id)) {
-        foreach ($users as $otheruser) {
-            // ...exclude self and...
-            if ($USER->id != $otheruser->id) {
-                // ...if teacher and groups then exclude students not in the current group
-                if (isteacher($course->id) and groupmode($course, $cm) and $groupid) {
-                    if (!ismember($groupid, $otheruser->id)) {
-                        continue;
-                    }
-                }
-                // ...if student and groupmode is SEPARATEGROUPS then exclude students not in student's group
-                if (isstudent($course->id) and (groupmode($course, $cm) == SEPARATEGROUPS)) {
-                    if (!ismember($groupid, $otheruser->id)) {
-                        continue;
-                    }
-                }
-                // ... and any already in any open conversations unless multiple conversations allowed
-                if ($dialogue->multipleconversations or count_records_select("dialogue_conversations", 
-                        "dialogueid = $dialogue->id AND 
-                        ((userid = $USER->id AND recipientid = $otheruser->id) OR 
-                        (userid = $otheruser->id AND recipientid = $USER->id)) AND closed = 0") == 0) {
-                    $names[$otheruser->id] = fullname($otheruser);
-                }
-            }
-        }
-    }
-    if (isset($gnames)) {
-        $list = $gnames;
-    }
-    if (isset($names)) {
-        natcasesort($names);
-        if (isset($list)) {
-            $list += $names;
-        } else {
-            $list = $names;
-        }
-    }
-    if (isset($list)) {
-        return $list;
-    } else {
-        return;
-    }
-}
-
-
-//////////////////////////////////////////////////////////////////////////////////////
-function dialogue_get_available_teachers($dialogue) {
-global $USER;
-    
-    if (! $course = get_record("course", "id", $dialogue->course)) {
-        error("Course is misconfigured");
-        }
-    if (! $cm = get_coursemodule_from_instance("dialogue", $dialogue->id, $course->id)) {
-        error("Course Module ID was incorrect");
-    }
-
-    $groupid = get_current_group($course->id);
-    // get the teachers on this course (default sort order)...
-    if ($users = get_course_teachers($course->id)) {
-        // $names[0] = "-----------------------";
-        foreach ($users as $otheruser) {
-            // ...exclude self and ...
-            if ($USER->id != $otheruser->id) {
-                // ...if groupmode is SEPARATEGROUPS then exclude teachers not in student's group
-                if ($groupid and (groupmode($course, $cm) == SEPARATEGROUPS)) {
-                    if (!ismember($groupid, $otheruser->id)) {
-                        continue;
-                    }
-                }
-                // ...any already in open conversations unless multiple conversations allowed 
-                if ($dialogue->multipleconversations or count_records_select("dialogue_conversations", 
-                        "dialogueid = $dialogue->id AND ((userid = $USER->id AND 
-                        recipientid = $otheruser->id) OR (userid = $otheruser->id AND 
-                        recipientid = $USER->id)) AND closed = 0") == 0) {
-                    $names[$otheruser->id] = fullname($otheruser);
-                }
-            }
-        }
-    }
-    if (isset($names)) {
-        natcasesort($names);
-        return $names;
-    }
-    return;
-}
-
-
-
-//////////////////////////////////////////////////////////////////////////////////////
-function dialogue_get_users_done($dialogue) {
-    global $CFG;
-
-    // make sure it works on the site course
-    $select = "s.course = '$dialogue->course' AND";
-    if ($courseid == SITEID) {
-        $select = '';
-    }
-    if (!$students = get_records_sql("SELECT u.* 
-                              FROM {$CFG->prefix}user u, 
-                                   {$CFG->prefix}user_students s,
-                                   {$CFG->prefix}dialogue_entries j
-                             WHERE ($select s.userid = u.id)
-                               AND u.id = j.userid 
-                               AND j.dialogue = '$dialogue->id'
-                          ORDER BY j.modified DESC")) {
-        $students = array();
-    }
-    if (!$teachers = get_records_sql("SELECT u.* 
-                              FROM {$CFG->prefix}user u, 
-                                   {$CFG->prefix}user_teachers s,
-                                   {$CFG->prefix}dialogue_entries j
-                             WHERE (s.course = '$dialogue->course' AND s.userid = u.id)
-                               AND u.id = j.userid 
-                               AND j.dialogue = '$dialogue->id'
-                          ORDER BY j.modified DESC")) {
-        $teachers = array();
-    }
-    return $teachers + $students;
-                          
-// The following original version is very inefficient on large sites
-//    return get_records_sql("SELECT u.* 
-//                              FROM {$CFG->prefix}user u, 
-//                                   {$CFG->prefix}user_students s, 
-//                                   {$CFG->prefix}user_teachers t, 
-//                                   {$CFG->prefix}dialogue_entries j
-//                             WHERE ((s.course = '$dialogue->course' AND s.userid = u.id) 
-//                                OR  (t.course = '$dialogue->course' AND t.userid = u.id))
-//                               AND u.id = j.userid 
-//                               AND j.dialogue = '$dialogue->id'
-//                          ORDER BY j.modified DESC");
-}
-
-
-// OTHER dialogue FUNCTIONS ///////////////////////////////////////////////////////////////////
-
-//////////////////////////////////////////////////////////////////////////////////////
-function dialogue_list_conversations_closed($dialogue) {
-// list the closed for the current user
-    global $USER;
-    
-    if (! $course = get_record("course", "id", $dialogue->course)) {
-        error("Course is misconfigured");
-    }
-    if (! $cm = get_coursemodule_from_instance("dialogue", $dialogue->id, $course->id)) {
-        error("Course Module ID was incorrect");
-    }
-    
-    if ($conversations = dialogue_get_conversations($dialogue, $USER, "closed = 1")) {
-        // reorder the conversations by (other) name
-        foreach ($conversations as $conversation) {
-            if ($USER->id != $conversation->userid) {
-                if (!$with = get_record("user", "id", $conversation->userid)) {
-                    error("User's record not found");
-                }
-            }
-            else {
-                if (!$with = get_record("user", "id", $conversation->recipientid)) {
-                    error("User's record not found");
-                }
-            }
-            $names[$conversation->id] = fullname($with);
-        }
-        natcasesort($names);
-        
-        print_simple_box_start("center");
-        $table->head = array (get_string("dialoguewith", "dialogue"), get_string("subject", "dialogue"),  
-            get_string("numberofentries", "dialogue"), get_string("lastentry", "dialogue"), 
-            get_string("status", "dialogue"));
-        $table->width = "100%";
-        $table->align = array ("left", "left", "center", "left", "left");
-        $table->size = array ("*", "*", "*", "*", "*");
-        $table->cellpadding = 2;
-        $table->cellspacing = 0;
-
-        foreach ($names as $cid=>$name) {
-            if (!$conversation = get_record("dialogue_conversations", "id", $cid)) {
-                error("Closed conversations: could not find conversation record");
-            }
-            $total = dialogue_count_entries($dialogue, $conversation);
-            $byuser = dialogue_count_entries($dialogue, $conversation, $USER);
-            if ($conversation->closed) {
-                $status = get_string("closed", "dialogue");
-            } else {
-                $status = get_string("open", "dialogue");
-            }
-            $table->data[] = array("<a href=\"dialogues.php?id=$cm->id&amp;action=showdialogues&amp;cid=$conversation->id\">".
-                "$name</a>", $conversation->subject, $byuser." ".get_string("of", "dialogue")." ".$total,
-                userdate($conversation->timemodified), $status);
-            }
-        print_table($table);
-        print_simple_box_end();
-    } 
-}
-
-
-//////////////////////////////////////////////////////////////////////////////////////
-function dialogue_list_conversations_other($dialogue) {
-// list the conversations of the current user awaiting response from the other person
-    global $THEME, $USER;
-    
-    if (!$course = get_record("course", "id", $dialogue->course)) {
-        error("Course is misconfigured");
-    }
-    if (!$cm = get_coursemodule_from_instance("dialogue", $dialogue->id, $course->id)) {
-        error("Course Module ID was incorrect");
-    }
-    
-    $timenow = time();
-    if ($conversations = dialogue_get_conversations($dialogue, $USER, "lastid = $USER->id AND closed = 0")) {
-        // reorder the conversations by (other) name
-        foreach ($conversations as $conversation) {
-            if ($USER->id != $conversation->userid) {
-                if (!$with = get_record("user", "id", $conversation->userid)) {
-                    error("User's record not found");
-                }
-            }
-            else {
-                if (!$with = get_record("user", "id", $conversation->recipientid)) {
-                    error("User's record not found");
-                }
-            }
-            $names[$conversation->id] = fullname($with);
-        }
-        natcasesort($names);
-        
-        print_simple_box_start("center");
-        $table->head = array (get_string("dialoguewith", "dialogue"), get_string("subject", "dialogue"),  
-            get_string("numberofentries", "dialogue"), get_string("lastentry", "dialogue"), 
-            get_string("status", "dialogue"));
-        $table->width = "100%";
-        $table->align = array ("left", "left", "center", "left", "left");
-        $table->size = array ("*", "*", "*", "*", "*");
-        $table->cellpadding = 2;
-        $table->cellspacing = 0;
-
-        foreach ($names as $cid=>$name) {
-            if (!$conversation = get_record("dialogue_conversations", "id", $cid)) {
-                error("Closed conversations: could not find conversation record");
-            }
-            $total = dialogue_count_entries($dialogue, $conversation);
-            $byuser = dialogue_count_entries($dialogue, $conversation, $USER);
-            if ($conversation->seenon) {
-                $status = get_string("seen", "dialogue", format_time($timenow - $conversation->seenon));
-            } else {
-                $status = get_string("notyetseen", "dialogue");
-            }
-            $table->data[] = array("<a href=\"dialogues.php?id=$cm->id&amp;action=printdialogue&amp;cid=$conversation->id\">".
-                "$name</a>", $conversation->subject, $byuser." ".get_string("of", "dialogue")." ".$total,
-                userdate($conversation->timemodified), $status);
-            }
-        print_table($table);
-        print_simple_box_end();
-    } 
-}
-
-
-//////////////////////////////////////////////////////////////////////////////////////
-function dialogue_list_conversations_self($dialogue) {
-// list open conversations of the current user awaiting their reply
-    global $THEME, $USER;
-    
-    if (! $course = get_record("course", "id", $dialogue->course)) {
-        error("Course is misconfigured");
-    }
-    if (! $cm = get_coursemodule_from_instance("dialogue", $dialogue->id, $course->id)) {
-        error("Course Module ID was incorrect");
-    }
-    
-    // set up some general variables
-    $usehtmleditor = can_use_html_editor();
-
-    $timenow = time();
-    $showbutton = false;
-    
-    echo "<form name=\"replies\" method=\"post\" action=\"dialogues.php\">\n";
-    echo "<input type=\"hidden\" name=\"action\" value=\"insertentries\"/>\n";
-    echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />\n";
-    echo "<input type=\"hidden\" name=\"pane\" value=\"1\" />\n";
-
-    // list the conversations requiring a resonse from this user in full
-    if ($conversations = dialogue_get_conversations($dialogue, $USER, "lastid != $USER->id AND closed = 0")) {
-        $showbutton = true;
-        print_simple_box_start("center");
-        foreach ($conversations as $conversation) {
-            // set seenon if required
-            if (!$conversation->seenon) {
-                if (!set_field("dialogue_conversations", "seenon", $timenow, "id", $conversation->id)) {
-                    error("List conversations self: could not set seenon");
-                }
-            }
-            echo "<table align=\"center\" border=\"1\" cellspacing=\"0\" valign=\"top\" cellpadding=\"4\" 
-                width=\"100%\">\n";
-            echo "<tr><td bgcolor=\"$THEME->cellheading2\" valign=\"top\">\n";
-            if ($conversation->userid == $USER->id) {
-                if (!$otheruser = get_record("user", "id", $conversation->recipientid)) {
-                    error("User not found");
-                }
-            }
-            else {
-                if (!$otheruser = get_record("user", "id", $conversation->userid)) {
-                    error("User not found");
-                }
-            }
-            // print_user_picture($user->id, $course->id, $user->picture);
-            echo "<b>".get_string("dialoguewith", "dialogue", fullname($otheruser)).
-                "</b></td>";
-            echo "<td bgcolor=\"$THEME->cellheading2\"><i>$conversation->subject&nbsp;</i><br />\n";
-            echo "<div align=\"right\">\n";
-            if (!$conversation->subject) {
-                // conversation does not have a subject, show add subject link
-                echo "<a href=\"dialogues.php?action=getsubject&amp;id=$cm->id&amp;cid=$conversation->id&amp;pane=2\">".
-                    get_string("addsubject", "dialogue")."</a>\n";
-                helpbutton("addsubject", get_string("addsubject", "dialogue"), "dialogue");
-                echo "&nbsp; | ";
-            }
-            if (dialogue_count_entries($dialogue, $conversation)) {
-                echo "<a href=\"dialogues.php?action=confirmclose&amp;id=$cm->id&amp;cid=$conversation->id&amp;pane=1\">".
-                    get_string("close", "dialogue")."</a>\n";
-                helpbutton("closedialogue", get_string("close", "dialogue"), "dialogue");
-            }
-            else {
-                echo "&nbsp;";
-            }
-            echo "<div></td></tr>";
-        
-            if ($entries = get_records_select("dialogue_entries", "conversationid = $conversation->id", 
-                    "id")) {
-                foreach ($entries as $entry) {
-                    if ($entry->userid == $USER->id) {
-                        echo "<tr><td colspan=\"2\" bgcolor=\"#FFFFFF\">\n";
-                        echo text_to_html("<font size=\"1\">".get_string("onyouwrote", "dialogue", 
-                            userdate($entry->timecreated)).":</font><br />".$entry->text);
-                        echo "</td></tr>\n";
-                    }
-                    else {
-                        echo "<tr><td colspan=\"2\" bgcolor=\"$THEME->body\">\n";
-                        echo text_to_html("<font size=\"1\">".get_string("onwrote", "dialogue", 
-                               userdate($entry->timecreated)." ".$otheruser->firstname).
-                               ":</font><br />".$entry->text);
-                        echo "</td></tr>\n";
-                    }
-                }
-            }
-        
-            echo "<tr><td colspan=\"2\" align=\"center\" valign=\"top\">\n";
-            if ($entries) {
-                echo "<i>".get_string("typereply", "dialogue")."</i>\n";
-            }
-            else {
-                echo "<i>".get_string("typefirstentry", "dialogue")."</i>\n";
-            }
-            echo "</td></tr>\n";
-            echo "<tr><td valign=\"top\" align=\"right\">\n";
-            helpbutton("writing", get_string("helpwriting"), "moodle", true, true);
-            echo "<br />";
-            echo "</td><td>\n";
-            // use a cumbersome name on the textarea is just historical :-) 
-            print_textarea($usehtmleditor, 20, 75, 630, 300, "reply$conversation->id");
-            echo "</td></tr>";
-            echo "</table><br />\n";
-        }
-        print_simple_box_end();
-        use_html_editor();
-        if ($showbutton) {
-            echo "<hr />\n";
-            echo "<p align=\"center\"><input type=\"submit\" value=\"".get_string("addmynewentries", "dialogue").
-                "\"/></p>\n";
-        }
-        echo "</form>\n";
-    }
-}
-
-
-//////////////////////////////////////////////////////////////////////////////////////
-function dialogue_print_conversation($dialogue, $conversation) {
-// print a conversation and allow a new entry
-    global $THEME, $USER;
-    
-    if (! $course = get_record("course", "id", $dialogue->course)) {
-        error("Course is misconfigured");
-    }
-    if (! $cm = get_coursemodule_from_instance("dialogue", $dialogue->id, $course->id)) {
-        error("Course Module ID was incorrect");
-    }
-    
-    $timenow = time();
-    $showbutton = false;
-    
-    echo "<form name=\"replies\" method=\"post\" action=\"dialogues.php\">\n";
-    echo "<input type=\"hidden\" name=\"action\" value=\"insertentries\"/>\n";
-    echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />\n";
-    echo "<input type=\"hidden\" name=\"pane\" value=\"2\" />\n";
-
-    $showbutton = true;
-    print_simple_box_start("center", "", $THEME->cellcontent2);
-    echo "<table align=\"center\" border=\"1\" cellspacing=\"0\" valign=\"top\" cellpadding=\"4\" 
-        width=\"100%\">\n";
-    echo "<tr><td bgcolor=\"$THEME->cellheading2\" valign=\"top\">\n";
-    if ($conversation->userid == $USER->id) {
-        if (!$otheruser = get_record("user", "id", $conversation->recipientid)) {
-            error("User not found");
-        }
-    }
-    else {
-        if (!$otheruser = get_record("user", "id", $conversation->userid)) {
-            error("User not found");
-        }
-    }
-    // print_user_picture($user->id, $course->id, $user->picture);
-    echo "<b>".get_string("dialoguewith", "dialogue", fullname($otheruser)).
-        "</b></td>";
-    echo "<td bgcolor=\"$THEME->cellheading2\"><i>$conversation->subject&nbsp;</i><br />\n";
-    echo "<div align=\"right\">\n";
-    if (!$conversation->subject) {
-        // conversation does not have a subject, show add subject link
-        echo "<a href=\"dialogues.php?action=getsubject&amp;id=$cm->id&amp;cid=$conversation->id&amp;pane=2\">".
-            get_string("addsubject", "dialogue")."</a>\n";
-        helpbutton("addsubject", get_string("addsubject", "dialogue"), "dialogue");
-        echo "&nbsp; | ";
-    }
-    echo "<a href=\"dialogues.php?action=confirmclose&amp;id=$cm->id&amp;cid=$conversation->id&amp;pane=2\">".
-        get_string("close", "dialogue")."</a>\n";
-    helpbutton("closedialogue", get_string("close", "dialogue"), "dialogue");
-    echo "</div></td></tr>\n";
-
-    if ($entries = get_records_select("dialogue_entries", "conversationid = $conversation->id", "id")) {
-        foreach ($entries as $entry) {
-            if ($entry->userid == $USER->id) {
-                echo "<tr><td colspan=\"2\" bgcolor=\"#FFFFFF\">\n";
-                echo text_to_html("<font size=\"1\">".get_string("onyouwrote", "dialogue", 
-                            userdate($entry->timecreated)).":</font><br />".$entry->text);
-            }
-            else {
-                echo "<tr><td colspan=\"2\" bgcolor=\"$THEME->body\">\n";
-                echo text_to_html("<font size=\"1\">".get_string("onwrote", "dialogue", 
-                            userdate($entry->timecreated)." ".$otheruser->firstname).":</font><br />".
-                        $entry->text);
-            }
-        }
-        echo "</td></tr>\n";
-    }
-    echo "<tr><td colspan=\"2\" align=\"center\" valign=\"top\"><i>".
-        get_string("typefollowup", "dialogue")."</i></td></tr>\n";
-    echo "<tr><td valign=\"top\" align=\"right\">\n";
-    helpbutton("writing", get_string("helpwriting"), "moodle", true, true);
-    echo "<br />";
-    echo "</td><td>\n";
-    // use a cumbersome name on the textarea for historical reasons :-) 
-    $usehtmleditor = can_use_html_editor();
-    print_textarea($usehtmleditor, 20, 75, 630, 300, "reply$conversation->id");
-    use_html_editor();
-    echo "</td></tr>";
-    echo "</table><br />\n";
-    print_simple_box_end();
-    if ($showbutton) {
-        echo "<hr />\n";
-        echo "<br /><input type=\"submit\" value=\"".get_string("addmynewentry", "dialogue")."\" />\n";
-    }
-    echo "</form>\n";
-}
-
-
-//////////////////////////////////////////////////////////////////////////////////////
-function dialogue_print_tabbed_heading($tabs) {
-// Prints a tabbed heading where one of the tabs highlighted.
-// $tabs is an object with several properties.
-//      $tabs->names      is an array of tab names
-//      $tabs->urls       is an array of links
-//      $tabs->align     is an array of column alignments (defaults to "center")
-//      $tabs->size      is an array of column sizes
-//      $tabs->wrap      is an array of "nowrap"s or nothing
-//      $tabs->highlight    is an index (zero based) of "active" heading .
-//      $tabs->width     is an percentage of the page (defualts to 80%)
-//      $tabs->cellpadding    padding on each cell (defaults to 5)
-
-    global $CFG, $THEME;
-    
-    if (isset($tabs->names)) {
-        foreach ($tabs->names as $key => $name) {
-            if (!empty($tabs->urls[$key])) {
-                $url =$tabs->urls[$key];
-                if ($tabs->highlight == $key) {
-                    $tabcontents[$key] = "<b>$name</b>";
-                } else {
-                    $tabcontents[$key] = "<a class= \"dimmed\" href=\"$url\"><b>$name</b></a>";
-                }
-            } else {
-                $tabcontents[$key] = "<b>$name</b>";
-            }
-        }
-    }
-
-    if (empty($tabs->width)) {
-        $tabs->width = "80%";
-    }
-
-    if (empty($tabs->cellpadding)) {
-        $tabs->cellpadding = "5";
-    }
-
-    // print_simple_box_start("center", "$table->width", "#ffffff", 0);
-    echo "<table width=\"$tabs->width\" border=\"0\" valign=\"top\" align=\"center\" ";
-    echo " cellpadding=\"$tabs->cellpadding\" cellspacing=\"0\" class=\"generaltable\">\n";
-
-    if (!empty($tabs->names)) {
-        echo "<tr>";
-        echo "<td  class=\"generaltablecell\">".
-            "<img width=\"10\" src=\"$CFG->wwwroot/pix/spacer.gif\" alt=\"\" /></td>\n";
-        foreach ($tabcontents as $key => $tab) {
-            if (isset($align[$key])) {
-                $alignment = "align=\"$align[$key]\"";
-            } else {
-                $alignment = "align=\"center\"";
-            }
-            if (isset($size[$key])) {
-                $width = "width=\"$size[$key]\"";
-            } else {
-                $width = "";
-            }
-            if (isset($wrap[$key])) {
-                $wrapping = "no wrap";
-            } else {
-                $wrapping = "";
-            }
-            if ($key == $tabs->highlight) {
-                echo "<td valign=top class=\"generaltabselected\" $alignment $width $wrapping bgcolor=\"$THEME->cellheading2\">$tab</td>\n";
-            } else {
-                echo "<td valign=top class=\"generaltab\" $alignment $width $wrapping bgcolor=\"$THEME->cellheading\">$tab</td>\n";
-            }
-        echo "<td  class=\"generaltablecell\">".
-            "<img width=\"10\" src=\"$CFG->wwwroot/pix/spacer.gif\" alt=\"\" /></td>\n";
-        }
-        echo "</tr>\n";
-    } else {
-        echo "<tr><td>No names specified</td></tr>\n";
-    }
-    // bottom stripe
-    $ncells = count($tabs->names)*2 +1;
-    $height = 2;
-    echo "<tr><td colspan=\"$ncells\" bgcolor=\"$THEME->cellheading2\">".
-        "<img height=\"$height\" src=\"$CFG->wwwroot/pix/spacer.gif\" alt=\"\" /></td></tr>\n";
-    echo "</table>\n";
-    // print_simple_box_end();
-
-    return true;
-}
-
-
-
-//////////////////////////////////////////////////////////////////////////////////////
-function dialogue_show_conversation($dialogue, $conversation) {
-    global $THEME, $USER;
-    
-    if (! $course = get_record("course", "id", $dialogue->course)) {
-        error("Course is misconfigured");
-        }
-    if (! $cm = get_coursemodule_from_instance("dialogue", $dialogue->id, $course->id)) {
-        error("Course Module ID was incorrect");
-    }
-    
-    $timenow = time();
-    print_simple_box_start("center");
-    echo "<table border=\"1\" cellspacing=\"0\" valign=\"top\" cellpadding=\"4\" width=\"100%\">\n";
-        
-    echo "<tr>";
-    echo "<td bgcolor=\"$THEME->cellheading2\" valign=\"top\">\n";
-    if ($conversation->userid == $USER->id) {
-        if (!$otheruser = get_record("user", "id", $conversation->recipientid)) {
-            error("User not found");
-        }
-    }
-    else {
-        if (!$otheruser = get_record("user", "id", $conversation->userid)) {
-            error("User not found");
-        }
-    }
-    // print_user_picture($user->id, $course->id, $user->picture);
-    echo "<b>".get_string("dialoguewith", "dialogue", fullname($otheruser)).
-        "</b></td>";
-    echo "<td bgcolor=\"$THEME->cellheading2\" valign=\"top\"><i>$conversation->subject&nbsp;</i></td></tr>";
-
-    if ($entries = get_records_select("dialogue_entries", "conversationid = $conversation->id", "id")) {
-        foreach ($entries as $entry) {
-            if ($entry->userid == $USER->id) {
-                echo "<tr><td  colspan=\"2\" bgcolor=\"#FFFFFF\">\n";
-                echo text_to_html("<font size=\"1\">".get_string("onyouwrote", "dialogue", 
-                    userdate($entry->timecreated)).
-                    ":</font><br />".$entry->text);
-                echo "</td></tr>\n";
-            }
-            else {
-                echo "<tr><td  colspan=\"2\" bgcolor=\"$THEME->body\">\n";
-                echo text_to_html("<font size=\"1\">".get_string("onwrote", "dialogue", 
-                    userdate($entry->timecreated)." ".$otheruser->firstname).":</font><br />".$entry->text);
-                echo "</td></tr>\n";
-            }
-        }
-    }
-    echo "</table><br />\n";
-    print_simple_box_end();
-    print_continue("view.php?id=$cm->id&amp;pane=3");
-}
-
-
-//////////////////////////////////////////////////////////////////////////////////////
-function dialogue_show_other_conversations($dialogue, $conversation) {
-// prints the other CLOSED conversations for this pair of users
-    global $THEME;
-    
-    if (! $course = get_record("course", "id", $dialogue->course)) {
-        error("Course is misconfigured");
-        }
-    if (! $cm = get_coursemodule_from_instance("dialogue", $dialogue->id, $course->id)) {
-        error("Course Module ID was incorrect");
-    }
-    
-    if (!$user = get_record("user", "id", $conversation->userid)) {
-        error("User not found");
-        }
-    if (!$otheruser = get_record("user", "id", $conversation->recipientid)) {
-        error("User not found");
-        }
-    
-    if ($conversations = get_records_select("dialogue_conversations", "dialogueid = $dialogue->id AND 
-            ((userid = $user->id AND recipientid = $otheruser->id) OR (userid = $otheruser->id AND 
-            recipientid = $user->id)) AND closed = 1", "timemodified DESC")) {
-        if (count($conversations) > 1) {
-            $timenow = time();
-            foreach ($conversations as $otherconversation) {
-                if ($conversation->id != $otherconversation->id) {
-                    // for this conversation work out which is the other user
-                    if ($otherconversation->userid == $user->id) {
-                        if (!$otheruser = get_record("user", "id", $otherconversation->recipientid)) {
-                            error("Show other conversations: could not get user record");
-                        }
-                    }
-                    else {
-                        if (!$otheruser = get_record("user", "id", $otherconversation->userid)) {
-                            error("Show other conversations: could not get user record");
-                        }
-                    } 
-                    print_simple_box_start("center");
-                    echo "<table align=\"center\" border=\"1\" cellspacing=\"0\" valign=\"top\" cellpadding=\"4\"
-                         width=\"100%\">\n";
-                    echo "<tr>";
-                    echo "<td bgcolor=\"$THEME->cellheading2\" valign=\"top\">\n";
-                    // print_user_picture($otheruser->id, $course->id, $otheruser->picture);
-                    echo "<b>".get_string("dialoguewith", "dialogue", 
-                        fullname($otheruser))."</b></td>";
-                    echo "<td bgcolor=\"$THEME->cellheading2\" valign=\"top\"><i>$otherconversation->subject&nbsp;</i></td></tr>";
-                    if ($entries = get_records_select("dialogue_entries", 
-                            "conversationid = $otherconversation->id", "id")) {
-                        foreach ($entries as $entry) {
-                            if ($entry->userid == $user->id) {
-                                echo "<tr><td  colspan=\"2\" bgcolor=\"#FFFFFF\">\n";
-                                echo text_to_html("<font size=\"1\">".get_string("onyouwrote", "dialogue", 
-                                    userdate($entry->timecreated)).":</font><br />".$entry->text);
-                                echo "</td></tr>\n";
-                            }
-                            else {
-                                echo "<tr><td  colspan=\"2\" bgcolor=\"$THEME->body\">\n";
-                                echo text_to_html("<font size=\"1\">".get_string("onwrote", "dialogue", 
-                                    userdate($entry->timecreated)." ".$otheruser->firstname).
-                                    ":</font><br />".$entry->text);
-                                echo "</td></tr>\n";
-                            }
-                        }
-                    }
-                
-                    echo "</table><br />\n";
-                    print_simple_box_end();
-                }
-            }
-            print_continue("view.php?id=$cm->id&amp;pane=3");
-        }
-    }
-}
-
-
-?>
+<?php  // $Id$\r
+\r
+/// Library of extra functions for the dialogue module\r
+\r
+// SQL FUNCTIONS ///////////////////////////////////////////////////////////////////\r
+\r
+//////////////////////////////////////////////////////////////////////////////////////\r
+function dialogue_count_all_needing_replies_self($user = '') {\r
+// count [conversations] needing replies [from] self for all dialogues\r
+// function requested by Williams Castillo 17 Oct 2003 \r
+    global $USER;\r
+    \r
+    if ($user) {    \r
+        return count_records_select("dialogue_conversations", "(userid = $user->id OR \r
+            recipientid = $user->id) AND lastid != $user->id AND closed = 0");\r
+    } else {\r
+        return count_records_select("dialogue_conversations", "(userid = $USER->id OR \r
+            recipientid = $USER->id) AND lastid != $USER->id AND closed = 0");\r
+    }\r
+}\r
+\r
+\r
+//////////////////////////////////////////////////////////////////////////////////////\r
+function dialogue_count_closed($dialogue, $user) {\r
+    \r
+    return count_records_select("dialogue_conversations", "dialogueid = $dialogue->id AND \r
+        (userid = $user->id OR recipientid = $user->id) AND closed = 1");\r
+    }\r
+\r
+\r
+//////////////////////////////////////////////////////////////////////////////////////\r
+function dialogue_count_open($dialogue, $user) {\r
+    \r
+    return count_records_select("dialogue_conversations", "dialogueid = $dialogue->id AND \r
+        (userid = $user->id OR recipientid = $user->id) AND closed = 0");\r
+}\r
+\r
+\r
+//////////////////////////////////////////////////////////////////////////////////////\r
+function dialogue_count_needing_replies_other($dialogue, $user) {\r
+// count [conversations] needing replies [from] other [person]  \r
+    return count_records_select("dialogue_conversations", "dialogueid = $dialogue->id AND \r
+        (userid = $user->id OR recipientid = $user->id) AND lastid = $user->id AND closed = 0");\r
+}\r
+\r
+\r
+//////////////////////////////////////////////////////////////////////////////////////\r
+function dialogue_count_needing_replies_self($dialogue, $user) {\r
+// count [conversations] needing replies [from] self\r
+    \r
+    return count_records_select("dialogue_conversations", "dialogueid = $dialogue->id AND \r
+        (userid = $user->id OR recipientid = $user->id) AND lastid != $user->id AND closed = 0");\r
+}\r
+\r
+\r
+//////////////////////////////////////////////////////////////////////////////////////\r
+function dialogue_get_participants($dialogueid) {\r
+//Returns the users with data in one dialogue\r
+//(users with records in dialogue_conversations, creators and receivers)\r
+\r
+    global $CFG;\r
+\r
+    //Get conversation's creators\r
+    $creators = get_records_sql("SELECT DISTINCT u.*\r
+                                FROM {$CFG->prefix}user u,\r
+                                     {$CFG->prefix}dialogue_conversations c\r
+                                WHERE c.dialogueid = '$dialogueid' and\r
+                                      u.id = c.userid");\r
+\r
+    //Get conversation's receivers\r
+    $receivers = get_records_sql("SELECT DISTINCT u.*\r
+                                FROM {$CFG->prefix}user u,\r
+                                     {$CFG->prefix}dialogue_conversations c\r
+                                WHERE c.dialogueid = '$dialogueid' and\r
+                                      u.id = c.recipientid");\r
+\r
+    //Add receivers to creators\r
+    if ($receivers) {\r
+        foreach ($receivers as $receiver) {\r
+            $creators[$receiver->id] = $receiver;\r
+        }\r
+    }\r
+\r
+    //Return creators array (it contains an array of unique users, creators and receivers)\r
+    return ($creators);\r
+} \r
+\r
+//////////////////////////////////////////////////////////////////////////////////////\r
+function dialogue_get_available_users($dialogue) {\r
+\r
+    if (! $course = get_record("course", "id", $dialogue->course)) {\r
+        error("Course is misconfigured");\r
+    }\r
+    switch ($dialogue->dialoguetype) {\r
+        case 0 : // teacher to student\r
+            if (isteacher($course->id)) {\r
+                return dialogue_get_available_students($dialogue);\r
+            }\r
+            else {\r
+                return dialogue_get_available_teachers($dialogue);\r
+            }\r
+        case 1: // student to student\r
+            if (isstudent($course->id)) {\r
+                return dialogue_get_available_students($dialogue);\r
+            }\r
+            else {\r
+                return;\r
+            }\r
+        case 2: // everyone\r
+            if ($teachers = dialogue_get_available_teachers($dialogue)) {\r
+                foreach ($teachers as $userid=>$name) {\r
+                    $names[$userid] = $name;\r
+                }\r
+                $names[-1] = "-------------";\r
+            }\r
+            if ($students = dialogue_get_available_students($dialogue)) {\r
+                foreach ($students as $userid=>$name) {\r
+                    $names[$userid] = $name;\r
+                }\r
+            }\r
+            if (isset($names)) {\r
+                return $names;\r
+            }\r
+            return;\r
+    }\r
+}\r
+\r
+                    \r
+//////////////////////////////////////////////////////////////////////////////////////\r
+function dialogue_get_available_students($dialogue) {\r
+global $USER;\r
+    \r
+    if (! $course = get_record("course", "id", $dialogue->course)) {\r
+        error("Course is misconfigured");\r
+    }\r
+    if (! $cm = get_coursemodule_from_instance("dialogue", $dialogue->id, $course->id)) {\r
+        error("Course Module ID was incorrect");\r
+    }\r
+    \r
+    $groupid = get_current_group($course->id);\r
+    // add current group before list of students if it's the teacher\r
+    if (isteacher($course->id) and groupmode($course, $cm)) {\r
+        // show teacher their current group\r
+        if ($groupid) {\r
+            if (!$group = get_record("groups", "id", $groupid)) {\r
+                error("Dialogue get available students: group not found");\r
+            }\r
+            $gnames["g$groupid"] = $group->name;\r
+        } else { // all participants\r
+            $gnames["g0"] = get_string("allparticipants");\r
+        }\r
+        $gnames["spacer"] = "------------";\r
+    }\r
+    // get the students on this course (default sort order)...\r
+    if ($users = get_course_students($course->id)) {\r
+        foreach ($users as $otheruser) {\r
+            // ...exclude self and...\r
+            if ($USER->id != $otheruser->id) {\r
+                // ...if teacher and groups then exclude students not in the current group\r
+                if (isteacher($course->id) and groupmode($course, $cm) and $groupid) {\r
+                    if (!ismember($groupid, $otheruser->id)) {\r
+                        continue;\r
+                    }\r
+                }\r
+                // ...if student and groupmode is SEPARATEGROUPS then exclude students not in student's group\r
+                if (isstudent($course->id) and (groupmode($course, $cm) == SEPARATEGROUPS)) {\r
+                    if (!ismember($groupid, $otheruser->id)) {\r
+                        continue;\r
+                    }\r
+                }\r
+                // ... and any already in any open conversations unless multiple conversations allowed\r
+                if ($dialogue->multipleconversations or count_records_select("dialogue_conversations", \r
+                        "dialogueid = $dialogue->id AND \r
+                        ((userid = $USER->id AND recipientid = $otheruser->id) OR \r
+                        (userid = $otheruser->id AND recipientid = $USER->id)) AND closed = 0") == 0) {\r
+                    $names[$otheruser->id] = fullname($otheruser);\r
+                }\r
+            }\r
+        }\r
+    }\r
+    if (isset($gnames)) {\r
+        $list = $gnames;\r
+    }\r
+    if (isset($names)) {\r
+        natcasesort($names);\r
+        if (isset($list)) {\r
+            $list += $names;\r
+        } else {\r
+            $list = $names;\r
+        }\r
+    }\r
+    if (isset($list)) {\r
+        return $list;\r
+    } else {\r
+        return;\r
+    }\r
+}\r
+\r
+\r
+//////////////////////////////////////////////////////////////////////////////////////\r
+function dialogue_get_available_teachers($dialogue) {\r
+global $USER;\r
+    \r
+    if (! $course = get_record("course", "id", $dialogue->course)) {\r
+        error("Course is misconfigured");\r
+        }\r
+    if (! $cm = get_coursemodule_from_instance("dialogue", $dialogue->id, $course->id)) {\r
+        error("Course Module ID was incorrect");\r
+    }\r
+\r
+    $groupid = get_current_group($course->id);\r
+    // get the teachers on this course (default sort order)...\r
+    if ($users = get_course_teachers($course->id)) {\r
+        // $names[0] = "-----------------------";\r
+        foreach ($users as $otheruser) {\r
+            // ...exclude self and ...\r
+            if ($USER->id != $otheruser->id) {\r
+                // ...if groupmode is SEPARATEGROUPS then exclude teachers not in student's group\r
+                if ($groupid and (groupmode($course, $cm) == SEPARATEGROUPS)) {\r
+                    if (!ismember($groupid, $otheruser->id)) {\r
+                        continue;\r
+                    }\r
+                }\r
+                // ...any already in open conversations unless multiple conversations allowed \r
+                if ($dialogue->multipleconversations or count_records_select("dialogue_conversations", \r
+                        "dialogueid = $dialogue->id AND ((userid = $USER->id AND \r
+                        recipientid = $otheruser->id) OR (userid = $otheruser->id AND \r
+                        recipientid = $USER->id)) AND closed = 0") == 0) {\r
+                    $names[$otheruser->id] = fullname($otheruser);\r
+                }\r
+            }\r
+        }\r
+    }\r
+    if (isset($names)) {\r
+        natcasesort($names);\r
+        return $names;\r
+    }\r
+    return;\r
+}\r
+\r
+\r
+\r
+//////////////////////////////////////////////////////////////////////////////////////\r
+function dialogue_get_users_done($dialogue) {\r
+    global $CFG;\r
+\r
+    // make sure it works on the site course\r
+    $select = "s.course = '$dialogue->course' AND";\r
+    if ($courseid == SITEID) {\r
+        $select = '';\r
+    }\r
+    if (!$students = get_records_sql("SELECT u.* \r
+                              FROM {$CFG->prefix}user u, \r
+                                   {$CFG->prefix}user_students s,\r
+                                   {$CFG->prefix}dialogue_entries j\r
+                             WHERE ($select s.userid = u.id)\r
+                               AND u.id = j.userid \r
+                               AND j.dialogue = '$dialogue->id'\r
+                          ORDER BY j.modified DESC")) {\r
+        $students = array();\r
+    }\r
+    if (!$teachers = get_records_sql("SELECT u.* \r
+                              FROM {$CFG->prefix}user u, \r
+                                   {$CFG->prefix}user_teachers s,\r
+                                   {$CFG->prefix}dialogue_entries j\r
+                             WHERE (s.course = '$dialogue->course' AND s.userid = u.id)\r
+                               AND u.id = j.userid \r
+                               AND j.dialogue = '$dialogue->id'\r
+                          ORDER BY j.modified DESC")) {\r
+        $teachers = array();\r
+    }\r
+    return $teachers + $students;\r
+                          \r
+// The following original version is very inefficient on large sites\r
+//    return get_records_sql("SELECT u.* \r
+//                              FROM {$CFG->prefix}user u, \r
+//                                   {$CFG->prefix}user_students s, \r
+//                                   {$CFG->prefix}user_teachers t, \r
+//                                   {$CFG->prefix}dialogue_entries j\r
+//                             WHERE ((s.course = '$dialogue->course' AND s.userid = u.id) \r
+//                                OR  (t.course = '$dialogue->course' AND t.userid = u.id))\r
+//                               AND u.id = j.userid \r
+//                               AND j.dialogue = '$dialogue->id'\r
+//                          ORDER BY j.modified DESC");\r
+}\r
+\r
+\r
+// OTHER dialogue FUNCTIONS ///////////////////////////////////////////////////////////////////\r
+\r
+//////////////////////////////////////////////////////////////////////////////////////\r
+function dialogue_list_conversations_closed($dialogue) {\r
+// list the closed for the current user\r
+    global $USER;\r
+    \r
+    if (! $course = get_record("course", "id", $dialogue->course)) {\r
+        error("Course is misconfigured");\r
+    }\r
+    if (! $cm = get_coursemodule_from_instance("dialogue", $dialogue->id, $course->id)) {\r
+        error("Course Module ID was incorrect");\r
+    }\r
+    \r
+    if ($conversations = dialogue_get_conversations($dialogue, $USER, "closed = 1")) {\r
+        // reorder the conversations by (other) name\r
+        foreach ($conversations as $conversation) {\r
+            if ($USER->id != $conversation->userid) {\r
+                if (!$with = get_record("user", "id", $conversation->userid)) {\r
+                    error("User's record not found");\r
+                }\r
+            }\r
+            else {\r
+                if (!$with = get_record("user", "id", $conversation->recipientid)) {\r
+                    error("User's record not found");\r
+                }\r
+            }\r
+            $names[$conversation->id] = fullname($with);\r
+        }\r
+        natcasesort($names);\r
+        \r
+        print_simple_box_start("center");\r
+        $table->head = array (get_string("dialoguewith", "dialogue"), get_string("subject", "dialogue"),  \r
+            get_string("numberofentries", "dialogue"), get_string("lastentry", "dialogue"), \r
+            get_string("status", "dialogue"));\r
+        $table->width = "100%";\r
+        $table->align = array ("left", "left", "center", "left", "left");\r
+        $table->size = array ("*", "*", "*", "*", "*");\r
+        $table->cellpadding = 2;\r
+        $table->cellspacing = 0;\r
+\r
+        foreach ($names as $cid=>$name) {\r
+            if (!$conversation = get_record("dialogue_conversations", "id", $cid)) {\r
+                error("Closed conversations: could not find conversation record");\r
+            }\r
+            $total = dialogue_count_entries($dialogue, $conversation);\r
+            $byuser = dialogue_count_entries($dialogue, $conversation, $USER);\r
+            if ($conversation->closed) {\r
+                $status = get_string("closed", "dialogue");\r
+            } else {\r
+                $status = get_string("open", "dialogue");\r
+            }\r
+            $table->data[] = array("<a href=\"dialogues.php?id=$cm->id&amp;action=showdialogues&amp;cid=$conversation->id\">".\r
+                "$name</a>", clean_text($conversation->subject), $byuser." ".get_string("of", "dialogue")." ".$total,\r
+                userdate($conversation->timemodified), $status);\r
+            }\r
+        print_table($table);\r
+        print_simple_box_end();\r
+    } \r
+}\r
+\r
+\r
+//////////////////////////////////////////////////////////////////////////////////////\r
+function dialogue_list_conversations_other($dialogue) {\r
+// list the conversations of the current user awaiting response from the other person\r
+    global $THEME, $USER;\r
+    \r
+    if (!$course = get_record("course", "id", $dialogue->course)) {\r
+        error("Course is misconfigured");\r
+    }\r
+    if (!$cm = get_coursemodule_from_instance("dialogue", $dialogue->id, $course->id)) {\r
+        error("Course Module ID was incorrect");\r
+    }\r
+    \r
+    $timenow = time();\r
+    if ($conversations = dialogue_get_conversations($dialogue, $USER, "lastid = $USER->id AND closed = 0")) {\r
+        // reorder the conversations by (other) name\r
+        foreach ($conversations as $conversation) {\r
+            if ($USER->id != $conversation->userid) {\r
+                if (!$with = get_record("user", "id", $conversation->userid)) {\r
+                    error("User's record not found");\r
+                }\r
+            }\r
+            else {\r
+                if (!$with = get_record("user", "id", $conversation->recipientid)) {\r
+                    error("User's record not found");\r
+                }\r
+            }\r
+            $names[$conversation->id] = fullname($with);\r
+        }\r
+        natcasesort($names);\r
+        \r
+        print_simple_box_start("center");\r
+        $table->head = array (get_string("dialoguewith", "dialogue"), get_string("subject", "dialogue"),  \r
+            get_string("numberofentries", "dialogue"), get_string("lastentry", "dialogue"), \r
+            get_string("status", "dialogue"));\r
+        $table->width = "100%";\r
+        $table->align = array ("left", "left", "center", "left", "left");\r
+        $table->size = array ("*", "*", "*", "*", "*");\r
+        $table->cellpadding = 2;\r
+        $table->cellspacing = 0;\r
+\r
+        foreach ($names as $cid=>$name) {\r
+            if (!$conversation = get_record("dialogue_conversations", "id", $cid)) {\r
+                error("Closed conversations: could not find conversation record");\r
+            }\r
+            $total = dialogue_count_entries($dialogue, $conversation);\r
+            $byuser = dialogue_count_entries($dialogue, $conversation, $USER);\r
+            if ($conversation->seenon) {\r
+                $status = get_string("seen", "dialogue", format_time($timenow - $conversation->seenon));\r
+            } else {\r
+                $status = get_string("notyetseen", "dialogue");\r
+            }\r
+            $table->data[] = array("<a href=\"dialogues.php?id=$cm->id&action=printdialogue&cid=$conversation->id\">".\r
+                "$name</a>", clean_text($conversation->subject), $byuser." ".get_string("of", "dialogue")." ".$total,\r
+                userdate($conversation->timemodified), $status);\r
+            }\r
+        print_table($table);\r
+        print_simple_box_end();\r
+    } \r
+}\r
+\r
+\r
+//////////////////////////////////////////////////////////////////////////////////////\r
+function dialogue_list_conversations_self($dialogue) {\r
+// list open conversations of the current user awaiting their reply\r
+    global $THEME, $USER;\r
+    \r
+    if (! $course = get_record("course", "id", $dialogue->course)) {\r
+        error("Course is misconfigured");\r
+    }\r
+    if (! $cm = get_coursemodule_from_instance("dialogue", $dialogue->id, $course->id)) {\r
+        error("Course Module ID was incorrect");\r
+    }\r
+    \r
+    // set up some general variables\r
+    $usehtmleditor = can_use_html_editor();\r
+\r
+    $timenow = time();\r
+    $showbutton = false;\r
+    \r
+    echo "<form name=\"replies\" method=\"post\" action=\"dialogues.php\">\n";\r
+    echo "<input type=\"hidden\" name=\"action\" value=\"insertentries\"/>\n";\r
+    echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />\n";\r
+    echo "<input type=\"hidden\" name=\"pane\" value=\"1\" />\n";\r
+\r
+    // list the conversations requiring a resonse from this user in full\r
+    if ($conversations = dialogue_get_conversations($dialogue, $USER, "lastid != $USER->id AND closed = 0")) {\r
+        $showbutton = true;\r
+        print_simple_box_start("center");\r
+        foreach ($conversations as $conversation) {\r
+            // set seenon if required\r
+            if (!$conversation->seenon) {\r
+                if (!set_field("dialogue_conversations", "seenon", $timenow, "id", $conversation->id)) {\r
+                    error("List conversations self: could not set seenon");\r
+                }\r
+            }\r
+            echo "<table align=\"center\" border=\"1\" cellspacing=\"0\" valign=\"top\" cellpadding=\"4\" \r
+                width=\"100%\">\n";\r
+            echo "<tr><td bgcolor=\"$THEME->cellheading2\" valign=\"top\">\n";\r
+            if ($conversation->userid == $USER->id) {\r
+                if (!$otheruser = get_record("user", "id", $conversation->recipientid)) {\r
+                    error("User not found");\r
+                }\r
+            }\r
+            else {\r
+                if (!$otheruser = get_record("user", "id", $conversation->userid)) {\r
+                    error("User not found");\r
+                }\r
+            }\r
+            // print_user_picture($user->id, $course->id, $user->picture);\r
+            echo "<b>".get_string("dialoguewith", "dialogue", fullname($otheruser)).\r
+                "</b></td>";\r
+            echo "<td bgcolor=\"$THEME->cellheading2\"><i>".clean_text($conversation->subject)."&nbsp;</i><br />\n";\r
+            echo "<div align=\"right\">\n";\r
+            if (!$conversation->subject) {\r
+                // conversation does not have a subject, show add subject link\r
+                echo "<a href=\"dialogues.php?action=getsubject&amp;id=$cm->id&amp;cid=$conversation->id&amp;pane=2\">".\r
+                    get_string("addsubject", "dialogue")."</a>\n";\r
+                helpbutton("addsubject", get_string("addsubject", "dialogue"), "dialogue");\r
+                echo "&nbsp; | ";\r
+            }\r
+            if (dialogue_count_entries($dialogue, $conversation)) {\r
+                echo "<a href=\"dialogues.php?action=confirmclose&amp;id=$cm->id&amp;cid=$conversation->id&amp;pane=1\">".\r
+                    get_string("close", "dialogue")."</a>\n";\r
+                helpbutton("closedialogue", get_string("close", "dialogue"), "dialogue");\r
+            }\r
+            else {\r
+                echo "&nbsp;";\r
+            }\r
+            echo "<div></td></tr>";\r
+        \r
+            if ($entries = get_records_select("dialogue_entries", "conversationid = $conversation->id", \r
+                    "id")) {\r
+                foreach ($entries as $entry) {\r
+                    if ($entry->userid == $USER->id) {\r
+                        echo "<tr><td colspan=\"2\" bgcolor=\"#FFFFFF\">\n";\r
+                        echo text_to_html("<font size=\"1\">".get_string("onyouwrote", "dialogue", \r
+                            userdate($entry->timecreated)).":</font><br />".clean_text($entry->text));\r
+                        echo "</td></tr>\n";\r
+                    }\r
+                    else {\r
+                        echo "<tr><td colspan=\"2\" bgcolor=\"$THEME->body\">\n";\r
+                        echo text_to_html("<font size=\"1\">".get_string("onwrote", "dialogue", \r
+                               userdate($entry->timecreated)." ".$otheruser->firstname).\r
+                               ":</font><br />".clean_text($entry->text));\r
+                        echo "</td></tr>\n";\r
+                    }\r
+                }\r
+            }\r
+        \r
+            echo "<tr><td colspan=\"2\" align=\"center\" valign=\"top\">\n";\r
+            if ($entries) {\r
+                echo "<i>".get_string("typereply", "dialogue")."</i>\n";\r
+            }\r
+            else {\r
+                echo "<i>".get_string("typefirstentry", "dialogue")."</i>\n";\r
+            }\r
+            echo "</td></tr>\n";\r
+            echo "<tr><td valign=\"top\" align=\"right\">\n";\r
+            helpbutton("writing", get_string("helpwriting"), "moodle", true, true);\r
+            echo "<br />";\r
+            echo "</td><td>\n";\r
+            // use a cumbersome name on the textarea is just historical :-) \r
+            print_textarea($usehtmleditor, 20, 75, 630, 300, "reply$conversation->id");\r
+            echo "</td></tr>";\r
+            echo "</table><br />\n";\r
+        }\r
+        print_simple_box_end();\r
+        use_html_editor();\r
+        if ($showbutton) {\r
+            echo "<hr />\n";\r
+            echo "<p align=\"center\"><input type=\"submit\" value=\"".get_string("addmynewentries", "dialogue").\r
+                "\"/></p>\n";\r
+        }\r
+        echo "</form>\n";\r
+    }\r
+}\r
+\r
+\r
+//////////////////////////////////////////////////////////////////////////////////////\r
+function dialogue_print_conversation($dialogue, $conversation) {\r
+// print a conversation and allow a new entry\r
+    global $THEME, $USER;\r
+    \r
+    if (! $course = get_record("course", "id", $dialogue->course)) {\r
+        error("Course is misconfigured");\r
+    }\r
+    if (! $cm = get_coursemodule_from_instance("dialogue", $dialogue->id, $course->id)) {\r
+        error("Course Module ID was incorrect");\r
+    }\r
+    \r
+    $timenow = time();\r
+    $showbutton = false;\r
+    \r
+    echo "<form name=\"replies\" method=\"post\" action=\"dialogues.php\">\n";\r
+    echo "<input type=\"hidden\" name=\"action\" value=\"insertentries\"/>\n";\r
+    echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />\n";\r
+    echo "<input type=\"hidden\" name=\"pane\" value=\"2\" />\n";\r
+\r
+    $showbutton = true;\r
+    print_simple_box_start("center", "", $THEME->cellcontent2);\r
+    echo "<table align=\"center\" border=\"1\" cellspacing=\"0\" valign=\"top\" cellpadding=\"4\" \r
+        width=\"100%\">\n";\r
+    echo "<tr><td bgcolor=\"$THEME->cellheading2\" valign=\"top\">\n";\r
+    if ($conversation->userid == $USER->id) {\r
+        if (!$otheruser = get_record("user", "id", $conversation->recipientid)) {\r
+            error("User not found");\r
+        }\r
+    }\r
+    else {\r
+        if (!$otheruser = get_record("user", "id", $conversation->userid)) {\r
+            error("User not found");\r
+        }\r
+    }\r
+    // print_user_picture($user->id, $course->id, $user->picture);\r
+    echo "<b>".get_string("dialoguewith", "dialogue", fullname($otheruser)).\r
+        "</b></td>";\r
+    echo "<td bgcolor=\"$THEME->cellheading2\"><i>".clean_text($conversation->subject)."&nbsp;</i><br />\n";\r
+    echo "<div align=\"right\">\n";\r
+    if (!$conversation->subject) {\r
+        // conversation does not have a subject, show add subject link\r
+        echo "<a href=\"dialogues.php?action=getsubject&amp;id=$cm->id&amp;cid=$conversation->id&amp;pane=2\">".\r
+            get_string("addsubject", "dialogue")."</a>\n";\r
+        helpbutton("addsubject", get_string("addsubject", "dialogue"), "dialogue");\r
+        echo "&nbsp; | ";\r
+    }\r
+    echo "<a href=\"dialogues.php?action=confirmclose&amp;id=$cm->id&amp;cid=$conversation->id&amp;pane=2\">".\r
+        get_string("close", "dialogue")."</a>\n";\r
+    helpbutton("closedialogue", get_string("close", "dialogue"), "dialogue");\r
+    echo "</div></td></tr>\n";\r
+\r
+    if ($entries = get_records_select("dialogue_entries", "conversationid = $conversation->id", "id")) {\r
+        foreach ($entries as $entry) {\r
+            if ($entry->userid == $USER->id) {\r
+                echo "<tr><td colspan=\"2\" bgcolor=\"#FFFFFF\">\n";\r
+                echo text_to_html("<font size=\"1\">".get_string("onyouwrote", "dialogue", \r
+                            userdate($entry->timecreated)).":</font><br />".clean_text($entry->text));\r
+            }\r
+            else {\r
+                echo "<tr><td colspan=\"2\" bgcolor=\"$THEME->body\">\n";\r
+                echo text_to_html("<font size=\"1\">".get_string("onwrote", "dialogue", \r
+                            userdate($entry->timecreated)." ".$otheruser->firstname).":</font><br />".\r
+                        clean_text($entry->text));\r
+            }\r
+        }\r
+        echo "</td></tr>\n";\r
+    }\r
+    echo "<tr><td colspan=\"2\" align=\"center\" valign=\"top\"><i>".\r
+        get_string("typefollowup", "dialogue")."</i></td></tr>\n";\r
+    echo "<tr><td valign=\"top\" align=\"right\">\n";\r
+    helpbutton("writing", get_string("helpwriting"), "moodle", true, true);\r
+    echo "<br />";\r
+    echo "</td><td>\n";\r
+    // use a cumbersome name on the textarea for historical reasons :-) \r
+    $usehtmleditor = can_use_html_editor();\r
+    print_textarea($usehtmleditor, 20, 75, 630, 300, "reply$conversation->id");\r
+    use_html_editor();\r
+    echo "</td></tr>";\r
+    echo "</table><br />\n";\r
+    print_simple_box_end();\r
+    if ($showbutton) {\r
+        echo "<hr />\n";\r
+        echo "<br /><input type=\"submit\" value=\"".get_string("addmynewentry", "dialogue")."\" />\n";\r
+    }\r
+    echo "</form>\n";\r
+}\r
+\r
+\r
+//////////////////////////////////////////////////////////////////////////////////////\r
+function dialogue_print_tabbed_heading($tabs) {\r
+// Prints a tabbed heading where one of the tabs highlighted.\r
+// $tabs is an object with several properties.\r
+//      $tabs->names      is an array of tab names\r
+//      $tabs->urls       is an array of links\r
+//      $tabs->align     is an array of column alignments (defaults to "center")\r
+//      $tabs->size      is an array of column sizes\r
+//      $tabs->wrap      is an array of "nowrap"s or nothing\r
+//      $tabs->highlight    is an index (zero based) of "active" heading .\r
+//      $tabs->width     is an percentage of the page (defualts to 80%)\r
+//      $tabs->cellpadding    padding on each cell (defaults to 5)\r
+\r
+    global $CFG, $THEME;\r
+    \r
+    if (isset($tabs->names)) {\r
+        foreach ($tabs->names as $key => $name) {\r
+            if (!empty($tabs->urls[$key])) {\r
+                $url =$tabs->urls[$key];\r
+                if ($tabs->highlight == $key) {\r
+                    $tabcontents[$key] = "<b>$name</b>";\r
+                } else {\r
+                    $tabcontents[$key] = "<a class= \"dimmed\" href=\"$url\"><b>$name</b></a>";\r
+                }\r
+            } else {\r
+                $tabcontents[$key] = "<b>$name</b>";\r
+            }\r
+        }\r
+    }\r
+\r
+    if (empty($tabs->width)) {\r
+        $tabs->width = "80%";\r
+    }\r
+\r
+    if (empty($tabs->cellpadding)) {\r
+        $tabs->cellpadding = "5";\r
+    }\r
+\r
+    // print_simple_box_start("center", "$table->width", "#ffffff", 0);\r
+    echo "<table width=\"$tabs->width\" border=\"0\" valign=\"top\" align=\"center\" ";\r
+    echo " cellpadding=\"$tabs->cellpadding\" cellspacing=\"0\" class=\"generaltable\">\n";\r
+\r
+    if (!empty($tabs->names)) {\r
+        echo "<tr>";\r
+        echo "<td  class=\"generaltablecell\">".\r
+            "<img width=\"10\" src=\"$CFG->wwwroot/pix/spacer.gif\" alt=\"\" /></td>\n";\r
+        foreach ($tabcontents as $key => $tab) {\r
+            if (isset($align[$key])) {\r
+                $alignment = "align=\"$align[$key]\"";\r
+            } else {\r
+                $alignment = "align=\"center\"";\r
+            }\r
+            if (isset($size[$key])) {\r
+                $width = "width=\"$size[$key]\"";\r
+            } else {\r
+                $width = "";\r
+            }\r
+            if (isset($wrap[$key])) {\r
+                $wrapping = "no wrap";\r
+            } else {\r
+                $wrapping = "";\r
+            }\r
+            if ($key == $tabs->highlight) {\r
+                echo "<td valign=top class=\"generaltabselected\" $alignment $width $wrapping bgcolor=\"$THEME->cellheading2\">$tab</td>\n";\r
+            } else {\r
+                echo "<td valign=top class=\"generaltab\" $alignment $width $wrapping bgcolor=\"$THEME->cellheading\">$tab</td>\n";\r
+            }\r
+        echo "<td  class=\"generaltablecell\">".\r
+            "<img width=\"10\" src=\"$CFG->wwwroot/pix/spacer.gif\" alt=\"\" /></td>\n";\r
+        }\r
+        echo "</tr>\n";\r
+    } else {\r
+        echo "<tr><td>No names specified</td></tr>\n";\r
+    }\r
+    // bottom stripe\r
+    $ncells = count($tabs->names)*2 +1;\r
+    $height = 2;\r
+    echo "<tr><td colspan=\"$ncells\" bgcolor=\"$THEME->cellheading2\">".\r
+        "<img height=\"$height\" src=\"$CFG->wwwroot/pix/spacer.gif\" alt=\"\" /></td></tr>\n";\r
+    echo "</table>\n";\r
+    // print_simple_box_end();\r
+\r
+    return true;\r
+}\r
+\r
+\r
+\r
+//////////////////////////////////////////////////////////////////////////////////////\r
+function dialogue_show_conversation($dialogue, $conversation) {\r
+    global $THEME, $USER;\r
+    \r
+    if (! $course = get_record("course", "id", $dialogue->course)) {\r
+        error("Course is misconfigured");\r
+        }\r
+    if (! $cm = get_coursemodule_from_instance("dialogue", $dialogue->id, $course->id)) {\r
+        error("Course Module ID was incorrect");\r
+    }\r
+    \r
+    $timenow = time();\r
+    print_simple_box_start("center");\r
+    echo "<table border=\"1\" cellspacing=\"0\" valign=\"top\" cellpadding=\"4\" width=\"100%\">\n";\r
+        \r
+    echo "<tr>";\r
+    echo "<td bgcolor=\"$THEME->cellheading2\" valign=\"top\">\n";\r
+    if ($conversation->userid == $USER->id) {\r
+        if (!$otheruser = get_record("user", "id", $conversation->recipientid)) {\r
+            error("User not found");\r
+        }\r
+    }\r
+    else {\r
+        if (!$otheruser = get_record("user", "id", $conversation->userid)) {\r
+            error("User not found");\r
+        }\r
+    }\r
+    // print_user_picture($user->id, $course->id, $user->picture);\r
+    echo "<b>".get_string("dialoguewith", "dialogue", fullname($otheruser)).\r
+        "</b></td>";\r
+    echo "<td bgcolor=\"$THEME->cellheading2\" valign=\"top\"><i>".clean_text($conversation->subject)."&nbsp;</i></td></tr>";\r
+\r
+    if ($entries = get_records_select("dialogue_entries", "conversationid = $conversation->id", "id")) {\r
+        foreach ($entries as $entry) {\r
+            if ($entry->userid == $USER->id) {\r
+                echo "<tr><td  colspan=\"2\" bgcolor=\"#FFFFFF\">\n";\r
+                echo text_to_html("<font size=\"1\">".get_string("onyouwrote", "dialogue", \r
+                    userdate($entry->timecreated)).\r
+                    ":</font><br />".clean_text($entry->text));\r
+                echo "</td></tr>\n";\r
+            }\r
+            else {\r
+                echo "<tr><td  colspan=\"2\" bgcolor=\"$THEME->body\">\n";\r
+                echo text_to_html("<font size=\"1\">".get_string("onwrote", "dialogue", \r
+                    userdate($entry->timecreated)." ".$otheruser->firstname).":</font><br />".clean_text($entry->text));\r
+                echo "</td></tr>\n";\r
+            }\r
+        }\r
+    }\r
+    echo "</table><br />\n";\r
+    print_simple_box_end();\r
+    print_continue("view.php?id=$cm->id&amp;pane=3");\r
+}\r
+\r
+\r
+//////////////////////////////////////////////////////////////////////////////////////\r
+function dialogue_show_other_conversations($dialogue, $conversation) {\r
+// prints the other CLOSED conversations for this pair of users\r
+    global $THEME;\r
+    \r
+    if (! $course = get_record("course", "id", $dialogue->course)) {\r
+        error("Course is misconfigured");\r
+        }\r
+    if (! $cm = get_coursemodule_from_instance("dialogue", $dialogue->id, $course->id)) {\r
+        error("Course Module ID was incorrect");\r
+    }\r
+    \r
+    if (!$user = get_record("user", "id", $conversation->userid)) {\r
+        error("User not found");\r
+        }\r
+    if (!$otheruser = get_record("user", "id", $conversation->recipientid)) {\r
+        error("User not found");\r
+        }\r
+    \r
+    if ($conversations = get_records_select("dialogue_conversations", "dialogueid = $dialogue->id AND \r
+            ((userid = $user->id AND recipientid = $otheruser->id) OR (userid = $otheruser->id AND \r
+            recipientid = $user->id)) AND closed = 1", "timemodified DESC")) {\r
+        if (count($conversations) > 1) {\r
+            $timenow = time();\r
+            foreach ($conversations as $otherconversation) {\r
+                if ($conversation->id != $otherconversation->id) {\r
+                    // for this conversation work out which is the other user\r
+                    if ($otherconversation->userid == $user->id) {\r
+                        if (!$otheruser = get_record("user", "id", $otherconversation->recipientid)) {\r
+                            error("Show other conversations: could not get user record");\r
+                        }\r
+                    }\r
+                    else {\r
+                        if (!$otheruser = get_record("user", "id", $otherconversation->userid)) {\r
+                            error("Show other conversations: could not get user record");\r
+                        }\r
+                    } \r
+                    print_simple_box_start("center");\r
+                    echo "<table align=\"center\" border=\"1\" cellspacing=\"0\" valign=\"top\" cellpadding=\"4\"\r
+                         width=\"100%\">\n";\r
+                    echo "<tr>";\r
+                    echo "<td bgcolor=\"$THEME->cellheading2\" valign=\"top\">\n";\r
+                    // print_user_picture($otheruser->id, $course->id, $otheruser->picture);\r
+                    echo "<b>".get_string("dialoguewith", "dialogue", \r
+                        fullname($otheruser))."</b></td>";\r
+                    echo "<td bgcolor=\"$THEME->cellheading2\" valign=\"top\"><i>$otherconversation->subject&nbsp;</i></td></tr>";\r
+                    if ($entries = get_records_select("dialogue_entries", \r
+                            "conversationid = $otherconversation->id", "id")) {\r
+                        foreach ($entries as $entry) {\r
+                            if ($entry->userid == $user->id) {\r
+                                echo "<tr><td  colspan=\"2\" bgcolor=\"#FFFFFF\">\n";\r
+                                echo text_to_html("<font size=\"1\">".get_string("onyouwrote", "dialogue", \r
+                                    userdate($entry->timecreated)).":</font><br />".clean_text($entry->text));\r
+                                echo "</td></tr>\n";\r
+                            }\r
+                            else {\r
+                                echo "<tr><td  colspan=\"2\" bgcolor=\"$THEME->body\">\n";\r
+                                echo text_to_html("<font size=\"1\">".get_string("onwrote", "dialogue", \r
+                                    userdate($entry->timecreated)." ".$otheruser->firstname).\r
+                                    ":</font><br />".clean_text($entry->text));\r
+                                echo "</td></tr>\n";\r
+                            }\r
+                        }\r
+                    }\r
+                \r
+                    echo "</table><br />\n";\r
+                    print_simple_box_end();\r
+                }\r
+            }\r
+            print_continue("view.php?id=$cm->id&amp;pane=3");\r
+        }\r
+    }\r
+}\r
+\r
+\r
+?>\r