updates on the student list editor for teachers

This commit is contained in:
jacob 2010-06-17 20:22:18 +00:00
parent 6c4a54fe94
commit 7cb1f55a3e

View File

@ -4,32 +4,67 @@ require_once('user.inc.php');
if($_SESSION['schoolid'] && $_SESSION['schoolaccesscode'] && $conference['type'] == 'scienceolympics'){ if($_SESSION['schoolid'] && $_SESSION['schoolaccesscode'] && $conference['type'] == 'scienceolympics'){
if($_GET['action'] == 'new'){ switch($_GET['action']){
$results = process_newRecord($_POST['firstName'], $_POST['lastName'], $_POST['email']); case 'new':
if($results !== true){ $results = process_newRecord($_POST['firstName'], $_POST['lastName'], $_POST['email']);
echo "<script type=\"text/javascript\">var savedRecord = false;</script>"; if($results !== true){
error_($results); echo "<script type=\"text/javascript\">var success = false;</script>";
}else{ error_($results);
echo "<script type=\"text/javascript\">var savedRecord = true;</script>"; }else{
} echo "<script type=\"text/javascript\">var success = true;</script>";
}else if($_GET['action'] == 'delete'){ }
if(!delete_record($_POST['uid'])){ break;
echo "<script type=\"text/javascript\">var deletedRecord = false;</script>";
error_("Unable to delete record"); case 'update':
}else{ $results = alter_record($_POST['recordId'], $_POST['firstName'], $_POST['lastName'], $_POST['email']);
echo "<script type=\"text/javascript\">var deletedRecord = true;</script>"; if($results !== true){
} echo "<script type=\"text/javascript\">var success = false;</script>";
}else{ error_($results);
$title = i18n("Manage Students"); }else{
send_header($title, array("School Home" => "schoolaccess.php")); echo "<script type=\"text/javascript\">var success = true;</script>";
draw_javascript(); }
draw_list(); break;
send_footer();
case 'delete':
if(!delete_record($_POST['uid'])){
echo "<script type=\"text/javascript\">var success = false;</script>";
error_("Unable to delete record");
}else{
echo "<script type=\"text/javascript\">var success = true;</script>";
}
break;
default:
$title = i18n("Manage Students");
send_header($title, array("School Home" => "schoolaccess.php"));
draw_javascript();
draw_list();
send_footer();
} }
}else{ }else{
header('Location: schoolaccess.php'); header('Location: schoolaccess.php');
} }
// alter an existing user record. returns true on success, error message on failure
function alter_record($uid, $firstName, $lastName, $email){
global $conference;
$returnval = true;
$firstName = trim($firstName);
$lastName = trim($lastName);
$email = strtolower(trim($email));
$user = user_load(null, $uid);
if($user){
$user['firstname'] = $firstName;
$user['lastname'] = $lastName;
$user['email'] = $email;
user_save($user);
echo user_row($uid, $user['username'], $firstName, $lastName, $email);
}else{
$returnval = "User not found";
}
return $returnval;
}
// create a new record with the given first name last name and e-mail address // create a new record with the given first name last name and e-mail address
// return true on success, error message on failure // return true on success, error message on failure
function process_newRecord($firstName, $lastName, $email){ function process_newRecord($firstName, $lastName, $email){
@ -40,25 +75,18 @@ function process_newRecord($firstName, $lastName, $email){
$uid = null; $uid = null;
// make sure we are actually given a first and last name // make sure we are actually given a first and last name
if(strlen($firstName) == 0 || strlen($lastName) == 0) return "First and last names are required fields"; if(strlen($firstName) == 0 || strlen($lastName) == 0){
return "First and last names are required fields";
}
// if they have an e-mail address, make sure it's not already in use
if($email != null){ if($email != null){
$user = user_load_by_email($email); $user = user_load_by_email($email);
}else{ }else{
$user = false; $user = false;
} }
if($user != false){ if($user != false){
// we're adding an existing user. First find out if they are in the school we have specified
return "e-mail address is already in use"; return "e-mail address is already in use";
// $user['firstname'] = $firstName;
// $user['lastname'] = $lastName;
/*
$query = "INSERT INTO users_conferences_link(conferences_id, users_uid) VALUES(";
$query .= $conference['id'] . ', ' . $user['uid'];
$firstName = $user['firstname'];
$lastName = $user['lastname'];
*/
}else{ }else{
// we're creating a new user // we're creating a new user
if(strlen($email) != 0){ if(strlen($email) != 0){
@ -87,20 +115,27 @@ function process_newRecord($firstName, $lastName, $email){
$user = user_create('student', $username); $user = user_create('student', $username);
$user['firstname'] = $firstName; $user['firstname'] = $firstName;
$user['lastname'] = $lastName; $user['lastname'] = $lastName;
$user['active'] = 'yes';
$user['complete'] = 'yes';
if($username == $email) if($username == $email)
$user['email'] = $email; $user['email'] = $email;
$user['schools_id'] = $_SESSION['schoolid']; $user['schools_id'] = $_SESSION['schoolid'];
user_save($user); user_save($user);
$uid = $user['uid'];
} }
$uid = $user['uid'];
echo user_row($uid, $username, $firstName, $lastName, $email); echo user_row($uid, $username, $firstName, $lastName, $email);
return true; return true;
} }
// generate the table row for thisa given record
function user_row($uid, $username, $firstName, $lastName, $email){ function user_row($uid, $username, $firstName, $lastName, $email){
$rval = "<tr id=\"$uid\">"; $rval = "<tr id=\"$uid\">";
$rval .= "<td>$username</td>"; $rval .= "<td onclick=\"populate($uid);\"";
$rval .= " onmouseover=\"document.body.style.cursor='pointer';\"";
$rval .= " onmouseout=\"document.body.style.cursor='auto';\"";
$rval .= ">$username</td>";
$rval .= "<td>$firstName</td>"; $rval .= "<td>$firstName</td>";
$rval .= "<td>$lastName</td>"; $rval .= "<td>$lastName</td>";
$rval .= "<td>$email</td>"; $rval .= "<td>$email</td>";
@ -126,6 +161,29 @@ function draw_javascript(){
<script type="text/javascript"> <script type="text/javascript">
var awaiting_ajax = false; // used to prevent the same record from being submitted multiple times var awaiting_ajax = false; // used to prevent the same record from being submitted multiple times
// populate the edit fields with this user's info
function populate(uid){
// extract the user's info from our table
var n = 0;
$('#' + uid + ' > td').each(function() {
switch(n){
case 1: $('#newFirstName').attr({value:this.innerHTML}); break;
case 2: $('#newLastName').attr({value:this.innerHTML}); break;
case 3: $('#newEmail').attr({value:this.innerHTML}); break;
}
n++;
});
$('#existingRecordId').attr({value:uid});
}
function clearFields(){
$('#newFirstName').select();
$('#existingRecordId').attr({ value: -1 });
$('#newFirstName').attr({ value: '' });
$('#newLastName').attr({ value: '' });
$('#newEmail').attr({ value: '' });
}
function deleteRecord(uid){ function deleteRecord(uid){
var params; var params;
if(awaiting_ajax) return false; if(awaiting_ajax) return false;
@ -134,7 +192,7 @@ function draw_javascript(){
params = [{ 'name' : 'uid', 'value' : uid }]; params = [{ 'name' : 'uid', 'value' : uid }];
$("#debug").load("schoolstudents.php?action=delete", params, function(response){ $("#debug").load("schoolstudents.php?action=delete", params, function(response){
if(deletedRecord){ if(success){
$('#' + uid).remove(); $('#' + uid).remove();
} }
awaiting_ajax = false; awaiting_ajax = false;
@ -142,37 +200,48 @@ function draw_javascript(){
} }
function addNewRecord(){ function saveRecord(){
var params; var params;
var firstName = $('#newFirstName').val(); var firstName = $('#newFirstName').val();
var lastName = $('#newLastName').val(); var lastName = $('#newLastName').val();
var email = $('#newEmail').val(); var email = $('#newEmail').val();
var recordId = $('#existingRecordId').val();
if(firstName == '' || lastName == ''){ if(firstName == '' || lastName == ''){
notice_create('error', '<?=i18n('First and last names are required fields'); ?>', 5000); notice_create('error', '<?=i18n('First and last names are required fields'); ?>', 5000);
return false; return false;
} }
// don't allow multiple submits
if(awaiting_ajax) return false; if(awaiting_ajax) return false;
awaiting_ajax = true; awaiting_ajax = true;
params = [ params = [
{ 'name' : 'firstName', 'value': firstName }, { 'name' : 'recordId', 'value' : recordId },
{ 'name' : 'lastName', 'value': lastName }, { 'name' : 'firstName', 'value' : firstName },
{ 'name' : 'email', 'value': email } { 'name' : 'lastName', 'value' : lastName },
{ 'name' : 'email', 'value' : email }
]; ];
$("#debug").load("schoolstudents.php?action=new", params, function(response){ if(recordId != -1){
if(savedRecord){ $("#debug").load("schoolstudents.php?action=update", params, function(response){
$('#studentList > tbody:last').append(response); if(success){
$('#newFirstName').select(); $('#' + recordId).remove();
$('#newFirstName').attr({ value: '' }); $('#studentList > tbody:last').append(response);
$('#newLastName').attr({ value: '' }); clearFields();
$('#newEmail').attr({ value: '' }); }
} awaiting_ajax = false;
awaiting_ajax = false; });
}); }else{
$("#debug").load("schoolstudents.php?action=new", params, function(response){
if(success){
$('#studentList > tbody:last').append(response);
clearFields();
}
awaiting_ajax = false;
});
}
} }
</script> </script>
<?php <?php
@ -193,11 +262,18 @@ function draw_list(){
<th></th> <th></th>
</tr> </tr>
<tr> <tr>
<th><?=i18n("New:")?></th> <th><span
onclick="clearFields();"
onmouseover="document.body.style.cursor='pointer';"
onmouseout="document.body.style.cursor='auto';">
<?=i18n("New:")?>
</span>
<input type="hidden" id="existingRecordId" value="-1"></input>
</th>
<th><input type="text" id="newFirstName"></input></th> <th><input type="text" id="newFirstName"></input></th>
<th><input type="text" id="newLastName"></input></th> <th><input type="text" id="newLastName"></input></th>
<th><input type="text" id="newEmail"></input></th> <th><input type="text" id="newEmail"></input></th>
<th><button name="newRecord" onclick="addNewRecord()"><?=i18n("Add")?></button></th> <th><button name="newRecord" onclick="saveRecord()"><?=i18n("Add")?></button></th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -207,6 +283,7 @@ function draw_list(){
$query .= ' JOIN users_conferences_link ucl ON ucl.users_uid = users_student.users_id'; $query .= ' JOIN users_conferences_link ucl ON ucl.users_uid = users_student.users_id';
$query .= ' WHERE schools_id = ' . $_SESSION['schoolid']; $query .= ' WHERE schools_id = ' . $_SESSION['schoolid'];
$query .= ' AND ucl.conferences_id=' . $conference['id']; $query .= ' AND ucl.conferences_id=' . $conference['id'];
$query .= ' AND users.deleted = "no"';
$data = mysql_query($query); $data = mysql_query($query);
if($data){ if($data){
while($row = mysql_fetch_array($data)){ while($row = mysql_fetch_array($data)){
@ -214,7 +291,6 @@ function draw_list(){
echo user_row($uid, $row['username'], $row['firstname'], $row['lastname'], $row['email']); echo user_row($uid, $row['username'], $row['firstname'], $row['lastname'], $row['email']);
} }
} }
?> ?>
</tbody> </tbody>
</table> </table>