aboutsummaryrefslogtreecommitdiff
path: root/lib/evaluation/classes/Evaluation.class.php
blob: 5f269a78212d3ffdd130debcdfc68a60aa19eba8 (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
531
532
533
534
535
536
537
538
539
<?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';
require_once EVAL_FILE_EVALDB;
require_once EVAL_FILE_OBJECT;
require_once EVAL_FILE_GROUP;
# ====================================================== end: including files #


# Define all required constants ============================================= #
/**
 * @const INSTANCEOF_EVAL Is instance of an evaluation object
 * @access public
 */
define("INSTANCEOF_EVAL", "Evaluation");
# ===================================================== end: define constants #


/**
 * The mainclass for an evaluation for the Stud.IP-project.
 *
 * @author  Alexander Willner <mail@AlexanderWillner.de>
 *
 * @copyright   2004 Stud.IP-Project
 * @access      public
 * @package     evaluation
 *
 */
class Evaluation extends EvaluationObject implements PrivacyObject
{
# Define all required variables ============================================= #
    /**
     * Startdate
     * @access   private
     * @var      integer $startdate
     */
    var $startdate;

    /**
     * Stopdate
     * @access   private
     * @var      integer $stopdate
     */
    var $stopdate;

    /**
     * Timespan
     * @access   private
     * @var      integer $timespan
     */
    var $timespan;

    /**
     * Time of creation. Is set automatically.
     * @access   private
     * @var      integer $mkdate
     */
    var $mkdate;

    /**
     * Time of last change. Is set automatically.
     * @access   private
     * @var      integer $chdate
     */
    var $chdate;

    /**
     * Defines wheter the evaluation is anonymous
     * @access   private
     * @var      boolean $anonymous
     */
    var $anonymous;

    /**
     * Defines whether the evaluation is visible
     * @access   private
     * @var      boolean $visible
     */
    var $visible;

    /**
     * Defines whether the evaluation template is shared
     * @access   private
     * @var      boolean $shared
     */
    var $shared;

    /**
     * Counts the number of connected ranges
     * @access   private
     * @var      integer $numberRanges
     */
    var $numberRanges;

    /**
     * Counts the number of connected ranges
     * @access   private
     * @var      integer $rangeNum
     */
    var $rangeNum;

    /**
     * Constructor
     * @access   public
     * @param string $objectID The ID of an existing evaluation
     * @param object $parentObject The parent object if exists
     * @param integer $loadChildren See const EVAL_LOAD_*_CHILDREN
     */
    public function __construct($objectID = "", $parentObject = null, $loadChildren = EVAL_LOAD_NO_CHILDREN)
    {
        parent::__construct($objectID, $parentObject, $loadChildren);
        $this->instanceof = INSTANCEOF_EVAL;

        $this->rangeID = [];
        $this->startdate = NULL;
        $this->stopdate = NULL;
        $this->timespan = NULL;
        $this->mkdate = time();
        $this->chdate = time();
        $this->anonymous = NO;
        $this->visible = NO;
        $this->shared = NO;
        $this->rangeNum = 0;
        $this->db = new EvaluationDB ();
        if ($this->db->isError()) {
            return $this->throwErrorFromClass($this->db);
        }
        $this->init($objectID);
    }

    /**
     * Sets the startdate
     * @access  public
     * @param integer $startdate The startdate.
     * @throws  error
     */
    public function setStartdate($startdate)
    {
        if (!empty ($startdate)) {
            if (!empty ($this->stopdate) && $startdate > $this->stopdate) {
                return $this->throwError(1, _("Das Startdatum ist nach dem Stoppdatum."));
            }
            if ($startdate <= 0) {
                return $this->throwError(1, _("Das Startdatum ist leider ungültig."));
            }
        }
        $this->startdate = $startdate;
    }

    /**
     * Gets the startdate
     * @access  public
     * @return  integer  The startdate
     */
    public function getStartdate()
    {
        return $this->startdate;
    }

    /**
     * Sets the stopdate
     * @access  public
     * @param integer $stopdate The stopdate.
     * @throws  error
     */
    public function setStopdate($stopdate)
    {
        if (!empty ($stopdate)) {
            if ($stopdate <= 0)
                return $this->throwError(1, _("Das Stoppdatum ist leider ungültig."));
            if ($stopdate < $this->startdate)
                return $this->throwError(1, _("Das Stoppdatum ist vor dem Startdatum."));
            if (!empty ($this->timespan))
                $this->timespan = NULL;
        }
        $this->stopdate = $stopdate;
    }

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

    /**
     * Gets the real stop date as a UNIX-timestamp (e.g. startdate + timespan)
     * @access  public
     * @return  integer The UNIX-timestamp with the real stopdate
     */
    public function getRealStopdate()
    {
        $stopdate = $this->getStopdate();

        if ($this->getTimespan() != NULL)
            $stopdate = $this->getStartdate() + $this->getTimespan();

        return $stopdate;
    }

    /**
     * Sets the timespan
     * @access  public
     * @param string $timespan The timespan.
     * @throws  error
     */
    public function setTimespan($timespan)
    {
        if (!empty ($timespan) && !empty ($this->stopdate))
            $this->stopdate = NULL;
        $this->timespan = $timespan;
    }

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

    /**
     * Gets the creationdate
     * @access  public
     * @return  integer  The creationdate
     */
    public function getCreationdate()
    {
        return $this->mkdate;
    }

    /**
     * Gets the changedate
     * @access  public
     * @return  integer  The changedate
     */
    public function getChangedate()
    {
        return $this->chdate;
    }

    /**
     * Sets anonymous
     * @access  public
     * @param string $anonymous The anonymous.
     * @throws  error
     */
    public function setAnonymous($anonymous)
    {
        $this->anonymous = $anonymous == YES ? YES : NO;
    }

    /**
     * Gets anonymous
     * @access  public
     * @return  string  The anonymous
     */
    public function isAnonymous()
    {
        return $this->anonymous == YES ? YES : NO;
    }

    /**
     * Sets visible
     * @access  public
     * @param string $visible The visible.
     * @throws  error
     */
    public function setVisible($visible)
    {
        $this->visible = $visible == YES ? YES : NO;
    }

    /**
     * Gets visible
     * @access  public
     * @return  string  The visible
     */
    public function isVisible()
    {
        return $this->visible == YES ? YES : NO;
    }

    /**
     * Set shared for a public search
     * @access  public
     * @param boolean $shared if true it is shared
     */
    public function setShared($shared)
    {
        if ($shared == YES && $this->isTemplate() == NO)
            return $this->throwError(1, _("Nur ein Template kann freigegeben werden"));
        $this->shared = $shared == YES ? YES : NO;
    }

    /**
     * Is shared for a public search?
     * @access  public
     * @return  boolen  true if it is shared template
     */
    public function isShared()
    {
        return $this->shared == YES ? YES : NO;
    }

    /**
     * Is this evaluation a template?
     * @access  public
     * @return  boolen  true if it is a template
     */
    public function isTemplate()
    {
        return empty ($this->rangeID) ? YES : NO;
    }

    /**
     * Has a user used this evaluation?
     * @access  public
     * @param string $userID Optional an user id
     * @return  string  YES if a user used this evaluation
     */
    public function hasVoted($userID = "")
    {
        return $this->db->hasVoted($this->getObjectID(), $userID);
    }

    /**
     * Removes a range from the object (not from the DB!)
     * @access  public
     * @param string $rangeID The range id
     */
    public function removeRangeID($rangeID)
    {
        $temp = [];
        while ($oldRangeID = $this->getNextRangeID()) {
            if ($oldRangeID != $rangeID) {
                $temp[] = $oldRangeID;
            }
        }
        $this->rangeID = $temp;
        $this->numberRanges = count($temp);
    }

    /**
     * Removes all rangeIDs
     * @access   public
     */
    public function removeRangeIDs()
    {
        while ($this->getRangeID()) ;
    }

    /**
     * Adds a rangeID
     * @access  public
     * @param string $rangeID The rangeID
     * @throws  error
     */
    public function addRangeID($rangeID)
    {
        $this->rangeID[] = $rangeID;
        $this->numberRanges++;
    }

    /**
     * Gets the first rangeID and removes it
     * @access  public
     * @return  string  The first object
     */
    public function getRangeID()
    {
        if ($this->numberRanges)
            $this->numberRanges--;
        return array_pop($this->rangeID);
    }

    /**
     * Gets the next rangeID
     * @access  public
     * @return  string   The rangeID
     */
    public function getNextRangeID()
    {
        if ($this->rangeNum >= $this->numberRanges) {
            $this->rangeNum = 0;
            return NULL;
        }
        return $this->rangeID[$this->rangeNum++];
    }

    /**
     * Gets all the rangeIDs from the evaluation
     * @access  public
     * @return  array  An array full of rangeIDs
     */
    public function getRangeIDs()
    {
        return $this->rangeID;
    }

    /**
     * Gets the number of ranges
     * @access  public
     * @return  integer  Number of ranges
     */
    public function getNumberRanges()
    {
        return $this->numberRanges;
    }

    /**
     * Resets all answers for this evaluation
     * @access public
     */
    public function resetAnswers()
    {
        // Für diesen Mist habe ich jetzt ca. 3 Stunden gebraucht :(
        $answers = $this->getSpecialChildobjects($this, INSTANCEOF_EVALANSWER);

        $number = count($answers);
        for ($i = 0; $i < $number; $i++) {
            $answer = &$answers[$i];
#while ($answer->getUserID ()); // delete users...
            $answer->db->resetVotes($answer);
        }

    }

    /**
     * Export available data of a given user into a storage object
     * (an instance of the StoredUserData class) for that user.
     *
     * @param StoredUserData $storage object to store data into
     */
    public static function exportUserData(StoredUserData $storage)
    {
        $field_data = DBManager::get()->fetchAll("SELECT * FROM eval WHERE author_id = ?", [$storage->user_id]);
        if ($field_data) {
            $storage->addTabularData(_('Evaluation'), 'eval', $field_data);
        }

        $field_data = DBManager::get()->fetchAll("SELECT * FROM evalanswer_user WHERE user_id = ?", [$storage->user_id]);
        if ($field_data) {
            $storage->addTabularData(_('EvaluationAnswerUser'), 'evalanswer_user', $field_data);
        }

        $field_data = DBManager::get()->fetchAll("SELECT * FROM eval_group_template WHERE user_id = ?", [$storage->user_id]);
        if ($field_data) {
            $storage->addTabularData(_('EvaluationGroupTemplate'), 'eval_group_template', $field_data);
        }

        $field_data = DBManager::get()->fetchAll("SELECT * FROM eval_templates WHERE user_id = ?", [$storage->user_id]);
        if ($field_data) {
            $storage->addTabularData(_('EvaluationTemplates'), 'eval_templates', $field_data);
        }

        $field_data = DBManager::get()->fetchAll("SELECT * FROM eval_templates_user WHERE user_id = ?", [$storage->user_id]);
        if ($field_data) {
            $storage->addTabularData(_('EvaluationTemplatesUser'), 'eval_templates_user', $field_data);
        }

        $field_data = DBManager::get()->fetchAll("SELECT * FROM eval_user WHERE user_id = ?", [$storage->user_id]);
        if ($field_data) {
            $storage->addTabularData(_('EvaluationUser'), 'eval_user', $field_data);
        }

    }

    /**
     * Sets the creationdate
     * @access  private
     * @param integer $creationdate The creationdate.
     * @throws  error
     */
    public function setCreationdate($creationdate)
    {
        $this->mkdate = $creationdate;
    }

    /**
     * Sets the changedate
     * @access  private
     * @param integer $changedate The changedate.
     * @throws  error
     */
    public function setChangedate($changedate)
    {
        $this->chdate = $changedate;
    }

    /**
     * Checks if object is in a valid state
     * @access private
     */
    public function check()
    {
        parent::check();
        if (empty ($this->title)) {
            $this->throwError(1, _("Der Titel darf nicht leer sein."));
        }

        if ($this->isTemplate() && $this->hasVoted()) {
            $this->throwError(2, _("Ungültiges Objekt: Bei einer Vorlage wurde abgestimmt."));
        }

        if (!$this->isTemplate() && $this->isShared()) {
            $this->throwError(3, _("Ungültiges Objekt: Eine aktive Evaluation wurde freigegeben."));
        }

    }
}