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

553 by mattgiuca
Added new app: Settings (UI for userservice).
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
580 by mattgiuca
settings: Now able to handle the save button. This updates the user's details.
23
var user_data;
24
25
/* Fetch the user's details from the server, and populate the page.
26
 * Returns false. */
577 by mattgiuca
settings: Now populates all fields on the fly with the info from the DB,
27
function revert_settings()
560 by mattgiuca
settings: Python code generates all of the HTML for the static page.
28
{
29
    var callback = function(xhr)
30
        {
31
            user = JSON.parse(xhr.responseText);
577 by mattgiuca
settings: Now populates all fields on the fly with the info from the DB,
32
            populate(user);
560 by mattgiuca
settings: Python code generates all of the HTML for the static page.
33
        }
34
    /* Just get details for the logged in user */
1099.1.33 by William Grant
Port www/apps/settings to new framework. It now appears under
35
    ajax_call(callback, "userservice", "get_user", {"login": settings_login}, "GET");
580 by mattgiuca
settings: Now able to handle the save button. This updates the user's details.
36
    return false;
560 by mattgiuca
settings: Python code generates all of the HTML for the static page.
37
}
577 by mattgiuca
settings: Now populates all fields on the fly with the info from the DB,
38
39
/* Populate the page with the given user's account details */
40
function populate(user)
41
{
580 by mattgiuca
settings: Now able to handle the save button. This updates the user's details.
42
    user_data = user;
577 by mattgiuca
settings: Now populates all fields on the fly with the info from the DB,
43
    /* Plain text elements (non-editable) */
44
    var login = document.getElementById("login");
1122 by William Grant
Display a message in UserSettingsView if the user is an admin, rather than
45
    var admin = document.getElementById("admin");
577 by mattgiuca
settings: Now populates all fields on the fly with the info from the DB,
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);
1122 by William Grant
Display a message in UserSettingsView if the user is an admin, rather than
63
    dom_removechildren(admin);
582 by mattgiuca
settings: Fixed clicking reload.
64
    dom_removechildren(changepassword);
577 by mattgiuca
settings: Now populates all fields on the fly with the info from the DB,
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
1122 by William Grant
Display a message in UserSettingsView if the user is an admin, rather than
82
    /* "admin" : <p>Privilege: <strong>administrator</strong></p>
577 by mattgiuca
settings: Now populates all fields on the fly with the info from the DB,
83
     */
1122 by William Grant
Display a message in UserSettingsView if the user is an admin, rather than
84
    if (user.admin)
577 by mattgiuca
settings: Now populates all fields on the fly with the info from the DB,
85
    {
1122 by William Grant
Display a message in UserSettingsView if the user is an admin, rather than
86
        text = "Privilege: ";
87
        admin.appendChild(document.createTextNode(text));
88
        strong = document.createElement("strong");
89
        strong.appendChild(document.createTextNode("IVLE administrator"));
90
        admin.appendChild(strong);
577 by mattgiuca
settings: Now populates all fields on the fly with the info from the DB,
91
    }
92
93
    /* "nick" and "email" boxes */
94
    nick.value = user.nick;
95
    email.value = user.email;
96
97
    /* Password change box */
98
    /* (Only if this user has a local password) */
99
    if (user.local_password)
100
    {
101
        p = document.createElement("h3");
102
        p.appendChild(document.createTextNode("Change password"))
103
        changepassword.appendChild(p);
582 by mattgiuca
settings: Fixed clicking reload.
104
        p = document.createElement("p");
1078 by chadnickbok
Updated the settings page to require the old password
105
        
106
        p.appendChild(document.createTextNode("Please type your old password, "
107
                + "and new password twice, for verification."));
582 by mattgiuca
settings: Fixed clicking reload.
108
        changepassword.appendChild(p);
109
577 by mattgiuca
settings: Now populates all fields on the fly with the info from the DB,
110
        table = document.createElement("table");
111
        tbody = document.createElement("tbody");
112
113
        tr = document.createElement("tr");
114
        td = document.createElement("td");
1078 by chadnickbok
Updated the settings page to require the old password
115
        td.appendChild(document.createTextNode("Old password:"))
116
        tr.appendChild(td);
117
        td = document.createElement("td");
118
        inputbox = document.createElement("input");
119
        inputbox.setAttribute("type", "password");
120
        inputbox.setAttribute("name", "oldpass");
121
        inputbox.setAttribute("id", "oldpass");
122
        inputbox.setAttribute("size", "40");
123
        td.appendChild(inputbox)
124
        tr.appendChild(td);
125
        tbody.appendChild(tr);
126
127
        tr = document.createElement("tr");
128
        td = document.createElement("td");
577 by mattgiuca
settings: Now populates all fields on the fly with the info from the DB,
129
        td.appendChild(document.createTextNode("New password:"))
130
        tr.appendChild(td);
131
        td = document.createElement("td");
132
        inputbox = document.createElement("input");
133
        inputbox.setAttribute("type", "password");
134
        inputbox.setAttribute("name", "newpass");
135
        inputbox.setAttribute("id", "newpass");
136
        inputbox.setAttribute("size", "40");
137
        td.appendChild(inputbox)
138
        tr.appendChild(td);
139
        tbody.appendChild(tr);
140
141
        tr = document.createElement("tr");
142
        td = document.createElement("td");
143
        td.appendChild(document.createTextNode("Retype password:"))
144
        tr.appendChild(td);
145
        td = document.createElement("td");
146
        inputbox = document.createElement("input");
147
        inputbox.setAttribute("type", "password");
148
        inputbox.setAttribute("name", "repeatpass");
149
        inputbox.setAttribute("id", "repeatpass");
150
        inputbox.setAttribute("size", "40");
151
        td.appendChild(inputbox)
152
        tr.appendChild(td);
153
        tbody.appendChild(tr);
154
155
        table.appendChild(tbody);
156
        changepassword.appendChild(table);
157
    }
