Commit 3a80228b authored by Nigel McNie's avatar Nigel McNie

Allow people to immediately unsubscribe from forums/topics via e-mail.

The cause of a lot of complaints on mahara.org! Now done properly.

Also, fixed up the keys on the subscription tables. The primary keys were missing. It looks like mahara.org has duplicate data somehow, so the upgrade path cleans that up.

Paid for thanks to Flexible Learning.
parent 43f67d3b
......@@ -53,10 +53,13 @@
<FIELDS>
<FIELD NAME="user" TYPE="int" NOTNULL="true" />
<FIELD NAME="forum" TYPE="int" NOTNULL="true" />
<FIELD NAME="key" TYPE="char" LENGTH="50" NOTNULL="true" />
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="user,forum" />
<KEY NAME="userfk" TYPE="foreign" FIELDS="user" REFTABLE="usr" REFFIELDS="id" />
<KEY NAME="forumfk" TYPE="foreign" FIELDS="forum" REFTABLE="interaction_instance" REFFIELDS="id" />
<KEY NAME="keyuk" TYPE="unique" FIELDS="key" />
</KEYS>
</TABLE>
......@@ -64,10 +67,13 @@
<FIELDS>
<FIELD NAME="user" TYPE="int" NOTNULL="true" />
<FIELD NAME="topic" TYPE="int" NOTNULL="true" />
<FIELD NAME="key" TYPE="char" LENGTH="50" NOTNULL="true" />
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="user,topic" />
<KEY NAME="userfk" TYPE="foreign" FIELDS="user" REFTABLE="usr" REFFIELDS="id" />
<KEY NAME="topicfk" TYPE="foreign" FIELDS="topic" REFTABLE="interaction_forum_topic" REFFIELDS="id" />
<KEY NAME="keyuk" TYPE="unique" FIELDS="key" />
</KEYS>
</TABLE>
......
<?php
/**
* Mahara: Electronic portfolio, weblog, resume builder and social networking
* Copyright (C) 2006-2008 Catalyst IT Ltd (http://www.catalyst.net.nz)
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package mahara
* @subpackage artefact-internal
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006-2008 Catalyst IT Ltd http://catalyst.net.nz
*
*/
defined('INTERNAL') || die();
function xmldb_interaction_forum_upgrade($oldversion=0) {
if ($oldversion < 2009062300) {
foreach (array('topic', 'forum') as $type) {
log_debug("Subscription upgrade for {$type}s");
// Add missing primary key to the subscription tables
// Step 1: remove duplicates
if ($dupes = get_records_sql_array('
SELECT "user", ' . $type . ', COUNT(*)
FROM {interaction_forum_subscription_' . $type . '}
GROUP BY "user", ' . $type . '
HAVING COUNT(*) > 1', array())) {
// We found duplicate subscriptions to a topic/forum
foreach ($dupes as $dupe) {
log_debug("interaction.forum: Removing duplicate $type subscription for {$dupe->user}");
delete_records('interaction_forum_subscription_' . $type, 'user', $dupe->user, $type, $dupe->$type);
insert_record('interaction_forum_subscription_' . $type, (object)array(
'user' => $dupe->user,
$type => $dupe->$type,
));
}
}
// Step 2: add the actual key
$table = new XMLDBTable('interaction_forum_subscription_' . $type);
$key = new XMLDBKey('primary');
$key->setAttributes(XMLDB_KEY_PRIMARY, array('user', $type));
add_key($table, $key);
// Add a 'key' column, used for unsubscriptions
$field = new XMLDBField('key');
$field->setAttributes(XMLDB_TYPE_CHAR, 50, XMLDB_UNSIGNED, null);
add_field($table, $field);
$key = new XMLDBKey('keyuk');
$key->setAttributes(XMLDB_KEY_UNIQUE, array('key'));
add_key($table, $key);
// Populate the key column
if ($records = get_records_array('interaction_forum_subscription_' . $type, '', '', '', '"user", ' . $type)) {
foreach ($records as $where) {
$new = (object)array(
'user' => $where->user,
$type => $where->$type,
'key' => dechex(mt_rand()),
);
update_record('interaction_forum_subscription_' . $type, $new, $where);
}
}
// Now make the key column not null
$field->setAttributes(XMLDB_TYPE_CHAR, 50, XMLDB_UNSIGNED, XMLDB_NOTNULL);
change_field_notnull($table, $field);
}
}
return true;
}
?>
......@@ -125,6 +125,7 @@ $string['topicisclosed'] = 'This topic is closed. Only moderators and the group
$string['topicopenedsuccess'] = 'Topics opened successfully';
$string['topicstickysuccess'] = 'Topics set as sticky successfully';
$string['topicsubscribesuccess'] = 'Topics subscribed successfully';
$string['topicsuccessfulunsubscribe'] = 'Topic unsubscribed successfully';
$string['topicunstickysuccess'] = 'Topic unset as sticky successfully';
$string['topicunsubscribesuccess'] = 'Topics unsubscribed successfully';
$string['topicupdatefailed'] = 'Topics update failed';
......@@ -134,6 +135,9 @@ $string['Unsubscribe'] = 'Unsubscribe';
$string['unsubscribefromforum'] = 'Unsubscribe from forum';
$string['unsubscribefromtopic'] = 'Unsubscribe from topic';
$string['updateselectedtopics'] = 'Update selected topics';
$string['youcannotunsubscribeotherusers'] = 'You cannot unsubscribe other users';
$string['youarenotsubscribedtothisforum'] = 'You are not subscribed to this forum';
$string['youarenotsubscribedtothistopic'] = 'You are not subscribed to this topic';
$string['today'] = 'Today';
$string['yesterday'] = 'Yesterday';
......
......@@ -290,11 +290,11 @@ class PluginInteractionForum extends PluginInteraction {
$currenttime = time();
$minpostdelay = $currenttime - 30 * 60;
$posts = get_records_sql_array(
'SELECT s.subscriber, s.type, p.id
'SELECT s.subscriber, s.type, s.key, p.id
FROM (
SELECT st."user" AS subscriber, st.topic AS topic, \'topic\' AS type
SELECT st."user" AS subscriber, st.topic AS topic, st.key AS key, \'topic\' AS type
FROM {interaction_forum_subscription_topic} st
UNION SELECT sf."user" AS subscriber, t.id AS topic, \'forum\' AS type
UNION SELECT sf."user" AS subscriber, t.id AS topic, sf.key AS key, \'forum\' AS type
FROM {interaction_forum_subscription_forum} sf
INNER JOIN {interaction_forum_topic} t ON t.forum = sf.forum
) s
......@@ -326,6 +326,7 @@ class PluginInteractionForum extends PluginInteraction {
'newpost',
array(
'type' => $post->type,
'key' => $post->key,
'postid' => $post->id,
'users' => $post->users
),
......@@ -404,12 +405,17 @@ class ActivityTypeInteractionForumNewPost extends ActivityTypePlugin {
else {
$user->subject = get_string_from_language($lang, 'newforumpostnotificationsubject', 'interaction.forum', $post->groupname, $post->subject);
}
$unsubscribeid = $post->{$data->type . 'id'};
$user->message = get_string_from_language($lang, 'forumposttemplate', 'interaction.forum',
$post->groupname, $post->forumtitle, $post->subject, display_name($post->poster, $user),
$post->groupname, $post->forumtitle,
$post->subject ? $post->subject : get_string_from_language($lang, 're', 'interaction.forum', $post->topicsubject),
display_name($post->poster, $user),
strftime(get_string('strftimedaydatetime'), $post->ctime), trim(html2text($post->body)),
get_config('wwwroot') . 'interaction/forum/topic.php?id=' . $post->topicid . '#post' . $this->postid,
'topic',
get_config('wwwroot') . 'interaction/forum/unsubscribe.php?topic=' . $post->topicid . '&key=aec9087db'
$data->type,
get_config('wwwroot') . 'interaction/forum/unsubscribe.php?' . $data->type . '=' . $unsubscribeid . '&key=' . $data->key
);
}
}
......@@ -539,7 +545,8 @@ function subscribe_forum_submit(Pieform $form, $values) {
'interaction_forum_subscription_forum',
(object)array(
'forum' => $values['forum'],
'user' => $USER->get('id')
'user' => $USER->get('id'),
'key' => dechex(mt_rand()),
)
);
delete_records_sql(
......
......@@ -218,7 +218,8 @@ function subscribe_topic_submit(Pieform $form, $values) {
'interaction_forum_subscription_topic',
(object)array(
'topic' => $values['topic'],
'user' => $USER->get('id')
'user' => $USER->get('id'),
'key' => dechex(mt_rand()),
)
);
}
......
<?php
/**
* Mahara: Electronic portfolio, weblog, resume builder and social networking
* Copyright (C) 2006-2008 Catalyst IT Ltd (http://www.catalyst.net.nz)
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package mahara
* @subpackage interaction-forum
* @author Catalyst IT Ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006-2008 Catalyst IT Ltd http://catalyst.net.nz
*
*/
define('INTERNAL', 1);
define('PUBLIC', 1);
define('MENUITEM', 'groups/forums');
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
$forum = $topic = 0;
$key = param_alphanum('key', '');
$subscriptiontype = 'forum';
$forum = param_integer('forum', 0);
if (!$forum) {
$subscriptiontype = 'topic';
$topic = param_integer('topic');
}
$goto = $USER->is_logged_in()
? ($subscriptiontype == 'forum') ? '/interaction/forum/view.php?id=' . $forum : '/interaction/forum/topic.php?id=' . $topic
: '/';
if ($key || $USER->is_logged_in()) {
// get record from forum subscriptions for this key
if ($key) {
$subscription = get_record('interaction_forum_subscription_' . $subscriptiontype, 'key', $key);
}
else {
$subscription = get_record('interaction_forum_subscription_' . $subscriptiontype, 'user', $USER->get('id'), $subscriptiontype, $$subscriptiontype);
}
if (!$subscription) {
$SESSION->add_info_msg(get_string('youarenotsubscribedtothis' . $subscriptiontype, 'interaction.forum'));
redirect($goto);
}
if ($USER->is_logged_in() && $subscription->user != $USER->get('id')) {
throw new AccessDeniedException(get_string('youcannotunsubscribeotherusers', 'interaction.forum'));
}
if ($key) {
delete_records('interaction_forum_subscription_' . $subscriptiontype, 'key', $key);
}
else {
delete_records('interaction_forum_subscription_' . $subscriptiontype, 'user', $USER->get('id'), $subscriptiontype, $$subscriptiontype);
}
$SESSION->add_ok_msg(get_string($subscriptiontype . 'successfulunsubscribe', 'interaction.forum'));
redirect($goto);
}
// Not logged in and no key provided
redirect('/');
?>
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