ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 {
15  protected $db;
16 
20  protected $user;
21 
22  protected $id; // [int]
23  protected $assignment_id; // [int]
24  protected $members = array(); // [array]
25 
29  const TEAM_LOG_ADD_FILE = 4;
31 
32  public function __construct($a_id = null)
33  {
34  global $DIC;
35 
36  $this->db = $DIC->database();
37  $this->user = $DIC->user();
38  if ($a_id) {
39  $this->read($a_id);
40  }
41  }
42 
43  public static function getInstanceByUserId($a_assignment_id, $a_user_id, $a_create_on_demand = false)
44  {
45  $id = self::getTeamId($a_assignment_id, $a_user_id, $a_create_on_demand);
46  return new self($id);
47  }
48 
49  public static function getInstancesFromMap($a_assignment_id)
50  {
51  $teams = array();
52  foreach (self::getAssignmentTeamMap($a_assignment_id) as $user_id => $team_id) {
53  $teams[$team_id][] = $user_id;
54  }
55 
56  $res = array();
57  foreach ($teams as $team_id => $members) {
58  $team = new self();
59  $team->id = $team_id;
60  $team->assignment_id = $a_assignment_id;
61  $team->members = $members;
62  $res[$team_id] = $team;
63  }
64 
65  return $res;
66  }
67 
68  public function getId()
69  {
70  return $this->id;
71  }
72 
73  protected function read($a_id)
74  {
75  $ilDB = $this->db;
76 
77  // #18094
78  $this->members = array();
79 
80  $sql = "SELECT * FROM il_exc_team" .
81  " WHERE id = " . $ilDB->quote($a_id, "integer");
82  $set = $ilDB->query($sql);
83  if ($ilDB->numRows($set)) {
84  $this->id = $a_id;
85 
86  while ($row = $ilDB->fetchAssoc($set)) {
87  $this->assignment_id = $row["ass_id"];
88  $this->members[] = $row["user_id"];
89  }
90  }
91  }
92 
102  public static function getTeamId($a_assignment_id, $a_user_id, $a_create_on_demand = false)
103  {
104  global $DIC;
105 
106  $ilDB = $DIC->database();
107 
108  $sql = "SELECT id FROM il_exc_team" .
109  " WHERE ass_id = " . $ilDB->quote($a_assignment_id, "integer") .
110  " AND user_id = " . $ilDB->quote($a_user_id, "integer");
111  $set = $ilDB->query($sql);
112  $row = $ilDB->fetchAssoc($set);
113  $id = $row["id"];
114 
115  if (!$id && $a_create_on_demand) {
116  $id = $ilDB->nextId("il_exc_team");
117 
118  $fields = array("id" => array("integer", $id),
119  "ass_id" => array("integer", $a_assignment_id),
120  "user_id" => array("integer", $a_user_id));
121  $ilDB->insert("il_exc_team", $fields);
122 
123  self::writeTeamLog($id, self::TEAM_LOG_CREATE_TEAM);
124  self::writeTeamLog(
125  $id,
126  self::TEAM_LOG_ADD_MEMBER,
127  ilObjUser::_lookupFullname($a_user_id)
128  );
129  }
130 
131  return $id;
132  }
133 
139  public function getMembers()
140  {
141  return $this->members;
142  }
143 
149  public function getMembersOfAllTeams()
150  {
151  $ilDB = $this->db;
152 
153  $ids = array();
154 
155  $sql = "SELECT user_id" .
156  " FROM il_exc_team" .
157  " WHERE ass_id = " . $ilDB->quote($this->assignment_id, "integer");
158  $set = $ilDB->query($sql);
159  while ($row = $ilDB->fetchAssoc($set)) {
160  $ids[] = $row["user_id"];
161  }
162 
163  return $ids;
164  }
165 
172  public function addTeamMember($a_user_id, $a_exc_ref_id = null)
173  {
174  $ilDB = $this->db;
175 
176  if (!$this->id) {
177  return false;
178  }
179 
180  // must not be in any team already
181  if (!in_array($a_user_id, $this->getMembersOfAllTeams())) {
182  $fields = array("id" => array("integer", $this->id),
183  "ass_id" => array("integer", $this->assignment_id),
184  "user_id" => array("integer", $a_user_id));
185  $ilDB->insert("il_exc_team", $fields);
186 
187  if ($a_exc_ref_id) {
188  $this->sendNotification($a_exc_ref_id, $a_user_id, "add");
189  }
190 
191  $this->writeLog(
192  self::TEAM_LOG_ADD_MEMBER,
193  ilObjUser::_lookupFullname($a_user_id)
194  );
195 
196  $this->read($this->id);
197 
198  return true;
199  }
200 
201  return false;
202  }
203 
210  public function removeTeamMember($a_user_id, $a_exc_ref_id = null)
211  {
212  $ilDB = $this->db;
213 
214  if (!$this->id) {
215  return;
216  }
217 
218  $sql = "DELETE FROM il_exc_team" .
219  " WHERE ass_id = " . $ilDB->quote($this->assignment_id, "integer") .
220  " AND id = " . $ilDB->quote($this->id, "integer") .
221  " AND user_id = " . $ilDB->quote($a_user_id, "integer");
222  $ilDB->manipulate($sql);
223 
224  if ($a_exc_ref_id) {
225  $this->sendNotification($a_exc_ref_id, $a_user_id, "rmv");
226  }
227 
228  $this->writeLog(
229  self::TEAM_LOG_REMOVE_MEMBER,
230  ilObjUser::_lookupFullname($a_user_id)
231  );
232 
233  $this->read($this->id);
234  }
235 
242  public static function getAssignmentTeamMap($a_ass_id)
243  {
244  global $DIC;
245 
246  $ilDB = $DIC->database();
247 
248  $map = array();
249 
250  $sql = "SELECT * FROM il_exc_team" .
251  " WHERE ass_id = " . $ilDB->quote($a_ass_id, "integer");
252  $set = $ilDB->query($sql);
253  while ($row = $ilDB->fetchAssoc($set)) {
254  $map[$row["user_id"]] = $row["id"];
255  }
256 
257  return $map;
258  }
259 
260  public function writeLog($a_action, $a_details = null)
261  {
262  self::writeTeamLog($this->id, $a_action, $a_details);
263  }
264 
272  public static function writeTeamLog($a_team_id, $a_action, $a_details = null)
273  {
274  global $DIC;
275 
276  $ilDB = $DIC->database();
277  $ilUser = $DIC->user();
278  $id = $ilDB->nextId('il_exc_team_log');
279 
280  $fields = array(
281  "log_id" => array("integer", $id),
282  "team_id" => array("integer", $a_team_id),
283  "user_id" => array("integer", $ilUser->getId()),
284  "action" => array("integer", $a_action),
285  "details" => array("text", $a_details),
286  "tstamp" => array("integer", time())
287  );
288 
289  $ilDB->insert("il_exc_team_log", $fields);
290  }
291 
298  public function getLog()
299  {
300  $ilDB = $this->db;
301 
302  $this->cleanLog();
303 
304  $res = array();
305 
306  $sql = "SELECT * FROM il_exc_team_log" .
307  " WHERE team_id = " . $ilDB->quote($this->id, "integer") .
308  " ORDER BY tstamp DESC";
309  $set = $ilDB->query($sql);
310  while ($row = $ilDB->fetchAssoc($set)) {
311  $res[] = $row;
312  }
313  return $res;
314  }
315 
321  protected function cleanLog()
322  {
323  $ilDB = $this->db;
324 
325  // #18179
326 
327  $teams = array();
328  $set = $ilDB->query("SELECT DISTINCT(id)" .
329  " FROM il_exc_team");
330  while ($row = $ilDB->fetchAssoc($set)) {
331  $teams[] = $row["id"];
332  }
333 
334  $set = $ilDB->query("SELECT DISTINCT(team_id)" .
335  " FROM il_exc_team_log");
336  while ($row = $ilDB->fetchAssoc($set)) {
337  $team_id = $row["team_id"];
338  if (!in_array($team_id, $teams)) {
339  $ilDB->manipulate("DELETE FROM il_exc_team_log" .
340  " WHERE team_id = " . $ilDB->quote($team_id, "integer"));
341  }
342  }
343  }
344 
352  public function sendNotification($a_exc_ref_id, $a_user_id, $a_action)
353  {
355 
356  // no need to notify current user
357  if (!$a_exc_ref_id ||
358  $ilUser->getId() == $a_user_id) {
359  return;
360  }
361 
362  $ass = new ilExAssignment($this->assignment_id);
363 
364  include_once "./Services/Notification/classes/class.ilSystemNotification.php";
365  $ntf = new ilSystemNotification();
366  $ntf->setLangModules(array("exc"));
367  $ntf->setRefId($a_exc_ref_id);
368  $ntf->setChangedByUserId($ilUser->getId());
369  $ntf->setSubjectLangId('exc_team_notification_subject_' . $a_action);
370  $ntf->setIntroductionLangId('exc_team_notification_body_' . $a_action);
371  $ntf->addAdditionalInfo("exc_assignment", $ass->getTitle());
372  $ntf->setGotoLangId('exc_team_notification_link');
373  $ntf->setReasonLangId('exc_team_notification_reason');
374  $ntf->sendMail(array($a_user_id));
375  }
376 
377 
378  public static function getAdoptableTeamAssignments($a_exercise_id, $a_exclude_ass_id = null, $a_user_id = null)
379  {
380  $res = array();
381 
383  foreach ($data as $row) {
384  if ($a_exclude_ass_id && $row["id"] == $a_exclude_ass_id) {
385  continue;
386  }
387 
388  if ($row["type"] == ilExAssignment::TYPE_UPLOAD_TEAM) {
389  $map = self::getAssignmentTeamMap($row["id"]);
390 
391  if ($a_user_id && !array_key_exists($a_user_id, $map)) {
392  continue;
393  }
394 
395  if (sizeof($map)) {
396  $user_team = null;
397  if ($a_user_id) {
398  $user_team_id = $map[$a_user_id];
399  $user_team = array();
400  foreach ($map as $user_id => $team_id) {
401  if ($user_id != $a_user_id &&
402  $user_team_id == $team_id) {
403  $user_team[] = $user_id;
404  }
405  }
406  }
407 
408  if (!$a_user_id ||
409  sizeof($user_team)) {
410  $res[$row["id"]] = array(
411  "title" => $row["title"],
412  "teams" => sizeof(array_flip($map)),
413  );
414 
415  if ($a_user_id) {
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  $teams[$team_id][] = $user_id;
433 
434  if ($a_user_id && $user_id == $a_user_id) {
435  $old_team = $team_id;
436  }
437  }
438 
439  if ($a_user_id) {
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  return;
444  }
445  }
446 
447  $current_map = self::getAssignmentTeamMap($a_target_ass_id);
448 
449  foreach ($teams as $team_id => $user_ids) {
450  if (!$old_team || $team_id == $old_team) {
451  // only not assigned users
452  $missing = array();
453  foreach ($user_ids as $user_id) {
454  if (!array_key_exists($user_id, $current_map)) {
455  $missing[] = $user_id;
456  }
457  }
458 
459  if (sizeof($missing)) {
460  // create new team
461  $first = array_shift($missing);
462  $new_team = self::getInstanceByUserId($a_target_ass_id, $first, true);
463 
464  if ($a_exc_ref_id) {
465  // getTeamId() does NOT send notification
466  $new_team->sendNotification($a_exc_ref_id, $first, "add");
467  }
468 
469  foreach ($missing as $user_id) {
470  $new_team->addTeamMember($user_id, $a_exc_ref_id);
471  }
472  }
473  }
474  }
475  }
476 
477  //
478  // GROUPS
479  //
480 
481  public static function getAdoptableGroups($a_exc_ref_id)
482  {
483  global $DIC;
484 
485  $tree = $DIC->repositoryTree();
486 
487  $res = array();
488 
489  $parent_ref_id = $tree->getParentId($a_exc_ref_id);
490  if ($parent_ref_id) {
491  foreach ($tree->getChildsByType($parent_ref_id, "grp") as $group) {
492  $res[] = $group["obj_id"];
493  }
494  }
495 
496  return $res;
497  }
498 
499  public static function getGroupMembersMap($a_exc_ref_id)
500  {
501  $res = array();
502 
503  include_once "Modules/Group/classes/class.ilGroupParticipants.php";
504  foreach (self::getAdoptableGroups($a_exc_ref_id) as $grp_obj_id) {
505  $members_obj = new ilGroupParticipants($grp_obj_id);
506 
507  $res[$grp_obj_id] = array(
508  "title" => ilObject::_lookupTitle($grp_obj_id)
509  ,"members" => $members_obj->getMembers()
510  );
511  }
512 
513  return ilUtil::sortArray($res, "title", "asc", false, true);
514  }
515 }
static sortArray( $array, $a_array_sortby, $a_array_sortorder=0, $a_numeric=false, $a_keep_keys=false)
sortArray
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)
global $DIC
Definition: saml.php:7
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)
user()
Definition: user.php:4
getMembers()
Get members of assignment team.
static getAdoptableTeamAssignments($a_exercise_id, $a_exclude_ass_id=null, $a_user_id=null)
foreach($_POST as $key=> $value) $res
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.