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