aboutsummaryrefslogtreecommitdiff
path: root/lib/evaluation/classes/EvaluationObject.class.php
blob: e42dfb0e1c924561d6786ce9918751b222009f58 (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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
<?php
# Lifter002: TODO
# Lifter007: TODO
# Lifter003: TODO
# Lifter010: TODO
// +--------------------------------------------------------------------------+
// This file is part of Stud.IP
// Copyright (C) 2001-2004 Stud.IP
// +--------------------------------------------------------------------------+
// 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.
// +--------------------------------------------------------------------------+


# Include all required files ================================================ #
require_once 'lib/evaluation/evaluation.config.php';
# ====================================================== end: including files #


# Define constants ========================================================== #
/**
 * @const INSTANCEOF_EVALOBJECT Is instance of an abstrakt evaluation object
 * @access public
 */
define ("INSTANCEOF_EVALOBJECT", "EvaluationObject");

/**
 * @const EVAL_LOAD_NO_CHILDREN Load no children from DB
 * @access public
 */
define ("EVAL_LOAD_NO_CHILDREN", 0);

/**
 * @const EVAL_LOAD_FIRST_CHILDREN Load just the direct children from DB
 * @access public
 */
define ("EVAL_LOAD_FIRST_CHILDREN", 1);

/**
 * @const EVAL_LOAD_ALL_CHILDREN Load all children from DB
 * @access public
 */
define ("EVAL_LOAD_ALL_CHILDREN", 2);

/**
 * @const EVAL_LOAD_ONLY_EVALGROUP Load just the groups for performance reasons
 * @access public
 */
define ("EVAL_LOAD_ONLY_EVALGROUP", 3);
# ===================================================== end: define constants #


/**
 * This abstract class provides functions for evaluation objects
 *
 * @author  Alexander Willner <mail@AlexanderWillner.de>
 * @copyright   2004 Stud.IP-Project
 * @access      public
 * @package     evaluation
 *
 */
class EvaluationObject extends StudipObject {
# Define all required variables ============================================= #
  /**
   * The parent object
   * @access   private
   * @var      string $parentObject
   */
  var $parentObject;

  /**
   * The parent object id
   * @access   private
   * @var      string $parentObjectCD
   */
  var $parentObjectID;

  /**
   * Array with all linked childobjects
   * @access   private
   * @var      array $childObjects
   */
  var $childObjects;

  /**
   * Counts the number of childs
   * @access   private
   * @var      integer  $numberChildren
   */
  var $numberChildren;

  /**
   * Title of the group
   * @access   private
   * @var      integer $title
   */
  var $title;

  /**
   * Text of the group
   * @access   private
   * @var      integer $text
   */
  var $text;

  /**
   * Position of this group in parent object
   * @access   private
   * @var      integer $position
   */
  var $position;

  /**
   * Holds the DB object
   * @access   private
   * @var      object DatabaseObject $db
   */
  var $db;

  /**
   * Is used as a counter for getNextChild )
   * @access   private
   * @var      integer $childNum
   */
  var $childNum;

  /**
   * Defines how many children to load. See EVAL_LOAD_*_CHILDREN
   * @access   private
   * @var      integer  $loadChildren
   */
  var $loadChildren;
# ============================================================ end: variables #


# Define constructor and destructor ========================================= #
  /**
   * Constructor
   * @param    string            $objectID       The ID of an existing object
   * @param    EvaluationObject  $parentObject   The parent object
   * @param    integer           $loadChildren   See const EVAL_LOAD_*_CHILDREN
   * @access   public
   */
  function __construct($objectID = "", $parentObject = NULL,
                              $loadChildren = EVAL_LOAD_NO_CHILDREN) {

    /* Set default values -------------------------------------------------- */
    parent::__construct($objectID);
    $this->instanceof = INSTANCEOF_EVALOBJECT;

    $this->parentObject   = $parentObject;
    $this->loadChildren   = $loadChildren;
    $this->db             = NULL;
    $this->childObjects   =  [];
    $this->numberChildren = 0;
    $this->title          = "";
    $this->text           = "";
    $this->position       = 0;
    $this->childNum       = 0;
    /* --------------------------------------------------------------------- */
  }
# =========================================== end: constructor and destructor #


# Define public functions =================================================== #
   /**
    * Sets the title
    * @access  public
    * @param   string  $title  The title.
    * @throws  error
    */
   function setTitle ($title) {
         $this->title = $title;
   }

   /**
    * Gets the title
    * @access  public
    * @return  string  The title
    */
   function getTitle () {
         return $this->title;
   }

   /**
    * Sets the text
    * @access  public
    * @param   string  $text        The text.
    * @throws  error
    */
   function setText ($text) {
         $this->text = $text;
   }

   /**
    * Gets the text
    * @access  public
    * @return  string  The text
    */
   function getText () {
         return $this->text;
   }


   /**
    * Sets the position
    * @access  public
    * @param   string  $position  The position.
    */
   function setPosition ($position) {
     $this->position = $position;
   }

   /**
    * Gets the position
    * @access  public
    * @return  string  The position
    */
   function getPosition () {
      return $this->position;
   }

   /**
    * Sets the parentObject
    * @access  public
    * @param   string  &$parentObject  The parentObject.
    */
   function setParentObject (&$parentObject) {
     $this->parentObject = &$parentObject;
     $this->parentObjectID = $this->parentObject->getObjectID ();
   }

   /**
    * Gets the parentObject
    * @access  public
    * @returns object  The parentObject.
    */
   function getParentObject () {
     return $this->parentObject;
   }

   /**
    * Gets the parentObjectID
    * @access  public
    * @returns string  The parentObjectID
    */
   function getParentID () {
     return $this->parentObjectID;
   }


   /**
    * Sets the parentObjectID
    * @access  public
    * @param   string  $parentID  The parent id
    */
   function setParentID ($parentID) {
     $this->parentObjectID = $parentID;
   }

   /**
    * Removes a child from the object (not from the DB!)
    * @access  public
    * @param   string   $childID   The child id
    */
   function removeChildID ($childID) {
      $temp         =  [];
      $childRemoved = NO;

      while ($child = &$this->getNextChild ()) {
         if ($childRemoved)
            $child->setPosition ($child->getPosition () - 1);

         if ($child->getObjectID () != $childID) {
            array_push ($temp, $child);
         } else {
            $childRemoved = YES;
         }
      }

      $this->childObjects = $temp;

      if ($childRemoved)
         $this->numberChildren--;
   }

   /**
    * Adds a child
    * @access  public
    * @param   object  EvaluationObject &$child  The child object
    */
   function addChild (&$child) {
     $child->setPosition ($this->numberChildren++);
     $child->setParentObject ($this);
     array_push ($this->childObjects, $child);
   }

   /**
    * Gets the first child and removes it (if no id is given)
    * @access  public
    * @param   string   $childID   The child id
    * @return  object   The first object
    */
   function &getChild ($childID = "") {
      if (!empty ($childID)) {
         while ($child = $this->getNextChild ())
         if ($child->getObjectID () == $childID) {
            $this->childNum = 0;
            return $child;
         }
         $ret = null;
         return $ret;
      } else {
         if ($this->numberChildren > 0)
            $this->numberChildren--;
         $ret = array_pop ($this->childObjects);
         return $ret;
      }
   }

   /**
    * Gets the next child
    * @access  public
    * @return  object   The next object, otherwise NULL
    */
   function &getNextChild () {
      if ($this->childNum >= $this->numberChildren) {
         $this->childNum = 0;
         $ret = null;
         return $ret;
      }
      return $this->childObjects[$this->childNum++];
   }

   /**
    * Gets all the childs in the object
    * @access  public
    * @return  array  An array full of childObjects
    */
   function getChildren () {
     return $this->childObjects;
   }

   /**
    * Gets the number of children
    * @access  public
    * @return  integer  Number of children
    */
   function getNumberChildren () {
     return $this->numberChildren;
   }

   /**
    * Saves the object into the database
    * @access public
    */
   function save () {
     /* Check own object --------------------------------------------------- */
     $this->check ();
     if ($this->isError ())
       return;
     /* --------------------------------------------------------- end: check */

     /* save own object ---------------------------------------------------- */
     $this->db->save ($this);
     if ($this->db->isError ())
       return $this->throwErrorFromClass ($this->db);
     /* ----------------------------------------------- end: save own object */

     /* save children ------------------------------------------------------ */
     while ($childObject = $this->getNextChild ()) {
       $childObject->save ();
       if ($childObject->isError ())
     return $this->throwErrorFromClass ($childObject);
     }
     /* ------------------------------------------------- end: save children */
   }

   /**
    * Deletes the object from the database
    * @access public
    */
   function delete () {
     /* remove id from parentobject if exists ------------------------------ */
      if (!empty ($this->parentObject)) {
         $this->parentObject->removeChildID ($this->getObjectID ());
      }
     /* ----------------------------------------- end: remove id from parent */

     /* delete own object -------------------------------------------------- */
     $this->db->delete ($this);
     /* --------------------------------------------- end: delete own object */

     /* delete children ---------------------------------------------------- */
     while ($childObject = $this->getChild ()) {
       $childObject->delete ();
       if ($childObject->isError ())
         $this->throwErrorFromClass ($childObject);
     }
     /* ----------------------------------------------- end: delete children */
   }

   /**
    * Duplicates the evaluation object. WARNING: Stored childs will be
    * modified :(
    * @access public
    */
   function &duplicate () {
     $newObject = $this;
     $newObject->duplicate_init ();
     return $newObject;
   }
# ===================================================== end: public functions #


# Define private functions ================================================== #
   /**
    * Initialisation for duplicated objects
    * @access   private
    */
   function duplicate_init () {
     $this->init ();
     while ($childObject =& $this->getNextChild ()) {
       $childObject->setParentID ($this->getObjectID ());
       $childObject->duplicate_init ();
     }
   }

   /**
    * Initialisation for objects
    * @access   private
    * @param    string   $objectID   The object id
    */
   function init ($objectID = "") {
     /* Load an evaluationobject or create a new one ----------------------- */
     if (empty ($objectID)) {
       $this->setObjectID(self::createNewID());
     } else {
       $this->setObjectID ($objectID);
       $this->load ();
       if ($this->db->isError ())
     return $this->throwErrorFromClass ($this->db);
     }
    /* -------------------------------------------------------------------- */

   }

   /**
    * Loads the Object from the database
    * @access private
    */
   function load () {
     $this->db->load ($this);
     if ($this->db->isError ())
       return $this->throwErrorFromClass ($this->db);
   }

   /**
    * Checks if object is in a valid state
    * @access private
    */
   function check () {
     if (empty ($this->db))
       $this->throwError (1, _("Es existiert kein DB-Objekt"));
   }

    /**
     * Gets all children of a special kind
     * @param   EvaluationObject  &$object      the parent object
     * @param   string            $instanceof  instance of the searched child
     * @param   boolean           $reset       for internal use
     * @access  public
     */
   function getSpecialChildobjects (&$object, $instanceof, $reset = false) {
      static $specialchildobjects =  [];
      if ($reset == YES) {
         $specialchildobjects =  [];
      }

      if ($object->x_instanceof () == $instanceof) {
         array_push ($specialchildobjects, $object);
      } else {
         while ($child = &$object->getNextChild ()) {
            $this->getSpecialChildobjects ($child, $instanceof, NO);
         }
      }
      return $specialchildobjects;
   }

   /**
    * Debugfunction
    * @access public
    */
   function toString () {
     echo "<table border=1 cellpadding=5><tr><td>";
     echo "Typ: ".$this->x_instanceof ()."<br>";
     echo "ObjectID: ".$this->getObjectID ()."<br>";
     echo "ParentID: ".$this->getParentID ()."<br>";
     echo "ParentObject: ".$this->getParentObject ()."<br>";
     echo "Author: ".$this->getAuthorID ()."<br>";
     echo "Titel: ".$this->getTitle ()."<br>";
     echo "Text: ".$this->getText ()."<br>";
     echo "Position: ".$this->getPosition ()."<br>";
     echo "Untergruppen: ".$this->getNumberChildren ()."<br>";
     echo "</td></tr>";
     $i = 0;
     while ($child = $this->getNextChild ()) {
       echo "<tr><td>";
       $i++;
       echo "<b>Kind $i</b>"."<br>";
       $child->toString ();
       echo "</td></tr>";
     }
     echo "</table>";
   }
# ==================================================== end: private functions #

}

?>