admin-points.php 5.23 KB
Newer Older
Scott committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	Description: Controller for admin page for settings about user points


	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	More about this license: http://www.question2answer.org/license.php
*/

Scott committed
22
if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
23
	header('Location: ../../../');
Scott committed
24 25
	exit;
}
Scott committed
26

Scott committed
27 28 29 30 31
require_once QA_INCLUDE_DIR . 'db/recalc.php';
require_once QA_INCLUDE_DIR . 'db/points.php';
require_once QA_INCLUDE_DIR . 'app/options.php';
require_once QA_INCLUDE_DIR . 'app/admin.php';
require_once QA_INCLUDE_DIR . 'util/sort.php';
Scott committed
32 33


Scott committed
34
// Check admin privileges
Scott committed
35

36
if (!qa_admin_check_privileges($qa_content)) {
Scott committed
37
	return $qa_content;
38
}
Scott committed
39 40


Scott committed
41
// Process user actions
Scott committed
42

Scott committed
43 44 45
$securityexpired = false;
$recalculate = false;
$optionnames = qa_db_points_option_names();
Scott committed
46

Scott committed
47 48
if (qa_clicked('doshowdefaults')) {
	$options = array();
Scott committed
49

50
	foreach ($optionnames as $optionname) {
Scott committed
51
		$options[$optionname] = qa_default_option($optionname);
52
	}
Scott committed
53
} else {
54 55
	if (qa_clicked('dosaverecalc')) {
		if (!qa_check_form_security_code('admin/points', qa_post_text('code'))) {
Scott committed
56
			$securityexpired = true;
57 58
		} else {
			foreach ($optionnames as $optionname) {
Scott committed
59
				qa_set_option($optionname, (int)qa_post_text('option_' . $optionname));
60
			}
Scott committed
61

62
			if (!qa_post_text('has_js')) {
Scott committed
63
				qa_redirect('admin/recalc', array('dorecalcpoints' => 1));
64
			} else {
Scott committed
65
				$recalculate = true;
66
			}
Scott committed
67 68 69
		}
	}

Scott committed
70 71 72
	$options = qa_get_options($optionnames);
}

Scott committed
73

Scott committed
74
// Prepare content for theme
Scott committed
75

Scott committed
76
$qa_content = qa_content_prepare();
Scott committed
77

Scott committed
78 79
$qa_content['title'] = qa_lang_html('admin/admin_title') . ' - ' . qa_lang_html('admin/points_title');
$qa_content['error'] = $securityexpired ? qa_lang_html('admin/form_security_expired') : qa_admin_page_error();
Scott committed
80

Scott committed
81 82
$qa_content['form'] = array(
	'tags' => 'method="post" action="' . qa_self_html() . '" name="points_form" onsubmit="document.forms.points_form.has_js.value=1; return true;"',
Scott committed
83

Scott committed
84
	'style' => 'wide',
Scott committed
85

Scott committed
86 87 88 89
	'buttons' => array(
		'saverecalc' => array(
			'tags' => 'id="dosaverecalc"',
			'label' => qa_lang_html('admin/save_recalc_button'),
Scott committed
90
		),
Scott committed
91
	),
Scott committed
92

Scott committed
93 94 95 96 97 98
	'hidden' => array(
		'dosaverecalc' => '1',
		'has_js' => '0',
		'code' => qa_get_form_security_code('admin/points'),
	),
);
Scott committed
99 100


Scott committed
101 102
if (qa_clicked('doshowdefaults')) {
	$qa_content['form']['ok'] = qa_lang_html('admin/points_defaults_shown');
Scott committed
103

Scott committed
104 105 106 107 108 109 110 111
	$qa_content['form']['buttons']['cancel'] = array(
		'tags' => 'name="docancel"',
		'label' => qa_lang_html('main/cancel_button'),
	);
} else {
	if ($recalculate) {
		$qa_content['form']['ok'] = '<span id="recalc_ok"></span>';
		$qa_content['form']['hidden']['code_recalc'] = qa_get_form_security_code('admin/recalc');
Scott committed
112

Scott committed
113 114
		$qa_content['script_rel'][] = 'qa-content/qa-admin.js?' . QA_VERSION;
		$qa_content['script_var']['qa_warning_recalc'] = qa_lang('admin/stop_recalc_warning');
Scott committed
115

Scott committed
116 117
		$qa_content['script_onloads'][] = array(
			"qa_recalc_click('dorecalcpoints', document.getElementById('dosaverecalc'), null, 'recalc_ok');"
Scott committed
118 119 120
		);
	}

Scott committed
121 122 123 124 125
	$qa_content['form']['buttons']['showdefaults'] = array(
		'tags' => 'name="doshowdefaults"',
		'label' => qa_lang_html('admin/show_defaults_button'),
	);
}
Scott committed
126 127


Scott committed
128 129 130 131 132 133 134 135
foreach ($optionnames as $optionname) {
	$optionfield = array(
		'label' => qa_lang_html('options/' . $optionname),
		'tags' => 'name="option_' . $optionname . '"',
		'value' => qa_html($options[$optionname]),
		'type' => 'number',
		'note' => qa_lang_html('admin/points'),
	);
Scott committed
136

Scott committed
137 138 139 140 141 142 143 144
	switch ($optionname) {
		case 'points_multiple':
			$prefix = '&#215;';
			unset($optionfield['note']);
			break;

		case 'points_per_q_voted_up':
		case 'points_per_a_voted_up':
Scott committed
145
		case 'points_per_c_voted_up':
Scott committed
146 147
		case 'points_q_voted_max_gain':
		case 'points_a_voted_max_gain':
Scott committed
148
		case 'points_c_voted_max_gain':
Scott committed
149 150 151 152 153
			$prefix = '+';
			break;

		case 'points_per_q_voted_down':
		case 'points_per_a_voted_down':
Scott committed
154
		case 'points_per_c_voted_down':
Scott committed
155 156
		case 'points_q_voted_max_loss':
		case 'points_a_voted_max_loss':
Scott committed
157
		case 'points_c_voted_max_loss':
Scott committed
158 159 160 161 162 163 164 165 166 167
			$prefix = '&ndash;';
			break;

		case 'points_base':
			$prefix = '+';
			break;

		default:
			$prefix = '<span style="visibility:hidden;">+</span>'; // for even alignment
			break;
Scott committed
168 169
	}

Scott committed
170
	$optionfield['prefix'] = '<span style="width:1em; display:inline-block; display:-moz-inline-stack;">' . $prefix . '</span>';
Scott committed
171

Scott committed
172 173
	$qa_content['form']['fields'][$optionname] = $optionfield;
}
Scott committed
174

Scott committed
175
qa_array_insert($qa_content['form']['fields'], 'points_post_a', array('blank0' => array('type' => 'blank')));
Scott committed
176 177 178
qa_array_insert($qa_content['form']['fields'], 'points_per_c_voted_up', array('blank1' => array('type' => 'blank')));
qa_array_insert($qa_content['form']['fields'], 'points_vote_up_q', array('blank2' => array('type' => 'blank')));
qa_array_insert($qa_content['form']['fields'], 'points_multiple', array('blank3' => array('type' => 'blank')));
Scott committed
179 180


Scott committed
181
$qa_content['navigation']['sub'] = qa_admin_sub_navigation();
Scott committed
182 183


Scott committed
184
return $qa_content;