1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
<?php
/**
* ForumAbo.php - Handle abonnements of areas/threads or even the whole forum
*
* 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.
*
* @author Till Glöggler <tgloeggl@uos.de>
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL version 3
* @category Stud.IP
*/
class ForumAbo
{
/**
* add the passed user as a watcher for the passed topic (including all
* current and future childs)
*
* @param string $topic_id
* @param string $user_id
*/
public static function add($topic_id, $user_id = null)
{
if (!$user_id) $user_id = $GLOBALS['user']->id;
$stmt = DBManager::get()->prepare("REPLACE INTO forum_abo_users
(topic_id, user_id) VALUEs (?, ?)");
$stmt->execute([$topic_id, $user_id]);
}
/**
* remove the passed user as a watcher from the passed topic (including all
* current and future childs)
*
* @param string $topic_id
* @param string $user_id
*/
public static function delete($topic_id, $user_id = null)
{
if (!$user_id) $user_id = $GLOBALS['user']->id;
$stmt = DBManager::get()->prepare("DELETE FROM forum_abo_users
WHERE topic_id = ? AND user_id = ?");
$stmt->execute([$topic_id, $user_id]);
}
/**
* check, if the passed user watches the passed topic. If no user_id is passed,
* the currently logged in user is used
*
* @param string $topic_id
* @param string $user_id
*
* @return boolean returns true if user is watching, false otherwise
*/
public static function has($topic_id, $user_id = null)
{
if (!$user_id) $user_id = $GLOBALS['user']->id;
$stmt = DBManager::get()->prepare("SELECT COUNT(*) FROM forum_abo_users
WHERE topic_id = ? AND user_id = ?");
$stmt->execute([$topic_id, $user_id]);
return $stmt->fetchColumn() > 0 ? true : false;
}
/**
* send out the notification messages for the passed topic. The contents
* and a link directly to the topic are added to the message.
*
* @param string $topic_id
*/
public static function notify($topic_id)
{
// send message to all abo-users
$db = DBManager::get();
$messaging = new ForumBulkMail();
// $messaging = new Messaging();
// get all parent topic-ids, to find out which users to notify
$path = ForumEntry::getPathToPosting($topic_id);
// fetch all users to notify, exclude current user
$stmt = $db->prepare("SELECT DISTINCT user_id
FROM forum_abo_users
WHERE topic_id IN (:topic_ids)
AND user_id != :user_id");
$stmt->bindParam(':topic_ids', array_keys($path), StudipPDO::PARAM_ARRAY);
$stmt->bindParam(':user_id', $GLOBALS['user']->id);
$stmt->execute();
// get details for topic
$topic = ForumEntry::getConstraints($topic_id);
$template = $GLOBALS['template_factory']->open('mail/forum_notification');
// notify users
while ($data = $stmt->fetch(PDO::FETCH_ASSOC)) {
$user_id = $data['user_id'];
// don't notify user if view permission is not granted
if (!ForumPerm::has('view', $topic['seminar_id'], $user_id)) {
continue;
}
$user = User::find($user_id);
// check if user wants an email for all or selected messages only
$force_email = false;
if ($messaging->user_wants_email($user_id)) {
$force_email = true;
}
// do not send mails when account is locked or expired
$expiration = UserConfig::get($user->id)->EXPIRATION_DATE;
if ($user->locked || ($expiration > 0 && $expiration < time())) {
$force_email = false;
}
$parent_id = ForumEntry::getParentTopicId($topic['topic_id']);
setTempLanguage($data['user_id']);
$notification = sprintf(_("%s hat einen Beitrag geschrieben"), ($topic['anonymous'] ? _('Anonym') : $topic['author']));
restoreLanguage();
PersonalNotifications::add(
$user_id,
URLHelper::getURL(
'dispatch.php/course/forum/index/index/' . $topic['topic_id'] . '#' . $topic['topic_id'],
['cid' => $topic['seminar_id']],
true
),
$notification,
"forumposting_" . $topic['topic_id'],
Icon::create('forum', 'clickable')
);
if ($force_email) {
$title = implode(' >> ', ForumEntry::getFlatPathToPosting($topic_id));
$subject = _('[Forum]') . ' ' . ($title ?: _('Neuer Beitrag'));
$htmlMessage = $template->render(
compact('user_id', 'topic', 'path')
);
$textMessage = trim(kill_format($htmlMessage));
$userWantsHtml = UserConfig::get($user_id)->MAIL_AS_HTML;
StudipMail::sendMessage(
$user->email,
$subject,
$textMessage,
$userWantsHtml ? $htmlMessage : null
);
}
}
$messaging->bulkSend();
}
/**
* Removes all abos for a given course and user
*
* @param String $course_id Id of the course
* @param String $user_id Id of the user
* @return int number of removed abos
*/
public static function removeForCourseAndUser($course_id, $user_id)
{
$query = "DELETE FROM `forum_abo_users`
WHERE `user_id` = :user_id
AND `topic_id` IN (
SELECT `topic_id`
FROM `forum_entries`
WHERE `seminar_id` = :course_id
)";
$statement = DBManager::get()->prepare($query);
$statement->bindValue(':course_id', $course_id);
$statement->bindValue(':user_id', $user_id);
$statement->execute();
return $statement->rowCount();
}
}
|