Browse Source

! This should add the ability to notify users upon a report being made.

Signed-off-by: Peter Spicer <[email protected]>
Peter Spicer 11 years ago
parent
commit
e790748d5f

+ 14 - 73
Sources/SendTopic.php

@@ -531,82 +531,23 @@ function ReportToModerator2()
 			),
 			array('id_comment')
 		);
-	}
-
-	// Find out who the real moderators are - for mod preferences.
-	$request = $smcFunc['db_query']('', '
-		SELECT id_member
-		FROM {db_prefix}moderators
-		WHERE id_board = {int:current_board}',
-		array(
-			'current_board' => $board,
-		)
-	);
-	$real_mods = array();
-	while ($row = $smcFunc['db_fetch_assoc']($request))
-		$real_mods[] = $row['id_member'];
-	$smcFunc['db_free_result']($request);
-
-	// Get any additional members who are in groups assigned to moderate this board
-	$request = $smcFunc['db_query']('', '
-		SELECT mem.id_member
-		FROM {db_prefix}members AS mem, {db_prefix}moderator_groups AS bm
-		WHERE bm.id_board = {int:current_board}
-			AND(
-				mem.id_group = bm.id_group
-				OR FIND_IN_SET(bm.id_group, mem.additional_groups) != 0
-			)',
-		array(
-			'current_board' => $board,
-		)
-	);
-
-	while ($row = $smcFunc['db_fetch_assoc']($request))
-		$real_mods[] = $row['id_member'];
-	$smcFunc['db_free_result']($request);
 
