Subversion Repositories VORC

Rev

Rev 184 | 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 cookie/;
my $h = HTML::Tiny->new( mode => 'html' );
my $dbh = WebDB::connect;
my $homeURL = '/';
my $pageTitle = "RollerCon 2024 Feedback";

#my $cookie_string = authenticate (RollerCon::USER) || die;
my $RCAUTH = cookie('allnewmvpclasses');

my $RCAUTH_cookie = CGI::Cookie->new(-name=>'RCSURVEY',-value=>"true");
my @ERRORS;
my $DEBUG = 0;
my $classid;
my $insert_question = $dbh->prepare ("insert into general_survey_answer (qid, uid, response, added, page, maxpage) values (?, ?, ?, date_sub(now(), interval 2 hour), ?, ?)");
my $update_maxpage  = $dbh->prepare ("update general_survey_answer set maxpage = ? where qid = ? and uid = ?");
my $UID =  param ('uid') // get_unique_id();

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

my %question_options;
$question_options{1} = ["MVP", "Skater", "Off-Skates", "Day Pass", "Child Pass"];
$question_options{2} = ["MVP Classes", "Skate Park Events", "Roller Derby Events", "Social Events & Parties", "Vendor Village", "Charity Events", "Volunteering", "I love it all!", "Other"];
$question_options{3} = ["Dance", "Derby", "Skatepark", "Rec", "I don't skate", "Other"];
$question_options{4} = ["Plaza", "Circa", "Downtown Grand", "Other"];


my ($existing) = $dbh->selectrow_array ("select count(*) from general_survey_answer where uid = ?", undef, $UID);
my $mode = $existing ? "disabled" : "edit";
$mode = "edit" if param("submit") eq "Edit";

display_survey ($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 ({ 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 general_survey_answer where uid = ? and qid = ?", undef, $UID, $Q->{q});
  $maxpage++;
  $insert_question->execute ($Q->{q}, $UID, $Q->{a}, $maxpage, $maxpage);
  $update_maxpage->execute ($maxpage, $Q->{q}, $UID);
  return 1;
}

sub get_unique_id {
  my $UID;
  while (!$UID) {
    ($UID) = $dbh->selectrow_array ("select floor(1 + rand() * (99998))");
    my ($test_UID) = $dbh->selectrow_array ("select RCid from general_survey_answer where RCid = ?", undef, $UID);
    $UID = "" unless !$test_UID;
  }
  return $UID;
}

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 general_survey_answer where qid = ? and uid = ? and response = ?", undef, $A->{q}, $UID, $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 >= 0) {
      emojis.innerHTML = "<img src='/images/meh.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;
    
  
#  print $h->div (["Class Details:",
#    $h->p ("Name: ".$CREF->{name},
#           "Coach: ".$CREF->{coach}
#           ),
#    ]), $h->hr;
    
  print $h->open ("form", { method => "POST" });
  print $h->input ({ type=>"hidden", name=>"uid", value=>$UID });
  foreach my $question (@{$dbh->selectall_arrayref ("select qid, question, type, required from general_survey_question order by qorder")}) {
    my ($qid, $text, $type, $required) = @{$question};
    my ($PRIOR_ANSWER) = $dbh->selectrow_array ("select response from general_survey_answer where qid=? and uid=? and page = maxpage", undef, $qid, $UID);
  
    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 list_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->select ({ name  => $question_id,
                  tabindex => $Q,
                  $M => []
                }, [ map { $_ eq $V ? $h->option ({selected=>[]}, $_) : $h->option ($_) } "", @{$question_options{$Q}}]),
  ]);
}


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;
}