lib.php 12.4 KB
Newer Older
Martyn Smith's avatar
Martyn Smith committed
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
<?php
/**
 * This program is part of Mahara
 *
 *  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
 *  (at your option) 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 * @package    mahara
 * @subpackage search-internal
 * @author     Martyn Smith <martyn@catalyst.net.nz>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
 *
 */

defined('INTERNAL') || die();

/**
30
 * The internal search plugin which searches against the
Martyn Smith's avatar
Martyn Smith committed
31
32
33
34
35
36
37
 * Mahara database.
 */
class PluginSearchInternal extends PluginSearch {

    /**
     * Implement user searching with SQL
     *
38
39
     * NOTE: user with ID zero should never be returned
     *
Martyn Smith's avatar
Martyn Smith committed
40
41
42
43
44
45
46
47
     * @param string  The query string
     * @param integer How many results to return
     * @param integer What result to start at (0 == first result)
     * @return array  A data structure containing results looking like ...
     *         $results = array(
     *               count   => integer, // total number of results
     *               limit   => integer, // how many results are returned
     *               offset  => integer, // starting from which result
48
     *               data    => array(   // the result records
Martyn Smith's avatar
Martyn Smith committed
49
     *                   array(
50
51
52
53
54
55
56
     *                       id            => integer,
     *                       username      => string,
     *                       institution   => string,
     *                       firstname     => string,
     *                       lastname      => string,
     *                       preferredname => string,
     *                       email         => string,
Martyn Smith's avatar
Martyn Smith committed
57
58
     *                   ),
     *                   array(
59
60
61
62
63
64
65
     *                       id            => integer,
     *                       username      => string,
     *                       institution   => string,
     *                       firstname     => string,
     *                       lastname      => string,
     *                       preferredname => string,
     *                       email         => string,
Martyn Smith's avatar
Martyn Smith committed
66
67
68
69
70
71
     *                   ),
     *                   array(...),
     *               ),
     *           );
     */
    public static function search_user($query_string, $limit, $offset = 0) {
72
73
74
        safe_require('artefact', 'internal');
        $publicfields = array_keys(ArtefactTypeProfile::get_public_fields());
        $prefix = get_config('dbprefix');
Martyn Smith's avatar
Martyn Smith committed
75
        if ( is_postgres() ) {
76
            if (!empty($publicfields)) {
77
78
79
80
81
82
83
84
85
86
87
88
89
90
                $fieldlist = "('" . join("','", $publicfields) . "')";
                $users = get_records_sql_assoc('
                    SELECT DISTINCT ON (u.preferredname, u.lastname, u.firstname, u.id)
                        u.id, u.preferredname, u.lastname, u.firstname, u.username, u.institution
                    FROM ' . $prefix . 'artefact a
                        INNER JOIN ' . $prefix .'usr u ON u.id = a.owner
                    WHERE
                        u.id <> 0
                        AND a.artefacttype IN ' . $fieldlist . "
                        AND (a.title ILIKE '%' || ? || '%')
                    ORDER BY u.preferredname, u.lastname, u.firstname, u.id",
                    array($query_string), $offset, $limit);
                $userlist = '(' . join(',', array_map(create_function('$u','return $u->id;'), $users)) . ')';

91
                $data = get_records_sql_array('
92
93
                    SELECT 
                        u.id, a.artefacttype, a.title
94
                    FROM
95
96
                        ' . $prefix . 'artefact a
                        INNER JOIN ' . $prefix . 'usr u ON u.id = a.owner
97
                    WHERE
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
                        a.artefacttype IN ' . $fieldlist . '
                        AND u.id IN ' . $userlist . '
                    ORDER BY u.preferredname, u.lastname, u.firstname, u.id, a.artefacttype',
                    array());

                if (!empty($data)) {
                    foreach ($data as $rec) {
                        if ($rec->artefacttype == 'email') {
                            $users[$rec->id]->email[] = $rec->title;
                        }
                        else {
                            $users[$rec->id]->{$rec->artefacttype} = $rec->title;
                        }
                    }
                }
Martyn Smith's avatar
Martyn Smith committed
113

114
115
                $count = get_field_sql('
                    SELECT 
116
                        COUNT(DISTINCT owner)
117
                    FROM
118
                        ' . $prefix . 'artefact
119
                    WHERE
120
121
122
123
                        owner <> 0
                        AND artefacttype IN ' . $fieldlist . "
                        AND ( title ILIKE '%' || ? || '%')",
                    array($query_string)
124
                );
125
126
                
                $data = array_values($users);
127
128
129
130
131
            }
            else {
                $data = false;
                $count = 0;
            }
Martyn Smith's avatar
Martyn Smith committed
132
133
134
135
136
        }
        // TODO
        // else if ( is_mysql() ) {
        // }
        else {
Nigel McNie's avatar
Nigel McNie committed
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
            throw new SQLException('search_user() is not implemented for your database engine (' . get_config('dbtype') . ')');
        }

        return array(
            'count'   => $count,
            'limit'   => $limit,
            'offset'  => $offset,
            'data'    => $data,
        );
    }
    
