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