Saturday, April 7, 2012

Score Poem


<cfsilent>

<cfquery name="qGetPoem"  datasource="#request.dsn#">
SELECT poemID, poemTitle, poemContent
FROM  PoetryContest
WHERE poemID = <cfqueryparam cfsqltype="cf_sql_varchar" value="#url.id#">
</cfquery>

<!--- Define an array to catch the form errors. --->
<cfset arrDuplicate = ArrayNew( 1 ) />

<cfquery name="duplicateCheck" datasource="#request.dsn#">
SELECT score, judgeID
FROM PoemScores
WHERE judgeID = '#session.username#' and poemID = #url.id#
</cfquery>

<cfif duplicateCheck.recordCount GT '0'>
<cfset ArrayAppend(
arrDuplicate,
"You have previously scored this poem.  Submitting your score will overwrite your previous score."
) />
</cfif>

<cfset newLine = Chr(13) & Chr(10)>

<!--- Param FORM variables. --->

    <cfparam
name="FORM.rating"
type="string"
default=""
/>

<!---
For the form submission flag, since we are asking
it to be of type numeric, we have to catch the
CFParam in case someone has hacked the HTML and
altered the value being sent (a non-numeric value
will throw a ColdFusion error).
--->
<cftry>
<cfparam
name="FORM.submitted"
type="numeric"
default="0"
/>

<!--- Catch CFParam data type errors. --->
<cfcatch>
<cfset FORM.submitted = 0 />
</cfcatch>
</cftry>


<!--- Define an array to catch the form errors. --->
<cfset arrErrors = ArrayNew( 1 ) />


<!--- Check to see if the form has been submitted. --->
<cfif FORM.submitted>

<!---
Now that the form has been submitted, we need
to validate the data.
--->


<!--- Validate first name. --->
<cfif NOT Len( FORM.rating )>
<cfset ArrayAppend(
arrErrors,
"You must rate the poem."
) />
</cfif>


<!---
Now that we have validated our form data, let's
check to see if there are any form validation
errors. Only if there are no errors do w want to
continue processing the data - otherwise, we want
to skip this next part and let the form re-render.
--->

<cfif NOT ArrayLen( arrErrors ) >
<cfquery name="updateScore" datasource="#request.dsn#">
UPDATE PoemScores
SET score = '#form.rating#'

WHERE poemID = <cfqueryparam cfsqltype="cf_sql_varchar" value="#url.id#">
</cfquery>
</cfif>

<cfif NOT ArrayLen( arrErrors )>

<!--- INSERT DATABASE QUERY--->
<cfquery name="qRatePoem" datasource="#request.dsn#">
INSERT INTO PoemScores (poemID, score, judgeID)
VALUES ('#url.id#', '#form.rating#', '#session.username#')
</cfquery>



<!---<cflocation url="dash.cfm" addtoken="no">--->

</cfif>

</cfif>


</cfsilent>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Poem Overview</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

<style>
body {font-family: "Helvetica Neue", Helvetica, Arial, Sans-serif; font-size: 0.875em; line-height: 1.3}
#poem {width:450px; position:relative;margin-left:auto; margin-right:auto; top: 90px;}
#poem h3{font-size:20px;font-weight:normal;background:#b9c9fe;border-top:4px solid #aabcfe;color:#039;padding:8px;margin-bottom:0;}
#poem p{background:#e8edff;border-bottom:1px solid #fff;color:#669;border-top:1px solid transparent;padding:0 7px 7px 7px; margin:0;}
.rating{ }
</style>

</head>

<body>
<div id="content">

<div id="poem">
<cfoutput query="qGetPoem">
<cfif ArrayLen( arrErrors )>
<h2>Please review the following:</h2>

<ul>
<cfloop
index="intError"
from="1"
to="#ArrayLen( arrErrors )#"
step="1">

<li>
#arrErrors[ intError ]#
</li>

</cfloop>
</ul>

</cfif>

<cfif ArrayLen( arrDuplicate)>
<h2>Attention:</h2>

<ul>
<cfloop
index="intError"
from="1"
to="#ArrayLen( arrDuplicate )#"
step="1">

<li>
#arrDuplicate[ intError ]#
</li>

</cfloop>
</ul>

</cfif>


<h3>#poemTitle#</h3>
<div class="rating">
<form
action="#cgi.SCRIPT_NAME#?id=#url.id#"
method="post"
enctype="multipart/form-data">
<!--- Our form submission flag. --->
<input type="hidden" name="submitted" value="1" />
<input type="hidden" value="#id#" />
<select name="rating">

<option value="">&ndash; Rate This Poem &ndash;</option>
<option value="1"<cfif duplicateCheck.score eq 1> selected="selected"</cfif>>1</option>
<option value="2"<cfif duplicateCheck.score eq 2> selected="selected"</cfif>>2</option>
<option value="3"<cfif duplicateCheck.score eq 3> selected="selected"</cfif>>3</option>



</select>
<input type="submit" value="Rate It!" />
</form>
</span>


<p>#Replace(poemContent, newLine, "<br>", "ALL")#</p>

</cfoutput>
</div>
  </div>
</div>
</body>
</html>

No comments:

Post a Comment