ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilExAssignment.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
12 {
13  const TYPE_UPLOAD = 1;
14  const TYPE_BLOG = 2;
15  const TYPE_PORTFOLIO = 3;
16  const TYPE_UPLOAD_TEAM = 4;
17 
21  const TEAM_LOG_ADD_FILE = 4;
23 
27  function __construct($a_id = 0)
28  {
29  $this->setType(self::TYPE_UPLOAD);
30 
31  if ($a_id > 0)
32  {
33  $this->setId($a_id);
34  $this->read();
35  }
36  }
37 
43  function setId($a_val)
44  {
45  $this->id = $a_val;
46  }
47 
53  function getId()
54  {
55  return $this->id;
56  }
57 
63  function setExerciseId($a_val)
64  {
65  $this->exc_id = $a_val;
66  }
67 
73  function getExerciseId()
74  {
75  return $this->exc_id;
76  }
77 
83  function setStartTime($a_val)
84  {
85  $this->start_time = $a_val;
86  }
87 
93  function getStartTime()
94  {
95  return $this->start_time;
96  }
97 
103  function setDeadline($a_val)
104  {
105  $this->deadline = $a_val;
106  }
107 
113  function getDeadline()
114  {
115  return $this->deadline;
116  }
117 
123  function setInstruction($a_val)
124  {
125  $this->instruction = $a_val;
126  }
127 
133  function getInstruction()
134  {
135  return $this->instruction;
136  }
137 
143  function setTitle($a_val)
144  {
145  $this->title = $a_val;
146  }
147 
153  function getTitle()
154  {
155  return $this->title;
156  }
157 
163  function setMandatory($a_val)
164  {
165  $this->mandatory = $a_val;
166  }
167 
173  function getMandatory()
174  {
175  return $this->mandatory;
176  }
177 
183  function setOrderNr($a_val)
184  {
185  $this->order_nr = $a_val;
186  }
187 
193  function getOrderNr()
194  {
195  return $this->order_nr;
196  }
197 
203  function setType($a_value)
204  {
205  if($this->isValidType($a_value))
206  {
207  $this->type = (int)$a_value;
208  }
209  }
210 
216  function getType()
217  {
218  return $this->type;
219  }
220 
227  function isValidType($a_value)
228  {
229  if(in_array((int)$a_value, array(self::TYPE_UPLOAD, self::TYPE_BLOG,
230  self::TYPE_PORTFOLIO, self::TYPE_UPLOAD_TEAM)))
231  {
232  return true;
233  }
234  return false;
235  }
236 
240  function read()
241  {
242  global $ilDB;
243 
244  $set = $ilDB->query("SELECT * FROM exc_assignment ".
245  " WHERE id = ".$ilDB->quote($this->getId(), "integer")
246  );
247  while ($rec = $ilDB->fetchAssoc($set))
248  {
249  $this->setExerciseId($rec["exc_id"]);
250  $this->setDeadline($rec["time_stamp"]);
251  $this->setInstruction($rec["instruction"]);
252  $this->setTitle($rec["title"]);
253  $this->setStartTime($rec["start_time"]);
254  $this->setOrderNr($rec["order_nr"]);
255  $this->setMandatory($rec["mandatory"]);
256  $this->setType($rec["type"]);
257  }
258  }
259 
263  function save()
264  {
265  global $ilDB;
266 
267  if ($this->getOrderNr() == 0)
268  {
269  $this->setOrderNr(
271  + 10);
272  }
273 
274  $next_id = $ilDB->nextId("exc_assignment");
275  $ilDB->insert("exc_assignment", array(
276  "id" => array("integer", $next_id),
277  "exc_id" => array("integer", $this->getExerciseId()),
278  "time_stamp" => array("integer", $this->getDeadline()),
279  "instruction" => array("clob", $this->getInstruction()),
280  "title" => array("text", $this->getTitle()),
281  "start_time" => array("integer", $this->getStartTime()),
282  "order_nr" => array("integer", $this->getOrderNr()),
283  "mandatory" => array("integer", $this->getMandatory()),
284  "type" => array("integer", $this->getType())
285  ));
286  $this->setId($next_id);
287  $exc = new ilObjExercise($this->getExerciseId(), false);
288  $exc->updateAllUsersStatus();
290  }
291 
295  function update()
296  {
297  global $ilDB;
298 
299  $ilDB->update("exc_assignment",
300  array(
301  "exc_id" => array("integer", $this->getExerciseId()),
302  "time_stamp" => array("integer", $this->getDeadline()),
303  "instruction" => array("clob", $this->getInstruction()),
304  "title" => array("text", $this->getTitle()),
305  "start_time" => array("integer", $this->getStartTime()),
306  "order_nr" => array("integer", $this->getOrderNr()),
307  "mandatory" => array("integer", $this->getMandatory()),
308  "type" => array("integer", $this->getType())
309  ),
310  array(
311  "id" => array("integer", $this->getId()),
312  ));
313  $exc = new ilObjExercise($this->getExerciseId(), false);
314  $exc->updateAllUsersStatus();
315  }
316 
320  function delete()
321  {
322  global $ilDB;
323 
324  $ilDB->manipulate("DELETE FROM exc_assignment WHERE ".
325  " id = ".$ilDB->quote($this->getId(), "integer")
326  );
327  $exc = new ilObjExercise($this->getExerciseId(), false);
328  $exc->updateAllUsersStatus();
329  }
330 
331 
335  static function getAssignmentDataOfExercise($a_exc_id)
336  {
337  global $ilDB;
338 
339  $set = $ilDB->query("SELECT * FROM exc_assignment ".
340  " WHERE exc_id = ".$ilDB->quote($a_exc_id, "integer").
341  " ORDER BY order_nr ASC");
342  $data = array();
343 
344  $order_val = 10;
345  while ($rec = $ilDB->fetchAssoc($set))
346  {
347 
348  $data[] = array(
349  "id" => $rec["id"],
350  "exc_id" => $rec["exc_id"],
351  "deadline" => $rec["time_stamp"],
352  "instruction" => $rec["instruction"],
353  "title" => $rec["title"],
354  "start_time" => $rec["start_time"],
355  "order_val" => $order_val,
356  "mandatory" => $rec["mandatory"],
357  "type" => $rec["type"]
358  );
359  $order_val += 10;
360  }
361  return $data;
362  }
363 
370  function cloneAssignmentsOfExercise($a_old_exc_id, $a_new_exc_id)
371  {
372  $ass_data = ilExAssignment::getAssignmentDataOfExercise($a_old_exc_id);
373  foreach ($ass_data as $d)
374  {
375  // clone assignment
376  $new_ass = new ilExAssignment();
377  $new_ass->setExerciseId($a_new_exc_id);
378  $new_ass->setTitle($d["title"]);
379  $new_ass->setDeadline($d["deadline"]);
380  $new_ass->setInstruction($d["instruction"]);
381  $new_ass->setMandatory($d["mandatory"]);
382  $new_ass->setOrderNr($d["order_val"]);
383  $new_ass->setStartTime($d["start_time"]);
384  $new_ass->setType($d["type"]);
385  $new_ass->save();
386 
387  // clone assignment files
388  include_once("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
389  $old_storage = new ilFSStorageExercise($a_old_exc_id, (int) $d["id"]);
390  $new_storage = new ilFSStorageExercise($a_new_exc_id, (int) $new_ass->getId());
391  $new_storage->create();
392 
393  if (is_dir($old_storage->getPath()))
394  {
395  ilUtil::rCopy($old_storage->getPath(), $new_storage->getPath());
396  }
397  }
398  }
399 
403  static function getFiles($a_exc_id, $a_ass_id)
404  {
405  include_once("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
406  $storage = new ilFSStorageExercise($a_exc_id, $a_ass_id);
407  return $storage->getFiles();
408  }
409 
413  static function lookupMaxOrderNrForEx($a_exc_id)
414  {
415  global $ilDB;
416 
417  $set = $ilDB->query("SELECT MAX(order_nr) mnr FROM exc_assignment ".
418  " WHERE exc_id = ".$ilDB->quote($a_exc_id, "integer")
419  );
420  while ($rec = $ilDB->fetchAssoc($set))
421  {
422  return (int) $rec["mnr"];
423  }
424  return 0;
425  }
426 
432  public static function lookupAssignmentOnline($a_ass_id)
433  {
434  global $ilDB;
435 
436  $query = "SELECT id FROM exc_assignment ".
437  "WHERE start_time <= ".$ilDB->quote(time(),'integer').' '.
438  "AND time_stamp >= ".$ilDB->quote(time(),'integer').' '.
439  "AND id = ".$ilDB->quote($a_ass_id,'integer');
440  $res = $ilDB->query($query);
441 
442  return $res->numRows() ? true : false;
443  }
444 
445 
449  private static function lookup($a_id, $a_field)
450  {
451  global $ilDB;
452 
453  $set = $ilDB->query("SELECT ".$a_field." FROM exc_assignment ".
454  " WHERE id = ".$ilDB->quote($a_id, "integer")
455  );
456 
457  $rec = $ilDB->fetchAssoc($set);
458 
459  return $rec[$a_field];
460  }
461 
465  static function lookupTitle($a_id)
466  {
467  return ilExAssignment::lookup($a_id, "title");
468  }
469 
473  static function lookupType($a_id)
474  {
475  return ilExAssignment::lookup($a_id, "type");
476  }
477 
481  function saveAssOrderOfExercise($a_ex_id, $a_order)
482  {
483  global $ilDB;
484 
485  $result_order = array();
486  asort($a_order);
487  $nr = 10;
488  foreach ($a_order as $k => $v)
489  {
490  // the check for exc_id is for security reasons. ass ids are unique.
491  $ilDB->manipulate($t = "UPDATE exc_assignment SET ".
492  " order_nr = ".$ilDB->quote($nr, "integer").
493  " WHERE id = ".$ilDB->quote((int) $k, "integer").
494  " AND exc_id = ".$ilDB->quote((int) $a_ex_id, "integer")
495  );
496  $nr+=10;
497  }
498  }
499 
503  function orderAssByDeadline($a_ex_id)
504  {
505  global $ilDB;
506 
507  $set = $ilDB->query("SELECT id FROM exc_assignment ".
508  " WHERE exc_id = ".$ilDB->quote($a_ex_id, "integer").
509  " ORDER BY time_stamp ASC"
510  );
511  $nr = 10;
512  while ($rec = $ilDB->fetchAssoc($set))
513  {
514  $ilDB->manipulate("UPDATE exc_assignment SET ".
515  " order_nr = ".$ilDB->quote($nr, "integer").
516  " WHERE id = ".$ilDB->quote($rec["id"], "integer")
517  );
518  $nr += 10;
519  }
520  }
521 
525  function countMandatory($a_ex_id)
526  {
527  global $ilDB;
528 
529  $set = $ilDB->query("SELECT count(*) cntm FROM exc_assignment ".
530  " WHERE exc_id = ".$ilDB->quote($a_ex_id, "integer").
531  " AND mandatory = ".$ilDB->quote(1, "integer")
532  );
533  $rec = $ilDB->fetchAssoc($set);
534  return $rec["cntm"];
535  }
536 
540 
544  private function lookupAssMemberField($a_ass_id, $a_user_id, $a_field)
545  {
546  global $ilDB;
547 
548  $set = $ilDB->query("SELECT ".$a_field." FROM exc_mem_ass_status ".
549  " WHERE ass_id = ".$ilDB->quote($a_ass_id, "integer").
550  " AND usr_id = ".$ilDB->quote($a_user_id, "integer")
551  );
552  $rec = $ilDB->fetchAssoc($set);
553 
554  return $rec[$a_field];
555  }
556 
560  private function updateAssMemberField($a_ass_id, $a_user_id, $a_field, $a_value, $a_type)
561  {
562  global $ilDB;
563 
564  $ilDB->manipulate("UPDATE exc_mem_ass_status SET ".
565  " ".$a_field." = ".$ilDB->quote($a_value, $a_type).
566  " WHERE ass_id = ".$ilDB->quote($a_ass_id, "integer").
567  " AND usr_id = ".$ilDB->quote($a_user_id, "integer")
568  );
569  }
570 
571 
572 /* function setStatus($a_status)
573  {
574  if(is_array($a_status))
575  {
576  $this->status = $a_status;
577  return true;
578  }
579  }
580  function getStatus()
581  {
582  return $this->status ? $this->status : array();
583  }*/
584 
588  function lookupCommentForUser($a_ass_id, $a_user_id)
589  {
590  return ilExAssignment::lookupAssMemberField($a_ass_id, $a_user_id, "u_comment");
591  }
592 
596  function updateCommentForUser($a_ass_id, $a_user_id, $a_value)
597  {
598  ilExAssignment::updateAssMemberField($a_ass_id, $a_user_id,
599  "u_comment", $a_value, "text");
600  }
601 
605  function lookupMarkOfUser($a_ass_id, $a_user_id)
606  {
607  return ilExAssignment::lookupAssMemberField($a_ass_id, $a_user_id, "mark");
608  }
609 
613  function updateMarkOfUser($a_ass_id, $a_user_id, $a_value)
614  {
615  ilExAssignment::updateAssMemberField($a_ass_id, $a_user_id,
616  "mark", $a_value, "text");
617  }
618 
622  function lookupStatusOfUser($a_ass_id, $a_user_id)
623  {
624  $stat = ilExAssignment::lookupAssMemberField($a_ass_id, $a_user_id, "status");
625  if ($stat == "")
626  {
627  $stat = "notgraded";
628  }
629  return $stat;
630  }
631 
635  function updateStatusOfUser($a_ass_id, $a_user_id, $a_status)
636  {
637  global $ilDB;
638 
639  $ilDB->manipulateF("UPDATE exc_mem_ass_status ".
640  "SET status = %s, status_time= %s ".
641  " WHERE ass_id = %s AND usr_id = %s AND status <> %s ",
642  array("text", "timestamp", "integer", "integer", "text"),
643  array($a_status, ilUtil::now(), $a_ass_id, $a_user_id, $a_status));
644 
645  $ass = new ilExAssignment($a_ass_id);
646  $exc = new ilObjExercise($ass->getExerciseId(), false);
647  $exc->updateUserStatus($a_user_id);
648  }
649 
653  function updateStatusTimeOfUser($a_ass_id, $a_user_id)
654  {
655  ilExAssignment::updateAssMemberField($a_ass_id, $a_user_id,
656  "status_time", ilUtil::now(), "timestamp");
657  }
658 
659 
660  /*function setStatusSent($a_status)
661  {
662  if(is_array($a_status))
663  {
664  $this->status_sent = $a_status;
665  return true;
666  }
667  }
668  function getStatusSent()
669  {
670  return $this->status_sent ? $this->status_sent : array(0 => 0);
671  }*/
672 
676  function lookupStatusSentOfUser($a_ass_id, $a_user_id)
677  {
678  return ilExAssignment::lookupAssMemberField($a_ass_id, $a_user_id, "sent");
679  }
680 
684  function updateStatusSentForUser($a_ass_id, $a_user_id, $a_status)
685  {
686  global $ilDB;
687 
688  $ilDB->manipulateF("UPDATE exc_mem_ass_status ".
689  "SET sent = %s, status_time= %s, sent_time = %s ".
690  " WHERE ass_id = %s AND usr_id = %s ",
691  array("integer", "timestamp", "timestamp", "integer", "integer"),
692  array((int) $a_status, ilUtil::now(), ($a_status ? ilUtil::now() : null),
693  $a_ass_id, $a_user_id));
694  }
695 
696  /*function getStatusReturned()
697  {
698  return $this->status_returned ? $this->status_returned : array(0 => 0);
699  }
700  function setStatusReturned($a_status)
701  {
702  if(is_array($a_status))
703  {
704  $this->status_returned = $a_status;
705  return true;
706  }
707  return false;
708  }*/
709 
713  function lookupStatusReturnedOfUser($a_ass_id, $a_user_id)
714  {
715  return ilExAssignment::lookupAssMemberField($a_ass_id, $a_user_id, "returned");
716  }
717 
721  function updateStatusReturnedForUser($a_ass_id, $a_user_id, $a_status)
722  {
723  global $ilDB;
724 
725  $ilDB->manipulateF("UPDATE exc_mem_ass_status ".
726  "SET returned = %s, status_time= %s ".
727  " WHERE ass_id = %s AND usr_id = %s",
728  array("integer", "timestamp", "integer", "integer"),
729  array((int) $a_status, ilUtil::now(),
730  $a_ass_id, $a_user_id));
731  }
732 
733 /* // feedback functions
734  function setStatusFeedback($a_status)
735  {
736  if(is_array($a_status))
737  {
738  $this->status_feedback = $a_status;
739  return true;
740  }
741  }
742  function getStatusFeedback()
743  {
744  return $this->status_feedback ? $this->status_feedback : array(0 => 0);
745  }*/
746 
750  function lookupStatusFeedbackOfUser($a_ass_id, $a_user_id)
751  {
752  return ilExAssignment::lookupAssMemberField($a_ass_id, $a_user_id, "feedback");
753  }
754 
758  function updateStatusFeedbackForUser($a_ass_id, $a_user_id, $a_status)
759  {
760  global $ilDB;
761 
762  $ilDB->manipulateF("UPDATE exc_mem_ass_status ".
763  "SET feedback = %s, status_time= %s, feedback_time = %s ".
764  " WHERE ass_id = %s AND usr_id = %s",
765  array("integer", "timestamp", "timestamp", "integer", "integer"),
766  array((int) $a_status, ilUtil::now(), ($a_status ? ilUtil::now() : null),
767  $a_ass_id, $a_user_id));
768  }
769 
773  static function lookupSentTimeOfUser($a_ass_id, $a_user_id)
774  {
776  ilExAssignment::lookupAssMemberField($a_ass_id, $a_user_id, "sent_time"));
777  }
778 
782  static function lookupFeedbackTimeOfUser($a_ass_id, $a_user_id)
783  {
785  ilExAssignment::lookupAssMemberField($a_ass_id, $a_user_id, "feedback_time"));
786  }
787 
791  static function lookupStatusTimeOfUser($a_ass_id, $a_user_id)
792  {
794  ilExAssignment::lookupAssMemberField($a_ass_id, $a_user_id, "status_time"));
795  }
796 
797  /*function getNotice()
798  {
799  return $this->notice ? $this->notice : array(0 => 0);
800  }
801 
802  function setNotice($a_notice)
803  {
804  if(is_array($a_notice))
805  {
806  $this->notice = $a_notice;
807  return true;
808  }
809  return false;
810  }*/
811 
815  function lookupNoticeOfUser($a_ass_id, $a_user_id)
816  {
817  return ilExAssignment::lookupAssMemberField($a_ass_id, $a_user_id, "notice");
818  }
819 
823  function hasReturned($a_ass_id, $a_user_id)
824  {
825  global $ilDB;
826 
827  $user_ids = self::getTeamMembersByAssignmentId($a_ass_id, $a_user_id);
828  if(!$user_ids)
829  {
830  $user_ids = array($a_user_id);
831  }
832 
833  $result = $ilDB->query("SELECT returned_id FROM exc_returned".
834  " WHERE ass_id = ".$ilDB->quote($a_ass_id, "integer").
835  " AND ".$ilDB->in("user_id", $user_ids, "", "integer"));
836  return $ilDB->numRows($result);
837  }
838 
842  function getAllDeliveredFiles($a_exc_id, $a_ass_id)
843  {
844  global $ilDB;
845 
846  include_once("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
847  $fs = new ilFSStorageExercise($a_exc_id, $a_ass_id);
848 
849  $query = "SELECT * FROM exc_returned WHERE ass_id = ".
850  $ilDB->quote($a_ass_id, "integer");
851 
852  $res = $ilDB->query($query);
853  while($row = $ilDB->fetchAssoc($res))
854  {
855  $row["timestamp"] = $row["ts"];
856  $row["filename"] = $fs->getAbsoluteSubmissionPath().
857  "/".$row["user_id"]."/".basename($row["filename"]);
858  $delivered[] = $row;
859  }
860 
861  //$delivered = ilObjExercise::_fixFilenameArray($delivered);
862 
863  return $delivered ? $delivered : array();
864  }
865 
869  function getDeliveredFiles($a_exc_id, $a_ass_id, $a_user_id, $a_filter_empty_filename = false)
870  {
871  global $ilDB;
872 
873  $user_ids = self::getTeamMembersByAssignmentId($a_ass_id, $a_user_id);
874  if(!$user_ids)
875  {
876  $user_ids = array($a_user_id);
877  }
878 
879  include_once("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
880  $fs = new ilFSStorageExercise($a_exc_id, $a_ass_id);
881 
882  $result = $ilDB->query("SELECT * FROM exc_returned".
883  " WHERE ass_id = ".$ilDB->quote($a_ass_id, "integer").
884  " AND ".$ilDB->in("user_id", $user_ids, "", "integer"));
885 
886  $delivered_files = array();
887  if ($ilDB->numRows($result))
888  {
889  while ($row = $ilDB->fetchAssoc($result))
890  {
891  if($a_filter_empty_filename && !$row["filename"])
892  {
893  continue;
894  }
895  $row["owner_id"] = $row["user_id"];
896  $row["timestamp"] = $row["ts"];
897  $row["timestamp14"] = substr($row["ts"], 0, 4).
898  substr($row["ts"], 5, 2).substr($row["ts"], 8, 2).
899  substr($row["ts"], 11, 2).substr($row["ts"], 14, 2).
900  substr($row["ts"], 17, 2);
901  $row["filename"] = $fs->getAbsoluteSubmissionPath().
902  "/".$row["user_id"]."/".basename($row["filename"]);
903  array_push($delivered_files, $row);
904  }
905  }
906 
907  //$delivered_files = ilObjExercise::_fixFilenameArray($delivered_files);
908  return $delivered_files;
909  }
910 
914  function deleteDeliveredFiles($a_exc_id, $a_ass_id, $file_id_array, $a_user_id)
915  {
916  global $ilDB;
917 
918  include_once("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
919  $fs = new ilFSStorageExercise($a_exc_id, $a_ass_id);
920 
921  if (count($file_id_array))
922  {
923  $team_id = self::getTeamIdByAssignment($a_ass_id, $a_user_id);
924  if($team_id)
925  {
926  // #11733
927  $user_ids = self::getTeamMembersByAssignmentId($a_ass_id, $a_user_id);
928  if(!$user_ids)
929  {
930  return;
931  }
932  }
933  else
934  {
935  $user_ids = array($a_user_id);
936  }
937 
938  $result = $ilDB->query("SELECT * FROM exc_returned".
939  " WHERE ".$ilDB->in("returned_id", $file_id_array, false, "integer").
940  " AND ".$ilDB->in("user_id", $user_ids, "", "integer"));
941 
942  if ($ilDB->numRows($result))
943  {
944  $result_array = array();
945  while ($row = $ilDB->fetchAssoc($result))
946  {
947  $row["timestamp"] = $row["ts"];
948  array_push($result_array, $row);
949  }
950 
951  // delete the entries in the database
952  $ilDB->manipulate("DELETE FROM exc_returned".
953  " WHERE ".$ilDB->in("returned_id", $file_id_array, false, "integer").
954  " AND ".$ilDB->in("user_id", $user_ids, "", "integer"));
955 
956  // delete the files
957  foreach ($result_array as $key => $value)
958  {
959  if($value["filename"])
960  {
961  if($team_id)
962  {
964  ilExAssignment::TEAM_LOG_REMOVE_FILE, $value["filetitle"]);
965  }
966 
967  $filename = $fs->getAbsoluteSubmissionPath().
968  "/".$value["user_id"]."/".basename($value["filename"]);
969  unlink($filename);
970  }
971  }
972  }
973  }
974  }
975 
982  static function deleteAllDeliveredFilesOfUser($a_exc_id, $a_user_id)
983  {
984  global $ilDB;
985 
986  include_once("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
987 
988  $delete_ids = array();
989 
990  // get the files and...
991  $set = $ilDB->query("SELECT * FROM exc_returned ".
992  " WHERE obj_id = ".$ilDB->quote($a_exc_id, "integer").
993  " AND user_id = ".$ilDB->quote($a_user_id, "integer")
994  );
995  while ($rec = $ilDB->fetchAssoc($set))
996  {
997  $ass = new self($rec["ass_id"]);
998  if($ass->getType() == self::TYPE_UPLOAD_TEAM)
999  {
1000  // switch upload to other team member
1001  $team = self::getTeamMembersByAssignmentId($ass->getId(), $a_user_id);
1002  if(sizeof($team) > 1)
1003  {
1004  $new_owner = array_pop($team);
1005  while($new_owner == $a_user_id && sizeof($team))
1006  {
1007  $new_owner = array_pop($team);
1008  }
1009 
1010  $ilDB->manipulate("UPDATE exc_returned".
1011  " SET user_id = ".$ilDB->quote($new_owner, "integer").
1012  " WHERE returned_id = ".$ilDB->quote($rec["returned_id"], "integer")
1013  );
1014 
1015  // no need to delete
1016  continue;
1017  }
1018  }
1019 
1020  $delete_ids[] = $rec["returned_id"];
1021 
1022  $fs = new ilFSStorageExercise($a_exc_id, $rec["ass_id"]);
1023 
1024  // ...delete files
1025  $filename = $fs->getAbsoluteSubmissionPath().
1026  "/".$a_user_id."/".basename($rec["filename"]);
1027  if (is_file($filename))
1028  {
1029  unlink($filename);
1030  }
1031  }
1032 
1033  // delete exc_returned records
1034  if($delete_ids)
1035  {
1036  $ilDB->manipulate("DELETE FROM exc_returned".
1037  " WHERE ".$ilDB->in("returned_id", $delete_ids, "", "integer"));
1038  }
1039 
1040  // delete il_exc_team records
1041  $ass_ids = array();
1042  foreach(self::getAssignmentDataOfExercise($a_exc_id) as $item)
1043  {
1044  self::updateStatusOfUser($item["id"], $a_user_id, "notgraded"); // #14900
1045 
1046  $ass_ids[] = $item["id"];
1047  }
1048  if($ass_ids)
1049  {
1050  $ilDB->manipulate($d = "DELETE FROM il_exc_team WHERE ".
1051  "user_id = ".$ilDB->quote($a_user_id, "integer").
1052  " AND ".$ilDB->in("ass_id", $ass_ids, "", "integer")
1053  );
1054  }
1055  }
1056 
1057 
1061  function deliverReturnedFiles($a_exc_id, $a_ass_id, $a_user_id, $a_only_new = false)
1062  {
1063  global $ilUser, $ilDB;
1064 
1065  // #11000 / #11785
1066  $is_team = true;
1067  $user_ids = self::getTeamMembersByAssignmentId($a_ass_id, $a_user_id);
1068  if(!$user_ids)
1069  {
1070  $is_team = false;
1071  $user_ids = array($a_user_id);
1072  }
1073 
1074  // get last download time
1075  $and_str = "";
1076  if ($a_only_new)
1077  {
1078  $q = "SELECT download_time FROM exc_usr_tutor WHERE ".
1079  " ass_id = ".$ilDB->quote($a_ass_id, "integer")." AND ".
1080  $ilDB->in("usr_id", $user_ids, "", "integer")." AND ".
1081  " tutor_id = ".$ilDB->quote($ilUser->getId(), "integer");
1082  $lu_set = $ilDB->query($q);
1083  if ($lu_rec = $ilDB->fetchAssoc($lu_set))
1084  {
1085  if ($lu_rec["download_time"] > 0)
1086  {
1087  $and_str = " AND ts > ".$ilDB->quote($lu_rec["download_time"], "timestamp");
1088  }
1089  }
1090  }
1091 
1092  foreach($user_ids as $user_id)
1093  {
1094  ilExAssignment::updateTutorDownloadTime($a_exc_id, $a_ass_id, $user_id);
1095  }
1096 
1097  $query = "SELECT * FROM exc_returned".
1098  " WHERE ass_id = ".$ilDB->quote($a_ass_id, "integer").
1099  " AND ".$ilDB->in("user_id", $user_ids, "", "integer").
1100  $and_str;
1101 
1102  $result = $ilDB->query($query);
1103  $count = $ilDB->numRows($result);
1104  if ($count == 1)
1105  {
1106  $row = $ilDB->fetchAssoc($result);
1107 
1108  switch(self::lookupType($a_ass_id))
1109  {
1110  case self::TYPE_BLOG:
1111  case self::TYPE_PORTFOLIO:
1112  $row["filetitle"] = ilObjUser::_lookupName($row["user_id"]);
1113  $row["filetitle"] = ilObject::_lookupTitle($a_exc_id)." - ".
1114  self::lookupTitle($a_ass_id)." - ".
1115  $row["filetitle"]["firstname"]." ".
1116  $row["filetitle"]["lastname"]." (".
1117  $row["filetitle"]["login"].").zip";
1118  break;
1119 
1120  default:
1121  break;
1122  }
1123 
1124  ilExAssignment::downloadSingleFile($a_exc_id, $a_ass_id, $row["user_id"],
1125  $row["filename"], $row["filetitle"]);
1126  }
1127  else if ($count > 0)
1128  {
1129  $array_files = array();
1130  while ($row = $ilDB->fetchAssoc($result))
1131  {
1132  $array_files[$row["user_id"]][] = basename($row["filename"]);
1133  }
1134  ilExAssignment::downloadMultipleFiles($a_exc_id, $a_ass_id, $array_files,
1135  ($is_team ? null : $a_user_id), $is_team);
1136  }
1137  else
1138  {
1139  return false;
1140  }
1141 
1142  return true;
1143  }
1144 
1145  // Update the timestamp of the last download of current user (=tutor)
1149  function updateTutorDownloadTime($a_exc_id, $a_ass_id, $a_user_id)
1150  {
1151  global $ilUser, $ilDB;
1152 
1153  $ilDB->manipulateF("DELETE FROM exc_usr_tutor ".
1154  "WHERE ass_id = %s AND usr_id = %s AND tutor_id = %s",
1155  array("integer", "integer", "integer"),
1156  array($a_ass_id, $a_user_id, $ilUser->getId()));
1157 
1158  $ilDB->manipulateF("INSERT INTO exc_usr_tutor (ass_id, obj_id, usr_id, tutor_id, download_time) VALUES ".
1159  "(%s, %s, %s, %s, %s)",
1160  array("integer", "integer", "integer", "integer", "timestamp"),
1161  array($a_ass_id, $a_exc_id, $a_user_id, $ilUser->getId(), ilUtil::now()));
1162  }
1163 
1167  function downloadSelectedFiles($a_exc_id, $a_ass_id, $a_user_id, $array_file_id)
1168  {
1169  global $ilDB;
1170 
1171  if (count($array_file_id))
1172  {
1173  // #11785
1174  $is_team = true;
1175  $user_ids = self::getTeamMembersByAssignmentId($a_ass_id, $a_user_id);
1176  if(!$user_ids)
1177  {
1178  $is_team = false;
1179  $user_ids = array($a_user_id);
1180  }
1181 
1182  $result = $ilDB->query("SELECT * FROM exc_returned WHERE ".
1183  $ilDB->in("returned_id", $array_file_id, false, "integer").
1184  " AND ".$ilDB->in("user_id", $user_ids, "", "integer"));
1185  if ($ilDB->numRows($result))
1186  {
1187  $array_found = array();
1188  while ($row = $ilDB->fetchAssoc($result))
1189  {
1190  $row["timestamp"] = $row["ts"];
1191  array_push($array_found, $row);
1192  }
1193  if (count($array_found) == 1)
1194  {
1195  // blog/portfolio submission
1196  if(is_numeric($array_found[0]["filetitle"]))
1197  {
1198  $ass = new ilExAssignment($array_found[0]["ass_id"]);
1199  if($ass->getType() == ilExAssignment::TYPE_BLOG ||
1200  $ass->getType() == ilExAssignment::TYPE_PORTFOLIO)
1201  {
1202  $user_data = ilObjUser::_lookupName($array_found[0]["user_id"]);
1203  $array_found[0]["filetitle"] = ilObject::_lookupTitle($array_found[0]["obj_id"])." - ".
1204  $ass->getTitle()." - ".
1205  $user_data["firstname"]." ".
1206  $user_data["lastname"]." (".
1207  $user_data["login"].").zip";
1208  }
1209  }
1210 
1211  ilExAssignment::downloadSingleFile($a_exc_id, $a_ass_id, $array_found[0]["user_id"],
1212  $array_found[0]["filename"], $array_found[0]["filetitle"]);
1213  }
1214  else
1215  {
1216  $filenames = array();
1217  foreach ($array_found as $value)
1218  {
1219  $filenames[$value["user_id"]][] = basename($value["filename"]);
1220  }
1221  ilExAssignment::downloadMultipleFiles($a_exc_id, $a_ass_id,
1222  $filenames, ($is_team ? null : $a_user_id), $is_team);
1223  }
1224  }
1225  }
1226  }
1227 
1231  function downloadSingleFile($a_exc_id, $a_ass_id, $a_user_id, $filename, $filetitle)
1232  {
1233  include_once("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
1234  $fs = new ilFSStorageExercise($a_exc_id, $a_ass_id);
1235 
1236  $filename = $fs->getAbsoluteSubmissionPath().
1237  "/".$a_user_id."/".basename($filename);
1238 
1239  require_once "./Services/Utilities/classes/class.ilUtil.php";
1240  ilUtil::deliverFile($filename, $filetitle);
1241  }
1242 
1246 // @todo: check whether files of multiple users are downloaded this way
1247  function downloadMultipleFiles($a_exc_id, $a_ass_id, $array_filenames,
1248  $a_user_id, $a_multi_user = false)
1249  {
1250  include_once("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
1251  $fs = new ilFSStorageExercise($a_exc_id, $a_ass_id);
1252 
1253  require_once "./Services/Utilities/classes/class.ilUtil.php";
1254  $cdir = getcwd();
1255 
1256  $zip = PATH_TO_ZIP;
1257  $tmpdir = ilUtil::ilTempnam();
1258  $tmpfile = ilUtil::ilTempnam();
1259  $tmpzipfile = $tmpfile . ".zip";
1260 
1261  ilUtil::makeDir($tmpdir);
1262  chdir($tmpdir);
1263 
1264  $assTitle = ilExAssignment::lookupTitle($a_ass_id);
1265  $deliverFilename = str_replace(" ", "_", $assTitle);
1266  if ($a_user_id > 0 && !$a_multi_user)
1267  {
1268  $userName = ilObjUser::_lookupName($a_user_id);
1269  $deliverFilename .= "_".$userName["lastname"]."_".$userName["firstname"];
1270  }
1271  else
1272  {
1273  $deliverFilename .= "_files";
1274  }
1275  $orgDeliverFilename = trim($deliverFilename);
1276  $deliverFilename = ilUtil::getASCIIFilename($orgDeliverFilename);
1277  ilUtil::makeDir($tmpdir."/".$deliverFilename);
1278  chdir($tmpdir."/".$deliverFilename);
1279 
1280  //copy all files to a temporary directory and remove them afterwards
1281  $parsed_files = $duplicates = array();
1282  foreach ($array_filenames as $user_id => $files)
1283  {
1284  $pathname = $fs->getAbsoluteSubmissionPath()."/".$user_id;
1285 
1286  foreach($files as $filename)
1287  {
1288  // remove timestamp
1289  $newFilename = trim($filename);
1290  $pos = strpos($newFilename , "_");
1291  if ($pos !== false)
1292  {
1293  $newFilename= substr($newFilename, $pos + 1);
1294  }
1295  // #11070
1296  $chkName = strtolower($newFilename);
1297  if(array_key_exists($chkName, $duplicates))
1298  {
1299  $suffix = strrpos($newFilename, ".");
1300  $newFilename = substr($newFilename, 0, $suffix).
1301  " (".(++$duplicates[$chkName]).")".
1302  substr($newFilename, $suffix);
1303  }
1304  else
1305  {
1306  $duplicates[$chkName] = 1;
1307  }
1308  $newFilename = $tmpdir.DIRECTORY_SEPARATOR.$deliverFilename.DIRECTORY_SEPARATOR.$newFilename;
1309  // copy to temporal directory
1310  $oldFilename = $pathname.DIRECTORY_SEPARATOR.$filename;
1311  if (!copy ($oldFilename, $newFilename))
1312  {
1313  echo 'Could not copy '.$oldFilename.' to '.$newFilename;
1314  }
1315  touch($newFilename, filectime($oldFilename));
1316  $parsed_files[] = ilUtil::escapeShellArg($deliverFilename.DIRECTORY_SEPARATOR.basename($newFilename));
1317  }
1318  }
1319 
1320  chdir($tmpdir);
1321  $zipcmd = $zip." ".ilUtil::escapeShellArg($tmpzipfile)." ".join($parsed_files, " ");
1322 //echo getcwd()."<br>";
1323 //echo $zipcmd;
1324  exec($zipcmd);
1325  ilUtil::delDir($tmpdir);
1326 
1327  chdir($cdir);
1328  ilUtil::deliverFile($tmpzipfile, $orgDeliverFilename.".zip", "", false, true);
1329  exit;
1330  }
1331 
1337  function downloadAllDeliveredFiles($a_exc_id, $a_ass_id, $members)
1338  {
1339  global $lng, $ilObjDataCache, $ilias;
1340 
1341  include_once "./Services/Utilities/classes/class.ilUtil.php";
1342  include_once("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
1343 
1344  $storage = new ilFSStorageExercise($a_exc_id, $a_ass_id);
1345  $storage->create();
1346 
1347  ksort($members);
1348  //$savepath = $this->getExercisePath() . "/" . $this->obj_id . "/";
1349  $savepath = $storage->getAbsoluteSubmissionPath();
1350  $cdir = getcwd();
1351 
1352 
1353  // important check: if the directory does not exist
1354  // ILIAS stays in the current directory (echoing only a warning)
1355  // and the zip command below archives the whole ILIAS directory
1356  // (including the data directory) and sends a mega file to the user :-o
1357  if (!is_dir($savepath))
1358  {
1359  return;
1360  }
1361  // Safe mode fix
1362 // chdir($this->getExercisePath());
1363  chdir($storage->getTempPath());
1364  $zip = PATH_TO_ZIP;
1365 
1366  // check first, if we have enough free disk space to copy all files to temporary directory
1367  $tmpdir = ilUtil::ilTempnam();
1368  ilUtil::makeDir($tmpdir);
1369  chdir($tmpdir);
1370 
1371 
1372  $dirsize = 0;
1373  foreach ($members as $id => $object) {
1374  $directory = $savepath.DIRECTORY_SEPARATOR.$id;
1375  $dirsize += ilUtil::dirsize($directory);
1376  }
1377  if ($dirsize > disk_free_space($tmpdir)) {
1378  return -1;
1379  }
1380 
1381  $ass_type = self::lookupType($a_ass_id);
1382 
1383  // copy all member directories to the temporary folder
1384  // switch from id to member name and append the login if the member name is double
1385  // ensure that no illegal filenames will be created
1386  // remove timestamp from filename
1387  $cache = array();
1388  foreach ($members as $id => $user)
1389  {
1390  $sourcedir = $savepath.DIRECTORY_SEPARATOR.$id;
1391  if (!is_dir($sourcedir))
1392  continue;
1393  $userName = ilObjUser::_lookupName($id);
1394  $directory = ilUtil::getASCIIFilename(trim($userName["lastname"])."_".trim($userName["firstname"]));
1395  if (array_key_exists($directory, $cache))
1396  {
1397  // first try is to append the login;
1398  $directory = ilUtil::getASCIIFilename($directory."_".trim(ilObjUser::_lookupLogin($id)));
1399  if (array_key_exists($directory, $cache)) {
1400  // second and secure: append the user id as well.
1401  $directory .= "_".$id;
1402  }
1403  }
1404 
1405  $cache[$directory] = $directory;
1406  ilUtil::makeDir ($directory);
1407  $sourcefiles = scandir($sourcedir);
1408  $duplicates = array();
1409  foreach ($sourcefiles as $sourcefile) {
1410  if ($sourcefile == "." || $sourcefile == "..")
1411  {
1412  continue;
1413  }
1414 
1415  $targetfile = trim(basename($sourcefile));
1416  $pos = strpos($targetfile, "_");
1417  if ($pos !== false)
1418  {
1419  $targetfile= substr($targetfile, $pos + 1);
1420  }
1421 
1422  // #14536
1423  if(array_key_exists($targetfile, $duplicates))
1424  {
1425  $suffix = strrpos($targetfile, ".");
1426  $targetfile = substr($targetfile, 0, $suffix).
1427  " (".(++$duplicates[$targetfile]).")".
1428  substr($targetfile, $suffix);
1429  }
1430  else
1431  {
1432  $duplicates[$targetfile] = 1;
1433  }
1434 
1435  $targetfile = $directory.DIRECTORY_SEPARATOR.$targetfile;
1436  $sourcefile = $sourcedir.DIRECTORY_SEPARATOR.$sourcefile;
1437 
1438  if (!copy ($sourcefile, $targetfile))
1439  {
1440  //echo 'Could not copy '.$sourcefile.' to '.$targetfile;
1441  $ilias->raiseError('Could not copy '.basename($sourcefile)." to '".$targetfile."'.",
1442  $ilias->error_obj->MESSAGE);
1443  }
1444  else
1445  {
1446  // preserve time stamp
1447  touch($targetfile, filectime($sourcefile));
1448 
1449  // blogs and portfolios are stored as zip and have to be unzipped
1450  if($ass_type == ilExAssignment::TYPE_PORTFOLIO ||
1451  $ass_type == ilExAssignment::TYPE_BLOG)
1452  {
1453  ilUtil::unzip($targetfile);
1454  unlink($targetfile);
1455  }
1456  }
1457 
1458  }
1459  }
1460 
1461  $tmpfile = ilUtil::ilTempnam();
1462  $tmpzipfile = $tmpfile . ".zip";
1463  // Safe mode fix
1464  $zipcmd = $zip." -r ".ilUtil::escapeShellArg($tmpzipfile)." .";
1465  exec($zipcmd);
1466  ilUtil::delDir($tmpdir);
1467 
1468  $assTitle = ilExAssignment::lookupTitle($a_ass_id);
1469  chdir($cdir);
1470  ilUtil::deliverFile($tmpzipfile, (strlen($assTitle) == 0
1471  ? strtolower($lng->txt("exc_assignment"))
1472  : $assTitle). ".zip", "", false, true);
1473  }
1474 
1478  function updateNoticeForUser($a_ass_id, $a_user_id, $a_notice)
1479  {
1480  global $ilDB;
1481 
1482  // #12181
1483  $ilDB->manipulate("UPDATE exc_mem_ass_status".
1484  " SET notice = ".$ilDB->quote($a_notice, "text").
1485  ",status_time= ".$ilDB->quote(ilUtil::now(), "timestamp").
1486  " WHERE ass_id = ".$ilDB->quote($a_ass_id, "integer").
1487  " AND usr_id = ".$ilDB->quote($a_user_id, "integer").
1488  " AND ".$ilDB->equalsNot("notice", $a_notice, "text", true));
1489  }
1490 
1494  function _getReturned($a_ass_id)
1495  {
1496  global $ilDB;
1497 
1498  $query = "SELECT DISTINCT(usr_id) as ud FROM exc_mem_ass_status ".
1499  "WHERE ass_id = ".$ilDB->quote($a_ass_id, "integer")." ".
1500  "AND returned = 1";
1501 
1502  $res = $ilDB->query($query);
1503  while($row = $ilDB->fetchObject($res))
1504  {
1505  $usr_ids[] = $row->ud;
1506  }
1507 
1508  return $usr_ids ? $usr_ids : array();
1509  }
1510 
1518  static function getLastSubmission($a_ass_id, $a_user_id)
1519  {
1520  global $ilDB, $lng;
1521 
1522  // team upload?
1523  $user_ids = self::getTeamMembersByAssignmentId($a_ass_id, $a_user_id);
1524  if(!$user_ids)
1525  {
1526  $user_ids = array($a_user_id);
1527  }
1528 
1529  $ilDB->setLimit(1);
1530 
1531  $q = "SELECT obj_id,user_id,ts FROM exc_returned".
1532  " WHERE ass_id = ".$ilDB->quote($a_ass_id, "integer").
1533  " AND ".$ilDB->in("user_id", $user_ids, "", "integer").
1534  " ORDER BY ts DESC";
1535 
1536  $usr_set = $ilDB->query($q);
1537 
1538  $array = $ilDB->fetchAssoc($usr_set);
1539  if ($array["ts"]==NULL)
1540  {
1541  return false;
1542  }
1543  else
1544  {
1545  return ilUtil::getMySQLTimestamp($array["ts"]);
1546  }
1547  }
1548 
1552  static function lookupAnyExerciseSent($a_exc_id, $a_ass_id)
1553  {
1554  global $ilDB;
1555 
1556  $q = "SELECT count(*) AS cnt FROM exc_mem_ass_status".
1557  " WHERE NOT sent_time IS NULL".
1558  " AND ass_id = ".$ilDB->quote($a_ass_id, "integer")." ".
1559  " ";
1560  $set = $ilDB->query($q);
1561  $rec = $ilDB->fetchAssoc($set);
1562 
1563  if ($rec["cnt"] > 0)
1564  {
1565  return true;
1566  }
1567  else
1568  {
1569  return false;
1570  }
1571  }
1572 
1577  static function lookupUpdatedSubmission($ass_id, $member_id)
1578  {
1579  global $ilDB, $lng;
1580 
1581  // team upload?
1582  $user_ids = self::getTeamMembersByAssignmentId($ass_id, $member_id);
1583  if(!$user_ids)
1584  {
1585  $user_ids = array($member_id);
1586  }
1587 
1588  $q="SELECT exc_mem_ass_status.status_time, exc_returned.ts ".
1589  "FROM exc_mem_ass_status, exc_returned ".
1590  "WHERE exc_mem_ass_status.status_time < exc_returned.ts ".
1591  "AND NOT exc_mem_ass_status.status_time IS NULL ".
1592  "AND exc_returned.ass_id = exc_mem_ass_status.ass_id ".
1593  "AND exc_returned.user_id = exc_mem_ass_status.usr_id ".
1594  "AND exc_returned.ass_id=".$ilDB->quote($ass_id, "integer").
1595  " AND ".$ilDB->in("exc_returned.user_id", $user_ids, "", "integer");
1596 
1597  $usr_set = $ilDB->query($q);
1598 
1599  $array = $ilDB->fetchAssoc($usr_set);
1600 
1601  if (count($array)==0)
1602  {
1603  return 0;
1604  }
1605  else
1606  {
1607  return 1;
1608  }
1609 
1610  }
1611 
1616  static function lookupNewFiles($ass_id, $member_id)
1617  {
1618  global $ilDB, $ilUser;
1619 
1620  // team upload?
1621  $user_ids = self::getTeamMembersByAssignmentId($ass_id, $member_id);
1622  if(!$user_ids)
1623  {
1624  $user_ids = array($member_id);
1625  }
1626 
1627  $q = "SELECT exc_returned.returned_id AS id ".
1628  "FROM exc_usr_tutor, exc_returned ".
1629  "WHERE exc_returned.ass_id = exc_usr_tutor.ass_id ".
1630  " AND exc_returned.user_id = exc_usr_tutor.usr_id ".
1631  " AND exc_returned.ass_id = ".$ilDB->quote($ass_id, "integer").
1632  " AND ".$ilDB->in("exc_returned.user_id", $user_ids, "", "integer").
1633  " AND exc_usr_tutor.tutor_id = ".$ilDB->quote($ilUser->getId(), "integer").
1634  " AND exc_usr_tutor.download_time < exc_returned.ts ";
1635 
1636  $new_up_set = $ilDB->query($q);
1637 
1638  $new_up = array();
1639  while ($new_up_rec = $ilDB->fetchAssoc($new_up_set))
1640  {
1641  $new_up[] = $new_up_rec["id"];
1642  }
1643 
1644  return $new_up;
1645  }
1646 
1650  function getMemberListData($a_exc_id, $a_ass_id)
1651  {
1652  global $ilDB;
1653 
1654  $mem = array();
1655 
1656  // first get list of members from member table
1657  $set = $ilDB->query("SELECT * FROM exc_members ".
1658  "WHERE obj_id = ".$ilDB->quote($a_exc_id, "integer"));
1659  while($rec = $ilDB->fetchAssoc($set))
1660  {
1661  if (ilObject::_exists($rec["usr_id"]) &&
1662  (ilObject::_lookupType($rec["usr_id"]) == "usr"))
1663  {
1664  $name = ilObjUser::_lookupName($rec["usr_id"]);
1665  $login = ilObjUser::_lookupLogin($rec["usr_id"]);
1666  $mem[$rec["usr_id"]] =
1667  array(
1668  "name" => $name["lastname"].", ".$name["firstname"],
1669  "login" => $login,
1670  "usr_id" => $rec["usr_id"],
1671  "lastname" => $name["lastname"],
1672  "firstname" => $name["firstname"]
1673  );
1674  }
1675  }
1676 
1677  $q = "SELECT * FROM exc_mem_ass_status ".
1678  "WHERE ass_id = ".$ilDB->quote($a_ass_id, "integer");
1679  $set = $ilDB->query($q);
1680  while($rec = $ilDB->fetchAssoc($set))
1681  {
1682  if (isset($mem[$rec["usr_id"]]))
1683  {
1684  $mem[$rec["usr_id"]]["sent_time"] = $rec["sent_time"];
1685  $mem[$rec["usr_id"]]["submission"] = ilExAssignment::getLastSubmission($a_ass_id, $rec["usr_id"]);
1686  $mem[$rec["usr_id"]]["status_time"] = $rec["status_time"];
1687  $mem[$rec["usr_id"]]["feedback_time"] = $rec["feedback_time"];
1688  $mem[$rec["usr_id"]]["notice"] = $rec["notice"];
1689  $mem[$rec["usr_id"]]["status"] = $rec["status"];
1690  }
1691  }
1692  return $mem;
1693  }
1694 
1698  static function createNewUserRecords($a_user_id, $a_exc_id)
1699  {
1700  global $ilDB;
1701 
1702  $ass_data = ilExAssignment::getAssignmentDataOfExercise($a_exc_id);
1703  foreach ($ass_data as $ass)
1704  {
1705 //echo "-".$ass["id"]."-".$a_user_id."-";
1706  $ilDB->replace("exc_mem_ass_status", array(
1707  "ass_id" => array("integer", $ass["id"]),
1708  "usr_id" => array("integer", $a_user_id)
1709  ), array(
1710  "status" => array("text", "notgraded")
1711  ));
1712  }
1713  }
1714 
1718  static function createNewAssignmentRecords($a_ass_id, $a_exc)
1719  {
1720  global $ilDB;
1721 
1722  include_once("./Modules/Exercise/classes/class.ilExerciseMembers.php");
1723  $exmem = new ilExerciseMembers($a_exc);
1724  $mems = $exmem->getMembers();
1725 
1726  foreach ($mems as $mem)
1727  {
1728  $ilDB->replace("exc_mem_ass_status", array(
1729  "ass_id" => array("integer", $a_ass_id),
1730  "usr_id" => array("integer", $mem)
1731  ), array(
1732  "status" => array("text", "notgraded")
1733  ));
1734  }
1735  }
1736 
1741  function uploadAssignmentFiles($a_files)
1742  {
1743  include_once("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
1744  $storage = new ilFSStorageExercise($this->getExerciseId(), $this->getId());
1745  $storage->create();
1746  $storage->uploadAssignmentFiles($a_files);
1747  }
1748 
1749  //
1750  // TEAM UPLOAD
1751  //
1752 
1762  function getTeamId($a_user_id, $a_create_on_demand = false)
1763  {
1764  global $ilDB;
1765 
1766  $sql = "SELECT id FROM il_exc_team".
1767  " WHERE ass_id = ".$ilDB->quote($this->getId(), "integer").
1768  " AND user_id = ".$ilDB->quote($a_user_id, "integer");
1769  $set = $ilDB->query($sql);
1770  $row = $ilDB->fetchAssoc($set);
1771  $id = $row["id"];
1772 
1773  if(!$id && $a_create_on_demand)
1774  {
1775  $id = $ilDB->nextId("il_exc_team");
1776 
1777  $fields = array("id" => array("integer", $id),
1778  "ass_id" => array("integer", $this->getId()),
1779  "user_id" => array("integer", $a_user_id));
1780  $ilDB->insert("il_exc_team", $fields);
1781 
1782  self::writeTeamLog($id, self::TEAM_LOG_CREATE_TEAM);
1783  self::writeTeamLog($id, self::TEAM_LOG_ADD_MEMBER,
1784  ilObjUser::_lookupFullname($a_user_id));
1785  }
1786 
1787  return $id;
1788  }
1789 
1796  function getTeamMembers($a_team_id)
1797  {
1798  global $ilDB;
1799 
1800  $ids = array();
1801 
1802  $sql = "SELECT user_id".
1803  " FROM il_exc_team".
1804  " WHERE ass_id = ".$ilDB->quote($this->getId(), "integer").
1805  " AND id = ".$ilDB->quote($a_team_id, "integer");
1806  $set = $ilDB->query($sql);
1807  while($row = $ilDB->fetchAssoc($set))
1808  {
1809  $ids[] = $row["user_id"];
1810  }
1811 
1812  return $ids;
1813  }
1814 
1821  {
1822  global $ilDB;
1823 
1824  $ids = array();
1825 
1826  $sql = "SELECT user_id".
1827  " FROM il_exc_team".
1828  " WHERE ass_id = ".$ilDB->quote($this->getId(), "integer");
1829  $set = $ilDB->query($sql);
1830  while($row = $ilDB->fetchAssoc($set))
1831  {
1832  $ids[] = $row["user_id"];
1833  }
1834 
1835  return $ids;
1836  }
1837 
1845  function addTeamMember($a_team_id, $a_user_id, $a_exc_ref_id)
1846  {
1847  global $ilDB;
1848 
1849  $members = $this->getTeamMembers($a_team_id);
1850  if(!in_array($a_user_id, $members))
1851  {
1852  $fields = array("id" => array("integer", $a_team_id),
1853  "ass_id" => array("integer", $this->getId()),
1854  "user_id" => array("integer", $a_user_id));
1855  $ilDB->insert("il_exc_team", $fields);
1856 
1857  $this->sendNotification($a_exc_ref_id, $a_user_id, "add");
1858 
1859  self::writeTeamLog($a_team_id, self::TEAM_LOG_ADD_MEMBER,
1860  ilObjUser::_lookupFullname($a_user_id));
1861  }
1862  }
1863 
1871  function removeTeamMember($a_team_id, $a_user_id, $a_exc_ref_id)
1872  {
1873  global $ilDB;
1874 
1875  $sql = "DELETE FROM il_exc_team".
1876  " WHERE ass_id = ".$ilDB->quote($this->getId(), "integer").
1877  " AND id = ".$ilDB->quote($a_team_id, "integer").
1878  " AND user_id = ".$ilDB->quote($a_user_id, "integer");
1879  $ilDB->manipulate($sql);
1880 
1881  $this->sendNotification($a_exc_ref_id, $a_user_id, "rmv");
1882 
1883  self::writeTeamLog($a_team_id, self::TEAM_LOG_REMOVE_MEMBER,
1884  ilObjUser::_lookupFullname($a_user_id));
1885  }
1886 
1894  public static function getTeamMembersByAssignmentId($a_ass_id, $a_user_id)
1895  {
1896  global $ilDB;
1897 
1898  $ids = array();
1899 
1900  $team_id = self::getTeamIdByAssignment($a_ass_id, $a_user_id);
1901  if($team_id)
1902  {
1903  $set = $ilDB->query("SELECT user_id".
1904  " FROM il_exc_team".
1905  " WHERE ass_id = ".$ilDB->quote($a_ass_id, "integer").
1906  " AND id = ". $ilDB->quote($team_id, "integer"));
1907  while($row = $ilDB->fetchAssoc($set))
1908  {
1909  $ids[] = $row["user_id"];
1910  }
1911  }
1912 
1913  return $ids;
1914  }
1915 
1923  public static function getTeamIdByAssignment($a_ass_id, $a_user_id)
1924  {
1925  global $ilDB;
1926 
1927  $result = $ilDB->query("SELECT type".
1928  " FROM exc_assignment".
1929  " WHERE id = ".$ilDB->quote($a_ass_id, "integer"));
1930  $type = $ilDB->fetchAssoc($result);
1931 
1932  if($type["type"] == self::TYPE_UPLOAD_TEAM)
1933  {
1934  $set = $ilDB->query("SELECT id".
1935  " FROM il_exc_team".
1936  " WHERE ass_id = ".$ilDB->quote($a_ass_id, "integer").
1937  " AND user_id = ".$ilDB->quote($a_user_id, "integer"));
1938  $team_id = $ilDB->fetchAssoc($set);
1939  return $team_id["id"];
1940  }
1941  }
1942 
1949  public static function getAssignmentTeamMap($a_ass_id)
1950  {
1951  global $ilDB;
1952 
1953  $map = array();
1954 
1955  $sql = "SELECT * FROM il_exc_team".
1956  " WHERE ass_id = ".$ilDB->quote($a_ass_id, "integer");
1957  $set = $ilDB->query($sql);
1958  while($row = $ilDB->fetchAssoc($set))
1959  {
1960  $map[$row["user_id"]] = $row["id"];
1961  }
1962 
1963  return $map;
1964  }
1965 
1973  public static function writeTeamLog($a_team_id, $a_action, $a_details = null)
1974  {
1975  global $ilDB, $ilUser;
1976 
1977  $fields = array(
1978  "team_id" => array("integer", $a_team_id),
1979  "user_id" => array("integer", $ilUser->getId()),
1980  "action" => array("integer", $a_action),
1981  "details" => array("text", $a_details),
1982  "tstamp" => array("integer", time())
1983  );
1984 
1985  $ilDB->insert("il_exc_team_log", $fields);
1986  }
1987 
1994  public static function getTeamLog($a_team_id)
1995  {
1996  global $ilDB;
1997 
1998  $res = array();
1999 
2000  $sql = "SELECT * FROM il_exc_team_log".
2001  " WHERE team_id = ".$ilDB->quote($a_team_id, "integer").
2002  " ORDER BY tstamp DESC";
2003  $set = $ilDB->query($sql);
2004  while($row = $ilDB->fetchAssoc($set))
2005  {
2006  $res[] = $row;
2007  }
2008  return $res;
2009  }
2010 
2018  public function sendNotification($a_exc_ref_id, $a_user_id, $a_action)
2019  {
2020  global $ilUser;
2021 
2022  // no need to notify current user
2023  if($ilUser->getId() == $a_user_id)
2024  {
2025  return;
2026  }
2027 
2028  include_once "./Services/Mail/classes/class.ilMail.php";
2029  include_once "./Services/User/classes/class.ilObjUser.php";
2030  include_once "./Services/Language/classes/class.ilLanguageFactory.php";
2031  include_once("./Services/User/classes/class.ilUserUtil.php");
2032  include_once("./Services/Link/classes/class.ilLink.php");
2033 
2034  $link = ilLink::_getStaticLink($a_exc_ref_id);
2035 
2036  // use language of recipient to compose message
2037  $ulng = ilLanguageFactory::_getLanguageOfUser($a_user_id);
2038  $ulng->loadLanguageModule('exc');
2039 
2040  $subject = sprintf($ulng->txt('exc_team_notification_subject_'.$a_action), $this->getTitle());
2041  $message = sprintf($ulng->txt('exc_team_notification_salutation'), ilObjUser::_lookupFullname($a_user_id))."\n\n";
2042 
2043  $message .= $ulng->txt('exc_team_notification_body_'.$a_action)."\n\n";
2044  $message .= $ulng->txt('obj_exc').": ".ilObject::_lookupTitle($this->getExerciseId())."\n";
2045  $message .= $ulng->txt('exc_assignment').": ".$this->getTitle()."\n";
2046  $message .= $ulng->txt('exc_team_notification_changed_by').": ".ilUserUtil::getNamePresentation($ilUser->getId())."\n\n";
2047  $message .= $ulng->txt('exc_team_notification_link').": ".$link;
2048 
2049  $mail_obj = new ilMail(ANONYMOUS_USER_ID);
2050  $mail_obj->appendInstallationSignature(true);
2051  $ret = $mail_obj->sendMail(ilObjUser::_lookupLogin($a_user_id),
2052  "", "", $subject, $message, array(), array("system"));
2053 
2054  // var_dump($ret);
2055  }
2056 }
2057 
2058 ?>