Incorrect variable name used for parameter.
[moodle-linuxchix.git] / mod / dialogue / lib.php
blob519195f14f58d912387bb217a527162a95739a83
1 <?php // $Id$
3 $DIALOGUE_DAYS = array (0 => 0, 7 => 7, 14 => 14, 30 => 30, 150 => 150, 365 => 365 );
6 // STANDARD MODULE FUNCTIONS /////////////////////////////////////////////////////////
8 //////////////////////////////////////////////////////////////////////////////////////
9 function dialogue_add_instance($dialogue) {
10 // Given an object containing all the necessary data,
11 // (defined by the form in mod.html) this function
12 // will create a new instance and return the id number
13 // of the new instance.
15 $dialogue->timemodified = time();
17 return insert_record("dialogue", $dialogue);
21 //////////////////////////////////////////////////////////////////////////////////////
22 function dialogue_cron () {
23 // Function to be run periodically according to the moodle cron
25 global $CFG, $USER;
27 // delete any closed conversations which have expired
28 dialogue_delete_expired_conversations();
30 // Finds all dialogue entries that have yet to be mailed out, and mails them
31 if ($entries = get_records_select("dialogue_entries", "mailed = '0'")) {
32 foreach ($entries as $entry) {
34 echo "Processing dialogue entry $entry->id\n";
36 if (! $userfrom = get_record("user", "id", "$entry->userid")) {
37 echo "Could not find user $entry->userid\n";
38 continue;
40 // get conversation record
41 if(!$conversation = get_record("dialogue_conversations", "id", $entry->conversationid)) {
42 echo "Could not find conversation $entry->conversationid\n";
44 if ($userfrom->id == $conversation->userid) {
45 if (!$userto = get_record("user", "id", $conversation->recipientid)) {
46 echo "Could not find use $conversation->recipientid\n";
49 else {
50 if (!$userto = get_record("user", "id", $conversation->userid)) {
51 echo "Could not find use $conversation->userid\n";
55 $USER->lang = $userto->lang;
57 if (! $dialogue = get_record("dialogue", "id", $conversation->dialogueid)) {
58 echo "Could not find dialogue id $conversation->dialogueid\n";
59 continue;
61 if (! $course = get_record("course", "id", "$dialogue->course")) {
62 echo "Could not find course $dialogue->course\n";
63 continue;
65 if (! $cm = get_coursemodule_from_instance("dialogue", $dialogue->id, $course->id)) {
66 echo "Course Module ID was incorrect\n";
69 if (! isstudent($course->id, $userfrom->id) and !isteacher($course->id, $userfrom->id)) {
70 continue; // Not an active participant
72 if (! isstudent($course->id, $userto->id) and !isteacher($course->id, $userto->id)) {
73 continue; // Not an active participant
76 $strdialogues = get_string("modulenameplural", "dialogue");
77 $strdialogue = get_string("modulename", "dialogue");
79 unset($dialogueinfo);
80 $dialogueinfo->userfrom = fullname($userfrom);
81 $dialogueinfo->dialogue = "$dialogue->name";
82 $dialogueinfo->url = "$CFG->wwwroot/mod/dialogue/view.php?id=$cm->id";
84 $postsubject = "$course->shortname: $strdialogues: $dialogue->name: ".
85 get_string("newentry", "dialogue");
86 $posttext = "$course->shortname -> $strdialogues -> $dialogue->name\n";
87 $posttext .= "---------------------------------------------------------------------\n";
88 $posttext .= get_string("dialoguemail", "dialogue", $dialogueinfo)." \n";
89 $posttext .= "---------------------------------------------------------------------\n";
90 if ($userto->mailformat == 1) { // HTML
91 $posthtml = "<p><font face=\"sans-serif\">".
92 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> ->".
93 "<a href=\"$CFG->wwwroot/mod/dialogue/index.php?id=$course->id\">dialogues</a> ->".
94 "<a href=\"$CFG->wwwroot/mod/dialogue/view.php?id=$cm->id\">$dialogue->name</a></font></p>";
95 $posthtml .= "<hr /><font face=\"sans-serif\">";
96 $posthtml .= "<p>".get_string("dialoguemailhtml", "dialogue", $dialogueinfo)."</p>";
97 $posthtml .= "</font><hr />";
98 } else {
99 $posthtml = "";
102 if (! email_to_user($userto, $userfrom, $postsubject, $posttext, $posthtml)) {
103 echo "Error: dialogue cron: Could not send out mail for id $entry->id to user $userto->id ($userto->email)\n";
105 if (! set_field("dialogue_entries", "mailed", "1", "id", "$entry->id")) {
106 echo "Could not update the mailed field for id $entry->id\n";
111 return true;
115 //////////////////////////////////////////////////////////////////////////////////////
116 function dialogue_delete_instance($id) {
117 // Given an ID of an instance of this module,
118 // this function will permanently delete the instance
119 // and any data that depends on it.
121 if (! $dialogue = get_record("dialogue", "id", $id)) {
122 return false;
125 $result = true;
127 if (! delete_records("dialogue_conversations", "dialogueid", $dialogue->id)) {
128 $result = false;
131 if (! delete_records("dialogue_entries", "dialogueid", $dialogue->id)) {
132 $result = false;
135 if (! delete_records("dialogue", "id", $dialogue->id)) {
136 $result = false;
139 return $result;
144 //////////////////////////////////////////////////////////////////////////////////////
145 function dialogue_print_recent_activity($course, $isteacher, $timestart) {
146 global $CFG;
148 // have a look for new entries
149 $addentrycontent = false;
150 if ($logs = dialogue_get_add_entry_logs($course, $timestart)) {
151 // got some, see if any belong to a visible module
152 foreach ($logs as $log) {
153 // Create a temp valid module structure (only need courseid, moduleid)
154 $tempmod->course = $course->id;
155 $tempmod->id = $log->dialogueid;
156 //Obtain the visible property from the instance
157 if (instance_is_visible("dialogue",$tempmod)) {
158 $addentrycontent = true;
159 break;
162 // if we got some "live" ones then output them
163 if ($addentrycontent) {
164 print_headline(get_string('newdialogueentries', 'dialogue').':');
165 foreach ($logs as $log) {
166 //Create a temp valid module structure (only need courseid, moduleid)
167 $tempmod->course = $course->id;
168 $tempmod->id = $log->dialogueid;
169 //Obtain the visible property from the instance
170 if (instance_is_visible("dialogue",$tempmod)) {
171 print_recent_activity_note($log->time, $log, $isteacher, $log->name,
172 $CFG->wwwroot.'/mod/dialogue/'.str_replace('&', '&amp;', $log->url));
178 // have a look for open conversations
179 $opencontent = false;
180 if ($logs = dialogue_get_open_conversations($course)) {
181 // got some, see if any belong to a visible module
182 foreach ($logs as $log) {
183 // Create a temp valid module structure (only need courseid, moduleid)
184 $tempmod->course = $course->id;
185 $tempmod->id = $log->dialogueid;
186 //Obtain the visible property from the instance
187 if (instance_is_visible('dialogue',$tempmod)) {
188 $opencontent = true;
189 break;
192 // if we got some 'live' ones then output them
193 if ($opencontent) {
194 print_headline(get_string('opendialogueentries', 'dialogue').':');
195 foreach ($logs as $log) {
196 //Create a temp valid module structure (only need courseid, moduleid)
197 $tempmod->course = $course->id;
198 $tempmod->id = $log->dialogueid;
199 //Obtain the visible property from the instance
200 if (instance_is_visible('dialogue',$tempmod)) {
201 print_recent_activity_note($log->time, $log, $isteacher, $log->name,
202 $CFG->wwwroot.'/mod/dialogue/'.str_replace('&', '&amp;', $log->url));
208 return $addentrycontent or $opencontent;
213 //////////////////////////////////////////////////////////////////////////////////////
214 function dialogue_update_instance($dialogue) {
215 // Given an object containing all the necessary data,
216 // (defined by the form in mod.html) this function
217 // will update an existing instance with new data.
219 $dialogue->timemodified = time();
220 $dialogue->id = $dialogue->instance;
222 return update_record("dialogue", $dialogue);
226 //////////////////////////////////////////////////////////////////////////////////////
227 function dialogue_user_complete($course, $user, $mod, $dialogue) {
229 if ($conversations = dialogue_get_conversations($dialogue, $user)) {
230 print_simple_box_start();
231 $table->head = array (get_string("dialoguewith", "dialogue"),
232 get_string("numberofentries", "dialogue"), get_string("lastentry", "dialogue"),
233 get_string("status", "dialogue"));
234 $table->width = "100%";
235 $table->align = array ("left", "center", "left", "left");
236 $table->size = array ("*", "*", "*", "*");
237 $table->cellpadding = 2;
238 $table->cellspacing = 0;
240 foreach ($conversations as $conversation) {
241 if ($user->id != $conversation->userid) {
242 if (!$with = get_record("user", "id", $conversation->userid)) {
243 error("User's record not found");
246 else {
247 if (!$with = get_record("user", "id", $conversation->recipientid)) {
248 error("User's record not found");
251 $total = dialogue_count_entries($dialogue, $conversation);
252 $byuser = dialogue_count_entries($dialogue, $conversation, $user);
253 if ($conversation->closed) {
254 $status = get_string("closed", "dialogue");
255 } else {
256 $status = get_string("open", "dialogue");
258 $table->data[] = array(fullname($with), $byuser." ".
259 get_string("of", "dialogue")." ".$total, userdate($conversation->timemodified), $status);
261 print_table($table);
262 print_simple_box_end();
264 else {
265 print_string("noentry", "dialogue");
270 //////////////////////////////////////////////////////////////////////////////////////
271 function dialogue_user_outline($course, $user, $mod, $dialogue) {
272 if ($entries = dialogue_get_user_entries($dialogue, $user)) {
273 $result->info = count($entries);
274 foreach ($entries as $entry) {
275 // dialogue_get_user_entries returns the most recent entry first
276 $result->time = $entry->timecreated;
277 break;
279 return $result;
281 return NULL;
284 //////////////////////////////////////////////////////////////////////////////////////
285 // Extra functions needed by the Standard functions
286 //////////////////////////////////////////////////////////////////////////////////////
288 //////////////////////////////////////////////////////////////////////////////////////
289 function dialogue_count_entries($dialogue, $conversation, $user = '') {
291 if (empty($user)) {
292 return count_records_select("dialogue_entries", "conversationid = $conversation->id");
294 else {
295 return count_records_select("dialogue_entries", "conversationid = $conversation->id AND
296 userid = $user->id");
301 //////////////////////////////////////////////////////////////////////////////////////
302 function dialogue_delete_expired_conversations() {
304 if ($dialogues = get_records("dialogue")) {
305 foreach ($dialogues as $dialogue) {
306 if ($dialogue->deleteafter) {
307 $expirytime = time() - $dialogue->deleteafter * 86400;
308 if ($conversations = get_records_select("dialogue_conversations",
309 "timemodified < $expirytime AND closed = 1")) {
310 foreach ($conversations as $conversation) {
311 delete_records("dialogue_conversations", "id", $conversation->id);
312 delete_records("dialogue_entries", "conversationid", $conversation->id);
321 //////////////////////////////////////////////////////////////////////////////////////
322 function dialogue_get_add_entry_logs($course, $timestart) {
323 // get the "add entry" entries and add the first and last names, we are not interested in the entries
324 // make by this user (the last condition)!
325 global $CFG, $USER;
326 if (!isset($USER->id)) {
327 return false;
329 return get_records_sql("SELECT l.time, l.url, u.firstname, u.lastname, e.dialogueid, d.name
330 FROM {$CFG->prefix}log l,
331 {$CFG->prefix}dialogue d,
332 {$CFG->prefix}dialogue_conversations c,
333 {$CFG->prefix}dialogue_entries e,
334 {$CFG->prefix}user u
335 WHERE l.time > $timestart AND l.course = $course->id AND l.module = 'dialogue'
336 AND l.action = 'add entry'
337 AND e.id = l.info
338 AND c.id = e.conversationid
339 AND (c.userid = $USER->id or c.recipientid = $USER->id)
340 AND d.id = e.dialogueid
341 AND u.id = e.userid
342 AND e.userid != $USER->id");
346 //////////////////////////////////////////////////////////////////////////////////////
347 function dialogue_get_closed_logs($course, $timestart) {
348 // get the "closed" entries and add the first and last names, we are not interested in the entries
349 // make by this user (the last condition)!
350 global $CFG, $USER;
351 if (!isset($USER->id)) {
352 return false;
354 return get_records_sql("SELECT l.time, l.url, u.firstname, u.lastname, c.dialogueid, d.name
355 FROM {$CFG->prefix}log l,
356 {$CFG->prefix}dialogue d,
357 {$CFG->prefix}dialogue_conversations c,
358 {$CFG->prefix}user u
359 WHERE l.time > $timestart AND l.course = $course->id AND l.module = 'dialogue'
360 AND l.action = 'closed'
361 AND c.id = l.info
362 AND (c.userid = $USER->id or c.recipientid = $USER->id)
363 AND d.id = c.dialogueid
364 AND u.id = c.lastid
365 AND c.lastid != $USER->id");
369 //////////////////////////////////////////////////////////////////////////////////////
370 function dialogue_get_conversations($dialogue, $user, $condition = '', $order = '') {
371 global $CFG;
373 if (!empty($condition)) {
374 $condition = ' AND '.$condition;
376 if (empty($order)) {
377 $order = "timemodified DESC";
379 return get_records_select("dialogue_conversations", "dialogueid = $dialogue->id AND
380 (userid = $user->id OR recipientid = $user->id) $condition", $order);
386 //////////////////////////////////////////////////////////////////////////////////////
387 function dialogue_get_open_conversations($course) {
388 // get the conversations which are waiting for a response for this user.
389 // Add the first and last names of the other participant
390 global $CFG, $USER;
391 if (empty($USER->id)) {
392 return false;
394 if ($conversations = get_records_sql("SELECT d.name AS dialoguename, c.id, c.dialogueid, c.timemodified, c.lastid
395 FROM {$CFG->prefix}dialogue d, {$CFG->prefix}dialogue_conversations c
396 WHERE d.course = $course->id
397 AND c.dialogueid = d.id
398 AND (c.userid = $USER->id OR c.recipientid = $USER->id)
399 AND c.lastid != $USER->id
400 AND c.closed =0")) {
402 foreach ($conversations as $conversation) {
403 if (!$user = get_record("user", "id", $conversation->lastid)) {
404 error("Get open conversations: user record not found");
406 if (!$cm = get_coursemodule_from_instance("dialogue", $conversation->dialogueid, $course->id)) {
407 error("Course Module ID was incorrect");
409 $entry[$conversation->id]->dialogueid = $conversation->dialogueid;
410 $entry[$conversation->id]->time = $conversation->timemodified;
411 $entry[$conversation->id]->url = "view.php?id=$cm->id";
412 $entry[$conversation->id]->firstname = $user->firstname;
413 $entry[$conversation->id]->lastname = $user->lastname;
414 $entry[$conversation->id]->name = $conversation->dialoguename;
416 return $entry;
418 return;
422 //////////////////////////////////////////////////////////////////////////////////////
423 function dialogue_get_user_entries($dialogue, $user) {
424 global $CFG;
425 return get_records_select("dialogue_entries", "dialogueid = $dialogue->id AND userid = $user->id",
426 "timecreated DESC");