lib.php 4.36 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
38
39
40
41
42
43
44
45
46
47
 * Mahara database.
 */
class PluginSearchInternal extends PluginSearch {

    /**
     * Implement user 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
     *               results => array(   // the result records
     *                   array(
48
49
50
51
52
53
54
     *                       id            => integer,
     *                       username      => string,
     *                       institution   => string,
     *                       firstname     => string,
     *                       lastname      => string,
     *                       preferredname => string,
     *                       email         => string,
Martyn Smith's avatar
Martyn Smith committed
55
56
     *                   ),
     *                   array(
57
58
59
60
61
62
63
     *                       id            => integer,
     *                       username      => string,
     *                       institution   => string,
     *                       firstname     => string,
     *                       lastname      => string,
     *                       preferredname => string,
     *                       email         => string,
Martyn Smith's avatar
Martyn Smith committed
64
65
66
67
68
69
70
71
72
     *                   ),
     *                   array(...),
     *               ),
     *           );
     */
    public static function search_user($query_string, $limit, $offset = 0) {
        if ( is_postgres() ) {
            $data = get_rows_sql("
                SELECT
73
                    id, username, institution, firstname, lastname, preferredname, email
Martyn Smith's avatar
Martyn Smith committed
74
75
76
77
78
                FROM
                    " . get_config('dbprefix') . "usr u
                WHERE
                    firstname ILIKE '%' || ? || '%' 
                    OR lastname ILIKE '%' || ? || '%' 
79
                    OR preferredname ILIKE '%' || ? || '%' 
Martyn Smith's avatar
Martyn Smith committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
                    OR email ILIKE '%' || ? || '%' 
                ",
                array($query_string, $query_string, $query_string, $query_string),
                $offset,
                $limit
            );

            $count = get_field_sql("
                SELECT
                    COUNT(*)
                FROM
                    " . get_config('dbprefix') . "usr u
                WHERE
                    firstname ILIKE '%' || ? || '%' 
                    OR lastname ILIKE '%' || ? || '%' 
95
                    OR preferredname ILIKE '%' || ? || '%' 
Martyn Smith's avatar
Martyn Smith committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
                    OR email ILIKE '%' || ? || '%' 
            ",
                array($query_string, $query_string, $query_string, $query_string),
                $offset,
                $limit
            );
        }
        // TODO
        // else if ( is_mysql() ) {
        // }
        else {
            throw new DatalibException('search_user() is not implemented for your database engine (' . get_config('dbtype') . ')');
        }

        return array(
            'count'   => $count,
            'limit'   => $limit,
            'offset'  => $offset,
            'results' => $data,
        );
    }
}

?>