<?php

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

File:         class_surveylist.php

*/

include "class_survey.php";

if (!defined("CLASS_SURVEYLIST"))
{
    define ("CLASS_SURVEYLIST", "DEFINED");

    class cSurveyList
    {

        var $m_survlFolder;
        var $m_survlIds;

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

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

        function Init()
        {
            unset($this->m_survlIds);
            $this->m_survlIds = array();
            $this->m_survlFolder = "";
        }

        function SetFolder($folder)
        {
            $this->m_survlFolder = $folder;
        }

        function SetSurveyList($suveryList)
        {
            $this->m_survlIds = $suveryList;
        }

        function GetSurveyList()
        {
            if (isset($this->m_survlIds) == true)
                return $this->m_survlIds;
            else
                return array();
        }
        
        function LoadSurveyList($filter = 0, $sort = true)
        {
            $rDir = dir($this->m_survlFolder);
            $strEntry = "";
            $fileName = "";
            $survSurvey = new cSurvey;
            $survSurvey->SetFolder($this->m_survlFolder);

            while (false !== ($strEntry = $rDir->read()))
            {
                if (substr($strEntry, -4 ) == '.sur')
                {
                    $fileName = substr($strEntry, 0, strlen($strEntry) - 4);
                    if ($filter == 0)
                        $this->m_survlIds[] = $fileName;
                    else
                    {
                        if ($survSurvey->LoadSurveyFromId($fileName) == true)
                        {
                            $addMe = false;
                            if ($survSurvey->GetQuestion() == $filter)
                                $addMe = true;
                            if ($addMe == true)
                                $this->m_survlIds[] = $fileName;
                        }
                    }
                }
            }
            if ($sort == true)
                $this->SortByDate(true);
        }
        
        function SortByDate($bAscending = true)
        {
            if (isset($this->m_survlIds) == false)
                return;
            $survSurvey = new cSurvey;
            $survSurvey->SetFolder($this->m_survlFolder);
            $preArray = array();
            foreach ($this->m_survlIds as $value)
            {
                $survSurvey->LoadSurveyFromId($value);
                $preArray[] = $survSurvey->GetDate() . '-|-' . $survSurvey->GetId();
            }
            if ($bAscending == true)
                sort($preArray);
            else
                rsort($preArray);
            unset($this->m_survlIds);
            foreach ($preArray as $value)
            {
                $tmpArray = explode('-|-', $value);
                $this->m_survlIds[] = $tmpArray[1];
            }
        }


        function SortByQuestion($bAscending = true)
        {
            if (isset($this->m_survlIds) == false)
                return;
            $survSurvey = new cSurvey;
            $survSurvey->SetFolder($this->m_survlFolder);
            $preArray = array();
            foreach ($this->m_survlIds as $value)
            {
                $survSurvey->LoadSurveyFromId($value);
                $preArray[] = $survSurvey->GetQuestion() . '-|-' . $survSurvey->GetId();
            }
            if ($bAscending == true)
                sort($preArray);
            else
                rsort($preArray);
            unset($this->m_survlIds);
            foreach ($preArray as $value)
            {
                $tmpArray = explode('-|-', $value);
                $this->m_survlIds[] = $tmpArray[1];
            }
        }
    }
}
?>