File: lib.php

package info (click to toggle)
moodle 1.6.3-2%2Betch3
  • links: PTS
  • area: main
  • in suites: etch
  • size: 37,172 kB
  • ctags: 51,688
  • sloc: php: 231,916; sql: 5,631; xml: 2,688; sh: 1,185; perl: 638; makefile: 48; pascal: 36
file content (556 lines) | stat: -rw-r--r-- 19,790 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
<?php // $Id: lib.php,v 1.75 2005/09/01 04:14:30 mjollnir_ Exp $


if (!isset($CFG->journal_showrecentactivity)) {
    set_config("journal_showrecentactivity", true);
} 



// STANDARD MODULE FUNCTIONS /////////////////////////////////////////////////////////

function journal_user_outline($course, $user, $mod, $journal) {
    if ($entry = get_record("journal_entries", "userid", $user->id, "journal", $journal->id)) {

        $numwords = count(preg_split("/\w\b/", $entry->text)) - 1;

        $result->info = get_string("numwords", "", $numwords);
        $result->time = $entry->modified;
        return $result;
    }
    return NULL;
}


function journal_user_complete($course, $user, $mod, $journal) {

    if ($entry = get_record("journal_entries", "userid", $user->id, "journal", $journal->id)) {

        print_simple_box_start();
        if ($entry->modified) {
            echo "<p><font size=\"1\">".get_string("lastedited").": ".userdate($entry->modified)."</font></p>";
        }
        if ($entry->text) {
            echo format_text($entry->text, $entry->format);
        }
        if ($entry->teacher) {
            $grades = make_grades_menu($journal->assessed);
            journal_print_feedback($course, $entry, $grades);
        }
        print_simple_box_end();

    } else {
        print_string("noentry", "journal");
    }
}


function journal_user_complete_index($course, $user, $journal, $journalopen, $heading) {
/// Prints a journal, entry and feedback ... used on the journal index page.

    if (isteacher($course->id)) {
        $entrycount = journal_count_entries($journal, get_current_group($course->id));
        $entryinfo  = "&nbsp;(<a href=\"report.php?id=$journal->coursemodule\">".get_string("viewallentries","journal", $entrycount)."</a>)";
    } else {
        $entryinfo = "";
    }

    $journal->name = "<a href=\"view.php?id=$journal->coursemodule\">".format_string($journal->name,true)."</a>";

    if ($heading) {
        echo "<h3>$heading - $journal->name$entryinfo</h3>";
    } else {
        echo "<h3>$journal->name$entryinfo</h3>";
    }

    print_simple_box_start("left", "90%");
    echo format_text($journal->intro,  $journal->introformat);
    print_simple_box_end();
    echo "<br clear=\"all\" />";
    echo "<br />";
    
    if (isstudent($course->id) or isteacher($course->id)) {

        print_simple_box_start("right", "90%");
    
        if ($journalopen) {
            echo "<p align=\"right\"><a href=\"edit.php?id=$journal->coursemodule\">";
            echo get_string("edit")."</a></p>";
        } else {
            echo "<p align=\"right\"><a href=\"view.php?id=$journal->coursemodule\">";
            echo get_string("view")."</a></p>";
        }
    
        if ($entry = get_record("journal_entries", "userid", $user->id, "journal", $journal->id)) {
            if ($entry->modified) {
                echo "<p align=\"center\"><font size=\"1\">".get_string("lastedited").": ".userdate($entry->modified)."</font></p>";
            }
            if ($entry->text) {
                echo format_text($entry->text, $entry->format);
            }
            if ($entry->teacher) {
                $grades = make_grades_menu($journal->assessed);
                journal_print_feedback($course, $entry, $grades);
            }
        } else {
            print_string("noentry", "journal");
        }
    
        print_simple_box_end();
        echo "<br clear=\"all\" />";
        echo "<br />";
    }

}


