4 if (!isset($CFG->journal_showrecentactivity
)) {
5 set_config("journal_showrecentactivity", true);
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
;
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>";
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();
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 = " (<a href=\"report.php?id=$journal->coursemodule\">".get_string("viewallentries","journal", $entrycount)."</a>)";
58 $journal->name
= "<a href=\"view.php?id=$journal->coursemodule\">".format_string($journal->name
,true)."</a>";
61 echo "<h3>$heading - $journal->name$entryinfo</h3>";
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\" />";
72 if (isstudent($course->id
) or isteacher($course->id
)) {
74 print_simple_box_start("right", "90%");
77 echo "<p align=\"right\"><a href=\"edit.php?id=$journal->coursemodule\">";
78 echo get_string("edit")."</a></p>";
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>";
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);
96 print_string("noentry", "journal");
99 print_simple_box_end();
100 echo "<br clear=\"all\" />";
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
113 $cutofftime = time() - $CFG->maxeditingtime
;
115 if ($entries = journal_get_unmailed_graded($cutofftime)) {
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";
127 $USER->lang
= $user->lang
;
129 if (! $course = get_record("course", "id", "$entry->course")) {
130 echo "Could not find course $entry->course\n";
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";
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";
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 />";
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";
185 function journal_print_recent_activity($course, $isteacher, $timestart) {
188 if (!empty($CFG->journal_showrecentactivity
)) { // Don't even bother
195 // log table should not be used here
197 if (!$logs = get_records_select('log', 'time > \''.$timestart.'\' AND '.
198 'course = \''.$course->id
.'\' AND '.
199 'module = \'journal\' AND '.
200 '(action = \'add entry\' OR action = \'update entry\')', 'time ASC')){
204 foreach ($logs as $log) {
205 ///Get journal info. I'll need it later
206 $j_log_info = journal_log_info($log);
208 $cm = $modinfo->instances
['journal'][$j_log_info->id
];
209 if (!$cm->uservisible
) {
213 if (!isset($journals[$log->info
])) {
214 $journals[$log->info
] = $j_log_info;
215 $journals[$log->info
]->time
= $log->time
;
216 $journals[$log->info
]->url
= str_replace('&', '&', $log->url
);
222 print_headline(get_string('newjournalentries', 'journal').':');
223 foreach ($journals as $journal) {
224 print_recent_activity_note($journal->time
, $journal, $journal->name
,
225 $CFG->wwwroot
.'/mod/journal/'.$journal->url
);
232 function journal_grades($journalid) {
233 /// Must return an array of grades, indexed by user, and a max grade.
235 if (!$journal = get_record("journal", "id", $journalid)) {
239 $grades = get_records_menu("journal_entries", "journal",
240 $journal->id
, "", "userid,rating");
242 if ($journal->assessed
> 0) {
243 $return->grades
= $grades;
244 $return->maxgrade
= $journal->assessed
;
246 } else if ($journal->assessed
== 0) {
250 if ($scale = get_record("scale", "id", - $journal->assessed
)) {
251 $scalegrades = make_menu_from_list($scale->scale
);
253 foreach ($grades as $key => $grade) {
254 $grades[$key] = $scalegrades[$grade];
258 $return->grades
= $grades;
259 $return->maxgrade
= "";
265 function journal_get_participants($journalid) {
266 //Returns the users with data in one journal
267 //(users with records in journal_entries, students and teachers)
272 $students = get_records_sql("SELECT DISTINCT u.id, u.id
273 FROM {$CFG->prefix}user u,
274 {$CFG->prefix}journal_entries j
275 WHERE j.journal = '$journalid' and
278 $teachers = get_records_sql("SELECT DISTINCT u.id, u.id
279 FROM {$CFG->prefix}user u,
280 {$CFG->prefix}journal_entries j
281 WHERE j.journal = '$journalid' and
284 //Add teachers to students
286 foreach ($teachers as $teacher) {
287 $students[$teacher->id
] = $teacher;
290 //Return students array (it contains an array of unique users)
294 function journal_scale_used ($journalid,$scaleid) {
295 //This function returns if a scale is being used by one journal
299 $rec = get_record("journal","id","$journalid","assessed","-$scaleid");
301 if (!empty($rec) && !empty($scaleid)) {
309 * Checks if scale is being used by any instance of journal
311 * This is used to find out if scale used anywhere
312 * @param $scaleid int
313 * @return boolean True if the scale is used by any journal
315 function journal_scale_used_anywhere($scaleid) {
316 if ($scaleid and record_exists('journal', 'assessed', -$scaleid)) {
323 // SQL FUNCTIONS ///////////////////////////////////////////////////////////////////
325 function journal_get_users_done($journal) {
328 $journals = get_records_sql ("SELECT u.*
329 FROM {$CFG->prefix}journal_entries j,
331 WHERE j.userid = u.id
332 AND j.journal = $journal->id
333 ORDER BY j.modified DESC");
335 if (empty($journals)) {
339 // remove unenrolled participants
340 foreach ($journals as $key=>$user) {
341 if (!isteacher($journal->course
, $user->id
) and !isstudent($journal->course
, $user->id
)) {
342 unset($journals[$key]);
348 function journal_count_entries($journal, $groupid=0) {
349 /// Counts all the journal entries (optionally in a given group)
353 if ($groupid) { /// How many in a particular group?
354 return count_records_sql("SELECT COUNT(*)
355 FROM {$CFG->prefix}journal_entries j,
356 {$CFG->prefix}groups_members g
357 WHERE j.journal = $journal->id
358 AND g.groupid = '$groupid'
359 AND j.userid = g.userid");
361 } else { /// Count all the entries from the whole course
363 $journals = get_records_sql ("SELECT u.*
364 FROM {$CFG->prefix}journal_entries j,
366 WHERE j.userid = u.id
367 AND j.journal = $journal->id
368 ORDER BY j.modified DESC");
370 if (empty($journals)) {
374 // remove unenrolled participants
375 foreach ($journals as $key=>$user) {
376 if (!isteacher($journal->course
, $user->id
) and !isstudent($journal->course
, $user->id
)) {
377 unset($journals[$key]);
380 return count($journals);
385 function journal_get_unmailed_graded($cutofftime) {
387 return get_records_sql("SELECT e.*, j.course, j.name
388 FROM {$CFG->prefix}journal_entries e,
389 {$CFG->prefix}journal j
391 AND e.timemarked < '$cutofftime'
393 AND e.journal = j.id");
396 function journal_log_info($log) {
398 return get_record_sql("SELECT j.*, u.firstname, u.lastname
399 FROM {$CFG->prefix}journal j,
400 {$CFG->prefix}journal_entries e,
402 WHERE e.id = '$log->info'
404 AND e.userid = u.id");
407 // OTHER JOURNAL FUNCTIONS ///////////////////////////////////////////////////////////////////
411 function journal_print_user_entry($course, $user, $entry, $teachers, $grades) {
414 echo "\n<table border=\"1\" cellspacing=\"0\" valign=\"top\" cellpadding=\"10\">";
417 echo "\n<td rowspan=\"2\" width=\"35\" valign=\"top\">";
418 print_user_picture($user->id
, $course->id
, $user->picture
);
420 echo "<td nowrap=\"nowrap\" width=\"100%\">".fullname($user);
422 echo " <font size=\"1\">".get_string("lastedited").": ".userdate($entry->modified
)."</font>";
427 echo "\n<tr><td width=\"100%\">";
429 echo format_text($entry->text
, $entry->format
);
431 print_string("noentry", "journal");
437 echo "<td width=\"35\" valign=\"top\">";
438 if (!$entry->teacher
) {
439 $entry->teacher
= $USER->id
;
441 print_user_picture($entry->teacher
, $course->id
, $teachers[$entry->teacher
]->picture
);
443 echo "<td>".get_string("feedback").":";
444 choose_from_menu($grades, "r$entry->id", $entry->rating
, get_string("nograde")."...");
445 if ($entry->timemarked
) {
446 echo " <font size=\"1\">".userdate($entry->timemarked
)."</font>";
448 echo "<br /><textarea name=\"c$entry->id\" rows=\"12\" cols=\"60\" wrap=\"virtual\">";
449 p($entry->entrycomment
);
450 echo "</textarea><br />";
453 echo "</table><br clear=\"all\" />\n";
457 function journal_add_instance($journal) {
458 // Given an object containing all the necessary data,
459 // (defined by the form in mod.html) this function
460 // will create a new instance and return the id number
461 // of the new instance.
463 $journal->timemodified
= time();
465 return insert_record("journal", $journal);
469 function journal_update_instance($journal) {
470 // Given an object containing all the necessary data,
471 // (defined by the form in mod.html) this function
472 // will update an existing instance with new data.
474 $journal->timemodified
= time();
475 $journal->id
= $journal->instance
;
477 return update_record("journal", $journal);
481 function journal_delete_instance($id) {
482 // Given an ID of an instance of this module,
483 // this function will permanently delete the instance
484 // and any data that depends on it.
486 if (! $journal = get_record("journal", "id", $id)) {
492 if (! delete_records("journal_entries", "journal", $journal->id
)) {
496 if (! delete_records("journal", "id", $journal->id
)) {
505 function journal_print_feedback($course, $entry, $grades) {
508 if (! $teacher = get_record('user', 'id', $entry->teacher
)) {
509 error('Weird journal error');
512 echo '<table cellspacing="0" align="center" class="feedbackbox">';
515 echo '<td class="left picture">';
516 print_user_picture($teacher->id
, $course->id
, $teacher->picture
);
518 echo '<td class="entryheader">';
519 echo '<span class="author">'.fullname($teacher).'</span>';
520 echo ' <span class="time">'.userdate($entry->timemarked
).'</span>';
525 echo '<td class="left side"> </td>';
526 echo '<td class="entrycontent">';
528 echo '<div class="grade">';
530 if (!empty($entry->rating
) and !empty($grades[$entry->rating
])) {
531 echo get_string('grade').': ';
532 echo $grades[$entry->rating
];
534 print_string('nograde');
538 echo format_text($entry->entrycomment
);
539 echo '</td></tr></table>';
542 function journal_get_view_actions() {
543 return array('view','view all','view responses');
546 function journal_get_post_actions() {
547 return array('add entry','update entry','update feedback');
551 * Returns all other caps used in module
553 function journal_get_extra_capabilities() {
554 return array('moodle/site:accessallgroups');