mygroups.php 6.46 KB
Newer Older
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
<?php
/**
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
 * Copyright (C) 2006-2007 Catalyst IT Ltd (http://www.catalyst.net.nz)
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 * @package    mahara
 * @subpackage core
 * @author     Clare Lenihan <clare@catalyst.net.nz>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
 * @copyright  (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
 *
 */

define('INTERNAL', 1);
define('MENUITEM', 'groups/mygroups');
require(dirname(dirname(__FILE__)) . '/init.php');
require_once('pieforms/pieform.php');
define('TITLE', get_string('mygroups'));
32
33
34
define('SECTION_PLUGINTYPE', 'core');
define('SECTION_PLUGINNAME', 'group');
define('SECTION_PAGE', 'mygroups');
35
36
37
38
39
40
41
require('group.php');
$filter = param_alpha('filter', 'all');
$offset = param_integer('offset', 'all');

$groupsperpage = 20;
$offset = (int)($offset / $groupsperpage) * $groupsperpage;

Clare Lenihan's avatar
Clare Lenihan committed
42
// different filters join on the different kinds of association
43
if ($filter == 'owner') {
44
45
    $sql = '
        INNER JOIN (
46
            SELECT g.id, CAST(\'owner\' AS VARCHAR(7)) AS type
47
48
49
50
51
52
53
54
            FROM {group} g
            WHERE g.owner = ?
        ) t ON t.id = g.id';
    $values = array($USER->get('id'));
}
else if ($filter == 'member') {
    $sql = '
        INNER JOIN (
55
            SELECT g.id, CAST(\'owner\' AS VARCHAR(7)) AS type
56
57
            FROM {group} g
            WHERE g.owner = ?
58
            UNION SELECT g.id, CAST(\'member\' AS VARCHAR(7)) AS type
59
60
61
62
63
64
65
66
67
            FROM {group} g
            INNER JOIN {group_member} gm ON (g.id = gm.group AND gm.member = ?)
            WHERE g.owner != gm.member
        ) t ON t.id = g.id';
    $values = array($USER->get('id'), $USER->get('id'));
}
else if ($filter == 'invite') {
    $sql = '
        INNER JOIN (
68
            SELECT g.id, CAST(\'invite\' AS VARCHAR(7)) AS type
69
70
71
72
73
74
75
76
            FROM {group} g
            INNER JOIN {group_member_invite} gmi ON (gmi.group = g.id AND gmi.member = ?)
        ) t ON t.id = g.id';
    $values = array($USER->get('id'));
}
else if ($filter == 'request') {
    $sql = '
        INNER JOIN (
77
            SELECT g.id, CAST(\'request\' AS VARCHAR(7)) AS type
78
79
80
81
82
            FROM {group} g
            INNER JOIN {group_member_request} gmr ON (gmr.group = g.id AND gmr.member = ?)
        ) t ON t.id = g.id';
    $values = array($USER->get('id'));
}
83
84
85
86
else { // all or some other text
    $filter = 'all';
    $sql = '
        INNER JOIN (
87
            SELECT g.id, CAST(\'owner\' AS VARCHAR(7)) AS type
88
89
            FROM {group} g
            WHERE g.owner = ?
90
            UNION SELECT g.id, CAST(\'member\' AS VARCHAR(7)) AS type
91
92
93
            FROM {group} g
            INNER JOIN {group_member} gm ON (g.id = gm.group AND gm.member = ?)
            WHERE g.owner != gm.member
94
            UNION SELECT g.id, CAST(\'invite\' AS VARCHAR(7)) AS type
95
96
            FROM {group} g
            INNER JOIN {group_member_invite} gmi ON (gmi.group = g.id AND gmi.member = ?)
97
            UNION SELECT g.id, CAST(\'request\' AS VARCHAR(7)) AS type
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
            FROM {group} g
            INNER JOIN {group_member_request} gmr ON (gmr.group = g.id AND gmr.member = ?)
        ) t ON t.id = g.id';
    $values = array($USER->get('id'), $USER->get('id'), $USER->get('id'), $USER->get('id'));
}

$form = pieform(array(
    'name' => 'filter',
    'method' => 'post',
    'renderer' => 'oneline',
    'elements' => array(
        'options' => array(
            'type' => 'select',
            'options' => array(
                'all' => get_string('allmygroups', 'group'),
                'owner' => get_string('groupsiown', 'group'),
                'member' => get_string('groupsimin', 'group'),
                'invite' => get_string('groupsiminvitedto', 'group'),
                'request' => get_string('groupsiwanttojoin', 'group')
            ),
            'defaultvalue' => $filter
        ),
        'submit' => array(
            'type' => 'submit',
            'value' => get_string('filter')
        )
    ),
));
126

127
128
$values[] = 0;

129
$count = count_records_sql('SELECT COUNT(*) FROM {group} g ' . $sql . ' WHERE g.deleted = ?', $values);
130

Clare Lenihan's avatar
Clare Lenihan committed
131
// almost the same as query used in find - common parts should probably be pulled out
132
// gets the groups filtered by above
Clare Lenihan's avatar
Clare Lenihan committed
133
134
135
// including type if the user is associated with the group in some way
// and the first three members by id

136
137
138
139
$sql = 'SELECT g.id, g.name, g.description, g.owner, g.jointype, t.type, COUNT(gm.member) AS membercount, COUNT(gmr.member) AS requests,
	(SELECT gm.member FROM {group_member} gm WHERE gm.group = g.id ORDER BY member LIMIT 1) AS member1,
	(SELECT gm.member FROM {group_member} gm WHERE gm.group = g.id ORDER BY member LIMIT 1 OFFSET 1) AS member2,
	(SELECT gm.member FROM {group_member} gm WHERE gm.group = g.id ORDER BY member LIMIT 1 OFFSET 2) AS member3
140
    FROM {group} g
141
    LEFT JOIN {group_member} gm ON (gm.group = g.id)
142
143
    LEFT JOIN {group_member_request} gmr ON (gmr.group = g.id)' .
    $sql . '
144
    WHERE g.deleted = ?
145
    GROUP BY 1, 2, 3, 4, 5, 6, 9, 10
146
147
148
149
150
    ORDER BY g.name';

$groups = get_records_sql_array($sql, $values, $offset, $groupsperpage);

$pagination = build_pagination(array(
Clare Lenihan's avatar
Clare Lenihan committed
151
    'url' => get_config('wwwroot') . 'group/mygroups.php?filter=' . $filter,
152
153
    'count' => $count,
    'limit' => $groupsperpage,
154
155
156
    'offset' => $offset,
    'resultcounttextsingular' => get_string('group', 'group'),
    'resultcounttextplural' => get_string('groups', 'group'),
157
158
159
160
161
162
163
164
165
));

setup_groups($groups, 'mygroups');

$smarty = smarty();
$smarty->assign('groups', $groups);
$smarty->assign('form', $form);
$smarty->assign('filter', $filter);
$smarty->assign('pagination', $pagination['html']);
166
$smarty->assign('searchingforgroups', array('<a href="' . get_config('wwwroot') . 'group/find.php">', '</a>'));
167
$smarty->assign('heading', get_string('mygroups'));
168
169
170
171
172
173
174
$smarty->display('group/mygroups.tpl');

function filter_submit(Pieform $form, $values) {
    redirect('/group/mygroups.php?filter=' . $values['options']);
}

?>