aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/RangeTreeObject.class.php
blob: c4c5320d3fb13092ff7a3406c3455dd6ff4335e7 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
# Lifter002: TODO
# Lifter007: TODO
# Lifter003: TODO
# Lifter010: TODO
// +---------------------------------------------------------------------------+
// This file is part of Stud.IP
// RangeTreeObject.class.php
// Class to handle items in the "range tree"
//
// Copyright (c) 2002 André Noack <noack@data-quest.de>
// Suchi & Berg GmbH <info@data-quest.de>
// +---------------------------------------------------------------------------+
// 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 any later version.
// +---------------------------------------------------------------------------+
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
// +---------------------------------------------------------------------------+

/**
* base class for items in the "range tree"
*
* This class is used for items
*
* @access   public
* @author   André Noack <noack@data-quest.de>
* @package
*/
class RangeTreeObject {

    /**
    * item_id in range_tree
    *
    *
    * @access   public
    * @var      string $tree_item_id
    */
    var $tree_item_id;
    /**
    * References the tree object
    *
    *
    * @access   private
    * @var      object StudipRangeTree $tree
    */
    var $tree;
    /**
    * associative array with data from database fields
    *
    *
    * @access   public
    * @var      array $item_data
    */
    var $item_data = null;

    /**
    * associative array with mapping for database fields
    *
    *
    * @access   public
    * @var      array $item_data_mapping
    */
    var $item_data_mapping = null;

    /**
    * Factory method
    *
    *
    * @access public
    * @static
    * @param    string  $item_id
    * @return   object RangeTreeObject
    */
    public static function GetInstance($item_id){
        $tree = TreeAbstract::GetInstance("StudipRangeTree", false);
        $class_name = "RangeTreeObject" . ucfirst($tree->tree_data[$item_id]['studip_object']);
        return new $class_name($item_id);
    }

    /**
    * Constructor
    *
    * Do not use directly, call factory method instead
    * @access private
    * @param    string  $item_id
    */
    function __construct($item_id) {
        $this->tree = TreeAbstract::GetInstance("StudipRangeTree", false);
        $this->tree_item_id = $item_id;
        $this->item_data = $this->tree->tree_data[$item_id];
    }

    /**
    * Returns all tree items which are kids of this object
    *
    *
    * @access public
    * @param    boolean $as_value_list
    * @return   mixed   returns numeric array if param is false, else comma separated string
    */
    function getAllItemKids($as_value_list = false){
        return ($as_value_list) ? $this->getValueList($this->tree->getKidsKids($this->tree_item_id)) : $this->tree->getKidsKids($this->tree_item_id);
    }

    /**
    * Returns all tree items which are kids of this object and are "real" Stud.IP objects
    *
    *
    * @access public
    * @param    boolean $as_value_list
    * @return   mixed   returns numeric array if param is false, else comma separated string
    */
    function getAllObjectKids($as_value_list = false){
        $all_object_kids = array_merge((array)$this->getInstKids(), (array)$this->getFakKids());
        return ($as_value_list) ? $this->getValueList($all_object_kids) : $all_object_kids;
    }

    /**
    * Returns all tree items which are kids of this object and are Stud.IP "Einrichtungen"
    *
    *
    * @access public
    * @param    boolean $as_value_list
    * @return   mixed   returns numeric array if param is false, else comma separated string
    */
    function getInstKids($as_value_list = false){
        $all_kids = $this->tree->getKidsKids($this->tree_item_id);
        $inst_kids = [];
        for ($i = 0; $i < count($all_kids); ++$i){
            if ($this->tree->tree_data[$all_kids[$i]]['studip_object'] == 'inst'){
                $inst_kids[] = $this->tree->tree_data[$all_kids[$i]]['studip_object_id'];
            }
        }
        return ($as_value_list) ? $this->getValueList($inst_kids) : $inst_kids;
    }

