Commit 69c675b3 authored by Cecilia Vela Gurovic's avatar Cecilia Vela Gurovic Committed by Robert Lyon

Bug 1805065: Allow user to unsubscribe forum setting

behatnotneeded

Change-Id: I0efe8ec1eacf9211f18c777286fbd1dc1742a0af
parent 7eb46ad6
......@@ -56,6 +56,7 @@ if ($forums) {
$i = 0;
foreach ($forums as $forum) {
$forum->feedlink = get_config('wwwroot') . 'interaction/forum/atom.php?type=f&id=' . $forum->id;
$allowunsubscribe = get_config_plugin_instance('interaction_forum', $forum->id, 'allowunsubscribe');
if ($membership) {
$forum->subscribe = pieform(array(
......@@ -71,7 +72,7 @@ if ($forums) {
'submit' => array(
'type' => 'button',
'usebuttontag' => true,
'class' => 'btn-default btn-sm',
'class' => 'btn-default btn-sm' . (isset($allowunsubscribe) && $allowunsubscribe == 0 ? ' disabled' : ''),
'renderelementsonly' => true,
'value' => $forum->subscribed ? '<span class="icon icon-lg icon-times left text-danger" role="presentation" aria-hidden="true"></span> ' . get_string('Unsubscribe', 'interaction.forum') : '<span class="icon icon-lg icon-star left" role="presentation" aria-hidden="true"></span> ' . get_string('Subscribe', 'interaction.forum'),
'help' => false
......
......@@ -15,6 +15,8 @@ $string['addpostsuccess'] = 'Added post successfully';
$string['addtitle'] = 'Add forum';
$string['addtopic'] = 'Add topic';
$string['addtopicsuccess'] = 'Added topic successfully';
$string['allowunsubscribe'] = 'Allow users to unsubscribe';
$string['allowunsubscribedescription'] = 'Choose whether users are allowed to unsubscribe from forums and topics';
$string['autosubscribeusers'] = 'Automatically subscribe users';
$string['autosubscribeusersdescription'] = 'Choose whether group users will automatically be subscribed to this forum';
$string['Body'] = 'Message';
......@@ -32,6 +34,7 @@ $string['cantfindtopic'] = 'Could not find topic with id %s';
$string['cantmakenonobjectionable'] = 'You are not allowed to mark this post as not objectionable.';
$string['cantviewforums'] = 'You are not allowed to view forums in this group';
$string['cantviewtopic'] = 'You are not allowed to view topics in this forum';
$string['cantunsubscribe'] = 'You are not allowed to unsubscribe from the forum';
$string['chooseanaction'] = 'Choose an action';
$string['clicksetsubject'] = 'Click to set a subject';
$string['Closed'] = 'Closed';
......
......@@ -20,12 +20,14 @@ define('DELETE_OBJECTIONABLE_TOPIC', 4);
class PluginInteractionForum extends PluginInteraction {
public static function instance_config_form($group, $instance=null) {
global $USER;
if (isset($instance)) {
$instanceconfig = get_records_assoc('interaction_forum_instance_config', 'forum', $instance->get('id'), '', 'field,value');
$autosubscribe = isset($instanceconfig['autosubscribe']) ? $instanceconfig['autosubscribe']->value : false;
$weight = isset($instanceconfig['weight']) ? $instanceconfig['weight']->value : null;
$createtopicusers = isset($instanceconfig['createtopicusers']) ? $instanceconfig['createtopicusers']->value : null;
$closetopics = !empty($instanceconfig['closetopics']);
$allowunsubscribe = isset($instanceconfig['allowunsubscribe']) ? $instanceconfig['allowunsubscribe']->value : null;
$indentmode = isset($instanceconfig['indentmode']) ? $instanceconfig['indentmode']->value : null;
$maxindent = isset($instanceconfig['maxindent']) ? $instanceconfig['maxindent']->value : null;
......@@ -57,7 +59,7 @@ class PluginInteractionForum extends PluginInteraction {
$existing = array();
}
return array(
$form = array(
'indentmode' => array(
'type' => 'select',
'title' => get_string('indentmode', 'interaction.forum'),
......@@ -138,6 +140,17 @@ class PluginInteractionForum extends PluginInteraction {
)
)
);
if ($USER->get('admin') || $USER->get('staff') || $USER->is_institutional_admin() || $USER->is_institutional_staff()) {
$form['fieldset']['elements']['allowunsubscribe'] = array(
'type' => 'switchbox',
'title' => get_string('allowunsubscribe', 'interaction.forum'),
'description' => get_string('allowunsubscribedescription', 'interaction.forum'),
'defaultvalue' => isset($allowunsubscribe) ? $allowunsubscribe : 1,
);
}
return $form;
}
public static function instance_config_js() {
......@@ -166,6 +179,7 @@ EOF;
}
public static function instance_config_save($instance, $values){
global $USER;
db_begin();
// Autosubscribe
......@@ -277,6 +291,23 @@ EOF;
));
}
// Allow users to unsubscribe
if ($USER->get('admin') || $USER->get('staff') || $USER->is_institutional_admin() || $USER->is_institutional_staff()) {
if (isset($values['allowunsubscribe'])) {
ensure_record_exists('interaction_forum_instance_config',
(object) array(
'forum'=> $instance->get('id'),
'field'=> 'allowunsubscribe'
),
(object) array(
'forum'=> $instance->get('id'),
'field'=> 'allowunsubscribe',
'value'=> $values['allowunsubscribe']
)
);
}
}
//Indent mode
delete_records_sql(
"DELETE FROM {interaction_forum_instance_config}
......@@ -1315,10 +1346,16 @@ function subscribe_forum_validate(Pieform $form, $values) {
if (!is_logged_in()) {
throw new AccessDeniedException();
}
$allowunsubscribe = get_config_plugin_instance('interaction_forum', $values['forum'], 'allowunsubscribe');
if (isset($allowunsubscribe) && $allowunsubscribe == 0) {
throw new AccessDeniedException(get_string('cantunsubscribe', 'interaction.forum'));
}
}
function subscribe_forum_submit(Pieform $form, $values) {
global $USER, $SESSION;
if ($values['type'] == 'subscribe') {
db_begin();
insert_record(
......
......@@ -170,7 +170,9 @@ if ($membership && param_exists('checked')) {
redirect('/interaction/forum/view.php?id=' . $forumid . '&offset=' . $offset);
}
if ($membership) {
$allowunsubscribe = get_config_plugin_instance('interaction_forum', $forum->id, 'allowunsubscribe');
if ($membership && ( !isset($allowunsubscribe) || $allowunsubscribe == 1)) {
$forum->subscribe = pieform(array(
'name' => 'subscribe_forum',
'renderer' => 'div',
......
......@@ -1170,7 +1170,9 @@ function get_config_plugin_instance($plugintype, $instanceid, $key) {
return $value;
}
$records = get_records_array($plugintype . '_instance_config', 'instance', $instanceid, 'field', 'field, value');
$instancefield = $plugintype == 'interaction_forum' ? 'forum' : 'instance';
$records = get_records_array($plugintype . '_instance_config', $instancefield, $instanceid, 'field', 'field, value');
if (!empty($records)) {
foreach($records as $record) {
$storeconfigname = "plugin_{$plugintype}_{$instance}_{$record->field}";
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment