-
Notifications
You must be signed in to change notification settings - Fork 29
/
lib.php
1486 lines (1287 loc) · 50 KB
/
lib.php
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Library of functions for the oublog module.
*
* This contains functions that are called also from outside the oublog module
* Functions that are only called by the quiz module itself are in {@link locallib.php}
*
* @author Matt Clarkson <[email protected]>
* @author Sam Marshall <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package oublog
*/
/**
* Given an object containing all the necessary data,
* (defined by the form in mod_form.php) this function
* will create a new instance and return the id number
* of the new instance.
*
* @param object $oublog the data from the mod form
* @return int The id od the newly inserted module
*/
function oublog_add_instance($oublog) {
global $DB;
$cmid = $oublog->coursemodule;
// Generate an accesstoken.
$oublog->accesstoken = md5(uniqid(rand(), true));
$oublog->timemodified = time();
if (empty($oublog->ratingtime) || empty($oublog->assessed)) {
$oublog->assesstimestart = 0;
$oublog->assesstimefinish = 0;
}
if (!$oublog->id = $DB->insert_record('oublog', $oublog)) {
return(false);
}
if (!empty($oublog->tagslist)) {
$blogtags = oublog_clarify_tags($oublog->tagslist);
// For each tag added to the blog check if it exists in oublog_tags table,
// if it does not a tag record is created.
foreach ($blogtags as $tag) {
if (!$DB->get_record('oublog_tags', array('tag' => $tag))) {
$DB->insert_record('oublog_tags', (object) array('tag' => $tag));
}
}
}
oublog_grade_item_update($oublog);
// Update completion event in calendar.
$completionexpected = (!empty($oublog->completionexpected)) ? $oublog->completionexpected : null;
\core_completion\api::update_completion_date_event($cmid, 'oublog', $oublog->id, $completionexpected);
return($oublog->id);
}
/**
* Given an object containing all the necessary data,(defined by the
* form in mod_form.php) this function will update an existing instance
* with new data.
*
* @param object $oublog the data from the mod form
* @return boolean true on success, false on failure.
*/
function oublog_update_instance($oublog) {
global $DB;
$cmid = $oublog->coursemodule;
$oublog->id = $oublog->instance;
$oublog->timemodified = time();
if (!$DB->get_record('oublog', array('id' => $oublog->id))) {
return(false);
}
if (empty($oublog->ratingtime) || empty($oublog->assessed)) {
$oublog->assesstimestart = 0;
$oublog->assesstimefinish = 0;
}
if (!$DB->update_record('oublog', $oublog)) {
return(false);
}
$blogtags = oublog_clarify_tags($oublog->tagslist);
// For each tag in the blog check if it already exists in oublog_tags table,
// if it does not a tag record is created.
foreach ($blogtags as $tag) {
if (!$DB->get_record('oublog_tags', array('tag' => $tag))) {
$DB->insert_record('oublog_tags', (object) array('tag' => $tag));
}
}
oublog_grade_item_update($oublog);
// Update completion event in calendar.
$completionexpected = (!empty($oublog->completionexpected)) ? $oublog->completionexpected : null;
\core_completion\api::update_completion_date_event($cmid, 'oublog', $oublog->id, $completionexpected);
return(true);
}
/**
* Given an ID of an instance of this module, this function will
* permanently delete the instance and any data that depends on it.
*
* @param int $id The ID of the module instance
* @return boolean true on success, false on failure.
*/
function oublog_delete_instance($oublogid) {
global $DB, $CFG;
if (!$oublog = $DB->get_record('oublog', array('id'=>$oublogid))) {
return(false);
}
if ($oublog->global) {
throw new moodle_exception('deleteglobalblog', 'oublog');
}
if ($instances = $DB->get_records('oublog_instances', array('oublogid'=>$oublog->id))) {
foreach ($instances as $oubloginstancesid => $bloginstance) {
// tags
$DB->delete_records('oublog_taginstances', array('oubloginstancesid'=>$oubloginstancesid));
if ($posts = $DB->get_records('oublog_posts', array('oubloginstancesid'=>$oubloginstancesid))) {
foreach ($posts as $postid => $post) {
// comments
$DB->delete_records('oublog_comments', array('postid'=>$postid));
// edits
$DB->delete_records('oublog_edits', array('postid'=>$postid));
}
// posts
$DB->delete_records('oublog_posts', array('oubloginstancesid'=>$oubloginstancesid));
}
}
}
// links
$DB->delete_records('oublog_links', array('oublogid'=>$oublog->id));
// instances
$DB->delete_records('oublog_instances', array('oublogid'=>$oublog->id));
if (!$cm = get_coursemodule_from_instance('oublog', $oublog->id)) {
throw new moodle_exception('invalidcoursemodule');
}
require_once(dirname(__FILE__).'/locallib.php');
oublog_grade_item_delete($oublog);
// Delete event in calendar when deleting activity.
\core_completion\api::update_completion_date_event($cm->id, 'oublog', $oublogid, null);
// oublog
return($DB->delete_records('oublog', array('id'=>$oublog->id)));
}
/**
* Return a small object with summary information about what a
* user has done with a given particular instance of this module
* Used for user activity reports.
*
* @param object $course
* @param object $user
* @param object $mod
* @param object $oublog
* @return object containing a time and info properties
*/
function oublog_user_outline($course, $user, $mod, $oublog) {
global $CFG, $DB;
$sql = "SELECT count(*) AS postcnt, MAX(timeposted) as lastpost
FROM {oublog_posts} p
INNER JOIN {oublog_instances} i ON p.oubloginstancesid = i.id
WHERE p.deletedby IS NULL AND i.userid = ? AND oublogid = ?";
if ($postinfo = $DB->get_record_sql($sql, array($user->id, $mod->instance))) {
$result = new stdClass();
$result->info = get_string('numposts', 'oublog', $postinfo->postcnt);
$result->time = $postinfo->lastpost;
return($result);
}
return(null);
}
/**
* Print a detailed representation of what a user has done with
* a given particular instance of this module, for user activity reports.
*
* @param object $course
* @param object $user
* @param object $mod
* @param object $oublog
* @return object containing a time and info properties
*/
function oublog_user_complete($course, $user, $mod, $oublog) {
global $CFG, $DB, $PAGE;
include_once($CFG->dirroot.'/mod/oublog/locallib.php');
$oublogoutput = $PAGE->get_renderer('mod_oublog');
$baseurl = $CFG->wwwroot.'/mod/oublog/view.php?id='.$mod->id;
$sql = "SELECT p.*
FROM {oublog_posts} p
INNER JOIN {oublog_instances} i ON p.oubloginstancesid = i.id
WHERE p.deletedby IS NULL AND i.userid = ? AND oublogid = ? ";
if ($posts = $DB->get_records_sql($sql, array($user->id, $mod->instance))) {
foreach ($posts as $post) {
$postdata = oublog_get_post($post->id);
echo $oublogoutput->render_post($mod, $oublog, $postdata, $baseurl, 'course');
}
} else {
echo get_string('noblogposts', 'oublog');
}
return(null);
}
/**
* Given a course and a time, this module should find recent activity
* that has occurred in newmodule activities and print it out.
* Return true if there was output, or false is there was none.
*
* @param object $course
* @param bool $isteacher
* @param int $timestart
* @return boolean true on success, false on failure.
**/
function oublog_print_recent_activity($course, $isteacher, $timestart) {
global $CFG, $DB, $OUTPUT;
include_once('locallib.php');
$sql = "SELECT i.oublogid, p.id AS postid, p.*, u.firstname, u.lastname, u.email, u.idnumber, i.userid
FROM {oublog_posts} p
INNER JOIN {oublog_instances} i ON p.oubloginstancesid = i.id
INNER JOIN {oublog} b ON i.oublogid = b.id
INNER JOIN {user} u ON i.userid = u.id
WHERE b.course = ? AND p.deletedby IS NULL AND p.timeposted >= ? ";
if (!$rs = $DB->get_recordset_sql($sql, array($course->id, $timestart))) {
return(true);
}
$modinfo = get_fast_modinfo($course);
$strftimerecent = get_string('strftimerecent');
echo $OUTPUT->heading(get_string('newblogposts', 'oublog'), 3);
echo "\n<ul class='unlist'>\n";
foreach ($rs as $blog) {
if (!isset($modinfo->instances['oublog'][$blog->oublogid])) {
// not visible
continue;
}
$cm = $modinfo->instances['oublog'][$blog->oublogid];
if (!$cm->uservisible) {
continue;
}
if (!has_capability('mod/oublog:view', context_module::instance($cm->id))) {
continue;
}
if (!has_capability('mod/oublog:view', context_user::instance($blog->userid))) {
continue;
}
$groupmode = oublog_get_activity_groupmode($cm, $course);
if ($groupmode) {
if ($blog->groupid && $groupmode != VISIBLEGROUPS) {
// separate mode
if (isguestuser()) {
// shortcut
continue;
}
if (is_null($modinfo->groups)) {
$modinfo->groups = groups_get_user_groups($course->id); // load all my groups and cache it in modinfo
}
if (!array_key_exists($blog->groupid, $modinfo->groups[0])) {
continue;
}
}
}
echo '<li><div class="head">'.
'<div class="date">'.oublog_date($blog->timeposted, $strftimerecent).'</div>'.
'<div class="name">'.fullname($blog).'</div>'.
'</div>';
echo '<div class="info">';
echo "<a href=\"{$CFG->wwwroot}/mod/oublog/viewpost.php?post={$blog->postid}\">";
echo break_up_long_words(format_string(empty($blog->title) ? $blog->message : $blog->title));
echo '</a>';
echo '</div>';
}
$rs->close();
echo "</ul>\n";
}
/**
* Get recent activity for a course
*
* @param array $activities
* @param int $index
* @param int $timestart
* @param int $courseid
* @param int $cmid
* @param int $userid
* @param int $groupid
* @return bool
*/
function oublog_get_recent_mod_activity(&$activities, &$index, $timestart, $courseid, $cmid, $userid=0, $groupid=0) {
global $CFG, $COURSE, $DB;
$sql = "SELECT i.oublogid, p.id AS postid, p.*, u.firstname, u.lastname, u.email, u.idnumber, u.picture, u.imagealt, i.userid
FROM {oublog_posts} p
INNER JOIN {oublog_instances} i ON p.oubloginstancesid = i.id
INNER JOIN {oublog} b ON i.oublogid = b.id
INNER JOIN {user} u ON i.userid = u.id
WHERE b.course = ? AND p.deletedby IS NULL AND p.timeposted >= ? ";
if (!$rs = $DB->get_recordset_sql($sql, array($courseid, $timestart))) {
return(true);
}
$modinfo = get_fast_modinfo($COURSE);
foreach ($rs as $blog) {
if (!isset($modinfo->instances['oublog'][$blog->oublogid])) {
// not visible
continue;
}
$cm = $modinfo->instances['oublog'][$blog->oublogid];
if (!$cm->uservisible) {
continue;
}
if (!has_capability('mod/oublog:view', context_module::instance($cm->id))) {
continue;
}
if (!has_capability('mod/oublog:view', context_user::instance($blog->userid))) {
continue;
}
$groupmode = oublog_get_activity_groupmode($cm, $COURSE);
if ($groupmode) {
if ($blog->groupid && $groupmode != VISIBLEGROUPS) {
// separate mode
if (isguestuser()) {
// shortcut
continue;
}
if (is_null($modinfo->groups)) {
$modinfo->groups = groups_get_user_groups($courseid); // load all my groups and cache it in modinfo
}
if (!array_key_exists($blog->groupid, $modinfo->groups[0])) {
continue;
}
}
}
$tmpactivity = new stdClass();
$tmpactivity->type = 'oublog';
$tmpactivity->cmid = $cm->id;
$tmpactivity->name = $blog->title;
$tmpactivity->sectionnum = $cm->sectionnum;
$tmpactivity->timeposted = $blog->timeposted;
$tmpactivity->content = new stdClass();
$tmpactivity->content->postid = $blog->postid;
$tmpactivity->content->title = format_string($blog->title);
$tmpactivity->user = new stdClass();
$tmpactivity->user->id = $blog->userid;
$tmpactivity->user->firstname = $blog->firstname;
$tmpactivity->user->lastname = $blog->lastname;
$tmpactivity->user->picture = $blog->picture;
$tmpactivity->user->imagealt = $blog->imagealt;
$tmpactivity->user->email = $blog->email;
$activities[$index++] = $tmpactivity;
}
$rs->close();
}
/**
* Print recent oublog activity for a course
*
* @param object $activity
* @param int $courseid
* @param bool $detail
* @param array $modnames
* @param bool $viewfullnames
*/
function oublog_print_recent_mod_activity($activity, $courseid, $detail, $modnames, $viewfullnames) {
global $CFG, $OUTPUT;
echo '<table border="0" cellpadding="3" cellspacing="0" class=oublog-recent">';
echo "<tr><td class=\"userpicture\" valign=\"top\">";
echo $OUTPUT->user_picture($activity->user, array('courseid'=>$courseid));
echo "</td><td>";
echo '<div class="title">';
if ($detail) {
echo "<img src=\"".$OUTPUT->image_url('monologo', $activity->type)."\" class=\"icon\" alt=\"".s($activity->title)."\" />";
}
echo "<a href=\"$CFG->wwwroot/mod/oublog/viewpost.php?post={$activity->content->postid}\">{$activity->content->title}</a>";
echo '</div>';
echo '<div class="user">';
$fullname = fullname($activity->user, $viewfullnames);
echo "<a href=\"$CFG->wwwroot/user/view.php?id={$activity->user->id}&course=$courseid\">"
."{$fullname}</a> - ".oublog_date($activity->timeposted);
echo '</div>';
echo "</td></tr></table>";
return;
}
/**
* Indicates API features that the module supports.
*
* @param string $feature
* @return mixed True if yes (some features may use other values)
*/
function oublog_supports($feature) {
switch($feature) {
case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
case FEATURE_COMPLETION_HAS_RULES: return true;
case FEATURE_BACKUP_MOODLE2: return true;
case FEATURE_MOD_INTRO: return true;
case FEATURE_GROUPINGS: return true;
case FEATURE_GROUPS: return true;
case FEATURE_GRADE_HAS_GRADE: return true;
case FEATURE_RATE: return true;
default: return null;
}
}
/**
* Obtains the automatic completion state for this module based on any conditions
* in module settings.
*
* @param object $course Course
* @param object $cm Course-module
* @param int $userid User ID
* @param bool $type Type of comparison (or/and; can be used as return value if no conditions)
* @return bool True if completed, false if not, $type if conditions not set.
*/
function oublog_get_completion_state_lib($cm, $userid, $type) {
global $DB;
// Get oublog details
if (!($oublog=$DB->get_record('oublog', array('id' => $cm->instance)))) {
throw new Exception("Can't find oublog {$cm->instance}");
}
$result=$type; // Default return value
if ($oublog->completionposts) {
// Count of posts by user
$value = $oublog->completionposts <= $DB->get_field_sql("
SELECT
COUNT(1)
FROM
{oublog_instances} i
INNER JOIN {oublog_posts} p ON i.id=p.oubloginstancesid
WHERE
i.userid= ? AND i.oublogid=? AND p.deletedby IS NULL", array($userid, $oublog->id));
if ($type==COMPLETION_AND) {
$result=$result && $value;
} else {
$result=$result || $value;
}
}
if ($oublog->completioncomments) {
// Count of comments by user (on posts by any user)
$value = $oublog->completioncomments <= $DB->get_field_sql("
SELECT
COUNT(1)
FROM
{oublog_comments} c
INNER JOIN {oublog_posts} p ON p.id=c.postid
INNER JOIN {oublog_instances} i ON i.id=p.oubloginstancesid
WHERE
c.userid= ? AND i.oublogid= ? AND p.deletedby IS NULL AND c.deletedby IS NULL", array($userid, $oublog->id));
if ($type==COMPLETION_AND) {
$result=$result && $value;
} else {
$result=$result || $value;
}
}
return $result;
}
/**
* This function returns a summary of all the postings since the current user
* last logged in.
*/
function oublog_print_overview($courses, &$htmlarray) {
global $USER, $CFG, $DB;
if (empty($courses) || !is_array($courses) || count($courses) == 0) {
return array();
}
if (!$blogs = get_all_instances_in_courses('oublog', $courses)) {
return;
}
// get all logs in ONE query
$sql = "SELECT instance,cmid,l.course,COUNT(l.id) as count FROM {log} l "
." JOIN {course_modules} cm ON cm.id = cmid "
." WHERE (";
$params = array();
foreach ($courses as $course) {
$sql .= '(l.course = ? AND l.time > ? ) OR ';
$params[] = $course->id;
$params[] = $course->lastaccess;
}
$sql = substr($sql, 0, -3); // take off the last OR
// Ignore comment actions for now, only entries.
$sql .= ") AND l.module = 'oublog' AND action in('add post','edit post')
AND userid != ? GROUP BY cmid,l.course,instance";
$params[] = $USER->id;
if (!$new = $DB->get_records_sql($sql, $params)) {
$new = array(); // avoid warnings
}
$strblogs = get_string('modulenameplural', 'oublog');
$site = get_site();
if (count( $courses ) == 1 && isset( $courses[$site->id])) {
$strnumrespsince1 = get_string('overviewnumentrylog1', 'oublog');
$strnumrespsince = get_string('overviewnumentrylog', 'oublog');
} else {
$strnumrespsince1 = get_string('overviewnumentryvw1', 'oublog');
$strnumrespsince = get_string('overviewnumentryvw', 'oublog');
}
// Go through the list of all oublog instances build previously, and check whether
// they have had any activity.
foreach ($blogs as $blog) {
if (array_key_exists($blog->id, $new) && !empty($new[$blog->id])) {
$count = $new[$blog->id]->count;
if ($count > 0) {
if ($count == 1) {
$strresp = $strnumrespsince1;
} else {
$strresp = $strnumrespsince;
}
$str = '<div class="overview oublog"><div class="name">'.
$strblogs.': <a title="'.$strblogs.'" href="';
if ($blog->global=='1') {
$str .= $CFG->wwwroot.'/mod/oublog/allposts.php">'.$blog->name.'</a></div>';
} else {
$str .= $CFG->wwwroot.'/mod/oublog/view.php?id='.$new[$blog->id]->cmid.'">'.$blog->name.'</a></div>';
}
$str .= '<div class="info">';
$str .= $count.' '.$strresp;
$str .= '</div></div>';
if (!array_key_exists($blog->course, $htmlarray)) {
$htmlarray[$blog->course] = array();
}
if (!array_key_exists('oublog', $htmlarray[$blog->course])) {
$htmlarray[$blog->course]['oublog'] = ''; // initialize, avoid warnings
}
$htmlarray[$blog->course]['oublog'] .= $str;
}
}
}
}
/**
* Serves the oublog attachments. Implements needed access control ;-)
*
* @param object $course
* @param object $cm
* @param object $context
* @param string $filearea
* @param array $args
* @param bool $forcedownload
* @return bool false if file not found, does not return if found - justsend the file
*/
function oublog_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload) {
global $CFG, $DB, $USER;
if ($context->contextlevel != CONTEXT_MODULE) {
return false;
}
$fileareas = array('attachment', 'message', 'edit', 'messagecomment', 'summary');
if (!in_array($filearea, $fileareas)) {
return false;
}
require_once(dirname(__FILE__).'/locallib.php');
if ($filearea=='edit') {
$editid = (int)array_shift($args);
if (!$edit = $DB->get_record('oublog_edits', array('id'=>$editid))) {
return false;
}
$postid = $edit->postid;
$fileid = $editid;
} else {
$postid = (int)array_shift($args);
$fileid = $postid;
}
if ($filearea != 'summary') {
if ($filearea == 'messagecomment') {
if (!$comment = $DB->get_record('oublog_comments', array('id' => $postid), 'postid')) {
return false;
}
$postid = $comment->postid;
}
if (!$post = $DB->get_record('oublog_posts', array('id'=>$postid))) {
return false;
}
if (!($oublog = oublog_get_blog_from_postid($post->id))) {
return false;
}
}
$fs = get_file_storage();
$relativepath = implode('/', $args);
$fullpath = "/$context->id/mod_oublog/$filearea/$fileid/$relativepath";
if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
return false;
}
// Make sure we're allowed to see it...
// Check if coming from webservice - if so always allow.
$ajax = constant('AJAX_SCRIPT') ? true : false;
$cmid = optional_param('cmid', null, PARAM_INT);
$context = $cmid ? context_module::instance($cmid) : $context;
if ($filearea != 'summary' && !$ajax && !oublog_can_view_post($post, $USER, $context, $cm, $oublog)) {
return false;
}
if ($filearea == 'attachment') {
$forcedownload = true;
} else {
$forcedownload = false;
}
// Finally send the file.
send_stored_file($file, 0, 0, $forcedownload);
}
/**
* File browsing support for oublog module.
* @param object $browser
* @param object $areas
* @param object $course
* @param object $cm
* @param object $context
* @param string $filearea
* @param int $itemid
* @param string $filepath
* @param string $filename
* @return file_info instance Representing an actual file or folder (null if not found
* or cannot access)
*/
function oublog_get_file_info($browser, $areas, $course, $cm, $context, $filearea,
$itemid, $filepath, $filename) {
global $CFG, $USER, $DB;
require_once($CFG->dirroot . '/mod/oublog/locallib.php');
if ($context->contextlevel != CONTEXT_MODULE) {
return null;
}
$fileareas = array('attachment', 'message', 'edit', 'messagecomment');
if (!in_array($filearea, $fileareas)) {
return null;
}
$postid = $itemid;
if ($filearea == 'messagecomment') {
if (!$comment = $DB->get_record('oublog_comments', array('id' => $postid), 'postid')) {
return null;
}
$postid = $comment->postid;
}
if (!($oublog = oublog_get_blog_from_postid($postid))) {
return null;
}
// Check if the user is allowed to view the blog.
if (!has_capability('mod/oublog:view', $context)) {
return null;
}
if (!$post = oublog_get_post($postid)) {
return null;
}
// Check if the user is allowed to view the post
try {
if (!oublog_can_view_post($post, $USER, $context, $cm, $oublog)) {
return null;
}
} catch (mod_oublog_exception $e) {
return null;
}
$fs = get_file_storage();
$filepath = is_null($filepath) ? '/' : $filepath;
$filename = is_null($filename) ? '.' : $filename;
if (!($storedfile = $fs->get_file($context->id, 'mod_oublog', $filearea, $itemid,
$filepath, $filename))) {
return null;
}
$urlbase = $CFG->wwwroot . '/pluginfile.php';
return new file_info_stored($browser, $context, $storedfile, $urlbase, $filearea,
$itemid, true, true, false);
}
/**
* Sets the module uservisible to false if the user has not got the view capability
* @param cm_info $cm
*/
function oublog_cm_info_dynamic(cm_info $cm) {
global $remoteuserid, $USER;
$userid = $USER;
if (isset($remoteuserid) && !empty($remoteuserid)) {
// Hack using dodgy global. The actual user id for specific user e.g. from webservice.
$userid = $remoteuserid;
}
$capability = 'mod/oublog:view';
if ($cm->course == SITEID && $cm->instance == 1) {
// Is global blog (To save DB call we make suspect assumption it is instance 1)?
$capability = 'mod/oublog:viewpersonal';
}
if (!has_capability($capability,
context_module::instance($cm->id), $userid)) {
$cm->set_user_visible(false);
$cm->set_available(false);
}
}
/**
* Show last updated date + time (post created).
*
* @param cm_info $cm
*/
function oublog_cm_info_view(cm_info $cm) {
global $CFG;
if (!$cm->uservisible) {
return;
}
require_once($CFG->dirroot . '/mod/oublog/locallib.php');
$lastpostdate = oublog_get_last_modified($cm, $cm->get_course());
if (!empty($lastpostdate)) {
$cm->set_after_link(html_writer::span(get_string('lastmodified', 'oublog',
userdate($lastpostdate, get_string('strftimerecent', 'oublog'))), 'lastmodtext oubloglmt'));
}
}
/**
* Return blogs on course that have last modified date for current user
*
* @param stdClass $course
* @return array
*/
function oublog_get_ourecent_activity($course) {
global $CFG;
require_once($CFG->dirroot . '/mod/oublog/locallib.php');
$modinfo = get_fast_modinfo($course);
$return = array();
foreach ($modinfo->get_instances_of('oublog') as $blog) {
if ($blog->uservisible) {
$lastpostdate = oublog_get_last_modified($blog, $blog->get_course());
if (!empty($lastpostdate)) {
$data = new stdClass();
$data->cm = $blog;
$data->text = get_string('lastmodified', 'oublog',
userdate($lastpostdate, get_string('strftimerecent', 'oublog')));
$data->date = $lastpostdate;
$return[$data->cm->id] = $data;
}
}
}
return $return;
}
/**
* Create grade item for given oublog
*
* @param object $oublog
* @param mixed $grades optional array/object of grade(s); 'reset' means reset grades in gradebook
* @return int 0 if ok, error code otherwise
*/
function oublog_grade_item_update($oublog, $grades = null) {
global $CFG;
require_once($CFG->libdir . '/gradelib.php');
require_once($CFG->dirroot . '/mod/oublog/locallib.php');
// Use 'grade' or 'scale' depends upon 'grading'.
if ($oublog->grading == OUBLOG_USE_RATING) {
$oublogscale = $oublog->scale;
} else if ($oublog->grading == OUBLOG_NO_GRADING) {
$oublogscale = 0;
} else {
$oublogscale = $oublog->grade;
}
$params = array('itemname' => $oublog->name);
if ($oublogscale > 0) {
$params['gradetype'] = GRADE_TYPE_VALUE;
$params['grademax'] = $oublogscale;
$params['grademin'] = 0;
} else if ($oublogscale < 0) {
$params['gradetype'] = GRADE_TYPE_SCALE;
$params['scaleid'] = -$oublogscale;
} else {
$params['gradetype'] = GRADE_TYPE_NONE;
}
if ($grades === 'reset') {
$params['reset'] = true;
$grades = null;
}
return grade_update('mod/oublog', $oublog->course, 'mod',
'oublog', $oublog->id, 0, $grades, $params);
}
/**
* Delete grade item for given oublog
*
* @param object $oublog object
* @return object oublog
*/
function oublog_grade_item_delete($oublog) {
global $CFG;
require_once($CFG->libdir.'/gradelib.php');
return grade_update('mod/oublog', $oublog->course, 'mod',
'oublog', $oublog->id, 0, null, array('deleted' => 1));
}
/**
* Returns all other caps used in oublog at module level.
*/
function oublog_get_extra_capabilities() {
return array('moodle/site:accessallgroups', 'moodle/site:viewfullnames',
'report/oualerts:managealerts', 'report/restrictuser:view',
'report/restrictuser:restrict', 'report/restrictuser:removerestrict');
}
/**
* Implementation of the function for printing the form elements that control
* whether the course reset functionality affects the oublog.
*
* @param object $mform form passed by reference
*/
function oublog_reset_course_form_definition(&$mform) {
$mform->addElement('header', 'oublogheader', get_string('modulenameplural', 'oublog'));
$mform->addElement('advcheckbox', 'reset_oublog', get_string('removeblogs', 'oublog'));
}
/**
* Actual implementation of the reset course functionality, delete all
* oublog posts.
*
* @global object
* @global object
* @param object $data the data submitted from the reset course.
* @return array status array
*/
function oublog_reset_userdata($data) {
global $DB;
$componentstr = get_string('modulenameplural', 'oublog');
$status = array();
if (!empty($data->reset_oublog)) {
// Delete post-related data.
$postidsql = "SELECT pst.id
FROM {oublog_posts} pst
JOIN {oublog_instances} ins ON (ins.id = pst.oubloginstancesid)
JOIN {oublog} obl ON (obl.id = ins.oublogid)
WHERE obl.course = ?";
$params = array($data->courseid);
$DB->delete_records_select('oublog_comments', "postid IN ($postidsql)", $params);
$DB->delete_records_select('oublog_comments_moderated', "postid IN ($postidsql)", $params);
$DB->delete_records_select('oublog_edits', "postid IN ($postidsql)", $params);
// Delete instance-related data.
$insidsql = "SELECT ins.id
FROM {oublog_instances} ins
JOIN {oublog} obl ON (obl.id = ins.oublogid)
WHERE obl.course = ?";
$DB->delete_records_select('oublog_links', "oubloginstancesid IN ($insidsql)", $params);
$DB->delete_records_select('oublog_taginstances', "oubloginstancesid IN ($insidsql)", $params);
$DB->delete_records_select('oublog_posts', "oubloginstancesid IN ($insidsql)", $params);
$blogidsql = "SELECT obl.id
FROM {oublog} obl
WHERE obl.course = ?";
// Delete instances:
$DB->delete_records_select('oublog_instances', "oublogid IN ($blogidsql)", $params);
// Reset views:
$DB->execute("UPDATE {oublog} SET views = 0 WHERE course = ?", $params);
$rm = new rating_manager();
$ratingdeloptions = new stdClass;
$ratingdeloptions->component = 'mod_oublog';
$ratingdeloptions->ratingarea = 'post';
// Now get rid of all attachments and ratings.
$fs = get_file_storage();
$oublogs = get_coursemodules_in_course('oublog', $data->courseid);
if ($oublogs) {
foreach ($oublogs as $oublogid => $unused) {
if (!$cm = get_coursemodule_from_instance('oublog', $oublogid)) {
continue;
}
$context = context_module::instance($cm->id);
$fs->delete_area_files($context->id, 'mod_oublog', 'attachment');
$fs->delete_area_files($context->id, 'mod_oublog', 'message');
$fs->delete_area_files($context->id, 'mod_oublog', 'messagecomment');
$ratingdeloptions->contextid = $context->id;
$rm->delete_ratings($ratingdeloptions);
}
}
$status[] = array(
'component' => $componentstr,
'item' => get_string('removeblogs', 'oublog'),
'error' => false
);
}
return $status;
}
/**
* List of view style log actions
* @return array
*/
function oublog_get_view_actions() {
return array('view', 'view all');
}