index.php 8.83 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 core
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
 *
 */

define('INTERNAL', 1);
28
define('MENUITEM', 'settings/preferences');
29
30
31
define('SECTION_PLUGINTYPE', 'core');
define('SECTION_PLUGINNAME', 'account');
define('SECTION_PAGE', 'preferences');
32
33

require(dirname(dirname(__FILE__)) . '/init.php');
34
define('TITLE', get_string('preferences'));
35
require_once('pieforms/pieform.php');
36

Penny Leach's avatar
Penny Leach committed
37
// load up user preferences
38
$prefs = (object)($USER->accountprefs);
Penny Leach's avatar
Penny Leach committed
39

40
$authobj = AuthFactory::create($USER->authinstance);
41
42

// @todo auth preference for a password change screen for all auth methods other than internal
43
if (method_exists($authobj, 'change_password')) {
44
45
    $elements = array(
        'changepassworddesc' => array(
46
            'value' => '<tr><td colspan="2"><h3>' . get_string('changepassworddesc', 'account') . '</h3></td></tr>'
47
48
49
        ),
        'oldpassword' => array( 'type' => 'password',
            'title' => get_string('oldpassword'),
50
            'help'  => true,
51
            'autocomplete' => 'off',
52
53
54
55
56
57
58
59
60
61
62
        ),
        'password1' => array(
            'type' => 'password',
            'title' => get_string('newpassword'),
        ),
        'password2' => array(
            'type' => 'password',
            'title' => get_string('confirmpassword')
        ),
    );
}
63
else if ($url = get_config_plugin_instance('auth', $USER->authinstance, 'changepasswordurl')) {
64
    // @todo contextual help
65
66
    $elements = array(
        'changepasswordotherinterface' => array(
67
            'value' => '<tr><td colspan="2"><h3>' . get_string('changepasswordotherinterface', 'account', $url) . '</h3></td></tr>'
68
69
70
71
72
73
74
        )
    );
}
else {
    $elements = array();
}

75
76
if ($authobj->authname == 'internal') {
    $elements['changeusernameheading'] = array(
77
        'value' => '<tr><td colspan="2"><h3>' . get_string('changeusernameheading', 'account') . '</h3></td></tr>'
78
79
80
81
82
    );
    $elements['username'] = array(
        'type' => 'text',
        'defaultvalue' => $USER->get('username'),
        'title' => get_string('changeusername', 'account'),
83
        'description' => get_string('changeusernamedesc', 'account', hsc(get_config('sitename'))),
84
85
86
    );
}

87
$elements['accountoptionsdesc'] = array(
88
    'value' => '<tr><td colspan="2"><h3>' . get_string('accountoptionsdesc', 'account') . '</h3></td></tr>'
89
90
91
92
93
);
$elements['friendscontrol'] = array(
    'type' => 'radio',
    'defaultvalue' => $prefs->friendscontrol, 
    'title'  => get_string('friendsdescr', 'account'),
94
    'separator' => '<br>',
95
96
97
98
99
100
101
102
103
104
105
    'options' => array(
        'nobody' => get_string('friendsnobody', 'account'),
        'auth'   => get_string('friendsauth', 'account'),
        'auto'   => get_string('friendsauto', 'account')
    ),
    'help' => true
);
$elements['wysiwyg'] = array(
    'type' => 'radio',
    'defaultvalue' => $prefs->wysiwyg,
    'title' => get_string('wysiwygdescr', 'account'),
106
    'separator' => '<br>',
107
108
109
110
    'options' => array(
        1 => get_string('on', 'account'),
        0 => get_string('off', 'account'),
    ),
111
    'help' => true,
112
113
114
115
116
);
$elements['messages'] = array(
    'type' => 'radio',
    'defaultvalue' => $prefs->messages,
    'title' => get_string('messagesdescr', 'account'),
117
    'separator' => '<br>',
118
119
120
121
122
    'options' => array(
        'nobody' => get_string('messagesnobody', 'account'),
        'friends' => get_string('messagesfriends', 'account'),
        'allow' => get_string('messagesallow', 'account'),
    ),
123
    'help' => true,
124
);
125
$languages = get_languages();
126
127
128
129
$elements['lang'] = array(
    'type' => 'select',
    'defaultvalue' => $prefs->lang,
    'title' => get_string('language', 'account'),
130
    'options' => array_merge(array('default' => get_string('sitedefault', 'admin') . ' (' . $languages[get_config('lang')] . ')'), $languages),
131
    'help' => true,
132
    'ignore' => count($languages) < 2,
133
134
135
136
137
138
139
140
141
);
$elements['addremovecolumns'] = array(
    'type' => 'radio',
    'options' => array(
        1 => get_string('on', 'account'),
        0 => get_string('off', 'account'),
    ),
    'defaultvalue' => $prefs->addremovecolumns,
    'title' => get_string('showviewcolumns', 'account'),
142
    'separator' => '<br>',
143
144
    'help' => 'true'
);
145
146
147
148
149
150
151
152
153
154
if (get_config('showtagssideblock')) {
    $elements['tagssideblockmaxtags'] = array(
        'type'         => 'text',
        'size'         => 4,
        'title'        => get_string('tagssideblockmaxtags', 'account'),
        'description'  => get_string('tagssideblockmaxtagsdescription', 'account'),
        'defaultvalue' => isset($prefs->tagssideblockmaxtags) ? $prefs->tagssideblockmaxtags : get_config('tagssideblockmaxtags'),
        'rules'        => array('integer' => true, 'minvalue' => 0, 'maxvalue' => 1000),
    );
}
155
156
157
158
159
$elements['submit'] = array(
    'type' => 'submit',
    'value' => get_string('save')
);

