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)
30
/* Fetch the user's details from the server, and populate the page.
32
function revert_settings()
34
var callback = function(xhr)
36
user = JSON.parse(xhr.responseText);
39
/* Just get details for the logged in user */
40
ajax_call(callback, "userservice", "get_user", {}, "GET");
44
/* Populate the page with the given user's account details */
45
function populate(user)
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");
67
dom_removechildren(login);
68
dom_removechildren(role);
69
dom_removechildren(changepassword);
70
dom_removechildren(notices);
72
/* Construct the page */
74
/* "login" : Full Name (<b>login</b> / studentid) */
75
text = user.fullname + " (";
76
login.appendChild(document.createTextNode(text));
78
b = document.createElement("b");
79
b.appendChild(document.createTextNode(text));
81
if (user.studentid != null)
82
text = " / " + user.studentid + ")"
85
login.appendChild(document.createTextNode(text));
87
/* "role" : <p>Your privilege level is <b>rolenm</b>.</p>
88
* Unless rolenm is "student"
90
if (user.rolenm != "student")
92
text = "Your privilege level is ";
93
role.appendChild(document.createTextNode(text));
94
b = document.createElement("b");
96
b.appendChild(document.createTextNode(text));
99
role.appendChild(document.createTextNode(text));
102
/* "nick" and "email" boxes */
103
nick.value = user.nick;
104
email.value = user.email;
106
/* Password change box */
107
/* (Only if this user has a local password) */
108
if (user.local_password)
110
p = document.createElement("h3");
111
p.appendChild(document.createTextNode("Change password"))
112
changepassword.appendChild(p);
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);
118
table = document.createElement("table");
119
tbody = document.createElement("tbody");
121
tr = document.createElement("tr");
122
td = document.createElement("td");
123
td.appendChild(document.createTextNode("New password:"))
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)
133
tbody.appendChild(tr);
135
tr = document.createElement("tr");
136
td = document.createElement("td");
137
td.appendChild(document.createTextNode("Retype password:"))
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)
147
tbody.appendChild(tr);
149
table.appendChild(tbody);
150
changepassword.appendChild(table);
153
if (user.pass_exp != null || user.acct_exp != null)
155
p = document.createElement("h3");
157
p.appendChild(document.createTextNode(text));
158
notices.appendChild(p);
159
if (user.pass_exp != null)
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);
170
if (user.acct_exp != null)
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);
182
/* Sets the "result" text.
183
* iserror (bool) determines the styling.
185
function set_result(text, iserror)
187
var p = document.getElementById("result");
188
dom_removechildren(p);
189
p.appendChild(document.createTextNode(text));
191
p.setAttribute("class", "error");
193
p.removeAttribute("class");
196
/* Writes the settings to the server.
198
function save_settings()
200
/* Button (input) elements */
201
var save = document.getElementById("save");
202
/* Textbox (input) elements */
205
var newpass = document.getElementById("newpass");
206
var repeatpass = document.getElementById("repeatpass");
211
var repeatpass = null;
213
var nick = document.getElementById("nick");
214
var email = document.getElementById("email");
217
newpassval = newpass == null ? null : newpass.value;
218
repeatpassval = repeatpass == null ? null : repeatpass.value;
219
nickval = nick.value;
220
emailval = email.value;
222
/* Clear the password boxes, even if there are errors later */
226
repeatpass.value = "";
234
set_result("Display name is empty.", true);
237
if (newpassval != repeatpassval)
239
set_result("Passwords do not match.", true);
243
/* Disable the heavy-duty supercolliding super button */
244
save.setAttribute("disabled", "disabled");
245
save.setAttribute("value", "Saving...");
246
var callback = function(xhr)
248
save.removeAttribute("disabled");
249
save.setAttribute("value", "Save");
251
if (xhr.status == 200)
253
set_result("Successfully updated details.");
254
user_data.nick = nickval;
255
user_data.email = emailval;
256
/* Now a little hack - update the user's nick display
257
* in the heading bar, so they are sure it has been changed.
259
var usernick = document.getElementById("usernick");
260
dom_removechildren(usernick);
261
usernick.appendChild(document.createTextNode(nickval));
265
set_result("There was a problem updating the details."
266
+ " Your changes have not been saved.");
270
"login": user_data.login,
274
if (newpassval != null && newpassval != "")
275
data['password'] = newpassval;
276
ajax_call(callback, "userservice", "update_user", data, "POST");