@@ -440,4 +440,116 @@ public static function empty_or_delete_comment($comment) {
440440 }
441441 }
442442
443+ /**
444+ * Finds the users in the given userlists's context.
445+ * @param userlist $userlist
446+ */
447+ public static function get_users_in_context (userlist $ userlist ) {
448+ $ context = $ userlist ->get_context ();
449+
450+ if (!$ context instanceof \context_module) {
451+ return ;
452+ }
453+
454+ $ params = [
455+ 'contextid ' => $ context ->instanceid ,
456+ 'modulename ' => 'pdfannotator ' ,
457+ ];
458+
459+ // Comments.
460+ $ sql = "SELECT ac.userid
461+ FROM {course_modules} cm
462+ JOIN {modules} m ON m.id = cm.module AND m.name = :modulename
463+ JOIN {pdfannotator} a ON a.id = cm.instance
464+ JOIN {pdfannotator_comments} ac ON ac.pdfannotatorid = a.id
465+ WHERE cm.id = :contextid " ;
466+ $ userlist ->add_from_sql ('userid ' , $ sql , $ params );
467+
468+ // Reports.
469+ $ sql = "SELECT ar.userid
470+ FROM {course_modules} cm
471+ JOIN {modules} m ON m.id = cm.module AND m.name = :modulename
472+ JOIN {pdfannotator} a ON a.id = cm.instance
473+ JOIN {pdfannotator_reports} ar ON ar.pdfannotatorid = a.id
474+ WHERE cm.id = :contextid " ;
475+ $ userlist ->add_from_sql ('userid ' , $ sql , $ params );
476+
477+ // Annotations.
478+ $ sql = "SELECT ats.userid
479+ FROM {course_modules} cm
480+ JOIN {modules} m ON m.id = cm.module AND m.name = :modulename
481+ JOIN {pdfannotator} a ON a.id = cm.instance
482+ JOIN {pdfannotator_annotations} ats ON ats.pdfannotatorid = a.id
483+ WHERE cm.id = :contextid " ;
484+ $ userlist ->add_from_sql ('userid ' , $ sql , $ params );
485+
486+ // Votes.
487+ $ sql = "SELECT v.userid
488+ FROM {course_modules} cm
489+ JOIN {modules} m ON m.id = cm.module AND m.name = :modulename
490+ JOIN {pdfannotator} a ON a.id = cm.instance
491+ JOIN {pdfannotator_comments} ac ON ac.pdfannotatorid = a.id
492+ JOIN {pdfannotator_votes} v ON v.commentid = ac.id
493+ WHERE cm.id = :contextid " ;
494+ $ userlist ->add_from_sql ('userid ' , $ sql , $ params );
495+
496+ // Subscriptions
497+ $ sql = "SELECT asub.userid
498+ FROM {course_modules} cm
499+ JOIN {modules} m ON m.id = cm.module AND m.name = :modulename
500+ JOIN {pdfannotator} a ON a.id = cm.instance
501+ JOIN {pdfannotator_annotations} ats ON ats.pdfannotatorid = a.id
502+ JOIN {pdfannotator_subscriptions} asub ON a.id = asub.annotationid
503+ WHERE cm.id = :contextid " ;
504+ $ userlist ->add_from_sql ('userid ' , $ sql , $ params );
505+ }
506+
507+ /**
508+ * Deletes data for users in given userlist's context.
509+ * @param approved_userlist $userlist
510+ */
511+ public static function delete_data_for_users (approved_userlist $ userlist ) {
512+ global $ DB ;
513+
514+ $ context = $ userlist ->get_context ();
515+
516+ if (!$ context instanceof \context_module) {
517+ return ;
518+ }
519+
520+ list ($ userinsql , $ userinparams ) = $ DB ->get_in_or_equal ($ userlist ->get_userids (), SQL_PARAMS_NAMED );
521+
522+ // Find the instance.
523+ $ annotationinstance = get_coursemodule_from_id ('pdfannotator ' , $ context ->instanceid );
524+ $ annotatorid = $ annotationinstance ->id ;
525+
526+ // Combine instance + user sql.
527+ $ params = array_merge (['pdfannotatorid ' => $ annotatorid ], $ userinparams );
528+ $ sql = "pdfannotatorid = :pdfannotatorid AND userid {$ userinsql }" ;
529+
530+ // Delete subscriptions.
531+ $ annotations = $ DB ->get_records ('pdfannotator_annotations ' , ['pdfannotatorid ' => $ annotatorid ]);
532+ $ annotationids = array_column ($ annotations , 'id ' );
533+ list ($ subinsql , $ subinparams ) = $ DB ->get_in_or_equal ($ annotationids , SQL_PARAMS_NAMED );
534+
535+ $ DB ->execute ("DELETE FROM {pdfannotator_subscriptions} sub
536+ WHERE sub.userid {$ userinsql }
537+ AND sub.annotationid {$ subinsql }" ,
538+ array_merge ($ userinparams , $ subinparams ));
539+
540+ // Delete votes.
541+ $ comments = $ DB ->get_records ('pdfannotator_comments ' , ['pdfannotatorid ' => $ annotatorid ]);
542+ $ commentsids = array_column ($ comments , 'id ' );
543+ list ($ commentinsql , $ commentinparams ) = $ DB ->get_in_or_equal ($ commentsids , SQL_PARAMS_NAMED );
544+
545+ $ DB ->execute ("DELETE FORM {pdfannotator_votes} votes
546+ WHERE vote.userid {$ userinsql }
547+ AND vote.commentid {$ commentinsql }" ,
548+ array_merge ($ userinparams , $ commentinparams ));
549+
550+ // Delete rest of data.
551+ $ DB ->delete_records_select ('pdfannotator_annotations ' , $ sql , $ params );
552+ $ DB ->delete_records_select ('pdfannotator_reports ' , $ sql , $ params );
553+ $ DB ->delete_records_select ('pdfannotator_comments ' , $ sql , $ params );
554+ }
443555}
0 commit comments