-	// Make sure we don't have any duplicates
-	$real_mods = array_unique($real_mods);
-
-	// Get the mods who want to be notified of stuff...
-	$request = $smcFunc['db_query']('', '
-		SELECT id_member, email_address, lngfile, mod_prefs
-		FROM {db_prefix}members
-		WHERE id_member IN ({array_int:moderator_list})
-			AND notify_types != {int:notify_types}
-		ORDER BY lngfile',
-		array(
-			'moderator_list' => $moderators,
-			'notify_types' => 4,
-		)
-	);
-
-	// Send every moderator an email.
-	while ($row = $smcFunc['db_fetch_assoc']($request))
-	{
-		// Maybe they don't want to know?!
-		if (!empty($row['mod_prefs']))
-		{
-			list(,, $pref_binary) = explode('|', $row['mod_prefs']);
-			if (!($pref_binary & 1) && (!($pref_binary & 2) || !in_array($row['id_member'], $real_mods)))
-				continue;
-		}
-
-		$replacements = array(
-			'TOPICSUBJECT' => $subject,
-			'POSTERNAME' => $poster_name,
-			'REPORTERNAME' => $reporterName,
-			'TOPICLINK' => $scripturl . '?topic=' . $topic . '.msg' . $_POST['msg'] . '#msg' . $_POST['msg'],
-			'REPORTLINK' => !empty($id_report) ? $scripturl . '?action=moderate;area=reports;report=' . $id_report : '',
-			'COMMENT' => $_POST['comment'],
+		// And get ready to notify people.
+		$smcFunc['db_insert']('insert',
+			'{db_prefix}background_tasks',
+			array('task_file' => 'string', 'task_class' => 'string', 'task_data' => 'string', 'claimed_time' => 'int'),
+			array('$sourcedir/tasks/MsgReport-Notify.php', 'MsgReport_Notify_Background', serialize(array(
+				'report_id' => $id_report,
+				'msg_id' => $_POST['msg'],
+				'topic_id' => $message['id_topic'],
+				'board_id' => $message['id_board'],
+				'sender_id' => $context['user']['id'],
+				'sender_name' => $context['user']['name'],
+				'time' => time(),
+			)), 0),
+			array('id_task')
 		);
-
-		$emaildata = loadEmailTemplate('report_to_moderator', $replacements, empty($row['lngfile']) || empty($modSettings['userLanguage']) ? $language : $row['lngfile']);
-
-		// Send it to the moderator.
-		sendmail($row['email_address'], $emaildata['subject'], $emaildata['body'], null, null, false, 2);
 	}
-	$smcFunc['db_free_result']($request);
 
 	// Keep track of when the mod reports get updated, that way we know when we need to look again.
 	updateSettings(array('last_mod_report_action' => time()));

+ 188 - 0
Sources/tasks/MsgReport-Notify.php

@@ -0,0 +1,188 @@
+<?php
+
+/**
+ * This task handles notifying users when a message gets reported.
+ *
+ * Simple Machines Forum (SMF)
+ *
+ * @package SMF
+ * @author Simple Machines http://www.simplemachines.org
+ * @copyright 2013 Simple Machines and individual contributors
+ * @license http://www.simplemachines.org/about/smf/license.php BSD
+ *
+ * @version 2.1 Alpha 1
+ */
+
+class MsgReport_Notify_Background extends SMF_BackgroundTask
+{
+	public function execute()
+	{
+		global $smcFunc, $sourcedir, $modSettings, $language, $scripturl;
+
+		// We need to know who can moderate this board - and therefore who can see this report.
+		// First up, people who have moderate_board in the board this topic was in.
+		require_once($sourcedir . '/Subs-Members.php');
+		$members = membersAllowedTo('moderate_board', $this->_details['board_id']);
+
+		// Second, anyone assigned to be a moderator of this board directly.
+		$request = $smcFunc['db_query']('', '
+			SELECT id_member
+			FROM {db_prefix}moderators
+			WHERE id_board = {int:current_board}',
+			array(
+				'current_board' => $this->_details['board_id'],
+			)
+		);
+		while ($row = $smcFunc['db_fetch_assoc']($request))
+			$members[] = $row['id_member'];
+		$smcFunc['db_free_result']($request);
+
+		// Thirdly, anyone assigned to be a moderator of this group as a group->board moderator.
+		$request = $smcFunc['db_query']('', '
+			SELECT mem.id_member
+			FROM {db_prefix}members AS mem, {db_prefix}moderator_groups AS bm
+			WHERE bm.id_board = {int:current_board}
+				AND(
+					mem.id_group = bm.id_group
+					OR FIND_IN_SET(bm.id_group, mem.additional_groups) != 0
+				)',
+			array(
+				'current_board' => $this->_details['board_id'],
+			)
+		);
+
+		while ($row = $smcFunc['db_fetch_assoc']($request))
+			$members[] = $row['id_member'];
+		$smcFunc['db_free_result']($request);
+
+		// And now weed out the duplicates.
+		$members = array_flip(array_flip($members));
+
+		// Having successfully figured this out, now let's get the preferences of everyone.
+		require_once($sourcedir . '/Subs-Notify.php');
+		$prefs = getNotifyPrefs($members, 'msg_report');
+
+		// Apply defaults if we got some.
+		if (isset($prefs[0]))
+		{
+			foreach ($members as $member)
+				if (!isset($prefs[$member]))
+					$prefs[$member] = $prefs[0];
+
+			unset ($prefs[0]); // Don't need this any more.
+		}
+
+		// So now we find out who wants what.
+		$alert_bits = array(
+			'alert' => 0x01,
+			'email' => 0x02,
+		);
+		$notifies = array();
+
+		foreach ($prefs as $member => $pref_option)
+		{
+			foreach ($alert_bits as $type => $bitvalue)
+				if ($pref_option & $bitvalue == $bitvalue)
+					$notifies[$type][] = $member;
+		}
+
+		// Firstly, anyone who wants alerts.
+		if (!empty($notifies['alert']))
+		{
+			// Alerts are relatively easy.
+			$insert_rows = array();
+			foreach ($notifies['alert'] as $member)
+			{
+				$insert_rows[] = array(
+					'alert_time' => $this->_details['time'],
+					'id_member' => $member,
+					'id_member_started' => $this->_details['sender_id'],
+					'member_name' => $this->_details['sender_name'],
+					'content_type' => 'msg',
+					'content_id' => $this->_details['msg_id'],
+					'content_action' => 'report',
+					'is_read' => 0,
+					'extra' => serialize(
+						array(
+							'report_link' => $scripturl . '?action=moderate;area=reports;report=' . $this->_details['report_id'],
+						)
+					),
+				);
+			}
+
+			$smcFunc['db_insert']('insert',
+				'{db_prefix}user_alerts',
+				array('alert_time' => 'int', 'id_member' => 'int', 'id_member_started' => 'int',
+					'member_name' => 'string', 'content_type' => 'string', 'content_id' => 'int',
+					'content_action' => 'string', 'is_read' => 'int', 'extra' => 'string'),
+				$insert_rows,
+				array('id_alert')
+			);
+
+			// And update the count of alerts for those people.
+			updateMemberData($notifies['alert'], array('alerts' => '+'));
+		}
+
+		// Secondly, anyone who wants emails.
+		if (!empty($notifies['email']))
+		{
+			// Emails are a bit complicated. We have to do language stuff.
+			require_once($sourcedir . '/Subs-Post.php');
+			require_once($sourcedir . '/ScheduledTasks.php');
+			loadEssentialThemeData();
+
+			// First, get everyone's language and details.
+			$emails = array();
+			$request = $smcFunc['db_query']('', '
+				SELECT id_member, lngfile, email_address
+				FROM {db_prefix}members
+				WHERE id_member IN ({array_int:members})',
+				array(
+					'members' => $notifies['email'],
+				)
+			);
+			while ($row = $smcFunc['db_fetch_assoc']($request))
+			{
+				if (empty($row['lngfile']))
+					$row['lngfile'] = $language;
+				$emails[$row['lngfile']][$row['id_member']] = $row['email_address'];
+			}
+			$smcFunc['db_free_result']($request);
+
+			// Second, get some details that might be nice for the report email.
+			// We don't bother cluttering up the tasks data for this, when it's really no bother to fetch it.
+			$request = $smcFunc['db_query']('', '
+				SELECT lr.subject, lr.membername, lr.body
+				FROM {db_prefix}log_reported AS lr
+				WHERE id_report = {int:report}',
+				array(
+					'report' => $this->_details['report_id'],
+				)
+			);
+			list ($subject, $poster_name, $comment) = $smcFunc['db_fetch_row']($request);
+			$smcFunc['db_free_result']($request);
+
+			// Third, iterate through each language, load the relevant templates and set up sending.
+			foreach ($emails as $this_lang => $recipients)
+			{
+				$replacements = array(
+					'TOPICSUBJECT' => $subject,
+					'POSTERNAME' => $poster_name,
+					'REPORTERNAME' => $this->_details['sender_name'],
+					'TOPICLINK' => $scripturl . '?topic=' . $this->_details['topic_id'] . '.msg' . $this->_details['msg_id'] . '#msg' . $this->_details['msg_id'],
+					'REPORTLINK' => $scripturl . '?action=moderate;area=reports;report=' . $this->_details['report_id'],
+					'COMMENT' => $comment,
+				);
+
+				$emaildata = loadEmailTemplate('report_to_moderator', $replacements, empty($modSettings['userLanguage']) ? $language : $this_lang);
+
+				// And do the actual sending...
+				foreach ($recipients as $id_member => $email_address)
+					sendmail($email_address, $emaildata['subject'], $emaildata['body'], null, null, false, 2);
+			}
+		}
+
+		// And now we're all done.
+		return true;
+	}
+}

+ 1 - 0
Themes/default/languages/Alerts.english.php

@@ -11,6 +11,7 @@ $txt['alert_settings'] = 'Settings';
 $txt['alerts_no_unread'] = 'No unread alerts.';
 
 $txt['alert_msg_like'] = '{member_link} liked your post {msg_msg}';
+$txt['alert_msg_report'] = '{member_link} <a href="{report_link}">reported a post</a> - {msg_msg}';
 $txt['alert_member_register_standard'] = '{member_link} just signed up';
 $txt['alert_member_register_approval'] = '{member_link} just signed up (account requires approval)';
 $txt['alert_member_register_activation'] = '{member_link} just signed up (account requires activation)';

+ 2 - 1
other/install_2-1_mysql.sql

@@ -2143,7 +2143,8 @@ INSERT INTO {$db_prefix}user_alerts_prefs
 	(id_member, alert_pref, alert_value)
 VALUES (0, 'member_group_request', 1),
 	(0, 'member_register', 1),
-	(0, 'msg_like', 1);
+	(0, 'msg_like', 1),
+	(0, 'msg_report', 1);
 # --------------------------------------------------------
 
 #

+ 1 - 0
other/install_2-1_postgresql.sql

@@ -2696,6 +2696,7 @@ CREATE TABLE {$db_prefix}user_alerts_prefs (
 INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'member_group_request', 1);
 INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'member_register', 1);
 INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'msg_like', 1);
+INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'msg_report', 1);
 # --------------------------------------------------------
 
 #

+ 1 - 0
other/install_2-1_sqlite.sql

@@ -2304,6 +2304,7 @@ CREATE TABLE {$db_prefix}user_alerts_prefs (
 INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'member_group_request', 1);
 INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'member_register', 1);
 INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'msg_like', 1);
+INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'msg_report', 1);
 # --------------------------------------------------------
 
 #

+ 1 - 0
other/install_2-1_sqlite3.sql

@@ -2304,6 +2304,7 @@ CREATE TABLE {$db_prefix}user_alerts_prefs (
 INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'member_group_request', 1);
 INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'member_register', 1);
 INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'msg_like', 1);
+INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'msg_report', 1);
 # --------------------------------------------------------
 
 #

+ 2 - 1
other/upgrade_2-1_mysql.sql

@@ -313,7 +313,8 @@ INSERT INTO {$db_prefix}user_alerts_prefs
 	(id_member, alert_pref, alert_value)
 VALUES (0, 'member_group_request', 1),
 	(0, 'member_register', 1),
-	(0, 'msg_like', 1);
+	(0, 'msg_like', 1),
+	(0, 'msg_report', 1);
 ---#
 
 /******************************************************************************/

+ 1 - 0
other/upgrade_2-1_postgresql.sql

@@ -388,6 +388,7 @@ CREATE TABLE {$db_prefix}user_alerts_prefs (
 INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'member_group_request', 1);
 INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'member_register', 1);
 INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'msg_like', 1);
+INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'msg_report', 1);
 ---#
 
 /******************************************************************************/

+ 1 - 0
other/upgrade_2-1_sqlite.sql

@@ -367,6 +367,7 @@ CREATE TABLE IF NOT EXISTS {$db_prefix}user_alerts_prefs (
 INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'member_group_request', 1);
 INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'member_register', 1);
 INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'msg_like', 1);
+INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'msg_report', 1);
 ---#
 
 /******************************************************************************/