aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/ForumIssue.php
blob: 8be894f5177d1640bce3edb6b773f89b8d1d94b1 (plain)
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
<?php
/**
 * ForumIssue.php - Manage issues linked to postings
 *
 * 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 ForumIssue
{
    /**
     * Get the id of the topic linked to the issue denoted by the passed id.
     *
     * @param string $issue_id
     * @return string  the id of the linked topic
     */
    static function getThreadIdForIssue($issue_id)
    {
        $stmt = DBManager::get()->prepare("SELECT topic_id FROM forum_entries_issues
            WHERE issue_id = ?");
        $stmt->execute([$issue_id]);

        return ($stmt->fetchColumn());
    }


    /**
     * Get the id of the issue linked to the topic denoted by the passed id.
     * 
     * @param string $topic_id
     * @return string  the id of the linked topic
     */
    static function getIssueIdForThread($topic_id)
    {
        $stmt = DBManager::get()->prepare("SELECT issue_id FROM forum_entries_issues
            WHERE topic_id = ?");
        $stmt->execute([$topic_id]);
        
        return ($stmt->fetchColumn());
    }


    /**
     * Create/Update the linked posting for the passed issue_id
     *
     * @param string $seminar_id
     * @param string $issue_id    issue id to link to
     * @param string $title       (new) title of the posting
     * @param string $content     (new) content of the posting
     */
    static function setThreadForIssue($seminar_id, $issue_id, $title, $content)
    {
        if ($topic_id = self::getThreadIdForIssue($issue_id)) {   // update
            ForumEntry::update($topic_id, $title ?: _('Ohne Titel'), $content);

        } else {                                                  // create
            // make sure the forum is set up properly
            ForumEntry::checkRootEntry($seminar_id);

            $topic_id = md5(uniqid(rand()));

            ForumEntry::insert([
                'topic_id'    => $topic_id,
                'seminar_id'  => $seminar_id,
                'user_id'     => $GLOBALS['user']->id,
                'name'        => $title ?: _('Ohne Titel'),
                'content'     => $content,
                'author'      => get_fullname($GLOBALS['user']->id),
                'author_host' => ($GLOBALS['user']->id == 'nobody') ? getenv('REMOTE_ADDR') : ''
            ], $seminar_id);

            $stmt = DBManager::get()->prepare("INSERT INTO forum_entries_issues
                (issue_id, topic_id) VALUES (?, ?)");
            $stmt->execute([$issue_id, $topic_id]);
        }
    }

    /**
     * Remove the link for the posting denoted by the passed topic_id
     *
     * @param object $notification
     * @param string $topic_id
     */
    static function unlinkIssue($notification, $topic_id)
    {
        $stmt = DBManager::get()->prepare("DELETE FROM forum_entries_issues
            WHERE topic_id = ?");
        $stmt->execute([$topic_id]);
    }
}