    /**
    * Returns all tree items which are kids of this object and are Stud.IP "Fakultaeten"
    *
    *
    * @access public
    * @param    boolean $as_value_list
    * @return   mixed   returns numeric array if param is false, else comma separated string
    */
    function getFakKids($as_value_list = false){
        $all_kids = $this->tree->getKidsKids($this->tree_item_id);
        $fak_kids = [];
        for ($i = 0; $i < count($all_kids); ++$i){
            if ($this->tree->tree_data[$all_kids[$i]]['studip_object'] == 'fak'){
                $inst_kids[] = $this->tree->tree_data[$all_kids[$i]]['studip_object_id'];
            }
        }
        return ($as_value_list) ? $this->getValueList($fak_kids) : $fak_kids;
    }

    /**
    * Returns array of Stud.IP range_ids of "real" objects
    *
    * This function is a wrapper for the according function in StudipRangeTree
    * @see StudipRangeTree::getAdminRange()
    * @access   public
    * @return   array   of primary keys from table "institute"
    */
    function getAdminRange(){
        return $this->tree->getAdminRange($this->tree_item_id);
    }

    /**
    * Only useful in RangeTreeObjectInst ,all other items are always in the correct branch
    *
    * @access   public
    * @return   bool
    */
    function isInCorrectBranch(){
        return true;
    }

    /**
    * Returns tree path of the current object
    *
    * This function is a wrapper for the according function in StudipRangeTree
    * @see StudipRangeTree::getItemPath()
    * @access public
    * @return   string
    */
    function getItemPath(){
        return $this->tree->getItemPath($this->tree_item_id);
    }

    /**
    * extends the $item_data array
    *
    * This function fills the $item_data array with fields from the according database table (is of no use in the base class)
    * @abstract
    * @access private
    */
    function initItemDetail(){
        if ($type = $this->item_data['studip_object']){
            $view = DbView::getView('range_tree');
            $view->params = [$this->tree->studip_objects[$type]['table'],
                                $this->tree->studip_objects[$type]['pk'],
                                $this->item_data['studip_object_id']];
            $snap = new DbSnapshot($view->get_query("view:TREE_OBJECT_DETAIL"));
            if ($snap->numRows){
                $fields = $snap->getFieldList();
                $snap->nextRow();
                for ($i = 0; $i < count($fields); ++$i){
                    $this->item_data[$fields[$i]] = $snap->getField($fields[$i]);
                }
            }
        return true;
        }
    return false;
    }

    /**
    * fetch categories of this object from database
    *
    * the categories are appended to the $item_data array, key 'categories', value is object of type DbSnapshot
    * @access private
    * @return   boolean true if categories were found
    */
    function fetchCategories(){
        $view = DbView::getView('range_tree');
        $view->params[] = $this->tree_item_id;
        $rs = $view->get_query("view:TREE_OBJECT_CAT");
        if (is_object($rs)){
            $this->item_data['categories'] = new DbSnapshot($rs);
            return true;
        }
    return false;
    }

    /**
    * getter method for categories of this object
    *
    *
    * @access public
    * @return   object DbSnapshot
    */
    function &getCategories(){
        if (empty($this->item_data['categories']) || !is_object($this->item_data['categories'])){
            $this->fetchCategories();
        }
        return $this->item_data['categories'];
    }

    function fetchNumStaff(){
        $view = DbView::getView('range_tree');
        if (!($view->params[0] = $this->item_data['studip_object_id']))
            $view->params[0] = $this->tree_item_id;
        $rs = $view->get_query("view:STATUS_COUNT");
        if ($rs->next_record()){
            $this->item_data['num_staff'] = $rs->f(0);
            return true;
        }
        return false;
    }

    function getNumStaff(){
        if(!isset($this->item_data['num_staff'])){
            $this->fetchNumStaff();
        }
        return $this->item_data['num_staff'];
    }


    /**
    * transform numerical array into a comma separated string
    *
    * the result could be used in a SQL query
    * @access private
    * @param    array   $list
    * @return   string
    */

    function getValueList($list){
        $value_list = false;
        if (count($list) == 1)
            $value_list = "'$list[0]'";
        else
            $value_list = "'".join("','",$list)."'";
        return $value_list;
    }

}