158
159
    if (user.pass_exp != null || user.acct_exp != null)
160
    {
161
        p = document.createElement("h3");
162
        text = "Notices";
163
        p.appendChild(document.createTextNode(text));
164
        notices.appendChild(p);
165
        if (user.pass_exp != null)
166
        {
167
            p = document.createElement("p");
168
            /* TODO: Nice passexp */
169
            var pass_exp = user.pass_exp.toString()
170
            text = "Your password will expire on " + pass_exp
171
                + ". You should change it before then to avoid having your "
172
                + "account disabled.";
173
            p.appendChild(document.createTextNode(text));
174
            notices.appendChild(p);
175
        }
176
        if (user.acct_exp != null)
177
        {
178
            p = document.createElement("p");
179
            /* TODO: Nice acct_exp */
180
            var acct_exp = user.acct_exp.toString()
181
            text = "Your IVLE account will expire on " + acct_exp + ".";
182
            p.appendChild(document.createTextNode(text));
183
            notices.appendChild(p);
184
        }
185
    }
186
}
580 by mattgiuca
settings: Now able to handle the save button. This updates the user's details.
187
188
/* Sets the "result" text.
189
 * iserror (bool) determines the styling.
190
 */
191
function set_result(text, iserror)
192
{
193
    var p = document.getElementById("result");
194
    dom_removechildren(p);
195
    p.appendChild(document.createTextNode(text));
196
    if (iserror)
197
        p.setAttribute("class", "error");
198
    else
199
        p.removeAttribute("class");
200
}
201
202
/* Writes the settings to the server.
203
 * Returns false. */
204
function save_settings()
205
{
206
    /* Button (input) elements */
207
    var save = document.getElementById("save");
208
    /* Textbox (input) elements */
209
    try
210
    {
1078 by chadnickbok
Updated the settings page to require the old password
211
        var oldpass = document.getElementById("oldpass");
580 by mattgiuca
settings: Now able to handle the save button. This updates the user's details.
212
        var newpass = document.getElementById("newpass");
213
        var repeatpass = document.getElementById("repeatpass");
214
    }
215
    catch (e)
216
    {
217
        var newpass = null;
218
        var repeatpass = null;
219
    }
220
    var nick = document.getElementById("nick");
221
    var email = document.getElementById("email");
222
223
    /* Check */
1078 by chadnickbok
Updated the settings page to require the old password
224
    oldpassval = oldpass == null ? null : oldpass.value;
580 by mattgiuca
settings: Now able to handle the save button. This updates the user's details.
225
    newpassval = newpass == null ? null : newpass.value;
226
    repeatpassval = repeatpass == null ? null : repeatpass.value;
227
    nickval = nick.value;
228
    emailval = email.value;
229
230
    /* Clear the password boxes, even if there are errors later */
231
    try
232
    {
233
        newpass.value = "";
234
        repeatpass.value = "";
235
    }
236
    catch (e)
237
    {
238
    }
239
240
    if (nickval == "")
241
    {
242
        set_result("Display name is empty.", true);
243
        return false;
244
    }
245
    if (newpassval != repeatpassval)
246
    {
247
        set_result("Passwords do not match.", true);
248
        return false;
249
    }
250
251
    /* Disable the heavy-duty supercolliding super button */
252
    save.setAttribute("disabled", "disabled");
253
    save.setAttribute("value", "Saving...");
254
    var callback = function(xhr)
255
    {
256
        save.removeAttribute("disabled");
257
        save.setAttribute("value", "Save");
258
259
        if (xhr.status == 200)
260
        {
261
            set_result("Successfully updated details.");
262
            user_data.nick = nickval;
263
            user_data.email = emailval;
605 by mattgiuca
settings.js: Updates the user's nickname in the top bar immediately, to avoid
264
            /* Now a little hack - update the user's nick display
265
             * in the heading bar, so they are sure it has been changed.
266
             */
267
            var usernick = document.getElementById("usernick");
268
            dom_removechildren(usernick);
269
            usernick.appendChild(document.createTextNode(nickval));
580 by mattgiuca
settings: Now able to handle the save button. This updates the user's details.
270
        }
1078 by chadnickbok
Updated the settings page to require the old password
271
        else if (xhr.getResponseHeader("X-IVLE-Action-Error"))
272
        {
273
            set_result(decodeURIComponent(xhr.getResponseHeader(
274
                                     "X-IVLE-Action-Error").toString()), true);        
275
        }
580 by mattgiuca
settings: Now able to handle the save button. This updates the user's details.
276
        else
277
        {
278
            set_result("There was a problem updating the details."
1078 by chadnickbok
Updated the settings page to require the old password
279
                + " Your changes have not been saved.", true);
580 by mattgiuca
settings: Now able to handle the save button. This updates the user's details.
280
        }
281
    }
282
    data = {
283
        "login": user_data.login,
284
        "nick": nickval,
285
        "email": emailval,
1078 by chadnickbok
Updated the settings page to require the old password
286
        "oldpass": oldpassval,
580 by mattgiuca
settings: Now able to handle the save button. This updates the user's details.
287
    }
288
    if (newpassval != null && newpassval != "")
289
        data['password'] = newpassval;
290
    ajax_call(callback, "userservice", "update_user", data, "POST");
291
    return false;
292
}