Skip to content

Commit

Permalink
Add ability to reopen conversation
Browse files Browse the repository at this point in the history
  • Loading branch information
sumaiyamannan committed Oct 9, 2024
1 parent a9cb77e commit 451abbc
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 2 deletions.
33 changes: 33 additions & 0 deletions classes/conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,39 @@ public function close() {
return true;
}

/**
* Reopen
* @return bool
* @throws \coding_exception
* @throws \dml_exception
* @throws \moodle_exception
*/
public function reopen() {
global $DB, $USER;

$context = $this->dialogue->context;

// Is this a draft.
if (is_null($this->_conversationid)) {
throw new \moodle_exception('cannotreopendraftconversation', 'dialogue');
}
// Permission check.
$canopen = (($this->_authorid == $USER->id) || has_capability('mod/dialogue:reopen', $context) ||
has_capability('mod/dialogue:reopenany', $context));
if (!$canopen) {
throw new \moodle_exception('nopermissiontoreopen', 'dialogue');
}

$openstate = dialogue::STATE_OPEN;
$closedstate = dialogue::STATE_CLOSED;
$params = array('conversationid' => $this->conversationid, 'state' => $closedstate);

// Reopen all messages in conversation that have a close state, we don't worry about drafts etc.
$DB->set_field('dialogue_messages', 'state', $openstate, $params);

return true;
}

/**
* Delete
* @return bool
Expand Down
71 changes: 71 additions & 0 deletions classes/event/conversation_reopened.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?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/>.

namespace mod_dialogue\event;


/**
* The mod_dialogue conversation reopened event.
*
* @package mod_dialogue
* @copyright Catalyst IT Ltd
* @author Sumaiya Javed <<[email protected]>>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class conversation_reopened extends \core\event\base {
/**
* Init method.
*
* @return void
*/
protected function init() {
$this->data['crud'] = 'u';
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'dialogue_conversations';
}

/**
* Returns description of what happened.
*
* @return string
*/
public function get_description() {
return "The user with id '$this->userid' has reopened the conversation with " .
"id '$this->objectid' in the dialogue with the course module id '$this->contextinstanceid'.";
}

/**
* Return localised event name.
*
* @return string
*/
public static function get_name() {
return get_string('eventconversationreopened', 'mod_dialogue');
}

/**
* Get URL related to the action
*
* @return \moodle_url
*/
public function get_url() {

$url = new \moodle_url('/mod/dialogue/conversation.php', array('conversationid' => $this->objectid,
'id' => $this->contextinstanceid));

return $url;
}
}
22 changes: 22 additions & 0 deletions conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,28 @@
exit;
}

// Reopen conversation.
if ($action == 'reopen') {
if (!empty($confirm) && confirm_sesskey()) {
$conversation->reopen();
// Trigger conversation closed event.
$eventparams = array(
'context' => $context,
'objectid' => $conversation->conversationid
);
$event = \mod_dialogue\event\conversation_reopened::create($eventparams);
$event->trigger();
redirect($returnurl, get_string('conversationreopen', 'dialogue',
$conversation->subject));
}
echo $OUTPUT->header($activityrecord->name);
$pageurl->param('confirm', $conversationid);
$message = get_string('conversationreopenconfirm', 'dialogue', $conversation->subject);
echo $OUTPUT->confirm($message, $pageurl, $returnurl);
echo $OUTPUT->footer();
exit;
}

// Close conversation.
if ($action == 'close') {
if (!empty($confirm) && confirm_sesskey()) {
Expand Down
24 changes: 24 additions & 0 deletions db/access.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,30 @@
)
),

'mod/dialogue:reopen' => array(

'captype' => 'read',
'contextlevel' => CONTEXT_MODULE,
'archetypes' => array(
'student' => CAP_PREVENT,
'teacher' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
'manager' => CAP_PREVENT
)
),

'mod/dialogue:reopenany' => array(

'captype' => 'read',
'contextlevel' => CONTEXT_MODULE,
'archetypes' => array(
'student' => CAP_PREVENT,
'teacher' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
'manager' => CAP_PREVENT
)
),