function journal_cron () {
// Function to be run periodically according to the moodle cron
// Finds all journal notifications that have yet to be mailed out, and mails them

    global $CFG, $USER;

    $cutofftime = time() - $CFG->maxeditingtime;

    if ($entries = journal_get_unmailed_graded($cutofftime)) {
        $timenow = time();

        foreach ($entries as $entry) {

            echo "Processing journal entry $entry->id\n";

            if (! $user = get_record("user", "id", "$entry->userid")) {
                echo "Could not find user $entry->userid\n";
                continue;
            }

            $USER->lang = $user->lang;

            if (! $course = get_record("course", "id", "$entry->course")) {
                echo "Could not find course $entry->course\n";
                continue;
            }

            if (! isstudent($course->id, $user->id) and !isteacher($course->id, $user->id)) {
                continue;  // Not an active participant
            }

            if (! $teacher = get_record("user", "id", "$entry->teacher")) {
                echo "Could not find teacher $entry->teacher\n";
                continue;
            }


            if (! $mod = get_coursemodule_from_instance("journal", $entry->journal, $course->id)) {
                echo "Could not find course module for journal id $entry->journal\n";
                continue;
            }

            unset($journalinfo);
            $journalinfo->teacher = fullname($teacher);
            $journalinfo->journal = format_string($entry->name,true);
            $journalinfo->url = "$CFG->wwwroot/mod/journal/view.php?id=$mod->id";
            $modnamepl = get_string( 'modulenameplural','journal' );
            $msubject = get_string( 'mailsubject','journal' );

            $postsubject = "$course->shortname: $msubject: ".format_string($entry->name,true);
            $posttext  = "$course->shortname -> $modnamepl -> ".format_string($entry->name,true)."\n";
            $posttext .= "---------------------------------------------------------------------\n";
            $posttext .= get_string("journalmail", "journal", $journalinfo)."\n";
            $posttext .= "---------------------------------------------------------------------\n";
            if ($user->mailformat == 1) {  // HTML
                $posthtml = "<p><font face=\"sans-serif\">".
                "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> ->".
                "<a href=\"$CFG->wwwroot/mod/journal/index.php?id=$course->id\">journals</a> ->".
                "<a href=\"$CFG->wwwroot/mod/journal/view.php?id=$mod->id\">".format_string($entry->name,true)."</a></font></p>";
                $posthtml .= "<hr /><font face=\"sans-serif\">";
                $posthtml .= "<p>".get_string("journalmailhtml", "journal", $journalinfo)."</p>";
                $posthtml .= "</font><hr />";
            } else {
              $posthtml = "";
            }

            if (! email_to_user($user, $teacher, $postsubject, $posttext, $posthtml)) {
                echo "Error: Journal cron: Could not send out mail for id $entry->id to user $user->id ($user->email)\n";
            }
            if (! set_field("journal_entries", "mailed", "1", "id", "$entry->id")) {
                echo "Could not update the mailed field for id $entry->id\n";
            }
        }
    }

    return true;
}

function journal_print_recent_activity($course, $isteacher, $timestart) {
    global $CFG;

    if (!empty($CFG->journal_showrecentactivity)) {    // Don't even bother
        return false;
    }

    $content = false;
    $journals = NULL;

    if (!$logs = get_records_select('log', 'time > \''.$timestart.'\' AND '.
                                           'course = \''.$course->id.'\' AND '.
                                           'module = \'journal\' AND '.
                                           '(action = \'add entry\' OR action = \'update entry\')', 'time ASC')){
        return false;
    }

    foreach ($logs as $log) {
        ///Get journal info.  I'll need it later
        $j_log_info = journal_log_info($log);

        //Create a temp valid module structure (course,id)
        $tempmod->course = $log->course;
        $tempmod->id = $j_log_info->id;
        //Obtain the visible property from the instance
        $modvisible = instance_is_visible($log->module,$tempmod);

        //Only if the mod is visible
        if ($modvisible) {
            if (!isset($journals[$log->info])) {
                $journals[$log->info] = $j_log_info;
                $journals[$log->info]->time = $log->time;
                $journals[$log->info]->url = str_replace('&', '&amp;', $log->url);
            }
        }
    }

    if ($journals) {
        $content = true;
        print_headline(get_string('newjournalentries', 'journal').':');
        foreach ($journals as $journal) {
            print_recent_activity_note($journal->time, $journal, $isteacher, $journal->name,
                                       $CFG->wwwroot.'/mod/journal/'.$journal->url);
        }
    }
 
    return $content;
}

function journal_grades($journalid) {
/// Must return an array of grades, indexed by user, and a max grade.

    if (!$journal = get_record("journal", "id", $journalid)) {
        return NULL;
    }

    $grades = get_records_menu("journal_entries", "journal", 
                               $journal->id, "", "userid,rating");

    if ($journal->assessed > 0) {
        $return->grades = $grades;
        $return->maxgrade = $journal->assessed;

    } else if ($journal->assessed == 0) {
        return NULL;

    } else {
        if ($scale = get_record("scale", "id", - $journal->assessed)) {
            $scalegrades = make_menu_from_list($scale->scale);
            if ($grades) {
                foreach ($grades as $key => $grade) {
                    $grades[$key] = $scalegrades[$grade];
                }
            }
        }
        $return->grades = $grades;
        $return->maxgrade = "";
    }

    return $return;
}