    /**
     * Implement group searching with SQL
     *
     * @param string  The query string
     * @param integer How many results to return
     * @param integer What result to start at (0 == first result)
     * @return array  A data structure containing results looking like ...
     *         $results = array(
     *               count   => integer, // total number of results
     *               limit   => integer, // how many results are returned
     *               offset  => integer, // starting from which result
     *               data    => array(   // the result records
     *                   array(
     *                       id            => integer,
     *                       username      => string,
     *                       institution   => string,
     *                       firstname     => string,
     *                       lastname      => string,
     *                       preferredname => string,
     *                       email         => string,
     *                   ),
     *                   array(
     *                       id            => integer,
     *                       username      => string,
     *                       institution   => string,
     *                       firstname     => string,
     *                       lastname      => string,
     *                       preferredname => string,
     *                       email         => string,
     *                   ),
     *                   array(...),
     *               ),
     *           );
     */
    public static function search_group($query_string, $limit, $offset = 0) {
        global $USER;
        if ( is_postgres() ) {
            $data = get_records_sql_array("
                SELECT
                    id, name, owner, description, ctime, mtime
                FROM
                    " . get_config('dbprefix') . "usr_group u
                WHERE
                    owner = ?
                    AND (
                        name ILIKE '%' || ? || '%' 
                        OR description ILIKE '%' || ? || '%' 
                    )
                ",
                array($USER->get('id'), $query_string, $query_string),
                $offset,
                $limit
            );

            $count = get_field_sql("
                SELECT
                    COUNT(*)
                FROM
                    " . get_config('dbprefix') . "usr_group u
                WHERE
                    owner = ?
                    AND (
                        name ILIKE '%' || ? || '%' 
                        OR description ILIKE '%' || ? || '%' 
                    )
            ",
Nigel McNie's avatar
Nigel McNie committed
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
                array($USER->get('id'), $query_string, $query_string)
            );
        }
        // TODO
        // else if ( is_mysql() ) {
        // }
        else {
            throw new SQLException('search_group() is not implemented for your database engine (' . get_config('dbtype') . ')');
        }

        return array(
            'count'   => $count,
            'limit'   => $limit,
            'offset'  => $offset,
            'data'    => $data,
        );
    }
    
    /**
     * Implement community searching with SQL
     *
     * @param string  The query string
     * @param integer How many results to return
     * @param integer What result to start at (0 == first result)
     * @return array  A data structure containing results looking like ...
     *         $results = array(
     *               count   => integer, // total number of results
     *               limit   => integer, // how many results are returned
     *               offset  => integer, // starting from which result
     *               data    => array(   // the result records
     *                   array(
     *                       id            => integer,
     *                       username      => string,
     *                       institution   => string,
     *                       firstname     => string,
     *                       lastname      => string,
     *                       preferredname => string,
     *                       email         => string,
     *                   ),
     *                   array(
     *                       id            => integer,
     *                       username      => string,
     *                       institution   => string,
     *                       firstname     => string,
     *                       lastname      => string,
     *                       preferredname => string,
     *                       email         => string,
     *                   ),
     *                   array(...),
     *               ),
     *           );
     */
266
    public static function search_community($query_string, $limit, $offset=0, $all=false) {
Nigel McNie's avatar
Nigel McNie committed
267
268
        global $USER;
        if ( is_postgres() ) {
269
            $sql = "
Nigel McNie's avatar
Nigel McNie committed
270
271
272
273
                SELECT
                    id, name, description, jointype, owner, ctime, mtime
                FROM
                    " . get_config('dbprefix') . "community
274
                WHERE (
Nigel McNie's avatar
Nigel McNie committed
275
276
                    name ILIKE '%' || ? || '%' 
                    OR description ILIKE '%' || ? || '%' 
277
278
279
280
                )";
            $values = array($query_string, $query_string);
            if (!$all) {
                $sql .=  "AND ( 
281
282
283
                    owner = ? OR id IN (
                        SELECT community FROM " . get_config('dbprefix') . "community_member WHERE member = ?
                    )
284
285
286
287
288
                )";
                $values[] = $USER->get('id');
                $values[] = $USER->get('id');
            }
            $data = get_records_sql_array($sql, $values, $offset, $limit);
Nigel McNie's avatar
Nigel McNie committed
289

290
            $sql = "
Nigel McNie's avatar
Nigel McNie committed
291
292
293
                SELECT
                    COUNT(*)
                FROM
Martyn Smith's avatar
Martyn Smith committed
294
                    " . get_config('dbprefix') . "community u
295
296
297
                WHERE (
                    name ILIKE '%' || ? || '%' 
                    OR description ILIKE '%' || ? || '%' 
298
299
300
301
302
303
                )";
            if (!$all) {
                $sql .= "AND ( 
                        owner = ? OR id IN (
                            SELECT community FROM " . get_config('dbprefix') . "community_member WHERE member = ?
                        )
Nigel McNie's avatar
Nigel McNie committed
304
                    )
305
306
307
                ";
            }
            $count = get_field_sql($sql, $values);
Nigel McNie's avatar
Nigel McNie committed
308
309
310
311
312
        }
        // TODO
        // else if ( is_mysql() ) {
        // }
        else {
Nigel McNie's avatar
Nigel McNie committed
313
            throw new SQLException('search_community() is not implemented for your database engine (' . get_config('dbtype') . ')');
Martyn Smith's avatar
Martyn Smith committed
314
315
316
317
318
319
        }

        return array(
            'count'   => $count,
            'limit'   => $limit,
            'offset'  => $offset,
320
            'data'    => $data,
Martyn Smith's avatar
Martyn Smith committed
321
322
323
324
325
        );
    }
}

?>