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 account codes
8*
9* @author Jörg Lützenkirchen <luetzenkirchen@leifos.com>
10* @version $Id$
11*
12* @ilCtrl_Calls ilAccountCodesTableGUI:
13* @ingroup ServicesUser
14*/
15class ilAccountCodesTableGUI extends ilTable2GUI
16{
17    /**
18    * Constructor
19    */
20    public function __construct($a_parent_obj, $a_parent_cmd)
21    {
22        global $DIC;
23
24        $ilCtrl = $DIC['ilCtrl'];
25        $lng = $DIC['lng'];
26
27        $this->setId("user_account_code");
28
29        parent::__construct($a_parent_obj, $a_parent_cmd);
30
31        $this->addColumn("", "", "1", true);
32        $this->addColumn($lng->txt("user_account_code"), "code");
33        $this->addColumn($lng->txt("user_account_code_valid_until"), "valid_until");
34        $this->addColumn($lng->txt("user_account_code_generated"), "generated");
35        $this->addColumn($lng->txt("user_account_code_used"), "used");
36
37        $this->setExternalSorting(true);
38        $this->setExternalSegmentation(true);
39        $this->setEnableHeader(true);
40        $this->setFormAction($ilCtrl->getFormAction($this->parent_obj, "listCodes"));
41        $this->setRowTemplate("tpl.code_list_row.html", "Services/User");
42        $this->setEnableTitle(true);
43        $this->initFilter();
44        $this->setFilterCommand("applyCodesFilter");
45        $this->setResetCommand("resetCodesFilter");
46        $this->setDefaultOrderField("generated");
47        $this->setDefaultOrderDirection("desc");
48
49        $this->setSelectAllCheckbox("id[]");
50        $this->setTopCommands(true);
51        $this->addMultiCommand("deleteConfirmation", $lng->txt("delete"));
52
53        include_once "Services/UIComponent/Button/classes/class.ilSubmitButton.php";
54        $button = ilSubmitButton::getInstance();
55        $button->setCaption("user_account_codes_export");
56        $button->setCommand("exportCodes");
57        $button->setOmitPreventDoubleSubmission(true);
58        $this->addCommandButtonInstance($button);
59
60        $this->getItems();
61    }
62
63    /**
64    * Get user items
65    */
66    public function getItems()
67    {
68        global $DIC;
69
70        $lng = $DIC['lng'];
71        $rbacreview = $DIC['rbacreview'];
72        $ilObjDataCache = $DIC['ilObjDataCache'];
73
74        $this->determineOffsetAndOrder();
75
76        include_once("./Services/User/classes/class.ilAccountCode.php");
77
78        $codes_data = ilAccountCode::getCodesData(
79            ilUtil::stripSlashes($this->getOrderField()),
80            ilUtil::stripSlashes($this->getOrderDirection()),
81            ilUtil::stripSlashes($this->getOffset()),
82            ilUtil::stripSlashes($this->getLimit()),
83            $this->filter["code"],
84            $this->filter["valid_until"],
85            $this->filter["generated"]
86        );
87
88        if (count($codes_data["set"]) == 0 && $this->getOffset() > 0) {
89            $this->resetOffset();
90            $codes_data = ilAccountCode::getCodesData(
91                ilUtil::stripSlashes($this->getOrderField()),
92                ilUtil::stripSlashes($this->getOrderDirection()),
93                ilUtil::stripSlashes($this->getOffset()),
94                ilUtil::stripSlashes($this->getLimit()),
95                $this->filter["code"],
96                $this->filter["valid_until"],
97                $this->filter["generated"]
98            );
99        }
100
101        $result = array();
102        foreach ($codes_data["set"] as $k => $code) {
103            $result[$k]["generated"] = ilDatePresentation::formatDate(new ilDateTime($code["generated"], IL_CAL_UNIX));
104
105            if ($code["used"]) {
106                $result[$k]["used"] = ilDatePresentation::formatDate(new ilDateTime($code["used"], IL_CAL_UNIX));
107            } else {
108                $result[$k]["used"] = "";
109            }
110
111            if ($code["valid_until"] === "0") {
112                $valid = $lng->txt("user_account_code_valid_until_unlimited");
113            } elseif (is_numeric($code["valid_until"])) {
114                $valid = $code["valid_until"] . " " . ($code["valid_until"] == 1 ? $lng->txt("day") : $lng->txt("days"));
115            } else {
116                $valid = ilDatePresentation::formatDate(new ilDate($code["valid_until"], IL_CAL_DATE));
117            }
118            $result[$k]["valid_until"] = $valid;
119
120            $result[$k]["code"] = $code["code"];
121            $result[$k]["code_id"] = $code["code_id"];
122        }
123
124        $this->setMaxCount($codes_data["cnt"]);
125        $this->setData($result);
126    }
127
128
129    /**
130    * Init filter
131    */
132    public function initFilter()
133    {
134        global $DIC;
135
136        $lng = $DIC['lng'];
137        $rbacreview = $DIC['rbacreview'];
138        $ilUser = $DIC['ilUser'];
139
140        include_once("./Services/User/classes/class.ilAccountCode.php");
141
142        // code
143        include_once("./Services/Form/classes/class.ilTextInputGUI.php");
144        $ti = new ilTextInputGUI($lng->txt("user_account_code"), "query");
145        $ti->setMaxLength(ilAccountCode::CODE_LENGTH);
146        $ti->setSize(20);
147        $ti->setSubmitFormOnEnter(true);
148        $this->addFilterItem($ti);
149        $ti->readFromSession();
150        $this->filter["code"] = $ti->getValue();
151
152        // generated
153        include_once("./Services/Form/classes/class.ilSelectInputGUI.php");
154        $options = array("" => $lng->txt("user_account_code_generated_all"));
155        foreach ((array) ilAccountCode::getGenerationDates() as $date) {
156            $options[$date] = ilDatePresentation::formatDate(new ilDateTime($date, IL_CAL_UNIX));
157        }
158        $si = new ilSelectInputGUI($lng->txt("user_account_code_generated"), "generated");
159        $si->setOptions($options);
160        $this->addFilterItem($si);
161        $si->readFromSession();
162        $this->filter["generated"] = $si->getValue();
163    }
164
165    /**
166    * Fill table row
167    */
168    protected function fillRow($code)
169    {
170        $this->tpl->setVariable("ID", $code["code_id"]);
171        $this->tpl->setVariable("VAL_CODE", $code["code"]);
172        $this->tpl->setVariable("VAL_VALID_UNTIL", $code["valid_until"]);
173        $this->tpl->setVariable("VAL_GENERATED", $code["generated"]);
174        $this->tpl->setVariable("VAL_USED", $code["used"]);
175    }
176}
177