-
Notifications
You must be signed in to change notification settings - Fork 4
/
lib.php
880 lines (736 loc) · 31.5 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
<?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/>.
/**
* Set of functions to delete user records, making them inactive and updating
* there record to reflect spammer.
*
* @package block_spam_deletion
* @category spam_deletion
* @copyright 2012 Rajesh Taneja
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/user/lib.php');
require_once($CFG->dirroot . '/mod/forum/lib.php');
require_once($CFG->dirroot.'/tag/lib.php');
require_once($CFG->libdir .'/tablelib.php');
require_once($CFG->dirroot . '/comment/lib.php');
class spammerlib {
/**
* @var stdClass user whose account will be marked as spammer
*/
private $user = null;
/**
* Constructor
* @param int $userid user id for spammer
*/
public function __construct($userid) {
global $DB;
if (!self::is_suspendable_user($userid)) {
throw new moodle_exception('User passed is not suspendedable');
}
$this->user = $DB->get_record('user', array('id' => $userid), '*', MUST_EXIST);
}
/**
* Is the passed userid able to be suspended as a user?
*
* @param int $userid the userid of the user being checked
* @return bool true if not a guest/admin/currnet user.
*/
public static function is_suspendable_user($userid) {
global $USER;
if (empty($userid)) {
// Userid of 0.
return false;
}
if ($userid == $USER->id) {
// Is current user.
return false;
}
if (isguestuser($userid)) {
return false;
}
if (is_siteadmin($userid)) {
return false;
}
return true;
}
/**
* returns true is user is active
*/
public function is_active() {
if (($this->user->deleted == 1) || ($this->user->suspended == 1)) {
return false;
}
return true;
}
/**
* returns true if user has first accessed account in last one month
*/
public function is_recentuser() {
$timegap = time() - (30 * 24 * 60 * 60);
if ($this->user->firstaccess > $timegap) {
return true;
} else {
return false;
}
}
/**
* Mark user as spammer, by deactivating account and setting description in profile
*/
private function set_profile_as_spammer() {
global $DB;
// Remove profile picture files from users file area.
$fs = get_file_storage();
$context = context_user::instance($this->user->id, MUST_EXIST);
$fs->delete_area_files($context->id, 'user', 'icon'); // drop all images in area
$updateuser = new stdClass();
$updateuser->id = $this->user->id;
$updateuser->suspended = 1;
$updateuser->picture = 0;
$updateuser->imagealt = '';
$updateuser->url = '';
$updateuser->icq = '';
$updateuser->skype = '';
$updateuser->yahoo = '';
$updateuser->aim = '';
$updateuser->msn = '';
$updateuser->phone1 = '';
$updateuser->phone2 = '';
$updateuser->department = '';
$updateuser->institution = '';
$updateuser->city = '-';
$updateuser->description = get_string('spamdescription', 'block_spam_deletion', date('l jS F g:i A'));
$DB->update_record('user', $updateuser);
// Remove custom user profile fields.
$DB->delete_records('user_info_data', array('userid' => $this->user->id));
// Force logout.
\core\session\manager::kill_user_sessions($this->user->id);
}
/**
* Delete all user messages
*/
private function delete_user_messages() {
global $DB;
$userid = $this->user->id;
// Delete message workers..
$sql = 'DELETE FROM {message_working}
WHERE unreadmessageid IN
(SELECT id FROM {message} WHERE useridfrom = ?)';
$DB->execute($sql, array($userid));
$DB->delete_records('message', array('useridfrom' => $userid));
$DB->delete_records('message_read', array('useridfrom' => $userid));
}
/**
* Replace user forum subject and message with spam string
*/
private function delete_user_forum() {
global $DB;
// Get discussions started by the spammer.
$rs = $DB->get_recordset('forum_posts', array('userid' => $this->user->id, 'parent' => 0));
foreach ($rs as $post) {
// This is really expensive, but it should be rare iterations and i'm lazy right now.
$discussion = $DB->get_record('forum_discussions', array('id' => $post->discussion), '*', MUST_EXIST);
$forum = $DB->get_record('forum', array('id' => $discussion->forum), '*', MUST_EXIST);
if ($forum->type == 'single') {
// It's too complicated, skip.
continue;
}
$course = $DB->get_record('course', array('id' => $forum->course), '*', MUST_EXIST);
$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id, false, MUST_EXIST);
forum_delete_discussion($discussion, false, $course, $cm, $forum);
}
$rs->close();
// Delete any remaining posts not discussions..
$rs = $DB->get_recordset('forum_posts', array('userid' => $this->user->id));
foreach ($rs as $post) {
// This is really expensive, but it should be rare iterations and i'm lazy right now.
$discussion = $DB->get_record('forum_discussions', array('id' => $post->discussion), '*', MUST_EXIST);
$forum = $DB->get_record('forum', array('id' => $discussion->forum), '*', MUST_EXIST);
if ($forum->type == 'single') {
// It's too complicated, skip.
continue;
}
$course = $DB->get_record('course', array('id' => $forum->course), '*', MUST_EXIST);
$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id, false, MUST_EXIST);
// Recursively delete post and any children.
forum_delete_post($post, true, $course, $cm, $forum);
}
$rs->close();
}
/**
* Delete all user comments
*/
private function delete_user_comments() {
global $DB;
$userid = $this->user->id;
$DB->delete_records('comments', array('userid' => $userid));
}
/**
* Delete all tags
*/
private function delete_user_tags() {
tag_set('user', $this->user->id, array());
}
/**
* Delete any spam reports from this block..
*/
private function delete_spam_votes() {
global $DB;
$DB->delete_records('block_spam_deletion_votes', array('spammerid' => $this->user->id));
}
/**
* Delete user records and mark user as spammer, by doing following:
* 1. Delete comment, message form this user
* 2. Update forum post and blog post with spam message
* 3. Suspend account and set profile description as spammer
*/
public function set_spammer() {
global $DB;
//Make sure deletion should only happen for recently created account
if ($this->is_active()) {
$transaction = $DB->start_delegated_transaction();
try {
$this->delete_user_comments();
$this->delete_user_forum();
$this->delete_user_messages();
$this->delete_user_tags();
$this->set_profile_as_spammer();
$this->delete_spam_votes();
$transaction->allow_commit();
} catch (Exception $e) {
$transaction->rollback($e);
throw $e;
}
} else {
throw new moodle_exception('cannotdelete', 'block_spam_deletion');
}
}
/**
* Return html to show data stats for spammer
*
* @return string html showing data count for spammer
*/
public function show_data_count() {
global $DB;
$htmlstr = '';
$params = array('userid' => $this->user->id);
$userdata[] = get_string('countmessageunread', 'block_spam_deletion', (int)$DB->count_records('message', array('useridfrom' => $this->user->id)));
$userdata[] = get_string('countmessageread', 'block_spam_deletion', (int)$DB->count_records('message_read', array('useridfrom' => $this->user->id)));
$userdata[] = get_string('countforum', 'block_spam_deletion', (int)$DB->count_records('forum_posts', $params));
$userdata[] = get_string('countcomment', 'block_spam_deletion', (int)$DB->count_records('comments', $params));
$userdata[] = get_string('counttags', 'block_spam_deletion', (int)$DB->count_records('tag', $params));
$htmlstr = html_writer::tag('div class="block_spam_bold block_spam_highlight"', get_string('totalcount', 'block_spam_deletion'));
$htmlstr .= html_writer::alist($userdata);
return $htmlstr;
}
public function get_user() {
return $this->user;
}
}
abstract class spam_report
{
public $course;
public $cm;
public $context;
abstract public function register_vote($userid);
abstract public function has_voted($userid);
abstract public function has_permission();
abstract public function content_html();
abstract public function return_url();
protected function get_vote_weighting($userid) {
// TODO: No yet implemented, will be looked at if we ever decide to
// take automatic action based on the weights. A possible metric might
// be the number of positive spam reports by the user in the past, or
// so.
return 1;
}
public static function notify_spam($spammerid, $voterid) {
global $DB, $CFG;
$spammer = $DB->get_record('user', array('id' => $spammerid));
$voter = $DB->get_record('user', array('id' => $voterid));
$a = new stdClass;
$a->spammer = fullname($spammer);
$a->url = $CFG->wwwroot.'/blocks/spam_deletion/viewvotes.php';
$message = get_string('spamreportmessage', 'block_spam_deletion', $a);
$title = get_string('spamreportmessagetitle', 'block_spam_deletion', $a);
$eventdata = new stdClass();
$eventdata->component = 'block_spam_deletion';
$eventdata->name = 'spamreport';
$eventdata->userfrom = $voter;
$eventdata->subject = $title;
$eventdata->fullmessage = $message;
$eventdata->fullmessageformat = FORMAT_PLAIN;
$eventdata->fullmessagehtml = $message;
$eventdata->smallmessage = $message;
$eventdata->notification = 1;
$moderators = get_users_by_capability(context_system::instance(), 'block/spam_deletion:viewspamreport');
foreach ($moderators as $m) {
$eventdata->userto = $m;
message_send($eventdata);
}
}
}
class forum_post_spam extends spam_report
{
public $post;
public $discussion;
public $forum;
public function __construct($postid) {
global $DB;
// Get various records.
if (!$this->post = forum_get_post_full($postid) ) {
throw new moodle_exception('invalidpostid', 'forum', $postid);
}
$this->discussion = $DB->get_record('forum_discussions', array('id' => $this->post->discussion), '*', MUST_EXIST);
$this->forum = $DB->get_record('forum', array('id' => $this->discussion->forum), '*', MUST_EXIST);
$this->course = $DB->get_record('course', array('id' => $this->forum->course), '*', MUST_EXIST);
$this->cm = get_coursemodule_from_instance('forum', $this->forum->id, $this->course->id, false, MUST_EXIST);
$this->context = context_module::instance($this->cm->id);
}
public function return_url() {
return new moodle_url('/mod/forum/discuss.php', array('d' => $this->discussion->id));
}
public function has_permission() {
return has_capability('mod/forum:replypost', $this->context);
}
public function register_vote($userid) {
global $DB;
$record = new stdClass();
$record->spammerid = $this->post->userid;
$record->voterid = $userid;
$record->weighting = $this->get_vote_weighting($userid);
$record->postid = $this->post->id;
$DB->insert_record('block_spam_deletion_votes', $record);
$this->populate_akismet_spam_report();
// Send message to notifiers.
$this->notify_spam($record->spammerid, $record->voterid);
}
public function has_voted($userid) {
global $DB;
$params = array('voterid' => $userid, 'postid' => $this->post->id);
return (bool) $DB->count_records('block_spam_deletion_votes', $params);
}
public function content_html() {
// Wrap te post in a div with the appropriate class so forum post CSS works.
$s = html_writer::start_tag('div', array('class' => 'path-mod-forum'));
$s .= forum_print_post($this->post, $this->discussion, $this->forum, $this->cm, $this->course, false, false, false, '', '', null, true, null, true);
$s .= html_writer::end_tag('div');
return $s;
}
/**
* Build up the data needed to submit a spam report to akismet and populate
* our spam report queue with it.
*
* @return int id of akismet spam report.
*/
public function populate_akismet_spam_report() {
global $DB;
$record = array();
$record['original_id'] = $this->post->id;
$record['is_spam'] = '1';
if ($akismetrecord = $DB->get_record('block_spam_deletion_akismet', $record, 'id')) {
// Already in the queue, dont add again.
return $akismetrecord->id;
}
$spammer = $DB->get_record('user', array('id' => $this->post->userid));
$postip = $this->get_ip_post_created_from();
$record['user_ip'] = $postip ? $postip : $spammer->lastip;
$record['user_agent'] = ''; // No chance, we dont log this.
$record['comment_author'] = fullname($spammer);
$record['comment_author_email'] = $spammer->email;
$record['comment_author_url'] = $spammer->url;
$record['comment_content'] = $this->post->subject."\n".$this->post->message;
return $DB->insert_record('block_spam_deletion_akismet', $record);
}
/**
* Attempts to get the original ip which a forum post was posted with.
*
* NOTE: This depends on the standard log store table being enabled
* which is pretty hacky..
* @return string|bool ip if found, false if not
*/
protected function get_ip_post_created_from() {
global $DB;
$params = array();
// NOTE: these fields are chosen carefuly to utilise existing index.
$params['userid'] = $this->post->userid;
$params['contextlevel'] = $this->context->contextlevel;
$params['contextinstanceid'] = $this->context->instanceid;
$params['crud'] = 'c';
$params['objectid'] = $this->post->id;
$params['eventname'] = '\mod_forum\event\post_created';
return $DB->get_field('logstore_standard_log', 'ip', $params);
}
}
class comment_spam extends spam_report
{
private $comment;
private $commentlib;
public function __construct($commentid) {
global $DB;
$this->comment = $DB->get_record('comments', array('id' => $commentid), '*', MUST_EXIST);
list($this->context, $this->course, $this->cm) = get_context_info_array($this->comment->contextid);
if (empty($this->course) and $this->context->id == SYSCONTEXTID) {
$this->course = get_site();
}
$options = new stdClass;
$options->context = $this->context;
$options->component = self::get_component_from_commentarea($this->comment->commentarea);
$options->area = $this->comment->commentarea;
$options->itemid = $this->comment->itemid;
$options->cm = $this->cm;
$options->course = $this->course;
$this->commentlib = new comment($options);
}
public function return_url() {
return $this->context->get_url();
}
public function has_permission() {
return $this->commentlib->can_post();
}
public function register_vote($userid) {
global $DB;
$record = new stdClass();
$record->spammerid = $this->comment->userid;
$record->voterid = $userid;
$record->weighting = $this->get_vote_weighting($userid);
$record->commentid = $this->comment->id;
$DB->insert_record('block_spam_deletion_votes', $record);
// Send message to notifiers.
$this->notify_spam($record->spammerid, $record->voterid);
}
public function has_voted($userid) {
global $DB;
$params = array('voterid' => $userid, 'commentid' => $this->comment->id);
return (bool) $DB->count_records('block_spam_deletion_votes', $params);
}
public function content_html() {
global $OUTPUT;
// This is to get the correctly formated data...
$comments = $this->commentlib->get_comments();
$commentid = $this->comment->id;
$filtered = array_filter($comments, function ($var) use ($commentid) {
if ($var->id == $commentid) {
return $var;
}
});
$o = $OUTPUT->box_start('generalbox center');
$o.= $this->commentlib->print_comment(array_shift($filtered));
$o.= $OUTPUT->box_end();
return $o;
}
private static function get_component_from_commentarea($commentarea) {
$component = null;
// TODO: this is an ugly hack because of MDL-37243, we don't store
// the component of a comment in the database, so have to 'guess' the component.
// Note that MDL-27548 has been fixed for 2.9 so we will want to
// address that in a future version of this block.
switch ($commentarea) {
case 'plugin_general':
$component = 'local_plugins';
break;
case 'page_comments':
$component = 'block_comments';
break;
case 'database_entry':
$component = 'data';
break;
case 'format_blog':
$component = 'blog';
break;
case 'glossary_entry':
$component = 'glossary';
break;
case 'wiki_page':
$component = 'wiki';
break;
case 'amos_contribution':
$component = 'local_amos';
break;
default:
throw new moodle_exception('unknowncomponent', 'block_spam_deletion', '', $commentarea);
}
return $component;
}
}
class spam_report_table extends table_sql
{
public function col_spammer($row) {
return html_writer::link(new moodle_url('/user/profile.php', array('id' => $row->spammerid)), fullname($row));
}
public function print_nothing_to_display() {
echo 'No spam reports :)';
}
}
class forum_spam_report_table extends spam_report_table
{
private $query = '';
public function __construct($uniqueid) {
parent::__construct($uniqueid);
$namefields = get_all_user_name_fields(true, 'u');
$this->query = "SELECT v.postid, f.subject, f.message, f.discussion, v.spammerid,
SUM(v.weighting) AS votes, COUNT(v.voterid) AS votecount, $namefields
FROM {block_spam_deletion_votes} v
JOIN {forum_posts} f ON f.id = v.postid
JOIN {user} u ON u.id = v.spammerid
WHERE v.postid IS NOT NULL
GROUP BY v.spammerid, v.postid, f.subject, f.message, f.discussion, $namefields
ORDER BY votes DESC, votecount DESC;";
$this->define_columns(array('postlink', 'message', 'spammer', 'votes', 'votecount', 'actions'));
$this->define_headers(array('Forum Post', 'Message', 'User', 'SPAM Score', 'Voters (weighting)', 'Actions'));
$this->collapsible(false);
$this->sortable(false);
}
public function query_db($pagesize, $useinitialsbar=true) {
global $DB;
$this->rawdata = $DB->get_records_sql($this->query, array(), $this->get_page_start(), $this->get_page_size());
}
public function col_votecount($row) {
global $DB;
$namefields = get_all_user_name_fields(true, 'u');
$votersql = "SELECT u.id, v.weighting, $namefields
FROM {block_spam_deletion_votes} v
JOIN {user} u ON u.id = v.voterid
WHERE v.postid = ?
ORDER BY $namefields";
$rs = $DB->get_recordset_sql($votersql, array($row->postid));
$voters = array();
foreach ($rs as $v) {
$voters[] = html_writer::link(new moodle_url('/user/profile.php', array('id' => $v->id)), fullname($v))." ({$v->weighting})";
}
$rs->close();
return implode('<br />', $voters);
}
public function col_postlink($row) {
$postlink = new moodle_url('/mod/forum/discuss.php', array('d' => $row->discussion));
$postlink->set_anchor('p'.$row->postid);
return html_writer::link($postlink, format_text($row->subject));
}
public function col_actions($row) {
global $OUTPUT;
$notspamurl = new moodle_url('/blocks/spam_deletion/marknotspam.php', array('postid' => $row->postid));
return $OUTPUT->single_button($notspamurl, 'Mark not spam');
}
}
class forum_deleted_spam_report_table extends spam_report_table
{
public function __construct($uniqueid) {
parent::__construct($uniqueid);
$namefields = get_all_user_name_fields(true, 'u');
$this->query = "SELECT v.postid, v.spammerid, SUM(v.weighting) AS votes,
COUNT(v.voterid) AS votecount, $namefields
FROM {block_spam_deletion_votes} v
JOIN {user} u ON u.id = v.spammerid
LEFT OUTER JOIN {forum_posts} f ON f.id = v.postid
WHERE f.id IS NULL AND v.postid IS NOT NULL
GROUP BY v.spammerid, v.postid, $namefields
ORDER BY votes DESC, votecount DESC";
$this->define_columns(array('spammer', 'votes', 'votecount', 'actions'));
$this->define_headers(array('User', 'SPAM Score', 'Voters (weighting)', 'Actions'));
$this->collapsible(false);
$this->sortable(false);
}
public function query_db($pagesize, $useinitialsbar=true) {
global $DB;
$this->rawdata = $DB->get_records_sql($this->query, array(), $this->get_page_start(), $this->get_page_size());
}
public function col_votecount($row) {
global $DB;
$namefields = get_all_user_name_fields(true, 'u');
$votersql = "SELECT u.id, v.weighting, $namefields
FROM {block_spam_deletion_votes} v
JOIN {user} u ON u.id = v.voterid
WHERE v.postid = ?
ORDER BY $namefields";
$rs = $DB->get_recordset_sql($votersql, array($row->postid));
$voters = array();
foreach ($rs as $v) {
$voters[] = html_writer::link(new moodle_url('/user/profile.php', array('id' => $v->id)), fullname($v))." ({$v->weighting})";
}
$rs->close();
return implode('<br />', $voters);
}
public function col_actions($row) {
global $OUTPUT;
$notspamurl = new moodle_url('/blocks/spam_deletion/marknotspam.php', array('postid' => $row->postid));
return $OUTPUT->single_button($notspamurl, 'Remove spam report');
}
}
class comment_spam_report_table extends spam_report_table
{
public function __construct($uniqueid) {
parent::__construct($uniqueid);
$namefields = get_all_user_name_fields(true, 'u');
$this->query = "SELECT v.commentid, c.content, v.spammerid, c.contextid,
SUM(v.weighting) AS votes, COUNT(v.voterid) AS votecount,
$namefields
FROM {block_spam_deletion_votes} v
JOIN {comments} c ON c.id = v.commentid
JOIN {user} u ON u.id = v.spammerid
WHERE v.commentid IS NOT NULL
GROUP BY v.commentid, c.content, v.spammerid, c.contextid, $namefields
ORDER BY votes DESC, votecount DESC";
$this->define_columns(array('postlink', 'content', 'spammer', 'votes', 'votecount', 'actions'));
$this->define_headers(array('Comment Location', 'Content', 'User', 'SPAM Score', 'Voters (weighting)', 'Actions'));
$this->collapsible(false);
$this->sortable(false);
}
public function query_db($pagesize, $useinitialsbar=true) {
global $DB;
$this->rawdata = $DB->get_records_sql($this->query, array(), $this->get_page_start(), $this->get_page_size());
}
public function col_votecount($row) {
global $DB;
$namefields = get_all_user_name_fields(true, 'u');
$votersql = "SELECT u.id, v.weighting, $namefields
FROM {block_spam_deletion_votes} v
JOIN {user} u ON u.id = v.voterid
WHERE v.commentid = ?
ORDER BY $namefields";
$rs = $DB->get_recordset_sql($votersql, array($row->commentid));
$voters = array();
foreach ($rs as $v) {
$voters[] = html_writer::link(new moodle_url('/user/profile.php', array('id' => $v->id)), fullname($v))." ({$v->weighting})";
}
$rs->close();
return implode('<br />', $voters);
}
public function col_postlink($row) {
$context = context::instance_by_id($row->contextid);
return html_writer::link($context->get_url(), $context->get_context_name());
}
public function col_actions($row) {
global $OUTPUT;
$notspamurl = new moodle_url('/blocks/spam_deletion/marknotspam.php', array('commentid' => $row->commentid));
return $OUTPUT->single_button($notspamurl, 'Mark not spam');
}
}
class comment_deleted_spam_report_table extends spam_report_table
{
public function __construct($uniqueid) {
parent::__construct($uniqueid);
$namefields = get_all_user_name_fields(true, 'u');
$this->query = "SELECT v.commentid, v.spammerid,
SUM(v.weighting) AS votes, COUNT(v.voterid) AS votecount,
$namefields
FROM {block_spam_deletion_votes} v
JOIN {user} u ON u.id = v.spammerid
LEFT OUTER JOIN {comments} f ON f.id = v.commentid
WHERE f.id IS NULL AND v.commentid IS NOT NULL
GROUP BY v.commentid, v.spammerid, $namefields
ORDER BY votes DESC, votecount DESC";
$this->define_columns(array('spammer', 'votes', 'votecount', 'actions'));
$this->define_headers(array('User', 'SPAM Score', 'Voters (weighting)', 'Actions'));
$this->collapsible(false);
$this->sortable(false);
}
public function col_votecount($row) {
global $DB;
$namefields = get_all_user_name_fields(true, 'u');
$votersql = "SELECT u.id, v.weighting, $namefields
FROM {block_spam_deletion_votes} v
JOIN {user} u ON u.id = v.voterid
WHERE v.commentid = ?
ORDER BY $namefields";
$rs = $DB->get_recordset_sql($votersql, array($row->commentid));
$voters = array();
foreach ($rs as $v) {
$voters[] = html_writer::link(new moodle_url('/user/profile.php', array('id' => $v->id)), fullname($v))." ({$v->weighting})";
}
$rs->close();
return implode('<br />', $voters);
}
public function query_db($pagesize, $useinitialsbar=true) {
global $DB;
$this->rawdata = $DB->get_records_sql($this->query, array(), $this->get_page_start(), $this->get_page_size());
}
public function col_actions($row) {
global $OUTPUT;
$notspamurl = new moodle_url('/blocks/spam_deletion/marknotspam.php', array('commentid' => $row->commentid));
return $OUTPUT->single_button($notspamurl, 'Remove spam report');
}
}
class user_profile_spam_table extends spam_report_table
{
public function __construct($uniqueid) {
parent::__construct($uniqueid);
$namefields = get_all_user_name_fields(true, 'u');
$this->query = "SELECT v.spammerid, u.description, SUM(v.weighting) AS votes,
COUNT(v.voterid) AS votecount, $namefields
FROM {block_spam_deletion_votes} v
JOIN {user} u ON u.id = v.spammerid
WHERE v.postid IS NULL AND v.commentid IS NULL
GROUP BY v.spammerid, u.description, $namefields
ORDER BY votes DESC, votecount DESC";
$this->define_columns(array('spammer', 'description', 'votecount', 'actions'));
$this->define_headers(array('User', 'Profile description', 'Votes', 'Actions'));
$this->collapsible(false);
$this->sortable(false);
}
public function col_votecount($row) {
global $DB;
$namefields = get_all_user_name_fields(true, 'u');
$votersql = "SELECT v.id, u.id AS userid, v.weighting, $namefields
FROM {block_spam_deletion_votes} v
LEFT OUTER JOIN {user} u ON u.id = v.voterid
WHERE v.postid IS NULL AND v.commentid IS NULL AND
v.spammerid = ?
ORDER BY $namefields";
$rs = $DB->get_recordset_sql($votersql, array($row->spammerid));
$voters = array();
foreach ($rs as $v) {
if (empty($v->userid)) {
$voters[] = "SYSTEM ({$v->weighting})";
} else {
$voters[] = html_writer::link(new moodle_url('/user/profile.php', array('id' => $v->id)), fullname($v))." ({$v->weighting})";
}
}
$rs->close();
return implode('<br />', $voters);
}
public function query_db($pagesize, $useinitialsbar=true) {
global $DB;
$this->rawdata = $DB->get_records_sql($this->query, array(), $this->get_page_start(), $this->get_page_size());
}
public function col_actions($row) {
global $OUTPUT;
$notspamurl = new moodle_url('/blocks/spam_deletion/marknotspam.php', array('spammerid' => $row->spammerid));
return $OUTPUT->single_button($notspamurl, 'Remove spam report');
}
}
class akismet_table extends table_sql
{
public function __construct($uniqueid) {
parent::__construct($uniqueid);
$this->set_sql('id, comment_author, comment_content', '{block_spam_deletion_akismet}', 'is_spam = ?', array('1'));
$this->define_columns(array('comment_author', 'comment_content', 'actions'));
$this->define_headers(array('Users Name', 'Spam content', 'Actions'));
$this->collapsible(false);
$this->sortable(false);
}
public function col_actions($row) {
global $OUTPUT;
$reporturl = new moodle_url('/blocks/spam_deletion/improveakismet.php', array('id' => $row->id));
$ignoreurl = clone $reporturl;
$ignoreurl->param('ignore', true);
return $OUTPUT->single_button($reporturl, 'Report to akismet') .
$OUTPUT->single_button($ignoreurl, 'Ignore (not spam)');
}
public function print_nothing_to_display() {
echo 'Nothing to send to akismet';
}
}