1
/* IVLE - Informatics Virtual Learning Environment
2
* Copyright (C) 2007-2008 The University of Melbourne
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.
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.
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
18
* Module: Settings (Client-side JavaScript)
25
/* Fetch the user's details from the server, and populate the page.
27
function revert_settings()
29
var callback = function(xhr)
31
user = JSON.parse(xhr.responseText);
34
/* Just get details for the logged in user */
35
ajax_call(callback, "userservice", "get_user", {"login": settings_login}, "GET");
39
/* Populate the page with the given user's account details */
40
function populate(user)
43
/* Plain text elements (non-editable) */
44
var login = document.getElementById("login");
45
var admin = document.getElementById("admin");
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");
62
dom_removechildren(login);
63
dom_removechildren(admin);
64
dom_removechildren(changepassword);
65
dom_removechildren(notices);
67
/* Construct the page */
69
/* "login" : Full Name (<b>login</b> / studentid) */
70
text = user.fullname + " (";
71
login.appendChild(document.createTextNode(text));
73
b = document.createElement("b");
74
b.appendChild(document.createTextNode(text));
76
if (user.studentid != null)
77
text = " / " + user.studentid + ")"
80
login.appendChild(document.createTextNode(text));
82
/* "admin" : <p>Privilege: <strong>administrator</strong></p>
87
admin.appendChild(document.createTextNode(text));
88
strong = document.createElement("strong");
89
strong.appendChild(document.createTextNode("IVLE administrator"));
90
admin.appendChild(strong);
93
/* "nick" and "email" boxes */
94
nick.value = user.nick;
95
email.value = user.email;
97
/* Password change box */
98
/* (Only if this user has a local password) */
99
if (user.local_password)
101
p = document.createElement("h3");
102
p.appendChild(document.createTextNode("Change password"))
103
changepassword.appendChild(p);
104
p = document.createElement("p");
106
p.appendChild(document.createTextNode("Please type your old password, "
107
+ "and new password twice, for verification."));
108
changepassword.appendChild(p);
110
table = document.createElement("table");
111
tbody = document.createElement("tbody");
113
tr = document.createElement("tr");
114
td = document.createElement("td");
115
td.appendChild(document.createTextNode("Old password:"))
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)
125
tbody.appendChild(tr);
127
tr = document.createElement("tr");
128
td = document.createElement("td");
129
td.appendChild(document.createTextNode("New password:"))
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)
139
tbody.appendChild(tr);
141
tr = document.createElement("tr");
142
td = document.createElement("td");
143
td.appendChild(document.createTextNode("Retype password:"))
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)
153
tbody.appendChild(tr);
155
table.appendChild(tbody);
156
changepassword.appendChild(table);
159
if (user.pass_exp != null || user.acct_exp != null)
161
p = document.createElement("h3");
163
p.appendChild(document.createTextNode(text));
164
notices.appendChild(p);
165
if (user.pass_exp != null)
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);
176
if (user.acct_exp != null)
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);
188
/* Sets the "result" text.
189
* iserror (bool) determines the styling.
191
function set_result(text, iserror)
193
var p = document.getElementById("result");
194
dom_removechildren(p);
195
p.appendChild(document.createTextNode(text));
197
p.setAttribute("class", "error");
199
p.removeAttribute("class");
202
/* Writes the settings to the server.
204
function save_settings()
206
/* Button (input) elements */
207
var save = document.getElementById("save");
208
/* Textbox (input) elements */
211
var oldpass = document.getElementById("oldpass");
212
var newpass = document.getElementById("newpass");
213
var repeatpass = document.getElementById("repeatpass");
218
var repeatpass = null;
220
var nick = document.getElementById("nick");
221
var email = document.getElementById("email");
224
oldpassval = oldpass == null ? null : oldpass.value;
225
newpassval = newpass == null ? null : newpass.value;
226
repeatpassval = repeatpass == null ? null : repeatpass.value;
227
nickval = nick.value;
228
emailval = email.value;
230
/* Clear the password boxes, even if there are errors later */
234
repeatpass.value = "";
242
set_result("Display name is empty.", true);
245
if (newpassval != repeatpassval)
247
set_result("Passwords do not match.", true);
251
/* Disable the heavy-duty supercolliding super button */
252
save.setAttribute("disabled", "disabled");
253
save.setAttribute("value", "Saving...");
254
var callback = function(xhr)
256
save.removeAttribute("disabled");
257
save.setAttribute("value", "Save");
259
if (xhr.status == 200)
261
set_result("Successfully updated details.");
262
user_data.nick = nickval;
263
user_data.email = emailval;
264
/* Now a little hack - update the user's nick display
265
* in the heading bar, so they are sure it has been changed.
267
var usernick = document.getElementById("usernick");
268
dom_removechildren(usernick);
269
usernick.appendChild(document.createTextNode(nickval));
271
else if (xhr.getResponseHeader("X-IVLE-Action-Error"))
273
set_result(decodeURIComponent(xhr.getResponseHeader(
274
"X-IVLE-Action-Error").toString()), true);
278
set_result("There was a problem updating the details."
279
+ " Your changes have not been saved.", true);
283
"login": user_data.login,
286
"oldpass": oldpassval,
288
if (newpassval != null && newpassval != "")
289
data['password'] = newpassval;
290
ajax_call(callback, "userservice", "update_user", data, "POST");