1<?php
2include_once('./Services/Table/classes/class.ilTable2GUI.php');
3include_once('./Services/History/classes/class.ilHistory.php');
4include_once('./Services/User/classes/class.ilUserUtil.php');
5/**
6 * Copyright (c) 1998-2014 ILIAS open source, Extended GPL, see docs/LICENSE
7 * Date: 24.10.14
8 * Time: 10:35
9 */
10/**
11* Lists History entrys in chronological order
12*
13* @author Fabian Wolf <wolf@leifos.com>
14* @version $Id$
15*
16* @ingroup ModuleHistory
17*/
18class ilHistoryTableGUI extends ilTable2GUI
19{
20    protected $obj_id;
21    protected $obj_type;
22    protected $ref_id;
23    protected $ilCtrl;
24
25    protected $comment_visibility = false;
26
27
28    public function __construct($a_parent_obj, $a_parent_cmd, $a_obj_id, $a_obj_type = null)
29    {
30        global $DIC;
31
32        $ilCtrl = $DIC->ctrl();
33        parent::__construct($a_parent_obj, $a_parent_cmd);
34
35        $this->setObjId($a_obj_id);
36        $this->setObjType($a_obj_type);
37        $this->ilCtrl = $ilCtrl;
38    }
39
40    /**
41    * Get data and put it into an array
42    */
43    public function getDataFromDb()
44    {
45        $entries = ilHistory::_getEntriesForObject($this->getObjId(), $this->getObjType());
46        $this->setData($entries);
47    }
48
49    /**
50     * init table
51     */
52    public function initTable()
53    {
54        $this->setRowTemplate("tpl.history_row.html", "Services/History");
55        $this->setFormAction($this->ilCtrl->getFormAction($this->getParentObject()));
56
57        $this->setTitle($this->lng->txt("history"));
58        $this->addColumn($this->lng->txt("user"), "", "25%");
59        $this->addColumn($this->lng->txt("date"), "", "25%");
60        $this->addColumn($this->lng->txt("action"), "", "50%");
61
62        $this->getDataFromDb();
63    }
64
65    /**
66    * Fill a single data row.
67    */
68    protected function fillRow($a_set)
69    {
70        $this->tpl->setVariable("TXT_USER", ilUserUtil::getNamePresentation($a_set["user_id"], false, false));
71        $this->tpl->setVariable(
72            'TXT_DATE',
73            ilDatePresentation::formatDate(new ilDateTime($a_set["date"], IL_CAL_DATETIME))
74        );
75        $this->tpl->setVariable("TXT_ACTION", $this->createInfoText($a_set));
76
77        if ($this->getObjType() == "lm") {
78            $obj_arr = explode(":", $a_set["obj_type"]);
79            switch ($obj_arr[1]) {
80                case "st":
81                    $img_type = "st";
82                    $class = "ilstructureobjectgui";
83                    $cmd = "view";
84                    break;
85
86                case "pg":
87                    $img_type = "pg";
88                    $class = "illmpageobjectgui";
89                    $cmd = "edit";
90                    break;
91
92                default:
93                    $img_type = $obj_arr[0];
94                    $class = "";
95                    $cmd = "view";
96                    break;
97            }
98
99            $this->tpl->setCurrentBlock("item_icon");
100            $this->tpl->setVariable("SRC_ICON", ilUtil::getImagePath("icon_" . $img_type . ".svg"));
101            $this->tpl->parseCurrentBlock();
102
103            if ($class != "") {
104                $this->tpl->setCurrentBlock("item_link");
105                $this->ilCtrl->setParameterByClass($class, "obj_id", $a_set["obj_id"]);
106                $this->tpl->setVariable(
107                    "HREF_LINK",
108                    $this->ilCtrl->getLinkTargetByClass($class, $cmd)
109                );
110                $this->tpl->setVariable("TXT_LINK", $a_set["title"]);
111                $this->tpl->parseCurrentBlock();
112            } else {
113                $this->tpl->setCurrentBlock("item_title");
114                $this->tpl->setVariable(
115                    "TXT_TITLE",
116                    ilObject::_lookupTitle($a_set["obj_id"])
117                );
118                $this->tpl->parseCurrentBlock();
119            }
120        }
121
122        if ($this->isCommentVisible() && $a_set["user_comment"] != "") {
123            $this->tpl->setCurrentBlock("user_comment");
124            $this->tpl->setVariable("TXT_COMMENT", $this->lng->txt("comment"));
125            $this->tpl->setVariable("TXT_USER_COMMENT", $a_set["user_comment"]);
126            $this->tpl->parseCurrentBlock();
127        }
128    }
129
130    /**
131     * format info parameters into info text
132     * @param $a_set
133     * @return mixed|string
134     */
135    protected function createInfoText($a_set)
136    {
137        $info_params = explode(",", $a_set["info_params"]);
138
139        switch ($this->getObjType()) {
140            case "lm":
141                $info_text = $this->lng->txt("hist_" . str_replace(":", "_", $a_set["obj_type"]) .
142            "_" . $a_set["action"]);
143                break;
144            default:
145                $info_text = $this->lng->txt("hist_" . str_replace(":", "_", $this->getObjType()) .
146                    "_" . $a_set["action"]);
147                break;
148        }
149
150        $i = 1;
151        foreach ($info_params as $info_param) {
152            $info_text = str_replace("%" . $i, $info_param, $info_text);
153            $i++;
154        }
155
156        return $info_text;
157    }
158
159    /**
160     * set comments visible
161     *
162     * @param $a_visible
163     */
164    public function setCommentVisibility($a_visible)
165    {
166        $this->comment_visibility = (bool) $a_visible;
167    }
168
169    /**
170     * comments visible?
171     * @return bool
172     */
173    public function isCommentVisible()
174    {
175        return $this->comment_visibility;
176    }
177
178    /**
179     * set object id
180     * @param $a_obj_id
181     */
182    public function setObjId($a_obj_id)
183    {
184        $this->obj_id = $a_obj_id;
185    }
186
187    /**
188     * get object id
189     * @return mixed
190     */
191    public function getObjId()
192    {
193        return $this->obj_id;
194    }
195
196    /**
197     * set object type (not required)
198     * @param $a_obj_type
199     */
200    public function setObjType($a_obj_type)
201    {
202        $this->obj_type = $a_obj_type;
203    }
204
205    /**
206     * get object type (if not set, it will be set via object id)
207     * @return mixed
208     */
209    public function getObjType()
210    {
211        if (!$this->obj_type) {
212            $this->setObjType(ilObject::_lookupType($this->getObjId()));
213        }
214        return $this->obj_type;
215    }
216}
217