Rev 112 | Rev 167 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
#!/usr/bin/perl# Redirect error messages to a log of my choosing. (it's annoying to filter for errors in the shared env)my $error_log_path = $ENV{SERVER_NAME} eq "volunteers.rollercon.com" ? "/home3/rollerco/logs/" : "/tmp/";close STDERR;open STDERR, '>>', $error_log_path.'vorc_error.log' or warn "Failed to open redirected logfile ($0): $!";#warn "Redirecting errors to ${error_log_path}vorc_error.log";use strict;use cPanelUserConfig;use WebDB;use HTML::Tiny;use RollerCon;use CGI qw/param header start_html url/;my $h = HTML::Tiny->new( mode => 'html' );my %F;my $cookie_string = authenticate (RollerCon::USER) || die;our ($EML, $PWD, $LVL) = split /&/, $cookie_string;my $user = getUser ($EML);$user->{department} = convertDepartments $user->{department};my $DepartmentNames = getDepartments ();my $username = $user->{derby_name};my $RCid = $user->{RCid};my $RCAUTH_cookie = CGI::Cookie->new(-name=>'RCAUTH',-value=>"$cookie_string",-expires=>"+30m");my $YEAR = 1900 + (localtime)[5];my $pageTitle = "View MVP Class";my $homeURL = "/schedule/";my $DBTable = "class";my %FIELDS = (id => [qw(ClassID 5 auto static )],name => [qw(ClassName 10 text required )],coach => [qw(Coach 15 select required )],date => [qw(Date 20 date required )],location => [qw(Location 25 text required )],level => [qw(Level 27 text required )],start_time => [qw(Start 30 time required )],end_time => [qw(End 35 time required )],capacity => [qw(Capacity 40 number required )],note => [qw(Notes 45 textarea )],);my %fieldDisplayName = map { $_ => $FIELDS{$_}->[0] } keys %FIELDS;my %fieldType = map { $_ => $FIELDS{$_}->[2] } keys %FIELDS;my @requiredFields = sort fieldOrder grep { defined $FIELDS{$_}->[3] } keys %FIELDS;my @DBFields = sort fieldOrder grep { $fieldType{$_} =~ /^(text|select|number|switch|date|time|auto)/ } keys %FIELDS;my @ROFields = sort fieldOrder grep { $fieldType{$_} =~ /^(readonly)/ } keys %FIELDS;my $primary = $DBFields[0];sub fieldOrder {$FIELDS{$a}->[1] <=> $FIELDS{$b}->[1];}sub saveForm {error ("ERROR: Only SysAdmins can change games.") unless $LVL >= RollerCon::ADMIN;my $FTS = shift;my $dbh = WebDB::connect ();if ($FTS->{$DBFields[0]} eq "NEW") {$dbh->do ("INSERT INTO $DBTable(name,coach,date,location,level,start_time,end_time,capacity,note)VALUES(?,?,?,?,?,?,?,?)",undef,$FTS->{name}, $FTS->{coach}, $FTS->{date}, $FTS->{location}, $FTS->{level}, $FTS->{start_time}, $FTS->{end_time}, $FTS->{capacity}, $FTS->{note});($FTS->{id}) = $dbh-> selectrow_array ("select max(id) from $DBTable where name = ? and date = ? and location = ? and start_time = ? and end_time = ?", undef, $FTS->{name}, $FTS->{date}, $FTS->{location}, $FTS->{start_time}, $FTS->{end_time});logit ($RCid, "$username created new class ($FTS->{id}, $FTS->{name}, $FTS->{date}, $FTS->{location}, $FTS->{start_time}, $FTS->{end_time}, $FTS->{capacity})");# Each class has a volunteer shift for the coach.$dbh->do ("INSERT INTO shift(dept,role,type,date,location,start_time,end_time,doubletime,note,assignee_id)VALUES (?,?,?,?,?,?,?,?,?,?)",undef,"COA", "Coach", "selected", $FTS->{date}, $FTS->{location}, $FTS->{start_time}, $FTS->{end_time}, 1, $FTS->{name}, $FTS->{coach});} else {$dbh->do ("UPDATE $DBTableSET name=?, coach=?, date=?, location=?, level=?, start_time=?, end_time=?, capacity=?, note=?WHERE id = ?",undef,$FTS->{name}, $FTS->{coach}, $FTS->{date}, $FTS->{location}, $FTS->{level}, $FTS->{start_time}, $FTS->{end_time}, $FTS->{capacity}, $FTS->{note}, $FTS->{id});logit ($RCid, "$username updated class ($FTS->{id}, $FTS->{name}, $FTS->{date}, $FTS->{location}, $FTS->{level}, $FTS->{start_time}, $FTS->{end_time}, $FTS->{capacity})");# Update the volunteer shift for the coach too.if ($FTS->{classshiftid}) {$dbh->do ("UPDATE shiftSET date=?, location=?, start_time=?, end_time=?, note=?, assignee_id=?WHERE id = ?",undef,$FTS->{date}, $FTS->{location}, $FTS->{start_time}, $FTS->{end_time}, $FTS->{name}, $FTS->{coach}, $FTS->{classshiftid});} else {$dbh->do ("INSERT INTO shift(dept,role,type,date,location,start_time,end_time,doubletime,note,assignee_id)VALUES (?,?,?,?,?,?,?,?,?,?)",undef,"COA", "Coach", "selected", $FTS->{date}, $FTS->{location}, $FTS->{start_time}, $FTS->{end_time}, 1, $FTS->{name}, $FTS->{coach});}}$dbh->disconnect (); # stored into database successfully.return $FTS->{id};}sub delete_item {error ("ERROR: Only SysAdmins can change games.") unless $LVL >= RollerCon::ADMIN;my $X = shift;my $dbh = WebDB::connect ();## First get the shift ID of the volunteer shift of the coach (and then delete it)my ($classshiftid) = $dbh->selectrow_array ("select shift.id from shiftjoin class where shift.date = class.date and shift.start_time = class.start_time and shift.location = class.location and dept = ? and class.id = ?",undef, "COA", $X->{$primary});$dbh->do ("delete from shift where id = ?", undef, $classshiftid);$dbh->do ("delete from $DBTable where $primary = ?", undef, $X->{$primary});## If we're deleting a class, we need to also delete all of the sign-ups## TBD: Maybe email all of the attendees that their class is being deleted first?$dbh->do ("delete from assignment where Gid = ? and role like ?", undef, $X->{$primary}, "CLA".'%');$dbh->disconnect ();logit ($RCid, "$username deleted Class ($X->{$primary})");print "Class Deleted: $X->{$primary}", $h->br;print &formField ("Cancel", "Back", "POSTSAVE");}sub select_coach {my $selection = shift // "";return $h->select ({ name=>"coach" }, [ $h->option (""), fetchDerbyNameWithRCid ("COA", $selection) ]);};print header (),start_html (-title => $pageTitle, -style => {'src' => "/style.css"} );print $h->div ({ class => "accent pageheader" }, [$h->h1 ($pageTitle),$h->div ({ class=>"sp0" }, [$h->div ({ class=>"spLeft" }, []),$h->div ({ class=>"spRight" }, [$h->input ({ type=>"button", value=>"Home", onClick=>"window.location.href='$homeURL'" }),]),]),]);my $choice = param ("choice") // "";if ($choice eq "Save") {process_form ();} elsif (defined (param ($primary))) {my $thing = param ($primary);if ($choice eq "Delete") {delete_item ({ $primary => $thing });} else {display_form ({ $primary => $thing }, $choice);}} else {display_form (); # blank form}print $h->close ("html");sub display_form {my $R = shift;my $view = shift // "";my $actionbutton;my $coachRCid;$view = "View" unless $LVL >= RollerCon::ADMIN;if ($view eq "POSTSAVE" and $R->{$primary} eq "NEW") {print &formField ("Cancel", "Back", "POSTSAVE");return;}if ($R) {# we're dealing with an existing thing. Get the current values out of the DB...my $dbh = WebDB::connect ();@F{@DBFields} = $dbh->selectrow_array ("SELECT ". join (", ", @DBFields) ." FROM $DBTable WHERE $primary = ?",undef, $R->{$primary});my ($classshiftid) = $dbh->selectrow_array ("select id from shift where dept = ? and date = ? and start_time = ? and location = ?",undef, "COA", $F{date}, $F{start_time}, $F{location});$dbh->disconnect ();# did we find a record?error ("Cannot find a database entry for Class with id: '$R->{$primary}'") unless defined $F{$DBFields[0]};# If the DB returns a null value, HTML::Tiny doesn't like it, so make sure nulls are converted to empty strings.map { $F{$_} = "" unless $F{$_} } @DBFields;if ($view eq "Update") {# We'd like to update that thing, give the user a form...print $h->p ("Updating Class: $R->{$primary}...");foreach (@DBFields) {$F{$_} = formField ($_, $F{$_});}$F{$DBFields[0]} .= $h->input ({ type=>"hidden", name=>$DBFields[0], value=> $F{$DBFields[0]} });$F{$DBFields[0]} .= $h->input ({ type=>"hidden", name=>"classshiftid", value=> $classshiftid });$actionbutton = formField ("choice", "Save");$actionbutton .= formField ("Cancel");} elsif ($view eq "Copy") {# We'd like to copy that thing, give the user a form...print $h->p ("Copying Class: $R->{$primary}...");foreach (@DBFields) {$F{$_} = formField ($_, $F{$_});}$F{$DBFields[0]} = "COPY".$h->input ({ type=>"hidden", name=>$DBFields[0], value=> "NEW" });$actionbutton = formField ("choice", "Save");$actionbutton .= formField ("Cancel");} else {# We're just looking at it...print $h->p ("Viewing Class: $R->{$primary}...");$F{$DBFields[0]} .= $h->input ({ type=>"hidden", name=>$DBFields[0], value=> $F{$DBFields[0]} });$coachRCid = $F{coach};$F{coach} = $h->a ({ href=>"/schedule/view_user.pl?RCid=$F{coach}" }, getUserDerbyName $F{coach});$F{start_time} = convertTime $F{start_time};$F{end_time} = convertTime $F{end_time};# if ($F{assignee_id}) {# my $temp;# $temp = getUserDerbyName ($F{assignee_id});# $temp .= " ".$h->a ({ onClick=>"if (confirm('Really? You want to drop this person from the shift?')==true) { window.open('make_shift_change.pl?change=del&RCid=$F{assignee_id}&id=$R->{$primary}','Confirm Shift Change','resizable,height=260,width=370'); return false; }" }, "[DROP]");# $F{assignee_id} = $temp;# } else {# $F{assignee_id} = $h->a ({ onClick=>"window.open('make_shift_change.pl?change=lookup&id=$R->{$primary}','Confirm Shift Change','resizable,height=260,width=370'); return false;" }, "[ADD USER]");# }$actionbutton = formField ("choice", "Update") unless $LVL < RollerCon::ADMIN;if ($view eq "POSTSAVE" or $choice eq "View") {$actionbutton .= formField ("Cancel", "Back", "POSTSAVE");} else {$actionbutton .= formField ("Cancel", "Back");}}} else {error ("No Game id provided.") unless $LVL >= RollerCon::ADMIN;print $h->p ("Adding a new Class...");foreach (@DBFields) {$F{$_} = formField ($_);}$F{$DBFields[0]} = "NEW".$h->input ({ type=>"hidden", name=>$DBFields[0], value=> "NEW" });$actionbutton = formField ("choice", "Save");$actionbutton .= formField ("Cancel");}print $h->open ("form", { action => url (), name=>"Req", method=>"POST" });print $h->div ({ class=>"sp0" },$h->div ({ class=>"rTable" }, [ map ({$h->div ({ class=>"rTableRow" }, [$h->div ({ class=>"rTableCell right top", style=>"font-size:unset;" }, "$fieldDisplayName{$_}: "),$h->div ({ class=>"rTableCell", style=>"font-size:unset;" }, $F{$_})])} sort fieldOrder keys %FIELDS),]));print $actionbutton;print $h->close ("form");my $dbh = WebDB::connect ();my ($class_is_over) = $dbh->selectrow_array ("select 1 from v_class where $primary = ? and concat_ws(' ', date, end_time) < date_sub(now(), interval 2 hour)", undef, $R->{$primary});# if (($LVL >= RollerCon::ADMIN or $user->{department}->{MVP} >= RollerCon::LEAD) and !$class_is_over) {if ($LVL >= RollerCon::ADMIN or $user->{department}->{MVP} >= RollerCon::LEAD) {my ($count) = $dbh->selectrow_array ("select count from v_class where $primary = ?", undef, $R->{$primary});if ($F{$DBFields[0]} !~ /^(NEW|COPY)/) {my @A = @{$dbh->selectall_arrayref ("select role, official.RCid, derby_name from v_class_signup join official on v_class_signup.RCid = official.RCid where $primary = ? order by role", undef, $R->{$primary})};my $classkey = join '|', $F{date}, $F{start_time}, $F{location};# my $adduserlink = (scalar @A < $F{capacity} and ($LVL >= RollerCon::ADMIN or $user->{department}->{MVP} >= RollerCon::LEAD)) ? $h->a ({ style=>"color:unset;font-size:x-small;", onClick=>"window.open('make_shift_change.pl?change=lookup&RCid=$RCid&id=$classkey','Confirm Shift Change','resizable,height=260,width=370'); return false;" }, "[ADD USER]") : "";my $adduserlink = ($LVL >= RollerCon::ADMIN or $user->{department}->{MVP} >= RollerCon::LEAD) ? $h->a ({ style=>"color:unset;font-size:x-small;", onClick=>"window.open('make_shift_change.pl?change=lookup&RCid=$RCid&id=$classkey','Confirm Shift Change','resizable,height=260,width=370'); return false;" }, "[ADD USER]") : "";print $h->br, $h->br;print $h->div ({ class=>"index", style=>"max-width:610px" }, [ $h->p ({ class=>"heading" }, [ "Sign-ups: ", $adduserlink ]),$h->ul ( [ map { $h->li ({ class=>"shaded", style=>"margin:4px;" },$h->div ({ class=>"lisp0" }, [$h->div ({ class=>"liLeft" }, [ $$_[0], ": ", $h->a ({ href=>"/schedule/view_user.pl?RCid=$$_[1]" }, $$_[2]) ]),$h->div ({ class=>"liRight" }, ($LVL >= RollerCon::ADMIN or $user->{department}->{MVP} >= RollerCon::LEAD) ? $h->a ({ style=>"font-size:small", onClick=>"if (confirm('Really? Delete $$_[2] from this class?')==true) { window.open('make_shift_change.pl?change=del&RCid=$$_[1]&id=$R->{$primary}&role=$$_[0]','Confirm Shift Change','resizable,height=260,width=370'); return false; }" }, "[DROP]") : "")])) } @A ])]);}}if (($LVL >= RollerCon::ADMIN or $user->{department}->{MVP} >= RollerCon::LEAD or $ORCUSER->{RCid} == $coachRCid) and $class_is_over) {my ($response_count) = $dbh->selectrow_array ("select count(distinct(RCid)) from v_survey_answer where classid = ?", undef, $R->{$primary});my $exclude_private = ($LVL >= RollerCon::ADMIN or $user->{department}->{MVP} >= RollerCon::LEAD) ? "" : "and private = 0";my @RESPONSES = @{$dbh->selectall_arrayref ("select qorder, type, question, average, qid from v_survey_results where classid = ? $exclude_private order by qorder", undef, $R->{$primary})};print $h->br, $h->br;print $h->div ({ class=>"index", style=>"max-width:610px" }, [ $h->p ({ class=>"heading" }, [ "Survey Results: ", $response_count." Response".(($response_count > 1) ? "s" : "") ]),$h->ul ( [ map { $h->li ({ class=>"shaded", style=>"margin:4px;" },$h->div ({ class=>"lisp0" }, [$h->div ({ class=>"liLeft" }, [ $$_[0], ": ", $$_[2] ]),$h->div ({ class=>"liRight" }, [ $$_[1] eq "text" ?$h->a ({ href=>"view_survey_comments.pl?classid=$R->{$primary}&qid=$$_[4]" }, [ $$_[3], (($$_[3] == 1) ? " comment" : " comments") ]) :$$_[3] ])# $h->div ({ class=>"liRight" }, [ $$_[3], $$_[1] eq "text" ? (($$_[3] == 1) ? " comment" : " comments") : "" ])# $h->div ({ class=>"liRight" }, $h->a ({ href=>"view_survey_comments.pl?classid=$R->{$primary}&qid=$$_[4]" }, $$_[3].$$_[1] eq "text" ? (($$_[3] == 1) ? " comment" : " comments") : ""))])) } @RESPONSES ])]);}$dbh->disconnect;}sub process_form {error ("ERROR: Only SysAdmins can change games.") unless $LVL >= RollerCon::ADMIN;my %FORM;foreach (keys %FIELDS) {if ($fieldType{$_} =~ /^text/) {$FORM{$_} = WebDB::trim param ($_) // "";} else {$FORM{$_} = param ($_) // "";}}$FORM{classshiftid} = param ("classshiftid") unless !param ("classshiftid");# check for required fieldsmy @errors = ();foreach (@requiredFields) {push @errors, "$fieldDisplayName{$_} is missing." if $FORM{$_} eq "";}push @errors, "Capacity should be a whole number." unless $FORM{capacity} =~ /^\d+$/;if (@errors) {print $h->div ({ class=>"error" }, [$h->p ("The following errors occurred:"),$h->ul ($h->li (@errors)),$h->p ("Please click your Browser's Back button to\n". "return to the previous page and correct the problem.")]);return;} # Form was okay.$FORM{id} = saveForm (\%FORM);print $h->p ({ class=>"success" }, "Class successfully saved.");display_form ({ $primary=>$FORM{id} }, "POSTSAVE");}sub error {my $msg = shift;print $h->p ({ class=>"error" }, "Error: $msg");print $h->close("html");exit (0);}sub formField {my $name = shift;my $value = shift // '';my $context = shift // '';my $type = $fieldType{$name} // "button";if ($type eq "button") {if ($name eq "Cancel") {if ($context eq "POSTSAVE") {return $h->input ({ type=>"button", value => $value ne '' ? $value : "Cancel" , onClick=>"window.location.href = \"classes.pl\"; return false;" });} else {return $h->input ({ type=>"button", value => $value ne '' ? $value : "Cancel" , onClick=>"history.back(); return false;" });}} else {return $h->input ({ type=>"submit", value => $value, name=>$name })}} elsif ($type eq "textarea") {return $h->tag ("textarea", {name => $name,override => 1,cols => 30,rows => 4}, $value);} elsif ($type eq "select") {no strict;return &{"select_".$name} ($value);} elsif ($type eq "auto") {return $name eq "assignee_id" ? getUserDerbyName ($value) : $value;} elsif ($type eq "time") {return $h->input ({name => $name,type => $type,value => $value,step => 900,required => [],override => 1,size => 30});} elsif ($type eq "number") {return $h->input ({ name=>$name, type=>"number", value=>$value, step=>1 });} elsif ($type eq "switch") {if ($value) {return $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>$name, value=>1, checked=>[] }), $h->span ({ class=>"slider round" })]);} else {return $h->label ({ class=>"switch" }, [$h->input ({ type=>"checkbox", name=>$name, value=>1 }), $h->span ({ class=>"slider round" })]);}} else {use tableViewer;if (inArray ($name, \@requiredFields)) {return $h->input ({name => $name,type => $type,value => $value,required => [],override => 1,size => 30});} else {return $h->input ({name => $name,type => $type,value => $value,override => 1,size => 30});}}}