<?php

/*  Alternate WebKit
(C) 2012 by Alternate Tools
-----------------------------------------------------

    filename: class_survey.php
*/
include "class_storagefile.php";

if (!defined ("CLASS_SURVEY"))
{
    define ("CLASS_SURVEY", "defined");

    class cSurvey
    {
        var $m_survId;
        var $m_survDate;
        var $m_survTitle;
        var $m_survQuestion;
        var $m_survOptions;
        var $m_survSelectedOption;
        var $m_survInputNames;
        var $m_survInputValues;
        var $m_survFolder;

        function cSurvey()  // PHP 4
        {
            $this->Init();
        }

        function __construct()
        {
            $this->Init();
        }

        function Init()
        {
            $this->m_survId = "0";
            $this->m_survDate = "";
            $this->m_survTitle = "";
            $this->m_survQuestion = "";
            $this->m_survOptions = "";
            $this->m_survSelectedOption = "";
            $this->m_survInputNames = "";
            $this->m_survInputValues = "";

            $this->m_survFolder = "";
        }
        
        // Getter / setter
        function GetId()
        {
            return $this->m_survId;
        }
        
        function GetDate()
        {
            return $this->m_survDate;
        }
        
        function SetTitle($title)
        {
            $this->m_survTitle = $title;
        }
        function GetTitle()
        {
            return $this->m_survTitle;
        }

        function SetQuestion($question)
        {
            $this->m_survQuestion = $question;
        }
        function GetQuestion()
        {
            return $this->m_survQuestion;
        }

        function SetOptions($options)
        {
            $this->m_survOptions = $options;
        }
        function GetOptions()
        {
            return $this->m_survOptions;
        }

        function SetSelectedOption($selectedOption)
        {
            $this->m_survSelectedOption = $selectedOption;
        }
        function GetSelectedOption()
        {
            return $this->m_survSelectedOption;
        }

        function SetInputNames($inputName)
        {
            $this->m_survInputNames = $inputName;
        }
        function GetInputNames()
        {
            return $this->m_survInputNames;
        }

        function SetInputValues($inputValues)
        {
            $this->m_survInputValues = $inputValues;
        }
        function GetInputValues()
        {
            return $this->m_survInputValues;
        }

        function SetFolder($folder)
        {
            $this->m_survFolder = $folder;
        }
        
        // Main methods
        function CreateSurvey()
        {
            $this->Init();
            $this->m_survId = uniqid("sur", FALSE);
            $this->m_survDate = date("Y-m-d H:i:s");
        }

        function DeleteSurveyFinal()
        {
            return unlink($this->m_survFolder . $this->m_survId . ".sur");
        }

        function DeleteSurveyFinalFromId($survId)
        {
            return unlink($this->m_survFolder . $survId . ".sur");
        }


        function LoadSurveyFromId($survId)
        {
            $this->m_survId = $survId;
            return $this->LoadSurvey();
        }

        function LoadSurvey()
        {
            $fileName = $this->m_survFolder . $this->m_survId  . ".sur";
            //echo  $fileName;

            if ($this->m_survId == "0" || $this->m_survFolder == "")
                return false;

            if (file_exists($fileName) == false)
                return false;

            $cStorage = new cStorageFile;
            if ($cStorage->LoadStorageFromFile($fileName) == false)
                return false;

            if ($cStorage->GetData("Id") == "")
                return false;

            $this->m_survId = $cStorage->GetData("Id");
            $this->m_survDate = $cStorage->GetData("Date");
            $this->m_survTitle = $cStorage->GetData("Title");
            $this->m_survQuestion = $cStorage->GetData("Question");
            $this->m_survOptions = $cStorage->GetData("Options");
            $this->m_survSelectedOption = $cStorage->GetData("SelectedOption");
            $this->m_survInputNames = $cStorage->GetData("InputNames");
            $this->m_survInputValues = $cStorage->GetData("InputValues");

            return true;

        }

        function SaveSurvey()
        {
            $newRow = "\r\n";
            $fileName = $this->m_survFolder . $this->m_survId  . ".sur";
            // echo $fileName;

            if ($this->m_survId == "0" || $this->m_survFolder == "")
                return false;

            $cStorage = new cStorageFile;
            $cStorage->SetType("survey");
            $cStorage->SetVersion("1.000");
            $cStorage->AddData("Id", $this->m_survId);
            $cStorage->AddData("Date", $this->m_survDate);
            $cStorage->AddData("Title", $this->m_survTitle);
            $cStorage->AddData("Question", $this->m_survQuestion);
            $cStorage->AddData("Options", $this->m_survOptions);
            $cStorage->AddData("SelectedOption", $this->m_survSelectedOption);
            $cStorage->AddData("InputNames", $this->m_survInputNames);
            $cStorage->AddData("InputValues", $this->m_survInputValues);

            return $cStorage->SaveStorageToFile($fileName);
        }

        function NoMagic($string)
        {
            if (get_magic_quotes_gpc())  // Magic quotes??
                return stripslashes($string);
            return $string;
        }
    }
}