Skip to content

Commit

Permalink
fix user null
Browse files Browse the repository at this point in the history
  • Loading branch information
EsdrasCaleb committed May 15, 2024
2 parents 706aa4c + 8d54830 commit 094c113
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ some time. In the same way, Moodle enables you to do notifications.
To use it go to Administration > Messages > Auto Delete Messages and set the time
when you want messages to be auto deleted.
To install the plugin use the admin panel or put it in the tools folder in MOODLE_DIR_ROOT/admin/tool

Now delete group messagens and private messages

License
=======
Expand Down
61 changes: 38 additions & 23 deletions classes/task/delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,13 @@ public function execute() {
$delteaction = \core_message\api::MESSAGE_ACTION_DELETED;// 2
$viewaction = \core_message\api::MESSAGE_ACTION_READ;// 1
$users = null;
$whereaction = "WHERE uad.id is null";

if ($configs->harddelete) {
$whereaction = "";
$types = $individualmessage;
if ($configs->deletegroupmessages > 0) {
$types .= ','.\core_message\api::MESSAGE_CONVERSATION_TYPE_GROUP;
}
if ($configs->deletepersonalmessage > 0) {
$types .= ','.\core_message\api::MESSAGE_CONVERSATION_TYPE_SELF;
}
if ($configs->deletereadmessages > 0) {
$reftime = time() - $configs->deletereadmessages;
Expand All @@ -68,15 +71,18 @@ public function execute() {
foreach ($users as $user) {
$sql = "SELECT m.id as messageid,ua.userid FROM {message_conversations} c
JOIN {messages} m on m.conversationid =c.id
and c.type=? and m.timecreated<?
and c.type in (:types) and m.timecreated<:timeref
JOIN {message_user_actions} ua on ua.messageid=m.id
and ua.action=? and ua.timecreated<? and ua.userid=?
{$whereaction}";
$readmessagens = $DB->get_records_sql($sql, [$individualmessage, $reftime, $viewaction, $reftime,
$user->userid, ]);
foreach ($readmessagens as $readmessage) {
if ($configs->harddelete) {
\core_message\api::delete_message($readmessage->userid, $readmessage->messageid);
and ua.action=:actiontype and ua.timecreated<:timeref2 and ua.userid=:userref
";

$readmessagens = $DB->get_records_sql($sql, ['types' => $individualmessage,
'timeref' => $reftime, 'actiontype' => $viewaction,
'userref' => $user->userid, 'timeref2' => $reftime,
]);
foreach ($readmessagens as $readmessage) {// Just soft delete if both has saw it will be hard deleted.
if($readmessage->useridfrom && $DB->record_exists('user', ['id' => $readmessage->useridfrom])) {
\core_message\api::delete_message($readmessage->useridfrom, $readmessage->messageid);
}
}
}
Expand All @@ -90,29 +96,38 @@ public function execute() {
$reftime = time() - $configs->deleteallmessages;
$sql = "SELECT m.id as messageid,m.useridfrom FROM {message_conversations} c
JOIN {messages} m on m.conversationid =c.id
LEFT JOIN {message_user_actions} uad on uad.messageid=m.id and uad.action=?
and c.type=? and m.timecreated<? and m.useridfrom=?
{$whereaction}";
$readmessagens = $DB->get_records_sql($sql, [$individualmessage, $reftime, $user->userid,]);
and c.type in (:types) and m.timecreated<:timeref and m.useridfrom=:userref
LEFT JOIN {message_user_actions} uad on uad.messageid=m.id
";
$readmessagens = $DB->get_records_sql($sql, ['types' => $individualmessage,
'timeref' => $reftime, 'userref' => $user->userid,
]);
foreach ($readmessagens as $readmessage) {
if ($configs->harddelete) {
if ($configs->harddelete) {// If is old it need to be deleted.
hard_delete_message($readmessage->messageid);
} else {
if($DB->record_exists('user', array('messageid'=>$readmessage->messageid))) {}
} elseif($readmessage->useridfrom && $DB->record_exists('user', ['id' => $readmessage->useridfrom])) {
\core_message\api::delete_message($readmessage->useridfrom, $readmessage->messageid);
}
}
}
}
if ($configs->cleanmessage) {
$sql = "SELECT c.id,count(ua.id) as usuarios_deletado,count(DISTINCT m.id) as mensagens FROM {message_conversations} c
$sql = "SELECT c.id,count(ua.id) as user_d,count(DISTINCT m.id) as mensagens FROM {message_conversations} c
JOIN {messages} m on m.conversationid =c.id
and c.type=?
join {message_conversation_members} cm on cm.conversationid = c.id
JOIN {message_user_actions} ua on ua.messageid=m.id
and ua.action=?
and ua.action=:deleteaction
GROUP BY c.id
HAVING count(DISTINCT ua.userid) >=count(distinct cm.userid)
and count(DISTINCT ua.messageid) >= count(DISTINCT m.id)
UNION
SELECT c.id,0 as user_d,count(DISTINCT m.id) as mensagens FROM {message_conversations} c
LEFT JOIN {messages} m on m.conversationid =c.id
WHERE m.id is null
GROUP BY c.id
HAVING count(ua.id) >=2*count(DISTINCT m.id)";
$deletedconversation = $DB->get_records_sql($sql, [$individualmessage, $delteaction]);
HAVING count(m.id) = 0
";
$deletedconversation = $DB->get_records_sql($sql, ['deleteaction' => $delteaction]);
foreach ($deletedconversation as $conversation) {
\core_message\api::delete_all_conversation_data($conversation->id);
}
Expand Down
4 changes: 4 additions & 0 deletions lang/en/tool_deletemessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@
$string['harddelete_desc'] = "All Deleted messages by this plugin will be deleted from database";
$string['cleanmessage'] = "Clear Conversations";
$string['cleanmessage_desc'] = "When Both user delete all messages between them the messages will be erased from database";
$string['deletegroupmessages'] = "Delete Group Messages";
$string['deletegroupmessages_desc'] = "Include Group Messagens in filter to delete old and readed messages";
$string['deletepersonalmessage'] = "Delete Private Messages";
$string['deletepersonalmessage_desc'] = "Delete messages in the personal conversation, messages send by user to himself if they are in filter";
4 changes: 4 additions & 0 deletions lang/pt_br/tool_deletemessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@
$string['harddelete_desc'] = "Todas as mensagens deletadas pelo plugin serão excluidas do banco de dados";
$string['cleanmessage'] = "Limpar Conversas";
$string['cleanmessage_desc'] = "Quando dois usuários apagam todas as mensagens entre eles as conversas serão apagadas do banco de dados";
$string['deletegroupmessages'] = "Deletar Mensagens de Grupo";
$string['deletegroupmessages_desc'] = "Incluir mensagens de grupo nas mensagens a serem apagadas apos leitura";
$string['deletepersonalmessage'] = "Deletar Mensagens Privadas";
$string['deletepersonalmessage_desc'] = "Incluir as mensagens do usuário para ele mesmo para serem apagadas pelo filtro";
8 changes: 8 additions & 0 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@
get_string('cleanmessage', 'tool_deletemessage'),
get_string('cleanmessage_desc', 'tool_deletemessage'), $default));

$page->add(new admin_setting_configcheckbox('tool_deletemessage/deletegroupmessages',
get_string('deletegroupmessages', 'tool_deletemessage'),
get_string('deletegroupmessages_desc', 'tool_deletemessage'), $default));

$page->add(new admin_setting_configcheckbox('tool_deletemessage/deletepersonalmessage',
get_string('deletepersonalmessage', 'tool_deletemessage'),
get_string('deletepersonalmessage_desc', 'tool_deletemessage'), $default));

$ADMIN->add('messaging', $page);

}
Expand Down
129 changes: 129 additions & 0 deletions tests/tool_deletemessage_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?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/>.
/**
* Delete Task.
*
* @package tool_deletemessage
* @author Esdras Caleb
* @copyright 2023 Esdras Caleb
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace tool_deletemessage;


/**
* Class test if this plugin is deleting things it should not delete
* @author Esdras Caleb
* @copyright 2023 Esdras Caleb
* @package core_message
* @category test
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_deletemessage_test extends \advanced_testcase {

/**
* Test set up.
*
* This is executed before running any test in this file.
*/
public function setUp(): void {
$this->preventResetByRollback(); // Messaging is not compatible with transactions.
$this->messagesink = $this->redirectMessages();
$this->resetAfterTest();
}

/**
* Make message to tests
* @return int message id
*/
private function make_message() {
global $DB;

$userfrom = $this->getDataGenerator()->create_user();
$userto = $this->getDataGenerator()->create_user();

// Message text.
$message = "hello";

if (empty($time)) {
$time = time();
}

if ($userfrom->id == $userto->id) {
// It's a self conversation.
$conversation = \core_message\api::get_self_conversation($userfrom->id);
if (empty($conversation)) {
$conversation = \core_message\api::create_conversation(
\core_message\api::MESSAGE_CONVERSATION_TYPE_SELF,
[$userfrom->id]
);
}
$conversationid = $conversation->id;
} else if (!$conversationid = \core_message\api::get_conversation_between_users([$userfrom->id, $userto->id])) {
// It's an individual conversation between two different users.
$conversation = \core_message\api::create_conversation(
\core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
[
$userfrom->id,
$userto->id,
]
);
$conversationid = $conversation->id;
}

// Ok, send the message.
$record = new \stdClass();
$record->useridfrom = $userfrom->id;
$record->conversationid = $conversationid;
$record->subject = 'No subject';
$record->fullmessage = $message;
$record->smallmessage = $message;
$record->timecreated = $time;

return $DB->insert_record('messages', $record);
}

/**
* Test if the hard delection function works
* @return void
*/
public function test_deleting() {
global $CFG, $DB;
require_once($CFG->dirroot.'/admin/tool/deletemessage/locallib.php');

$messageid = $this->make_message();
$this->assertNotEmpty($DB->get_records('messages', ['id' => $messageid]));
hard_delete_message($messageid);
$this->assertEmpty($DB->get_records('messages', ['id' => $messageid]));
}

/**
* Test taks of delection to not delete all messages
* @return void
*/
public function test_taks_isnotdeleting() {
global $CFG, $DB;
require_once($CFG->dirroot.'/admin/tool/deletemessage/locallib.php');

$messageid = $this->make_message();

$cron = new \tool_deletemessage\task\delete();
$cron->execute();
$this->assertNotEmpty($DB->get_records('messages', ['id' => $messageid]));
$this->assertNotEmpty($DB->get_records('messages'));
}
}
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
defined('MOODLE_INTERNAL') || die();

// Plugin version.
$plugin->version = 2024012900;
$plugin->version = 2024051500;

// Required Moodle version.
$plugin->requires = 2018051718; // Requires this Moodle version - at least 3.5 (new messsage system).
Expand Down

0 comments on commit 094c113

Please sign in to comment.