aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/StudipRangeTree.class.php
blob: 291d443066192dc03e336b73d5dc4b8e589ad24c (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
<?
# Lifter002: TODO
# Lifter007: TODO
# Lifter003: TODO
# Lifter010: TODO
// +---------------------------------------------------------------------------+
// This file is part of Stud.IP
// StudipRangeTree.class.php
// Class to handle structure of 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.
// +---------------------------------------------------------------------------+

/**
* class to handle the "range tree"
*
* This class provides an interface to the structure of the "range tree"
*
* @access   public
* @author   André Noack <noack@data-quest.de>
* @package
*/
class StudipRangeTree extends TreeAbstract
{
    var $sem_number;
    var $sem_status;
    var $sem_dates;
    var $studip_objects = [];
    var $visible_only;
    var $entries_init_done = false;

    /**
    * constructor
    *
    * do not use directly, call TreeAbstract::GetInstance("StudipRangeTree")
    * @access private
    */
    function __construct($args) {
        DbView::addView('range_tree');

        $this->root_name = Config::get()->UNI_NAME_CLEAN;
        $this->studip_objects['inst'] = ['pk' => 'Institut_id', 'table' => 'Institute'];
        $this->studip_objects['fak'] = ['pk' => 'Institut_id', 'table' => 'Institute'];
        if (isset($args['sem_number']) ){
            $this->sem_number = array_map('intval', $args['sem_number']);
        }
        if (isset($args['sem_status']) ){
            $this->sem_status = array_map('intval', $args['sem_status']);
        }
        $this->visible_only = (int)$args['visible_only'];
        parent::__construct(); //calling the baseclass constructor
        $this->sem_dates = Semester::findAllVisible();
    }

    /**
    * initializes the tree
    *
    * stores all rows from table range_tree in array $tree_data
    * @access public
    */
    function init(){
        parent::init();
        $this->tree_data['root']['studip_object_id'] = 'root';
        $db = $this->view->get_query("view:TREE_GET_DATA");
        while ($db->next_record()){
            $item_name = $db->f("name");
            if ($db->f("studip_object")){
                $item_name = $db->f("studip_object_name");
            }
            $this->tree_data[$db->f("item_id")] = ["studip_object" => $db->f("studip_object"),
                                                    "studip_object_id" => $db->f("studip_object_id"),
                                                    "fakultaets_id" => $db->f("fakultaets_id"),"entries" => 0];
            $this->storeItem($db->f("item_id"), $db->f("parent_id"), $item_name, $db->f("priority"));
        }
    }

    function initEntries(){
        $this->view->params[0] = (isset($this->sem_status)) ? " AND d.status IN('" . join("','", $this->sem_status) . "')" : " ";
        $this->view->params[0] .= $this->visible_only ? " AND visible=1 " : "";
        $this->view->params[1] = (isset($this->sem_number)) ? " WHERE ((" . $this->view->sem_number_sql
                                . ") IN (" . join(",",$this->sem_number) .") OR ((" . $this->view->sem_number_sql
                                .") <= " . $this->sem_number[count($this->sem_number)-1]
                                . "  AND ((" . $this->view->sem_number_end_sql . ") >= " . $this->sem_number[count($this->sem_number)-1]
                                . " OR (" . $this->view->sem_number_end_sql  . ") = -1))) " : "";

        $db = $this->view->get_query("view:TREE_GET_SEM_ENTRIES");
        while ($db->next_record()){
            $this->tree_data[$db->f("item_id")]['entries'] = $db->f('entries');
        }
        $this->entries_init_done = true;
    }

    /**
    * Returns Stud.IP range_id of the next "real" object
    *
    * This function finds the next item wich is a real Stud.IP Object, either an "Einrichtung" or a "Fakultaet"<br>
    * useful for the user rights management
    * @access   public
    * @param    string  $item_id
    * @return   array   of primary keys from table "institute"
    */
    function getAdminRange($item_id) {
        if (!$this->tree_data[$item_id]) {
            return false;
        }

        $found = false;
        $ret = false;
        $next_link = $item_id;

        while(($next_link = $this->getNextLink($next_link)) != 'root') {
            if ($this->tree_data[$next_link]['studip_object'] == 'inst') {
                $found[] = $next_link;
            }

            if ($this->tree_data[$next_link]['studip_object'] == 'fak') {
                if (is_array($found) && count($found)) {
                    foreach($found as $f) {
                        if ($this->tree_data[$f]['fakultaets_id'] == $this->tree_data[$next_link]['studip_object_id']) {
                            $ret[] = $this->tree_data[$f]['studip_object_id'];
                        }
                    }

                    $ret[] = $this->tree_data[$next_link]['studip_object_id'];
                } else {
                    $ret[] = $this->tree_data[$next_link]['studip_object_id'];
                }
                break;
            }
            $next_link = $this->tree_data[$next_link]['parent_id'];
        }

        if (!$ret){
            $ret[] = $next_link;
        }

        return $ret;
    }
    /**
    * returns the next item_id upwards the tree which is a Stud.IP object
    *
    * help function for getAdminRange()
    *
    * @access   private
    * @param    string  $item_id
    * @return   string
    */

    function getNextLink($item_id){
    if (!$this->tree_data[$item_id])
            return false;
        $ret_id = $item_id;
        while (!$this->tree_data[$ret_id]['studip_object_id']){
            $ret_id = $this->tree_data[$ret_id]['parent_id'];
        }
        return $ret_id;
    }

    function getSemIds($item_id,$ids_from_kids = false){
        if (!$this->tree_data[$item_id])
            return false;
        if ($ids_from_kids){
            $this->view->params[0] = $this->getKidsKids($item_id);
        }
        $this->view->params[0][] = $item_id;
        $this->view->params[1] = (isset($this->sem_number)) ? " HAVING sem_number IN (" . join(",",$this->sem_number) .") OR (sem_number <= " . $this->sem_number[count($this->sem_number)-1] . "  AND (sem_number_end >= " . $this->sem_number[count($this->sem_number)-1] . " OR sem_number_end = -1)) " : " ";
        $ret = false;
        $rs = $this->view->get_query("view:RANGE_TREE_GET_SEMIDS");
        while($rs->next_record()){
            $ret[] = $rs->f(0);
        }
        return $ret;
    }

    function getNumEntries($item_id, $num_entries_from_kids = false){
        if (!$this->tree_data[$item_id])
            return false;
        if (!$this->entries_init_done) $this->initEntries();

        return parent::getNumEntries($item_id, $num_entries_from_kids);
    }

    function InsertItem($item_id, $parent_id, $item_name, $priority,$studip_object,$studip_object_id){
        $view = new DbView();
        $view->params = [$item_id,$parent_id,$item_name,$priority,$studip_object,$studip_object_id];
        $rs = $view->get_query("view:TREE_INS_ITEM");
        return $rs->affected_rows();
    }

    function UpdateItem($item_name,$studip_object,$studip_object_id,$item_id){
        $view = new DbView();
        $view->params = [$item_name,$studip_object,$studip_object_id,$item_id];
        $rs = $view->get_query("view:TREE_UPD_ITEM");
        return $rs->affected_rows();
    }

    function DeleteItems($items_to_delete){
        $view = new DbView();
        $view->params[0] = (is_array($items_to_delete)) ? $items_to_delete : [$items_to_delete];
        $view->auto_free_params = false;
        $rs = $view->get_query("view:TREE_DEL_ITEM");
        $deleted['items'] = $rs->affected_rows();
        $rs = $view->get_query("view:CAT_DEL_RANGE");
        $deleted['categories'] = $rs->affected_rows();
        return $deleted;
    }
}
?>