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", {}, "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 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");
62
dom_removechildren(login);
63
dom_removechildren(role);
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
/* "role" : <p>Your privilege level is <b>rolenm</b>.</p>
83
* Unless rolenm is "student"
85
if (user.rolenm != "student")
87
text = "Your privilege level is ";
88
role.appendChild(document.createTextNode(text));
89
b = document.createElement("b");
91
b.appendChild(document.createTextNode(text));
94
role.appendChild(document.createTextNode(text));
97
/* "nick" and "email" boxes */
98
nick.value = user.nick;
99
email.value = user.email;
101
/* Password change box */
102
/* (Only if this user has a local password) */
103
if (user.local_password)
105
p = document.createElement("h3");
106
p.appendChild(document.createTextNode("Change password"))
107
changepassword.appendChild(p);
108
p = document.createElement("p");
110
p.appendChild(document.createTextNode("Please type your old password, "
111
+ "and new password twice, for verification."));
112
changepassword.appendChild(p);
114
table = document.createElement("table");
115
tbody = document.createElement("tbody");
117
tr = document.createElement("tr");
118
td = document.createElement("td");
119
td.appendChild(document.createTextNode("Old password:"))
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)
129
tbody.appendChild(tr);
131
tr = document.createElement("tr");
132
td = document.createElement("td");
133
td.appendChild(document.createTextNode("New password:"))
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)
143
tbody.appendChild(tr);
145
tr = document.createElement("tr");
146
td = document.createElement("td");
147
td.appendChild(document.createTextNode("Retype password:"))
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)
157
tbody.appendChild(tr);
159
table.appendChild(tbody);
160
changepassword.appendChild(table);
163
if (user.pass_exp != null || user.acct_exp != null)
165
p = document.createElement("h3");
167
p.appendChild(document.createTextNode(text));
168
notices.appendChild(p);
169
if (user.pass_exp != null)
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);
180
if (user.acct_exp != null)
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);
192
/* Sets the "result" text.
193
* iserror (bool) determines the styling.
195
function set_result(text, iserror)
197
var p = document.getElementById("result");
198
dom_removechildren(p);
199
p.appendChild(document.createTextNode(text));
201
p.setAttribute("class", "error");
203
p.removeAttribute("class");
206
/* Writes the settings to the server.
208
function save_settings()
210
/* Button (input) elements */
211
var save = document.getElementById("save");
212
/* Textbox (input) elements */
215
var oldpass = document.getElementById("oldpass");
216
var newpass = document.getElementById("newpass");
217
var repeatpass = document.getElementById("repeatpass");
222
var repeatpass = null;
224
var nick = document.getElementById("nick");
225
var email = document.getElementById("email");
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;
234
/* Clear the password boxes, even if there are errors later */
238
repeatpass.value = "";
246
set_result("Display name is empty.", true);
249
if (newpassval != repeatpassval)
251
set_result("Passwords do not match.", true);
255
/* Disable the heavy-duty supercolliding super button */
256
save.setAttribute("disabled", "disabled");
257
save.setAttribute("value", "Saving...");
258
var callback = function(xhr)
260
save.removeAttribute("disabled");
261
save.setAttribute("value", "Save");
263
if (xhr.status == 200)
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.
271
var usernick = document.getElementById("usernick");
272
dom_removechildren(usernick);
273
usernick.appendChild(document.createTextNode(nickval));
275
else if (xhr.getResponseHeader("X-IVLE-Action-Error"))
277
set_result(decodeURIComponent(xhr.getResponseHeader(
278
"X-IVLE-Action-Error").toString()), true);
282
set_result("There was a problem updating the details."
283
+ " Your changes have not been saved.", true);
287
"login": user_data.login,
290
"oldpass": oldpassval,
292
if (newpassval != null && newpassval != "")
293
data['password'] = newpassval;
294
ajax_call(callback, "userservice", "update_user", data, "POST");