lib.php 8.42 KB
Newer Older
1
2
<?php
/**
Francois Marier's avatar
Francois Marier committed
3
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
4
 * Copyright (C) 2006-2008 Catalyst IT Ltd (http://www.catalyst.net.nz)
5
 *
Francois Marier's avatar
Francois Marier committed
6
7
8
9
 * 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.
10
 *
Francois Marier's avatar
Francois Marier committed
11
12
13
14
 * 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.
15
 *
Francois Marier's avatar
Francois Marier committed
16
17
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
20
 *
 * @package    mahara
 * @subpackage blocktype-profileinfo
21
 * @author     Catalyst IT Ltd
22
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
23
 * @copyright  (C) 2006-2008 Catalyst IT Ltd http://catalyst.net.nz
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 *
 */

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

class PluginBlocktypeProfileinfo extends PluginBlocktype {

    public static function get_title() {
        return get_string('title', 'blocktype.internal/profileinfo');
    }

    public static function get_description() {
        return get_string('description', 'blocktype.internal/profileinfo');
    }

    public static function get_categories() {
        return array('internal');
    }

43
44
45
46
    public static function get_viewtypes() {
        return array('portfolio', 'profile');
    }

47
    public static function render_instance(BlockInstance $instance) {
48
        require_once(get_config('docroot') . 'artefact/lib.php');
49
50
51
52
53
        $smarty = smarty_core();
        $configdata = $instance->get('configdata');

        $data = array();

54
55
56
57
58
        // add in the selected email address
        if (!empty($configdata['email'])) {
            $configdata['artefactids'][] = $configdata['email'];
        }

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
        // Get data about the profile fields in this blockinstance
        if (!empty($configdata['artefactids'])) {
            $artefactids = implode(', ', array_map('db_quote', $configdata['artefactids']));
            $profiledata = get_records_select_array('artefact',
                'id IN (' . $artefactids . ') AND owner = (SELECT owner FROM {view} WHERE id = ?)',
                array($instance->get('view')),
                '',
                'artefacttype, title'
            );

            foreach ($profiledata as $profilefield) {
                $data[$profilefield->artefacttype] = $profilefield->title;
            }
        }

        // Work out the path to the thumbnail for the profile image
        if (!empty($configdata['profileicon'])) {
76
77
            $downloadpath = get_config('wwwroot') . 'thumb.php?type=profileiconbyid&id=' . $configdata['profileicon'];
            $downloadpath .= '&maxwidth=80';
78
79
80
81
82
83
84
85
86
87
88
89
90
91
            $smarty->assign('profileiconpath', $downloadpath);
        }

        // Override the introduction text if the user has any for this 
        // particular blockinstance
        if (!empty($configdata['introtext'])) {
            $data['introduction'] = $configdata['introtext'];
        }

        $smarty->assign('profileinfo', $data);

        return $smarty->fetch('blocktype:profileinfo:content.tpl');
    }

92
93
94
95
96
97
98
99
    /**
     * Overrides the standard get_artefacts method to make sure the profileicon 
     * is added also.
     *
     * @param BlockInstance $instance The blockinstance to get artefacts for
     * @return array A list of artefact IDs in the blockinstance, or false if 
     *               there are none
     */
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
    public static function get_artefacts(BlockInstance $instance) {
        $configdata = $instance->get('configdata');
        $return = false;
        if (isset($configdata['artefactids'])) {
            $return = $configdata['artefactids'];
        }
        if (!empty($configdata['profileicon'])) {
            $return = array_merge((array)$return, array($configdata['profileicon']));
        }

        return $return;
    }

    public static function has_instance_config() {
        return true;
    }

