1<?php
2// pc_tagreport.php -- HotCRP helper classes for paper list content
3// Copyright (c) 2006-2018 Eddie Kohler; see LICENSE.
4
5class TagReport_PaperColumn extends PaperColumn {
6    private $tag;
7    private $viewtype;
8    function __construct(Conf $conf, $cj) {
9        parent::__construct($conf, $cj);
10        $this->override = PaperColumn::OVERRIDE_FOLD_IFEMPTY;
11        $this->tag = $cj->tag;
12    }
13    function prepare(PaperList $pl, $visible) {
14        if (!$pl->user->can_view_any_peruser_tags($this->tag))
15            return false;
16        if ($visible)
17            $pl->qopts["tags"] = 1;
18        $dt = $pl->conf->tags()->check($this->tag);
19        if (!$dt || $dt->rank || (!$dt->vote && !$dt->approval))
20            $this->viewtype = 0;
21        else
22            $this->viewtype = $dt->approval ? 1 : 2;
23        return true;
24    }
25    function header(PaperList $pl, $is_text) {
26        return "#~" . $this->tag . " report";
27    }
28    function content_empty(PaperList $pl, PaperInfo $row) {
29        return !$pl->user->can_view_peruser_tags($row, $this->tag);
30    }
31    function content(PaperList $pl, PaperInfo $row) {
32        $a = [];
33        preg_match_all('/ (\d+)~' . preg_quote($this->tag) . '#(\S+)/i', $row->all_tags_text(), $m);
34        for ($i = 0; $i != count($m[0]); ++$i) {
35            if ($this->viewtype == 2 && $m[2][$i] <= 0)
36                continue;
37            $n = $pl->user->name_html_for($m[1][$i]);
38            if ($this->viewtype != 1)
39                $n .= " (" . $m[2][$i] . ")";
40            $a[$m[1][$i]] = $n;
41        }
42        if (empty($a))
43            return "";
44        $pl->user->ksort_cid_array($a);
45        return '<span class="nb">' . join(',</span> <span class="nb">', $a) . '</span>';
46    }
47}
48
49class TagReport_PaperColumnFactory {
50    static private function column_json($xfj, $tag) {
51        $cj = (array) $xfj;
52        $cj["name"] = "tagreport:" . strtolower($tag);
53        $cj["tag"] = $tag;
54        return (object) $cj;
55    }
56    static function expand($name, Conf $conf, $xfj, $m) {
57        if (!$conf->xt_user->can_view_most_tags())
58            return null;
59        $tagset = $conf->tags();
60        if ($name === "tagreports") {
61            $conf->xt_factory_mark_matched();
62            return array_map(function ($t) use ($xfj) {
63                return self::column_json($xfj, $t->tag);
64            }, $tagset->filter_by(function ($t) {
65                return $t->vote || $t->approval || $t->rank;
66            }));
67        } else {
68            $t = $tagset->check($m[1]);
69            if ($t && ($t->vote || $t->approval || $t->rank))
70                return self::column_json($xfj, $m[1]);
71            else
72                return null;
73        }
74    }
75}
76