From b0e3d82e9d6ebc789bf2ad394bcc4de180767625 Mon Sep 17 00:00:00 2001
From: David Siegfried <david.siegfried@uni-vechta.de>
Date: Thu, 15 Dec 2022 11:16:47 +0000
Subject: [PATCH] move code to controller, closes #806

Closes #806

Merge request studip/studip!1243
---
 app/controllers/oer/market.php | 41 +++++++++++++++++++++++-
 app/views/oer/market/index.php | 58 +++++++---------------------------
 2 files changed, 52 insertions(+), 47 deletions(-)

diff --git a/app/controllers/oer/market.php b/app/controllers/oer/market.php
index 4071d07b5ff..fadc7b60aa5 100644
--- a/app/controllers/oer/market.php
+++ b/app/controllers/oer/market.php
@@ -25,7 +25,13 @@ class Oer_MarketController extends StudipController
         }
         $tag_matrix_entries_number = 9;
         $this->best_nine_tags = OERTag::findBest($tag_matrix_entries_number);
-
+        $this->tags = [];
+        foreach ($this->best_nine_tags as $tag) {
+            $this->tags[] = [
+                'tag_hash' => $tag->tag_hash,
+                'name'     => $tag->name,
+            ];
+        }
         if (Request::get("tag")) {
             $this->materialien = OERMaterial::findByTag(Request::get("tag"));
         }
@@ -35,6 +41,22 @@ class Oer_MarketController extends StudipController
         if (Request::get("get") === "all") {
             $this->materialien = OERMaterial::findBySQL("1 ORDER BY oer_material.mkdate DESC");
         }
+
+        $this->material_data = false;
+        if (!empty($this->materialien)) {
+            $this->material_data = [];
+            foreach ($this->materialien as $material) {
+                $data = $material->toRawArray();
+
+                $data['tags'] = array_map(function($tag) {
+                    return $tag['name'];
+                }, $material->getTopics());
+
+                $data['logo_url'] = $material->getLogoURL();
+                $data['download_url'] = $material->getDownloadUrl();
+                $this->material_data[] = $data;
+            }
+        }
         $this->new_ones = OERMaterial::findBySQL("LEFT JOIN oer_hosts ON (oer_hosts.host_id = oer_material.host_id)
             WHERE draft = '0'
                 AND (oer_material.host_id IS NULL OR oer_hosts.`active` = '1')
@@ -48,6 +70,23 @@ class Oer_MarketController extends StudipController
         ");
         $statement->execute([$GLOBALS['user']->id]);
         $this->abo = (bool) $statement->fetch(PDO::FETCH_COLUMN, 0);
+
+        if ($GLOBALS['perm']->have_perm('autor')) {
+            $actions = new ActionsWidget();
+            $actions->addLink(
+                _('Neues Lernmaterial hochladen'),
+                $this->url_for('oer/mymaterial/edit'),
+                Icon::create('add'),
+                ['data-dialog' => 'size=auto']
+            );
+            $actions->addLink(
+                $this->abo ? _('Neuigkeiten abbestellen') : _('Neuigkeiten abonnieren'),
+                $this->aboURL(),
+                Icon::create($this->abo ? 'decline' : 'rss'),
+                ['data-dialog' => 'size=auto']
+            );
+            Sidebar::Get()->addWidget($actions);
+        }
     }
 
     public function get_tags_action()
diff --git a/app/views/oer/market/index.php b/app/views/oer/market/index.php
index 643c0ee5760..66e1f2c4785 100644
--- a/app/views/oer/market/index.php
+++ b/app/views/oer/market/index.php
@@ -1,38 +1,20 @@
-<?
-$tags = [];
-if ($best_nine_tags && count($best_nine_tags) > 0) {
-    foreach ($best_nine_tags as $tag) {
-        $tags[] = [
-            'tag_hash' => $tag['tag_hash'],
-            'name' => $tag['name']
-        ];
-    }
-}
-$material_data = false;
-if (!empty($materialien)) {
-    $material_data = [];
-    foreach ($materialien as $material) {
-        $data = $material->toRawArray();
-
-        $data['tags'] = array_map(function($tag) {
-            return $tag['name'];
-        }, $material->getTopics());
-
-        $data['logo_url'] = $material->getLogoURL();
-        $data['download_url'] = $material->getDownloadUrl();
-        $material_data[] = $data;
-    }
-}
+<?php
+/**
+ * @var Oer_MarketController $controller
+ * @var array $material_data
+ * @var array $tags
+ * @var OERMaterial[] $new_ones
+ */
 ?>
 <form class="oer_search"
-      action="<?= $controller->link_for("oer/market/search") ?>"
+      action="<?= $controller->search() ?>"
       method="GET" aria-live="polite"
       data-searchresults="<?= htmlReady(json_encode($material_data)) ?>"
-      data-filteredtag="<?= htmlReady(Request::get("tag")) ?>"
-      data-filteredcategory="<?= htmlReady(Request::get("category")) ?>"
+      data-filteredtag="<?= htmlReady(Request::get('tag')) ?>"
+      data-filteredcategory="<?= htmlReady(Request::get('category')) ?>"
       data-tags="<?= htmlReady(json_encode($tags)) ?>"
-      data-material_select_url_template="<?= htmlReady($controller->url_for('oer/market/details/__material_id__')) ?>">
-    <?= $this->render_partial("oer/market/_searchform") ?>
+      data-material_select_url_template="<?= htmlReady($controller->detailsURL('__material_id__')) ?>">
+    <?= $this->render_partial('oer/market/_searchform') ?>
 </form>
 
 
@@ -51,19 +33,3 @@ if (!empty($materialien)) {
 
 
 <?
-if ($GLOBALS['perm']->have_perm("autor")) {
-    $actions = new ActionsWidget();
-    $actions->addLink(
-        _('Neues Lernmaterial hochladen'),
-        $controller->url_for("oer/mymaterial/edit"),
-        Icon::create("add", Icon::ROLE_CLICKABLE),
-        ['data-dialog' => "1"]
-    );
-    $actions->addLink(
-        $abo ? _('Neuigkeiten abbestellen') : _('Neuigkeiten abonnieren'),
-        $controller->url_for("oer/market/abo"),
-        $abo ? Icon::create("decline", Icon::ROLE_CLICKABLE) : Icon::create("rss", Icon::ROLE_CLICKABLE),
-        ['data-dialog' => "size=small"]
-    );
-    Sidebar::Get()->addWidget($actions);
-}
-- 
GitLab