Moving to GitHub.
[platal.git] / classes / visibility.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2014 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22 class Visibility
23 {
24 /** Visibility levels.
25 * The VIEW_* constants describe the access level
26 * The EXPORT_* constants describe the degree of confidentiality of the data.
27 */
28 const VIEW_NONE = 'none';
29 const VIEW_PUBLIC = 'public';
30 const VIEW_AX = 'ax';
31 const VIEW_PRIVATE = 'private';
32 const VIEW_ADMIN = 'admin';
33
34 const EXPORT_PUBLIC = 'public';
35 const EXPORT_AX = 'ax';
36 const EXPORT_PRIVATE = 'private';
37 const EXPORT_HIDDEN = 'hidden';
38
39 /** Map each VIEW_ level to the list of EXPORT_ levels it can view.
40 */
41 static private $view_levels = array(
42 self::VIEW_NONE => array(),
43 self::VIEW_PUBLIC => array(self::EXPORT_PUBLIC),
44 self::VIEW_AX => array(self::EXPORT_AX, self::EXPORT_PUBLIC),
45 self::VIEW_PRIVATE => array(self::EXPORT_PRIVATE, self::EXPORT_AX, self::EXPORT_PUBLIC),
46 self::VIEW_ADMIN => array(self::EXPORT_HIDDEN, self::EXPORT_PRIVATE, self::EXPORT_AX, self::EXPORT_PUBLIC),
47 );
48
49 static private $display_levels = array(
50 self::EXPORT_PUBLIC => 0,
51 self::EXPORT_AX => 1,
52 self::EXPORT_PRIVATE => 2,
53 self::EXPORT_HIDDEN => 3,
54 );
55
56 private $level;
57
58 private function __construct($level)
59 {
60 $this->level = $level;
61 }
62
63 static private $vis_list = array();
64 public static function get($level)
65 {
66 Platal::assert(array_key_exists($level, self::$view_levels), "Invalid visibility access level $level.");
67 if (!array_key_exists($level, self::$vis_list)) {
68 self::$vis_list[$level] = new Visibility($level);
69 }
70 return self::$vis_list[$level];
71 }
72
73 public function level()
74 {
75 return $this->level;
76 }
77
78 public static function defaultForRead($max_level = null)
79 {
80 if (!S::logged()) {
81 $vis = self::get(self::VIEW_PUBLIC);
82 } else {
83 $vis = S::user()->readVisibility();
84 }
85 if ($max_level != null) {
86 return $vis->restrict($max_level);
87 } else {
88 return $vis;
89 }
90 }
91
92 public static function defaultForEdit($max_level = null)
93 {
94 if (!S::logged()) {
95 $vis = self::get(self::VIEW_NONE);
96 } else {
97 $vis = S::user()->editVisibility();
98 }
99 if ($max_level != null) {
100 return $vis->restrict($max_level);
101 } else {
102 return $vis;
103 }
104 }
105
106 /** Retrieve a 'restricted' version of the current Visibility.
107 *
108 * @param $level The visibility level to restrict to
109 * @return A new Visibility instance, whose level is min($this->level, $level)
110 */
111 public function restrict($level = null)
112 {
113 if ($level != null && !$this->isVisible($level)) {
114 $level = $this->level();
115 }
116
117 return self::get($level);
118 }
119
120 public function isVisible($visibility)
121 {
122 return in_array($visibility, self::$view_levels[$this->level()]);
123 }
124
125 public function equals($visibility)
126 {
127 return $visibility !== null && $this->level() == $visibility->level();
128 }
129
130 static public function isLessRestrictive($level_a, $level_b)
131 {
132 // self::$display_levels is order from least restrictive
133 // to most restrictive.
134 return self::$display_levels[$level_a] >= self::$display_levels[$level_b];
135 }
136
137 /** Compare the visibility of two fields.
138 * Returns:
139 * >0 if $a is less restrictive than $b,
140 * <0 if $a is more restrictive than $b,
141 * 0 if $a is equal to $b.
142 */
143 static public function cmpLessRestrictive($a, $b)
144 {
145 $a_pub = self::$display_levels[$a];
146 $b_pub = self::$display_levels[$b];
147 /* self::$display_levels is ordered from least restrictive to
148 * most restrictive.
149 * This will be 0 if both levels are equal, < 0 if $b_pub is less
150 * than $a_pub, thus less restrictive, which means that $a comes
151 * before $b in descending restrictiveness order.
152 */
153 return $b_pub - $a_pub;
154 }
155
156 static public function comparePublicity($a, $b)
157 {
158 return self::cmpLessRestrictive($a['pub'], $b['pub']);
159 }
160 }
161
162 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
163 ?>