aboutsummaryrefslogtreecommitdiff
path: root/lib/models/Courseware/BlockTypes/Blubber.php
blob: 88624a89abc51475efaf8894d592f19dc184ddb5 (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
97
98
99
<?php

namespace Courseware\BlockTypes;

use BlubberThread;
use Course;

/**
 * This class represents the content of a Courseware blubber block.
 *
 * @author  Ron Lucke <lucke@elan-ev.de>
 * @license GPL2 or any later version
 *
 * @since   Stud.IP 5.0
 */
class Blubber extends BlockType
{
    public static function getType(): string
    {
        return 'blubber';
    }

    public static function getTitle(): string
    {
        return _('Blubber');
    }

    public static function getDescription(): string
    {
        return _('Lehrende können eine Konversation starten oder eine bestehende Konversation einbinden.');
    }

    public function initialPayload(): array
    {
        return [
            'thread_id' => '',
        ];
    }

    public static function getJsonSchema(): string
    {
        $schemaFile = __DIR__.'/Blubber.json';
        return file_get_contents($schemaFile);
    }

    public static function getCategories(): array
    {
        return ['interaction'];
    }

    public static function getContentTypes(): array
    {
        return ['text'];
    }

    public static function getFileTypes(): array
    {
        return [];
    }

    public function copyPayload(string $rangeId = ''): array
    {
        $payload = $this->getPayload();
        $threadId = $payload['thread_id'];

        $course = Course::find($rangeId);

        if ( $threadId === '' || $rangeId === '' || !$course) {
            return $this->initialPayload();
        }

        $remoteBlubberThread = \BlubberThread::find($threadId);

        $threadTitle = $remoteBlubberThread['content'];

        $presentBlubberThread = \BlubberThread::findOneBySQL('content = ? AND context_id = ?', array($threadTitle, $rangeId));

        if ($presentBlubberThread !== null) {
            $payload['thread_id'] = $presentBlubberThread['thread_id'];
        } else {
            $user = \User::findCurrent();
            $newBlubberThread = \BlubberThread::create(
                [
                    'context_type' => 'course',
                    'context_id' => $rangeId,
                    'user_id' => $user->id,
                    'external_contact' => 0,
                    'display_class' => null,
                    'visible_in_stream' => 1,
                    'commentable' => 1,
                    'content' => $threadTitle,
                ]
            );
            $payload['thread_id'] = $newBlubberThread['thread_id'];
        }

        return $payload;
    }
}