1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8function smarty_function_rating($params, $smarty)
9{
10	global $prefs, $user;
11	$ratinglib = TikiLib::lib('rating');
12
13	if (! isset($params['type'], $params['id'])) {
14		return tra('No object information provided for rating.');
15	}
16
17	$type = $params['type'];
18	$id = $params['id'];
19	if (isset($params['changemandated']) && $params['changemandated'] == 'y') {
20		$changemandated = true; // needed to fix multiple submission problem in comments
21	} else {
22		$changemandated = false;
23	}
24
25	if (isset($_REQUEST['rating_value'][$type][$id], $_REQUEST['rating_prev'][$type][$id])) {
26		$value = $_REQUEST['rating_value'][$type][$id];
27		$prev = $_REQUEST['rating_prev'][$type][$id];
28		if (( ! $changemandated || $value != $prev ) && $ratinglib->record_vote($type, $id, $value)) {
29			// Handle type-specific actions
30			if ($type == 'comment') {
31				if ($user) {
32					$commentslib = TikiLib::lib('comments');
33					$commentslib->vote_comment($id, $user, $value);
34				}
35			}
36
37			$tikilib = TikiLib::lib('tiki');
38			if ($type == 'comment') {
39				$forum_id = $commentslib->get_comment_forum_id($id);
40				  $forum_info = $commentslib->get_forum($forum_id);
41				  $thread_info = $commentslib->get_comment($id, null, $forum_info);
42				  $item_user = $thread_info['userName'];
43			} elseif ($type == 'article') {
44				  $artlib = TikiLib::lib('art');
45				  $res = $artlib->get_article($id);
46				  $item_user = $res['author'];
47			}
48			if ($value == '1') {
49				TikiLib::events()->trigger(
50					'tiki.social.rating.add',
51					[
52						'type' => $type,
53						'object' => $id,
54						'author' => $item_user,
55						'user' => $user,
56					]
57				);
58			} elseif ($value == '2') {
59				TikiLib::events()->trigger(
60					'tiki.social.rating.remove',
61					[
62						'type' => $type,
63						'object' => $id,
64						'author' => $item_user,
65						'user' => $user,
66					]
67				);
68			}
69		} elseif ($value != $prev) {
70			return tra('An error occurred.');
71		}
72	}
73
74	$vote = $ratinglib->get_vote($type, $id);
75	$options = $ratinglib->get_options($type, $id, false, $hasLabels);
76
77	if ($prefs['rating_smileys'] == 'y') {
78		$smiles = $ratinglib->get_options_smiles($type, $id);
79		$smarty->assign('rating_smiles', $smiles);
80	}
81
82	$smarty->assign('rating_type', $type);
83	$smarty->assign('rating_id', $id);
84	$smarty->assign('rating_options', $options);
85	$smarty->assign('current_rating', $vote);
86	$smarty->assign('rating_has_labels', $hasLabels);
87	return $smarty->fetch('rating.tpl');
88}
89