1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4include_once("./Services/Table/classes/class.ilTable2GUI.php");
5
6/**
7* TableGUI class for poll users
8*
9* @author Jörg Lützenkirchen <luetzenkirchen@leifos.com>
10* @version $Id$
11*
12* @ingroup ModulesPoll
13*/
14class ilPollUserTableGUI extends ilTable2GUI
15{
16    protected $answer_ids; // [array]
17
18    public function __construct($a_parent_obj, $a_parent_cmd)
19    {
20        global $DIC;
21
22        $this->ctrl = $DIC->ctrl();
23        $this->lng = $DIC->language();
24        $ilCtrl = $DIC->ctrl();
25        $lng = $DIC->language();
26
27        $this->setId("ilobjpollusr");
28
29        parent::__construct($a_parent_obj, $a_parent_cmd);
30
31        $this->addColumn($lng->txt("login"), "login");
32        $this->addColumn($lng->txt("lastname"), "lastname");
33        $this->addColumn($lng->txt("firstname"), "firstname");
34
35        foreach ($this->getParentObject()->object->getAnswers() as $answer) {
36            $this->answer_ids[] = $answer["id"];
37            $this->addColumn($answer["answer"], "answer" . $answer["id"]);
38        }
39
40        $this->getItems($this->answer_ids);
41
42        $this->setTitle($this->lng->txt("poll_question") . ": \"" .
43            $this->getParentObject()->object->getQuestion() . "\"");
44
45        $this->setFormAction($ilCtrl->getFormAction($a_parent_obj, $a_parent_cmd));
46        $this->setRowTemplate("tpl.user_row.html", "Modules/Poll");
47        $this->setDefaultOrderField("login");
48        $this->setDefaultOrderDirection("asc");
49
50        $this->setExportFormats(array(self::EXPORT_CSV, self::EXPORT_EXCEL));
51    }
52
53    protected function getItems(array $a_answer_ids)
54    {
55        $data = array();
56
57        foreach ($this->getParentObject()->object->getVotesByUsers() as $user_id => $vote) {
58            $answers = $vote["answers"];
59            unset($vote["answers"]);
60
61            foreach ($a_answer_ids as $answer_id) {
62                $vote["answer" . $answer_id] = in_array($answer_id, $answers);
63            }
64
65            $data[] = $vote;
66        }
67
68        $this->setData($data);
69    }
70
71    protected function fillRow($a_set)
72    {
73        $this->tpl->setCurrentBlock("answer_bl");
74        foreach ($this->answer_ids as $answer_id) {
75            if ($a_set["answer" . $answer_id]) {
76                $this->tpl->setVariable("ANSWER", '<img src="' . ilUtil::getImagePath("icon_ok.svg") . '" />');
77            } else {
78                $this->tpl->setVariable("ANSWER", "&nbsp;");
79            }
80            $this->tpl->parseCurrentBlock();
81        }
82
83        $this->tpl->setVariable("LOGIN", $a_set["login"]);
84        $this->tpl->setVariable("FIRSTNAME", $a_set["firstname"]);
85        $this->tpl->setVariable("LASTNAME", $a_set["lastname"]);
86    }
87
88    protected function fillRowCSV($a_csv, $a_set)
89    {
90        $a_csv->addColumn($a_set["login"]);
91        $a_csv->addColumn($a_set["lastname"]);
92        $a_csv->addColumn($a_set["firstname"]);
93        foreach ($this->answer_ids as $answer_id) {
94            if ($a_set["answer" . $answer_id]) {
95                $a_csv->addColumn(true);
96            } else {
97                $a_csv->addColumn(false);
98            }
99        }
100        $a_csv->addRow();
101    }
102
103    protected function fillRowExcel(ilExcel $a_excel, &$a_row, $a_set)
104    {
105        $a_excel->setCell($a_row, 0, $a_set["login"]);
106        $a_excel->setCell($a_row, 1, $a_set["lastname"]);
107        $a_excel->setCell($a_row, 2, $a_set["firstname"]);
108
109        $col = 2;
110        foreach ($this->answer_ids as $answer_id) {
111            if ($a_set["answer" . $answer_id]) {
112                $a_excel->setCell($a_row, ++$col, true);
113            } else {
114                $a_excel->setCell($a_row, ++$col, false);
115            }
116        }
117    }
118}
119