ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
class.ilExAssignmentTeam.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
11 {
12  protected $id; // [int]
13  protected $assignment_id; // [int]
14  protected $members = array(); // [array]
15 
19  const TEAM_LOG_ADD_FILE = 4;
21 
22  public function __construct($a_id = null)
23  {
24  if($a_id)
25  {
26  $this->read($a_id);
27  }
28  }
29 
30  public static function getInstanceByUserId($a_assignment_id, $a_user_id, $a_create_on_demand = false)
31  {
32  $id = self::getTeamId($a_assignment_id, $a_user_id, $a_create_on_demand);
33  return new self($id);
34  }
35 
36  public static function getInstancesFromMap($a_assignment_id)
37  {
38  $teams = array();
39  foreach(self::getAssignmentTeamMap($a_assignment_id) as $user_id => $team_id)
40  {
41  $teams[$team_id][] = $user_id;
42  }
43 
44  $res = array();
45  foreach($teams as $team_id => $members)
46  {
47  $team = new self();
48  $team->id = $team_id;
49  $team->members = $members;
50  $res[$team_id] = $team;
51  }
52 
53  return $res;
54  }
55 
56  public function getId()
57  {
58  return $this->id;
59  }
60 
61  protected function read($a_id)
62  {
63  global $ilDB;
64 
65  // #18094
66  $this->members = array();
67 
68  $sql = "SELECT * FROM il_exc_team".
69  " WHERE id = ".$ilDB->quote($a_id, "integer");
70  $set = $ilDB->query($sql);
71  if($ilDB->numRows($set))
72  {
73  $this->id = $a_id;
74 
75  while($row = $ilDB->fetchAssoc($set))
76  {
77  $this->assignment_id = $row["ass_id"];
78  $this->members[] = $row["user_id"];
79  }
80  }
81  }
82 
92  public static function getTeamId($a_assignment_id, $a_user_id, $a_create_on_demand = false)
93  {
94  global $ilDB;
95 
96  $sql = "SELECT id FROM il_exc_team".
97  " WHERE ass_id = ".$ilDB->quote($a_assignment_id, "integer").
98  " AND user_id = ".$ilDB->quote($a_user_id, "integer");
99  $set = $ilDB->query($sql);
100  $row = $ilDB->fetchAssoc($set);
101  $id = $row["id"];
102 
103  if(!$id && $a_create_on_demand)
104  {
105  $id = $ilDB->nextId("il_exc_team");
106 
107  $fields = array("id" => array("integer", $id),
108  "ass_id" => array("integer", $a_assignment_id),
109  "user_id" => array("integer", $a_user_id));
110  $ilDB->insert("il_exc_team", $fields);
111 
112  self::writeTeamLog($id, self::TEAM_LOG_CREATE_TEAM);
113  self::writeTeamLog($id, self::TEAM_LOG_ADD_MEMBER,
114  ilObjUser::_lookupFullname($a_user_id));
115  }
116 
117  return $id;
118  }
119 
125  public function getMembers()
126  {
127  return $this->members;
128  }
129 
136  {
137  global $ilDB;
138 
139  $ids = array();
140 
141  $sql = "SELECT user_id".
142  " FROM il_exc_team".
143  " WHERE ass_id = ".$ilDB->quote($this->assignment_id, "integer");
144  $set = $ilDB->query($sql);
145  while($row = $ilDB->fetchAssoc($set))
146  {
147  $ids[] = $row["user_id"];
148  }
149 
150  return $ids;
151  }
152 
159  function addTeamMember($a_user_id, $a_exc_ref_id = null)
160  {
161  global $ilDB;
162 
163  if(!$this->id)
164  {
165  return false;
166  }
167 
168  // must not be in any team already
169  if(!in_array($a_user_id, $this->getMembersOfAllTeams()))
170  {
171  $fields = array("id" => array("integer", $this->id),
172  "ass_id" => array("integer", $this->assignment_id),
173  "user_id" => array("integer", $a_user_id));
174  $ilDB->insert("il_exc_team", $fields);
175 
176  if($a_exc_ref_id)
177  {
178  $this->sendNotification($a_exc_ref_id, $a_user_id, "add");
179  }
180 
181  $this->writeLog(self::TEAM_LOG_ADD_MEMBER,
182  ilObjUser::_lookupFullname($a_user_id));
183 
184  $this->read($this->id);
185 
186  return true;
187  }
188 
189  return false;
190  }
191 
198  function removeTeamMember($a_user_id, $a_exc_ref_id = null)
199  {
200  global $ilDB;
201 
202  if(!$this->id)
203  {
204  return;
205  }
206 
207  $sql = "DELETE FROM il_exc_team".
208  " WHERE ass_id = ".$ilDB->quote($this->assignment_id, "integer").
209  " AND id = ".$ilDB->quote($this->id, "integer").
210  " AND user_id = ".$ilDB->quote($a_user_id, "integer");
211  $ilDB->manipulate($sql);
212 
213  if($a_exc_ref_id)
214  {
215  $this->sendNotification($a_exc_ref_id, $a_user_id, "rmv");
216  }
217 
218  $this->writeLog(self::TEAM_LOG_REMOVE_MEMBER,
219  ilObjUser::_lookupFullname($a_user_id));
220 
221  $this->read($this->id);
222  }
223 
230  public static function getAssignmentTeamMap($a_ass_id)
231  {
232  global $ilDB;
233 
234  $map = array();
235 
236  $sql = "SELECT * FROM il_exc_team".
237  " WHERE ass_id = ".$ilDB->quote($a_ass_id, "integer");
238  $set = $ilDB->query($sql);
239  while($row = $ilDB->fetchAssoc($set))
240  {
241  $map[$row["user_id"]] = $row["id"];
242  }
243 
244  return $map;
245  }
246 
247  public function writeLog($a_action, $a_details = null)
248  {
249  self::writeTeamLog($this->id, $a_action, $a_details);
250  }
251 
259  public static function writeTeamLog($a_team_id, $a_action, $a_details = null)
260  {
261  global $ilDB, $ilUser;
262 
263  $fields = array(
264  "team_id" => array("integer", $a_team_id),
265  "user_id" => array("integer", $ilUser->getId()),
266  "action" => array("integer", $a_action),
267  "details" => array("text", $a_details),
268  "tstamp" => array("integer", time())
269  );
270 
271  $ilDB->insert("il_exc_team_log", $fields);
272  }
273 
280  public function getLog()
281  {
282  global $ilDB;
283 
284  $this->cleanLog();
285 
286  $res = array();
287 
288  $sql = "SELECT * FROM il_exc_team_log".
289  " WHERE team_id = ".$ilDB->quote($this->id, "integer").
290  " ORDER BY tstamp DESC";
291  $set = $ilDB->query($sql);
292  while($row = $ilDB->fetchAssoc($set))
293  {
294  $res[] = $row;
295  }
296  return $res;
297  }
298 
304  protected function cleanLog()
305  {
306  global $ilDB;
307 
308  // #18179
309 
310  $teams = array();
311  $set = $ilDB->query("SELECT DISTINCT(id)".
312  " FROM il_exc_team");
313  while($row = $ilDB->fetchAssoc($set))
314  {
315  $teams[] = $row["id"];
316  }
317 
318  $set = $ilDB->query("SELECT DISTINCT(team_id)".
319  " FROM il_exc_team_log");
320  while($row = $ilDB->fetchAssoc($set))
321  {
322  $team_id = $row["team_id"];
323  if(!in_array($team_id, $teams))
324  {
325  $ilDB->manipulate("DELETE FROM il_exc_team_log".
326  " WHERE team_id = ".$ilDB->quote($team_id, "integer"));
327  }
328  }
329  }
330 
338  public function sendNotification($a_exc_ref_id, $a_user_id, $a_action)
339  {
340  global $ilUser;
341 
342  // no need to notify current user
343  if(!$a_exc_ref_id ||
344  $ilUser->getId() == $a_user_id)
345  {
346  return;
347  }
348 
349  $ass = new ilExAssignment($this->assignment_id);
350 
351  include_once "./Services/Notification/classes/class.ilSystemNotification.php";
352  $ntf = new ilSystemNotification();
353  $ntf->setLangModules(array("exc"));
354  $ntf->setRefId($a_exc_ref_id);
355  $ntf->setChangedByUserId($ilUser->getId());
356  $ntf->setSubjectLangId('exc_team_notification_subject_'.$a_action);
357  $ntf->setIntroductionLangId('exc_team_notification_body_'.$a_action);
358  $ntf->addAdditionalInfo("exc_assignment", $ass->getTitle());
359  $ntf->setGotoLangId('exc_team_notification_link');
360  $ntf->setReasonLangId('exc_team_notification_reason');
361  $ntf->sendMail(array($a_user_id));
362  }
363 
364 
365  public static function getAdoptableTeamAssignments($a_exercise_id, $a_exclude_ass_id = null, $a_user_id = null)
366  {
367  $res = array();
368 
370  foreach($data as $row)
371  {
372  if($a_exclude_ass_id && $row["id"] == $a_exclude_ass_id)
373  {
374  continue;
375  }
376 
377  if($row["type"] == ilExAssignment::TYPE_UPLOAD_TEAM)
378  {
379  $map = self::getAssignmentTeamMap($row["id"]);
380 
381  if($a_user_id && !array_key_exists($a_user_id, $map))
382  {
383  continue;
384  }
385 
386  if(sizeof($map))
387  {
388  $user_team = null;
389  if($a_user_id)
390  {
391  $user_team_id = $map[$a_user_id];
392  $user_team = array();
393  foreach($map as $user_id => $team_id)
394  {
395  if($user_id != $a_user_id &&
396  $user_team_id == $team_id)
397  {
398  $user_team[] = $user_id;
399  }
400  }
401  }
402 
403  if(!$a_user_id ||
404  sizeof($user_team))
405  {
406  $res[$row["id"]] = array(
407  "title" => $row["title"],
408  "teams" => sizeof(array_flip($map)),
409  );
410 
411  if($a_user_id)
412  {
413  $res[$row["id"]]["user_team"] = $user_team;
414  }
415  }
416  }
417  }
418  }
419 
420  return ilUtil::sortArray($res, "title", "asc", false, true);
421  }
422 
423  public static function adoptTeams($a_source_ass_id, $a_target_ass_id, $a_user_id = null, $a_exc_ref_id = null)
424  {
425  $teams = array();
426 
427  $old_team = null;
428  foreach(self::getAssignmentTeamMap($a_source_ass_id) as $user_id => $team_id)
429  {
430  $teams[$team_id][] = $user_id;
431 
432  if($a_user_id && $user_id == $a_user_id)
433  {
434  $old_team = $team_id;
435  }
436  }
437 
438  if($a_user_id)
439  {
440  // no existing team (in source) or user already in team (in current)
441  if(!$old_team ||
442  self::getInstanceByUserId($a_target_ass_id, $a_user_id)->getId())
443  {
444  return;
445  }
446  }
447 
448  $current_map = self::getAssignmentTeamMap($a_target_ass_id);
449 
450  foreach($teams as $team_id => $user_ids)
451  {
452  if(!$old_team || $team_id == $old_team)
453  {
454  // only not assigned users
455  $missing = array();
456  foreach($user_ids as $user_id)
457  {
458  if(!array_key_exists($user_id, $current_map))
459  {
460  $missing[] = $user_id;
461  }
462  }
463 
464  if(sizeof($missing))
465  {
466  // create new team
467  $first = array_shift($missing);
468  $new_team = self::getInstanceByUserId($a_target_ass_id, $first, true);
469 
470  if($a_exc_ref_id)
471  {
472  // getTeamId() does NOT send notification
473  $new_team->sendNotification($a_exc_ref_id, $first, "add");
474  }
475 
476  foreach($missing as $user_id)
477  {
478  $new_team->addTeamMember($user_id, $a_exc_ref_id);
479  }
480  }
481  }
482  }
483  }
484 
485  //
486  // GROUPS
487  //
488 
489  public static function getAdoptableGroups($a_exc_ref_id)
490  {
491  global $tree;
492 
493  $res = array();
494 
495  $parent_ref_id = $tree->getParentId($a_exc_ref_id);
496  if($parent_ref_id)
497  {
498  foreach($tree->getChildsByType($parent_ref_id, "grp") as $group)
499  {
500  $res[] = $group["obj_id"];
501  }
502  }
503 
504  return $res;
505  }
506 
507  public static function getGroupMembersMap($a_exc_ref_id)
508  {
509  $res = array();
510 
511  include_once "Modules/Group/classes/class.ilGroupParticipants.php";
512  foreach(self::getAdoptableGroups($a_exc_ref_id) as $grp_obj_id)
513  {
514  $members_obj = new ilGroupParticipants($grp_obj_id);
515 
516  $res[$grp_obj_id] = array(
517  "title" => ilObject::_lookupTitle($grp_obj_id)
518  ,"members" => $members_obj->getMembers()
519  );
520  }
521 
522  return ilUtil::sortArray($res, "title", "asc", false, true);
523  }
524 }
525 
static getAssignmentDataOfExercise($a_exc_id)
Get assignments data of an exercise in an array.
Exercise assignment.
static getAdoptableGroups($a_exc_ref_id)
static getGroupMembersMap($a_exc_ref_id)
static getAssignmentTeamMap($a_ass_id)
Get team structure for assignment.
static getTeamId($a_assignment_id, $a_user_id, $a_create_on_demand=false)
Get team id for member id.
static getInstancesFromMap($a_assignment_id)
addTeamMember($a_user_id, $a_exc_ref_id=null)
Add new member to team.
getLog()
Get all log entries for team.
Exercise assignment team.
static _lookupTitle($a_id)
lookup object title
static adoptTeams($a_source_ass_id, $a_target_ass_id, $a_user_id=null, $a_exc_ref_id=null)
static sortArray($array, $a_array_sortby, $a_array_sortorder=0, $a_numeric=false, $a_keep_keys=false)
sortArray
_lookupFullname($a_user_id)
Lookup Full Name.
getMembers()
Get members of assignment team.
static getAdoptableTeamAssignments($a_exercise_id, $a_exclude_ass_id=null, $a_user_id=null)
$data
sendNotification($a_exc_ref_id, $a_user_id, $a_action)
Send notification about team status.
static getInstanceByUserId($a_assignment_id, $a_user_id, $a_create_on_demand=false)
removeTeamMember($a_user_id, $a_exc_ref_id=null)
Remove member from team.
cleanLog()
Remove obsolete log entries.
getMembersOfAllTeams()
Get members for all teams of assignment.
global $ilUser
Definition: imgupload.php:15
global $ilDB
writeLog($a_action, $a_details=null)
Wrapper classes for system notifications.
static writeTeamLog($a_team_id, $a_action, $a_details=null)
Add entry to team log.