aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/oer/addfile.php
blob: acd57d763e06cad2986b4be19170cd9586d8ac4b (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
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
<?php

class Oer_AddfileController extends AuthenticatedController
{
    public function choose_file_action()
    {
        if (Request::get('to_plugin')) {
            $to_plugin = PluginManager::getInstance()->getPlugin(Request::get('to_plugin'));
            $this->to_folder_type = $to_plugin->getFolder(Request::get('to_folder_id', ''));
        } else {
            if (!Request::get('to_folder_id')) {
                throw new Exception('target folder_id must be set.');
            }
            $folder = new Folder(Request::option('to_folder_id', ''));
            $this->to_folder_type = $folder->getTypedFolder();
        }

        if (Request::option("material_id")) {
            $material = OERMaterial::find(Request::option("material_id"));
            $uploaded_file = [
                'name' => $material['name'],
                'type' => $material['content_type'],
                'content_terms_of_use_id' => "FREE_LICENSE",
                'description' => $material['description']
            ];
            $url = $material->getDownloadUrl();
            if ($url) {
                if ($material['host_id']) {
                    $tmp_name = $GLOBALS['TMP_PATH'] . '/oer_' . $material->getId();
                    file_put_contents($tmp_name, file_get_contents($url, false, get_default_http_stream_context($url)));
                    $uploaded_file['tmp_name'] = $tmp_name;
                    $uploaded_file['size'] = filesize($tmp_name);
                } else {
                    $uploaded_file['tmp_name'] = $material->getFilePath();
                    $uploaded_file['size'] = filesize($material->getFilePath());
                }

                $standardfile = StandardFile::create($uploaded_file);
            } elseif($material['source_url']) {
                $standardfile = URLFile::create([
                    'url' => $material['source_url'],
                    'name' => $material['name'],
                    'author_name' => implode(', ', array_map(function ($a) { return $a['name']; }, $material)),
                    'description' => $material['description'],
                    'content_terms_of_use_id' => "FREE_LICENSE"
                ]);
            }

            if ($standardfile->getSize()) {
                $error = $this->to_folder_type->validateUpload($standardfile, User::findCurrent()->id);
                if ($error && is_string($error)) {
                    if ($tmp_name) {
                        @unlink($tmp_name);
                    }
                    return [$error];
                }

                $newfile = $this->to_folder_type->addFile($standardfile);
                if ($tmp_name) {
                    @unlink($tmp_name);
                }
                if (!$newfile) {
                    PageLayout::postError(_('Daten konnten nicht kopiert werden!'));
                } else {
                    PageLayout::postSuccess(_('Datei wurde hinzugefügt.'));
                }
            } else {
                if ($tmp_name) {
                    @unlink($tmp_name);
                }
                PageLayout::postError(_('Daten konnten nicht kopiert werden!'));
            }
            return $this->redirectToFolder($this->to_folder_type);
        }

        $tag_matrix_entries_number = 9;
        $this->best_nine_tags = OERTag::findBest($tag_matrix_entries_number);


        //Load the folder by its ID.
        $folder = new Folder();
        $folder_type = $folder->folder_type;
        //Check if the specified folder type is a FolderType implementation.
        if (is_a($folder_type, 'FolderType', true)) {
            //Get an instance of the FolderType implementation
            //and use it in the code below this point.
            $this->top_folder = new $folder_type($folder);
            if (!$this->top_folder->isReadable($GLOBALS['user']->id)) {
                throw new AccessDeniedException();
            }
        }

        $this->to_folder_name = _('Hauptordner');

        //A top folder can have its parent-ID set to an empty string
        //or its folder_type set to 'RootFolder'.
        if ($this->to_folder_type->parent_id == ''
            or $this->to_folder_type->folder_type == 'RootFolder') {
            //We have a top folder. Now we check if its range-ID
            //references a Stud.IP object and set the displayed folder name
            //to the name of that object.
            if ($this->to_folder_type->range_id) {
                $range_type = Folder::findRangeTypeById($this->to_folder_type->range_id);

                switch ($range_type) {
                    case 'course': {
                        $course = Course::find($this->to_folder_type->range_id);
                        if ($course) {
                            $this->to_folder_name = $course->getFullName();
                        }
                        break;
                    }
                    case 'institute': {
                        $institute = Institute::find($this->to_folder_type->range_id);
                        if ($institute) {
                            $this->to_folder_name = $institute->getFullName();
                        }
                        break;
                    }
                    case 'user': {
                        $user = User::find($this->to_folder_type->range_id);
                        if ($user) {
                            $this->to_folder_name = $user->getFullName();
                        }
                        break;
                    }
                    case 'message': {
                        $message = Message::find($this->to_folder_type->range_id);
                        if ($message) {
                            $this->to_folder_name = $message->subject;
                        }
                        break;
                    }
                }
            }
        } else {
            //The folder is not a top folder. We can use its name directly.
            $this->to_folder_name = $this->to_folder_type->name;
        }
        if (Request::isAjax()) {
            $this->response->add_header('X-Dialog-Execute', 'STUDIP.OER.initSearch');
        }
    }

    /**
     * This is a helper method that decides where a redirect shall be made
     * in case of error or success after an action was executed.
     */
    public function redirectToFolder($folder)
    {
        switch ($folder->range_type) {
            case 'course':
            case 'institute':
                $this->relocate($folder->range_type . '/files/index/' . $folder->getId(), ['cid' => $folder->range_id]);
                break;
            case 'user':
                $this->relocate('files/index/' . $folder->getId(), ['cid' => null]);
                break;
            case 'Resource':
                $this->relocate(
                    'resources/resource/files/'
                    . $folder->range_id . '/'
                    . $folder->getId()
                );
                break;
            case 'user':
                $this->relocate('files/flat', ['cid' => null]);
                break;
            default:
                //Plugins should not be available in the flat view.
                $this->relocate('files/system/' . $folder->range_type . '/' . $folder->getId(), ['cid' => null]);
                break;
        }
    }
}