| [5] | 1 | <!--- |
|---|
| 2 | Name: survey.cfc |
|---|
| 3 | Created: |
|---|
| 4 | Last Updated: April 10, 2006 |
|---|
| 5 | History: duplicateSurvey, fix question creation (rkc 3/29/05) |
|---|
| 6 | support itemidfk (rkc 10/8/05) |
|---|
| 7 | caching updates (rkc 2/11/06) |
|---|
| 8 | tableprefix support (rkc 3/11/06) |
|---|
| 9 | tableprefix fix (rkc 3/22/06) |
|---|
| 10 | yet ANOTHER fix (rkc 3/24/06) |
|---|
| 11 | support for clearing results (rkc 3/30/06) |
|---|
| 12 | fix issue that occurs when you delete questions w/o fixing ranks (4/10/06) |
|---|
| 13 | ---> |
|---|
| 14 | |
|---|
| 15 | <cfcomponent displayName="Survey" hint="Handles all survey interactions." output="false"> |
|---|
| 16 | |
|---|
| 17 | <cfset variables.utils = createObject("component","utils")> |
|---|
| 18 | <cfinvoke component="soundings" method="getSettings" returnVariable="variables.settings"> |
|---|
| 19 | |
|---|
| 20 | <cffunction name="init" access="public" returnType="survey" output="false" |
|---|
| 21 | hint="Returns an instance of the CFC initialized with the correct DSN."> |
|---|
| 22 | <cfargument name="dsn" type="string" required="true" hint="DSN used for all operations in the CFC."> |
|---|
| 23 | <cfargument name="dbtype" type="string" required="true" hint="Database type."> |
|---|
| 24 | <cfargument name="tableprefix" type="string" required="true" hint="Table prefix."> |
|---|
| 25 | |
|---|
| 26 | <cfset variables.dsn = arguments.dsn> |
|---|
| 27 | <cfset variables.dbtype = arguments.dbtype> |
|---|
| 28 | <cfset variables.tableprefix = arguments.tableprefix> |
|---|
| 29 | |
|---|
| 30 | <cfset variables.answerCache = structNew()> |
|---|
| 31 | <cfset variables.itemCache = structNew()> |
|---|
| 32 | <cfset variables.questionCache = structNew()> |
|---|
| 33 | |
|---|
| 34 | <cfreturn this> |
|---|
| 35 | |
|---|
| 36 | </cffunction> |
|---|
| 37 | |
|---|
| 38 | <cffunction name="addEmailList" access="public" returnType="void" output="false" |
|---|
| 39 | hint="Adds to a survey's email restriction list."> |
|---|
| 40 | <cfargument name="id" type="uuid" required="true" hint="The UUID of the survey."> |
|---|
| 41 | <cfargument name="emails" type="array" required="true" hint="The emails to add."> |
|---|
| 42 | <cfset var x = ""> |
|---|
| 43 | |
|---|
| 44 | <cfloop index="x" from="1" to="#arrayLen(emails)#"> |
|---|
| 45 | <cfquery datasource="#variables.dsn#"> |
|---|
| 46 | insert into #variables.tableprefix#survey_emailaddresses(surveyidfk,emailaddress) |
|---|
| 47 | values(<cfqueryparam value="#arguments.id#" cfsqltype="CF_SQL_VARCHAR" maxlength="35">, |
|---|
| 48 | <cfqueryparam value="#emails[x]#" cfsqltype="CF_SQL_VARCHAR" maxlength="50">) |
|---|
| 49 | </cfquery> |
|---|
| 50 | </cfloop> |
|---|
| 51 | |
|---|
| 52 | </cffunction> |
|---|
| 53 | |
|---|
| 54 | <cffunction name="addSurvey" access="public" returnType="uuid" output="false" |
|---|
| 55 | hint="Adds a new survey."> |
|---|
| 56 | <cfargument name="name" type="string" required="true" hint="Survey name."> |
|---|
| 57 | <cfargument name="description" type="string" required="true" hint="Survey description."> |
|---|
| 58 | <cfargument name="active" type="boolean" required="true" hint="Determines if the survey is active or not."> |
|---|
| 59 | <cfargument name="dateBegin" type="date" required="false" hint="Time when survey begins."> |
|---|
| 60 | <cfargument name="dateEnd" type="date" required="false" hint="Time when survey ends."> |
|---|
| 61 | <cfargument name="resultMailto" type="string" required="false" hint="Email address to send results to."> |
|---|
| 62 | <cfargument name="surveyPassword" type="string" required="false" hint="Survey password necessary for access."> |
|---|
| 63 | <cfargument name="thankYouMsg" type="string" required="false" hint="Survey thank you message."> |
|---|
| 64 | |
|---|
| 65 | <cfset var newID = createUUID()> |
|---|
| 66 | |
|---|
| 67 | <cfif not validData(arguments)> |
|---|
| 68 | <cfset variables.utils.throw("SurveyCFC","Survey data is not valid.")> |
|---|
| 69 | </cfif> |
|---|
| 70 | |
|---|
| 71 | <cfquery datasource="#variables.dsn#"> |
|---|
| 72 | insert into #variables.tableprefix#surveys(id,name,description,active,datebegin,dateend,resultmailto,surveypassword,thankyoumsg) |
|---|
| 73 | values( |
|---|
| 74 | <cfqueryparam value="#newID#" cfsqltype="CF_SQL_VARCHAR" maxlength="35">, |
|---|
| 75 | <cfqueryparam value="#arguments.name#" cfsqltype="CF_SQL_VARCHAR" maxlength="50">, |
|---|
| 76 | <cfqueryparam value="#arguments.description#" cfsqltype="CF_SQL_VARCHAR" maxlength="255">, |
|---|
| 77 | <cfqueryparam value="#arguments.active#" cfsqltype="#variables.utils.getQueryParamType(variables.dbtype,"CF_SQL_BIT")#">, |
|---|
| 78 | <cfif isDefined("attributes.dateBegin")> |
|---|
| 79 | <cfqueryparam value="#arguments.dateBegin#" cfsqltype="CF_SQL_TIMESTAMP">, |
|---|
| 80 | <cfelse> |
|---|
| 81 | null, |
|---|
| 82 | </cfif> |
|---|
| 83 | <cfif isDefined("attributes.dateBegin")> |
|---|
| 84 | <cfqueryparam value="#arguments.dateEnd#" cfsqltype="CF_SQL_TIMESTAMP">, |
|---|
| 85 | <cfelse> |
|---|
| 86 | null, |
|---|
| 87 | </cfif> |
|---|
| 88 | <cfqueryparam value="#arguments.resultMailTo#" cfsqltype="CF_SQL_VARCHAR" maxlength="50">, |
|---|
| 89 | <cfqueryparam value="#arguments.surveyPassword#" cfsqltype="CF_SQL_VARCHAR" maxlength="50">, |
|---|
| 90 | <cfqueryparam value="#arguments.thankYouMsg#" cfsqltype="CF_SQL_LONGVARCHAR"> |
|---|
| 91 | ) |
|---|
| 92 | </cfquery> |
|---|
| 93 | |
|---|
| 94 | <cfreturn newID> |
|---|
| 95 | </cffunction> |
|---|
| 96 | |
|---|
| 97 | <cffunction name="clearResults" access="public" returnType="void" output="false" |
|---|
| 98 | hint="Removes all results from a survey."> |
|---|
| 99 | <cfargument name="id" type="uuid" required="true" hint="The UUID of the survey."> |
|---|
| 100 | <cfset var questions = ""> |
|---|
| 101 | <cfset var questionlist = ""> |
|---|
| 102 | |
|---|
| 103 | <!--- first get questionidfk, this lets us empty results ---> |
|---|
| 104 | <cfquery name="questions" datasource="#variables.dsn#"> |
|---|
| 105 | select id |
|---|
| 106 | from #variables.tableprefix#questions |
|---|
| 107 | where surveyidfk = <cfqueryparam value="#arguments.id#" cfsqltype="CF_SQL_VARCHAR" maxlength="35"> |
|---|
| 108 | </cfquery> |
|---|
| 109 | <cfset questionlist = valueList(questions.id)> |
|---|
| 110 | |
|---|
| 111 | <cfif len(questionlist)> |
|---|
| 112 | |
|---|
| 113 | <cfquery datasource="#variables.dsn#"> |
|---|
| 114 | delete from #variables.tableprefix#results |
|---|
| 115 | where questionidfk in (<cfqueryparam cfsqltype="cf_sql_varchar" maxlength="35" value="#questionlist#" list="true">) |
|---|
| 116 | </cfquery> |
|---|
| 117 | |
|---|
| 118 | <cfquery datasource="#variables.dsn#"> |
|---|
| 119 | delete from #variables.tableprefix#survey_results |
|---|
| 120 | where surveyidfk = <cfqueryparam value="#arguments.id#" cfsqltype="CF_SQL_VARCHAR" maxlength="35"> |
|---|
| 121 | </cfquery> |
|---|
| 122 | |
|---|
| 123 | </cfif> |
|---|
| 124 | |
|---|
| 125 | </cffunction> |
|---|
| 126 | |
|---|
| 127 | <cffunction name="completeSurvey" access="public" returnType="void" output="false" |
|---|
| 128 | hint="Simply marks the survey complete."> |
|---|
| 129 | <cfargument name="id" type="uuid" required="true" hint="The UUID of the survey."> |
|---|
| 130 | <cfargument name="ownerid" type="string" required="true" hint="The owner id. Either an email address or a simple UUID."> |
|---|
| 131 | <cfset var survey = getSurvey(arguments.id)> |
|---|
| 132 | <cfset var question = ""> |
|---|
| 133 | <cfset var questions = ""> |
|---|
| 134 | |
|---|
| 135 | <cfif not surveyCompletedBy(arguments.id, arguments.ownerid)> |
|---|
| 136 | |
|---|
| 137 | <!--- Nuke the question cache ---> |
|---|
| 138 | <!--- A bit overkill, but it works. questionCache is a cache of results, so this is necessary. ---> |
|---|
| 139 | <cfset variables.questionCache = structNew()> |
|---|
| 140 | |
|---|
| 141 | <cfquery datasource="#variables.dsn#"> |
|---|
| 142 | insert into #variables.tableprefix#survey_results(surveyidfk,ownerid,completed) |
|---|
| 143 | values( |
|---|
| 144 | <cfqueryparam value="#arguments.id#" cfsqltype="CF_SQL_VARCHAR" maxlength="35">, |
|---|
| 145 | <cfqueryparam value="#arguments.ownerid#" cfsqltype="CF_SQL_VARCHAR" maxlength="255">, |
|---|
| 146 | <cfqueryparam value="#now()#" cfsqltype="CF_SQL_TIMESTAMP" > |
|---|
| 147 | ) |
|---|
| 148 | </cfquery> |
|---|
| 149 | |
|---|
| 150 | <!--- Do we need to mail? ---> |
|---|
| 151 | <cfif len(survey.resultMailTo)> |
|---|
| 152 | <cfset question = createObject("component","question").init(variables.dsn,variables.dbtype,variables.tableprefix)> |
|---|
| 153 | <cfset questions = question.getQuestions(arguments.id)> |
|---|
| 154 | |
|---|
| 155 | <cfmail to="#survey.resultMailTo#" from="#variables.settings.fromAddress#" subject="Survey Completion: #survey.name#"> |
|---|
| 156 | The survey, #survey.name#, was just completed. |
|---|
| 157 | The survey was completed at #dateFormat(now(),"m/dd/yy")# at #timeFormat(now(),"h:mm tt")# |
|---|
| 158 | Owner Key: #arguments.ownerid# |
|---|
| 159 | ------------------------------------------------- |
|---|
| 160 | <cfloop query="questions"> |
|---|
| 161 | Q#currentRow#) #questions.question# |
|---|
| 162 | A) #getAnswerResult(questions.id,arguments.ownerid)# |
|---|
| 163 | </cfloop> |
|---|
| 164 | </cfmail> |
|---|
| 165 | </cfif> |
|---|
| 166 | </cfif> |
|---|
| 167 | |
|---|
| 168 | </cffunction> |
|---|
| 169 | |
|---|
| 170 | <cffunction name="deleteSurvey" access="public" returnType="void" output="false" |
|---|
| 171 | hint="Deletes a survey. Also does cleanup on results/questions/answers."> |
|---|
| 172 | <cfargument name="id" type="uuid" required="true" hint="The UUID of the survey to delete."> |
|---|
| 173 | <cfset var question = createObject("component","question").init(variables.dsn, variables.dbtype,variables.tableprefix)> |
|---|
| 174 | <cfset var questions = question.getQuestions(arguments.id)> |
|---|
| 175 | |
|---|
| 176 | <cfloop query="questions"> |
|---|
| 177 | <cfset question.deleteQuestion(questions.id)> |
|---|
| 178 | </cfloop> |
|---|
| 179 | |
|---|
| 180 | <!--- remove EL ---> |
|---|
| 181 | <cfset resetEmailList(arguments.id)> |
|---|
| 182 | |
|---|
| 183 | <!--- remove core results ---> |
|---|
| 184 | <cfquery datasource="#variables.dsn#"> |
|---|
| 185 | delete from #variables.tableprefix#survey_results |
|---|
| 186 | where surveyidfk = <cfqueryparam value="#arguments.id#" cfsqltype="CF_SQL_VARCHAR" maxlength="35"> |
|---|
| 187 | </cfquery> |
|---|
| 188 | |
|---|
| 189 | <cfquery datasource="#variables.dsn#"> |
|---|
| 190 | delete from #variables.tableprefix#surveys |
|---|
| 191 | where id = <cfqueryparam value="#arguments.id#" cfsqltype="CF_SQL_VARCHAR" maxlength="35"> |
|---|
| 192 | </cfquery> |
|---|
| 193 | |
|---|
| 194 | </cffunction> |
|---|
| 195 | |
|---|
| 196 | <cffunction name="duplicateSurvey" access="public" returnType="void" output="false" |
|---|
| 197 | hint="Duplicates a survey."> |
|---|
| 198 | <cfargument name="id" type="uuid" required="true" hint="The UUID of the survey to duplicate."> |
|---|
| 199 | <cfset var s = getSurvey(arguments.id)> |
|---|
| 200 | <cfset var question = createObject("component","question").init(variables.dsn, variables.dbtype, variables.tableprefix)> |
|---|
| 201 | <cfset var questions = question.getQuestions(arguments.id)> |
|---|
| 202 | <cfset var el = getEmailList(arguments.id)> |
|---|
| 203 | <cfset var elA = listToArray(valueList(el.emailAddress))> |
|---|
| 204 | <cfset var newID = ""> |
|---|
| 205 | <cfset args = structNew()> |
|---|
| 206 | |
|---|
| 207 | <cfset args.name = "Copy of " & s.name> |
|---|
| 208 | <cfset args.description = s.description> |
|---|
| 209 | <cfset args.active = s.active> |
|---|
| 210 | <cfif isDate(s.dateBegin)> |
|---|
| 211 | <cfset args.dateBegin = s.dateBegin> |
|---|
| 212 | </cfif> |
|---|
| 213 | <cfif isDate(s.dateEnd)> |
|---|
| 214 | <cfset args.dateEnd = s.dateEnd> |
|---|
| 215 | </cfif> |
|---|
| 216 | <cfset args.resultMailTo = s.resultMailTo> |
|---|
| 217 | <cfset args.surveyPassword = s.surveyPassword> |
|---|
| 218 | <cfset args.thankYouMsg = s.thankYouMsg> |
|---|
| 219 | |
|---|
| 220 | <cfset newid = addSurvey(argumentCollection=args)> |
|---|
| 221 | |
|---|
| 222 | <cfloop query="questions"> |
|---|
| 223 | <cfset question.duplicateQuestion(questions.id,newid)> |
|---|
| 224 | </cfloop> |
|---|
| 225 | |
|---|
| 226 | <cfset addEmailList(newid, elA)> |
|---|
| 227 | |
|---|
| 228 | </cffunction> |
|---|
| 229 | |
|---|
| 230 | <cffunction name="getAnswerResult" returnType="string" output="false" hint="Gets the answer as a string."> |
|---|
| 231 | <cfargument name="questionidfk" type="UUID" required="true"> |
|---|
| 232 | <cfargument name="owneridfk" type="string" required="true"> |
|---|
| 233 | <cfset var getAnswers = ""> |
|---|
| 234 | <cfset var getAnswer = ""> |
|---|
| 235 | <cfset var result = ""> |
|---|
| 236 | <cfset var getItem = ""> |
|---|
| 237 | <cfset var ptr = ""> |
|---|
| 238 | |
|---|
| 239 | <!--- Do we have the question cache? ---> |
|---|
| 240 | <cfif not structKeyExists(variables.questionCache, arguments.questionidfk)> |
|---|
| 241 | <cfquery name="getAnswers" datasource="#variables.dsn#"> |
|---|
| 242 | select answeridfk, truefalse, textbox, textboxmulti, other, itemidfk, owneridfk |
|---|
| 243 | from #variables.tableprefix#results |
|---|
| 244 | where questionidfk = <cfqueryparam value="#arguments.questionidfk#" cfsqltype="CF_SQL_VARCHAR" maxlength="35"> |
|---|
| 245 | </cfquery> |
|---|
| 246 | <cfset variables.questionCache[arguments.questionidfk] = getAnswers> |
|---|
| 247 | </cfif> |
|---|
| 248 | <cfset ptr = variables.questionCache[arguments.questionidfk]> |
|---|
| 249 | |
|---|
| 250 | <cfquery name="getAnswers" dbtype="query"> |
|---|
| 251 | select answeridfk, truefalse, textbox, textboxmulti, other, itemidfk |
|---|
| 252 | from ptr |
|---|
| 253 | where owneridfk = <cfqueryparam value="#arguments.owneridfk#" cfsqltype="CF_SQL_VARCHAR" maxlength="255"> |
|---|
| 254 | </cfquery> |
|---|
| 255 | |
|---|
| 256 | <!--- |
|---|
| 257 | <!--- first get all the owner's answers to a question ---> |
|---|
| 258 | <cfquery name="getAnswers" datasource="#variables.dsn#" blockfactor=4> |
|---|
| 259 | select answeridfk, truefalse, textbox, textboxmulti, other, itemidfk |
|---|
| 260 | from results |
|---|
| 261 | where questionidfk = <cfqueryparam value="#arguments.questionidfk#" cfsqltype="CF_SQL_VARCHAR" maxlength="35"> |
|---|
| 262 | and owneridfk = <cfqueryparam value="#arguments.owneridfk#" cfsqltype="CF_SQL_VARCHAR" maxlength="255"> |
|---|
| 263 | </cfquery> |
|---|
| 264 | ---> |
|---|
| 265 | <cfloop query="getAnswers"> |
|---|
| 266 | <cfif answeridfk is not ""> |
|---|
| 267 | <cfif not structKeyExists(variables.answerCache, answeridfk)> |
|---|
| 268 | <cfquery name="getAnswer" datasource="#variables.dsn#"> |
|---|
| 269 | select answer |
|---|
| 270 | from #variables.tableprefix#answers |
|---|
| 271 | where questionidfk = <cfqueryparam value="#arguments.questionidfk#" cfsqltype="CF_SQL_VARCHAR" maxlength="35"> |
|---|
| 272 | and id = <cfqueryparam value="#answeridfk#" cfsqltype="CF_SQL_VARCHAR" maxlength="35"> |
|---|
| 273 | </cfquery> |
|---|
| 274 | <cfset variables.answerCache[answeridfk] = getAnswer.answer> |
|---|
| 275 | </cfif> |
|---|
| 276 | <cfif itemidfk is not ""> |
|---|
| 277 | <cfif not structKeyExists(variables.itemCache, itemidfk)> |
|---|
| 278 | <cfquery name="getItem" datasource="#variables.dsn#"> |
|---|
| 279 | select answer |
|---|
| 280 | from #variables.tableprefix#answers |
|---|
| 281 | where questionidfk = <cfqueryparam value="#arguments.questionidfk#" cfsqltype="CF_SQL_VARCHAR" maxlength="35"> |
|---|
| 282 | and id = <cfqueryparam value="#itemidfk#" cfsqltype="CF_SQL_VARCHAR" maxlength="35"> |
|---|
| 283 | </cfquery> |
|---|
| 284 | <cfset variables.itemCache[itemidfk] = getItem.answer> |
|---|
| 285 | </cfif> |
|---|
| 286 | <cfset result = listAppend(result, variables.itemCache[itemidfk] & ": " & variables.answerCache[answeridfk])> |
|---|
| 287 | <cfelse> |
|---|
| 288 | <cfset result = listAppend(result, variables.answerCache[answeridfk])> |
|---|
| 289 | </cfif> |
|---|
| 290 | |
|---|
| 291 | <cfelseif other is not ""> |
|---|
| 292 | <cfset result = listAppend(result, other)> |
|---|
| 293 | <cfelseif textbox is not ""> |
|---|
| 294 | <cfset result = listAppend(result, textbox)> |
|---|
| 295 | <cfelseif textboxmulti is not ""> |
|---|
| 296 | <cfset result = listAppend(result, textboxmulti)> |
|---|
| 297 | <cfelse> |
|---|
| 298 | <cfset result = listAppend(result, yesNoFormat(truefalse))> |
|---|
| 299 | </cfif> |
|---|
| 300 | </cfloop> |
|---|
| 301 | |
|---|
| 302 | <cfreturn result> |
|---|
| 303 | </cffunction> |
|---|
| 304 | |
|---|
| 305 | <cffunction name="getEmailList" access="public" returnType="query" output="false" |
|---|
| 306 | hint="Returns a survey's email restriction list (if one exists)."> |
|---|
| 307 | <cfargument name="id" type="uuid" required="true" hint="The UUID of the survey to retrieve."> |
|---|
| 308 | <cfset var qGetSurveyEmails = ""> |
|---|
| 309 | |
|---|
| 310 | <cfquery name="qGetSurveyEmails" datasource="#variables.dsn#"> |
|---|
| 311 | select emailaddress |
|---|
| 312 | from #variables.tableprefix#survey_emailaddresses |
|---|
| 313 | where surveyidfk = <cfqueryparam value="#arguments.id#" cfsqltype="CF_SQL_VARCHAR" maxlength="35"> |
|---|
| 314 | </cfquery> |
|---|
| 315 | |
|---|
| 316 | <cfreturn qGetSurveyEmails> |
|---|
| 317 | </cffunction> |
|---|
| 318 | |
|---|
| 319 | <cffunction name="getStats" access="public" returnType="struct" output="false" |
|---|
| 320 | hint="Returns general stats for a survey."> |
|---|
| 321 | <cfargument name="id" type="uuid" required="true" hint="The UUID of the survey to retrieve."> |
|---|
| 322 | <cfset var stats = ""> |
|---|
| 323 | |
|---|
| 324 | <cfquery name="getSurveys" datasource="#variables.dsn#"> |
|---|
| 325 | select count(surveyidfk) as totalresults, min(completed) as firstResult, |
|---|
| 326 | max(completed) as lastResult |
|---|
| 327 | from #variables.tableprefix#survey_results |
|---|
| 328 | where surveyidfk = <cfqueryparam value="#arguments.id#" cfsqltype="CF_SQL_VARCHAR" maxlength="35"> |
|---|
| 329 | </cfquery> |
|---|
| 330 | |
|---|
| 331 | <cfif getSurveys.recordCount> |
|---|
| 332 | <cfset stats = variables.utils.queryToStruct(getSurveys)> |
|---|
| 333 | <cfelse> |
|---|
| 334 | <cfset stats.totalresults = 0> |
|---|
| 335 | <cfset stats.firstresult = ""> |
|---|
| 336 | <cfset stats.lastresult = ""> |
|---|
| 337 | </cfif> |
|---|
| 338 | |
|---|
| 339 | <cfreturn stats> |
|---|
| 340 | </cffunction> |
|---|
| 341 | |
|---|
| 342 | |
|---|
| 343 | <cffunction name="getSurvey" access="public" returnType="query" output="false" |
|---|
| 344 | hint="Returns a survey."> |
|---|
| 345 | <cfargument name="id" type="uuid" required="true" hint="The UUID of the survey to retrieve."> |
|---|
| 346 | <cfset var qGetSurvey = ""> |
|---|
| 347 | <cfset var el = ""> |
|---|
| 348 | |
|---|
| 349 | <cfquery name="qGetSurvey" datasource="#variables.dsn#"> |
|---|
| 350 | select id, name, description, active, datebegin, dateend, resultmailto, surveypassword, thankyoumsg |
|---|
| 351 | from #variables.tableprefix#surveys |
|---|
| 352 | where id = <cfqueryparam value="#arguments.id#" cfsqltype="CF_SQL_VARCHAR" maxlength="35"> |
|---|
| 353 | </cfquery> |
|---|
| 354 | |
|---|
| 355 | <cfif qGetSurvey.recordCount> |
|---|
| 356 | <cfset queryAddColumn(qGetSurvey,"emailList",arrayNew(1))> |
|---|
| 357 | <cfset el = getEmailList(arguments.id)> |
|---|
| 358 | <cfset qGetSurvey.emailList[1] = valueList(el.emailaddress)> |
|---|
| 359 | <cfreturn qGetSurvey> |
|---|
| 360 | <cfelse> |
|---|
| 361 | <cfset variables.utils.throw("SurveyCFC","Invalid Survey requested.")> |
|---|
| 362 | </cfif> |
|---|
| 363 | |
|---|
| 364 | </cffunction> |
|---|
| 365 | |
|---|
| 366 | <cffunction name="getSurveys" access="public" returnType="query" output="false" |
|---|
| 367 | hint="Returns all the surveys."> |
|---|
| 368 | <cfargument name="bActiveOnly" type="boolean" required="false" default="false" hint="Restrict to active surveys only. Also does the date restriction."> |
|---|
| 369 | <cfset var qGetSurveys = ""> |
|---|
| 370 | <cfset var survey = ""> |
|---|
| 371 | |
|---|
| 372 | <cfquery name="qGetSurveys" datasource="#variables.dsn#"> |
|---|
| 373 | select id, name, description, active, datebegin, dateend, resultmailto, surveypassword, thankyoumsg |
|---|
| 374 | from #variables.tableprefix#surveys |
|---|
| 375 | <cfif arguments.bActiveOnly> |
|---|
| 376 | where active = 1 |
|---|
| 377 | and (datebegin is null or datebegin < <cfqueryparam value="#now()#" cfsqltype="CF_SQL_TIMESTAMP">) |
|---|
| 378 | and (dateend is null or dateend > <cfqueryparam value="#now()#" cfsqltype="CF_SQL_TIMESTAMP">) |
|---|
| 379 | </cfif> |
|---|
| 380 | </cfquery> |
|---|
| 381 | |
|---|
| 382 | <cfreturn qGetSurveys> |
|---|
| 383 | |
|---|
| 384 | </cffunction> |
|---|
| 385 | |
|---|
| 386 | <cffunction name="getTopRank" access="public" returnType="numeric" output="false" |
|---|
| 387 | hint="Gets the highest rank of the questions."> |
|---|
| 388 | <cfargument name="id" type="uuid" required="true" hint="The UUID of the survey."> |
|---|
| 389 | <cfset var qGetTop = ""> |
|---|
| 390 | <cfset var top = 0> |
|---|
| 391 | |
|---|
| 392 | <cfquery name="qGetTop" datasource="#variables.dsn#"> |
|---|
| 393 | select max(rank) as highest |
|---|
| 394 | from #variables.tableprefix#questions |
|---|
| 395 | where surveyidfk = <cfqueryparam value="#arguments.id#" cfsqltype="CF_SQL_VARCHAR" maxlength="35"> |
|---|
| 396 | </cfquery> |
|---|
| 397 | |
|---|
| 398 | <cfif qGetTop.recordCount> |
|---|
| 399 | <cfset top = val(qGetTop.highest)> |
|---|
| 400 | </cfif> |
|---|
| 401 | |
|---|
| 402 | <cfreturn top> |
|---|
| 403 | <!--- |
|---|
| 404 | So for some reason, I had switched from doing max+1 to this. I don't know why. Reverting back. |
|---|
| 405 | <cfset var qGetTop = ""> |
|---|
| 406 | <cfset var top = 0> |
|---|
| 407 | |
|---|
| 408 | <cfquery name="qGetTop" datasource="#variables.dsn#"> |
|---|
| 409 | select count(rank) as highest |
|---|
| 410 | from #variables.tableprefix#questions |
|---|
| 411 | where surveyidfk = <cfqueryparam value="#arguments.id#" cfsqltype="CF_SQL_VARCHAR" maxlength="35"> |
|---|
| 412 | </cfquery> |
|---|
| 413 | |
|---|
| 414 | <cfif qGetTop.recordCount> |
|---|
| 415 | <cfset top = val(qGetTop.highest)> |
|---|
| 416 | </cfif> |
|---|
| 417 | |
|---|
| 418 | <cfreturn top> |
|---|
| 419 | ---> |
|---|
| 420 | </cffunction> |
|---|
| 421 | |
|---|
| 422 | <cffunction name="resetEmailList" access="public" returnType="void" output="false" |
|---|
| 423 | hint="Resets a survey's email restriction list."> |
|---|
| 424 | <cfargument name="id" type="uuid" required="true" hint="The UUID of the survey."> |
|---|
| 425 | |
|---|
| 426 | <cfquery datasource="#variables.dsn#"> |
|---|
| 427 | delete from #variables.tableprefix#survey_emailaddresses |
|---|
| 428 | where surveyidfk = <cfqueryparam value="#arguments.id#" cfsqltype="CF_SQL_VARCHAR" maxlength="35"> |
|---|
| 429 | </cfquery> |
|---|
| 430 | |
|---|
| 431 | </cffunction> |
|---|
| 432 | |
|---|
| 433 | <cffunction name="surveyCompletedBy" access="public" returnType="boolean" output="false" |
|---|
| 434 | hint="Returns true if a owner has taken a survey."> |
|---|
| 435 | <cfargument name="id" type="uuid" required="true" hint="The UUID of the survey."> |
|---|
| 436 | <cfargument name="ownerid" type="string" required="true" hint="The owner."> |
|---|
| 437 | <cfset var qResults = ""> |
|---|
| 438 | |
|---|
| 439 | <cfquery name="qResults" datasource="#variables.dsn#"> |
|---|
| 440 | select surveyidfk |
|---|
| 441 | from #variables.tableprefix#survey_results |
|---|
| 442 | where surveyidfk = <cfqueryparam value="#arguments.id#" cfsqltype="CF_SQL_VARCHAR" maxlength="35"> |
|---|
| 443 | and ownerid = <cfqueryparam value="#arguments.ownerid#" cfsqltype="CF_SQL_VARCHAR" maxlength="255"> |
|---|
| 444 | </cfquery> |
|---|
| 445 | |
|---|
| 446 | <cfreturn qResults.recordCount gt 0> |
|---|
| 447 | |
|---|
| 448 | </cffunction> |
|---|
| 449 | |
|---|
| 450 | <cffunction name="updateSurvey" access="public" returnType="void" output="false" |
|---|
| 451 | hint="Updates a survey in the db."> |
|---|
| 452 | <cfargument name="id" type="uuid" required="true" hint="Survey ID."> |
|---|
| 453 | <cfargument name="name" type="string" required="true" hint="Survey name."> |
|---|
| 454 | <cfargument name="description" type="string" required="true" hint="Survey description."> |
|---|
| 455 | <cfargument name="active" type="boolean" required="true" hint="Determines if the survey is active or not."> |
|---|
| 456 | <cfargument name="dateBegin" type="date" required="false" hint="Time when survey begins."> |
|---|
| 457 | <cfargument name="dateEnd" type="date" required="false" hint="Time when survey ends."> |
|---|
| 458 | <cfargument name="resultMailto" type="string" required="false" hint="Email address to send results to."> |
|---|
| 459 | <cfargument name="surveyPassword" type="string" required="false" hint="Survey password necessary for access."> |
|---|
| 460 | <cfargument name="thankYouMsg" type="string" required="false" hint="Survey thank you message."> |
|---|
| 461 | |
|---|
| 462 | <cfif not validData(arguments)> |
|---|
| 463 | <cfset variables.utils.throw("SurveyCFC","This survey data is not valid.")> |
|---|
| 464 | </cfif> |
|---|
| 465 | |
|---|
| 466 | <cfquery datasource="#variables.dsn#"> |
|---|
| 467 | update #variables.tableprefix#surveys |
|---|
| 468 | set |
|---|
| 469 | name = <cfqueryparam value="#arguments.name#" cfsqltype="CF_SQL_VARCHAR" maxlength="50">, |
|---|
| 470 | description = <cfqueryparam value="#arguments.description#" cfsqltype="CF_SQL_VARCHAR" maxlength="255">, |
|---|
| 471 | active = <cfqueryparam value="#arguments.active#" cfsqltype="#variables.utils.getQueryParamType(variables.dbtype,"CF_SQL_BIT")#">, |
|---|
| 472 | <cfif isDefined("arguments.dateBegin")> |
|---|
| 473 | datebegin = <cfqueryparam value="#arguments.dateBegin#" cfsqltype="CF_SQL_TIMESTAMP">, |
|---|
| 474 | <cfelse> |
|---|
| 475 | datebegin = null, |
|---|
| 476 | </cfif> |
|---|
| 477 | <cfif isDefined("arguments.dateEnd")> |
|---|
| 478 | dateend = <cfqueryparam value="#arguments.dateEnd#" cfsqltype="CF_SQL_TIMESTAMP">, |
|---|
| 479 | <cfelse> |
|---|
| 480 | dateend = null, |
|---|
| 481 | </cfif> |
|---|
| 482 | resultmailto = <cfqueryparam value="#arguments.resultMailTo#" cfsqltype="CF_SQL_VARCHAR" maxlength="50">, |
|---|
| 483 | surveypassword = <cfqueryparam value="#arguments.surveyPassword#" cfsqltype="CF_SQL_VARCHAR" maxlength="50">, |
|---|
| 484 | thankyoumsg = <cfqueryparam value="#arguments.thankyoumsg#" cfsqltype="CF_SQL_LONGVARCHAR"> |
|---|
| 485 | |
|---|
| 486 | where id = <cfqueryparam value="#arguments.ID#" cfsqltype="CF_SQL_VARCHAR" maxlength="35"> |
|---|
| 487 | </cfquery> |
|---|
| 488 | |
|---|
| 489 | </cffunction> |
|---|
| 490 | |
|---|
| 491 | <cffunction name="validData" access="private" returnType="boolean" output="false" |
|---|
| 492 | hint="Checks a structure to see if it contains valid data for adding/updating a survey."> |
|---|
| 493 | <cfargument name="data" type="struct" required="true" hint="The data to validate."> |
|---|
| 494 | |
|---|
| 495 | <cfif not structKeyExists(arguments.data,"name") or not len(trim(arguments.data.name)) or |
|---|
| 496 | not structKeyExists(arguments.data,"description") or not len(trim(arguments.data.description))> |
|---|
| 497 | <cfreturn false> |
|---|
| 498 | </cfif> |
|---|
| 499 | |
|---|
| 500 | <!--- if a begin and end date has been specified, end date must be after begin date ---> |
|---|
| 501 | <cfif structKeyExists(arguments.data,"beginDate") and structKeyExists(arguments.data,"endDate") and |
|---|
| 502 | arguments.data.beginDate is not "" and arguments.data.endDate is not "" and |
|---|
| 503 | dateCompare(arguments.data.dateBegin,arguments.data.dateEnd,"s") gte 0> |
|---|
| 504 | <cfreturn false> |
|---|
| 505 | </cfif> |
|---|
| 506 | |
|---|
| 507 | <cfreturn true> |
|---|
| 508 | |
|---|
| 509 | </cffunction> |
|---|
| 510 | |
|---|
| 511 | </cfcomponent> |
|---|