ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilBadgeUserTableGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once("Services/Table/classes/class.ilTable2GUI.php");
5 
15 {
19  protected $ctrl;
20 
24  protected $tree;
25 
26  protected $award_badge; // [ilBadge]
27  protected $do_parent; // [bool]
28 
29  public function __construct($a_parent_obj, $a_parent_cmd = "", $a_parent_ref_id, ilBadge $a_award_bagde = null, $a_parent_obj_id = null, $a_restrict_badge_id = null)
30  {
31  global $DIC;
32 
33  $this->ctrl = $DIC->ctrl();
34  $this->lng = $DIC->language();
35  $this->tree = $DIC->repositoryTree();
36  $ilCtrl = $DIC->ctrl();
37  $lng = $DIC->language();
38 
39  $this->setId("bdgusr");
40  $this->award_badge = $a_award_bagde;
41  $this->do_parent = false;
42 
43  $parent_type = ilObject::_lookupType($a_parent_ref_id, true);
44  if (in_array($parent_type, array("grp", "crs"))) {
45  $this->do_parent = (!$a_parent_obj_id && !$this->award_badge);
46  }
47 
48  parent::__construct($a_parent_obj, $a_parent_cmd);
49 
50  $this->setLimit(9999);
51 
52  if ($this->award_badge) {
53  $this->setTitle($lng->txt("badge_award_badge") . ": " . $a_award_bagde->getTitle());
54  $this->setDescription($a_award_bagde->getDescription());
55 
56  $this->addColumn("", "", 1);
57 
58  $this->setFormAction($ilCtrl->getFormAction($a_parent_obj));
59  $this->addMultiCommand("assignBadge", $lng->txt("badge_award_badge"));
60  $this->addMultiCommand("confirmDeassignBadge", $lng->txt("badge_remove_badge"));
61  } else {
62  $parent = "";
63  if ($a_parent_obj_id) {
64  $title = ilObject::_lookupTitle($a_parent_obj_id);
65  if (!$title) {
66  include_once "Services/Object/classes/class.ilObjectDataDeletionLog.php";
67  $title = ilObjectDataDeletionLog::get($a_parent_obj_id);
68  if ($title) {
69  $title = $title["title"];
70  }
71  }
72  if ($a_restrict_badge_id) {
73  include_once("Services/Badge/classes/class.ilBadge.php");
74  $badge = new ilBadge($a_restrict_badge_id);
75  $title .= " - " . $badge->getTitle();
76  }
77  $parent = $title . ": ";
78  }
79  $this->setTitle($parent . $lng->txt("users"));
80  }
81 
82  $this->addColumn($lng->txt("name"), "name");
83  $this->addColumn($lng->txt("login"), "login");
84  $this->addColumn($lng->txt("type"), "type");
85  $this->addColumn($lng->txt("title"), "title");
86  $this->addColumn($lng->txt("badge_issued_on"), "issued");
87 
88  if ($this->do_parent) {
89  $this->addColumn($lng->txt("object"), "parent_id");
90  }
91 
92  $this->setDefaultOrderField("name");
93 
94  $this->setRowTemplate("tpl.user_row.html", "Services/Badge");
95 
96  $this->setFormAction($ilCtrl->getFormAction($a_parent_obj));
97  $this->setFilterCommand("apply" . ucfirst($this->getParentCmd()));
98  $this->setResetCommand("reset" . ucfirst($this->getParentCmd()));
99 
100  $this->initFilter();
101 
102  $this->getItems($a_parent_ref_id, $this->award_badge, $a_parent_obj_id, $a_restrict_badge_id);
103  }
104 
105  public function initFilter()
106  {
107  $lng = $this->lng;
108 
109  $name = $this->addFilterItemByMetaType("name", self::FILTER_TEXT, false, $lng->txt("name"));
110  $this->filter["name"] = $name->getValue();
111  }
112 
113  public function getItems($a_parent_ref_id, ilBadge $a_award_bagde = null, $a_parent_obj_id = null, $a_restrict_badge_id = null)
114  {
115  $tree = $this->tree;
116 
117  $data = array();
118 
119  if (!$a_parent_obj_id) {
120  $a_parent_obj_id = ilObject::_lookupObjId($a_parent_ref_id);
121  }
122 
123  // repository context: walk tree for available users
124  if ($a_parent_ref_id) {
125  $user_ids = ilBadgeHandler::getInstance()->getUserIds($a_parent_ref_id, $a_parent_obj_id);
126  }
127 
128  $obj_ids = array($a_parent_obj_id);
129 
130  // add sub-items
131  if ($this->do_parent) {
132  foreach ($tree->getSubTree($tree->getNodeData($a_parent_ref_id)) as $node) {
133  $obj_ids[] = $node["obj_id"];
134  }
135  }
136 
137  include_once "Services/Badge/classes/class.ilBadge.php";
138  include_once "Services/Badge/classes/class.ilBadgeAssignment.php";
139  $badges = $assignments = array();
140  foreach ($obj_ids as $obj_id) {
141  foreach (ilBadge::getInstancesByParentId($obj_id) as $badge) {
142  $badges[$badge->getId()] = $badge;
143  }
144 
145  foreach (ilBadgeAssignment::getInstancesByParentId($obj_id) as $ass) {
146  if ($a_restrict_badge_id &&
147  $a_restrict_badge_id != $ass->getBadgeId()) {
148  continue;
149  }
150 
151  // when awarding we only want to see the current badge
152  if ($this->award_badge &&
153  $ass->getBadgeId() != $this->award_badge->getId()) {
154  continue;
155  }
156 
157  $assignments[$ass->getUserId()][] = $ass;
158  }
159  }
160 
161  // administration context: show only existing assignments
162  if (!$user_ids) {
163  $user_ids = array_keys($assignments);
164  }
165 
166  include_once "Services/Badge/classes/class.ilBadgeRenderer.php";
167  include_once "Services/User/classes/class.ilUserQuery.php";
168  $tmp["set"] = array();
169  if (count($user_ids) > 0) {
170  $uquery = new ilUserQuery();
171  $uquery->setLimit(9999);
172  $uquery->setUserFilter($user_ids);
173 
174  if ($this->filter["name"]) {
175  $uquery->setTextFilter($this->filter["name"]);
176  }
177 
178  $tmp = $uquery->query();
179  }
180  foreach ($tmp["set"] as $user) {
181  // add 1 entry for each badge
182  if (array_key_exists($user["usr_id"], $assignments)) {
183  foreach ($assignments[$user["usr_id"]] as $user_ass) {
184  $idx = $user_ass->getBadgeId() . "-" . $user["usr_id"];
185 
186  $badge = $badges[$user_ass->getBadgeId()];
187 
188  if ($this->do_parent) {
189  $parent = $badge->getParentMeta();
190  }
191 
192  $data[$idx] = array(
193  "user_id" => $user["usr_id"],
194  "name" => $user["lastname"] . ", " . $user["firstname"],
195  "login" => $user["login"],
196  "type" => ilBadge::getExtendedTypeCaption($badge->getTypeInstance()),
197  "title" => $badge->getTitle(),
198  "issued" => $user_ass->getTimestamp(),
199  "parent_id" => $parent["id"],
200  "parent_meta" => $parent
201  );
202  }
203  }
204  // no badge yet, add dummy entry (for manual awarding)
205  elseif ($this->award_badge) {
206  $idx = "0-" . $user["usr_id"];
207 
208  $data[$idx] = array(
209  "user_id" => $user["usr_id"],
210  "name" => $user["lastname"] . ", " . $user["firstname"],
211  "login" => $user["login"],
212  "type" => "",
213  "title" => "",
214  "issued" => "",
215  "parent_id" => ""
216  );
217  }
218  }
219 
220  $this->setData($data);
221  }
222 
223  protected function fillRow($a_set)
224  {
225  $lng = $this->lng;
226 
227  if ($this->award_badge) {
228  $this->tpl->setVariable("VAL_ID", $a_set["user_id"]);
229  }
230 
231  $this->tpl->setVariable("TXT_NAME", $a_set["name"]);
232  $this->tpl->setVariable("TXT_LOGIN", $a_set["login"]);
233  $this->tpl->setVariable("TXT_TYPE", $a_set["type"]);
234  $this->tpl->setVariable("TXT_TITLE", $a_set["title"]);
235  $this->tpl->setVariable("TXT_ISSUED", $a_set["issued"]
236  ? ilDatePresentation::formatDate(new ilDateTime($a_set["issued"], IL_CAL_UNIX))
237  : "");
238 
239  if ($a_set["parent_id"]) {
240  $parent = $a_set["parent_meta"];
241  $this->tpl->setVariable("PARENT", $parent["title"]);
242  $this->tpl->setVariable("PARENT_TYPE", $lng->txt("obj_" . $parent["type"]));
243  $this->tpl->setVariable(
244  "PARENT_ICON",
245  ilObject::_getIcon($parent["id"], "big", $parent["type"])
246  );
247  }
248  }
249 }
setDescription($a_val)
Set description.
setDefaultOrderField($a_defaultorderfield)
Set Default order field.
User query class.
global $DIC
Definition: saml.php:7
addFilterItemByMetaType($id, $type=self::FILTER_TEXT, $a_optional=false, $caption=null)
Add filter by standard type.
static _lookupTitle($a_id)
lookup object title
const IL_CAL_UNIX
getParentCmd()
Get parent command.
static formatDate(ilDateTime $date, $a_skip_day=false, $a_include_wd=false, $include_seconds=false)
Format a date public.
setId($a_val)
Set id.
getItems($a_parent_ref_id, ilBadge $a_award_bagde=null, $a_parent_obj_id=null, $a_restrict_badge_id=null)
global $ilCtrl
Definition: ilias.php:18
setTitle($a_title, $a_icon=0, $a_icon_alt=0)
Set title and title icon.
Class ilTable2GUI.
static getInstancesByParentId($a_parent_id, array $a_filter=null)
setResetCommand($a_val, $a_caption=null)
Set reset filter command.
static getExtendedTypeCaption(ilBadgeType $a_type)
static _lookupObjId($a_id)
addMultiCommand($a_cmd, $a_text)
Add Command button.
Date and time handling
$user
Definition: migrateto20.php:57
setRowTemplate($a_template, $a_template_dir="")
Set row template.
static _lookupType($a_id, $a_reference=false)
lookup object type
setFormAction($a_form_action, $a_multipart=false)
Set Form action parameter.
__construct($a_parent_obj, $a_parent_cmd="", $a_parent_ref_id, ilBadge $a_award_bagde=null, $a_parent_obj_id=null, $a_restrict_badge_id=null)
addColumn( $a_text, $a_sort_field="", $a_width="", $a_is_checkbox_action_column=false, $a_class="", $a_tooltip="", $a_tooltip_with_html=false)
Add a column to the header.
TableGUI class for badge user listing.
setLimit($a_limit=0, $a_default_limit=0)
static getInstance()
Constructor.
static getInstancesByParentId($a_parent_obj_id)
setFilterCommand($a_val, $a_caption=null)
Set filter command.