<?php

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

File:         modify_surveyresults.php
Description:  Administrative module to unlock/lock/delete a survey result

*/

    include "../modules/class_survey.php";
    include "../modules/mod_admin.php";

    $bSurveyResultLoadFailed = false;
    $surId = "";
    $errorString = "";
    
    // Check privileges
    check_login();
    if (user_is_admin() == false)
    {
        log_out();
        return;
    }

    // Get action
    $surModAction = "delete";

    $surSurveyResult = new cSurvey;
    $surSurveyResult->SetFolder($SURVEY_FOLDER);

    if (isset ($_POST['sur_id']) == true)
        $surId = $_POST['sur_id'];
    else if (isset ($_GET['id']) == true)
        $surId = $_GET['id'];

    if ($surSurveyResult->LoadSurveyFromId($surId) == false)
    {
        print_error_message(get_translation('admin_surveymod_errorload') . ', ID: ' . $surId . '!!');
        $bSurveyResultLoadFailed = true;
    }

    // Cancel modification?
    if (isset ($_POST['sur_Cancel']))
    {
        header('location: ' . $ADMIN_LIST_SURVEYRESULTS_SITE);
        return;
    }
    // Modify survey result?
    if (isset ($_POST['sur_DoAction']) && $bSurveyResultLoadFailed == false)
    {
        if ($surModAction == "delete")
        {
            $bDone = $surSurveyResult->DeleteSurveyFinal();  // Survey results can be unlinked
            if ($bDone == false)
                $errorString = get_translation('admin_surveymod_errordelete') . ', ID: ' . $surId . '!!';
            else
            {
                log_message ('User ' . get_user_name() . ' deleted survey result ' . $surSurveyResult->GetId());
                header('location: ' . $ADMIN_LIST_SURVEYRESULTS_SITE);
                return;
            }
        }
    }

    // Display
    $titleString = get_translation('admin_surveymod_deletehead');
    print_admin_header($titleString, true, get_translation('admin_login_logout'), "", true, get_translation('admin_backtomain'),
                       $ADMIN_LIST_PAGEIMAGES_SITE, get_translation('admin_overview'));

    if ($errorString != "")
        print_error_message($errorString);
        
    if ($surModAction == "delete")
        echo_r('<p>' . get_translation('admin_surveymod_questdelete') . ':</p><br>');
    echo_r('<table width="100%" border="0">');
    echo_r('  <tr>');
    echo_r('    <td width="50%">');
    echo_r('   ' . get_translation('admin_survey_id') . ': ' . $surSurveyResult->GetId() . '<br>');
    echo_r('   ' . get_translation('admin_survey_date') . ': ' . $surSurveyResult->GetDate() . '<br>');
    echo_r('   ' . get_translation('admin_survey_title') . ': ' . $surSurveyResult->GetTitle() . '<br>');
    echo_r('   <b>' . get_translation('admin_survey_question') . ': ' . $surSurveyResult->GetQuestion() . '</b><br>');
    echo_r('   ' . get_translation('admin_survey_answer') . ': ' . $surSurveyResult->GetSelectedOption() . '<br>');
    echo_r('    </td>');
    echo_r('    <td width="50%">');
    $inputNames = explode(";", $surSurveyResult->GetInputNames());
    $inputValues = explode(";", $surSurveyResult->GetInputValues());
    for ($i = 0; $i < count($inputNames); $i ++)
    {
        echo_r('   ' . $inputNames[$i] . ': ' . $inputValues[$i] . '<br>');
    }
    echo_r('    </td>');
    echo_r('  </tr>');
    echo_r('</table>');
    
    echo_r('<form method="post" action="">');
    print_bottom_links("sur_DoAction", get_translation('admin_yes'),
                       "sur_Cancel", get_translation('admin_cancel'), false);
    echo_r('  <input type="hidden" value="' .  $surId . '" name="sur_id">  ');
    echo_r('  <input type="hidden" value="' .  $surModAction . '" name="sur_action">  ');
    echo_r('</form>');

    print_admin_footer();
?>