Skip to content

Commit ee40096

Browse files
authored
Merge pull request #138 from sumaiyamannan/reopenconversation
Add ability to reopen conversation for a role with reopen / reopen permissions
2 parents a9cb77e + 451abbc commit ee40096

7 files changed

+176
-2
lines changed

classes/conversation.php

+33
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,39 @@ public function close() {
168168
return true;
169169
}
170170

171+
/**
172+
* Reopen
173+
* @return bool
174+
* @throws \coding_exception
175+
* @throws \dml_exception
176+
* @throws \moodle_exception
177+
*/
178+
public function reopen() {
179+
global $DB, $USER;
180+
181+
$context = $this->dialogue->context;
182+
183+
// Is this a draft.
184+
if (is_null($this->_conversationid)) {
185+
throw new \moodle_exception('cannotreopendraftconversation', 'dialogue');
186+
}
187+
// Permission check.
188+
$canopen = (($this->_authorid == $USER->id) || has_capability('mod/dialogue:reopen', $context) ||
189+
has_capability('mod/dialogue:reopenany', $context));
190+
if (!$canopen) {
191+
throw new \moodle_exception('nopermissiontoreopen', 'dialogue');
192+
}
193+
194+
$openstate = dialogue::STATE_OPEN;
195+
$closedstate = dialogue::STATE_CLOSED;
196+
$params = array('conversationid' => $this->conversationid, 'state' => $closedstate);
197+
198+
// Reopen all messages in conversation that have a close state, we don't worry about drafts etc.
199+
$DB->set_field('dialogue_messages', 'state', $openstate, $params);
200+
201+
return true;
202+
}
203+
171204
/**
172205
* Delete
173206
* @return bool
+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace mod_dialogue\event;
18+
19+
20+
/**
21+
* The mod_dialogue conversation reopened event.
22+
*
23+
* @package mod_dialogue
24+
* @copyright Catalyst IT Ltd
25+
* @author Sumaiya Javed <<[email protected]>>
26+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27+
*/
28+
class conversation_reopened extends \core\event\base {
29+
/**
30+
* Init method.
31+
*
32+
* @return void
33+
*/
34+
protected function init() {
35+
$this->data['crud'] = 'u';
36+
$this->data['edulevel'] = self::LEVEL_OTHER;
37+
$this->data['objecttable'] = 'dialogue_conversations';
38+
}
39+
40+
/**
41+
* Returns description of what happened.
42+
*
43+
* @return string
44+
*/
45+
public function get_description() {
46+
return "The user with id '$this->userid' has reopened the conversation with " .
47+
"id '$this->objectid' in the dialogue with the course module id '$this->contextinstanceid'.";
48+
}
49+
50+
/**
51+
* Return localised event name.
52+
*
53+
* @return string
54+
*/
55+
public static function get_name() {
56+
return get_string('eventconversationreopened', 'mod_dialogue');
57+
}
58+
59+
/**
60+
* Get URL related to the action
61+
*
62+
* @return \moodle_url
63+
*/
64+
public function get_url() {
65+
66+
$url = new \moodle_url('/mod/dialogue/conversation.php', array('conversationid' => $this->objectid,
67+
'id' => $this->contextinstanceid));
68+
69+
return $url;
70+
}
71+
}

conversation.php

+22
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,28 @@
105105
exit;
106106
}
107107

108+
// Reopen conversation.
109+
if ($action == 'reopen') {
110+
if (!empty($confirm) && confirm_sesskey()) {
111+
$conversation->reopen();
112+
// Trigger conversation closed event.
113+
$eventparams = array(
114+
'context' => $context,
115+
'objectid' => $conversation->conversationid
116+
);
117+
$event = \mod_dialogue\event\conversation_reopened::create($eventparams);
118+
$event->trigger();
119+
redirect($returnurl, get_string('conversationreopen', 'dialogue',
120+
$conversation->subject));
121+
}
122+
echo $OUTPUT->header($activityrecord->name);
123+
$pageurl->param('confirm', $conversationid);
124+
$message = get_string('conversationreopenconfirm', 'dialogue', $conversation->subject);
125+
echo $OUTPUT->confirm($message, $pageurl, $returnurl);
126+
echo $OUTPUT->footer();
127+
exit;
128+
}
129+
108130
// Close conversation.
109131
if ($action == 'close') {
110132
if (!empty($confirm) && confirm_sesskey()) {

db/access.php

+24
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,30 @@
127127
)
128128
),
129129

