1: <?php
2: /*
3: You may not change or alter any portion of this comment or credits
4: of supporting developers from this source code or any supporting source code
5: which is considered copyrighted (c) material of the original comment or credit authors.
6:
7: This program is distributed in the hope that it will be useful,
8: but WITHOUT ANY WARRANTY; without even the implied warranty of
9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: use Xoops\Core\Kernel\Handlers\XoopsUser;
13: use Xoops\Core\Service\AbstractContract;
14: use Xoops\Core\Service\Contract\UserRankInterface;
15: use Xoops\Core\Service\Response;
16:
17: /**
18: * UserRank provider for service manager
19: *
20: * @category class
21: * @package UserRankProvider
22: * @author Richard Griffith <richard@geekwright.com>
23: * @copyright 2013-2015 XOOPS Project (http://xoops.org)
24: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
25: * @link http://xoops.org
26: * @since 2.6.0
27: */
28: class UserRankProvider extends AbstractContract implements UserRankInterface
29: {
30: /**
31: * getName - get a short name for this service provider. This should be unique within the
32: * scope of the named service, so using module dirname is suggested.
33: *
34: * @return string - a unique name for the service provider
35: */
36: public function getName()
37: {
38: return 'userrank';
39: }
40:
41: /**
42: * getDescription - get human readable description of the service provider
43: *
44: * @return string
45: */
46: public function getDescription()
47: {
48: return 'Traditional XOOPS User Ranks.';
49: }
50:
51: /**
52: * getUserRank - given user info return array of rank information for the user
53: *
54: * @param Response $response \Xoops\Core\Service\Response object
55: * @param mixed $userinfo Xoops\Core\Kernel\Handlers\XoopsUser object for user (preferred) or
56: * array of user info,
57: * 'uid' => (int) id of system user
58: * 'posts' => (int) contribution count associated with the user
59: * 'rank' => (int) id of manually assigned rank, 0 if none assigned
60: *
61: * @return void - $response->value set to array of rank information
62: * 'title' => string that describes the rank
63: * 'image' => url of image associated with the rank
64: */
65: public function getUserRank(Response $response, $userinfo)
66: {
67: $uid = isset($userinfo['uid']) ? (int) $userinfo['uid'] : null;
68: $posts = isset($userinfo['posts']) ? (int) $userinfo['posts'] : null;
69: $rank = isset($userinfo['rank']) ? (int) $userinfo['rank'] : null;
70: if ($uid === null || $posts === null || $rank === null) {
71: $response->setSuccess(false)->addErrorMessage('User info is invalid');
72: return;
73: }
74:
75: $myts = \Xoops\Core\Text\Sanitizer::getInstance();
76: $db = \Xoops::getInstance()->db();
77: $qb = $db->createXoopsQueryBuilder();
78: $eb = $qb->expr();
79: $qb ->select('r.rank_title AS title')
80: ->addSelect('r.rank_image AS image')
81: ->fromPrefix('userrank_rank', 'r');
82: if ($rank != 0) {
83: $qb->where($eb->eq('r.rank_id', ':rank'))
84: ->setParameter(':rank', $rank, \PDO::PARAM_INT);
85: } else {
86: $qb->where($eb->lte('r.rank_min', ':posts'))
87: ->andWhere($eb->gte('r.rank_max', ':posts'))
88: ->andWhere($eb->eq('r.rank_special', 0))
89: ->setParameter(':posts', $posts, \PDO::PARAM_INT);
90: }
91: $result = $qb->execute();
92: $rank = $result->fetch(\PDO::FETCH_ASSOC);
93:
94: $rank['title'] = isset($rank['title']) ? $myts->htmlSpecialChars($rank['title']) : '';
95: $rank['image'] = \XoopsBaseConfig::get('uploads-url') .
96: (isset($rank['image']) ? '/' . $rank['image'] : '/blank.gif');
97:
98: $response->setValue($rank);
99: }
100:
101: /**
102: * getAssignableUserRankList - return a list of ranks that can be assigned
103: *
104: * @param Response $response \Xoops\Core\Service\Response object
105: *
106: * @return void - response->value set to array of (int) id => (string) rank title
107: * entries of assignable ranks
108: */
109: public function getAssignableUserRankList(Response $response)
110: {
111: $db = \Xoops::getInstance()->db();
112: $myts = \Xoops\Core\Text\Sanitizer::getInstance();
113:
114: $ret = array();
115:
116: $sql = $db->createXoopsQueryBuilder();
117: $eb = $sql->expr();
118: $sql->select('rank_id')
119: ->addSelect('rank_title')
120: ->fromPrefix('userrank_rank', 'r')
121: ->where($eb->eq('rank_special', ':rankspecial'))
122: ->orderBy('rank_title')
123: ->setParameter(':rankspecial', 1);
124:
125: $result = $sql->execute();
126: while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) {
127: $ret[$myrow['rank_id']] = $myts->htmlSpecialChars($myrow['rank_title']);
128: }
129: $response->setValue($ret);
130: }
131: }
132: