tiki-take_survey.php 4.2 KB
Newer Older
lrargerich's avatar
lrargerich committed
1
<?php
changi67's avatar
changi67 committed
2 3 4
/**
 * @package tikiwiki
 */
5
// (c) Copyright 2002-2016 by authors of the Tiki Wiki CMS Groupware Project
changi67's avatar
changi67 committed
6
// 
7 8
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
changi67's avatar
changi67 committed
9 10
// $Id$

11
$section = 'surveys';
12 13
require_once ('tiki-setup.php');
include_once ('lib/surveys/surveylib.php');
14
if ($prefs['feature_categories'] == 'y') {
15
	$categlib = TikiLib::lib('categ');
16
}
17 18 19

$access->check_feature('feature_surveys');

20 21
if (!isset($_REQUEST["surveyId"])) {
	$smarty->assign('msg', tra("No survey indicated"));
22
	$smarty->display("error.tpl");
23
	die;
lrargerich's avatar
lrargerich committed
24
}
25
$access->check_permission('take_survey', 'Take Survey', 'survey', $_REQUEST['surveyId']);
26

27 28
$smarty->assign('surveyId', $_REQUEST["surveyId"]);
$survey_info = $srvlib->get_survey($_REQUEST["surveyId"]);
nyloth's avatar
nyloth committed
29
$smarty->assign('survey_info', $survey_info);
30

lrargerich's avatar
lrargerich committed
31
// Check if user has taken this survey
32 33 34
if ($tiki_p_admin != 'y') {
	if ($tikilib->user_has_voted($user, 'survey' . $_REQUEST["surveyId"])) {
		$smarty->assign('msg', tra("You cannot take this survey twice"));
35
		$smarty->display("error.tpl");
36 37
		die;
	}
lrargerich's avatar
lrargerich committed
38
}
39
$questions = $srvlib->list_survey_questions($_REQUEST["surveyId"], 0, -1, 'position_asc', '');
40 41 42 43 44 45 46 47
$smarty->assign('pagination', false);
foreach($questions['data'] as $question) {
	if ($question['type'] === 'h' && !empty($question['explode']) && $question['explode'][0] === 'y') {
		$smarty->assign('pagination', true);
		$headerlib->add_css('.questionblock, .submit {display:none;}')
			->add_jq_onready('
(function($) {
	var surveyPage, surveyPageCount = 0, surveyHeight = 0, h = 0, beenToLastPage = false;
48 49 50
	if (typeof surveyKeepSameHeight === "undefined") {
		surveyKeepSameHeight = false;
	}
51 52 53 54 55 56 57 58 59 60
	$(".questionblock").each(function () {
		h += $(this).outerHeight(true);
		if ($(this).hasClass("page" + (surveyPageCount + 1))) {
			surveyPageCount++;
			if (h > surveyHeight) {
				surveyHeight = h;
			}
			h = 0;
		}
	});
61 62 63 64 65
	if (surveyKeepSameHeight) {
		if (h > surveyHeight) {
			surveyHeight = h;
		}
		$(".surveyquestions").height(surveyHeight + $(".submit").outerHeight(true));
66
	}
67 68 69
	var showPage = function (page) {
		if (page < 1) {
			page = 0;
70 71 72
			$(".btn-prev").attr("disabled", true);
			$(".btn-next").attr("disabled", false);
		} else if (page >= surveyPageCount) {
73
			page = surveyPageCount;
74 75 76 77 78
			$(".btn-next").attr("disabled", true);
			$(".btn-prev").attr("disabled", false);
		} else {
			$(".btn-next").attr("disabled", false);
			$(".btn-prev").attr("disabled", false);
79
		}
80
		if (page != surveyPage) {
81
			surveyPage = page;
82 83 84 85 86 87
			var sTop = $(".surveyquestions").offset().top - 10;
			if ($(window).scrollTop() > sTop) {
				$(\'html, body\').animate({
					scrollTop: sTop
				}, 1000);
			}
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
			$(".questionblock:visible").slideUp("fast");
			$(".page" + surveyPage).slideDown("fast");
			location.hash = "page" + surveyPage;
			$(".pageNum").text(surveyPage + 1);
			$(".pageCount").text(surveyPageCount + 1);
			if (surveyPage === surveyPageCount) {
				beenToLastPage = true;
				$(".submit").show("fast");
			} else if (!beenToLastPage) {
				$(".submit").hide("fast");
			}
		}
	};
	$(".btn-prev").click(function () {
		showPage(surveyPage - 1);
		return false;
	});
	$(".btn-next").click(function () {
		showPage(surveyPage + 1);
		return false;
	});
109 110 111 112 113 114 115 116
	$(window).on("hashchange load", function () {
		var goPage = location.hash.match(/page(\d+)/);
		if (goPage) {
			showPage(Number(goPage[1]));
		} else {
			showPage(0);
		}
	});
117 118 119 120 121
})(jQuery)
			');
		break;
	}
}
122
$smarty->assign_by_ref('questions', $questions["data"]);
123
$error_msg = '';
124
if (isset($_REQUEST["ans"])) {
125
	check_ticket('take-survey');
126
	$srvlib->register_answers($_REQUEST['surveyId'], $questions['data'], $_REQUEST, $error_msg);
127 128 129 130 131 132 133
	if (empty($error_msg)) {
		if (!empty($_REQUEST["vote"])) {
			$srvlib->add_survey_hit($_REQUEST["surveyId"]);
		}
		header('Location: tiki-list_surveys.php');
		die;
	}
134 135
}

136 137 138 139 140
$showToolBars = false;
if($prefs['poll_surveys_textarea_hidetoolbar'] != 'y')
	$showToolBars = true;
$smarty->assign('showToolBars', $showToolBars);

141
include_once ('tiki-section_options.php');
142
ask_ticket('take-survey');
lrargerich's avatar
lrargerich committed
143
// Display the template
nyloth's avatar
nyloth committed
144
$smarty->assign('error_msg', $error_msg);
145
$smarty->assign('mid', 'tiki-take_survey.tpl');
146
$smarty->display("tiki.tpl");