aboutsummaryrefslogtreecommitdiff
path: root/lib/filesystem/RootFolder.php
blob: 523a1a165b7d22b25280e3403c833183e9ce5dda (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
<?php
/**
 * RootFolder.php
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * @author    André Noack <noack@data-quest.de>
 * @copyright 2017 Stud.IP Core-Group
 * @license   http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category  Stud.IP
 */
class RootFolder extends StandardFolder
{

    /**
     * @return string
     */
    public static function getTypeName()
    {
        return _('Hauptordner');
    }

    public static function availableInRange($range_id_or_object, $user_id)
    {
        return false;
    }

    /**
     * @param string $attribute
     * @return mixed
     */
    public function __get($attribute)
    {
        if ($attribute === 'name') {
            $range = $this->getRangeObject();
            return isset($range) ? $range->getFullName('short') : '';
        }
        return $this->folderdata[$attribute];
    }

    /**
     * @param string $user_id
     * @return bool
     */
    public function isWritable($user_id)
    {
        if (
            ($this->range_type === 'user' && $this->range_id === $user_id)
            || $this->isEditable($user_id)
        ) {
            return true;
        }

        if (!Seminar_Perm::get()->have_studip_perm('autor', $this->range_id, $user_id)) {
            return false;
        }

        //The user has autor permissions. This is a special case since the upload to the root folder
        //may be denied globally and allowed locally, or it may be allowed globally and denied locally.
        //Also, this only affects courses, not study groups or root folders in other range types (institutes etc.).
        if ($this->range_type !== 'course') {
            //Upload allowed.
            return true;
        }

        //The root folder belongs to a course object.
        $course = Course::find($this->range_id);
        $locked_status = null;
        if (isset($this->folderdata['data_content']['locked'])) {
            $locked_status = $this->folderdata['data_content']['locked'] === 1;
        }
        if ($course->isStudygroup()) {
            //Study groups are not affected by the global PREVENT_ROOT_FOLDER_UPLOADS_BY_STUDENTS_IN_COURSES config.
            return !$locked_status;
        }
        //At this point, only the settings for real courses are left to be checked:
        if ($locked_status !== null) {
            //The locked status for the folder is set. Uploading to the folder is allowed
            //when the locked status is not '1'.
            return !$locked_status;
        }

        // The locked status for the folder is not set. Therefore, the global configuration
        // is relevant for checking if upload is allowed:
        return !Config::get()->PREVENT_ROOT_FOLDER_UPLOADS_BY_STUDENTS_IN_COURSES;
    }

    /**
     * @param string $user_id
     * @return bool
     */
    public function isEditable($user_id)
    {
        return Seminar_Perm::get()->have_studip_perm('tutor', $this->range_id, $user_id);
    }

    /**
     * Returns the parent-folder as a StandardFolder
     * @return FolderType
     */
    public function getParent()
    {
        return null;
    }

    /**
     * @return bool|number
     */
    public function store()
    {
        $this->folderdata['parent_id'] = '';
        return $this->folderdata->store();
    }

    /**
     * @return Flexi\Template
     */
    public function getEditTemplate()
    {
        $template = $GLOBALS['template_factory']->open('filesystem/root_folder/edit');
        $template->folder = $this;
        return $template;
    }

    /**
     * @param array $request
     * @return FolderType|MessageBox
     */
    public function setDataFromEditTemplate($request)
    {
        $locked_status = null;
        if (isset($request['locked'])) {
            //The locked status is defined in one way or another.
            $locked_status = $request['locked'] ? 1 : 0;
        }
        $this->folderdata['data_content'] = [
            'locked' => $locked_status
        ];
        return $this;
    }
}