1<?php
2/*
3    +-----------------------------------------------------------------------------+
4    | ILIAS open source                                                           |
5    +-----------------------------------------------------------------------------+
6    | Copyright (c) 1998-2006 ILIAS open source, University of Cologne            |
7    |                                                                             |
8    | This program is free software; you can redistribute it and/or               |
9    | modify it under the terms of the GNU General Public License                 |
10    | as published by the Free Software Foundation; either version 2              |
11    | of the License, or (at your option) any later version.                      |
12    |                                                                             |
13    | This program is distributed in the hope that it will be useful,             |
14    | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
15    | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
16    | GNU General Public License for more details.                                |
17    |                                                                             |
18    | You should have received a copy of the GNU General Public License           |
19    | along with this program; if not, write to the Free Software                 |
20    | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
21    +-----------------------------------------------------------------------------+
22*/
23
24/**
25*
26* @author Stefan Meyer <meyer@leifos.com>
27* @version $Id$
28*
29*
30* @ingroup ModulesCourse
31*/
32class ilCourseUserData
33{
34    private $db;
35    private $user_id;
36    private $field_id;
37    private $value;
38
39
40    /**
41     * Contructor
42     *
43     * @access public
44     * @param int user id
45     * @param int field id
46     *
47     */
48    public function __construct($a_user_id, $a_field_id = 0)
49    {
50        global $DIC;
51
52        $ilDB = $DIC['ilDB'];
53
54        $this->db = $ilDB;
55        $this->user_id = $a_user_id;
56        $this->field_id = $a_field_id;
57
58        if ($this->field_id) {
59            $this->read();
60        }
61    }
62
63    /**
64     * Get values by obj_id (for all users)
65     *
66     * @access public
67     * @static
68     *
69     * @param int obj_id
70     */
71    public static function _getValuesByObjId($a_obj_id)
72    {
73        global $DIC;
74
75        $ilDB = $DIC['ilDB'];
76
77        include_once('Modules/Course/classes/Export/class.ilCourseDefinedFieldDefinition.php');
78        $field_ids = ilCourseDefinedFieldDefinition::_getFieldIds($a_obj_id);
79        if (!count($field_ids)) {
80            return array();
81        }
82
83        $where = "WHERE " . $ilDB->in('field_id', $field_ids, false, 'integer');
84        $query = "SELECT * FROM crs_user_data " .
85            $where;
86
87        $res = $ilDB->query($query);
88        while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
89            $user_data[$row->usr_id][$row->field_id] = $row->value;
90        }
91
92        return $user_data ? $user_data : array();
93    }
94
95    /**
96     * Check required fields
97     *
98     * @access public
99     * @static
100     *
101     * @param int user id
102     * @param int object id
103     *
104     * @return bool all fields filled
105     */
106    public static function _checkRequired($a_usr_id, $a_obj_id)
107    {
108        global $DIC;
109
110        $ilDB = $DIC['ilDB'];
111
112        include_once('Modules/Course/classes/Export/class.ilCourseDefinedFieldDefinition.php');
113        $required = ilCourseDefinedFieldDefinition::_getRequiredFieldIds($a_obj_id);
114        if (!count($required)) {
115            return true;
116        }
117
118        //$and = ("AND field_id IN (".implode(",",ilUtil::quoteArray($required)).")");
119        $and = "AND " . $ilDB->in('field_id', $required, false, 'integer');
120
121        $query = "SELECT COUNT(*) num_entries FROM crs_user_data " .
122            "WHERE usr_id = " . $ilDB->quote($a_usr_id, 'integer') . " " .
123            "AND value != '' AND value IS NOT NULL " .
124            $and . " " .
125            " ";
126        $res = $ilDB->query($query);
127        $row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT);
128
129        return $row->num_entries == count($required);
130    }
131
132    /**
133     * Delete all entries of an user
134     *
135     * @access public
136     * @static
137     * @param int user_id
138     *
139     */
140    public static function _deleteByUser($a_user_id)
141    {
142        global $DIC;
143
144        $ilDB = $DIC['ilDB'];
145
146        $query = "DELETE FROM crs_user_data " .
147            "WHERE usr_id = " . $ilDB->quote($a_user_id, 'integer');
148        $res = $ilDB->manipulate($query);
149    }
150
151    /**
152     * Delete by field
153     *
154     * @access public
155     * @param
156     *
157     */
158    public static function _deleteByField($a_field_id)
159    {
160        global $DIC;
161
162        $ilDB = $DIC['ilDB'];
163
164        $query = "DELETE FROM crs_user_data " .
165            "WHERE field_id = " . $ilDB->quote($a_field_id, 'integer');
166        $res = $ilDB->manipulate($query);
167    }
168
169    public function setValue($a_value)
170    {
171        $this->value = $a_value;
172    }
173    public function getValue()
174    {
175        return $this->value;
176    }
177
178    /**
179     * update value
180     *
181     * @access public
182     *
183     */
184    public function update()
185    {
186        $this->delete();
187        $this->create();
188    }
189
190    /**
191     * insert entry
192     *
193     * @access public
194     *
195     */
196    public function delete()
197    {
198        global $DIC;
199
200        $ilDB = $DIC['ilDB'];
201
202        $query = "DELETE FROM crs_user_data " .
203            "WHERE usr_id = " . $this->db->quote($this->user_id, 'integer') . " " .
204            "AND field_id = " . $this->db->quote($this->field_id, 'integer');
205        $res = $ilDB->manipulate($query);
206    }
207
208    /**
209     * Add entry
210     *
211     * @access public
212     *
213     */
214    public function create()
215    {
216        global $DIC;
217
218        $ilDB = $DIC['ilDB'];
219
220        $query = "INSERT INTO crs_user_data (value,usr_id,field_id) " .
221            "VALUES( " .
222            $this->db->quote($this->getValue(), 'text') . ", " .
223            $this->db->quote($this->user_id, 'integer') . ", " .
224            $this->db->quote($this->field_id, 'integer') . " " .
225            ")";
226
227        $res = $ilDB->manipulate($query);
228    }
229
230    /**
231     * Read value
232     *
233     * @access private
234     */
235    private function read()
236    {
237        global $DIC;
238
239        $ilDB = $DIC['ilDB'];
240
241        $query = "SELECT * FROM crs_user_data " .
242            "WHERE usr_id = " . $this->db->quote($this->user_id, 'integer') . " " .
243            "AND field_id = " . $this->db->quote($this->field_id, 'integer');
244        $res = $this->db->query($query);
245        $row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT);
246
247        $this->setValue($row->value);
248    }
249}
250