21 function saveIntro($isMarketing, $dbh, $Cu, $languageArray)
29 throw new exception(
"Operation not available for marketing messages!", 8);
30 if ($languageArray ==
"")
31 throw new exception(
"Language array is invalid!", 1);
32 $languageArray= HCU_JsonDecode($languageArray);
33 if (!is_array($languageArray))
34 throw new exception(
"Language array is invalid!", 2);
36 $validLanguages= array();
38 $sql=
"select language from culanguage";
41 $sth= db_query($sql, $dbh);
43 throw new exception(
"Language query failed!", 5);
46 for($i=0; $row=db_fetch_array($sth, $i); $i++)
48 $validLanguages[]= $row[0];
51 foreach($languageArray as $languageRow)
53 if (!HCU_array_key_exists(
"language", $languageRow) || !HCU_array_key_exists(
"surveyintro", $languageRow) || !HCU_array_key_exists(
"exists", $languageRow))
54 throw new exception(
"Language array is invalid!", 3);
55 if (!in_array($languageRow[
"exists"], array(
"Y",
"N")))
56 throw new exception(
"Exists variable is invalid!", 4);
57 if (!in_array($languageRow[
"language"], $validLanguages))
58 throw new exception(
"Language is invalid!", 6);
61 if (!db_work ($dbh, HOMECU_WORK_BEGIN))
62 throw new exception(
"begin query failed!", 9);
64 foreach($languageArray as $languageRow)
66 $language= trim($languageRow[
"language"]);
67 $surveyintro= trim(strip_tags($languageRow[
"surveyintro"]));
68 $exists= $languageRow[
"exists"] ==
"Y";
72 $sql= $surveyintro ==
"" ?
"delete from cusurveyintro where cu='$Cu' and language='" . prep_save($language, 5) .
"'" 73 :
"update cusurveyintro set surveyintro='" . prep_save($surveyintro) .
"' where cu='$Cu' and language='" . prep_save($language, 5) .
"'";
76 else if ($surveyintro !=
"")
78 $sql=
"insert into cusurveyintro (cu, language, surveyintro) values ('$Cu', '" . prep_save($language, 5) .
"', '" . prep_save($surveyintro) .
"')";
82 $sth= db_query($sql, $dbh);
84 throw new exception(
"surveyinto update failed!", 110);
87 if (!db_work($dbh, HOMECU_WORK_COMMIT))
88 throw new exception(
"commit work failed!", 111);
92 if ($e->getCode() >= 100)
93 db_work($dbh, HOMECU_WORK_ROLLBACK);
95 return array(
"error" => array($e->getMessage()),
"code" => $e->getCode(),
"sql" => $sqls);
97 return array(
"error" => array(),
"code" => 0,
"sql" => $sqls);
113 function readIntro($isMarketing, $dbh, $Cu)
119 throw new exception(
"Operation not available for marketing messages!", 2);
120 $sql=
"select l.description, l.language, i.surveyintro from culanguage l left join cusurveyintro i on l.language = i.language and i.cu='$Cu' order by l.language";
122 $sth= db_query($sql, $dbh);
124 throw new exception(
"select query failed!", 1);
125 $languageArray= array();
126 for($i=0; $row= db_fetch_assoc($sth, $i); $i++)
128 $surveyintro= trim($row[
"surveyintro"]);
129 $row[
"exists"]= $surveyintro !=
"";
130 $row[
"surveyintro"]= $surveyintro;
131 $row[
"description"]= isset($row[
"description"]) ? trim($row[
"description"]) :
"";
132 $row[
"language"]= isset($row[
"language"]) ? trim($row[
"language"]) :
"";
135 $languageArray[]= $row;
140 return array(
"code" => $e->getCode(),
"error" => array($e->getMessage()),
"sql" => $sqls);
142 return array(
"code" => 0,
"error" => array(),
"sql" => $sqls,
"languageArray" => $languageArray);
159 function readResultDetails($isMarketing, $dbh, $Cu, $surveyid, $language, $answerid)
163 $surveyid= isset($surveyid) ? intval($surveyid) : 0;
164 $answerid= isset($answerid) ? intval($answerid) : 0;
165 $language= isset($language) ? trim($language) :
"";
170 throw new exception(
"Operation not available for marketing messages!", 2);
172 $sql=
"with subset as (select d.answerid, d.surveyid, d.language, c.cu as saysfound, c.user_id, c.user_name, c.optin, c.email, d.answertext as answer, q.question, m.surveyname, 173 m.startdate, m.stopdate from cusurveydetail d 174 inner join cusurveymaster m on d.surveyid = m.surveyid and d.cu = m.cu and d.surveyid= $surveyid and d.cu= '$Cu' and d.language= '" . prep_save($language, 5) .
"' 175 inner join cusurveyquest q on d.surveyid = q.surveyid and d.cu = q.cu and d.language = q.language 176 left join (select s.cu, s.answerid, s.surveyid, s.language, u.user_name, u.egenl_flag as optin, u.email, u.user_id from cusurveysays s 177 inner join ${Cu}user u on s.user_id= u.user_id) c on d.surveyid= c.surveyid and d.cu = c.cu and d.language = c.language and d.answerid= c.answerid) 178 select a.*, coalesce(b.votes, 0) as votes, coalesce(c.totalvotes, 0) as totalvotes 179 from (select * from subset where answerid= $answerid) a 180 cross join (select count(saysfound) from subset where answerid= $answerid) b(votes) 181 cross join (select count(saysfound) from subset) c(totalvotes)";
183 $sth= db_query($sql, $dbh);
185 throw new exception(
"Subset query failed!", 1);
189 $bottomData= array();
190 for($i=0; $row= db_fetch_assoc($sth, $i); $i++)
194 $startdate=
new DateTime($row[
"startdate"]);
195 $stopdate=
new DateTime($row[
"stopdate"]);
196 $row[
"range"]= $startdate->format(
"m/d/Y") .
" - " . $stopdate->format(
"m/d/Y");
197 $row[
"percentage"]= $row[
"totalvotes"] == 0 ? 0 : $row[
"votes"] / $row[
"totalvotes"];
200 unset($topData[0][
"user_id"]);
201 unset($topData[0][
"user_name"]);
202 unset($topData[0][
"optin"]);
203 unset($topData[0][
"email"]);
204 unset($topData[0][
"startdate"]);
205 unset($topData[0][
"stopdate"]);
209 if (isset($row[
"saysfound"]))
210 $bottomData[]= array(
"user_id" => $row[
"user_id"],
"user_name" => $row[
"user_name"],
"optin" => trim($row[
"optin"]) ==
"Y",
"email" => $row[
"email"]);
215 return array(
"sql" => $sqls,
"error" => array($e->getMessage()),
"code" => $e->getCode());
217 return array(
"sql" => $sqls,
"error" => array(),
"code" => 0,
"topData" => $topData,
"bottomData" => $bottomData);
233 function readResults($isMarketing, $dbh, $Cu, $mode, $surveyid)
235 $parameters= array();
241 throw new exception(
"Operation not available for marketing messages!", 3);
243 $masterSubquery= getMasterSubquery($Cu,
false, $mode, $surveyid);
244 $sql=
"select a.surveyid, a.surveyname, a.startdate, a.stopdate, l.description, l.language, b.answerid, b.count, q.question, b.answertext 245 from ($masterSubquery) a 246 inner join (select d.surveyid, d.answerid, d.language, d.answertext, count(s.oid) as count from cusurveydetail d 247 left join cusurveysays s on d.cu = s.cu and d.surveyid = s.surveyid and d.answerid = s.answerid and d.language = s.language and d.cu='$Cu' 248 group by d.surveyid, d.answerid, d.language, d.answertext) as b on a.surveyid = b.surveyid 249 inner join culanguage l on b.language = l.language 250 inner join cusurveyquest q on b.language = q.language and b.surveyid = q.surveyid and q.cu= '$Cu' order by a.startdate desc, a.stopdate desc, a.surveyid, l.language, b.answerid";
253 $sth= db_query($sql, $dbh);
255 throw new exception(
"Count query failed!", 2);
259 for($i=0; list($surveyid, $surveyname, $startdate, $stopdate, $description, $language, $answerid, $numVotes, $question, $answer) = db_fetch_array($sth, $i); $i++)
263 $startdate=
new DateTime($startdate);
264 $stopdate=
new DateTime($stopdate);
265 $range= $startdate->format(
"m/d/Y") .
" - " . $stopdate->format(
"m/d/Y");
267 if (!isset($map[$surveyid]))
268 $map[$surveyid]= array(
"surveyid" => $surveyid,
"surveyname" => $surveyname,
"range" => $range,
"details" => array(),
"sort" => $sort++);
269 if (!isset($map[$surveyid][
"details"][$language]))
270 $map[$surveyid][
"details"][$language]= array(
"description" => $description,
"language" => $language,
"total" => 0,
"question" => $question,
"answers" => array());
271 $map[$surveyid][
"details"][$language][
"answers"][]= array(
"answerid" => $answerid,
"text" => $answer,
"count" => $numVotes);
272 $map[$surveyid][
"details"][$language][
"total"]+= $numVotes;
275 $colorList= array(
'Green',
'Red',
'Blue',
'Orange',
'Purple');
276 $clLength= count($colorList);
277 foreach($map as $surveyid => $hRow)
279 foreach($hRow[
"details"] as $language => $iRow)
282 foreach($iRow[
"answers"] as $j => $jRow)
284 $map[$surveyid][
"details"][$language][
"answers"][$j][
"percentage"]= $iRow[
"total"] == 0 ? 0 : round(floatval($jRow[
"count"]) / intval($iRow[
"total"]), 2);
285 $map[$surveyid][
"details"][$language][
"answers"][$j][
"color"]= $colorList[$j % $clLength];
291 $map= array_values($map);
292 for($i=0, $length=count($map); $i != $length; $i++)
294 $map[$i][
"details"]= array_values($map[$i][
"details"]);
299 return array(
"sqls" => $sqls,
"error" => array($e->getMessage()),
"code" => $e->getCode());
302 return array(
"sqls" => $sqls,
"error" => array(),
"code" => 0,
"data" => $map);
319 function save($isMarketing, $dbh, $Cu, $surveyid, $startdate, $stopdate, $surveyname, $surveytitle, $runstat, $employee, $incBalances, $dontshow, $questions, $answers, $extendDateOnly=
false)
323 $surveyid= isset($surveyid) ? intval($surveyid) : 0;
324 $startdate= isset($startdate) ? trim($startdate) :
"";
325 $stopdate= isset($stopdate) ? trim($stopdate) :
"";
326 $surveyname= isset($surveyname) ? trim($surveyname) :
"";
327 $surveytitle= isset($surveytitle) ? trim($surveytitle) :
"";
328 $employee= isset($employee) ? strtoupper(trim($employee)) :
"";
329 $incBalances= isset($incBalances) ? strtoupper(trim($incBalances)) :
"";
330 $dontshow= isset($dontshow) ? strtoupper(trim($dontshow)) :
"";
331 $questions= isset($questions) ? trim($questions) :
"";
332 $answers= isset($answers) ? trim($answers) :
"";
336 $dateA= DateTime::createFromFormat(
"Y-m-d", $startdate);
338 throw new exception(
"Start date is invalid.", 1);
339 if ($dateA->format(
"Y-m-d") != $startdate)
340 throw new exception(
"Start date is invalid.", 2);
341 $dateB= DateTime::createFromFormat(
"Y-m-d", $stopdate);
343 throw new exception(
"Stop date is invalid.", 3);
344 if ($dateB->format(
"Y-m-d") != $stopdate)
345 throw new exception(
"Stop date is invalid.", 4);
346 if (!in_array($dontshow, array(
"Y",
"N")))
347 throw new exception(
"Don't Show is invalid.", 9);
350 throw new exception(
"Stop date need to be after start date.", 19);
352 if (!$extendDateOnly)
354 if (!is_numeric($runstat))
355 throw new exception(
"Runstat is invalid.", 5);
356 if (!in_array($runstat, array(0,1)))
357 throw new exception(
"Runstat is invalid.", 6);
358 if (!in_array($employee, array(
"Y",
"N")))
359 throw new exception(
"Employee is invalid.", 7);
360 if (!in_array($incBalances, array(
"Y",
"N")))
361 throw new exception(
"IncBalances is invalid.", 8);
364 $surveytype= $isMarketing ? 1 : 0;
366 if (!db_work ($dbh, HOMECU_WORK_BEGIN))
367 throw new exception(
"begin query failed.", 227);
372 throw new exception(
"Can only extend dates of the existing.", 118);
374 $sql=
"select nextval('cusurveymaster_surveyid_seq')";
377 $sth= db_query($sql, $dbh);
379 throw new exception(
"Nextval query failed.", 212);
380 if (db_num_rows($sth) == 0)
381 throw new exception(
"Nextval query failed.", 213);
382 list($surveyid)= db_fetch_array($sth, 0);
383 $sql=
"insert into cusurveymaster (surveyid, cu, startdate, stopdate, surveyname, surveytitle, qstyle, runstat, employee, inc_balances, surveytype, dontshow) 384 values ($surveyid, '$Cu', '$startdate','$stopdate','" . prep_save($surveyname, 50) .
"','" . prep_save($surveytitle) .
"', 3, $runstat, '$employee', '$incBalances', 385 $surveytype, '$dontshow')";
389 $sql= $extendDateOnly ?
"update cusurveymaster set startdate= '$startdate', stopdate='$stopdate', dontshow='$dontshow', surveyname='" . prep_save($surveyname, 50) .
"' 390 where cu='$Cu' and surveyid=$surveyid" 391 :
"update cusurveymaster set startdate= '$startdate', stopdate='$stopdate', surveyname='" . prep_save($surveyname, 50) .
"', surveytitle='" . prep_save($surveytitle)
392 .
"', runstat= $runstat, employee= '$employee', inc_balances='$incBalances', dontshow='$dontshow' where cu='$Cu' and surveyid=$surveyid";
395 $sth= db_query($sql, $dbh);
397 throw new exception(
"insert/update cusurveymaster failed.", 228);
399 if (!$extendDateOnly)
401 $validLanguages= array();
402 if ($questions !=
"" || $answers !=
"")
405 $sql=
"select language from culanguage";
408 $sth= db_query($sql, $dbh);
410 throw new exception(
"Language query failed.", 214);
413 for($i=0; $row=db_fetch_array($sth, $i); $i++)
415 $validLanguages[]= $row[0];
419 $questionExists= array(
"english" =>
false,
"spanish" =>
false,
"polish" =>
false);
420 $questionsForSQL= array();
421 if ($questions !=
"")
423 $questions= HCU_JsonDecode($questions);
424 if (!is_array($questions))
425 throw new exception($isMarketing ?
"Messages weren't properly encoded." :
"Questions weren't properly encoded.", 110);
426 foreach($questions as $questionRow)
428 $question= trim($questionRow[
"question"]);
429 $language= trim($questionRow[
"language"]);
430 $question= $isMarketing ? strip_tags(str_replace(array(
"<!--",
"-->"),
"", $question),
"<b><i><p><a><li><ol><ul><em><br><tt><strong><blockquote><img><span><div><style>")
431 : strip_tags($question);
433 if (!in_array($language, $validLanguages))
434 throw new exception(
"Language is not valid.", 115);
438 $questionExists[
"english"]= $question !=
"";
441 $questionExists[
"spanish"]= $question !=
"";
444 $questionExists[
"polish"]= $question !=
"";
448 $questionsForSQL[]=
"('" . prep_save($language, 5) .
"', '" . prep_save($question) .
"')";
452 $sql=
"delete from cusurveyquest where cu='$Cu' and surveyid='$surveyid'";
454 $sth= db_query($sql, $dbh);
456 throw new exception(
"delete cusurveyquest failed.", 229);
458 if (count($questionsForSQL) > 0)
460 $sql=
"insert into cusurveyquest (cu, surveyid, language, question) select '$Cu', $surveyid, a.* from (values " . implode(
", ", $questionsForSQL) .
") as a";
462 $sth= db_query($sql, $dbh);
464 throw new exception(
"insert cusurveyquest failed.", 230);
468 if (!$questionExists[
"english"])
469 throw new exception(
"Please include a " . ($isMarketing ?
"message body" :
"question") .
" for English.", 121);
473 $answerExists= array(
"english" =>
false,
"spanish" =>
false,
"polish" =>
false);
474 $answersForSQL= array();
477 $answers= HCU_JsonDecode($answers);
478 if (!is_array($answers))
479 throw new exception(
"Answers weren't properly encoded.", 116);
482 foreach($answers as $answerRow)
484 $english= trim($answerRow[
"english"]);
485 $polish= trim($answerRow[
"polish"]);
486 $spanish= trim($answerRow[
"spanish"]);
490 if ($polish ==
"" && $spanish ==
"")
492 throw new exception(
"English answer must be populated if either the polish or spanish answers are populated.", 122);
495 $answersForSQL[]=
"($index, 'en_US', '" . prep_save($english, 255) .
"')";
499 $answerExists[
"polish"]=
true;
500 if (!$questionExists[
"polish"])
501 throw new exception(
"Polish question is required when there are one or more Polish answers.", 123);
502 $answersForSQL[]=
"($index, 'pl_US', '" . prep_save($polish, 255) .
"')";
507 $answerExists[
"spanish"]=
true;
508 if (!$questionExists[
"spanish"])
509 throw new exception(
"Spanish question is required when there are one or more Spanish answers.", 124);
510 $answersForSQL[]=
"($index, 'es_US', '" . prep_save($spanish, 255) .
"')";
515 $sql=
"delete from cusurveydetail where cu='$Cu' and surveyid='$surveyid'";
517 $sth= db_query($sql, $dbh);
519 throw new exception(
"delete cusurveydetail failed.", 231);
521 if (count($answersForSQL) > 0)
523 $sql=
"insert into cusurveydetail (cu, surveyid, slidermin, slidermax, answerid, language, answertext) 524 select '$Cu', $surveyid, 0, 0, a.* from (values " . implode(
", ", $answersForSQL) .
") as a";
526 $sth= db_query($sql, $dbh);
528 throw new exception(
"insert cusurveydetail failed!", 132);
532 if ($questionExists[
"spanish"] && !$answerExists[
"spanish"])
533 throw new exception(
"One or more Spanish answers are required when there is a Spanish question.", 125);
534 if ($questionExists[
"polish"] && !$answerExists[
"polish"])
535 throw new exception(
"One or more Polish answers are required when there is a Polish question.", 126);
539 $sql=
"delete from cusurveysays where surveyid='$surveyid' and cu='$Cu'";
541 $sth= db_query($sql, $dbh);
543 throw new exception(
"delete cusurveysays failed!", 233);
546 if (!db_work($dbh, HOMECU_WORK_COMMIT))
547 throw new exception(
"commit work failed!", 234);
551 if ($e->getCode() >= 100)
552 db_work($dbh, HOMECU_WORK_ROLLBACK);
554 return array(
"sql" => $sqls,
"error" => array($e->getCode >= 200 ?
"Save failed with code: " . $e->getCode() .
"." : $e->getMessage()),
"code" => $e->getCode());
556 return array(
"sql" => $sqls,
"error" => array(),
"code" => 0);
573 function getMasterSubquery($Cu, $isMarketing, $mode=
"all", $surveyid=0)
575 $masterWhere= array(
"cu='$Cu'", $isMarketing ?
"surveytype = 1" :
"coalesce(surveytype, 0) = 0");
581 $masterWhere[]=
"runstat = 1";
582 $masterWhere[]=
"now() between startdate and stopdate";
585 $masterWhere[]=
"runstat = 1";
586 $masterWhere[]=
"startdate <= stopdate";
587 $masterWhere[]=
"now() > stopdate";
590 $masterWhere[]=
"runstat = 1";
591 $masterWhere[]=
"startdate <= stopdate";
592 $masterWhere[]=
"now() < startdate";
595 $masterWhere[]=
"runstat = 0";
599 if (intval($surveyid) == 0)
600 throw new exception(
"Survey needs to be defined.", 2);
601 $masterWhere[]=
"surveyid = $surveyid";
604 throw new exception(
"Mode is invalid.", 1);
607 return "select surveyid, startdate, stopdate, surveyname, runstat, employee, dontshow, inc_balances, surveytitle from cusurveymaster" 608 . (count($masterWhere) > 0 ?
" where " . implode(
" and ", $masterWhere) :
"");
628 function readView($isMarketing, $dbh, $Cu, $surveyid, $mode, $includeMasterData=
false)
632 $surveyid= isset($surveyid) ? intval($surveyid) : 0;
633 $mode= isset($mode) ? trim($mode) :
"";
637 $masterSubquery= getMasterSubquery($Cu, $isMarketing, $mode, $surveyid);
638 $sql=
"select a.pname, q.question, l.language, l.description, " . (!$isMarketing ?
"i.surveyintro, d.answerid, d.answertext, " :
"")
639 . ($includeMasterData ?
"m.* " :
"m.surveyid ") .
"from cuadmin a inner join culanguage l on a.cu= '$Cu' cross join ($masterSubquery) m 640 left join cusurveyquest q on a.cu = q.cu and m.surveyid = q.surveyid and l.language = q.language " 641 . (!$isMarketing ?
"left join cusurveydetail d on a.cu = d.cu and l.language = d.language and m.surveyid = d.surveyid 642 left join cusurveyintro i on a.cu= i.cu and l.language = i.language " :
"")
643 .
"order by l.language, m.stopdate, m.surveyid" . (!$isMarketing ?
", d.answerid" :
"");
645 $sth= db_query($sql, $dbh);
648 throw new exception(
"Query failed!", 1);
652 for($i=0; $row= db_fetch_assoc($sth, $i); $i++)
654 $language= isset($row[
"language"]) ? trim($row[
"language"]) :
"";
655 $description= isset($row[
"description"]) ? trim($row[
"description"]) :
"";
656 $surveyintro= isset($row[
"surveyintro"]) ? trim($row[
"surveyintro"]) :
"";
657 $question= isset($row[
"question"]) ? trim($row[
"question"]) :
"";
658 $answer= isset($row[
"answertext"]) ? trim($row[
"answertext"]) :
"";
660 if ($includeMasterData)
662 if (!isset($data[$row[
"surveyid"]]))
664 $surveyRow= array(
"surveyid" => $row[
"surveyid"],
"question" => trim($row[
"question"]),
"languages" => array(),
"startdate" => $row[
"startdate"],
665 "surveytitle" => $row[
"surveytitle"],
"surveyname" => $row[
"surveyname"],
"stopdate" => $row[
"stopdate"],
"runstat" => $row[
"runstat"],
666 "employee" => $row[
"employee"],
"dontshow" => $row[
"dontshow"],
"inc_balances" => $row[
"inc_balances"]);
667 modifyMasterData($surveyRow);
668 $data[$row[
"surveyid"]]= $surveyRow;
670 if ($question !=
"" || $answer !=
"")
672 if (!isset($data[$row[
"surveyid"]][
"languages"][$row[
"language"]]))
673 $data[$row[
"surveyid"]][
"languages"][$row[
"language"]]= array(
"language" => $language,
"description" => $description,
"surveyintro" => $surveyintro,
674 "answers" => array(),
"question" => $question);
675 if (!$isMarketing && isset($row[
"answerid"]))
676 $data[$row[
"surveyid"]][
"languages"][$row[
"language"]][
"answers"][]= array(
"id" => $row[
"answerid"],
"text" => $answer);
681 if (!isset($data[$row[
"language"]]))
682 $data[$row[
"language"]]= array(
"language" => $language,
"description" => $description,
"surveyintro" => $surveyintro,
683 "title" => $language ==
"en_US" ?
"" : $description,
"surveys" => array());
684 if ($question !=
"" || $answer !=
"")
686 if (!isset($data[$row[
"language"]][
"surveys"][$row[
"surveyid"]]))
688 $surveyRow= array(
"surveyid" => $row[
"surveyid"],
"question" => $question,
"answers" => array());
689 $data[$row[
"language"]][
"surveys"][$row[
"surveyid"]]= $surveyRow;
691 if (!$isMarketing && isset($row[
"answerid"]))
692 $data[$row[
"language"]][
"surveys"][$row[
"surveyid"]][
"answers"][]= array(
"id" => $row[
"answerid"],
"text" => $answer);
697 $data= array_values($data);
698 for($i=0, $iCount= count($data); $i != $iCount; $i++)
700 if ($includeMasterData)
701 $data[$i][
"languages"]= array_values($data[$i][
"languages"]);
703 $data[$i][
"surveys"]= array_values($data[$i][
"surveys"]);
708 return array(
"error" => array($e->getMessage()),
"code" => $e->getCode(),
"sql" => $sqls);
710 return array(
"error" => array(),
"code" => 0,
"sql" => $sqls,
"data" => $data);
725 function readNew($dbh, $Cu)
729 $sql=
"select l.description, l.language, '' as question from culanguage l order by l.language";
732 $sth= db_query($sql, $dbh);
734 throw new exception(
"Survey question query failed!", 3);
735 $questionData= array();
736 for($i=0; $row= db_fetch_assoc($sth, $i); $i++)
738 $row[
"exists"]=
false;
739 $questionData[]= $row;
744 return array(
"sql" => $sqls,
"error" => array($e->getMessage()),
"code" => $e->getCode());
746 return array(
"sql" => $sqls,
"error" => array(),
"code" => 0,
"questionData" => $questionData);
755 function modifyMasterData(&$row)
757 $row[
"isDraft"]= intval($row[
"runstat"]) == 0;
758 $row[
"affectsEmployees"]= trim($row[
"employee"]) ==
"Y";
759 $row[
"isPopup"]= trim($row[
"inc_balances"]) !=
"Y";
760 $row[
"dontShow"]= trim($row[
"dontshow"]) ==
"Y";
761 unset($row[
"runstat"]);
762 unset($row[
"employee"]);
763 unset($row[
"inc_balances"]);
764 unset($row[
"dontshow"]);
784 function readEdit($isMarketing, $dbh, $Cu, $surveyid, $includeMainInfo=
true, $includeHasResults=
true)
787 $surveyid= trim($surveyid) ? intval($surveyid) : 0;
792 if ($includeMainInfo)
794 $sql= getMasterSubquery($Cu, $isMarketing,
"single", $surveyid);
797 $sth= db_query($sql, $dbh);
799 throw new exception(
"Survey master query failed.", 101);
800 if (db_num_rows($sth) == 0)
801 throw new exception(
"Survey master record not found.", 102);
803 $masterData= db_fetch_assoc($sth, 0);
804 if ($includeHasResults && $isMarketing)
805 $hasResults= intval($masterData[
"runstat"]) != 0;
806 modifyMasterData($masterData);
809 $sql=
"select trim(l.description) as description, trim(l.language) as language, trim(coalesce(q.question, '')) as question, q.cu from culanguage l 810 left join cusurveyquest q on l.language = q.language and q.cu= '$Cu' and q.surveyid= '$surveyid' order by l.language";
813 $sth= db_query($sql, $dbh);
815 throw new exception(
"Survey question query failed.", 103);
819 $questionData= array();
820 for($i=0; $row= db_fetch_assoc($sth, $i); $i++)
822 $row[
"exists"]= isset($row[
"cu"]);
825 switch(trim($row[
"language"]))
828 $hasPolish= $row[
"exists"] && $row[
"question"] !=
"";
831 $hasSpanish= $row[
"exists"] && $row[
"question"] !=
"";
834 $questionData[]= $row;
840 $sql=
"select d.answerid, d.answertext, d.language from cusurveydetail d where d.surveyid='$surveyid' and cu='$Cu' order by d.answerid";
843 $sth= db_query($sql, $dbh);
845 throw new exception(
"Survey detail query failed.", 104);
847 $answerData= array();
848 for($i=0; $row= db_fetch_assoc($sth, $i); $i++)
850 $answerid= intval($row[
"answerid"]);
851 $text= trim($row[
"answertext"]);
852 if (!isset($answerData[$answerid]))
853 $answerData[$answerid]= array(
"rowid" => $i+1,
"polish" =>
"",
"spanish" =>
"",
"english" =>
"");
854 switch($row[
"language"])
857 $answerData[$answerid][
"polish"]= $text;
861 $answerData[$answerid][
"spanish"]= $text;
866 $answerData[$answerid][
"english"]= $text;
869 throw new exception(
"Only English, Polish, and Spanish are allowed.", 6);
872 $nextAnswerRowid= $i+1;
874 if ($includeHasResults)
876 $sql=
"select count(*) from cusurveysays s where s.surveyid= $surveyid and cu='$Cu'";
877 $sth= db_query($sql, $dbh);
879 throw new exception(
"Count query failed!", 5);
880 $hasResults= intval(db_fetch_array($sth, 0)[0]) > 0;
886 return array(
"sql" => $sqls,
"error" => array($e->getCode() >= 200 ?
"Display failed with code: " . $e->getCode() .
"." : $e->getMessage()),
"code" => $e->getCode());
888 $returnArray= array(
"sql" => $sqls,
"error" => array(),
"code" => 0,
"masterData" => $masterData,
"questionData" => $questionData,
"hasResults" => $includeHasResults && $hasResults,
889 "hasSpanish" => $hasSpanish,
"hasPolish" => $hasPolish);
892 $returnArray[
"answerData"]= array_values($answerData);
893 $returnArray[
"nextAnswerRowid"]= $nextAnswerRowid;
909 function delete($dbh, $Cu, $surveyid)
913 $surveyid= trim($surveyid) ? intval($surveyid) : 0;
915 if (!db_work ($dbh, HOMECU_WORK_BEGIN))
916 throw new exception(
"begin query failed.", 1);
920 $sql=
"delete from cusurveymaster where surveyid='$surveyid' and cu='$Cu'";
922 $sth= db_query($sql, $dbh);
924 throw new exception(
"cusurveymaster delete failed.", 102);
926 $sql=
"delete from cusurveydetail where surveyid='$surveyid' and cu='$Cu'";
928 $sth= db_query($sql, $dbh);
930 throw new exception(
"cusurveydetail delete failed.", 103);
932 $sql=
"delete from cusurveysays where surveyid='$surveyid' and cu='$Cu'";
934 $sth= db_query($sql, $dbh);
936 throw new exception(
"cusurveysays delete failed.", 104);
938 $sql=
"delete from cusurveyquest where surveyid='$surveyid' and cu='$Cu'";
940 $sth= db_query($sql, $dbh);
942 throw new exception(
"cusurveyquest delete failed.", 105);
944 if (!db_work($dbh, HOMECU_WORK_COMMIT))
945 throw new exception(
"commit work failed.", 106);
949 if ($e->getCode() >= 100)
950 db_work($dbh, HOMECU_WORK_ROLLBACK);
952 return array(
"code" => $e->getCode(),
"error" => array(
"Delete failed with code: " . $e->getCode() .
"."),
"sql" => $sqls);
954 return array(
"code" => 0,
"error" => array(),
"sql" => $sqls);
965 function mainCmp($a, $b)
967 $statusPriority= array_flip(array(
"",
"Invalid",
"Draft",
"Present",
"Future",
"Past"));
968 $statusCompare= intval($statusPriority[$b[
"status"]]) - intval($statusPriority[$a[
"status"]]);
969 if ($statusCompare != 0)
970 return $statusCompare;
971 $startCompare= $a[
"startMs"] - $b[
"startMs"];
972 if ($startCompare != 0)
973 return $startCompare;
974 $stopCompare= $a[
"stopMs"] - $b[
"stopMs"];
991 function readMain($isMarketing, $dbh, $Cu)
996 $sql= getMasterSubquery($Cu, $isMarketing);
999 $sth= db_query($sql, $dbh);
1001 throw new exception(
"Read query failed!", 1);
1003 $now=
new DateTime();
1004 $now= DateTime::createFromFormat(
"Y-m-d", $now->format(
"Y-m-d"));
1005 for($i=0; $record= db_fetch_assoc($sth, $i); $i++)
1007 $start= DateTime::createFromFormat(
"Y-m-d", $record[
"startdate"]);
1008 $stop= DateTime::createFromFormat(
"Y-m-d", $record[
"stopdate"]);
1010 $record[
"startMs"]= intval($start->format(
"U"));
1011 $record[
"stopMs"]= intval($start->format(
"U"));
1013 $record[
"range"]= $start->format(
"m/d/y") .
" - " . $stop->format(
"m/d/y");
1015 if ($record[
"runstat"] == 0)
1016 $record[
"status"]=
"Draft";
1017 else if ($stop < $start)
1018 $record[
"status"]=
"Invalid";
1019 else if ($now < $start)
1020 $record[
"status"]=
"Future";
1021 else if ($now > $stop)
1022 $record[
"status"]=
"Past";
1024 $record[
"status"]=
"Present";
1026 $record[
"surveyType"]= $record[
"inc_balances"] ==
"Y" ?
"Balances" :
"Popup";
1028 unset($record[
"runstat"]);
1029 unset($record[
"datestat"]);
1030 unset($record[
"inc_balances"]);
1031 $records[]= $record;
1035 usort($records,
"mainCmp");
1037 for($i=0, $length=count($records); $i!= $length; $i++)
1039 $records[$i][
"sortid"]= $sortid++;
1040 unset($records[$i][
"startMs"]);
1041 unset($records[$i][
"stopMs"]);
1046 return array(
"code" => $e->getCode(),
"error" => array($e->getMessage()),
"sql" => $sqls);
1048 return array(
"code" => 0,
"error" => array(),
"record" => $records,
"sql" => $sqls);