Subversion Repositories VORC

Rev

Rev 109 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
105 bgadell 1
#!/usr/bin/perl
2
 
3
# Redirect error messages to a log of my choosing. (it's annoying to filter for errors in the shared env)
4
my $error_log_path = $ENV{SERVER_NAME} eq "volunteers.rollercon.com" ? "/home3/rollerco/logs/" : "/tmp/";
5
close STDERR;
6
open STDERR, '>>', $error_log_path.'vorc_error.log' or warn "Failed to open redirected logfile ($0): $!";
7
#warn "Redirecting errors to ${error_log_path}vorc_error.log";
8
 
9
use strict;
10
use cPanelUserConfig;
11
use WebDB;
12
use HTML::Tiny;
13
use RollerCon;
14
use CGI qw/param header start_html url/;
15
my $h = HTML::Tiny->new( mode => 'html' );
16
my $dbh = WebDB::connect;
17
my $homeURL = '/schedule/';
18
my $pageTitle = "MVP Class Feedback";
19
 
20
my $cookie_string = authenticate (RollerCon::USER) || die;
21
my $RCAUTH_cookie = CGI::Cookie->new(-name=>'RCAUTH',-value=>"$cookie_string",-expires=>"+30m");
22
my @ERRORS;
23
my $DEBUG = 0;
24
 
25
my $classid = param ("classid") // "";
26
error ("No ClassID.") unless $classid;
27
 
28
my ($registered) = $dbh->selectrow_array ("select id from v_class_signup where id = ? and RCid = ?", undef, $classid, $ORCUSER->{RCid});
29
error ("You don't appear to be registered for this class.") unless $registered;
30
 
31
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});
32
error ("You can't provide feedback until after the class has ended.") unless $started;
33
 
34
my $insert_question = $dbh->prepare ("insert into survey_answer (classid, qid, RCid, response, added, page, maxpage) values (?, ?, ?, ?, date_sub(now(), interval 2 hour), ?, ?)");
35
my $update_maxpage  = $dbh->prepare ("update survey_answer set maxpage = ? where classid = ? and qid = ? and RCid = ?");
36
 
37
my $updated = save_survey ($classid) if param("submit") eq "Save";
38
 
39
my ($existing) = $dbh->selectrow_array ("select count(*) from v_survey_answer where classid = ? and RCid = ?", undef, $classid, $ORCUSER->{RCid});
40
my $mode = $existing ? "disabled" : "edit";
41
$mode = "edit" if param("submit") eq "Edit";
42
 
43
display_survey ($classid, $mode, $updated);
44
 
45
 
46
 
47
 
48
 
49
sub save_survey {
50
  my $CID = shift // error ("No ClassID");
51
  warn "Saving survey for RCID: $ORCUSER->{RCid}, Class: $CID" if $DEBUG;
52
  my $changes = 0;
53
 
54
  foreach (grep { /Question/ } param ()) {
55
    my ($prefix, $q) = split /_/;
56
    $changes += save_question ({ cid=>$CID, q=>$q, a=>WebDB::trim (scalar param ($_)) });
57
  }
58
 
59
  return $changes;
60
}
61
 
62
sub save_question {
63
  my $Q = shift // error ("No data provided to save.");
64
  return 0 unless answerChanged ($Q);
65
  warn "Saving question $Q->{q}: $Q->{a}" if $DEBUG;
66
  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});
67
  $maxpage++;
68
  $insert_question->execute ($Q->{cid}, $Q->{q}, $ORCUSER->{RCid}, $Q->{a}, $maxpage, $maxpage);
69
  $update_maxpage->execute ($maxpage, $Q->{cid}, $Q->{q}, $ORCUSER->{RCid});
70
  return 1;
71
}
72
 
73
sub answerChanged {
74
  my $A = shift // error ("No answer provided to check.");
75
  warn "Checking question $A->{q}: $A->{a}" if $DEBUG;
76
  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});
