MDL-12627 - when visiting the gradebook for the first time, we don't sort
[moodle.git] / mod / chat / lib.php
CommitLineData
1c61c8d6 1<?php // $Id$
1515a89e 2
3/// Library of functions and constants for module chat
c4d588cc 4require_once($CFG->libdir.'/pagelib.php');
5
1515a89e 6
1515a89e 7// The HTML head for the message window to start with (<!-- nix --> is used to get some browsers starting with output
8$CHAT_HTMLHEAD = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\"><html><head></head>\n<body bgcolor=\"#FFFFFF\">\n\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n";
9
10// The HTML head for the message window to start with (with js scrolling)
32f0b38a 11$CHAT_HTMLHEAD_JS = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\"><html><head><script type=\"text/javascript\">\n//<![CDATA[\nfunction move()\n{\nif (scroll_active) window.scroll(1,400000);\nwindow.setTimeout(\"move()\",100);\n}\nscroll_active = true;\nmove();\n//]]>\n</script>\n</head>\n<body bgcolor=\"#FFFFFF\" onBlur=\"scroll_active = true\" onFocus=\"scroll_active = false\">\n\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n";
1515a89e 12
13// The HTML code for standard empty pages (e.g. if a user was kicked out)
89f688b6 14$CHAT_HTMLHEAD_OUT = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\"><html><head><title>You are out!</title></head><body bgcolor=\"#FFFFFF\"></body></html>";
1515a89e 15
16// The HTML head for the message input page
89f688b6 17$CHAT_HTMLHEAD_MSGINPUT = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\"><html><head><title>Message Input</title></head><body bgcolor=\"#FFFFFF\">";
1515a89e 18
19// The HTML code for the message input page, with JavaScript
32f0b38a 20$CHAT_HTMLHEAD_MSGINPUT_JS = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"http://www.w3.org/TR/REC-html40/loose.dtd\"><html><head><title>Message Input</title>\n<script type=\"text/javascript\">\n//<![CDATA[\nscroll_active = true;\nfunction empty_field_and_submit()\n{\ndocument.fdummy.arsc_message.value=document.f.arsc_message.value;\ndocument.fdummy.submit();\ndocument.f.arsc_message.focus();\ndocument.f.arsc_message.select();\nreturn false;\n}\n//]]>\n</script>\n</head><body bgcolor=\"#FFFFFF\" OnLoad=\"document.f.arsc_message.focus();document.f.arsc_message.select();\">";
1515a89e 21
fbabbd23 22// Dummy data that gets output to the browser as needed, in order to make it show output
23$CHAT_DUMMY_DATA = "<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n<!-- nix -->\n";
1515a89e 24
25function chat_add_instance($chat) {
b5de723d 26/// Given an object containing all the necessary data,
27/// (defined by the form in mod.html) this function
28/// will create a new instance and return the id number
1515a89e 29/// of the new instance.
30
31 $chat->timemodified = time();
32
8496c4af 33 if ($returnid = insert_record("chat", $chat)) {
34
35 $event = NULL;
36 $event->name = $chat->name;
37 $event->description = $chat->intro;
38 $event->courseid = $chat->course;
39 $event->groupid = 0;
40 $event->userid = 0;
41 $event->modulename = 'chat';
42 $event->instance = $returnid;
43 $event->eventtype = $chat->schedule;
44 $event->timestart = $chat->chattime;
45 $event->timeduration = 0;
46
47 add_event($event);
48 }
49
50 return $returnid;
1515a89e 51}
52
53
54function chat_update_instance($chat) {
b5de723d 55/// Given an object containing all the necessary data,
56/// (defined by the form in mod.html) this function
1515a89e 57/// will update an existing instance with new data.
58
59 $chat->timemodified = time();
60 $chat->id = $chat->instance;
61
1515a89e 62
8496c4af 63 if ($returnid = update_record("chat", $chat)) {
64
264867fd 65 $event = new object();
8496c4af 66
67 if ($event->id = get_field('event', 'id', 'modulename', 'chat', 'instance', $chat->id)) {
68
69 $event->name = $chat->name;
70 $event->description = $chat->intro;
71 $event->timestart = $chat->chattime;
72
73 update_event($event);
74 }
75 }
76
77 return $returnid;
1515a89e 78}
79
80
81function chat_delete_instance($id) {
b5de723d 82/// Given an ID of an instance of this module,
83/// this function will permanently delete the instance
84/// and any data that depends on it.
1515a89e 85
a71efae3 86 if (! $chat = get_record('chat', 'id', $id)) {
1515a89e 87 return false;
88 }
89
90 $result = true;
91
92 # Delete any dependent records here #
93
a71efae3 94 if (! delete_records('chat', 'id', $chat->id)) {
95 $result = false;
96 }
97 if (! delete_records('chat_messages', 'chatid', $chat->id)) {
98 $result = false;
99 }
100 if (! delete_records('chat_users', 'chatid', $chat->id)) {
1515a89e 101 $result = false;
102 }
103
880d8675 104 $pagetypes = page_import_types('mod/chat/');
105 foreach($pagetypes as $pagetype) {
106 if(!delete_records('block_instance', 'pageid', $chat->id, 'pagetype', $pagetype)) {
107 $result = false;
108 }
109 }
110
36eb856f 111 if (! delete_records('event', 'modulename', 'chat', 'instance', $chat->id)) {
112 $result = false;
113 }
114
1515a89e 115 return $result;
116}
117
118function chat_user_outline($course, $user, $mod, $chat) {
b5de723d 119/// Return a small object with summary information about what a
1515a89e 120/// user has done with a given particular instance of this module
121/// Used for user activity reports.
122/// $return->time = the time they did it
123/// $return->info = a short text description
124
5c05d23b 125 $return = NULL;
1515a89e 126 return $return;
127}
128
129function chat_user_complete($course, $user, $mod, $chat) {
b5de723d 130/// Print a detailed representation of what a user has done with
1515a89e 131/// a given particular instance of this module, for user activity reports.
132
133 return true;
134}
135
b7602a11 136function chat_print_recent_activity($course, $isteacher, $timestart) {
137/// Given a course and a date, prints a summary of all chat rooms
138/// that currently have people in them.
1515a89e 139/// This function is called from course/lib.php: print_recent_activity()
140
b7602a11 141 global $CFG;
142
c5a05b95 143 $timeold = time() - $CFG->chat_old_ping;
144
8f7dc7f1 145 $lastpingsearch = ($CFG->chat_method == 'sockets') ? '': 'AND cu.lastping > \''.$timeold.'\'';
0469cccf 146
132911c8 147 if (!$chatusers = get_records_sql("SELECT u.id, cu.chatid, u.firstname, u.lastname
3d81c41e 148 FROM {$CFG->prefix}chat_users cu,
149 {$CFG->prefix}chat ch,
150 {$CFG->prefix}user u
b5de723d 151 WHERE cu.userid = u.id
0469cccf 152 AND cu.chatid = ch.id $lastpingsearch
cea57965 153 AND ch.course = '$course->id'
b7602a11 154 ORDER BY cu.chatid ASC") ) {
155 return false;
156 }
1515a89e 157
3120f6f2 158 $outputstarted = false;
b7602a11 159 $current = 0;
160 foreach ($chatusers as $chatuser) {
161 if ($current != $chatuser->chatid) {
162 if ($current) {
8f7dc7f1 163 echo '</ul></div>'; // room
164 $current = 0;
b7602a11 165 }
8f7dc7f1 166 if ($chat = get_record('chat', 'id', $chatuser->chatid)) {
264867fd 167
dabfd0ed 168 // we find the course module id
80aede31 169 $cm = get_coursemodule_from_instance('chat', $chat->id, $course->id);
dabfd0ed 170 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
264867fd 171
dabfd0ed 172 // needs to be fixed
0468976c 173 if (!(has_capability('mod/chat:readlog', $context) or instance_is_visible('chat', $chat))) { // Chat hidden to students
3120f6f2 174 continue;
175 }
176 if (!$outputstarted) {
8f7dc7f1 177 print_headline(get_string('currentchats', 'chat').':');
3120f6f2 178 $outputstarted = true;
179 }
200371e9 180 echo '<div class="room"><p class="head"><a href="'.$CFG->wwwroot.'/mod/chat/view.php?c='.$chat->id.'">'.format_string($chat->name,true).'</a></p><ul>';
b7602a11 181 }
182 $current = $chatuser->chatid;
183 }
80aede31 184 $fullname = fullname($chatuser, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
8f7dc7f1 185 echo '<li class="info name">'.$fullname.'</li>';
186 }
187
188 if ($current) {
189 echo '</ul></div>'; // room
b7602a11 190 }
191
192 return true;
1515a89e 193}
194
8f7dc7f1 195
1515a89e 196function chat_cron () {
197/// Function to be run periodically according to the moodle cron
b5de723d 198/// This function searches for things that need to be done, such
199/// as sending out mail, toggling flags etc ...
1515a89e 200
201 global $CFG;
202
fcd3a1ee 203 chat_update_chat_times();
204
7d792369 205 chat_delete_old_users();
206
319038c3 207 /// Delete old messages with a
208 /// single SQL query.
209 $subselect = "SELECT c.keepdays
210 FROM {$CFG->prefix}chat c
211 WHERE c.id = {$CFG->prefix}chat_messages.chatid";
4388027c 212
319038c3 213 $sql = "DELETE
4388027c 214 FROM {$CFG->prefix}chat_messages
817fb4cb 215 WHERE ($subselect) > 0 AND timestamp < ( ".time()." -($subselect) * 24 * 3600)";
4388027c 216
217 execute_sql($sql, false);
22a4491a 218
1515a89e 219 return true;
220}
221
84a2fdd7 222function chat_get_participants($chatid, $groupid=0) {
05855091 223//Returns the users with data in one chat
224//(users with records in chat_messages, students)
225
226 global $CFG;
227
84a2fdd7 228 if ($groupid) {
229 $groupselect = " AND (c.groupid='$groupid' OR c.groupid='0')";
230 } else {
231 $groupselect = "";
232 }
233
05855091 234 //Get students
95e72c12 235 $students = get_records_sql("SELECT DISTINCT u.id, u.id
05855091 236 FROM {$CFG->prefix}user u,
237 {$CFG->prefix}chat_messages c
84a2fdd7 238 WHERE c.chatid = '$chatid' $groupselect
239 AND u.id = c.userid");
1515a89e 240
05855091 241 //Return students array (it contains an array of unique users)
242 return ($students);
243}
1515a89e 244
8496c4af 245function chat_refresh_events($courseid = 0) {
246// This standard function will check all instances of this module
247// and make sure there are up-to-date events created for each of them.
248// If courseid = 0, then every chat event in the site is checked, else
249// only chat events belonging to the course specified are checked.
250// This function is used, in its new format, by restore_refresh_events()
251
252 if ($courseid) {
253 if (! $chats = get_records("chat", "course", $courseid)) {
254 return true;
255 }
256 } else {
257 if (! $chats = get_records("chat")) {
258 return true;
259 }
260 }
dcd338ff 261 $moduleid = get_field('modules', 'id', 'name', 'chat');
8496c4af 262
263 foreach ($chats as $chat) {
264 $event = NULL;
265 $event->name = addslashes($chat->name);
266 $event->description = addslashes($chat->intro);
267 $event->timestart = $chat->chattime;
268
269 if ($event->id = get_field('event', 'id', 'modulename', 'chat', 'instance', $chat->id)) {
270 update_event($event);
271
272 } else {
273 $event->courseid = $chat->course;
274 $event->groupid = 0;
275 $event->userid = 0;
276 $event->modulename = 'chat';
277 $event->instance = $chat->id;
278 $event->eventtype = $chat->schedule;
279 $event->timeduration = 0;
dcd338ff 280 $event->visible = get_field('course_modules', 'visible', 'module', $moduleid, 'instance', $chat->id);
b5de723d 281
8496c4af 282 add_event($event);
283 }
284 }
285 return true;
286}
287
516121bd 288
1515a89e 289//////////////////////////////////////////////////////////////////////
290/// Functions that require some SQL
291
a12e11c1 292function chat_get_users($chatid, $groupid=0, $groupingid=0) {
1515a89e 293
294 global $CFG;
84a2fdd7 295
296 if ($groupid) {
297 $groupselect = " AND (c.groupid='$groupid' OR c.groupid='0')";
298 } else {
299 $groupselect = "";
300 }
a12e11c1 301
302 if (!empty($CFG->enablegroupings) && !(empty($groupingid))) {
303 $groupingjoin = "INNER JOIN {$CFG->prefix}groups_members gm ON u.id = gm.userid
304 INNER JOIN {$CFG->prefix}groupings_groups gg ON gm.groupid = gg.groupid AND gg.groupingid = $groupingid ";
305
306 } else {
307 $groupingjoin = '';
308 }
b5de723d 309
426c0437 310 return get_records_sql("SELECT DISTINCT u.id, u.firstname, u.lastname, u.picture, c.lastmessageping, c.firstping
a12e11c1 311 FROM {$CFG->prefix}chat_users c
312 INNER JOIN {$CFG->prefix}user u ON u.id = c.userid
313 $groupingjoin
1515a89e 314 WHERE c.chatid = '$chatid'
a12e11c1 315 $groupselect
1515a89e 316 ORDER BY c.firstping ASC");
317}
318
84a2fdd7 319function chat_get_latest_message($chatid, $groupid=0) {
5a8625e4 320/// Efficient way to extract just the latest message
321/// Uses ADOdb directly instead of get_record_sql()
b5de723d 322/// because the LIMIT command causes problems with
5a8625e4 323/// the developer debugging in there.
1515a89e 324
5a8625e4 325 global $db, $CFG;
1515a89e 326
84a2fdd7 327 if ($groupid) {
328 $groupselect = " AND (groupid='$groupid' OR groupid='0')";
329 } else {
330 $groupselect = "";
331 }
332
0217757b 333 if (!$rs = $db->SelectLimit("SELECT *
334 FROM {$CFG->prefix}chat_messages
335 WHERE chatid = '$chatid' $groupselect
336 ORDER BY timestamp DESC", 1)) {
5a8625e4 337 return false;
338 }
03cedd62 339
340 $result = rs_fetch_record($rs);
82f66bed 341
220a90c5 342 rs_close($rs);
03cedd62 343
03cedd62 344 return $result;
1515a89e 345}
346
5a8625e4 347
1515a89e 348//////////////////////////////////////////////////////////////////////
516121bd 349// login if not already logged in
1515a89e 350
a32c7772 351function chat_login_user($chatid, $version, $groupid, $course) {
1515a89e 352 global $USER;
e7fbd0b3 353 if (($version != 'sockets') and $chatuser = get_record_select('chat_users', "chatid='$chatid' AND userid='$USER->id' AND groupid='$groupid'")) {
516121bd 354 $chatuser->version = $version;
d96466d2 355 $chatuser->ip = $USER->lastip;
516121bd 356 $chatuser->lastping = time();
357 $chatuser->lang = current_language();
1515a89e 358
d96466d2 359 // Sometimes $USER->lastip is not setup properly
d13ef2fb 360 // during login. Update with current value if possible
f83edcb1 361 // or provide a dummy value for the db
d13ef2fb 362 if (empty($chatuser->ip)) {
363 $chatuser->ip = getremoteaddr();
364 if (empty($chatuser->ip)) {
f83edcb1 365 $chatuser->ip = '';
d13ef2fb 366 }
367 }
368
516121bd 369 if (($chatuser->course != $course->id)
370 or ($chatuser->userid != $USER->id)) {
371 return false;
372 }
373 if (!update_record('chat_users', $chatuser)) {
374 return false;
375 }
516121bd 376 } else {
6ee78cee 377 $chatuser = new object();
516121bd 378 $chatuser->chatid = $chatid;
379 $chatuser->userid = $USER->id;
380 $chatuser->groupid = $groupid;
381 $chatuser->version = $version;
d96466d2 382 $chatuser->ip = $USER->lastip;
516121bd 383 $chatuser->lastping = $chatuser->firstping = $chatuser->lastmessageping = time();
384 $chatuser->sid = random_string(32);
3dfd307f 385 $chatuser->course = $course->id; //caching - needed for current_language too
386 $chatuser->lang = current_language(); //caching - to resource intensive to find out later
516121bd 387
d96466d2 388 // Sometimes $USER->lastip is not setup properly
274f0091 389 // during login. Update with current value if possible
390 // or provide a dummy value for the db
391 if (empty($chatuser->ip)) {
392 $chatuser->ip = getremoteaddr();
393 if (empty($chatuser->ip)) {
394 $chatuser->ip = '';
395 }
396 }
397
398
516121bd 399 if (!insert_record('chat_users', $chatuser)) {
400 return false;
401 }
402
a32c7772 403 if ($version == 'sockets') {
404 // do not send 'enter' message, chatd will do it
405 } else {
6ee78cee 406 $message = new object();
2ac0d13b 407 $message->chatid = $chatuser->chatid;
408 $message->userid = $chatuser->userid;
409 $message->groupid = $groupid;
410 $message->message = 'enter';
411 $message->system = 1;
412 $message->timestamp = time();
413
414 if (!insert_record('chat_messages', $message)) {
415 error('Could not insert a chat message!');
416 }
516121bd 417 }
1515a89e 418 }
419
420 return $chatuser->sid;
421}
422
7d792369 423function chat_delete_old_users() {
424// Delete the old and in the way
425
b5012f3e 426 global $CFG;
427
e7fbd0b3 428 $timeold = time() - $CFG->chat_old_ping;
953eb6f3 429 $timeoldext = time() - ($CFG->chat_old_ping*10); // JSless gui_basic needs much longer timeouts
a32c7772 430
6ee78cee 431 $query = "(version<>'basic' AND lastping<'$timeold') OR (version='basic' AND lastping<'$timeoldext')";
7d792369 432
516121bd 433 if ($oldusers = get_records_select('chat_users', $query) ) {
434 delete_records_select('chat_users', $query);
7d792369 435 foreach ($oldusers as $olduser) {
6ee78cee 436 $message = new object();
516121bd 437 $message->chatid = $olduser->chatid;
438 $message->userid = $olduser->userid;
439 $message->groupid = $olduser->groupid;
440 $message->message = 'exit';
441 $message->system = 1;
7d792369 442 $message->timestamp = time();
b5de723d 443
516121bd 444 if (!insert_record('chat_messages', $message)) {
445 error('Could not insert a chat message!');
7d792369 446 }
447 }
448 }
449}
1515a89e 450
22a4491a 451
fcd3a1ee 452function chat_update_chat_times($chatid=0) {
453/// Updates chat records so that the next chat time is correct
454
455 $timenow = time();
456 if ($chatid) {
457 if (!$chats[] = get_record_select("chat", "id = '$chatid' AND chattime <= '$timenow' AND schedule > '0'")) {
458 return;
459 }
460 } else {
461 if (!$chats = get_records_select("chat", "chattime <= '$timenow' AND schedule > '0'")) {
462 return;
463 }
464 }
465
466 foreach ($chats as $chat) {
7626969c 467 unset($chat->name);
468 unset($chat->intro);
fcd3a1ee 469 switch ($chat->schedule) {
470 case 1: // Single event - turn off schedule and disable
471 $chat->chattime = 0;
472 $chat->schedule = 0;
473 break;
474 case 2: // Repeat daily
f0d3bb9e 475 while ($chat->chattime <= $timenow) {
476 $chat->chattime += 24 * 3600;
477 }
fcd3a1ee 478 break;
479 case 3: // Repeat weekly
f0d3bb9e 480 while ($chat->chattime <= $timenow) {
481 $chat->chattime += 7 * 24 * 3600;
482 }
fcd3a1ee 483 break;
484 }
485 update_record("chat", $chat);
8496c4af 486
487 $event = NULL; // Update calendar too
b78f4cbd 488 $cond = "modulename='chat' AND instance = {$chat->id}
489 AND timestart != {$chat->chattime}";
490 if ($event->id = get_field_select('event', 'id', $cond)) {
8496c4af 491 $event->timestart = $chat->chattime;
492 update_event($event);
493 }
fcd3a1ee 494 }
495}
496
497
aa5c32fd 498function chat_format_message_manually($message, $courseid, $sender, $currentuser, $chat_lastrow=NULL) {
72989350 499 global $CFG, $USER;
1515a89e 500
6ee78cee 501 $output = new object();
516121bd 502 $output->beep = false; // by default
503 $output->refreshusers = false; // by default
7d792369 504
72989350 505 // Use get_user_timezone() to find the correct timezone for displaying this message:
506 // It's either the current user's timezone or else decided by some Moodle config setting
970f144e 507 // First, "reset" $USER->timezone (which could have been set by a previous call to here)
508 // because otherwise the value for the previous $currentuser will take precedence over $CFG->timezone
509 $USER->timezone = 99;
72989350 510 $tz = get_user_timezone($currentuser->timezone);
b5de723d 511
72989350 512 // Before formatting the message time string, set $USER->timezone to the above.
513 // This will allow dst_offset_on (called by userdate) to work correctly, otherwise the
514 // message times appear off because DST is not taken into account when it should be.
515 $USER->timezone = $tz;
b5de723d 516 $message->strtime = userdate($message->timestamp, get_string('strftimemessage', 'chat'), $tz);
517
518 $message->picture = print_user_picture($sender->id, 0, $sender->picture, false, true, false);
582de679 519 if ($courseid) {
d3981e38 520 $message->picture = "<a onclick=\"window.open('$CFG->wwwroot/user/view.php?id=$sender->id&amp;course=$courseid')\" href=\"$CFG->wwwroot/user/view.php?id=$sender->id&amp;course=$courseid\">$message->picture</a>";
582de679 521 }
1515a89e 522
aa5c32fd 523 //Calculate the row class
524 if ($chat_lastrow !== NULL) {
525 $rowclass = ' class="r'.$chat_lastrow.'" ';
526 } else {
527 $rowclass = '';
528 }
529
b5de723d 530 // Start processing the message
1515a89e 531
b5de723d 532 if(!empty($message->system)) {
533 // System event
534 $output->text = $message->strtime.': '.get_string('message'.$message->message, 'chat', fullname($sender));
aa5c32fd 535 $output->html = '<table class="chat-event"><tr'.$rowclass.'><td class="picture">'.$message->picture.'</td><td class="text">';
536 $output->html .= '<span class="event">'.$output->text.'</span></td></tr></table>';
953eb6f3 537 $output->basic = '<dl><dt class="event">'.$message->strtime.': '.get_string('message'.$message->message, 'chat', fullname($sender)).'</dt></dl>';
7d792369 538
516121bd 539 if($message->message == 'exit' or $message->message == 'enter') {
540 $output->refreshusers = true; //force user panel refresh ASAP
541 }
1515a89e 542 return $output;
543 }
544
82a524ef 545 // It's not a system event
b5de723d 546
547 $text = $message->message;
82a524ef 548
549 /// Parse the text to clean and filter it
550
6ee78cee 551 $options = new object();
82a524ef 552 $options->para = false;
553 $text = format_text($text, FORMAT_MOODLE, $options, $courseid);
927a7808 554
b5de723d 555 // And now check for special cases
927a7808 556 $special = false;
557
b5de723d 558 if (substr($text, 0, 5) == 'beep ') {
927a7808 559 /// It's a beep!
560 $special = true;
7d792369 561 $beepwho = trim(substr($text, 5));
9f85bed4 562
b5de723d 563 if ($beepwho == 'all') { // everyone
564 $outinfo = $message->strtime.': '.get_string('messagebeepseveryone', 'chat', fullname($sender));
565 $outmain = '';
566 $output->beep = true; // (eventually this should be set to
7d792369 567 // to a filename uploaded by the user)
568
82a524ef 569 } else if ($beepwho == $currentuser->id) { // current user
b5de723d 570 $outinfo = $message->strtime.': '.get_string('messagebeepsyou', 'chat', fullname($sender));
571 $outmain = '';
7d792369 572 $output->beep = true;
264867fd 573
0eda0a46 574 } else { //something is not caught?
7d792369 575 return false;
576 }
b5de723d 577 } else if (substr($text, 0, 1) == '/') { /// It's a user command
927a7808 578 if (trim(substr($text, 0, 4)) == '/me') {
579 $special = true;
b5de723d 580 $outinfo = $message->strtime;
581 $outmain = $sender->firstname.' '.substr($text, 4);
1515a89e 582 }
927a7808 583 }
9f85bed4 584
927a7808 585 if(!$special) {
b5de723d 586 $outinfo = $message->strtime.' '.$sender->firstname;
7d792369 587 $outmain = $text;
1515a89e 588 }
264867fd 589
9f85bed4 590 /// Format the message as a small table
1515a89e 591
b5de723d 592 $output->text = strip_tags($outinfo.': '.$outmain);
7d792369 593
5379d249 594 $output->html = "<table class=\"chat-message\"><tr$rowclass><td class=\"picture\" valign=\"top\">$message->picture</td><td class=\"text\">";
aa5c32fd 595 $output->html .= "<span class=\"title\">$outinfo</span>";
7d792369 596 if ($outmain) {
597 $output->html .= ": $outmain";
953eb6f3 598 $output->basic = '<dl><dt class="title">'.$outinfo.':</dt><dd class="text">'.$outmain.'</dd></dl>';
6ee78cee 599 } else {
953eb6f3 600 $output->basic = '<dl><dt class="title">'.$outinfo.'</dt></dl>';
7d792369 601 }
aa5c32fd 602 $output->html .= "</td></tr></table>";
7d792369 603 return $output;
b5de723d 604}
605
aa5c32fd 606function chat_format_message($message, $courseid, $currentuser, $chat_lastrow=NULL) {
b5de723d 607/// Given a message object full of information, this function
608/// formats it appropriately into text and html, then
609/// returns the formatted data.
610
78c98892 611 static $users; // Cache user lookups
612
613 if (isset($users[$message->userid])) {
614 $user = $users[$message->userid];
615 } else if ($user = get_record('user', 'id', $message->userid, '','','','','id,picture,firstname,lastname')) {
616 $users[$message->userid] = $user;
617 } else {
618 return NULL;
b5de723d 619 }
aa5c32fd 620 return chat_format_message_manually($message, $courseid, $user, $currentuser, $chat_lastrow);
1515a89e 621}
622
f3221af9 623function chat_get_view_actions() {
624 return array('view','view all','report');
625}
626
627function chat_get_post_actions() {
628 return array('talk');
629}
630
9ca0187e 631function chat_print_overview($courses, &$htmlarray) {
632 global $USER, $CFG;
633
634 if (empty($courses) || !is_array($courses) || count($courses) == 0) {
635 return array();
636 }
637
638 if (!$chats = get_all_instances_in_courses('chat',$courses)) {
639 return;
640 }
641
642 $strchat = get_string('modulename', 'chat');
643 $strnextsession = get_string('nextsession', 'chat');
9ca0187e 644
645 foreach ($chats as $chat) {
9ca0187e 646 if ($chat->chattime and $chat->schedule) { // A chat is scheduled
a2a37336 647 $str = '<div class="chat overview"><div class="name">'.
648 $strchat.': <a '.($chat->visible?'':' class="dimmed"').
649 ' href="'.$CFG->wwwroot.'/mod/chat/view.php?id='.$chat->coursemodule.'">'.
650 $chat->name.'</a></div>';
651 $str .= '<div class="info">'.$strnextsession.': '.userdate($chat->chattime).'</div></div>';
652
653 if (empty($htmlarray[$chat->course]['chat'])) {
654 $htmlarray[$chat->course]['chat'] = $str;
655 } else {
656 $htmlarray[$chat->course]['chat'] .= $str;
657 }
9ca0187e 658 }
9ca0187e 659 }
660}
661
0b5a80a1 662
663/**
664 * Implementation of the function for printing the form elements that control
665 * whether the course reset functionality affects the chat.
666 * @param $mform form passed by reference
667 */
668function chat_reset_course_form_definition(&$mform) {
669 $mform->addElement('header', 'chatheader', get_string('modulenameplural', 'chat'));
670 $mform->addElement('advcheckbox', 'reset_chat', get_string('removemessages','chat'));
671}
672
673/**
674 * Course reset form defaults.
675 */
676function chat_reset_course_form_defaults($course) {
677 return array('reset_chat'=>1);
678}
679
680/**
681 * Actual implementation of the rest coures functionality, delete all the
682 * chat messages for course $data->courseid.
683 * @param $data the data submitted from the reset course.
684 * @return array status array
685 */
686function chat_reset_userdata($data) {
687 global $CFG;
688
689 $componentstr = get_string('modulenameplural', 'chat');
690 $status = array();
691
692 if (!empty($data->reset_chat)) {
693 $chatessql = "SELECT ch.id
694 FROM {$CFG->prefix}chat ch
695 WHERE ch.course={$data->courseid}";
696
697 delete_records_select('chat_messages', "chatid IN ($chatessql)");
698 delete_records_select('chat_users', "chatid IN ($chatessql)");
699 $status[] = array('component'=>$componentstr, 'item'=>get_string('removemessages', 'chat'), 'error'=>false);
700 }
701
702 /// updating dates - shift may be negative too
703 if ($data->timeshift) {
704 shift_course_mod_dates('chat', array('chattime'), $data->timeshift, $data->courseid);
705 $status[] = array('component'=>$componentstr, 'item'=>get_string('datechanged'), 'error'=>false);
706 }
707
708 return $status;
709}
710
1515a89e 711?>