function journal_get_participants($journalid) {
//Returns the users with data in one journal
//(users with records in journal_entries, students and teachers)

    global $CFG;

    //Get students
    $students = get_records_sql("SELECT DISTINCT u.id, u.id
                                 FROM {$CFG->prefix}user u,
                                      {$CFG->prefix}journal_entries j
                                 WHERE j.journal = '$journalid' and
                                       u.id = j.userid");
    //Get teachers
    $teachers = get_records_sql("SELECT DISTINCT u.id, u.id
                                 FROM {$CFG->prefix}user u,
                                      {$CFG->prefix}journal_entries j
                                 WHERE j.journal = '$journalid' and
                                       u.id = j.teacher");

    //Add teachers to students
    if ($teachers) {
        foreach ($teachers as $teacher) {
            $students[$teacher->id] = $teacher;
        }
    }
    //Return students array (it contains an array of unique users)
    return ($students);
}

function journal_scale_used ($journalid,$scaleid) {
//This function returns if a scale is being used by one journal
    
    $return = false;                  
                                 
    $rec = get_record("journal","id","$journalid","assessed","-$scaleid");

    if (!empty($rec) && !empty($scaleid)) {
        $return = true;
    }

    return $return;
}

// SQL FUNCTIONS ///////////////////////////////////////////////////////////////////

function journal_get_users_done($journal) {
    global $CFG;

    // make sure it works on the site course
    $select = "s.course = '$journal->course' AND";
    if ($journal->course == SITEID) {
        $select = '';
    }

    $studentjournals = get_records_sql ("SELECT u.*
                                  FROM {$CFG->prefix}journal_entries j,
                                       {$CFG->prefix}user u, 
                                       {$CFG->prefix}user_students s
                                 WHERE j.userid = u.id
                                   AND s.userid = u.id 
                                   AND $select j.journal = $journal->id
                              ORDER BY j.modified DESC");

    $teacherjournals = get_records_sql ("SELECT u.*
                                  FROM {$CFG->prefix}journal_entries j,
                                       {$CFG->prefix}user u, 
                                       {$CFG->prefix}user_teachers t
                                 WHERE j.userid = u.id
                                   AND t.userid = u.id 
                                   AND j.journal = $journal->id
                                   AND t.course = $journal->course
                              ORDER BY j.modified DESC");

    if ($studentjournals and !$teacherjournals) {
        return $studentjournals;
    }
    if ($teacherjournals and !$studentjournals) {
        return $teacherjournals;
    }
    if (!$teacherjournals and !$studentjournals) {
        return array();
    }
    return(array_merge($studentjournals, $teacherjournals));
}

function journal_count_entries($journal, $groupid=0) {
/// Counts all the journal entries (optionally in a given group)

    global $CFG, $db;

    if ($groupid) {     /// How many in a particular group?
        return count_records_sql("SELECT COUNT(*) 
                                     FROM {$CFG->prefix}journal_entries j,
                                          {$CFG->prefix}groups_members g
                                    WHERE j.journal = $journal->id 
                                      AND g.groupid = '$groupid' 
                                      AND j.userid = g.userid");

    } else { /// Count all the entries from the whole course
    
        // make sure it works on the site course
        $select = "s.course = '$journal->course' AND";
        if ($journal->course == SITEID) {
            $select = '';
        }

        $studentjournals = count_records_sql("SELECT COUNT(*)
                                                 FROM {$CFG->prefix}journal_entries j,
                                                      {$CFG->prefix}user u, 
                                                      {$CFG->prefix}user_students s
                                                WHERE j.userid = u.id
                                                  AND s.userid = u.id 
                                                  AND $select j.journal = $journal->id");

        $teacherjournals = count_records_sql("SELECT COUNT(*)
                                                 FROM {$CFG->prefix}journal_entries j,
                                                      {$CFG->prefix}user u, 
                                                      {$CFG->prefix}user_teachers t
                                                WHERE j.userid = u.id
                                                  AND t.userid = u.id 
                                                  AND j.journal = $journal->id
                                                  AND t.course = $journal->course ");
        return ($studentjournals + $teacherjournals);
    }
}


function journal_get_unmailed_graded($cutofftime) {
    global $CFG;
    return get_records_sql("SELECT e.*, j.course, j.name
                              FROM {$CFG->prefix}journal_entries e, 
                                   {$CFG->prefix}journal j
                             WHERE e.mailed = '0' 
                               AND e.timemarked < '$cutofftime' 
                               AND e.timemarked > 0
                               AND e.journal = j.id");
}

function journal_log_info($log) {
    global $CFG;
    return get_record_sql("SELECT j.*, u.firstname, u.lastname
                             FROM {$CFG->prefix}journal j, 
                                  {$CFG->prefix}journal_entries e, 
                                  {$CFG->prefix}user u
                            WHERE e.id = '$log->info' 
                              AND e.journal = j.id
                              AND e.userid = u.id");
}

// OTHER JOURNAL FUNCTIONS ///////////////////////////////////////////////////////////////////



function journal_print_user_entry($course, $user, $entry, $teachers, $grades) {
    global $USER;

    echo "\n<table border=\"1\" cellspacing=\"0\" valign=\"top\" cellpadding=\"10\">";
        
    echo "\n<tr>";
    echo "\n<td rowspan=\"2\" width=\"35\" valign=\"top\">";
    print_user_picture($user->id, $course->id, $user->picture);
    echo "</td>";
    echo "<td nowrap=\"nowrap\" width=\"100%\">".fullname($user);
    if ($entry) {
        echo "&nbsp;&nbsp;<font size=\"1\">".get_string("lastedited").": ".userdate($entry->modified)."</font>";
    }
    echo "</tr>";

    echo "\n<tr><td width=\"100%\">";
    if ($entry) {
        echo format_text($entry->text, $entry->format);
    } else {
        print_string("noentry", "journal");
    }
    echo "</td></tr>";

    if ($entry) {
        echo "\n<tr>";
        echo "<td width=\"35\" valign=\"top\">";
        if (!$entry->teacher) {
            $entry->teacher = $USER->id;
        }
        print_user_picture($entry->teacher, $course->id, $teachers[$entry->teacher]->picture);
        echo "<td>".get_string("feedback").":";
        choose_from_menu($grades, "r$entry->id", $entry->rating, get_string("nograde")."...");
        if ($entry->timemarked) {
            echo "&nbsp;&nbsp;<font size=\"1\">".userdate($entry->timemarked)."</font>";
        }
        echo "<br /><textarea name=\"c$entry->id\" rows=\"12\" cols=\"60\" wrap=\"virtual\">";
        p($entry->comment);
        echo "</textarea><br />";
        echo "</td></tr>";
    }
    echo "</table><br clear=\"all\" />\n";
}


function journal_add_instance($journal) {
// Given an object containing all the necessary data, 
// (defined by the form in mod.html) this function 
// will create a new instance and return the id number 
// of the new instance.

    $journal->timemodified = time();

    return insert_record("journal", $journal);
}


function journal_update_instance($journal) {
// Given an object containing all the necessary data, 
// (defined by the form in mod.html) this function 
// will update an existing instance with new data.

    $journal->timemodified = time();
    $journal->id = $journal->instance;

    return update_record("journal", $journal);
}


function journal_delete_instance($id) {
// Given an ID of an instance of this module, 
// this function will permanently delete the instance 
// and any data that depends on it.  

    if (! $journal = get_record("journal", "id", $id)) {
        return false;
    }

    $result = true;

    if (! delete_records("journal_entries", "journal", $journal->id)) {
        $result = false;
    }

    if (! delete_records("journal", "id", $journal->id)) {
        $result = false;
    }

    return $result;

}


function journal_print_feedback($course, $entry, $grades) {
    global $CFG;

    if (! $teacher = get_record('user', 'id', $entry->teacher)) {
        error('Weird journal error');
    }

    echo '<table cellspacing="0" align="center" class="feedbackbox">';

    echo '<tr>';
    echo '<td class="left picture">';
    print_user_picture($teacher->id, $course->id, $teacher->picture);
    echo '</td>';
    echo '<td class="entryheader">';
    echo '<span class="author">'.fullname($teacher).'</span>';
    echo '&nbsp;&nbsp;<span class="time">'.userdate($entry->timemarked).'</span>';
    echo '</tr>';

    echo '<tr>';
    echo '<td class="left side">&nbsp;</td>';
    echo '<td class="entrycontent">';

    echo '<div class="grade">';
    
    if (!empty($entry->rating) and !empty($grades[$entry->rating])) {
        echo get_string('grade').': ';
        echo $grades[$entry->rating];
    } else {
        print_string('nograde');
    }
    echo '</div>';

    echo format_text($entry->comment);
    echo '</td></tr></table>';
}

function journal_get_view_actions() {
    return array('view','view all','view responses');
}

function journal_get_post_actions() {
    return array('add entry','update entry','update feedback');
}

?>