130+
'mod/dialogue:reopen' => array(
131+
132+
'captype' => 'read',
133+
'contextlevel' => CONTEXT_MODULE,
134+
'archetypes' => array(
135+
'student' => CAP_PREVENT,
136+
'teacher' => CAP_ALLOW,
137+
'editingteacher' => CAP_ALLOW,
138+
'manager' => CAP_PREVENT
139+
)
140+
),
141+
142+
'mod/dialogue:reopenany' => array(
143+
144+
'captype' => 'read',
145+
'contextlevel' => CONTEXT_MODULE,
146+
'archetypes' => array(
147+
'student' => CAP_PREVENT,
148+
'teacher' => CAP_ALLOW,
149+
'editingteacher' => CAP_ALLOW,
150+
'manager' => CAP_PREVENT
151+
)
152+
),
153+
130154
'mod/dialogue:receive' => array(
131155

132156
'captype' => 'read',

lang/en/dialogue.php

+8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
$string['cachedef_unreadcounts'] = 'Users unread message counts in conversations';
3636
$string['cachedef_userdetails'] = 'User brief details, all enrolled users';
3737
$string['cannotclosedraftconversation'] = 'You cannot close a conversation that hasn\'t started!';
38+
$string['cannotreopendraftconversation'] = 'You reopen a conversation that hasn\'t started!';
3839
$string['cannotdeleteopenconversation'] = 'You cannot delete a open conversation';
3940
$string['closeconversation'] = 'Close conversation';
4041
$string['closed'] = 'Closed';
@@ -49,6 +50,8 @@
4950
$string['conversationclosed'] = 'Conversation {$a} has been closed';
5051
$string['conversationdeleteconfirm'] = 'Are you sure you want to delete conversation {$a}, this cannot be undone?';
5152
$string['conversationdeleted'] = 'Conversation {$a} has been deleted';
53+
$string['conversationreopenconfirm'] = 'Are you sure you want to reopen conversation {$a} ?';
54+
$string['conversationreopened'] = 'Conversation {$a} has been reopened';
5255
$string['conversationdiscarded'] = 'Conversation discarded';
5356
$string['conversationlistdisplayheader'] = 'Displaying {$a->show} {$a->state} conversations {$a->groupname}';
5457
$string['conversationopened'] = 'Conversation has been opened';
@@ -65,6 +68,7 @@
6568
$string['deletealldrafts'] = 'Delete all drafts';
6669
$string['deleteallrules'] = 'Delete all opener rules';
6770
$string['deleteconversation'] = 'Delete conversation';
71+
$string['reopenconversation'] = 'Reopen conversation';
6872
$string['deletereply'] = 'Delete reply';
6973
$string['dialogue:addinstance'] = 'Add a Dialogue';
7074
$string['dialogue:bulkopenrulecreate'] = 'Create a bulk opener rule';
@@ -74,6 +78,8 @@
7478
$string['dialogue:delete'] = 'Delete own';
7579
$string['dialogue:deleteany'] = 'Delete any';
7680
$string['dialogue:open'] = 'Open a conversation';
81+
$string['dialogue:reopen'] = 'Reopen own';
82+
$string['dialogue:reopenany'] = 'Reopen any';
7783
$string['dialogue:receive'] = 'Receive, who can be the recipient when opening a conversation';
7884
$string['dialogue:reply'] = 'Reply';
7985
$string['dialogue:replyany'] = 'Reply any';
@@ -100,6 +106,7 @@
100106
$string['eventconversationcreated'] = 'Conversation created';
101107
$string['eventconversationdeleted'] = 'Conversation deleted';
102108
$string['eventconversationviewed'] = 'Conversation viewed';
109+
$string['conversationreopen'] = 'Conversation reopened';
103110
$string['eventreplycreated'] = 'Reply created';
104111
$string['everybody'] = 'Everybody (free for all)';
105112
$string['everyone'] = 'Everyone';
@@ -143,6 +150,7 @@
143150
$string['nodraftsfound'] = 'No drafts found!';
144151
$string['nomatchingpeople'] = 'No people match \'{$a}\'';
145152
$string['nopermissiontoclose'] = 'You do not have permission to close this conversation!';
153+
$string['nopermissiontoreopen'] = 'You do not have permission to reopen this conversation!';
146154
$string['nopermissiontodelete'] = 'You do not have permission to delete!';
147155
$string['nosubject'] = '[no subject]';
148156
$string['numberattachments'] = '{$a} attachments';

renderer.php

+16
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,22 @@ public function render_conversation(mod_dialogue\conversation $conversation) {
101101
}
102102
}
103103

104+
if ($conversation->state == \mod_dialogue\dialogue::STATE_CLOSED) {
105+
$canreopen = ((has_capability('mod/dialogue:reopen', $context) && $USER->id == $conversation->author->id) ||
106+
has_capability('mod/dialogue:reopenany', $context));
107+
108+
if ($canreopen) {
109+
$html .= html_writer::start_tag('li');
110+
$trashicon = html_writer::tag('i', '', array('class' => "fa fa-trash-o"));
111+
$reopenurl = new moodle_url('/mod/dialogue/conversation.php');
112+
$reopenurl->param('id', $cm->id);
113+
$reopenurl->param('conversationid', $conversation->conversationid);
114+
$reopenurl->param('action', 'reopen');
115+
$html .= html_writer::link($reopenurl, get_string('reopenconversation', 'dialogue') . $trashicon);
116+
$html .= html_writer::end_tag('li');
117+
}
118+
}
119+
104120
$candelete = ((has_capability('mod/dialogue:delete', $context) && $USER->id == $conversation->author->id) ||
105121
has_capability('mod/dialogue:deleteany', $context));
106122

version.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
*/
2424
defined('MOODLE_INTERNAL') || die();
2525

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

0 commit comments

Comments
 (0)