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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
<?php
class Oer_MymaterialController extends AuthenticatedController
{
protected $_autobind = true;
public function before_filter(&$action, &$args)
{
parent::before_filter($action, $args);
PageLayout::setTitle(_('Lernmaterialien'));
}
public function index_action()
{
if (Navigation::hasItem("/oer/mymaterial")) {
Navigation::activateItem("/oer/mymaterial");
}
$this->materialien = OERMaterial::findMine();
$this->buildSidebar();
}
public function edit_action(OERMaterial $material = null)
{
PageLayout::setTitle($material->isNew() ? _('Neues Material hochladen') : _('Material bearbeiten'));
if ($material->id && !$material->isMine() && !$GLOBALS['perm']->have_perm('root')) {
throw new AccessDeniedException();
}
$content_types = ['application/x-zip-compressed', 'application/zip', 'application/x-zip'];
$tmp_folder = $GLOBALS['TMP_PATH'] . '/temp_folder_' . md5(uniqid());
if (Request::submitted('delete') && Request::isPost()) {
$material->pushDataToIndexServers('delete');
$material->delete();
PageLayout::postSuccess(_('Das Material wurde gelöscht.'));
$this->redirect('oer/market/index');
return;
} elseif (Request::isPost()) {
$was_new = $material->isNew();
$was_on_twillo = (bool) $material['published_id_on_twillo'];
$material->setData(Request::getArray('data'));
$material['host_id'] = null;
$material['license_identifier'] = Request::get('license', 'CC-BY-SA-4.0');
if (!empty($_FILES['file']['tmp_name'])) {
$material['content_type'] = $_FILES['file']['type'];
if (in_array($material['content_type'], $content_types)) {
mkdir($tmp_folder);
\Studip\ZipArchive::extractToPath($_FILES['file']['tmp_name'], $tmp_folder);
$material['structure'] = $this->getFolderStructure($tmp_folder);
rmdirr($tmp_folder);
} else {
$material['structure'] = null;
}
$material['filename'] = $_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], $material->getFilePath());
} elseif (!empty($_SESSION['NEW_OER']['tmp_name'])) {
$material['content_type'] = $_SESSION['NEW_OER']['content_type'] ?: get_mime_type($_SESSION['NEW_OER']['tmp_name']);
if (in_array($material['content_type'], $content_types)) {
mkdir($tmp_folder);
\Studip\ZipArchive::extractToPath($_SESSION['NEW_OER']['tmp_name'], $tmp_folder);
$material['structure'] = $this->getFolderStructure($tmp_folder);
rmdirr($tmp_folder);
} else {
$material['structure'] = null;
}
$material['filename'] = $_SESSION['NEW_OER']['filename'];
copy($_SESSION['NEW_OER']['tmp_name'], $material->getFilePath());
}
if (!empty($_FILES['image']['tmp_name'])) {
$material['front_image_content_type'] = $_FILES['image']['type'];
move_uploaded_file($_FILES['image']['tmp_name'], $material->getFrontImageFilePath());
} elseif (!empty($_SESSION['NEW_OER']['image_tmp_name'])) {
$material['front_image_content_type'] = get_mime_type($_SESSION['NEW_OER']['image_tmp_name']);
copy($_SESSION['NEW_OER']['image_tmp_name'], $material->getFrontImageFilePath());
}
if (Request::get('delete_front_image')) {
$material['front_image_content_type'] = null;
}
if ($material->isNew() && $material['category'] === 'auto') {
$material['category'] = $material->autoDetectCategory();
}
$material->store();
if ($was_new) {
OERMaterialUser::create(
[
'material_id' => $material->getId(),
'user_id' => $GLOBALS['user']->id,
'external_contact' => 0,
'position' => 1
]
);
$material->notifyFollowersAboutNewMaterial();
}
$removed_users = Request::getArray('remove_users');
if (!empty($removed_users)) {
foreach (Request::getArray('remove_users') as $index => $user) {
if (!$index && count($removed_users) === count($material->users)) {
continue;
}
[$external, $user_id] = array_map('trim', explode('_', $user));
OERMaterialUser::deleteBySQL(
'user_id = ? AND material_id = ? AND external_contact = ?',
[$user_id, $material->getId(), $external]
);
}
}
if (Request::get('new_user')) {
[$external, $user_id] = array_map('trim', explode('_', Request::get('new_user')));
OERMaterialUser::create(
[
'material_id' => $material->getId(),
'user_id' => $user_id,
'external_contact' => $external,
'position' => count($material->users) + 1
]
);
}
//Topics:
$tags = Request::getArray('tags');
if (!empty($tags)) {
foreach ($tags as $key => $tag) {
if (!trim($tag)) {
unset($tags[$key]);
}
}
}
$material->setTopics($tags);
$material->pushDataToIndexServers();
if (Config::get()->OERCAMPUS_ENABLE_TWILLO && TwilloConnector::getTwilloUserID()) {
if (Request::bool('publish_on_twillo') || $_SESSION['NEW_OER']['publish_on_twillo']) {
//upload it to twillo.de
$succes_or_error = $material->uploadToTwillo($material);
if (is_string($succes_or_error)) {
PageLayout::postWarning(_('Konnte Material nicht zu twillo.de hochladen.'), [$succes_or_error]);
}
} elseif ($was_on_twillo) {
//remove it from twillo.de if able
$material->deleteFromTwillo();
}
}
unset($_SESSION['NEW_OER']);
PageLayout::postSuccess(_('Lernmaterial erfolgreich gespeichert.'));
if (Request::get('redirect_url')) {
if (Request::get('redirect_url') === 'files') {
$this->redirect(
URLHelper::getURL(
'dispatch.php/course/files/index/' . Request::get('dir'),
['cid' => Request::get('cid')]
)
);
} else {
$this->redirect(URLHelper::getURL(Request::get('redirect_url'), [
'material_id' => $material->getId(),
'url' => $this->url_for('oer/market/details/' . $material->id)
]));
}
} else {
$this->redirect('oer/market/details/' . $material->id);
}
}
if (isset($_SESSION['NEW_OER'])) {
$this->template = $_SESSION['NEW_OER'];
}
$this->usersearch = new SQLSearch("
SELECT DISTINCT CONCAT('0_', auth_user_md5.user_id), CONCAT(auth_user_md5.Nachname, ', ', auth_user_md5.Vorname, ' (', auth_user_md5.username, ')')
FROM auth_user_md5 LEFT JOIN user_info ON (user_info.user_id = auth_user_md5.user_id)
WHERE (CONCAT(auth_user_md5.Vorname, ' ', auth_user_md5.Nachname) LIKE REPLACE(:input, ' ', '% ')
OR CONCAT(auth_user_md5.Nachname, ' ', auth_user_md5.Vorname) LIKE REPLACE(:input, ' ', '% ')
OR CONCAT(auth_user_md5.Nachname, ', ', auth_user_md5.Vorname) LIKE :input
OR auth_user_md5.username LIKE :input) AND " . get_vis_query() . "
UNION SELECT CONCAT('1_', oer_user.user_id), oer_user.name
FROM oer_user
WHERE name LIKE :input
", _('Person hinzufügen'), 'user_id');
$this->tagsearch = new SQLSearch("
SELECT oer_tags.name, oer_tags.name
FROM oer_tags
WHERE name LIKE :input
ORDER BY oer_tags.name
", _("Thema suchen"));
}
public function delete_action(OERMaterial $material)
{
if ($material->id && !$material->isMine() && !$GLOBALS['perm']->have_perm('root')) {
throw new AccessDeniedException();
}
if (Request::isPost()) {
$material->pushDataToIndexServers('delete');
$material->delete();
PageLayout::postSuccess(_('Das Material wurde gelöscht.'));
$this->redirect('oer/market/index');
return;
} else {
throw new Exception("Use this route with POST.");
}
}
public function statistics_action(OERMaterial $material)
{
PageLayout::setTitle(sprintf(
_('Zugriffszahlen für %s'),
$material->name
));
if (!$GLOBALS['perm']->have_perm('root') && $material->user_id && $material->user_id !== $GLOBALS['user']->id) {
throw new AccessDeniedException();
}
if (Request::get("export")) {
$this->counter = OERDownloadcounter::findBySQL(
'material_id = ? ORDER BY mkdate DESC',
[$material->id]
);
$output = [
['Datum', 'Longitude', 'Latitude']
];
foreach ($this->counter as $counter) {
$output[] = [
date('Y-m-d H:i:s', $counter['mkdate']),
$counter['longitude'],
$counter['latitude']
];
}
$this->render_csv($output, FileManager::cleanFileName('Zugriffszahlen ' . $material->name . '.csv'));
return;
}
$this->counter = OERDownloadcounter::countBySQL("material_id = ?", [$material->id]);
$this->counter_today = OERDownloadcounter::countBySQL("material_id = :material_id AND mkdate >= :start", [
'material_id' => $material->id,
'start' => mktime(0, 0, 0)
]);
}
public function show_tmp_image_action()
{
if ($_SESSION['NEW_OER']['image_tmp_name'] && file_exists($_SESSION['NEW_OER']['image_tmp_name'])) {
$this->render_file(
$_SESSION['NEW_OER']['image_tmp_name'],
null,
null,
'inline'
);
} else {
throw new Exception(_("Datei ist nicht vorhanden"));
}
}
protected function getFolderStructure($folder)
{
$structure = [];
foreach (scandir($folder) as $file) {
if (!in_array($file, [".", ".."])) {
$attributes = [
'is_folder' => is_dir($folder . "/" . $file) ? 1 : 0
];
if (is_dir($folder . "/" . $file)) {
$attributes['structure'] = $this->getFolderStructure($folder . "/" . $file);
} else {
$attributes['size'] = filesize($folder . "/" . $file);
}
$structure[$file] = $attributes;
}
}
return $structure;
}
public function add_tag_action()
{
if (!Request::isPost()) {
throw new AccessDeniedException();
}
$this->material = new OERMaterial(Request::option('material_id'));
$this->render_nothing();
}
private function buildSidebar()
{
$actions = new ActionsWidget();
$actions->addLink(
_('Neues Lernmaterial hochladen'),
$this->editURL(),
Icon::create('add'),
['data-dialog' => 'size=auto']
);
Sidebar::Get()->addWidget($actions);
}
}
|