~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 */
35
    ajax_call(callback, "userservice", "get_user", {}, "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");
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);
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
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);
582 by mattgiuca
settings: Fixed clicking reload.
108
        p = document.createElement("p");
109
        p.appendChild(document.createTextNode("Please type your new password "
110
            + "twice, to make sure you remember it."))
111
        changepassword.appendChild(p);
112
577 by mattgiuca
settings: Now populates all fields on the fly with the info from the DB,
113
        table = document.createElement("table");
114
        tbody = document.createElement("tbody");
115
116
        tr = document.createElement("tr");
117
        td = document.createElement("td");
118
        td.appendChild(document.createTextNode("New password:"))
119
        tr.appendChild(td);
120
        td = document.createElement("td");
121
        inputbox = document.createElement("input");
122
        inputbox.setAttribute("type", "password");
123
        inputbox.setAttribute("name", "newpass");
124
        inputbox.setAttribute("id", "newpass");
125
        inputbox.setAttribute("size", "40");
126
        td.appendChild(inputbox)
127
        tr.appendChild(td);
128
        tbody.appendChild(tr);
129
130
        tr = document.createElement("tr");
131
        td = document.createElement("td");
132
        td.appendChild(document.createTextNode("Retype password:"))
133
        tr.appendChild(td);
134
        td = document.createElement("td");
135
        inputbox = document.createElement("input");
136
        inputbox.setAttribute("type", "password");
137
        inputbox.setAttribute("name", "repeatpass");
138
        inputbox.setAttribute("id", "repeatpass");
139
        inputbox.setAttribute("size", "40");
140
        td.appendChild(inputbox)
141
        tr.appendChild(td);
142
        tbody.appendChild(tr);
143
144
        table.appendChild(tbody);
145
        changepassword.appendChild(table);
146
    }
147
148
    if (user.pass_exp != null || user.acct_exp != null)
149
    {
150
        p = document.createElement("h3");
151
        text = "Notices";
152
        p.appendChild(document.createTextNode(text));
153
        notices.appendChild(p);
154
        if (user.pass_exp != null)
155
        {
156
            p = document.createElement("p");
157
            /* TODO: Nice passexp */
158
            var pass_exp = user.pass_exp.toString()
159
            text = "Your password will expire on " + pass_exp
160
                + ". You should change it before then to avoid having your "
161
                + "account disabled.";
162
            p.appendChild(document.createTextNode(text));
163
            notices.appendChild(p);
164
        }
165
        if (user.acct_exp != null)
166
        {
167
            p = document.createElement("p");
168
            /* TODO: Nice acct_exp */
169
            var acct_exp = user.acct_exp.toString()
170
            text = "Your IVLE account will expire on " + acct_exp + ".";
171
            p.appendChild(document.createTextNode(text));
172
            notices.appendChild(p);
173
        }
174
    }
175
}
580 by mattgiuca
settings: Now able to handle the save button. This updates the user's details.
176
177
/* Sets the "result" text.
178
 * iserror (bool) determines the styling.
179
 */
180
function set_result(text, iserror)
181
{
182
    var p = document.getElementById("result");
183
    dom_removechildren(p);
184
    p.appendChild(document.createTextNode(text));
185
    if (iserror)
186
        p.setAttribute("class", "error");
187
    else
188
        p.removeAttribute("class");
189
}
190
191
/* Writes the settings to the server.
192
 * Returns false. */
193
function save_settings()
194
{
195
    /* Button (input) elements */
196
    var save = document.getElementById("save");
197
    /* Textbox (input) elements */
198
    try
199
    {
200
        var newpass = document.getElementById("newpass");
201
        var repeatpass = document.getElementById("repeatpass");
202
    }
203
    catch (e)
204
    {
205
        var newpass = null;
206
        var repeatpass = null;
207
    }
208
    var nick = document.getElementById("nick");
209
    var email = document.getElementById("email");
210
211
    /* Check */
212
    newpassval = newpass == null ? null : newpass.value;
213
    repeatpassval = repeatpass == null ? null : repeatpass.value;
214
    nickval = nick.value;
215
    emailval = email.value;
216
217
    /* Clear the password boxes, even if there are errors later */
218
    try
219
    {
220
        newpass.value = "";
221
        repeatpass.value = "";
222
    }
223
    catch (e)
224
    {
225
    }
226
227
    if (nickval == "")
228
    {
229
        set_result("Display name is empty.", true);
230
        return false;
231
    }
232
    if (newpassval != repeatpassval)
233
    {
234
        set_result("Passwords do not match.", true);
235
        return false;
236
    }
237
238
    /* Disable the heavy-duty supercolliding super button */
239
    save.setAttribute("disabled", "disabled");
240
    save.setAttribute("value", "Saving...");
241
    var callback = function(xhr)
242
    {
243
        save.removeAttribute("disabled");
244
        save.setAttribute("value", "Save");
245
246
        if (xhr.status == 200)
247
        {
248
            set_result("Successfully updated details.");
249
            user_data.nick = nickval;
250
            user_data.email = emailval;
605 by mattgiuca
settings.js: Updates the user's nickname in the top bar immediately, to avoid
251
            /* Now a little hack - update the user's nick display
252
             * in the heading bar, so they are sure it has been changed.
253
             */
254
            var usernick = document.getElementById("usernick");
255
            dom_removechildren(usernick);
256
            usernick.appendChild(document.createTextNode(nickval));
580 by mattgiuca
settings: Now able to handle the save button. This updates the user's details.
257
        }
258
        else
259
        {
260
            set_result("There was a problem updating the details."
261
                + " Your changes have not been saved.");
262
        }
263
    }
264
    data = {
265
        "login": user_data.login,
266
        "nick": nickval,
267
        "email": emailval,
268
    }
269
    if (newpassval != null && newpassval != "")
270
        data['password'] = newpassval;
271
    ajax_call(callback, "userservice", "update_user", data, "POST");
272
    return false;
273
}