Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Discussion Section / Office Hours Poll Taker

by hossman (Prior)
on Sep 14, 2004 at 05:14 UTC ( #390742=sourcecode: print w/replies, xml ) Need Help??
Category: CGI Programming
Author/Contact Info hossman
Description:

This is a CGI for gathering personal info from students, and what times of the day are convinient for them to have Discussion Sections (or Office hours). The DATA section shows an example of what the HTML form that posts to it should look like.

I've also written a script to parse the output into an Excel file

#!/usr/local/bin/perl

# $Id: class-schedule-form.cgi,v 1.7 2004/08/18 18:31:12 hossman Exp $
# $Source: /home/hossman/cvs_archive/code/perl/class-scheduler/class-s
+chedule-fo
rm.cgi,v $
#

use warnings;
use strict;

use Fcntl qw(:flock :seek);
use CGI;

use vars qw($file $thank);
use vars qw($email $fname $lname $sid $status @sections $none);
use vars qw($q @err);
            
# the file all of our data goes to
$file = "./class-schedule-data.txt";

# the thank you page
$thank = "./thanks.html";

eval {
    $q = new CGI;
    
    # get our section list, ensure all be numbers
    # (don't bother reporting errors, only comes from spoofers, so fuc
+k them)
    @sections = $q->param('section');
    $_ =~ s/[^\d]//g foreach @sections;
    
    # get all vals, and strip out colon, and line enders
    ($email  = $q->param('email')  || '') =~ s/:|\n|\r//g;
    ($fname  = $q->param('fname')  || '') =~ s/:|\n|\r//g;
    ($lname  = $q->param('lname')  || '') =~ s/:|\n|\r//g;
    ($sid    = $q->param('sid')    || '') =~ s/:|\n|\r//g;
    ($status = $q->param('status') || '') =~ s/:|\n|\r//g;
    ($none   = $q->param('none')   || '') =~ s/:|\n|\r//g;

    push @err, "You must specify a valid Student ID"
        unless $sid    =~ /^\d+$/;
    push @err, "You must specify your First Name"    unless $fname =~ 
+/.+/;
    push @err, "You must specify your Last Name"     unless $lname =~ 
+/.+/;
    push @err, "You must specify your Email Address" unless $email =~ 
+/.+/;
    push @err, "You must specify your Enrollment Status"
        unless $status =~ /^(W|E|N)$/;
    if (0 == @sections and ! $none) {
        push @err, "You must check the sections you can attend " .
            "or indicate that you can't attend any";
    } elsif (0 < @sections and $none) {
        push @err, "You can not check sections that you can attend " .
            "AND indicate that you can't attend any";
    }
    
    die "User Input Errors" if @err;

    # lock our file, and write out to it
    open(FILE, ">>$file") or die "couldn't open data file to append";
    flock(FILE, LOCK_EX) or die "can't lock data file";
    seek(FILE, 0, SEEK_END) or die "can't see data file";
    print FILE "$sid:$lname:$fname:$email:$status:",join(" ",@sections
+),"\n";
    close FILE;
    
    print $q->redirect($thank);
};
if ($@) {
    my $msg = $@;
    push @err, "There was a server problem.  Please contact your GSI."
        unless @err;
    
    print
        $q->header(-status=>500),
        $q->start_html('Something Went Wrong'),
        $q->h2('Something Went Wrong'),
        $q->ul(map { $q->li($_) } @err ),
        $q->end_html;
    die $msg;
}


__DATA__

<form action="./class-schedule-form.cgi">

SID: <input type="text" name="sid" /></td>
<br/>
First Name: <input type="text" name="fname" />
<br/>
Last Name: <input type="text" name="lname" />
<br/>
Email Address: <input type="text" name="email" />
<br/>
<p>
Enrollment Status:
<select name="status">
  <option value="none" selected>Pick One</option>
  <option value="E">Enrolled</option>
  <option value="W">Waitlisted</option>
  <option value="N">Neither</option>
</select>
<br/>
<p>
Sections Available...<br/>
<input type="checkbox" name="section" value="0" /> Mon 10 - 11am<br/>
<input type="checkbox" name="section" value="1" /> Mon 2 - 3pm<br/>
<input type="checkbox" name="section" value="2" /> Mon 3 - 4pm<br/>
<input type="checkbox" name="section" value="3" /> Mon 4 - 5pm<br/>
<input type="checkbox" name="section" value="4" /> Tue 12 - 1pm<br/>
<input type="checkbox" name="section" value="5" /> Tue 1 - 2pm<br/>
<input type="checkbox" name="section" value="6" /> Tue 4 - 5pm<br/>
<input type="checkbox" name="section" value="7" /> Wed 9 - 10am<br/>
<input type="checkbox" name="section" value="8" /> Wed 10 - 11am<br/>
<input type="checkbox" name="section" value="9" /> Thur 12 - 1pm<br/>
<input type="checkbox" name="section" value="10" /> Thur 1 - 2pm<br/>
<p>
<input type="checkbox" name="none" value="1" /> I can't make any of th
+ese<br/>
<p>
<input type="submit" value="Submit"/>
</form>

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://390742]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2021-01-21 18:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?