    public static function instance_config_form($instance) {
        $configdata = $instance->get('configdata');

        $form = array();

        // Which fields does the user want
        $form[] = self::artefactchooser_element((isset($configdata['artefactids'])) ? $configdata['artefactids'] : null);

        // Profile icon
126
        if (!$result = get_records_sql_array('SELECT a.id, a.artefacttype, a.title, a.note
127
            FROM {artefact} a
128
            WHERE (artefacttype = \'profileicon\' OR artefacttype = \'email\')
129
130
131
132
133
            AND a.owner = (
                SELECT owner
                FROM {view}
                WHERE id = ?
            )
134
135
136
            ORDER BY a.id', array($instance->get('view')))) {
            $result = array();
        }
137

138
        $iconoptions = array(
139
140
            0 => get_string('dontshowprofileicon', 'blocktype.internal/profileinfo'),
        );
141
142
143
144
145
146
147
148
149
150
        $emailoptions = array(
            0 => get_string('dontshowemail', 'blocktype.internal/profileinfo'),
        );
        foreach ($result as $profilefield) {
            if ($profilefield->artefacttype == 'profileicon') {
                $iconoptions[$profilefield->id] = ($profilefield->title) ? $profilefield->title : $profilefield->note;
            } 
            else {
                $emailoptions[$profilefield->id] = $profilefield->title;
            }
151
152
153
154
155
        }

        $form['profileicon'] = array(
            'type'    => 'radio',
            'title'   => get_string('profileicon', 'artefact.internal'),
156
            'options' => $iconoptions,
157
158
159
160
            'defaultvalue' => (isset($configdata['profileicon'])) ? $configdata['profileicon'] : 0,
            'separator' => HTML_BR,
        );

161
162
163
164
165
166
167
168
        $form['email'] = array(
            'type'    => 'radio',
            'title'   => get_string('email', 'artefact.internal'),
            'options' => $emailoptions,
            'defaultvalue' => (isset($configdata['email'])) ? $configdata['email'] : 0,
            'separator' => HTML_BR,
        );

169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
        // Introduction
        $form['introtext'] = array(
            'type'    => 'tinywysiwyg',
            'title'   => get_string('introtext', 'blocktype.internal/profileinfo'),
            'description' => get_string('useintroductioninstead', 'blocktype.internal/profileinfo'),
            'defaultvalue' => (isset($configdata['introtext'])) ? $configdata['introtext'] : '',
            'width' => '100%',
            'height' => '150px',
        );

        return $form;
    }

    // TODO: make decision on whether this should be abstract or not
    public static function artefactchooser_element($default=null) {
        safe_require('artefact', 'internal');
        return array(
            'name'  => 'artefactids',
            'type'  => 'artefactchooser',
            'title' => get_string('fieldstoshow', 'blocktype.internal/profileinfo'),
            'defaultvalue' => $default,
            'blocktype' => 'profileinfo',
            'limit'     => 655360, // 640K profile fields is enough for anyone!
            'selectone' => false,
193
            'search'    => false,
194
            'artefacttypes' => array_diff(PluginArtefactInternal::get_artefact_types(), array('profileicon', 'email')),
195
196
197
198
199
200
201
202
203
204
205
206
            'template'  => 'artefact:internal:artefactchooser-element.tpl',
        );
    }

    /**
     * Deliberately enforce _no_ sort order. The database will return them in 
     * the order they were inserted, which means roughly the order that they 
     * are listed in the profile screen
     */
    public static function artefactchooser_get_sort_order() {
        return '';
    }
Richard Mansfield's avatar
Richard Mansfield committed
207

208
209
    public static function copy_allowed($ownertype=null) {
        return $ownertype == 'user';
Richard Mansfield's avatar
Richard Mansfield committed
210
211
    }

212
    public static function copy_artefacts_allowed($ownertype=null) {
Richard Mansfield's avatar
Richard Mansfield committed
213
214
215
        return false;
    }

216
217
218
219
220
221
222
223
224
225
226
227
228
229
    public static function default_artefact_config($ownertype=null, $ownerid=null, $configdata) {
        if ($ownertype == 'user') {
            $artefacttypes = array_diff(PluginArtefactInternal::get_artefact_types(), array('profileicon', 'email'));
            $artefactids = get_column_sql('
                SELECT id FROM {artefact}
                WHERE owner = ? AND artefacttype IN (' . join(',', array_map('db_quote', $artefacttypes)) . ')', array($ownerid));
            $configdata['artefactids'] = $artefactids;
        }
        else {
            $configdata['artefactids'] = array();
        }
        return $configdata;
    }

230
231
232
}

?>