~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/media/settings/settings.js

  • Committer: William Grant
  • Date: 2009-01-22 04:47:42 UTC
  • mfrom: (1080.1.93 storm)
  • Revision ID: grantw@unimelb.edu.au-20090122044742-sa8gnww0ma2bm2rv
Merge Storm branch. ivle.db is dead. Watch out for the schema change.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* IVLE - Informatics Virtual Learning Environment
 
2
 * Copyright (C) 2007-2008 The University of Melbourne
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2 of the License, or
 
7
 * (at your option) any later version.
 
8
 *
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License
 
15
 * along with this program; if not, write to the Free Software
 
16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
17
 *
 
18
 * Module: Settings (Client-side JavaScript)
 
19
 * Author: Matt Giuca
 
20
 * Date: 25/2/2008
 
21
 */
 
22
 
 
23
var user_data;
 
24
 
 
25
/* Fetch the user's details from the server, and populate the page.
 
26
 * Returns false. */
 
27
function revert_settings()
 
28
{
 
29
    var callback = function(xhr)
 
30
        {
 
31
            user = JSON.parse(xhr.responseText);
 
32
            populate(user);
 
33
        }
 
34
    /* Just get details for the logged in user */
 
35
    ajax_call(callback, "userservice", "get_user", {}, "GET");
 
36
    return false;
 
37
}
 
38
 
 
39
/* Populate the page with the given user's account details */
 
40
function populate(user)
 
41
{
 
42
    user_data = user;
 
43
    /* Plain text elements (non-editable) */
 
44
    var login = document.getElementById("login");
 
45
    var role = document.getElementById("role");
 
46
    var changepassword = document.getElementById("changepassword");
 
47
    var notices = document.getElementById("notices");
 
48
    /* Textbox (input) elements */
 
49
    var nick = document.getElementById("nick");
 
50
    var email = document.getElementById("email");
 
51
 
 
52
    var text;
 
53
    var p;
 
54
    var b;
 
55
    var table;
 
56
    var tbody;
 
57
    var tr;
 
58
    var td;
 
59
    var inputbox;
 
60
 
 
61
    /* Clear things */
 
62
    dom_removechildren(login);
 
63
    dom_removechildren(role);
 
64
    dom_removechildren(changepassword);
 
65
    dom_removechildren(notices);
 
66
 
 
67
    /* Construct the page */
 
68
 
 
69
    /* "login" : Full Name (<b>login</b> / studentid) */
 
70
    text = user.fullname + " (";
 
71
    login.appendChild(document.createTextNode(text));
 
72
    text = user.login
 
73
    b = document.createElement("b");
 
74
    b.appendChild(document.createTextNode(text));
 
75
    login.appendChild(b);
 
76
    if (user.studentid != null)
 
77
        text = " / " + user.studentid + ")"
 
78
    else
 
79
        text = ")"
 
80
    login.appendChild(document.createTextNode(text));
 
81
 
 
82
    /* "role" : <p>Your privilege level is <b>rolenm</b>.</p>
 
83
     * Unless rolenm is "student"
 
84
     */
 
85
    if (user.rolenm != "student")
 
86
    {
 
87
        text = "Your privilege level is ";
 
88
        role.appendChild(document.createTextNode(text));
 
89
        b = document.createElement("b");
 
90
        text = user.rolenm;
 
91
        b.appendChild(document.createTextNode(text));
 
92
        role.appendChild(b);
 
93
        text = ".";
 
94
        role.appendChild(document.createTextNode(text));
 
95
    }
 
96
 
 
97
    /* "nick" and "email" boxes */
 
98
    nick.value = user.nick;
 
99
    email.value = user.email;
 
100
 
 
101
    /* Password change box */
 
102
    /* (Only if this user has a local password) */
 
103
    if (user.local_password)
 
104
    {
 
105
        p = document.createElement("h3");
 
106
        p.appendChild(document.createTextNode("Change password"))
 
107
        changepassword.appendChild(p);
 
108
        p = document.createElement("p");
 
109
        
 
110
        p.appendChild(document.createTextNode("Please type your old password, "
 
111
                + "and new password twice, for verification."));
 
112
        changepassword.appendChild(p);
 
113
 
 
114
        table = document.createElement("table");
 
115
        tbody = document.createElement("tbody");
 
116
 
 
117
        tr = document.createElement("tr");
 
118
        td = document.createElement("td");
 
119
        td.appendChild(document.createTextNode("Old password:"))
 
120
        tr.appendChild(td);
 
121
        td = document.createElement("td");
 
122
        inputbox = document.createElement("input");
 
123
        inputbox.setAttribute("type", "password");
 
124
        inputbox.setAttribute("name", "oldpass");
 
125
        inputbox.setAttribute("id", "oldpass");
 
126
        inputbox.setAttribute("size", "40");
 
127
        td.appendChild(inputbox)
 
128
        tr.appendChild(td);
 
129
        tbody.appendChild(tr);
 
130
 
 
131
        tr = document.createElement("tr");
 
132
        td = document.createElement("td");
 
133
        td.appendChild(document.createTextNode("New password:"))
 
134
        tr.appendChild(td);
 
135
        td = document.createElement("td");
 
136
        inputbox = document.createElement("input");
 
137
        inputbox.setAttribute("type", "password");
 
138
        inputbox.setAttribute("name", "newpass");
 
139
        inputbox.setAttribute("id", "newpass");
 
140
        inputbox.setAttribute("size", "40");
 
141
        td.appendChild(inputbox)
 
142
        tr.appendChild(td);
 
143
        tbody.appendChild(tr);
 
144
 
 
145
        tr = document.createElement("tr");
 
146
        td = document.createElement("td");
 
147
        td.appendChild(document.createTextNode("Retype password:"))
 
148
        tr.appendChild(td);
 
149
        td = document.createElement("td");
 
150
        inputbox = document.createElement("input");
 
151
        inputbox.setAttribute("type", "password");
 
152
        inputbox.setAttribute("name", "repeatpass");
 
153
        inputbox.setAttribute("id", "repeatpass");
 
154
        inputbox.setAttribute("size", "40");
 
155
        td.appendChild(inputbox)
 
156
        tr.appendChild(td);
 
157
        tbody.appendChild(tr);
 
158
 
 
159
        table.appendChild(tbody);
 
160
        changepassword.appendChild(table);
 
161
    }
 
162
 
 
163
    if (user.pass_exp != null || user.acct_exp != null)
 
164
    {
 
165
        p = document.createElement("h3");
 
166
        text = "Notices";
 
167
        p.appendChild(document.createTextNode(text));
 
168
        notices.appendChild(p);
 
169
        if (user.pass_exp != null)
 
170
        {
 
171
            p = document.createElement("p");
 
172
            /* TODO: Nice passexp */
 
173
            var pass_exp = user.pass_exp.toString()
 
174
            text = "Your password will expire on " + pass_exp
 
175
                + ". You should change it before then to avoid having your "
 
176
                + "account disabled.";
 
177
            p.appendChild(document.createTextNode(text));
 
178
            notices.appendChild(p);
 
179
        }
 
180
        if (user.acct_exp != null)
 
181
        {
 
182
            p = document.createElement("p");
 
183
            /* TODO: Nice acct_exp */
 
184
            var acct_exp = user.acct_exp.toString()
 
185
            text = "Your IVLE account will expire on " + acct_exp + ".";
 
186
            p.appendChild(document.createTextNode(text));
 
187
            notices.appendChild(p);
 
188
        }
 
189
    }
 
190
}
 
