Issues (1963)

html/user/team_search.php (4 issues)

1
<?php
2
// This file is part of BOINC.
3
// http://boinc.berkeley.edu
4
// Copyright (C) 2014 University of California
5
//
6
// BOINC is free software; you can redistribute it and/or modify it
7
// under the terms of the GNU Lesser General Public License
8
// as published by the Free Software Foundation,
9
// either version 3 of the License, or (at your option) any later version.
10
//
11
// BOINC is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
// See the GNU Lesser General Public License for more details.
15
//
16
// You should have received a copy of the GNU Lesser General Public License
17
// along with BOINC.  If not, see <http://www.gnu.org/licenses/>.
18
19
include_once("../inc/boinc_db.inc");
20
include_once("../inc/util.inc");
21
include_once("../inc/team.inc");
22
include_once("../inc/team_types.inc");
23
include_once("../inc/xml.inc");
24
25
if (DISABLE_TEAMS) error_page("Teams are disabled");
26
27
check_get_args(array("keywords", "active", "country", "type", "submit", "xml"));
28
29
// Merge list1 into list2.
30
// list entries are of the form id => team,
31
// where team includes a field "refcnt".
32
//
33
function merge_lists($list1, &$list2, $weight) {
34
    foreach($list1 as $team) {
35
        $id = $team->id;
36
        if (array_key_exists($id, $list2)) {
37
            $list2[$id]->refcnt += $weight;
38
        } else {
39
            $list2[$id] = $team;
40
            $list2[$id]->refcnt = $weight;
41
        }
42
    }
43
}
44
45
function compare_teams($t1, $t2) {
46
    if ($t1->refcnt > $t2->refcnt) return -1;
47
    if ($t1->refcnt < $t2->refcnt) return 1;
48
    if ($t1->rnd > $t2->rnd) return -1;
49
    if ($t1->rnd < $t2->rnd) return 1;
50
    return 0;
51
}
52
53
// Sort list by decreasing refcnt
54
//
55
function sort_list(&$list) {
56
    foreach ($list as $a=>$b) {
0 ignored issues
show
Expected 1 space before "=>"; 0 found
Loading history...
Expected 1 space after "=>"; 0 found
Loading history...
57
        $b->rnd = rand();
58
    }
59
    usort($list, 'compare_teams');
60
}
61
62
function get_teams($clause, $active) {
63
    $c2 = '';
64
    if ($active) $c2 = "and expavg_credit>0.1";
65
    $x = BoincTeam::enum("$clause $c2 order by expavg_credit desc limit 20");
66
    foreach ($x as $t) {
67
        $t->refcnt = 0;
68
    }
69
    return $x;
70
}
71
72
function show_list($list) {
73
    start_table('table-striped');
74
    $x = array();
75
    $a = array();
76
    $x[] = tra("Team name");
77
    $a[] = null;
78
    if (defined("SHOW_NONVALIDATED_TEAMS")) {
79
        $x[] = tra("Validated?");
80
        $a[] = null;
81
    }
82
    $x[] = tra("Description");
83
    $a[] = null;
84
    $x[] = tra("Average credit");
85
    $a[] = ALIGN_RIGHT;
86
    $x[] = tra("Type");
87
    $a[] = null;
88
    $x[] = tra("Country");
89
    $a[] = null;
90
    row_heading_array($x, $a);
91
92
    foreach ($list as $team) {
93
        $type = team_type_name($team->type);
94
        echo "<tr>
95
            <td><a href=team_display.php?teamid=$team->id>$team->name</a></td>
96
        ";
97
        if (defined("SHOW_NONVALIDATED_TEAMS")) {
98
            $user = BoincUser::lookup_id($team->userid);
99
            echo "<td>";
100
            echo $user->email_validated?"Yes":"No";
101
            echo "</td>\n";
102
        }
103
        echo "
104
            <td><p class=\"text-muted\">".sanitize_html($team->description)."</p></td>
105
            <td align=right>".format_credit($team->expavg_credit)."</td>
106
            <td>$type</td>
107
            <td>$team->country</td>
108
            </tr>
109
        ";
110
    }
111
    echo "</table>";
112
}
113
114
function show_teams_html($list, $params) {
115
    page_head(tra("Team search results"));
116
    if (sizeof($list) == 0) {
117
        echo tra("No teams were found matching your criteria. Try another search.")
118
            ."<p>"
119
            .tra("Or you can %1 create a new team %2.", "<a href=team_create_form.php>", "</a>")
120
            ."</p>\n";
121
        team_search_form($params);
122
    } else {
123
        echo tra("The following teams match one or more of your search criteria.
124
            To join a team, click its name to go to the team page,
125
               then click %1 Join this team %2.", "<strong>", "</strong>")
126
            ."<p>
127
        ";
128
        sort_list($list);
129
        show_list($list);
0 ignored issues
show
The call to show_list() has too few arguments starting with name. ( Ignorable by Annotation )

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

129
        /** @scrutinizer ignore-call */ 
130
        show_list($list);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
130
        echo "<h2>".tra("Change your search")."</h2>";
131
        team_search_form($params);
132
    }
133
    page_tail();
134
}
135
136
function show_teams_xml($list) {
137
    xml_header();
138
    echo "<teams>\n";
139
    sort_list($list);
140
    foreach($list as $team) {
141
        show_team_xml($team);
142
    }
143
    echo "</teams>\n";
144
}
145
146
function search($params) {
147
    $list = array();
148
    $tried = false;
149
    if (strlen($params->keywords)) {
150
        $kw = BoincDb::escape_string($params->keywords);
151
        $name_lc = strtolower($kw);
152
153
        $list2 = get_teams("name='$name_lc'", $params->active);
154
        merge_lists($list2, $list, 20);
155
156
        $name_lc = escape_pattern($name_lc);
157
        $list2 = get_teams("name like '".$name_lc."%'", $params->active);
158
        //echo "<br>name like matches: ",sizeof($list2);
159
        merge_lists($list2, $list, 5);
160
161
        $list2 = get_teams(
162
            "match(name, description) against ('$kw')", $params->active
163
        );
164
        //echo "<br>keyword matches: ",sizeof($list2);
165
        merge_lists($list2, $list, 3);
166
        $tried = true;
167
    }
168
    if (strlen($params->country) && $params->country!='None') {
169
        $country = BoincDb::escape_string($params->country);
170
        $list2 = get_teams("country = '$country'", $params->active);
171
        //echo "<br>country matches: ",sizeof($list2);
172
        merge_lists($list2, $list, 1);
173
        $tried = true;
174
    }
175
    if ($params->type and $params->type>1) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
176
        $list2 = get_teams("type=$params->type", $params->active);
177
        //echo "<br>type matches: ",sizeof($list2);
178
        merge_lists($list2, $list, 2);
179
        $tried = true;
180
    }
181
    if (!$tried) {
182
        $list = get_teams("id>0", $params->active);
183
    }
184
185
    return $list;
186
}
187
188
$user = get_logged_in_user(false);
189
$submit = get_str("submit", true);
190
$xml = get_str("xml", true);
191
if ($submit || $xml) {
192
    $params = new StdClass;
193
    $params->keywords = get_str('keywords', true);
194
    $params->country = get_str("country", true);
195
    $params->type = get_int("type", true);
196
    $params->active = get_str('active', true);
197
    $list = search($params);
198
    if ($xml) {
199
        show_teams_xml($list);
200
    } else {
201
        show_teams_html($list, $params);
202
    }
203
} else {
204
    page_head(tra("Find a team"), 'onload="document.form.keywords.focus()"');
205
    echo tra("You can team up with other people with similar interests, or from the same country, company, or school.")
206
        ."<p>"
207
        .tra("Use this form to find teams that might be right for you.")
208
        ."</p>\n";
209
    team_search_form(null);
210
    if (isset($_COOKIE['init'])) {
211
        echo "<p>
212
            ".tra("%1 I'm not interested %2 in joining a team right now.",
213
                sprintf('<a href="%s">', HOME_PAGE),
214
                "</a>"
215
            );
216
    }
217
    page_tail();
218
}
219
220
?>
221