1<?php
2// search/st_conflict.php -- HotCRP helper class for searching for papers
3// Copyright (c) 2006-2018 Eddie Kohler; see LICENSE.
4
5class Conflict_SearchTerm extends SearchTerm {
6    private $csm;
7
8    function __construct($countexpr, $contacts, Contact $user) {
9        parent::__construct("conflict");
10        $this->csm = new ContactCountMatcher($countexpr, $contacts);
11    }
12    static function parse($word, SearchWord $sword, PaperSearch $srch) {
13        $m = PaperSearch::unpack_comparison($word, $sword->quoted);
14        if (($qr = PaperSearch::check_tautology($m[1])))
15            return $qr;
16        else {
17            $contacts = $srch->matching_users($m[0], $sword->quoted, $sword->kwdef->pc_only);
18            return new Conflict_SearchTerm($m[1], $contacts, $srch->user);
19        }
20    }
21    function trivial_rights(Contact $user, PaperSearch $srch) {
22        return $this->csm->has_sole_contact($user->contactId);
23    }
24    function sqlexpr(SearchQueryInfo $sqi) {
25        $thistab = "Conflict_" . count($sqi->tables);
26        $where = $this->csm->contact_match_sql("$thistab.contactId");
27
28        $compar = $this->csm->simplified_nonnegative_countexpr();
29        if ($compar !== ">0" && $compar !== "=0") {
30            $sqi->add_table($thistab, ["left join", "(select paperId, count(*) ct from PaperConflict $thistab where $where group by paperId)"]);
31            return "coalesce($thistab.ct,0)$compar";
32        } else {
33            $sqi->add_table($thistab, ["left join", "PaperConflict", $where]);
34            if ($compar === "=0")
35                return "$thistab.contactId is null";
36            else
37                return "$thistab.contactId is not null";
38        }
39    }
40    function exec(PaperInfo $row, PaperSearch $srch) {
41        $can_view = $srch->user->can_view_conflicts($row);
42        $n = 0;
43        foreach ($this->csm->contact_set() as $cid) {
44            if (($cid == $srch->cid || $can_view)
45                && $row->has_conflict($cid))
46                ++$n;
47        }
48        return $this->csm->test($n);
49    }
50}
51