191
 
 
192
/* Sets the "result" text.
 
193
 * iserror (bool) determines the styling.
 
194
 */
 
195
function set_result(text, iserror)
 
196
{
 
197
    var p = document.getElementById("result");
 
198
    dom_removechildren(p);
 
199
    p.appendChild(document.createTextNode(text));
 
200
    if (iserror)
 
201
        p.setAttribute("class", "error");
 
202
    else
 
203
        p.removeAttribute("class");
 
204
}
 
205
 
 
206
/* Writes the settings to the server.
 
207
 * Returns false. */
 
208
function save_settings()
 
209
{
 
210
    /* Button (input) elements */
 
211
    var save = document.getElementById("save");
 
212
    /* Textbox (input) elements */
 
213
    try
 
214
    {
 
215
        var oldpass = document.getElementById("oldpass");
 
216
        var newpass = document.getElementById("newpass");
 
217
        var repeatpass = document.getElementById("repeatpass");
 
218
    }
 
219
    catch (e)
 
220
    {
 
221
        var newpass = null;
 
222
        var repeatpass = null;
 
223
    }
 
224
    var nick = document.getElementById("nick");
 
225
    var email = document.getElementById("email");
 
226
 
 
227
    /* Check */
 
228
    oldpassval = oldpass == null ? null : oldpass.value;
 
229
    newpassval = newpass == null ? null : newpass.value;
 
230
    repeatpassval = repeatpass == null ? null : repeatpass.value;
 
231
    nickval = nick.value;
 
232
    emailval = email.value;
 
233
 
 
234
    /* Clear the password boxes, even if there are errors later */
 
235
    try
 
236
    {
 
237
        newpass.value = "";
 
238
        repeatpass.value = "";
 
239
    }
 
240
    catch (e)
 
241
    {
 
242
    }
 
243
 
 
244
    if (nickval == "")
 
245
    {
 
246
        set_result("Display name is empty.", true);
 
247
        return false;
 
248
    }
 
249
    if (newpassval != repeatpassval)
 
250
    {
 
251
        set_result("Passwords do not match.", true);
 
252
        return false;
 
253
    }
 
254
 
 
255
    /* Disable the heavy-duty supercolliding super button */
 
256
    save.setAttribute("disabled", "disabled");
 
257
    save.setAttribute("value", "Saving...");
 
258
    var callback = function(xhr)
 
259
    {
 
260
        save.removeAttribute("disabled");
 
261
        save.setAttribute("value", "Save");
 
262
 
 
263
        if (xhr.status == 200)
 
264
        {
 
265
            set_result("Successfully updated details.");
 
266
            user_data.nick = nickval;
 
267
            user_data.email = emailval;
 
268
            /* Now a little hack - update the user's nick display
 
269
             * in the heading bar, so they are sure it has been changed.
 
270
             */
 
271
            var usernick = document.getElementById("usernick");
 
272
            dom_removechildren(usernick);
 
273
            usernick.appendChild(document.createTextNode(nickval));
 
274
        }
 
275
        else if (xhr.getResponseHeader("X-IVLE-Action-Error"))
 
276
        {
 
277
            set_result(decodeURIComponent(xhr.getResponseHeader(
 
278
                                     "X-IVLE-Action-Error").toString()), true);        
 
279
        }
 
280
        else
 
281
        {
 
282
            set_result("There was a problem updating the details."
 
283
                + " Your changes have not been saved.", true);
 
284
        }
 
285
    }
 
286
    data = {
 
287
        "login": user_data.login,
 
288
        "nick": nickval,
 
289
        "email": emailval,
 
290
        "oldpass": oldpassval,
 
291
    }
 
292
    if (newpassval != null && newpassval != "")
 
293
        data['password'] = newpassval;
 
294
    ajax_call(callback, "userservice", "update_user", data, "POST");
 
295
    return false;
 
296
}