Issues (1369)

includes/functions/_news.php (13 issues)

1
<?php
2
3
use DBAL\db_mysql;
4
use DBAL\DbSqlPaging;
5
use General\Helpers\PagingRenderer;
6
7
/**
8
 * @param template $template
9
 * @param string   $query_where
10
 * @param int      $query_limit
11
 */
12
function nws_render(&$user, &$template, $query_where = '', $query_limit = 20) {
13
  if($user['authlevel'] < AUTH_LEVEL_ADMINISTRATOR) {
14
    $query_where .= (!empty($query_where) ? ' AND ' : '')
15
      . '`tsTimeStamp` <= (NOW())';
16
  }
17
18
  $mmModuleIsActive = !empty(SN::$gc->modules->getModulesInGroup('payment'));
19
20
  $sqlText = "SELECT a.*, UNIX_TIMESTAMP(`tsTimeStamp`) AS unix_time, u.authlevel, s.*
21
    FROM
22
      `{{announce}}` AS a
23
      LEFT JOIN `{{survey}}` AS s ON s.survey_announce_id = a.idAnnounce
24
      LEFT JOIN `{{users}}` AS u ON u.id = a.user_id
25
    WHERE 1 {$query_where}
26
    ORDER BY `tsTimeStamp` DESC, idAnnounce";
27
28
  $announce_list = new DbSqlPaging($sqlText, $query_limit, sys_get_param_int(PagingRenderer::KEYWORD));
29
  $pager = new PagingRenderer($announce_list, 'announce.php');
30
31
  $users = array();
32
  foreach ($announce_list as $announce) {
33
    if ($announce['user_id'] && !isset($users[$announce['user_id']])) {
34
      $users[$announce['user_id']] = db_user_by_id($announce['user_id']);
0 ignored issues
show
Deprecated Code introduced by
The function db_user_by_id() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

34
      $users[$announce['user_id']] = /** @scrutinizer ignore-deprecated */ db_user_by_id($announce['user_id']);
Loading history...
35
    }
36
37
    $survey_vote = array('survey_vote_id' => 1);
38
    $survey_complete = strtotime($announce['survey_until']) < SN_TIME_NOW;
39
40
    if ($announce['survey_id'] && !empty($user['id'])) {
41
      $survey_vote = !$survey_complete ? $survey_vote = doquery("SELECT `survey_vote_id` FROM `{{survey_votes}}` WHERE survey_parent_id = {$announce['survey_id']} AND survey_vote_user_id = {$user['id']} LIMIT 1;", true) : array();
0 ignored issues
show
true of type true is incompatible with the type string expected by parameter $table of doquery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
      $survey_vote = !$survey_complete ? $survey_vote = doquery("SELECT `survey_vote_id` FROM `{{survey_votes}}` WHERE survey_parent_id = {$announce['survey_id']} AND survey_vote_user_id = {$user['id']} LIMIT 1;", /** @scrutinizer ignore-type */ true) : array();
Loading history...
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

41
      $survey_vote = !$survey_complete ? $survey_vote = /** @scrutinizer ignore-deprecated */ doquery("SELECT `survey_vote_id` FROM `{{survey_votes}}` WHERE survey_parent_id = {$announce['survey_id']} AND survey_vote_user_id = {$user['id']} LIMIT 1;", true) : array();
Loading history...
The assignment to $survey_vote is dead and can be removed.
Loading history...
42
    }
43
44
    $announce_exploded = explode("<br /><br />", SN::$gc->bbCodeParser->expandBbCode($announce['strAnnounce'], intval($announce['authlevel'])));
45
46
    $template->assign_block_vars('announces', array(
47
      'ID'              => $announce['idAnnounce'],
48
      'TIME'            => date(FMT_DATE_TIME, $announce['unix_time'] + SN_CLIENT_TIME_DIFF),
49
      'ANNOUNCE'        => SN::$gc->bbCodeParser->expandBbCode($announce['strAnnounce'], intval($announce['authlevel'])),
50
      'DETAIL_URL'      => $announce['detail_url'],
51
      'USER_NAME'       => !empty($users[$announce['user_id']])
52
        ? player_nick_render_to_html($users[$announce['user_id']], array('color' => true))
53
        : js_safe_string($announce['user_name']),
54
      'NEW'             => $announce['unix_time'] + SN::$config->game_news_actual >= SN_TIME_NOW,
55
      'FUTURE'          => $announce['unix_time'] > SN_TIME_NOW,
56
      'SURVEY_ID'       => $announce['survey_id'],
57
      'SURVEY_TEXT'     => $announce['survey_question'],
58
      'SURVEY_CAN_VOTE' => empty($survey_vote) && !$survey_complete,
59
      'SURVEY_COMPLETE' => $survey_complete,
60
      'SURVEY_UNTIL'    => $announce['survey_until'],
61
    ));
62
63
    foreach ($announce_exploded as $announce_paragraph) {
64
      $template->assign_block_vars('announces.paragraph', array(
65
        'TEXT' => $announce_paragraph,
66
      ));
67
    }
68
69
    if ($announce['survey_id']) {
70
      $survey_query = doquery(
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

70
      $survey_query = /** @scrutinizer ignore-deprecated */ doquery(
Loading history...
71
        "SELECT survey_answer_id AS `ID`, survey_answer_text AS `TEXT`, count(DISTINCT survey_vote_id) AS `VOTES`
72
          FROM `{{survey_answers}}` AS sa
73
            LEFT JOIN `{{survey_votes}}` AS sv ON sv.survey_parent_answer_id = sa.survey_answer_id
74
          WHERE sa.survey_parent_id = {$announce['survey_id']}
75
          GROUP BY survey_answer_id
76
          ORDER BY survey_answer_id;"
77
      );
78
      $survey_vote_result = array();
79
      $total_votes = 0;
80
      $total_mm = 0;
81
      $total_money = 0;
82
      while ($row = db_fetch($survey_query)) {
0 ignored issues
show
Deprecated Code introduced by
The function db_fetch() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

82
      while ($row = /** @scrutinizer ignore-deprecated */ db_fetch($survey_query)) {
Loading history...
83
        $survey_vote_result[$row['ID']] = $row;
84
        $total_votes += $row['VOTES'];
85
      }
86
87
      if ($mmModuleIsActive && $user['authlevel'] >= AUTH_LEVEL_ADMINISTRATOR) {
88
        $mQuery = doquery(
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

88
        $mQuery = /** @scrutinizer ignore-deprecated */ doquery(
Loading history...
89
          "SELECT
90
            sa.survey_answer_id,
91
            sum(acc.account_metamatter_total) AS `mm`,
92
            (
93
              SELECT sum(payment_amount)
94
              FROM `{{payment}}` AS pay
95
              WHERE payment_currency = 'USD' AND pay.payment_user_id = sv.survey_vote_user_id
96
              GROUP BY payment_user_id, payment_currency
97
            )                                 AS `money`
98
          FROM `{{survey_votes}}` AS sv
99
            LEFT JOIN `{{survey_answers}}` AS sa ON sa.survey_answer_id = sv.survey_parent_answer_id
100
            LEFT JOIN `{{account_translate}}` AS act ON act.user_id = sv.survey_vote_user_id
101
            LEFT JOIN `{{account}}` AS acc ON acc.account_id = act.provider_account_id
102
          WHERE sv.survey_parent_id = {$announce['survey_id']}
103
          GROUP BY sv.survey_parent_id, sv.survey_parent_answer_id;"
104
        );
105
        while ($row = db_fetch($mQuery)) {
0 ignored issues
show
Deprecated Code introduced by
The function db_fetch() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

105
        while ($row = /** @scrutinizer ignore-deprecated */ db_fetch($mQuery)) {
Loading history...
106
          $survey_vote_result[$row['survey_answer_id']] += [
107
            'MM'    => $row['mm'],
108
            'MONEY' => $row['money'],
109
          ];
110
          $total_mm += $row['mm'];
111
          $total_money += $row['money'];
112
        }
113
      }
114
115
      // Show result
116
      foreach ($survey_vote_result as &$vote_result) {
117
        $vote_percent = $total_votes ? $vote_result['VOTES'] / $total_votes * 100 : 0;
118
        $vote_result['PERCENT'] = $vote_percent;
119
        $vote_result['PERCENT_TEXT'] = round($vote_percent, 1);
120
        $vote_result['VOTES'] = HelperString::numberFloorAndFormat($vote_result['VOTES']);
121
122
        if ($mmModuleIsActive && $user['authlevel'] >= AUTH_LEVEL_ADMINISTRATOR) {
123
          $vote_result['PERCENT_MM'] = $total_mm ? $vote_result['MM'] / $total_mm * 100 : 0;
124
          $vote_result['PERCENT_MONEY'] = $total_money ? $vote_result['MONEY'] / $total_money * 100 : 0;
125
        }
126
127
        $template->assign_block_vars('announces.survey_answers', $vote_result);
128
      }
129
      // Dirty hack
130
      $template->assign_block_vars('announces.total_votes', array(
131
        'TOTAL_VOTES' => $total_votes,
132
        'TOTAL_MM'    => $total_mm,
133
        'TOTAL_MONEY' => $total_money,
134
      ));
135
    }
136
  }
137
138
  $template->assign_vars([
139
    'PAGER_MESSAGES' => $pager ? $pager->render() : '',
0 ignored issues
show
$pager is of type General\Helpers\PagingRenderer, thus it always evaluated to true.
Loading history...
140
    'NEWS_COUNT'     => HelperString::numberFloorAndFormat($announce_list->getTotalRecords()),
141
142
    'MM_MODULE_ACTIVE' => $mmModuleIsActive,
143
  ]);
144
}
145
146
function nws_mark_read(&$user) {
147
  if (!empty($user['id'])) {
148
    db_user_set_by_id($user['id'], '`news_lastread` = ' . SN_TIME_NOW);
0 ignored issues
show
Deprecated Code introduced by
The function db_user_set_by_id() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

148
    /** @scrutinizer ignore-deprecated */ db_user_set_by_id($user['id'], '`news_lastread` = ' . SN_TIME_NOW);
Loading history...
149
    $user['news_lastread'] = SN_TIME_NOW;
150
  }
151
152
  return true;
153
}
154
155
function survey_vote(&$user) {
156
  if (empty($user['id'])) {
157
    return true;
158
  }
159
160
  db_mysql::db_transaction_start();
161
  $survey_id = sys_get_param_id('survey_id');
162
  $is_voted = doquery("SELECT `survey_vote_id` FROM `{{survey_votes}}` WHERE survey_parent_id = {$survey_id} AND survey_vote_user_id = {$user['id']} FOR UPDATE;", true);
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

162
  $is_voted = /** @scrutinizer ignore-deprecated */ doquery("SELECT `survey_vote_id` FROM `{{survey_votes}}` WHERE survey_parent_id = {$survey_id} AND survey_vote_user_id = {$user['id']} FOR UPDATE;", true);
Loading history...
163
  if (empty($is_voted)) {
164
    $survey_vote_id = sys_get_param_id('survey_vote');
165
    $is_answer_exists = doquery("SELECT `survey_answer_id` FROM `{{survey_answers}}` WHERE survey_parent_id = {$survey_id} AND survey_answer_id = {$survey_vote_id};", true);
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

165
    $is_answer_exists = /** @scrutinizer ignore-deprecated */ doquery("SELECT `survey_answer_id` FROM `{{survey_answers}}` WHERE survey_parent_id = {$survey_id} AND survey_answer_id = {$survey_vote_id};", true);
Loading history...
166
    if (!empty($is_answer_exists)) {
167
      $user_name_safe = SN::$db->db_escape($user['username']);
168
      doquery("INSERT INTO `{{survey_votes}}` SET `survey_parent_id` = {$survey_id}, `survey_parent_answer_id` = {$survey_vote_id}, `survey_vote_user_id` = {$user['id']}, `survey_vote_user_name` = '{$user_name_safe}';");
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

168
      /** @scrutinizer ignore-deprecated */ doquery("INSERT INTO `{{survey_votes}}` SET `survey_parent_id` = {$survey_id}, `survey_parent_answer_id` = {$survey_vote_id}, `survey_vote_user_id` = {$user['id']}, `survey_vote_user_name` = '{$user_name_safe}';");
Loading history...
169
    }
170
  }
171
  db_mysql::db_transaction_commit();
172
173
  return true;
174
}
175