Penny Leach's avatar
Penny Leach committed
160
161
$prefsform = array(
    'name'        => 'accountprefs',
162
    'renderer'    => 'table',
Penny Leach's avatar
Penny Leach committed
163
    'method'      => 'post',
164
    'jsform'      => true,
165
    'plugintype'  => 'core',
Penny Leach's avatar
Penny Leach committed
166
    'pluginname'  => 'account',
167
    'jssuccesscallback' => 'clearPasswords',
168
    'elements'    => $elements
Penny Leach's avatar
Penny Leach committed
169
);
170

171
function accountprefs_validate(Pieform $form, $values) {
172
173
174
175
    global $USER;

    $authobj = AuthFactory::create($USER->authinstance);

176
177
178
179
180
181
182
183
184
185
186
    if (isset($values['oldpassword'])) {
        if ($values['oldpassword'] !== '') {
            global $USER, $authtype, $authclass;
            if (!$authobj->authenticate_user_account($USER, $values['oldpassword'])) {
                $form->set_error('oldpassword', get_string('oldpasswordincorrect', 'account'));
                return;
            }
            password_validate($form, $values, $USER);
        }
        else if ($values['password1'] !== '' || $values['password2'] !== '') {
            $form->set_error('oldpassword', get_string('mustspecifyoldpassword'));
187
188
        }
    }
189
190
191
192
193
194
195
196
197

    if ($authobj->authname == 'internal' && $values['username'] != $USER->get('username')) {
        if (!AuthInternal::is_username_valid($values['username'])) {
            $form->set_error('username', get_string('usernameinvalidform', 'auth.internal'));
        }
        if (!$form->get_error('username') && record_exists_select('usr', 'LOWER(username) = ?', strtolower($values['username']))) {
            $form->set_error('username', get_string('usernamealreadytaken', 'auth.internal'));
        }
    }
198
199
}

200
function accountprefs_submit(Pieform $form, $values) {
201
    global $USER;
202

203
204
    $authobj = AuthFactory::create($USER->authinstance);

205
    db_begin();
206
    if (isset($values['password1']) && $values['password1'] !== '') {
207
        global $authclass;
208
209
210
211
        $password = $authobj->change_password($USER, $values['password1']);
        $USER->password = $password;
        $USER->passwordchange = 0;
        $USER->commit();
212
213
    }

214
215
    // use this as looping through values is not safe.
    $expectedprefs = expected_account_preferences(); 
Penny Leach's avatar
Penny Leach committed
216
    foreach (array_keys($expectedprefs) as $pref) {
217
218
219
        if (isset($values[$pref])) {
            $USER->set_account_preference($pref, $values[$pref]);
        }
220
    }
221

222
223
    $returndata = array();

224
225
226
    if (isset($values['username']) && $values['username'] != $USER->get('username')) {
        $USER->username = $values['username'];
        $USER->commit();
227
        $returndata['username'] = $values['username'];
228
229
    }

230
    db_commit();
231
232
    $returndata['message'] = get_string('prefssaved', 'account');
    $form->json_reply(PIEFORM_OK, $returndata);
Penny Leach's avatar
Penny Leach committed
233
234
235
}


236

Richard Mansfield's avatar
Richard Mansfield committed
237
$prefsform = pieform($prefsform);
238
239

$smarty = smarty();
Richard Mansfield's avatar
Richard Mansfield committed
240
$smarty->assign('form', $prefsform);
241
242
243
$smarty->assign('INLINEJAVASCRIPT', "
function clearPasswords(form, data) {
    formSuccess(form, data);
244
245
246
247
248
    if ($('accountprefs_oldpassword')) {
        $('accountprefs_oldpassword').value = '';
        $('accountprefs_password1').value = '';
        $('accountprefs_password2').value = '';
    }
249
250
251
252
253
254
    if (data.username) {
        var username = getFirstElementByTagAndClassName('a', null, 'profile-sideblock-username');
        replaceChildNodes(username, data.username);
    }
}
");
255
$smarty->assign('PAGEHEADING', hsc(get_string('preferences')));
256
257
258
$smarty->display('account/index.tpl');


259
?>