aboutsummaryrefslogtreecommitdiff
path: root/lib/evaluation/classes/db/EvaluationAnswerDB.class.php
blob: 54bdb7dda72b465f474de6b0753a42fffb16e021 (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
<?php
# Lifter002: TODO
# Lifter007: TODO
# Lifter003: TODO
# Lifter010: TODO
/**
 * Beschreibung
 *
 * @author      Alexander Willner <mail@AlexanderWillner.de>
 * @copyright   2004 Stud.IP-Project
 * @access      public
 * @package     evaluation
 * @modulegroup evaluation_modules
 *
 */

// +--------------------------------------------------------------------------+
// 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.
// +--------------------------------------------------------------------------+

require_once 'lib/evaluation/evaluation.config.php';
require_once EVAL_FILE_OBJECTDB;


class EvaluationAnswerDB extends EvaluationObjectDB
{
    /**
     * Constructor
     * @access   public
     */
    public function __construct()
    {
        parent::__construct();
        $this->instanceof = 'EvalANSWERDB';
    }

    /**
     * Loads answers of a group from the DB
     * @access  public
     * @param EvaluationAnswer   &&$answerObject The answer object
     */
    public function load(&$answerObject)
    {
        /* load answer --------------------------------------------------------- */
        $row = DBManager::get()->fetchOne(
            "SELECT * FROM evalanswer WHERE evalanswer_id= ?", [$answerObject->getObjectID()]
        );
        if (!count($row)) {
            return $this->throwError(2, _("Keine Antwort mit dieser ID gefunden."));
        }

        $answerObject->setObjectID($row['evalanswer_id']);
        $answerObject->setParentID($row['parent_id']);
        $answerObject->setPosition($row['position']);
        $answerObject->setText($row['text']);
        $answerObject->setValue($row['value']);
        $answerObject->setRows($row['rows']);
        $answerObject->setResidual($row['residual']);
    }


    /**
     * Loads the votes from the users for this answer
     * @access   public
     * @param EvaluationAnswer   &$answerObject The answer object
     */
    function loadVotes(&$answerObject)
    {
        /* load users -------------------------------------------------------- */
        $result = DBManager::get()->fetchFirst("SELECT user_id FROM evalanswer_user
                                            WHERE evalanswer_id= ?", [$answerObject->getObjectID()]);
        foreach ($result as $row) {
            $answerObject->addUserID($row, NO);
        }
    }
    /* ----------------------------------------------------------- end: users */

    /**
     * Writes answers into the DB
     * @access    public
     * @param EvaluationAnswer   &$answerObject The answerobject
     * @throws    error
     */
    function save(&$answerObject)
    {
        /* save answers -------------------------------------------------------- */
        DBManager::get()->execute(
            "REPLACE INTO evalanswer SET
                `evalanswer_id`   = ?,
                `parent_id`       = ?,
                `position`        = ?,
                `text`            = ?,
                `value`           = ?,
                `rows`            = ?,
                `residual`        = ?
                ",
            [$answerObject->getObjectID(),
                $answerObject->getParentID(),
                $answerObject->getPosition(),
                $answerObject->getText(),
                $answerObject->getValue(),
                $answerObject->getRows(),
                $answerObject->isResidual()]);
        /* ----------------------------------------------------- end: answersave */

        /* connect answer to users --------------------------------------------- */
        while ($userID = $answerObject->getNextUserID()) {
            DBManager::get()->execute(
                "INSERT INTO evalanswer_user SET
                    evalanswer_id   = ?,
                    user_id         = ?,
                    evaldate        = UNIX_TIMESTAMP()",
                [$answerObject->getObjectID(), $userID]);
        }
        /* ----------------------------------------------------- end: connecting */

    } // saved

    /**
     * Deletes all votes from the users for this answers
     * @access   public
     * @param EvaluationAnswer   &$answerObject The answer object
     */
    function resetVotes(&$answerObject)
    {
        /* delete userconnects ------------------------------------------------- */
        DBManager::get()->execute("
        DELETE FROM evalanswer_user
            WHERE evalanswer_id   = ?",
            [$answerObject->getObjectID()]);
        /* ------------------------------------------------------- end: deleting */
    }

    /**
     * Deletes a answer
     * @access public
     * @param EvaluationAnswer   &$answerObject The answer to delete
     * @throws  error
     */
    function delete(&$answerObject)
    {
        /* delete answer ----------------------------------------------------- */
        DBManager::get()->execute("
        DELETE FROM evalanswer
            WHERE evalanswer_id   = ?",
            [$answerObject->getObjectID()]);
        /* ------------------------------------------------------- end: deleting */
        $this->resetVotes($answerObject);
    } // deleted


    /**
     * Checks if answer with this ID exists
     * @access  public
     * @param string $answerID The answerID
     * @return  bool     YES if exists
     */
    function exists($answerID)
    {
        $result = DBManager::get()->fetchOne("SELECT 1 FROM evalanswer
                                            WHERE evalanswer_id= ?", [$answerID]);
        if (count($result) > 0)
            return true;
        return false;
    }


    /**
     * Adds the children to a parent object
     * @access  public
     * @param EvaluationObject  &$parentObject The parent object
     */
    public static function addChildren(&$parentObject)
    {
        $result = DBManager::get()->fetchFirst("SELECT evalanswer_id FROM evalanswer
                                            WHERE parent_id= ? ORDER by position",
            [$parentObject->getObjectID()]);

        $loadChildren =
            $parentObject->loadChildren == EVAL_LOAD_ALL_CHILDREN ? EVAL_LOAD_ALL_CHILDREN : EVAL_LOAD_NO_CHILDREN;

        foreach ($result as $row) {
            $child = new EvaluationAnswer($row, $parentObject, $loadChildren);
            $parentObject->addChild($child);
        }
    }

    /**
     * Returns the type of an objectID
     * @access public
     * @param string $objectID The objectID
     * @return string  INSTANCEOF_x, else NO
     */
    function getType($objectID)
    {
        if ($this->exists($objectID)) {
            return INSTANCEOF_EVALANSWER;
        } else {
            return NO;
        }
    }

    /**
     * Returns the id from the parent object
     * @access public
     * @param string $objectID The object id
     * @return string  The id from the parent object
     */
    public static function getParentID($objectID)
    {
        return DBManager::get()->fetchColumn("SELECT parent_id FROM evalanswer
                                            WHERE evalanswer_id = ?",
            [$objectID]);
    }

    /**
     * Give all textanswers for a user and question for the export
     * @access  public
     * @param string $questionID The question id
     * @param string $userID The user id
     */
    function getUserAnwerIDs($questionID, $userID)
    {
        /* ask database ------------------------------------------------------- */
        $sql = "SELECT a.evalanswer_id as ttt FROM evalanswer a, evalanswer_user b
                    WHERE a.parent_id = ? AND a.evalanswer_id = b.evalanswer_id";
        if (empty ($userID))
            $answer_ids = DBManager::get()->fetchFirst($sql, [$questionID]);
        else
            $answer_ids = DBManager::get()->fetchFirst($sql . " AND b.user_id = ?", [$questionID, $userID]);
        /* -------------------------------------------------------- end: asking */
        return $answer_ids;
    }

    /**
     * Checks whether a user has voted for an answer
     * @access   public
     * @param string $answerID The answer id
     * @param string $userID The user id
     * @return   boolean  YES if user has voted for the answer
     */
    function hasVoted($answerID, $userID)
    {
        $result = DBManager::get()->fetchOne("SELECT 1 FROM evalanswer_user
                                            WHERE evalanswer_id= ? AND user_id", [$answerID, $userID]);
        if (count($result) > 0)
            return true;
        return false;
    }

    function getAllAnswers($question_id, $userID, $only_user_answered = false)
    {
        if ($only_user_answered)
            return DBManager::get()->fetchAll("
                SELECT evalanswer.*, COUNT(IF(user_id=?,1,NULL)) AS has_voted
                FROM evalanswer LEFT JOIN evalanswer_user USING(evalanswer_id)
                WHERE parent_id = ? AND user_id = ?
                GROUP BY evalanswer.evalanswer_id ORDER BY position",
                [$userID, $question_id, $userID]);
        else
            return DBManager::get()->fetchAll("
                SELECT evalanswer.*, COUNT(IF(user_id=?,1,NULL)) AS has_voted
                FROM evalanswer LEFT JOIN evalanswer_user USING(evalanswer_id)
                WHERE parent_id = ?
                GROUP BY evalanswer.evalanswer_id ORDER BY position",
                [$userID, $question_id]);
    }
}

?>