77
  warn "Answer hasn't changed [$check], not saving." if $check and $DEBUG;
78
  return $check ? 0 : 1;
79
}
80
 
81
sub display_survey {
82
  my $CID = shift // error ("No ClassID");
83
  my $mode = shift // "disabled";
84
  my $saved = shift // "";
85
 
86
  print header (-cookie=> [ $RCAUTH_cookie ] ),
87
  	start_html (-title => $pageTitle, -style => [{'src' => "/style.css"},{'src' => "/survey.css"}] );
88
  print<<JAVASCRIPT;
89
	<SCRIPT language="JavaScript">
90
	<!--
91
  function displayScore(score, targetspan) {
92
    sliderscore  = document.getElementById(score);
93
    emojis = document.getElementById(targetspan);
94
 
95
    if (sliderscore.value == 5) {
96
      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'>";
97
    } else if (sliderscore.value >= 4) {
98
      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'>";
99
    } else if (sliderscore.value >= 3) {
100
      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'>";
101
    } else if (sliderscore.value >= 2) {
102
      emojis.innerHTML = "<img src='/images/star-icon.png' align='middle'><img src='/images/star-icon.png' align='middle'>";
103
    } else if (sliderscore.value >= 1) {
104
      emojis.innerHTML = "<img src='/images/star-icon.png' align='middle'>";
105
    } else if (sliderscore.value > -1) {
106
      emojis.innerHTML = "<img src='/images/meh.png' align='middle'>";
107
    } else if (sliderscore.value > -2) {
108
      emojis.innerHTML = "<img src='/images/angry.png' align='middle'>";
109
    } else if (sliderscore.value > -3) {
110
      emojis.innerHTML = "<img src='/images/angry.png' align='middle'><img src='/images/angry.png' align='middle'>";
111
    } else if (sliderscore.value > -4) {
112
      emojis.innerHTML = "<img src='/images/angry.png' align='middle'><img src='/images/angry.png' align='middle'><img src='/images/angry.png' align='middle'>";
113
    } else if (sliderscore.value > -5) {
114
      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'>";
115
    } else if (sliderscore.value == -5) {
116
      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'>";
117
    } else {
118
      emojis.innerHTML = sliderscore.value;
119
    }
120
  }
121
	//-->
122
	</SCRIPT>
123
JAVASCRIPT
124
 
125
  print $h->div ({ class => "accent pageheader" }, [
126
    $h->h1 ($pageTitle),
127
    $h->div ({ class=>"sp0" }, [
128
      $h->div ({ class=>"spLeft" }, [
129
      ]),
130
      $h->div ({ class=>"spRight" }, [
131
        $h->input ({ type=>"button", value=>"Home", onClick=>"window.location.href='$homeURL'" }),
132
      ]),
133
    ]),
134
  ]);
135
 
136
  print $h->div ({ id=>"savedmsg", class=>"saved fadeOut"}, "Changes Saved!") unless !$saved;
137
 
138
  my ($CREF) = $dbh->selectrow_hashref ("select * from v_class where id = ?", undef, $classid);
139
  $CREF->{time} = convertTime $CREF->{time};
140
  $CREF->{date} = $CREF->{date}." [".$CREF->{dayofweek}."]";
141
  print $h->ul ( { style=>"max-width:610px" }, [ map { $h->li ({ class=>"shaded", style=>"margin:4px;" },
142
    $h->div ({ class=>"lisp0" }, [
143
      $h->div ({ class=>"liNarrowLeft"  }, ucfirst $_),
144
      $h->div ({ class=>"liWideRight"   }, $CREF->{$_})
145
      ])
146
    ) } ("name", "coach", "date", "time", "location") ]);
147
 
148
 
149
 
150
#  print $h->div (["Class Details:",
151
#    $h->p ("Name: ".$CREF->{name},
152
#           "Coach: ".$CREF->{coach}
153
#           ),
154
#    ]), $h->hr;
155
 
156
  print $h->open ("form");
157
  print $h->input ({ type=>"hidden", name=>"classid", value=>$CID });
158
  foreach my $question (@{$dbh->selectall_arrayref ("select qid, question, type, required from survey_question order by qorder")}) {
159
    my ($qid, $text, $type, $required) = @{$question};
160
    my ($PRIOR_ANSWER) = $dbh->selectrow_array ("select response from v_survey_answer where classid = ? and qid=? and RCid=?", undef, $CID, $qid, $ORCUSER->{RCid});
161
 
162
    no strict;
163
    &{$type."_input"} ($qid, $text, $required, $PRIOR_ANSWER, $mode);
164
    print $h->br;
165
 
166
  }
167
 
168
  my $leftbutton = $mode eq "disabled" ?
169
    $h->input ({ type=>"button", name=>"back", value=>"Home", onClick=>"window.location.href='$homeURL'" }) :
170
    $h->input ({ type=>"reset", name=>"Reset", onClick=>"return confirm('Are you sure you want to reset all of your changes?');" });
171
 
172
  print $leftbutton,
173
        '&nbsp;',
174
        $h->input ({ type=>"submit", name=>"submit", value=> $mode eq "disabled" ? "Edit" : "Save" });
175
 
176
  print $h->close ("form", "html");
177
  exit;
178
}
179
 
