aboutsummaryrefslogtreecommitdiff
path: root/app/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'app/controllers')
-rw-r--r--app/controllers/oer/mymaterial.php344
1 files changed, 190 insertions, 154 deletions
diff --git a/app/controllers/oer/mymaterial.php b/app/controllers/oer/mymaterial.php
index ef45722..4cc0920 100644
--- a/app/controllers/oer/mymaterial.php
+++ b/app/controllers/oer/mymaterial.php
@@ -9,8 +9,7 @@ class Oer_MymaterialController extends AuthenticatedController
parent::before_filter($action, $args);
if (
!Config::get()->OERCAMPUS_ENABLED
- ||
- !$GLOBALS['perm']->have_perm(Config::get()->OER_PUBLIC_STATUS)
+ || !$GLOBALS['perm']->have_perm(Config::get()->OER_PUBLIC_STATUS)
) {
throw new AccessDeniedException();
}
@@ -28,176 +27,199 @@ class Oer_MymaterialController extends AuthenticatedController
public function edit_action(OERMaterial $material = null)
{
+ $this->validateMaterial($material);
+
PageLayout::setTitle($material->isNew() ? _('Neues Material hochladen') : _('Material bearbeiten'));
- if ($material->id && !$material->isMine() && !$GLOBALS['perm']->have_perm('root')) {
- throw new AccessDeniedException();
- }
+
+ $tagsearch = new SQLSearch(
+ "SELECT oer_tags.name, oer_tags.name
+ FROM oer_tags
+ WHERE name LIKE :input
+ ORDER BY oer_tags.name",
+ _('Thema suchen')
+ );
+
+ $this->render_vue_app(
+ Studip\VueApp::create('OERMaterialEditor')
+ ->withProps([
+ 'store-url' => $this->storeURL($material),
+ 'material' => [
+ ...$material->toArray(),
+
+ 'filesize' => $material->getFilePath() && file_exists($material->getFilePath()) ? filesize($material->getFilePath()) : null,
+ 'logoUrl' => $material->getLogoURL(),
+ 'tags' => array_column($material->getTopics(), 'name'),
+ 'users' => $material->users->map(function (OERMaterialUser $user) {
+ if ($user->external_contact) {
+ return [
+ 'id' => $user->oeruser->id,
+ 'name' => $user->oeruser->name,
+ 'avatar' => $user->oeruser->avatar_url,
+ 'external' => true,
+ ];
+ }
+
+ $u = User::find($user->user_id);
+ return [
+ 'id' => $u->user_id,
+ 'avatar' => Avatar::getAvatar($u->id)->getURL(Avatar::SMALL),
+ 'name' => $u ? $u->getFullName() : _('unbekannt'),
+ 'external' => false,
+ ];
+ }),
+ ],
+ 'template' => $_SESSION['NEW_OER'] ?? null,
+ 'tag-search' => (string) $tagsearch,
+ 'licenses-enabled' => !Config::get()->getValue('OER_DISABLE_LICENSE'),
+ 'licenses' => License::findAndMapBySQL(
+ function (License $license) {
+ return [
+ 'id' => $license->id,
+ 'name' => $license->name,
+ ];
+ },
+ '1 ORDER BY name'
+ ),
+ 'enable-twillo' => $this->isTwilloEnabled(),
+ ])
+ );
+
+ }
+
+ public function store_action(OERMaterial $material = null)
+ {
+ $material = $this->validateMaterial($material);
+
+ CSRFProtection::verifyUnsafeRequest();
+
$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()) {
- CSRFProtection::verifyUnsafeRequest();
- $material->pushDataToIndexServers('delete');
- $material->delete();
- PageLayout::postSuccess(_('Das Material wurde gelöscht.'));
- $this->redirect('oer/market/index');
- return;
- } elseif (Request::isPost()) {
- CSRFProtection::verifyUnsafeRequest();
- $was_new = $material->isNew();
- $was_on_twillo = (bool) $material['published_id_on_twillo'];
- $data = Request::getArray('data');
- $material->setData($data);
- if ($data['player_url'] && !$material->hasValidPreviewUrl()) {
- PageLayout::postWarning(_('Die angegebene URL muss mit http(s) beginnen.'));
- $material->player_url = '';
+
+ $was_new = $material->isNew();
+ $was_on_twillo = (bool) $material->published_id_on_twillo;
+ $data = Request::getArray('data');
+ $material->setData($data);
+ if ($data['player_url'] && !$material->hasValidPreviewUrl()) {
+ PageLayout::postWarning(_('Die angegebene URL muss mit http(s) beginnen.'));
+ $material->player_url = '';
+ }
+ $material->host_id = null;
+ if (!empty($_FILES['file']['tmp_name'])) {
+ $material->content_type = get_mime_type($_FILES['file']['name']);
+ 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['host_id'] = null;
- $material['license_identifier'] = Request::get('license', 'CC-BY-SA-4.0');
- if (!empty($_FILES['file']['tmp_name'])) {
- $material['content_type'] = get_mime_type($_FILES['file']['name']);
- 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());
+ $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']) && is_array(getimagesize($_FILES['image']['tmp_name']))) {
- $material['front_image_content_type'] = get_mime_type($_FILES['image']['name']);
- 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
- ]
- );
- }
+ if (
+ !empty($_FILES['image']['tmp_name'])
+ && getimagesize($_FILES['image']['tmp_name']) !== false
+ ) {
+ $material->front_image_content_type = get_mime_type($_FILES['image']['name']);
+ 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();
- //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();
- }
+ if ($was_new) {
+ OERMaterialUser::create([
+ 'material_id' => $material->id,
+ 'user_id' => User::findCurrent()->id,
+ 'external_contact' => false,
+ 'position' => 1,
+ ]);
+ $material->notifyFollowersAboutNewMaterial();
+ }
+ $removed_users = Request::getArray('remove_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]
+ );
+ }
- unset($_SESSION['NEW_OER']);
- PageLayout::postSuccess(_('Lernmaterial erfolgreich gespeichert.'));
+ //Topics:
+ $tags = Request::getArray('tags');
+ $material->setTopics($tags);
- 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')]
- )
+ $material->pushDataToIndexServers();
+
+ if ($this->isTwilloEnabled()) {
+ if (
+ Request::bool('publish_on_twillo')
+ || !empty($_SESSION['NEW_OER']['publish_on_twillo'])
+ ) {
+ //upload it to twillo.de
+ $succes_or_error = $material->uploadToTwillo();
+ if (is_string($succes_or_error)) {
+ PageLayout::postWarning(
+ _('Konnte Material nicht zu twillo.de hochladen.'),
+ [htmlReady($succes_or_error)]
);
- } 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);
+ } elseif ($was_on_twillo) {
+ //remove it from twillo.de if able
+ $material->deleteFromTwillo();
}
+ }
+ unset($_SESSION['NEW_OER']);
+ PageLayout::postSuccess(_('Lernmaterial erfolgreich gespeichert.'));
+ $redirect_url = Request::get('redirect_url');
+ if (!$redirect_url) {
+ $this->redirect('oer/market/details/' . $material->id);
+ } elseif ($redirect_url === 'files') {
+ $this->redirect(
+ URLHelper::getURL(
+ 'dispatch.php/course/files/index/' . Request::get('dir'),
+ ['cid' => Request::get('cid')]
+ )
+ );
+ } else {
+ $this->redirect(URLHelper::getURL($redirect_url, [
+ 'material_id' => $material->id,
+ 'url' => $this->url_for('oer/market/details/' . $material->id)
+ ]));
}
- if (isset($_SESSION['NEW_OER'])) {
- $this->template = $_SESSION['NEW_OER'];
- }
-
- $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();
- }
+ $material = $this->validateMaterial($material);
+
if (Request::isPost()) {
$material->pushDataToIndexServers('delete');
$material->delete();
@@ -211,13 +233,12 @@ class Oer_MymaterialController extends AuthenticatedController
public function statistics_action(OERMaterial $material)
{
+ $material = $this->validateMaterial($material);
+
PageLayout::setTitle(sprintf(
_('Zugriffszahlen für %s'),
$material->name
));
- if (!$GLOBALS['perm']->have_perm('root') && !$material->isMine()) {
- throw new AccessDeniedException();
- }
if (Request::get("export")) {
$this->counter = OERDownloadcounter::findBySQL(
'material_id = ? ORDER BY mkdate DESC',
@@ -299,4 +320,19 @@ class Oer_MymaterialController extends AuthenticatedController
Sidebar::Get()->addWidget($actions);
}
+
+ private function isTwilloEnabled(): bool
+ {
+ return Config::get()->getValue('OERCAMPUS_ENABLE_TWILLO')
+ && TwilloConnector::getTwilloUserID();
+ }
+
+ private function validateMaterial(OERMaterial $material): OERMaterial
+ {
+ if (!$material->isNew() && !$material->isMine() && !$GLOBALS['perm']->have_perm('root')) {
+ throw new AccessDeniedException();
+ }
+
+ return $material;
+ }
}