'mod/dialogue:receive' => array(

'captype' => 'read',
Expand Down
8 changes: 8 additions & 0 deletions lang/en/dialogue.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
$string['cachedef_unreadcounts'] = 'Users unread message counts in conversations';
$string['cachedef_userdetails'] = 'User brief details, all enrolled users';
$string['cannotclosedraftconversation'] = 'You cannot close a conversation that hasn\'t started!';
$string['cannotreopendraftconversation'] = 'You reopen a conversation that hasn\'t started!';
$string['cannotdeleteopenconversation'] = 'You cannot delete a open conversation';
$string['closeconversation'] = 'Close conversation';
$string['closed'] = 'Closed';
Expand All @@ -49,6 +50,8 @@
$string['conversationclosed'] = 'Conversation {$a} has been closed';
$string['conversationdeleteconfirm'] = 'Are you sure you want to delete conversation {$a}, this cannot be undone?';
$string['conversationdeleted'] = 'Conversation {$a} has been deleted';
$string['conversationreopenconfirm'] = 'Are you sure you want to reopen conversation {$a} ?';
$string['conversationreopened'] = 'Conversation {$a} has been reopened';
$string['conversationdiscarded'] = 'Conversation discarded';
$string['conversationlistdisplayheader'] = 'Displaying {$a->show} {$a->state} conversations {$a->groupname}';
$string['conversationopened'] = 'Conversation has been opened';
Expand All @@ -65,6 +68,7 @@
$string['deletealldrafts'] = 'Delete all drafts';
$string['deleteallrules'] = 'Delete all opener rules';
$string['deleteconversation'] = 'Delete conversation';
$string['reopenconversation'] = 'Reopen conversation';
$string['deletereply'] = 'Delete reply';
$string['dialogue:addinstance'] = 'Add a Dialogue';
$string['dialogue:bulkopenrulecreate'] = 'Create a bulk opener rule';
Expand All @@ -74,6 +78,8 @@
$string['dialogue:delete'] = 'Delete own';
$string['dialogue:deleteany'] = 'Delete any';
$string['dialogue:open'] = 'Open a conversation';
$string['dialogue:reopen'] = 'Reopen own';
$string['dialogue:reopenany'] = 'Reopen any';
$string['dialogue:receive'] = 'Receive, who can be the recipient when opening a conversation';
$string['dialogue:reply'] = 'Reply';
$string['dialogue:replyany'] = 'Reply any';
Expand All @@ -100,6 +106,7 @@
$string['eventconversationcreated'] = 'Conversation created';
$string['eventconversationdeleted'] = 'Conversation deleted';
$string['eventconversationviewed'] = 'Conversation viewed';
$string['conversationreopen'] = 'Conversation reopened';
$string['eventreplycreated'] = 'Reply created';
$string['everybody'] = 'Everybody (free for all)';
$string['everyone'] = 'Everyone';
Expand Down Expand Up @@ -143,6 +150,7 @@
$string['nodraftsfound'] = 'No drafts found!';
$string['nomatchingpeople'] = 'No people match \'{$a}\'';
$string['nopermissiontoclose'] = 'You do not have permission to close this conversation!';
$string['nopermissiontoreopen'] = 'You do not have permission to reopen this conversation!';
$string['nopermissiontodelete'] = 'You do not have permission to delete!';
$string['nosubject'] = '[no subject]';
$string['numberattachments'] = '{$a} attachments';
Expand Down
16 changes: 16 additions & 0 deletions renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@ public function render_conversation(mod_dialogue\conversation $conversation) {
}
}

if ($conversation->state == \mod_dialogue\dialogue::STATE_CLOSED) {
$canreopen = ((has_capability('mod/dialogue:reopen', $context) && $USER->id == $conversation->author->id) ||
has_capability('mod/dialogue:reopenany', $context));

if ($canreopen) {
$html .= html_writer::start_tag('li');
$trashicon = html_writer::tag('i', '', array('class' => "fa fa-trash-o"));
$reopenurl = new moodle_url('/mod/dialogue/conversation.php');
$reopenurl->param('id', $cm->id);
$reopenurl->param('conversationid', $conversation->conversationid);
$reopenurl->param('action', 'reopen');
$html .= html_writer::link($reopenurl, get_string('reopenconversation', 'dialogue') . $trashicon);
$html .= html_writer::end_tag('li');
}
}

$candelete = ((has_capability('mod/dialogue:delete', $context) && $USER->id == $conversation->author->id) ||
has_capability('mod/dialogue:deleteany', $context));

Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
*/
defined('MOODLE_INTERNAL') || die();

$plugin->version = 2024050200;
$plugin->release = 2024050200;
$plugin->version = 2024100901;
$plugin->release = 2024100901;
$plugin->requires = 2022112805; // Requires 4.1 or higher.
$plugin->component = 'mod_dialogue'; // Full name of the plugin (used for diagnostics).
$plugin->maturity = MATURITY_STABLE; // This version's maturity level.
Expand Down

0 comments on commit 451abbc

Please sign in to comment.