qa-widget-related-qs.php 2.92 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 22
<?php
/*
	Question2Answer by Gideon Greenspan and contributors
	http://www.question2answer.org/

	File: qa-include/qa-widget-related-qs.php
	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
23 24 25 26
class qa_related_qs
{
	public function allow_template($template)
	{
27
		return $template == 'question';
Scott committed
28
	}
Scott committed
29

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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