-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpeechToTextPlugin.php
More file actions
132 lines (106 loc) · 4.69 KB
/
SpeechToTextPlugin.php
File metadata and controls
132 lines (106 loc) · 4.69 KB
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
<?php
use Slim\Interfaces\RouteCollectorProxyInterface;
use SpeechToTextPlugin\Routing\Router;
use SpeechToTextPlugin\Models\Job;
class SpeechToTextPlugin extends StudIPPlugin implements SystemPlugin, PrivacyPlugin
{
use TranslatablePluginTrait;
private Router $router;
public static function onEnable($pluginId)
{
RolePersistence::assignPluginRoles($pluginId, [7]);
}
public function __construct()
{
parent::__construct();
require_once __DIR__ . '/vendor/autoload.php';
NotificationCenter::on('UserDidDelete', $this->onDeleteUser(...));
NotificationCenter::on('UserDataDidRemove', $this->onRemoveData(...));
$this->initializeTranslation('speech-to-text');
$this->router = new Router($this);
if (UpdateInformation::isCollecting()) {
$this->setUpdateInformation();
} else {
$this->addContentsNavigation();
}
}
public function registerSlimRoutes(RouteCollectorProxyInterface $app, string $unconsumedPath): void
{
$this->router->registerRoutes($app, $unconsumedPath);
}
private function addContentsNavigation(): void
{
if (Navigation::hasItem('/contents')) {
Navigation::addItem('/contents/speech-to-text', $this->createNavigation());
}
}
private function createNavigation(?string $cid = null): Navigation
{
$params = $cid ? ['cid' => $cid] : [];
$navigation = new Navigation($this->_('Transkriptionen'));
$navigation->setDescription('Automatisch Sprache in Text verwandeln');
$navigation->setImage(Icon::create('wizard', 'navigation'));
$navigation->setURL(PluginEngine::getURL($this, $params, '', true));
// subnavigation
$navigation->addSubnavigation('index', clone $navigation);
return $navigation;
}
private function setUpdateInformation(): void
{
if (UpdateInformation::hasData(self::class)) {
$since = new DateTime(UpdateInformation::getData(self::class)['since']);
if ($since) {
$latest = Job::latest(User::findCurrent());
if ($latest && new DateTime('@' . $latest->chdate) > $since) {
UpdateInformation::setInformation(self::class, 'reload');
}
}
}
}
/**
* Export available data of a given user into a storage object
* (an instance of the StoredUserData class) for that user.
*
* @param StoredUserData $storage object to store data into
*/
public function exportUserData(StoredUserData $storage)
{
$db = \DBManager::get();
$tableData = $db->fetchAll('SELECT * FROM speech_to_text_jobs WHERE user_id = ?', [$storage->user_id]);
$storage->addTabularData('Audiotranskriptionen', 'speech_to_text_jobs', $tableData);
$folderIds = $db->fetchFirst('SELECT id FROM folders WHERE user_id = ? AND range_type = ?', [$storage->user_id, \SpeechToTextPlugin\Models\Job::class]);
$fileRefIds = $db->fetchFirst('SELECT id FROM file_refs WHERE folder_id IN (?)', [$folderIds]);
foreach (\FileRef::findMany($fileRefIds) as $fileRef) {
$storage->addFileRef($fileRef);
}
}
private function onDeleteUser($event, $user)
{
$this->deleteUserData($user->id);
}
private function onRemoveData($event, $userId, $type)
{
$this->deleteUserData($userId);
$info = match ($type) {
'course_documents', 'personal_documents' => 'Dokumente von Nutzer X aus MyPlugin gelöscht',
'course_contents', 'personal_contents' => 'Inhalte von Nutzer X aus MyPlugin gelöscht',
'names' => 'Namen von Nutzer X aus MyPlugin gelöscht',
default => null,
};
if ($info) {
\PageLayout::postInfo($info);
}
}
private function deleteUserData(string $userId): void
{
$db = \DBManager::get();
$folderIds = $db->fetchFirst('SELECT id FROM folders WHERE user_id = ? AND range_type = ?', [$userId, \SpeechToTextPlugin\Models\Job::class]);
$fileRefIds = $db->fetchFirst('SELECT id FROM file_refs WHERE folder_id IN (?)', [$folderIds]);
$fileIds = $db->fetchFirst('SELECT file_id FROM file_refs WHERE folder_id IN (?)', [$folderIds]);
$db->execute('DELETE FROM files WHERE id IN (?)', [$fileIds]);
$db->execute('DELETE FROM file_refs WHERE id IN (?)', [$fileRefIds]);
$db->execute('DELETE FROM folders WHERE id IN (?)', [$folderIds]);
$db->execute('DELETE FROM speech_to_text_jobs WHERE user_id = ?', [$userId]);
$db->execute('DELETE FROM speech_to_text_user_uploads WHERE user_id = ?', [$userId]);
}
}