aboutsummaryrefslogtreecommitdiff
path: root/lib/filesystem/CoursePublicFolder.php
blob: a55539dea7741c0c95d48dc2fd1244efa45560d4 (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
<?php
/**
 * CoursePublicFolder.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    Elmar Ludwig <elmar.ludwig@uos.de>
 * @copyright 2017 Stud.IP Core-Group
 * @license   http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category  Stud.IP
 */
class CoursePublicFolder extends StandardFolder
{
    public static $sorter = 7;


    /**
     * Returns a localised name of the CoursePublicFolder type.
     *
     * @return string The localised name of this folder type.
     */
    public static function getTypeName()
    {
        return _('Ordner für öffentlich zugängliche Dateien');
    }

    /**
     * @param Object|string $range_id_or_object
     * @param string $user_id
     * @return bool
     */
    public static function availableInRange($range_id_or_object, $user_id)
    {
        $course = Course::toObject($range_id_or_object);
        if ($course && !$course->isNew()) {
            return Seminar_Perm::get()->have_studip_perm('tutor', $course->id, $user_id);
        }
    }

    public function getIcon($role = Icon::DEFAULT_ROLE)
    {
        $shape = $this->is_empty
               ? 'folder-public-empty'
               : 'folder-public-full';

        return Icon::create($shape, $role);
    }


    /**
     * Determines, if the folder is accessible worldwide.
     *
     * @return bool True, if the folder is accessible worldwide,
     *     false otherwise.
     */
    public function hasWorldwideAccess()
    {
        return (bool) $this->folderdata['data_content']['worldwide_access'];
    }


    /**
     * CoursePublicFolders are visible for all logged in users.
     * If ENABLE_FREE_ACCESS is set to 'courses_only' or '1', the folder is
     * visible for everyone. In case, the worldwide access option for the
     * folder is set, the folder is visible worldwide.
     *
     * @param string $user_id The user who wishes to see the folder.
     *
     * @return bool True
     */
    public function isVisible($user_id)
    {
        if ($user_id === null || $user_id === 'nobody') {
            if ($this->hasWorldwideAccess()) {
                return true;
            }
            $range = $this->getRangeObject();
            return Config::get()->ENABLE_FREE_ACCESS && isset($range) && $range->lesezugriff == 0;
        }

        return true;
    }

    /**
     * CoursePublicFolders are readable for all logged in users.
     *
     * @param string $user_id The user who wishes to read the folder.
     *
     * @return bool True
     */
    public function isReadable($user_id)
    {
        return $this->isVisible($user_id);
    }

    /**
     * Returns a description template for CoursePublicFolders.
     *
     * @return string A string describing this folder type.
     */
    public function getDescriptionTemplate()
    {
        return _('Dateien aus diesem Ordner werden auf der Detailseite der Veranstaltung zum Download angeboten.');
    }


    /**
     * Returns the edit template for this folder type.
     *
     * @return Flexi_Template
     */
    public function getEditTemplate()
    {
        $template = $GLOBALS['template_factory']->open('filesystem/course_public_folder/edit.php');
        $template->worldwide_access = $this->hasWorldwideAccess();
        return $template;
    }

    /**
     * Sets the data from a submitted edit template.
     *
     * @param array $request The data from the edit template.
     *
     * @return FolderType
     */
    public function setDataFromEditTemplate($request)
    {
        $this->folderdata['data_content']['worldwide_access'] = $request['course_public_folder_worldwide_access'];
        return parent::setDataFromEditTemplate($request);
    }


    /**
     * Files in CoursePublicFolders are downloadable for all logged in users.
     *
     * @param string $file_id The ID to a FileRef.
     * @param string $user_id The user who wishes to downlaod the file.
     *
     * @return bool
     */
    public function isFileDownloadable($file_id, $user_id)
    {
        return $this->isVisible($user_id);
    }
}