aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/visibility/VisibilitySettings.php
blob: ec2a2c0f7c53f433ee6b9bf63edd30be8c79c27c (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
<?php

/**
 * VisibilitySettings.php - Group of all possible states of a visibility
 *
 * To be able to edit visibilitysettings as easy as possible we define a state
 * in a class found in the visibilitySettings. To group all the visibilitySetting
 * we use this class. On a verify command this class decides to what class the
 * order should be passed to.
 *
 * 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      Florian Bieringer <florian.bieringer@uni-passau.de>
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @category    Stud.IP
 */
require_once 'visibilitySettings/Me.php';
require_once 'visibilitySettings/Buddies.php';
require_once 'visibilitySettings/Domain.php';
require_once 'visibilitySettings/Studip.php';
require_once 'visibilitySettings/Extern.php';

/**
 * Groups all visibilitySettings
 */
class VisibilitySettings
{
    public static $CLASSES = [
        'Visibility_Me',
        'Visibility_Buddies',
        'Visibility_Domain',
        'Visibility_Studip',
        'Visibility_Extern'
    ];

    /**
     * @var array all visibilitystates 
     */
    public $states = [];
    
     /**
     * @var array all names of all states
     */   
    private $names = [];
    
    /**
     * I/O is expensive. Therefore we make the whole class sessionwide singleton
     * to save some I/O.
     * 
     * @return VisibilitySettings The sessionwide visibilitySettings
     */
    static public function getInstance() 
    {
        static $instance;

        if (!isset($instance)) {
            $instance = new self;
        }

        return $instance;
    }

    /**
     * On first construct we scan the visibilitySettings folder and load all
     * applied visibilitySettings
     */
    function __construct()
    {
        foreach (self::$CLASSES as $classname) {
            $tmp = new $classname;
            if ($tmp->isActivated()) {
                $this->states[$tmp->getIntRepresentation()] = $tmp;
                $this->names[$tmp->getIntRepresentation()] = $tmp->getDisplayName();
            }
        }
        ksort($this->names);
    }

    /**
     * Passthrou to the specified verify method
     * @param string $user_id The userid
     * @param string $owner_id The owner of the visibility
     * @param int $visibility the visibilityID
     * @return boolean true if the user may see it, false if the user is not
     * allowed to see 
     */
    function verify($user_id, $owner_id, $visibility)
    {
        return $this->states[$visibility] && $this->states[$visibility]->verify($owner_id, $user_id);
    }

    /**
     * Get Description of a specific state
     * @param int $stateid the int representation
     * @return string State description
     */
    function getDescription($stateid)
    {
        return $this->states[$stateid]->getDescription();
    }

    /**
     * Returns all keys of states
     * @return array all keys of states 
     */
    function getAllKeys()
    {
        return array_keys($this->names);
    }

    /**
     * Returns all names of states
     * @return array all names of states 
     */
    function getAllNames()
    {
        return $this->names;
    }

    /**
     * Returns the number of possible states
     * @return type 
     */
    function count()
    {
        return count($this->states);
    }

}

?>