<?php
//*Client Data System, Copyright (C) 2000, 2001, 2002 Tedd Kelleher. This is free software, subject to the
//*GNU GENERAL PUBLIC LICENSE, Version 2, June 1991 (in file named gpl.txt), which should accompany
//*any distribution of this file. Tedd Kelleher can be contacted at hide@address.com
class Survey {
var $survey_name;
var $survey_questions;
var $survey_profile;
var $existing_survey_answers;
var $client_ip;
function Survey ($survey_name) {
GLOBAL $db_link;
if(ereg("[^a-z\_]", $survey_name)) {
echo "Illegal survey name"; exit;
}
else {
$this->survey_name = $survey_name;
}
$this->client_ip = addslashes(getenv("REMOTE_ADDR"));
$db_link = db_generic_connect ();
$this->survey_questions = new Questions_into_array ($this->survey_name, "40", "9999", "9999", "display");
//echo "Values are";
//display_value ($this->survey_questions->questions);
$this->survey_profile = pull_report_type_profile ($this->survey_name);
if ( $this->survey_profile["report_on_unit"] != "survey" ) {
echo "Not a survey type report";
exit;
}
}
function check_for_recent_survey_submission () {
//Only allows a new survey from an individual IP once every 24 hours
$unix_time_one_day_ago = time() - 86400;
$sql = "SELECT * FROM logged_in_log WHERE log_user_name LIKE 'SURVEY'
AND log_group_name LIKE '".$this->survey_name."'
AND log_unix_date > '".$unix_time_one_day_ago."'
AND log_ip LIKE '".$this->client_ip."' ORDER BY log_unix_date DESC";
//
$result = run_query ($sql, "Checking for existing reports");
if ( num_rows( $result ) > 0 ) {
$row = fetch_array ( $result, "Fetching existing survery data", 0 );
//display_value ($row);
//Client report id stored in group_id field
return $row["log_group_id"];
}
else {
return 0;
}
}
function pull_existing_answers () {
//Check to see if they have submitted recently
$existing_report_id = check_for_recent_survey_submission();
if ( $existing_report_id > 0 ) {
$cnt_rpt = new Client_report;
$existing_survey_answers = $cnt_rpt->pull_existing_client_report_answers ( $existing_report_id,
$this->survey_questions->questions, $this->survey_questions->question_elements );
}
else {
$existing_survey_answers = "";
}
return $existing_survey_answers;
}
function insert_survey_answers ($vetted_values, $existing_report_id) {
GLOBAL $unix_date;
$insert = new Insert_client_report;
$new_report_id = $insert->insert_client_report_answers ($this->survey_profile["report_type_id"],
9999, $this->survey_questions->questions, $vetted_values,
"yes", $existing_report_id);
//Store the name of the report in user_name, and the report id number in the group id field
$sql = "INSERT INTO logged_in_log ( log_user_name, log_group_name, log_group_id, log_ip, log_unix_date )
VALUES ( 'SURVEY', '".$this->survey_name."', '".$new_report_id."', '".$this->client_ip."', '".$unix_date."' )";
run_query ( $sql, "Inserting record of submitted survey" );
}
function display_survey ($vetted_form_answers) {
GLOBAL $question_validation_error, $head_dynamic_style, $message, $message_type, $this_page;
if($question_validation_error) {
if(!$message) {$message .= "ERROR: Incorrect form input, see below. ";}
$message_type = "error";
}
if($message_type == "ok") {
$head_dynamic_style .= "\n.message {background-color: #66FF99; color: #000066; font-size: \"90%\" } ";
}
elseif ($message_type == "error") {
$head_dynamic_style .= "\n.message {background-color: #FF0000; color: #FFCCCC; font-size: \"90%\" } ";
}
$ht .= form_start($this_page."?name=".$this->survey_name);
$ht .= "<table>";
$ht .= "<tr><td class=\"generictabletop\">";
$ht .= $this->survey_profile["report_title"];
$ht .= "</td></tr>";
$ht .= "<tr><td class=\"smalltd\">";
$ht .= $this->survey_profile["report_instructions"];
$ht .= "</td></tr>";
$ht .= "<tr><td class=\"message\">";
$ht .= $message." ";
$ht .= "</td></tr>";
foreach ( $this->survey_questions->questions AS $current_question ) {
$ht .= "<tr>";
$ht .= question_display ($current_question, $this->survey_questions->question_elements[$current_question["question_id"]],
$vetted_form_answers);
$ht .= "</tr>";
$ht .= "<tr><td> </td></tr>";
}
$ht .= "</table>";
$ht .= form_end("Click Here to Submit Survey");
return $ht;
}
}
?>