qa-widget-related-qs.php 2.88 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: Widget module class for related questions


	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 23 24 25
class qa_related_qs
{
	public function allow_template($template)
	{
26
		return $template == 'question';
Scott committed
27
	}
Scott committed
28

Scott committed
29 30
	public function allow_region($region)
	{
31
		return in_array($region, array('side', 'main', 'full'));
Scott committed
32
	}
Scott committed
33

Scott committed
34 35
	public function output_widget($region, $place, $themeobject, $template, $request, $qa_content)
	{
Scott committed
36
		require_once QA_INCLUDE_DIR . 'db/selects.php';
Scott committed
37

38
		if (!isset($qa_content['q_view']['raw']['type']) || $qa_content['q_view']['raw']['type'] != 'Q') // question might not be visible, etc...
Scott committed
39
			return;
Scott committed
40

41
		$questionid = $qa_content['q_view']['raw']['postid'];
Scott committed
42

43 44
		$userid = qa_get_logged_in_userid();
		$cookieid = qa_cookie_get();
Scott committed
45

46
		$questions = qa_db_single_select(qa_db_related_qs_selectspec($userid, $questionid, qa_opt('page_size_related_qs')));
Scott committed
47

48
		$minscore = qa_match_to_min_score(qa_opt('match_related_qs'));
Scott committed
49

50 51
		foreach ($questions as $key => $question) {
			if ($question['score'] < $minscore)
Scott committed
52
				unset($questions[$key]);
53
		}
Scott committed
54

55
		$titlehtml = qa_lang_html(count($questions) ? 'main/related_qs_title' : 'main/no_related_qs_title');
Scott committed
56

57
		if ($region == 'side') {
Scott committed
58 59 60 61 62 63
			$themeobject->output(
				'<div class="qa-related-qs">',
				'<h2 style="margin-top:0; padding-top:0;">',
				$titlehtml,
				'</h2>'
			);
Scott committed
64

Scott committed
65
			$themeobject->output('<ul class="qa-related-q-list">');
Scott committed
66

67 68
			foreach ($questions as $question) {
				$themeobject->output(
Scott committed
69 70 71 72 73
					'<li class="qa-related-q-item">' .
					'<a href="' . qa_q_path_html($question['postid'], $question['title']) . '">' .
					qa_html($question['title']) .
					'</a>' .
					'</li>'
74 75
				);
			}
Scott committed
76

Scott committed
77 78 79 80 81 82 83 84 85 86
			$themeobject->output(
				'</ul>',
				'</div>'
			);
		} else {
			$themeobject->output(
				'<h2>',
				$titlehtml,
				'</h2>'
			);
Scott committed
87

88
			$q_list = array(
Scott committed
89
				'form' => array(
90
					'tags' => 'method="post" action="' . qa_self_html() . '"',
Scott committed
91 92
					'hidden' => array(
						'code' => qa_get_form_security_code('vote'),
Scott committed
93
					),
Scott committed
94 95 96
				),
				'qs' => array(),
			);
Scott committed
97

98 99
			$defaults = qa_post_html_defaults('Q');
			$usershtml = qa_userids_handles_html($questions);
Scott committed
100

Scott committed
101
			foreach ($questions as $question) {
102
				$q_list['qs'][] = qa_post_html_fields($question, $userid, $cookieid, $usershtml, null, qa_post_html_options($question, $defaults));
Scott committed
103
			}
Scott committed
104

Scott committed
105
			$themeobject->q_list_and_form($q_list);
Scott committed
106 107
		}
	}
Scott committed
108
}