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");
109
p.appendChild(document.createTextNode("Please type your new password "
110
+ "twice, to make sure you remember it."))
111
changepassword.appendChild(p);
113
table = document.createElement("table");
114
tbody = document.createElement("tbody");
116
tr = document.createElement("tr");
117
td = document.createElement("td");
118
td.appendChild(document.createTextNode("New password:"))
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)
128
tbody.appendChild(tr);
130
tr = document.createElement("tr");
131
td = document.createElement("td");
132
td.appendChild(document.createTextNode("Retype password:"))
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)
142
tbody.appendChild(tr);
144
table.appendChild(tbody);
145
changepassword.appendChild(table);
148
if (user.pass_exp != null || user.acct_exp != null)
150
p = document.createElement("h3");
152
p.appendChild(document.createTextNode(text));
153
notices.appendChild(p);
154
if (user.pass_exp != null)
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);
165
if (user.acct_exp != null)
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);
177
/* Sets the "result" text.
178
* iserror (bool) determines the styling.
180
function set_result(text, iserror)
182
var p = document.getElementById("result");
183
dom_removechildren(p);
184
p.appendChild(document.createTextNode(text));
186
p.setAttribute("class", "error");
188
p.removeAttribute("class");
191
/* Writes the settings to the server.
193
function save_settings()
195
/* Button (input) elements */
196
var save = document.getElementById("save");
197
/* Textbox (input) elements */
200
var newpass = document.getElementById("newpass");
201
var repeatpass = document.getElementById("repeatpass");
206
var repeatpass = null;
208
var nick = document.getElementById("nick");
209
var email = document.getElementById("email");
212
newpassval = newpass == null ? null : newpass.value;
213
repeatpassval = repeatpass == null ? null : repeatpass.value;
214
nickval = nick.value;
215
emailval = email.value;
217
/* Clear the password boxes, even if there are errors later */
221
repeatpass.value = "";
229
set_result("Display name is empty.", true);
232
if (newpassval != repeatpassval)
234
set_result("Passwords do not match.", true);
238
/* Disable the heavy-duty supercolliding super button */
239
save.setAttribute("disabled", "disabled");
240
save.setAttribute("value", "Saving...");
241
var callback = function(xhr)
243
save.removeAttribute("disabled");
244
save.setAttribute("value", "Save");
246
if (xhr.status == 200)
248
set_result("Successfully updated details.");
249
user_data.nick = nickval;
250
user_data.email = emailval;
251
/* Now a little hack - update the user's nick display
252
* in the heading bar, so they are sure it has been changed.
254
var usernick = document.getElementById("usernick");
255
dom_removechildren(usernick);
256
usernick.appendChild(document.createTextNode(nickval));
260
set_result("There was a problem updating the details."
261
+ " Your changes have not been saved.");
265
"login": user_data.login,
269
if (newpassval != null && newpassval != "")
270
data['password'] = newpassval;
271
ajax_call(callback, "userservice", "update_user", data, "POST");