180
 
181
sub range_input {
182
  my $Q = shift // "";
183
  my $T = shift // "";
184
  my $R = shift // "";
185
  my $V = shift // 0;
186
  my $M = shift // "disabled";
187
 
188
  my $question_id = "Question_".$Q;
189
 
190
  print $h->div ({ class => "slidecontainer"}, [
191
    $h->label ({ for => $question_id }, $T), $h->br,
192
    $h->input ({ type => "range",
193
                 id   => $question_id,
194
                 name => $question_id,
195
                 tabindex => $Q,
196
                 min  => "-5",
197
                 max  => "5",
198
                 value => $V,
199
                 step  => ".1",
200
                 class => "rangeslider",
201
                 onInput => "displayScore('$question_id', 'question$Q');",
202
                 $M => [],
203
              }),
204
    $h->span ({ id => "question".$Q }, $h->img ({ src=>"/images/meh.png", align=>"middle" }))
205
  ]);
206
  print "<SCRIPT language='JavaScript'>displayScore('$question_id', 'question$Q');</SCRIPT>\n" if $V;
207
}
208
 
209
sub text_input {
210
  my $Q = shift // "";
211
  my $T = shift // "";
212
  my $R = shift // "";
213
  my $V = shift // "";
214
  my $M = shift // "disabled";
215
 
216
  my $question_id = "Question_".$Q;
217
 
218
  print $h->div ({ class=>"" }, [
219
    $h->label ({ for => $question_id }, $T), $h->br,
220
    $h->textarea ({ style => "width: 610px; height: 85px;",
221
                    name  => $question_id,
222
                    tabindex => $Q,
223
                    $M => [],
224
                 }, $V)
225
  ]);
226
}
227
 
228
 
229
sub error {
230
  my $msg = shift // "";
231
 
232
  print header (-cookie=> [ $RCAUTH_cookie ] ),
233
			start_html (-title => $pageTitle, -style => {'src' => "/style.css"} );
234
 
235
  print $h->div ({ class => "accent pageheader" }, [
236
    $h->h1 ($pageTitle),
237
    $h->div ({ class=>"sp0" }, [
238
      $h->div ({ class=>"spLeft" }, [
239
      ]),
240
      $h->div ({ class=>"spRight" }, [
241
        $h->input ({ type=>"button", value=>"Home", onClick=>"window.location.href='$homeURL'" }),
242
      ]),
243
    ]),
244
  ]),
245
  $h->div ({ class => "error" }, $msg),
246
  $h->close ("html");
247
 
248
  exit;
249
}