index.php 6.24 KB
Newer Older
1 2
<?php
/**
Francois Marier's avatar
Francois Marier committed
3 4
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
 * Copyright (C) 2006-2007 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 21 22 23 24 25 26 27
 *
 * @package    mahara
 * @subpackage core
 * @author     Penny Leach <penny@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);
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
require_once('pieforms/pieform.php');
35

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

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

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

$elements['accountoptionsdesc'] = array(
74
    'value' => '<tr><td colspan="2"><p>' . get_string('accountoptionsdesc', 'account') . '</p></td></tr>'
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
);
$elements['friendscontrol'] = array(
    'type' => 'radio',
    'defaultvalue' => $prefs->friendscontrol, 
    'title'  => get_string('friendsdescr', 'account'),
    'separator' => HTML_BR,
    'options' => array(
        'nobody' => get_string('friendsnobody', 'account'),
        'auth'   => get_string('friendsauth', 'account'),
        'auto'   => get_string('friendsauto', 'account')
    ),
   'rules' => array(
        'required' => true
    ),
    'help' => true
);
$elements['wysiwyg'] = array(
    'type' => 'radio',
    'defaultvalue' => $prefs->wysiwyg,
    'title' => get_string('wysiwygdescr', 'account'),
    'options' => array(
        1 => get_string('on', 'account'),
        0 => get_string('off', 'account'),
    ),
   'rules' => array(
        'required' => true
101 102
    ),
    'help' => true,
103 104 105 106 107 108 109 110 111 112 113 114 115
);
$elements['messages'] = array(
    'type' => 'radio',
    'defaultvalue' => $prefs->messages,
    'title' => get_string('messagesdescr', 'account'),
    'separator' => HTML_BR,
    'options' => array(
        'nobody' => get_string('messagesnobody', 'account'),
        'friends' => get_string('messagesfriends', 'account'),
        'allow' => get_string('messagesallow', 'account'),
    ),
   'rules' => array(
       'required' => true
116 117
    ),
    'help' => true,
118 119 120 121 122
);
$elements['lang'] = array(
    'type' => 'select',
    'defaultvalue' => $prefs->lang,
    'title' => get_string('language', 'account'),
123
    'options' => array_merge(array('default' => get_string('sitedefault', 'admin')), get_languages()),
124 125
    'rules' => array(
        'required' => true
126 127
    ),
    'help' => true,
128 129 130 131 132 133
);                        
$elements['submit'] = array(
    'type' => 'submit',
    'value' => get_string('save')
);

Penny Leach's avatar
Penny Leach committed
134 135 136
$prefsform = array(
    'name'        => 'accountprefs',
    'method'      => 'post',
137
    'jsform'      => true,
138
    'plugintype'  => 'core',
Penny Leach's avatar
Penny Leach committed
139
    'pluginname'  => 'account',
140
    'jssuccesscallback' => 'clearPasswords',
141
    'elements'    => $elements
Penny Leach's avatar
Penny Leach committed
142
);
143

Penny Leach's avatar
Penny Leach committed
144 145

$smarty = smarty();
146
$smarty->assign('form', pieform($prefsform));
147
$smarty->assign('INLINEJAVASCRIPT', "
148 149
function clearPasswords(form, data) {
    formSuccess(form, data);
150 151 152
    $('accountprefs_oldpassword').value = '';
    $('accountprefs_password1').value = '';
    $('accountprefs_password2').value = '';
153
}");
154 155
$smarty->display('account/index.tpl');

156
function accountprefs_validate(Pieform $form, $values) {
157 158 159 160
    global $USER;

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

161
    if ($values['oldpassword'] !== '') {
162
        global $USER, $authtype, $authclass;
163
        if (!$authobj->authenticate_user_account($USER, $values['oldpassword'])) {
164 165 166
            $form->set_error('oldpassword', get_string('oldpasswordincorrect', 'account'));
            return;
        }
167
        password_validate($form, $values, $USER);
168 169 170 171 172 173
    }
    else if ($values['password1'] !== '' || $values['password2'] !== '') {
        $form->set_error('oldpassword', get_string('mustspecifyoldpassword'));
    }
}

174
function accountprefs_submit(Pieform $form, $values) {
175
    global $USER;
176

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

179 180 181
    db_begin();
    if ($values['password1'] !== '') {
        global $authclass;
182 183 184 185
        $password = $authobj->change_password($USER, $values['password1']);
        $USER->password = $password;
        $USER->passwordchange = 0;
        $USER->commit();
186 187
    }

188 189
    // use this as looping through values is not safe.
    $expectedprefs = expected_account_preferences(); 
Penny Leach's avatar
Penny Leach committed
190
    foreach (array_keys($expectedprefs) as $pref) {
191
        $USER->set_account_preference($pref, $values[$pref]);
192
    }
193 194

    db_commit();
195
    $form->json_reply(PIEFORM_OK, get_string('prefssaved', 'account'));
Penny Leach's avatar
Penny Leach committed
196 197 198
}


199
?>