ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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
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 private function setId($a_id)
74 {
75 $this->id = $a_id;
76 }
77
78 protected function read($a_id)
79 {
81
82 // #18094
83 $this->members = array();
84
85 $sql = "SELECT * FROM il_exc_team" .
86 " WHERE id = " . $ilDB->quote($a_id, "integer");
87 $set = $ilDB->query($sql);
88 if ($ilDB->numRows($set)) {
89 $this->setId($a_id);
90
91 while ($row = $ilDB->fetchAssoc($set)) {
92 $this->assignment_id = $row["ass_id"];
93 $this->members[] = $row["user_id"];
94 }
95 }
96 }
97
107 public static function getTeamId($a_assignment_id, $a_user_id, $a_create_on_demand = false)
108 {
109 global $DIC;
110
111 $ilDB = $DIC->database();
112
113 $sql = "SELECT id FROM il_exc_team" .
114 " WHERE ass_id = " . $ilDB->quote($a_assignment_id, "integer") .
115 " AND user_id = " . $ilDB->quote($a_user_id, "integer");
116 $set = $ilDB->query($sql);
117 $row = $ilDB->fetchAssoc($set);
118 $id = $row["id"];
119
120 if (!$id && $a_create_on_demand) {
121 $id = $ilDB->nextId("il_exc_team");
122
123 // get starting timestamp (relative deadlines) from individual deadline
124 include_once("./Modules/Exercise/classes/class.ilExcIndividualDeadline.php");
125 $idl = ilExcIndividualDeadline::getInstance($a_assignment_id, $a_user_id);
126
127 $fields = array("id" => array("integer", $id),
128 "ass_id" => array("integer", $a_assignment_id),
129 "user_id" => array("integer", $a_user_id));
130 $ilDB->insert("il_exc_team", $fields);
131
132 // set starting timestamp for created team
133 if ($idl->getStartingTimestamp() > 0) {
134 $idl_team = ilExcIndividualDeadline::getInstance($a_assignment_id, $id, true);
135 $idl_team->setStartingTimestamp($idl->getStartingTimestamp());
136 $idl_team->save();
137 }
138
139 self::writeTeamLog($id, self::TEAM_LOG_CREATE_TEAM);
141 $id,
142 self::TEAM_LOG_ADD_MEMBER,
144 );
145 }
146
147 return $id;
148 }
149
150 public function createTeam($a_assignment_id, $a_user_id)
151 {
153 $id = $ilDB->nextId("il_exc_team");
154 $fields = array("id" => array("integer", $id),
155 "ass_id" => array("integer", $a_assignment_id),
156 "user_id" => array("integer", $a_user_id));
157 $ilDB->insert("il_exc_team", $fields);
158 self::writeTeamLog($id, self::TEAM_LOG_CREATE_TEAM);
160 $id,
161 self::TEAM_LOG_ADD_MEMBER,
163 );
164 return $id;
165 }
166
172 public function getMembers()
173 {
174 return $this->members;
175 }
176
182 public function getMembersOfAllTeams()
183 {
185
186 $ids = array();
187
188 $sql = "SELECT user_id" .
189 " FROM il_exc_team" .
190 " WHERE ass_id = " . $ilDB->quote($this->assignment_id, "integer");
191 $set = $ilDB->query($sql);
192 while ($row = $ilDB->fetchAssoc($set)) {
193 $ids[] = $row["user_id"];
194 }
195
196 return $ids;
197 }
198
205 public function addTeamMember($a_user_id, $a_exc_ref_id = null)
206 {
208
209 if (!$this->id) {
210 return false;
211 }
212
213 // must not be in any team already
214 if (!in_array($a_user_id, $this->getMembersOfAllTeams())) {
215 $fields = array("id" => array("integer", $this->id),
216 "ass_id" => array("integer", $this->assignment_id),
217 "user_id" => array("integer", $a_user_id));
218 $ilDB->insert("il_exc_team", $fields);
219
220 if ($a_exc_ref_id) {
221 $this->sendNotification($a_exc_ref_id, $a_user_id, "add");
222 }
223
224 $this->writeLog(
225 self::TEAM_LOG_ADD_MEMBER,
227 );
228
229 $this->read($this->id);
230
231 return true;
232 }
233
234 return false;
235 }
236
243 public function removeTeamMember($a_user_id, $a_exc_ref_id = null)
244 {
246
247 if (!$this->id) {
248 return;
249 }
250
251 $sql = "DELETE FROM il_exc_team" .
252 " WHERE ass_id = " . $ilDB->quote($this->assignment_id, "integer") .
253 " AND id = " . $ilDB->quote($this->id, "integer") .
254 " AND user_id = " . $ilDB->quote($a_user_id, "integer");
255 $ilDB->manipulate($sql);
256
257 if ($a_exc_ref_id) {
258 $this->sendNotification($a_exc_ref_id, $a_user_id, "rmv");
259 }
260
261 $this->writeLog(
262 self::TEAM_LOG_REMOVE_MEMBER,
264 );
265
266 $this->read($this->id);
267 }
268
275 public static function getAssignmentTeamMap($a_ass_id)
276 {
277 global $DIC;
278
279 $ilDB = $DIC->database();
280
281 $map = array();
282
283 $sql = "SELECT * FROM il_exc_team" .
284 " WHERE ass_id = " . $ilDB->quote($a_ass_id, "integer");
285 $set = $ilDB->query($sql);
286 while ($row = $ilDB->fetchAssoc($set)) {
287 $map[$row["user_id"]] = $row["id"];
288 }
289
290 return $map;
291 }
292
293 public function writeLog($a_action, $a_details = null)
294 {
295 self::writeTeamLog($this->id, $a_action, $a_details);
296 }
297
305 public static function writeTeamLog($a_team_id, $a_action, $a_details = null)
306 {
307 global $DIC;
308
309 $ilDB = $DIC->database();
310 $ilUser = $DIC->user();
311 $id = $ilDB->nextId('il_exc_team_log');
312
313 $fields = array(
314 "log_id" => array("integer", $id),
315 "team_id" => array("integer", $a_team_id),
316 "user_id" => array("integer", $ilUser->getId()),
317 "action" => array("integer", $a_action),
318 "details" => array("text", $a_details),
319 "tstamp" => array("integer", time())
320 );
321
322 $ilDB->insert("il_exc_team_log", $fields);
323 }
324
331 public function getLog()
332 {
334
335 $this->cleanLog();
336
337 $res = array();
338
339 $sql = "SELECT * FROM il_exc_team_log" .
340 " WHERE team_id = " . $ilDB->quote($this->id, "integer") .
341 " ORDER BY tstamp DESC";
342 $set = $ilDB->query($sql);
343 while ($row = $ilDB->fetchAssoc($set)) {
344 $res[] = $row;
345 }
346 return $res;
347 }
348
354 protected function cleanLog()
355 {
357
358 // #18179
359
360 $teams = array();
361 $set = $ilDB->query("SELECT DISTINCT(id)" .
362 " FROM il_exc_team");
363 while ($row = $ilDB->fetchAssoc($set)) {
364 $teams[] = $row["id"];
365 }
366
367 $set = $ilDB->query("SELECT DISTINCT(team_id)" .
368 " FROM il_exc_team_log");
369 while ($row = $ilDB->fetchAssoc($set)) {
370 $team_id = $row["team_id"];
371 if (!in_array($team_id, $teams)) {
372 $ilDB->manipulate("DELETE FROM il_exc_team_log" .
373 " WHERE team_id = " . $ilDB->quote($team_id, "integer"));
374 }
375 }
376 }
377
385 public function sendNotification($a_exc_ref_id, $a_user_id, $a_action)
386 {
388
389 // no need to notify current user
390 if (!$a_exc_ref_id ||
391 $ilUser->getId() == $a_user_id) {
392 return;
393 }
394
395 $ass = new ilExAssignment($this->assignment_id);
396
397 include_once "./Services/Notification/classes/class.ilSystemNotification.php";
398 $ntf = new ilSystemNotification();
399 $ntf->setLangModules(array("exc"));
400 $ntf->setRefId($a_exc_ref_id);
401 $ntf->setChangedByUserId($ilUser->getId());
402 $ntf->setSubjectLangId('exc_team_notification_subject_' . $a_action);
403 $ntf->setIntroductionLangId('exc_team_notification_body_' . $a_action);
404 $ntf->addAdditionalInfo("exc_assignment", $ass->getTitle());
405 $ntf->setGotoLangId('exc_team_notification_link');
406 $ntf->setReasonLangId('exc_team_notification_reason');
407 $ntf->sendMail(array($a_user_id));
408 }
409
410
411 public static function getAdoptableTeamAssignments($a_exercise_id, $a_exclude_ass_id = null, $a_user_id = null)
412 {
413 $res = array();
414
416 foreach ($data as $row) {
417 if ($a_exclude_ass_id && $row["id"] == $a_exclude_ass_id) {
418 continue;
419 }
420
421 if ($row["type"] == ilExAssignment::TYPE_UPLOAD_TEAM) {
423
424 if ($a_user_id && !array_key_exists($a_user_id, $map)) {
425 continue;
426 }
427
428 if (sizeof($map)) {
429 $user_team = null;
430 if ($a_user_id) {
431 $user_team_id = $map[$a_user_id];
432 $user_team = array();
433 foreach ($map as $user_id => $team_id) {
434 if ($user_id != $a_user_id &&
435 $user_team_id == $team_id) {
436 $user_team[] = $user_id;
437 }
438 }
439 }
440
441 if (!$a_user_id ||
442 sizeof($user_team)) {
443 $res[$row["id"]] = array(
444 "title" => $row["title"],
445 "teams" => sizeof(array_flip($map)),
446 );
447
448 if ($a_user_id) {
449 $res[$row["id"]]["user_team"] = $user_team;
450 }
451 }
452 }
453 }
454 }
455
456 return ilUtil::sortArray($res, "title", "asc", false, true);
457 }
458
459 public static function adoptTeams($a_source_ass_id, $a_target_ass_id, $a_user_id = null, $a_exc_ref_id = null)
460 {
461 $teams = array();
462
463 $old_team = null;
464 foreach (self::getAssignmentTeamMap($a_source_ass_id) as $user_id => $team_id) {
465 $teams[$team_id][] = $user_id;
466
467 if ($a_user_id && $user_id == $a_user_id) {
468 $old_team = $team_id;
469 }
470 }
471
472 if ($a_user_id) {
473 // no existing team (in source) or user already in team (in current)
474 if (!$old_team ||
475 self::getInstanceByUserId($a_target_ass_id, $a_user_id)->getId()) {
476 return;
477 }
478 }
479
480 $current_map = self::getAssignmentTeamMap($a_target_ass_id);
481
482 foreach ($teams as $team_id => $user_ids) {
483 if (!$old_team || $team_id == $old_team) {
484 // only not assigned users
485 $missing = array();
486 foreach ($user_ids as $user_id) {
487 if (!array_key_exists($user_id, $current_map)) {
488 $missing[] = $user_id;
489 }
490 }
491
492 if (sizeof($missing)) {
493 // create new team
494 $first = array_shift($missing);
495 $new_team = self::getInstanceByUserId($a_target_ass_id, $first, true);
496
497 // give new team starting time of original user
498 if ($a_user_id > 0 && $old_team > 0) {
499 $idl = ilExcIndividualDeadline::getInstance($a_target_ass_id, $a_user_id);
500 if ($idl->getStartingTimestamp()) {
501 $idl_team = ilExcIndividualDeadline::getInstance($a_target_ass_id, $new_team->getId(), true);
502 $idl_team->setStartingTimestamp($idl->getStartingTimestamp());
503 $idl_team->save();
504 }
505 }
506
507 if ($a_exc_ref_id) {
508 // getTeamId() does NOT send notification
509 $new_team->sendNotification($a_exc_ref_id, $first, "add");
510 }
511
512 foreach ($missing as $user_id) {
513 $new_team->addTeamMember($user_id, $a_exc_ref_id);
514 }
515 }
516 }
517 }
518 }
519
520 //
521 // GROUPS
522 //
523
524 public static function getAdoptableGroups($a_exc_ref_id)
525 {
526 global $DIC;
527
528 $tree = $DIC->repositoryTree();
529
530 $res = array();
531
532 $parent_ref_id = $tree->getParentId($a_exc_ref_id);
533 if ($parent_ref_id) {
534 foreach ($tree->getChildsByType($parent_ref_id, "grp") as $group) {
535 $res[] = $group["obj_id"];
536 }
537 }
538
539 return $res;
540 }
541
542 public static function getGroupMembersMap($a_exc_ref_id)
543 {
544 $res = array();
545
546 include_once "Modules/Group/classes/class.ilGroupParticipants.php";
547 foreach (self::getAdoptableGroups($a_exc_ref_id) as $grp_obj_id) {
548 $members_obj = new ilGroupParticipants($grp_obj_id);
549
550 $res[$grp_obj_id] = array(
551 "title" => ilObject::_lookupTitle($grp_obj_id)
552 ,"members" => $members_obj->getMembers()
553 );
554 }
555
556 return ilUtil::sortArray($res, "title", "asc", false, true);
557 }
558
573 public function createRandomTeams($a_exercise_id, $a_assignment_id, $a_number_teams, $a_min_participants)
574 {
575 //just in case...
576 if (count(self::getAssignmentTeamMap($a_assignment_id))) {
577 return false;
578 }
579 $exercise = new ilObjExercise($a_exercise_id, false);
580 $obj_exc_members = new ilExerciseMembers($exercise);
581 $members = $obj_exc_members->getMembers();
582 $total_exc_members = count($members);
583 $number_of_teams = $a_number_teams;
584 if (!$number_of_teams) {
585 if ($a_min_participants) {
586 $number_of_teams = round($total_exc_members / $a_min_participants);
587 } else {
588 $number_of_teams = random_int(1, $total_exc_members);
589 }
590 }
591 $members_per_team = round($total_exc_members / $number_of_teams);
592 shuffle($members);
593 for ($i = 0;$i < $number_of_teams;$i++) {
594 $members_counter = 0;
595 while (!empty($members) && $members_counter < $members_per_team) {
596 $member_id = array_pop($members);
597 if ($members_counter == 0) {
598 $team_id = $this->createTeam($a_assignment_id, $member_id);
599 $this->setId($team_id);
600 $this->assignment_id = $a_assignment_id;
601 } else {
602 $this->addTeamMember($member_id);
603 }
604 $members_counter++;
605 }
606 }
607 //get the new teams, remove duplicates.
608 $teams = array_unique(array_values(self::getAssignmentTeamMap($a_assignment_id)));
609 shuffle($teams);
610 while (!empty($members)) {
611 $member_id = array_pop($members);
612 $team_id = array_pop($teams);
613 $this->setId($team_id);
614 $this->addTeamMember($member_id);
615 }
616 return true;
617 }
618}
user()
Definition: user.php:4
An exception for terminatinating execution or to throw for unit testing.
Exercise assignment team.
sendNotification($a_exc_ref_id, $a_user_id, $a_action)
Send notification about team status.
getMembers()
Get members of assignment team.
addTeamMember($a_user_id, $a_exc_ref_id=null)
Add new member to team.
createTeam($a_assignment_id, $a_user_id)
static getAdoptableGroups($a_exc_ref_id)
createRandomTeams($a_exercise_id, $a_assignment_id, $a_number_teams, $a_min_participants)
Create random teams for assignment type "team upload" following specific rules.
getMembersOfAllTeams()
Get members for all teams of assignment.
getLog()
Get all log entries for team.
writeLog($a_action, $a_details=null)
static getGroupMembersMap($a_exc_ref_id)
removeTeamMember($a_user_id, $a_exc_ref_id=null)
Remove member from team.
static getAssignmentTeamMap($a_ass_id)
Get team structure for assignment.
static getInstanceByUserId($a_assignment_id, $a_user_id, $a_create_on_demand=false)
static getInstancesFromMap($a_assignment_id)
static getTeamId($a_assignment_id, $a_user_id, $a_create_on_demand=false)
Get team id for member id.
static getAdoptableTeamAssignments($a_exercise_id, $a_exclude_ass_id=null, $a_user_id=null)
static writeTeamLog($a_team_id, $a_action, $a_details=null)
Add entry to team log.
cleanLog()
Remove obsolete log entries.
static adoptTeams($a_source_ass_id, $a_target_ass_id, $a_user_id=null, $a_exc_ref_id=null)
Exercise assignment.
static getAssignmentDataOfExercise($a_exc_id)
Get assignments data of an exercise in an array.
static getInstance($a_ass_id, $a_participant_id, $a_is_team=false)
Get instance.
Class ilExerciseMembers.
Class ilObjExercise.
static _lookupFullname($a_user_id)
Lookup Full Name.
static _lookupTitle($a_id)
lookup object title
Wrapper classes for system notifications.
static sortArray( $array, $a_array_sortby, $a_array_sortorder=0, $a_numeric=false, $a_keep_keys=false)
sortArray
$i
Definition: disco.tpl.php:19
$row
global $DIC
Definition: saml.php:7
foreach($_POST as $key=> $value) $res
global $ilDB
$ilUser
Definition: imgupload.php:18
$data
Definition: bench.php:6