Added teacher abillity to add/remove students

Updated the user_save to output more meaningful errors
Updates on 'questions' and 'pagetext' tables to change the 0 conference ID to -1, matching the -1 year
This commit is contained in:
jacob 2010-11-29 22:54:12 +00:00
parent 1e1fa0097a
commit 39f53e4168
6 changed files with 365 additions and 19 deletions

View File

@ -136,19 +136,19 @@ function account_create($username,$password=NULL)
/* Sanity check username */
if(!account_valid_user($username)) {
return -1;
return i18n("Invalid user name \"%1\"", array($username));
}
/* Make sure the user doesn't exist */
$us = mysql_real_escape_string($username);
$q = mysql_query("SELECT * FROM accounts WHERE username='$us'");
if(mysql_num_rows($q)) {
return -2;
return i18n("The username \"%1\" is already in use", array($username));
}
//if the password is set, make sure its valid, if its null, thats OK, it'll get generated and set by account_set_password
if($password && !account_valid_password($password)) {
return -3;
return i18n("Invalid password");
}
/* Create the account */

12
api.php
View File

@ -261,15 +261,9 @@ switch($request[0]) {
$account=account_load($a['id']);
$ret['status']="ok";
$ret['account']=$account;
}
else {
$ret['status']="error";
switch($a) {
case -1: $ret['error']="invalid username"; break;
case -2: $ret['error']="username already exists"; break;
case -3: $ret['error']="invalid password"; break;
default: $ret['error']="unknown account creation error"; break;
}
}else{
$ret['status'] = "error";
$ret['error'] = $a;
}
} else {
$ret['status']="error";

View File

@ -1 +1 @@
225
226

2
db/db.update.226.sql Normal file
View File

@ -0,0 +1,2 @@
UPDATE `questions` SET conferences_id = -1 WHERE `year` = -1 AND conferences_id = 0;
UPDATE `pagetext` SET conferences_id = -1 WHERE `year` = -1 AND conferences_id = 0;

352
invitestudents.php Normal file
View File

@ -0,0 +1,352 @@
<?php
require_once('common.inc.php');
require_once('user.inc.php');
user_auth_required("teacher");
if(array_key_exists('action', $_POST)){
switch($_POST['action']){
case 'invite':
// get the schoolId
$u = user_load($_SESSION['users_id']);
$schoolId = $u['schools_id'];
if($_POST['firstname'] && $_POST['lastname'] && $_POST['email'] && $_POST['password'] && $_POST['grade']){
// first we create the account
$account = account_create($_POST['email'], $_POST['password']);
if(!is_array($account)){
echo error(i18n("Error creating account: %1", array($account)));
break;
}else{
// ok, let's add their e-mail address as well
$account['email'] = $_POST['email'];
}
// now the user
$user = user_create($account['id'], $conference['id']);
if(!is_array($user)){
echo i18n("Error creating user");
break;
}
// now give them a student role
$result = user_add_role($user, 'participant');
if($result != 'ok'){
echo i18n("Error adding 'participant' role: %1", array($result));
break;
}
// and populate their user data
$user['grade'] = $_POST['grade'];
$user['schools_id'] = $schoolId;
$user['firstname'] = $_POST['firstname'];
$user['lastname'] = $_POST['lastname'];
// $user['email'] = $_POST['email'];
$message = user_save($user);
if($message != 'ok'){
echo error(i18n("Error saving user: %1", array($message)));
break;
}
// we have saved the user successfully
happy_(i18n("The participant has been successfully invited"));
}else{
error_(i18n("All fields are required for invitations"));
}
draw_student_list($schoolId);
break;
case 'uninvite':
$u = user_load($_SESSION['users_id']);
$schoolId = $u['schools_id'];
if(!is_numeric($_POST['studentid'])) break;
$u = user_load($_POST['studentid']);
if(is_array($u)){
user_remove_role($u, 'participant');
draw_student_list($schoolId);
}
break;
}
exit();
}
send_header("Invite Students");
draw_page();
send_footer();
function draw_page(){
global $config, $conference;
$u = user_load($_SESSION['users_id']);
?>
<script type="text/javascript">
var busy = false;
function setBusy(state){
if(state == true){
document.body.style.cursor = 'wait';
busy = true;
}else{
document.body.style.cursor = 'auto';
busy = false;
}
}
function invite(){
if(busy == false){
setBusy(true);
$.post('invitestudents.php',
{
'action':'invite',
'grade': $('#grade').val(),
'email': $('#email').val(),
'password': $('#password').val(),
'emailcontact': $('#emailcontact').val(),
'firstname': $('#firstname').val(),
'lastname': $('#lastname').val()
},
function(result){
$('#studentlist').html(result);
setBusy(false);
}
);
}
}
function uninvite(student_id){
if(busy == false){
setBusy(true);
$.post('invitestudents.php', {'action':'uninvite', 'studentid':student_id}, function(result){
$('#studentlist').html(result);
setBusy(false);
});
}
}
</script>
<?php
// get the school information
$schoolId = $u['schools_id'];
//$query = "SELECT * FROM schools WHERE id='$schoolId' AND conferences_id='".$conference['id']."'";
$query = "SELECT * FROM schools WHERE id='$schoolId'";
$q = mysql_query($query);
$school = mysql_fetch_object($q);
if(!$school){
echo "<p>" . i18n("Invalid school ID") . "</p>";
return;
}
switch($config['participant_registration_type']){
case "schoolpassword":
echo "<h4>".i18n("Participant Registration Password")."</h4>";
echo "<p>" . i18n("In order for your school's students to register for the fair, they will need to know your specific school registration password") . "</p>";
echo "<p>" . i18n("Registration Password: <strong>%1</strong>",array($school->registration_password)) . "</p>";
break;
case "invite": case "openorinvite":
draw_invitation_form($school);
break;
case "open":
echo "<p>" . i18n("Registration for this conference is open.") . "</p>";
break;
default:
echo $config['participant_registration_type'];
}
}
function draw_invitation_form($school){
global $config, $conference;
$q=mysql_query("SELECT (NOW()>'".$config['dates']['regopen']."' AND NOW()<'".$config['dates']['regclose']."') AS datecheck");
$datecheck=mysql_fetch_object($q);
/*
$query = "
SELECT
JOIN user_roles ON user_roles.users_id = users.id
JOIN roles ON roles.id = user_roles.roles_id
WHERE roles.`type` = 'participant'
*/
/* $q=mysql_query("SELECT students.*,
registrations.num,
registrations.emailcontact
FROM
students,
registrations
WHERE
students.schools_id='".$school->id."'
AND students.conferences_id='".$conference['id']."'
AND students.registrations_id=registrations.id
ORDER BY
lastname,
firstname");
*/
$currentinvited= 'FIXME';//mysql_num_rows($q);
if($datecheck!=0)
{
echo i18n("In order for your school's students to register for the fair, you will need to invite them to register. Simply enter their email address below to invite them to register. <b>Important</b>: for group projects, only add one of the participants, that participant will then add the other group member(s) to the project");
echo "<br />";
echo "<br />";
$okaygrades=array();
if($config['participant_registration_type']=="invite")
{
if($school->projectlimitper=="total")
{
if($school->projectlimit){
echo i18n("You have invited %1 of %2 total projects for your school",array($currentinvited, $school->projectlimit));
if($currenteinvited < $school->projectlimit){
for($a=$config['mingrade'];$a<=$config['maxgrade'];$a++)
$okaygrades[]=$a;
}
}
else{
echo i18n("You have invited %1 project(s) for your school",array($currentinvited, $school->projectlimit));
for($a=$config['mingrade'];$a<=$config['maxgrade'];$a++)
$okaygrades[]=$a;
}
}
else if($school->projectlimitper=="agecategory")
{
echo "<br />";
$catq=mysql_query("SELECT * FROM projectcategories WHERE conferences_id='".$conference['id']."' ORDER BY id");
while($catr=mysql_fetch_object($catq)){
$q2=mysql_query("SELECT COUNT(students.id) AS num
FROM
students,
registrations
WHERE
students.schools_id='".$school->id."'
AND students.grade>='$catr->mingrade'
AND students.grade<='$catr->maxgrade'
AND students.conferences_id='".$conference['id']."'
AND students.registrations_id=registrations.id
");
echo mysql_error();
$r2=mysql_fetch_object($q2);
$currentinvited = $r2->num;
if($currentinvited < $school->projectlimit || $school->projectlimit==0){
for($a=$catr->mingrade;$a<=$catr->maxgrade;$a++)
$okaygrades[]=$a;
}
echo i18n("You have invited %1 of %2 total projects for for the %3 age category",array($currentinvited,$school->projectlimit,i18n($catr->category)));
echo "<br />";
}
}
else
{
//hmm projectlimitper has not been set
//so we have no limits, anyone can register or they can add as many as they want.
for($x=$config['mingrade']; $x<=$config['maxgrade']; $x++)
$okaygrades[]=$x;
}
}
else
{
// this could be an else if $config['participant_registration_type']=="openorinvite" )
//because openorinvite is the only other option
//so we have no limits, anyone can register or they can add as many as they want.
//you cannot enforce limits when the system is 'open' because anyone can choose any school
//and if its openorinvite then whatever happens in the inviter still morepeople can be added
//by themselves, so there's no point in having limits.
for($x=$config['mingrade']; $x<=$config['maxgrade']; $x++)
$okaygrades[]=$x;
}
echo "<br />";
if(count($okaygrades))
{
echo "<form>";
echo "<input type=hidden name=action value=\"invite\">";
echo "<table>";
echo "<tr><td><nobr>".i18n("Student Email Address")."</nobr></td><td><input type=\"text\" id=\"email\" /></td><td>".i18n("Or unique username for student")."</td></tr>";
echo "<tr><td><nobr>".i18n("Student Password")."</nobr></td><td colspan=\"2\"><input type=\"text\" id=\"password\" /></td><td></tr>";
echo "<tr><td><nobr>".i18n("Contact Email Address")."</nobr></td><td><input type=\"text\" id=\"emailcontact\" /></td><td>".i18n("Any emails that would normally go to the student, will also be sent to this address")."</td></tr>";
echo "<tr><td><nobr>".i18n("Student First Name")."</nobr></td><td colspan=\"2\"><input type=\"text\" id=\"firstname\" /></td></tr>";
echo "<tr><td><nobr>".i18n("Student Last Name")."</nobr></td><td colspan=\"2\"><input type=\"text\" id=\"lastname\" /></td></tr>";
echo "<tr><td><nobr>".i18n("Grade")."</nobr></td><td colspan=\"2\">";
echo "<select id=\"grade\">\n";
echo "<option value=\"\">".i18n("Select Grade")."</option>\n";
foreach($okaygrades AS $gr)
{
echo "<option value=\"$gr\">$gr</option>\n";
}
echo "</td></tr>";
echo "</table>";
echo '<button onclick="invite(); return false;">' . i18n("Invite Participant") . '</button>';
echo "</form>";
}
else
{
echo notice(i18n("You have invited the maximum number of participants for your school"));
}
}
echo "<br />";
echo "<h4>".i18n("Invited participants from your school")."</h4>";
echo "<div id=\"studentlist\">";
draw_student_list($school->id);
echo "</div>";
}
function getStudents($schoolId){
$result = array();
$query = mysql_query("
SELECT users.id, users.firstname, users.lastname, accounts.username, users.grade
FROM users
JOIN accounts ON users.accounts_id = accounts.id
WHERE users.id IN (
SELECT users_id FROM user_roles
JOIN roles ON roles.id = user_roles.roles_id
WHERE roles.`type` = 'participant'
)
AND schools_id = {$schoolId}
");
while($row = mysql_fetch_assoc($query)){
$result[] = $row;
}
return $result;
}
function draw_student_list($schoolId){
global $config;
$studentList = getStudents($schoolId);
if(count($studentList) > 0){
echo "<table class=\"summarytable\">";
echo "<tr><th>".i18n("Last Name")."</th><th>".i18n("First Name")."</th>";
echo "<th>".i18n("Username")."</th>";
echo "<th>".i18n("Grade")."</th>";
echo "<th>".i18n("Registration Number")."</th>";
echo "<th colspan=\"2\">".i18n("Actions")."</th></tr>";
foreach($studentList as $student){
echo "<tr>";
echo "<td>{$student['lastname']}</td>";
echo "<td>{$student['firstname']}</td>";
echo "<td>{$student['username']}</td>";
echo "<td>{$student['grade']}</td>";
echo "<td><!-- FIXME --></td>"; // FIXME this should be the registration number
echo "<td><a onclick=\"uninvite({$student['id']}); return false;\"><img border=0 src=\"".$config['SFIABDIRECTORY']."/images/16/button_cancel.".$config['icon_extension']."\"></a></td>";
echo "</tr>";
}
echo "</table>";
}else{
echo i18n("You have not yet invited any participants from your school");
}
}

View File

@ -653,7 +653,6 @@ function user_save(&$u)
$fields_for_role['sponsor'] = array('sponsors_id','primary','position');
$fields_for_role['teacher'] = array();
$fields_for_role['volunteer'] = array('languages');
/* Merge fields as necessary, build a big list of fields to save */
foreach($new_roles as $r) {
if(!array_key_exists($r, $fields_for_role)) continue;
@ -1001,6 +1000,9 @@ function user_add_role(&$u, $role, $password = null){
$_SESSION['roles'][] = $role;
}
// also, update the user:
$u = user_load($u['id']);
}
return $result;
}
@ -1507,11 +1509,7 @@ function user_invite($username, $password, $email, $roles_id){
// all fields have been passed in, let's go ahead and create the account/user/role
$newAccount = account_create($username, $password);
if(!is_array($newAccount)){
switch($newAccount){
case -1: $returnval = "Invalid username"; break;
case -2: $returnval = "Username already in use"; break;
case -3: $returnval = "Invalid password"; break;
}
$returnval = $newAccount;
}
}