Subversion Repositories VORC

Rev

Rev 121 | 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 $dbh = WebDB::connect;
my $homeURL = '/schedule/';
my $pageTitle = "MVP Class Feedback";

my $cookie_string = authenticate (RollerCon::USER) || die;
my $RCAUTH_cookie = CGI::Cookie->new(-name=>'RCAUTH',-value=>"$cookie_string",-expires=>"+30m");
my @ERRORS;
my $DEBUG = 0;

my $classid = param ("classid") // get_query_string_param ("classid");
error ("No ClassID.") unless $classid;

my ($registered) = $dbh->selectrow_array ("select id from v_class_signup where id = ? and RCid = ?", undef, $classid, $ORCUSER->{RCid});
error ("You don't appear to be registered for this class.") unless $registered;

my ($started) = $dbh->selectrow_array ("select id from v_class_signup where id = ? and RCid = ? and concat_ws(date, end_time) < date_sub(now(), interval 2 hour)", undef, $classid, $ORCUSER->{RCid});
error ("You can't provide feedback until after the class has ended.") unless $started;

my $insert_question = $dbh->prepare ("insert into survey_answer (classid, qid, RCid, response, added, page, maxpage) values (?, ?, ?, ?, date_sub(now(), interval 2 hour), ?, ?)");
my $update_maxpage  = $dbh->prepare ("update survey_answer set maxpage = ? where classid = ? and qid = ? and RCid = ?");

my $updated = save_survey ($classid) if param("submit") eq "Save";

my ($existing) = $dbh->selectrow_array ("select count(*) from v_survey_answer where classid = ? and RCid = ?", undef, $classid, $ORCUSER->{RCid});
my $mode = $existing ? "disabled" : "edit";
$mode = "edit" if param("submit") eq "Edit";

display_survey ($classid, $mode, $updated);


sub get_query_string_param {
  my $field = shift // "";
  
  foreach (split (/&/, $ENV{'QUERY_STRING'})) {
    my ($name, $value) = split (/=/);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9] [a-fA-F0-9])/pack("C", hex($1))/eg;
    $value =~ s/~!/ ~!/g;
    return $value if $name eq $field;
  }
  return "";
}


sub save_survey {
  my $CID = shift // error ("No ClassID");
  warn "Saving survey for RCID: $ORCUSER->{RCid}, Class: $CID" if $DEBUG;
  my $changes = 0;
  
  foreach (grep { /Question/ } param ()) {
    my ($prefix, $q) = split /_/;
    $changes += save_question ({ cid=>$CID, q=>$q, a=>WebDB::trim (scalar param ($_)) });
  }
  
  return $changes;
}

sub save_question {
  my $Q = shift // error ("No data provided to save.");
  return 0 unless answerChanged ($Q);
  warn "Saving question $Q->{q}: $Q->{a}" if $DEBUG;
  my ($maxpage) = $dbh->selectrow_array ("select ifnull(max(page), 0) from survey_answer where RCid = ? and classid = ? and qid = ?", undef, $ORCUSER->{RCid}, $Q->{cid}, $Q->{q});
  $maxpage++;
  $insert_question->execute ($Q->{cid}, $Q->{q}, $ORCUSER->{RCid}, $Q->{a}, $maxpage, $maxpage);
  $update_maxpage->execute ($maxpage, $Q->{cid}, $Q->{q}, $ORCUSER->{RCid});
  return 1;
}

sub answerChanged {
  my $A = shift // error ("No answer provided to check.");
  warn "Checking question $A->{q}: $A->{a}" if $DEBUG;
  my ($check) = $dbh->selectrow_array ("select count(*) from v_survey_answer where classid = ? and qid = ? and RCid = ? and response = ?", undef, $A->{cid}, $A->{q}, $ORCUSER->{RCid}, $A->{a});
  warn "Answer hasn't changed [$check], not saving." if $check and $DEBUG;
  return $check ? 0 : 1;
}

sub display_survey {
  my $CID = shift // error ("No ClassID");
  my $mode = shift // "disabled";
  my $saved = shift // "";
  
  print header (-cookie=> [ $RCAUTH_cookie ] ),
        start_html (-title => $pageTitle, -style => [{'src' => "/style.css"},{'src' => "/survey.css"}] );
  print<<JAVASCRIPT;
        <SCRIPT language="JavaScript">
        <!--
  function displayScore(score, targetspan) {
    sliderscore  = document.getElementById(score);
    emojis = document.getElementById(targetspan);
    
    if (sliderscore.value == 5) {
      emojis.innerHTML = "<img src='/images/star-icon.png' align='middle'><img src='/images/star-icon.png' align='middle'><img src='/images/star-icon.png' align='middle'><img src='/images/star-icon.png' align='middle'><img src='/images/star-icon.png' align='middle'>";
    } else if (sliderscore.value >= 4) {
      emojis.innerHTML = "<img src='/images/star-icon.png' align='middle'><img src='/images/star-icon.png' align='middle'><img src='/images/star-icon.png' align='middle'><img src='/images/star-icon.png' align='middle'>";
    } else if (sliderscore.value >= 3) {
      emojis.innerHTML = "<img src='/images/star-icon.png' align='middle'><img src='/images/star-icon.png' align='middle'><img src='/images/star-icon.png' align='middle'>";
    } else if (sliderscore.value >= 2) {
      emojis.innerHTML = "<img src='/images/star-icon.png' align='middle'><img src='/images/star-icon.png' align='middle'>";
    } else if (sliderscore.value >= 1) {
      emojis.innerHTML = "<img src='/images/star-icon.png' align='middle'>";
    } else if (sliderscore.value > -1) {
      emojis.innerHTML = "<img src='/images/meh.png' align='middle'>";
    } else if (sliderscore.value > -2) {
      emojis.innerHTML = "<img src='/images/angry.png' align='middle'>";
    } else if (sliderscore.value > -3) {
      emojis.innerHTML = "<img src='/images/angry.png' align='middle'><img src='/images/angry.png' align='middle'>";
    } else if (sliderscore.value > -4) {
      emojis.innerHTML = "<img src='/images/angry.png' align='middle'><img src='/images/angry.png' align='middle'><img src='/images/angry.png' align='middle'>";
    } else if (sliderscore.value > -5) {
      emojis.innerHTML = "<img src='/images/angry.png' align='middle'><img src='/images/angry.png' align='middle'><img src='/images/angry.png' align='middle'><img src='/images/angry.png' align='middle'>";
    } else if (sliderscore.value == -5) {
      emojis.innerHTML = "<img src='/images/angry.png' align='middle'><img src='/images/angry.png' align='middle'><img src='/images/angry.png' align='middle'><img src='/images/angry.png' align='middle'><img src='/images/angry.png' align='middle'>";
    } else {
      emojis.innerHTML = sliderscore.value;
    }
  }
        //-->
        </SCRIPT>
JAVASCRIPT
  
  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'" }),
      ]),
    ]),
  ]);
  
  print $h->div ({ id=>"savedmsg", class=>"saved fadeOut"}, "Changes Saved!") unless !$saved;
  
  my ($CREF) = $dbh->selectrow_hashref ("select * from v_class where id = ?", undef, $classid);
  $CREF->{time} = convertTime $CREF->{time};
  $CREF->{date} = $CREF->{date}." [".$CREF->{dayofweek}."]";
  print $h->ul ( { style=>"max-width:610px" }, [ map { $h->li ({ class=>"shaded", style=>"margin:4px;" },
    $h->div ({ class=>"lisp0" }, [
      $h->div ({ class=>"liNarrowLeft"  }, ucfirst $_),
      $h->div ({ class=>"liWideRight"   }, $CREF->{$_})
      ])
    ) } ("name", "coach", "date", "time", "location") ]);

  
  
#  print $h->div (["Class Details:",
#    $h->p ("Name: ".$CREF->{name},
#           "Coach: ".$CREF->{coach}
#           ),
#    ]), $h->hr;
    
  print $h->open ("form");
  print $h->input ({ type=>"hidden", name=>"classid", value=>$CID });
  foreach my $question (@{$dbh->selectall_arrayref ("select qid, question, type, required from survey_question order by qorder")}) {
    my ($qid, $text, $type, $required) = @{$question};
    my ($PRIOR_ANSWER) = $dbh->selectrow_array ("select response from v_survey_answer where classid = ? and qid=? and RCid=?", undef, $CID, $qid, $ORCUSER->{RCid});
  
    no strict;
    &{$type."_input"} ($qid, $text, $required, $PRIOR_ANSWER, $mode);
    print $h->br;
    
  }
  
  my $leftbutton = $mode eq "disabled" ?
    $h->input ({ type=>"button", name=>"back", value=>"Home", onClick=>"window.location.href='$homeURL'" }) :
    $h->input ({ type=>"reset", name=>"Reset", onClick=>"return confirm('Are you sure you want to reset all of your changes?');" });
    
  print $leftbutton,
        '&nbsp;',
        $h->input ({ type=>"submit", name=>"submit", value=> $mode eq "disabled" ? "Edit" : "Save" });
  
  print $h->close ("form", "html");
  exit;
}


sub range_input {
  my $Q = shift // "";
  my $T = shift // "";
  my $R = shift // "";
  my $V = shift // 0;
  my $M = shift // "disabled";
  
  my $question_id = "Question_".$Q;
  
  print $h->div ({ class => "slidecontainer"}, [
    $h->label ({ for => $question_id }, $T), $h->br,
    $h->input ({ type => "range",
                 id   => $question_id,
                 name => $question_id,
                 tabindex => $Q,
                 min  => "0",
                 max  => "5",
                 value => $V,
                 step  => ".1",
                 class => "rangeslider",
                 onInput => "displayScore('$question_id', 'question$Q');",
                 $M => [],
              }),
    $h->span ({ id => "question".$Q }, $h->img ({ src=>"/images/meh.png", align=>"middle" }))
  ]);
  print "<SCRIPT language='JavaScript'>displayScore('$question_id', 'question$Q');</SCRIPT>\n" if $V;
}

sub text_input {
  my $Q = shift // "";
  my $T = shift // "";
  my $R = shift // "";
  my $V = shift // "";
  my $M = shift // "disabled";
  
  my $question_id = "Question_".$Q;
  
  print $h->div ({ class=>"" }, [
    $h->label ({ for => $question_id }, $T), $h->br,
    $h->textarea ({ style => "width: 610px; height: 85px;",
                    name  => $question_id,
                    tabindex => $Q,
                    $M => [],
                 }, $V)
  ]);
}


sub error {
  my $msg = shift // "";
  
  print header (-cookie=> [ $RCAUTH_cookie ] ),
                        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'" }),
      ]),
    ]),
  ]),
  $h->div ({ class => "error" }, $msg),
  $h->close ("html");
  
  exit;
}