Automatic installer.php lang files by installer_builder (20070726)
[moodle-linuxchix.git] / mod / journal / lib.php
blob4ff157c234b596a886ca7f885699e65393047e40
1 <?php // $Id$
4 if (!isset($CFG->journal_showrecentactivity)) {
5 set_config("journal_showrecentactivity", true);
6 }
10 // STANDARD MODULE FUNCTIONS /////////////////////////////////////////////////////////
12 function journal_user_outline($course, $user, $mod, $journal) {
13 if ($entry = get_record("journal_entries", "userid", $user->id, "journal", $journal->id)) {
15 $numwords = count(preg_split("/\w\b/", $entry->text)) - 1;
17 $result->info = get_string("numwords", "", $numwords);
18 $result->time = $entry->modified;
19 return $result;
21 return NULL;
25 function journal_user_complete($course, $user, $mod, $journal) {
27 if ($entry = get_record("journal_entries", "userid", $user->id, "journal", $journal->id)) {
29 print_simple_box_start();
30 if ($entry->modified) {
31 echo "<p><font size=\"1\">".get_string("lastedited").": ".userdate($entry->modified)."</font></p>";
33 if ($entry->text) {
34 echo format_text($entry->text, $entry->format);
36 if ($entry->teacher) {
37 $grades = make_grades_menu($journal->assessed);
38 journal_print_feedback($course, $entry, $grades);
40 print_simple_box_end();
42 } else {
43 print_string("noentry", "journal");
48 function journal_user_complete_index($course, $user, $journal, $journalopen, $heading) {
49 /// Prints a journal, entry and feedback ... used on the journal index page.
51 if (isteacher($course->id)) {
52 $entrycount = journal_count_entries($journal, get_current_group($course->id));
53 $entryinfo = "&nbsp;(<a href=\"report.php?id=$journal->coursemodule\">".get_string("viewallentries","journal", $entrycount)."</a>)";
54 } else {
55 $entryinfo = "";
58 $journal->name = "<a href=\"view.php?id=$journal->coursemodule\">".format_string($journal->name,true)."</a>";
60 if ($heading) {
61 echo "<h3>$heading - $journal->name$entryinfo</h3>";
62 } else {
63 echo "<h3>$journal->name$entryinfo</h3>";
66 print_simple_box_start("left", "90%");
67 echo format_text($journal->intro, $journal->introformat);
68 print_simple_box_end();
69 echo "<br clear=\"all\" />";
70 echo "<br />";
72 if (isstudent($course->id) or isteacher($course->id)) {
74 print_simple_box_start("right", "90%");
76 if ($journalopen) {
77 echo "<p align=\"right\"><a href=\"edit.php?id=$journal->coursemodule\">";
78 echo get_string("edit")."</a></p>";
79 } else {
80 echo "<p align=\"right\"><a href=\"view.php?id=$journal->coursemodule\">";
81 echo get_string("view")."</a></p>";
84 if ($entry = get_record("journal_entries", "userid", $user->id, "journal", $journal->id)) {
85 if ($entry->modified) {
86 echo "<p align=\"center\"><font size=\"1\">".get_string("lastedited").": ".userdate($entry->modified)."</font></p>";
88 if ($entry->text) {
89 echo format_text($entry->text, $entry->format);
91 if ($entry->teacher) {
92 $grades = make_grades_menu($journal->assessed);
93 journal_print_feedback($course, $entry, $grades);
95 } else {
96 print_string("noentry", "journal");
99 print_simple_box_end();
100 echo "<br clear=\"all\" />";
101 echo "<br />";
107 function journal_cron () {
108 // Function to be run periodically according to the moodle cron
109 // Finds all journal notifications that have yet to be mailed out, and mails them
111 global $CFG, $USER;
113 $cutofftime = time() - $CFG->maxeditingtime;
115 if ($entries = journal_get_unmailed_graded($cutofftime)) {
116 $timenow = time();
118 foreach ($entries as $entry) {
120 echo "Processing journal entry $entry->id\n";
122 if (! $user = get_record("user", "id", "$entry->userid")) {
123 echo "Could not find user $entry->userid\n";
124 continue;
127 $USER->lang = $user->lang;
129 if (! $course = get_record("course", "id", "$entry->course")) {
130 echo "Could not find course $entry->course\n";
131 continue;
134 if (! isstudent($course->id, $user->id) and !isteacher($course->id, $user->id)) {
135 continue; // Not an active participant
138 if (! $teacher = get_record("user", "id", "$entry->teacher")) {
139 echo "Could not find teacher $entry->teacher\n";
140 continue;
144 if (! $mod = get_coursemodule_from_instance("journal", $entry->journal, $course->id)) {
145 echo "Could not find course module for journal id $entry->journal\n";
146 continue;
149 unset($journalinfo);
150 $journalinfo->teacher = fullname($teacher);
151 $journalinfo->journal = format_string($entry->name,true);
152 $journalinfo->url = "$CFG->wwwroot/mod/journal/view.php?id=$mod->id";
153 $modnamepl = get_string( 'modulenameplural','journal' );
154 $msubject = get_string( 'mailsubject','journal' );
156 $postsubject = "$course->shortname: $msubject: ".format_string($entry->name,true);
157 $posttext = "$course->shortname -> $modnamepl -> ".format_string($entry->name,true)."\n";
158 $posttext .= "---------------------------------------------------------------------\n";
159 $posttext .= get_string("journalmail", "journal", $journalinfo)."\n";
160 $posttext .= "---------------------------------------------------------------------\n";
161 if ($user->mailformat == 1) { // HTML
162 $posthtml = "<p><font face=\"sans-serif\">".
163 "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> ->".
164 "<a href=\"$CFG->wwwroot/mod/journal/index.php?id=$course->id\">journals</a> ->".
165 "<a href=\"$CFG->wwwroot/mod/journal/view.php?id=$mod->id\">".format_string($entry->name,true)."</a></font></p>";
166 $posthtml .= "<hr /><font face=\"sans-serif\">";
167 $posthtml .= "<p>".get_string("journalmailhtml", "journal", $journalinfo)."</p>";
168 $posthtml .= "</font><hr />";
169 } else {
170 $posthtml = "";
173 if (! email_to_user($user, $teacher, $postsubject, $posttext, $posthtml)) {
174 echo "Error: Journal cron: Could not send out mail for id $entry->id to user $user->id ($user->email)\n";
176 if (! set_field("journal_entries", "mailed", "1", "id", "$entry->id")) {
177 echo "Could not update the mailed field for id $entry->id\n";
182 return true;
185 function journal_print_recent_activity($course, $isteacher, $timestart) {
186 global $CFG;
188 if (!empty($CFG->journal_showrecentactivity)) { // Don't even bother
189 return false;
192 $content = false;
193 $journals = NULL;
195 if (!$logs = get_records_select('log', 'time > \''.$timestart.'\' AND '.
196 'course = \''.$course->id.'\' AND '.
197 'module = \'journal\' AND '.
198 '(action = \'add entry\' OR action = \'update entry\')', 'time ASC')){
199 return false;
202 foreach ($logs as $log) {
203 ///Get journal info. I'll need it later
204 $j_log_info = journal_log_info($log);
206 //Create a temp valid module structure (course,id)
207 $tempmod->course = $log->course;
208 $tempmod->id = $j_log_info->id;
209 //Obtain the visible property from the instance
210 $modvisible = instance_is_visible($log->module,$tempmod);
212 //Only if the mod is visible
213 if ($modvisible) {
214 if (!isset($journals[$log->info])) {
215 $journals[$log->info] = $j_log_info;
216 $journals[$log->info]->time = $log->time;
217 $journals[$log->info]->url = str_replace('&', '&amp;', $log->url);
222 if ($journals) {
223 $content = true;
224 print_headline(get_string('newjournalentries', 'journal').':');
225 foreach ($journals as $journal) {
226 print_recent_activity_note($journal->time, $journal, $journal->name,
227 $CFG->wwwroot.'/mod/journal/'.$journal->url);
231 return $content;
234 function journal_grades($journalid) {
235 /// Must return an array of grades, indexed by user, and a max grade.
237 if (!$journal = get_record("journal", "id", $journalid)) {
238 return NULL;
241 $grades = get_records_menu("journal_entries", "journal",
242 $journal->id, "", "userid,rating");
244 if ($journal->assessed > 0) {
245 $return->grades = $grades;
246 $return->maxgrade = $journal->assessed;
248 } else if ($journal->assessed == 0) {
249 return NULL;
251 } else {
252 if ($scale = get_record("scale", "id", - $journal->assessed)) {
253 $scalegrades = make_menu_from_list($scale->scale);
254 if ($grades) {
255 foreach ($grades as $key => $grade) {
256 $grades[$key] = $scalegrades[$grade];
260 $return->grades = $grades;
261 $return->maxgrade = "";
264 return $return;
267 function journal_get_participants($journalid) {
268 //Returns the users with data in one journal
269 //(users with records in journal_entries, students and teachers)
271 global $CFG;
273 //Get students
274 $students = get_records_sql("SELECT DISTINCT u.id, u.id
275 FROM {$CFG->prefix}user u,
276 {$CFG->prefix}journal_entries j
277 WHERE j.journal = '$journalid' and
278 u.id = j.userid");
279 //Get teachers
280 $teachers = get_records_sql("SELECT DISTINCT u.id, u.id
281 FROM {$CFG->prefix}user u,
282 {$CFG->prefix}journal_entries j
283 WHERE j.journal = '$journalid' and
284 u.id = j.teacher");
286 //Add teachers to students
287 if ($teachers) {
288 foreach ($teachers as $teacher) {
289 $students[$teacher->id] = $teacher;
292 //Return students array (it contains an array of unique users)
293 return ($students);
296 function journal_scale_used ($journalid,$scaleid) {
297 //This function returns if a scale is being used by one journal
299 $return = false;
301 $rec = get_record("journal","id","$journalid","assessed","-$scaleid");
303 if (!empty($rec) && !empty($scaleid)) {
304 $return = true;
307 return $return;
310 // SQL FUNCTIONS ///////////////////////////////////////////////////////////////////
312 function journal_get_users_done($journal) {
313 global $CFG;
315 $journals = get_records_sql ("SELECT u.*
316 FROM {$CFG->prefix}journal_entries j,
317 {$CFG->prefix}user u
318 WHERE j.userid = u.id
319 AND j.journal = $journal->id
320 ORDER BY j.modified DESC");
322 if (empty($journals)) {
323 return NULL;
326 // remove unenrolled participants
327 foreach ($journals as $key=>$user) {
328 if (!isteacher($journal->course, $user->id) and !isstudent($journal->course, $user->id)) {
329 unset($journals[$key]);
332 return $journals;
335 function journal_count_entries($journal, $groupid=0) {
336 /// Counts all the journal entries (optionally in a given group)
338 global $CFG, $db;
340 if ($groupid) { /// How many in a particular group?
341 return count_records_sql("SELECT COUNT(*)
342 FROM {$CFG->prefix}journal_entries j,
343 ".groups_members_from_sql()."
344 WHERE j.journal = $journal->id
345 AND ".groups_members_where_sql($groupid, 'j.userid'));
347 } else { /// Count all the entries from the whole course
349 $journals = get_records_sql ("SELECT u.*
350 FROM {$CFG->prefix}journal_entries j,
351 {$CFG->prefix}user u
352 WHERE j.userid = u.id
353 AND j.journal = $journal->id
354 ORDER BY j.modified DESC");
356 if (empty($journals)) {
357 return 0;
360 // remove unenrolled participants
361 foreach ($journals as $key=>$user) {
362 if (!isteacher($journal->course, $user->id) and !isstudent($journal->course, $user->id)) {
363 unset($journals[$key]);
366 return count($journals);
371 function journal_get_unmailed_graded($cutofftime) {
372 global $CFG;
373 return get_records_sql("SELECT e.*, j.course, j.name
374 FROM {$CFG->prefix}journal_entries e,
375 {$CFG->prefix}journal j
376 WHERE e.mailed = '0'
377 AND e.timemarked < '$cutofftime'
378 AND e.timemarked > 0
379 AND e.journal = j.id");
382 function journal_log_info($log) {
383 global $CFG;
384 return get_record_sql("SELECT j.*, u.firstname, u.lastname
385 FROM {$CFG->prefix}journal j,
386 {$CFG->prefix}journal_entries e,
387 {$CFG->prefix}user u
388 WHERE e.id = '$log->info'
389 AND e.journal = j.id
390 AND e.userid = u.id");
393 // OTHER JOURNAL FUNCTIONS ///////////////////////////////////////////////////////////////////
397 function journal_print_user_entry($course, $user, $entry, $teachers, $grades) {
398 global $USER;
400 echo "\n<table border=\"1\" cellspacing=\"0\" valign=\"top\" cellpadding=\"10\">";
402 echo "\n<tr>";
403 echo "\n<td rowspan=\"2\" width=\"35\" valign=\"top\">";
404 print_user_picture($user->id, $course->id, $user->picture);
405 echo "</td>";
406 echo "<td nowrap=\"nowrap\" width=\"100%\">".fullname($user);
407 if ($entry) {
408 echo "&nbsp;&nbsp;<font size=\"1\">".get_string("lastedited").": ".userdate($entry->modified)."</font>";
410 echo "</td>";
411 echo "</tr>";
413 echo "\n<tr><td width=\"100%\">";
414 if ($entry) {
415 echo format_text($entry->text, $entry->format);
416 } else {
417 print_string("noentry", "journal");
419 echo "</td></tr>";
421 if ($entry) {
422 echo "\n<tr>";
423 echo "<td width=\"35\" valign=\"top\">";
424 if (!$entry->teacher) {
425 $entry->teacher = $USER->id;
427 print_user_picture($entry->teacher, $course->id, $teachers[$entry->teacher]->picture);
428 echo "</td>";
429 echo "<td>".get_string("feedback").":";
430 choose_from_menu($grades, "r$entry->id", $entry->rating, get_string("nograde")."...");
431 if ($entry->timemarked) {
432 echo "&nbsp;&nbsp;<font size=\"1\">".userdate($entry->timemarked)."</font>";
434 echo "<br /><textarea name=\"c$entry->id\" rows=\"12\" cols=\"60\" wrap=\"virtual\">";
435 p($entry->entrycomment);
436 echo "</textarea><br />";
437 echo "</td></tr>";
439 echo "</table><br clear=\"all\" />\n";
443 function journal_add_instance($journal) {
444 // Given an object containing all the necessary data,
445 // (defined by the form in mod.html) this function
446 // will create a new instance and return the id number
447 // of the new instance.
449 $journal->timemodified = time();
451 return insert_record("journal", $journal);
455 function journal_update_instance($journal) {
456 // Given an object containing all the necessary data,
457 // (defined by the form in mod.html) this function
458 // will update an existing instance with new data.
460 $journal->timemodified = time();
461 $journal->id = $journal->instance;
463 return update_record("journal", $journal);
467 function journal_delete_instance($id) {
468 // Given an ID of an instance of this module,
469 // this function will permanently delete the instance
470 // and any data that depends on it.
472 if (! $journal = get_record("journal", "id", $id)) {
473 return false;
476 $result = true;
478 if (! delete_records("journal_entries", "journal", $journal->id)) {
479 $result = false;
482 if (! delete_records("journal", "id", $journal->id)) {
483 $result = false;
486 return $result;
491 function journal_print_feedback($course, $entry, $grades) {
492 global $CFG;
494 if (! $teacher = get_record('user', 'id', $entry->teacher)) {
495 error('Weird journal error');
498 echo '<table cellspacing="0" align="center" class="feedbackbox">';
500 echo '<tr>';
501 echo '<td class="left picture">';
502 print_user_picture($teacher->id, $course->id, $teacher->picture);
503 echo '</td>';
504 echo '<td class="entryheader">';
505 echo '<span class="author">'.fullname($teacher).'</span>';
506 echo '&nbsp;&nbsp;<span class="time">'.userdate($entry->timemarked).'</span>';
507 echo '</td>';
508 echo '</tr>';
510 echo '<tr>';
511 echo '<td class="left side">&nbsp;</td>';
512 echo '<td class="entrycontent">';
514 echo '<div class="grade">';
516 if (!empty($entry->rating) and !empty($grades[$entry->rating])) {
517 echo get_string('grade').': ';
518 echo $grades[$entry->rating];
519 } else {
520 print_string('nograde');
522 echo '</div>';
524 echo format_text($entry->entrycomment);
525 echo '</td></tr></table>';
528 function journal_get_view_actions() {
529 return array('view','view all','view responses');
532 function journal_get_post_actions() {
533 return array('add entry','update entry','update feedback');