1<?php
2// listactions/la_get_revpref.php -- HotCRP helper classes for list actions
3// Copyright (c) 2006-2018 Eddie Kohler; see LICENSE.
4
5class GetRevpref_ListAction extends ListAction {
6    private $extended;
7    function __construct($conf, $fj) {
8        $this->extended = $fj->name === "get/revprefx";
9    }
10    function allow(Contact $user) {
11        return $user->isPC;
12    }
13    static function render_upload(PaperList $pl) {
14        return ["<b>&nbsp;preference file:</b> &nbsp;"
15                . "<input class=\"want-focus js-autosubmit\" type='file' name='uploadedFile' accept='text/plain' size='20' data-autosubmit-type=\"uploadpref\" />&nbsp; "
16                . Ht::submit("fn", "Go", ["value" => "uploadpref", "data-default-submit-all" => 1, "class" => "btn uix js-submit-mark"])];
17    }
18    static function render_set(PaperList $pl) {
19        return [Ht::entry("pref", "", array("class" => "want-focus js-autosubmit", "size" => 4, "data-autosubmit-type" => "setpref"))
20                . " &nbsp;" . Ht::submit("fn", "Go", ["value" => "setpref", "class" => "btn uix js-submit-mark"])];
21    }
22    function run(Contact $user, $qreq, $ssel) {
23        // maybe download preferences for someone else
24        $Rev = $user;
25        if ($qreq->reviewer) {
26            $Rev = null;
27            foreach ($user->conf->pc_members() as $pcm)
28                if (strcasecmp($pcm->email, $qreq->reviewer) == 0
29                    || (string) $pcm->contactId === $qreq->reviewer) {
30                    $Rev = $pcm;
31                    break;
32                }
33            if (!$Rev)
34                return Conf::msg_error("No such reviewer");
35        }
36        if (!$Rev->isPC)
37            return self::EPERM;
38
39        $not_me = $user->contactId !== $Rev->contactId;
40        $has_conflict = false;
41        $texts = array();
42        foreach ($user->paper_set($ssel, ["topics" => 1, "reviewerPreference" => 1]) as $prow) {
43            if ($not_me && !$user->allow_administer($prow))
44                continue;
45            $item = ["paper" => $prow->paperId, "title" => $prow->title];
46            if ($not_me)
47                $item["email"] = $Rev->email;
48            $item["preference"] = unparse_preference($prow->reviewer_preference($Rev));
49            if ($prow->has_conflict($Rev)) {
50                $item["notes"] = "conflict";
51                $has_conflict = true;
52            }
53            if ($this->extended) {
54                $x = "";
55                if ($Rev->can_view_authors($prow, false))
56                    $x .= prefix_word_wrap(" Authors: ", $prow->pretty_text_author_list(), "          ");
57                $x .= prefix_word_wrap("Abstract: ", rtrim($prow->abstract), "          ");
58                if ($prow->topicIds != "")
59                    $x .= prefix_word_wrap("  Topics: ", $prow->unparse_topics_text(), "          ");
60                $item["__postcomment__"] = $x;
61            }
62            $texts[$prow->paperId][] = $item;
63        }
64        $fields = array_merge(["paper", "title"], $not_me ? ["email"] : [], ["preference"], $has_conflict ? ["notes"] : []);
65        $title = "revprefs";
66        if ($not_me)
67            $title .= "-" . (preg_replace('/@.*|[^\w@.]/', "", $Rev->email) ? : "user");
68        return $user->conf->make_csvg($title, CsvGenerator::FLAG_ITEM_COMMENTS)
69            ->select($fields)->add($ssel->reorder($texts));
70    }
71}
72