index.php 9.99 KB
Newer Older
1 2
<?php
/**
Francois Marier's avatar
Francois Marier committed
3
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
4 5
 * Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
 *                         http://wiki.mahara.org/Contributors
6
 *
Francois Marier's avatar
Francois Marier committed
7 8 9 10
 * 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.
11
 *
Francois Marier's avatar
Francois Marier committed
12 13 14 15
 * 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.
16
 *
Francois Marier's avatar
Francois Marier committed
17 18
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 20 21
 *
 * @package    mahara
 * @subpackage core
22
 * @author     Catalyst IT Ltd
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
24
 * @copyright  (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
25 26 27 28
 *
 */

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

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

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

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

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

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

88
$elements['accountoptionsdesc'] = array(
89
    'value' => '<tr><td colspan="2"><h3>' . get_string('accountoptionsdesc', 'account') . '</h3></td></tr>'
90 91 92 93 94
);
$elements['friendscontrol'] = array(
    'type' => 'radio',
    'defaultvalue' => $prefs->friendscontrol, 
    'title'  => get_string('friendsdescr', 'account'),
95
    'separator' => '<br>',
96 97 98 99 100 101 102 103 104 105 106
    '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'),
107
    'separator' => '<br>',
108 109 110 111
    'options' => array(
        1 => get_string('on', 'account'),
        0 => get_string('off', 'account'),
    ),
112
    'help' => true,
113
);
114 115 116 117 118 119 120 121 122 123 124
$elements['maildisabled'] = array(
    'type' => 'radio',
    'defaultvalue' => get_account_preference($USER->get('id'), 'maildisabled'),
    'title' => get_string('email'),
    'separator' => '<br>',
    'options' => array(
        0 => get_string('enabled', 'account'),
        1 => get_string('disabled', 'account'),
    ),
    'help' => true,
);
125 126 127 128
$elements['messages'] = array(
    'type' => 'radio',
    'defaultvalue' => $prefs->messages,
    'title' => get_string('messagesdescr', 'account'),
129
    'separator' => '<br>',
130 131 132 133 134
    'options' => array(
        'nobody' => get_string('messagesnobody', 'account'),
        'friends' => get_string('messagesfriends', 'account'),
        'allow' => get_string('messagesallow', 'account'),
    ),
135
    'help' => true,
136
);
137
$languages = get_languages();
138 139 140 141
$elements['lang'] = array(
    'type' => 'select',
    'defaultvalue' => $prefs->lang,
    'title' => get_string('language', 'account'),
142
    'options' => array_merge(array('default' => get_string('sitedefault', 'admin') . ' (' . $languages[get_config('lang')] . ')'), $languages),
143
    'help' => true,
144
    'ignore' => count($languages) < 2,
145 146 147 148 149 150 151 152 153
);
$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'),
154
    'separator' => '<br>',
155 156
    'help' => 'true'
);
157 158 159 160 161 162 163 164 165 166
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),
    );
}
167 168 169 170 171 172 173 174
if (get_config('userscanhiderealnames')) {
    $elements['hiderealname'] = array(
        'type'         => 'checkbox',
        'title'        => get_string('hiderealname', 'account'),
        'description'  => get_string('hiderealnamedescription', 'account'),
        'defaultvalue' => $prefs->hiderealname,
    );
}
175 176 177 178 179
$elements['submit'] = array(
    'type' => 'submit',
    'value' => get_string('save')
);

Penny Leach's avatar
Penny Leach committed
180 181
$prefsform = array(
    'name'        => 'accountprefs',
182
    'renderer'    => 'table',
Penny Leach's avatar
Penny Leach committed
183
    'method'      => 'post',
184
    'jsform'      => true,
185
    'plugintype'  => 'core',
Penny Leach's avatar
Penny Leach committed
186
    'pluginname'  => 'account',
187
    'jssuccesscallback' => 'clearPasswords',
188
    'elements'    => $elements
Penny Leach's avatar
Penny Leach committed
189
);
190

191
function accountprefs_validate(Pieform $form, $values) {
192 193 194 195
    global $USER;

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

196 197 198 199 200 201 202 203 204 205 206
    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'));
207 208
        }
    }
209 210 211 212 213 214 215 216 217

    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'));
        }
    }
218 219
}

220
function accountprefs_submit(Pieform $form, $values) {
221
    global $USER;
222

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

225
    db_begin();
226
    if (isset($values['password1']) && $values['password1'] !== '') {
227
        global $authclass;
228 229 230 231
        $password = $authobj->change_password($USER, $values['password1']);
        $USER->password = $password;
        $USER->passwordchange = 0;
        $USER->commit();
232 233
    }

234 235
    // use this as looping through values is not safe.
    $expectedprefs = expected_account_preferences(); 
236 237 238 239 240 241 242 243 244 245
    if ($values['maildisabled'] == 0 && get_account_preference($USER->get('id'), 'maildisabled') == 1) {
        // Reset the sent and bounce counts otherwise mail will be disabled
        // on the next send attempt
        $u = new StdClass;
        $u->email = $USER->get('email');
        $u->id = $USER->get('id');
        update_bounce_count($u,true);
        update_send_count($u,true);
    }

Penny Leach's avatar
Penny Leach committed
246
    foreach (array_keys($expectedprefs) as $pref) {
247 248 249
        if (isset($values[$pref])) {
            $USER->set_account_preference($pref, $values[$pref]);
        }
250
    }
251

252 253
    $returndata = array();

254 255 256
    if (isset($values['username']) && $values['username'] != $USER->get('username')) {
        $USER->username = $values['username'];
        $USER->commit();
257
        $returndata['username'] = $values['username'];
258 259
    }

260
    db_commit();
261 262
    $returndata['message'] = get_string('prefssaved', 'account');
    $form->json_reply(PIEFORM_OK, $returndata);
Penny Leach's avatar
Penny Leach committed
263 264 265
}


266

Richard Mansfield's avatar
Richard Mansfield committed
267
$prefsform = pieform($prefsform);
268 269

$smarty = smarty();
Richard Mansfield's avatar
Richard Mansfield committed
270
$smarty->assign('form', $prefsform);
271
$smarty->assign('candeleteself', $USER->can_delete_self());
272 273 274
$smarty->assign('INLINEJAVASCRIPT', "
function clearPasswords(form, data) {
    formSuccess(form, data);
275 276 277 278 279
    if ($('accountprefs_oldpassword')) {
        $('accountprefs_oldpassword').value = '';
        $('accountprefs_password1').value = '';
        $('accountprefs_password2').value = '';
    }
280 281 282 283 284 285
    if (data.username) {
        var username = getFirstElementByTagAndClassName('a', null, 'profile-sideblock-username');
        replaceChildNodes(username, data.username);
    }
}
");
286
$smarty->assign('PAGEHEADING', hsc(get_string('preferences')));
287 288 289
$smarty->display('account/index.tpl');


290
?>