ILIAS  Release_4_2_x_branch Revision 61807
 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 
20  function __construct($a_id = 0)
21  {
22  $this->setType(self::TYPE_UPLOAD);
23 
24  if ($a_id > 0)
25  {
26  $this->setId($a_id);
27  $this->read();
28  }
29  }
30 
36  function setId($a_val)
37  {
38  $this->id = $a_val;
39  }
40 
46  function getId()
47  {
48  return $this->id;
49  }
50 
56  function setExerciseId($a_val)
57  {
58  $this->exc_id = $a_val;
59  }
60 
66  function getExerciseId()
67  {
68  return $this->exc_id;
69  }
70 
76  function setStartTime($a_val)
77  {
78  $this->start_time = $a_val;
79  }
80 
86  function getStartTime()
87  {
88  return $this->start_time;
89  }
90 
96  function setDeadline($a_val)
97  {
98  $this->deadline = $a_val;
99  }
100 
106  function getDeadline()
107  {
108  return $this->deadline;
109  }
110 
116  function setInstruction($a_val)
117  {
118  $this->instruction = $a_val;
119  }
120 
126  function getInstruction()
127  {
128  return $this->instruction;
129  }
130 
136  function setTitle($a_val)
137  {
138  $this->title = $a_val;
139  }
140 
146  function getTitle()
147  {
148  return $this->title;
149  }
150 
156  function setMandatory($a_val)
157  {
158  $this->mandatory = $a_val;
159  }
160 
166  function getMandatory()
167  {
168  return $this->mandatory;
169  }
170 
176  function setOrderNr($a_val)
177  {
178  $this->order_nr = $a_val;
179  }
180 
186  function getOrderNr()
187  {
188  return $this->order_nr;
189  }
190 
196  function setType($a_value)
197  {
198  if($this->isValidType($a_value))
199  {
200  $this->type = (int)$a_value;
201  }
202  }
203 
209  function getType()
210  {
211  return $this->type;
212  }
213 
220  function isValidType($a_value)
221  {
222  if(in_array((int)$a_value, array(self::TYPE_UPLOAD, self::TYPE_BLOG, self::TYPE_PORTFOLIO)))
223  {
224  return true;
225  }
226  return false;
227  }
228 
232  function read()
233  {
234  global $ilDB;
235 
236  $set = $ilDB->query("SELECT * FROM exc_assignment ".
237  " WHERE id = ".$ilDB->quote($this->getId(), "integer")
238  );
239  while ($rec = $ilDB->fetchAssoc($set))
240  {
241  $this->setExerciseId($rec["exc_id"]);
242  $this->setDeadline($rec["time_stamp"]);
243  $this->setInstruction($rec["instruction"]);
244  $this->setTitle($rec["title"]);
245  $this->setStartTime($rec["start_time"]);
246  $this->setOrderNr($rec["order_nr"]);
247  $this->setMandatory($rec["mandatory"]);
248  $this->setType($rec["type"]);
249  }
250  }
251 
255  function save()
256  {
257  global $ilDB;
258 
259  if ($this->getOrderNr() == 0)
260  {
261  $this->setOrderNr(
263  + 10);
264  }
265 
266  $next_id = $ilDB->nextId("exc_assignment");
267  $ilDB->insert("exc_assignment", array(
268  "id" => array("integer", $next_id),
269  "exc_id" => array("integer", $this->getExerciseId()),
270  "time_stamp" => array("integer", $this->getDeadline()),
271  "instruction" => array("clob", $this->getInstruction()),
272  "title" => array("text", $this->getTitle()),
273  "start_time" => array("integer", $this->getStartTime()),
274  "order_nr" => array("integer", $this->getOrderNr()),
275  "mandatory" => array("integer", $this->getMandatory()),
276  "type" => array("integer", $this->getType())
277  ));
278  $this->setId($next_id);
279  $exc = new ilObjExercise($this->getExerciseId(), false);
280  $exc->updateAllUsersStatus();
282  }
283 
287  function update()
288  {
289  global $ilDB;
290 
291  $ilDB->update("exc_assignment",
292  array(
293  "exc_id" => array("integer", $this->getExerciseId()),
294  "time_stamp" => array("integer", $this->getDeadline()),
295  "instruction" => array("clob", $this->getInstruction()),
296  "title" => array("text", $this->getTitle()),
297  "start_time" => array("integer", $this->getStartTime()),
298  "order_nr" => array("integer", $this->getOrderNr()),
299  "mandatory" => array("integer", $this->getMandatory()),
300  "type" => array("integer", $this->getType())
301  ),
302  array(
303  "id" => array("integer", $this->getId()),
304  ));
305  $exc = new ilObjExercise($this->getExerciseId(), false);
306  $exc->updateAllUsersStatus();
307  }
308 
312  function delete()
313  {
314  global $ilDB;
315 
316  $ilDB->manipulate("DELETE FROM exc_assignment WHERE ".
317  " id = ".$ilDB->quote($this->getId(), "integer")
318  );
319  $exc = new ilObjExercise($this->getExerciseId(), false);
320  $exc->updateAllUsersStatus();
321  }
322 
323 
327  static function getAssignmentDataOfExercise($a_exc_id)
328  {
329  global $ilDB;
330 
331  $set = $ilDB->query("SELECT * FROM exc_assignment ".
332  " WHERE exc_id = ".$ilDB->quote($a_exc_id, "integer").
333  " ORDER BY order_nr ASC");
334  $data = array();
335 
336  $order_val = 10;
337  while ($rec = $ilDB->fetchAssoc($set))
338  {
339 
340  $data[] = array(
341  "id" => $rec["id"],
342  "exc_id" => $rec["exc_id"],
343  "deadline" => $rec["time_stamp"],
344  "instruction" => $rec["instruction"],
345  "title" => $rec["title"],
346  "start_time" => $rec["start_time"],
347  "order_val" => $order_val,
348  "mandatory" => $rec["mandatory"],
349  "type" => $rec["type"]
350  );
351  $order_val += 10;
352  }
353  return $data;
354  }
355 
362  function cloneAssignmentsOfExercise($a_old_exc_id, $a_new_exc_id)
363  {
364  $ass_data = ilExAssignment::getAssignmentDataOfExercise($a_old_exc_id);
365  foreach ($ass_data as $d)
366  {
367  // clone assignment
368  $new_ass = new ilExAssignment();
369  $new_ass->setExerciseId($a_new_exc_id);
370  $new_ass->setTitle($d["title"]);
371  $new_ass->setDeadline($d["deadline"]);
372  $new_ass->setInstruction($d["instruction"]);
373  $new_ass->setMandatory($d["mandatory"]);
374  $new_ass->setOrderNr($d["order_val"]);
375  $new_ass->setStartTime($d["start_time"]);
376  $new_ass->setType($d["type"]);
377  $new_ass->save();
378 
379  // clone assignment files
380  include_once("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
381  $old_storage = new ilFSStorageExercise($a_old_exc_id, (int) $d["id"]);
382  $new_storage = new ilFSStorageExercise($a_new_exc_id, (int) $new_ass->getId());
383  $new_storage->create();
384 
385  if (is_dir($old_storage->getPath()))
386  {
387  ilUtil::rCopy($old_storage->getPath(), $new_storage->getPath());
388  }
389  }
390  }
391 
395  static function getFiles($a_exc_id, $a_ass_id)
396  {
397  include_once("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
398  $storage = new ilFSStorageExercise($a_exc_id, $a_ass_id);
399  return $storage->getFiles();
400  }
401 
405  static function lookupMaxOrderNrForEx($a_exc_id)
406  {
407  global $ilDB;
408 
409  $set = $ilDB->query("SELECT MAX(order_nr) mnr FROM exc_assignment ".
410  " WHERE exc_id = ".$ilDB->quote($a_exc_id, "integer")
411  );
412  while ($rec = $ilDB->fetchAssoc($set))
413  {
414  return (int) $rec["mnr"];
415  }
416  return 0;
417  }
418 
424  public static function lookupAssignmentOnline($a_ass_id)
425  {
426  global $ilDB;
427 
428  $query = "SELECT id FROM exc_assignment ".
429  "WHERE start_time <= ".$ilDB->quote(time(),'integer').' '.
430  "AND time_stamp >= ".$ilDB->quote(time(),'integer').' '.
431  "AND id = ".$ilDB->quote($a_ass_id,'integer');
432  $res = $ilDB->query($query);
433 
434  return $res->numRows() ? true : false;
435  }
436 
437 
441  private static function lookup($a_id, $a_field)
442  {
443  global $ilDB;
444 
445  $set = $ilDB->query("SELECT ".$a_field." FROM exc_assignment ".
446  " WHERE id = ".$ilDB->quote($a_id, "integer")
447  );
448 
449  $rec = $ilDB->fetchAssoc($set);
450 
451  return $rec[$a_field];
452  }
453 
457  static function lookupTitle($a_id)
458  {
459  return ilExAssignment::lookup($a_id, "title");
460  }
461 
465  static function lookupType($a_id)
466  {
467  return ilExAssignment::lookup($a_id, "type");
468  }
469 
473  function saveAssOrderOfExercise($a_ex_id, $a_order)
474  {
475  global $ilDB;
476 
477  $result_order = array();
478  asort($a_order);
479  $nr = 10;
480  foreach ($a_order as $k => $v)
481  {
482  // the check for exc_id is for security reasons. ass ids are unique.
483  $ilDB->manipulate($t = "UPDATE exc_assignment SET ".
484  " order_nr = ".$ilDB->quote($nr, "integer").
485  " WHERE id = ".$ilDB->quote((int) $k, "integer").
486  " AND exc_id = ".$ilDB->quote((int) $a_ex_id, "integer")
487  );
488  $nr+=10;
489  }
490  }
491 
495  function orderAssByDeadline($a_ex_id)
496  {
497  global $ilDB;
498 
499  $set = $ilDB->query("SELECT id FROM exc_assignment ".
500  " WHERE exc_id = ".$ilDB->quote($a_ex_id, "integer").
501  " ORDER BY time_stamp ASC"
502  );
503  $nr = 10;
504  while ($rec = $ilDB->fetchAssoc($set))
505  {
506  $ilDB->manipulate("UPDATE exc_assignment SET ".
507  " order_nr = ".$ilDB->quote($nr, "integer").
508  " WHERE id = ".$ilDB->quote($rec["id"], "integer")
509  );
510  $nr += 10;
511  }
512  }
513 
517  function countMandatory($a_ex_id)
518  {
519  global $ilDB;
520 
521  $set = $ilDB->query("SELECT count(*) cntm FROM exc_assignment ".
522  " WHERE exc_id = ".$ilDB->quote($a_ex_id, "integer").
523  " AND mandatory = ".$ilDB->quote(1, "integer")
524  );
525  $rec = $ilDB->fetchAssoc($set);
526  return $rec["cntm"];
527  }
528 
532 
536  private function lookupAssMemberField($a_ass_id, $a_user_id, $a_field)
537  {
538  global $ilDB;
539 
540  $set = $ilDB->query("SELECT ".$a_field." FROM exc_mem_ass_status ".
541  " WHERE ass_id = ".$ilDB->quote($a_ass_id, "integer").
542  " AND usr_id = ".$ilDB->quote($a_user_id, "integer")
543  );
544  $rec = $ilDB->fetchAssoc($set);
545 
546  return $rec[$a_field];
547  }
548 
552  private function updateAssMemberField($a_ass_id, $a_user_id, $a_field, $a_value, $a_type)
553  {
554  global $ilDB;
555 
556  $ilDB->manipulate("UPDATE exc_mem_ass_status SET ".
557  " ".$a_field." = ".$ilDB->quote($a_value, $a_type).
558  " WHERE ass_id = ".$ilDB->quote($a_ass_id, "integer").
559  " AND usr_id = ".$ilDB->quote($a_user_id, "integer")
560  );
561  }
562 
563 
564 /* function setStatus($a_status)
565  {
566  if(is_array($a_status))
567  {
568  $this->status = $a_status;
569  return true;
570  }
571  }
572  function getStatus()
573  {
574  return $this->status ? $this->status : array();
575  }*/
576 
580  function lookupCommentForUser($a_ass_id, $a_user_id)
581  {
582  return ilExAssignment::lookupAssMemberField($a_ass_id, $a_user_id, "u_comment");
583  }
584 
588  function updateCommentForUser($a_ass_id, $a_user_id, $a_value)
589  {
590  ilExAssignment::updateAssMemberField($a_ass_id, $a_user_id,
591  "u_comment", $a_value, "text");
592  }
593 
597  function lookupMarkOfUser($a_ass_id, $a_user_id)
598  {
599  return ilExAssignment::lookupAssMemberField($a_ass_id, $a_user_id, "mark");
600  }
601 
605  function updateMarkOfUser($a_ass_id, $a_user_id, $a_value)
606  {
607  ilExAssignment::updateAssMemberField($a_ass_id, $a_user_id,
608  "mark", $a_value, "text");
609  }
610 
614  function lookupStatusOfUser($a_ass_id, $a_user_id)
615  {
616  $stat = ilExAssignment::lookupAssMemberField($a_ass_id, $a_user_id, "status");
617  if ($stat == "")
618  {
619  $stat = "notgraded";
620  }
621  return $stat;
622  }
623 
627  function updateStatusOfUser($a_ass_id, $a_user_id, $a_status)
628  {
629  global $ilDB;
630 
631  $ilDB->manipulateF("UPDATE exc_mem_ass_status ".
632  "SET status = %s, status_time= %s ".
633  " WHERE ass_id = %s AND usr_id = %s AND status <> %s ",
634  array("text", "timestamp", "integer", "integer", "text"),
635  array($a_status, ilUtil::now(), $a_ass_id, $a_user_id, $a_status));
636 
637  $ass = new ilExAssignment($a_ass_id);
638  $exc = new ilObjExercise($ass->getExerciseId(), false);
639  $exc->updateUserStatus($a_user_id);
640  }
641 
645  function updateStatusTimeOfUser($a_ass_id, $a_user_id)
646  {
647  ilExAssignment::updateAssMemberField($a_ass_id, $a_user_id,
648  "status_time", ilUtil::now(), "timestamp");
649  }
650 
651 
652  /*function setStatusSent($a_status)
653  {
654  if(is_array($a_status))
655  {
656  $this->status_sent = $a_status;
657  return true;
658  }
659  }
660  function getStatusSent()
661  {
662  return $this->status_sent ? $this->status_sent : array(0 => 0);
663  }*/
664 
668  function lookupStatusSentOfUser($a_ass_id, $a_user_id)
669  {
670  return ilExAssignment::lookupAssMemberField($a_ass_id, $a_user_id, "sent");
671  }
672 
676  function updateStatusSentForUser($a_ass_id, $a_user_id, $a_status)
677  {
678  global $ilDB;
679 
680  $ilDB->manipulateF("UPDATE exc_mem_ass_status ".
681  "SET sent = %s, status_time= %s, sent_time = %s ".
682  " WHERE ass_id = %s AND usr_id = %s ",
683  array("integer", "timestamp", "timestamp", "integer", "integer"),
684  array((int) $a_status, ilUtil::now(), ($a_status ? ilUtil::now() : null),
685  $a_ass_id, $a_user_id));
686  }
687 
688  /*function getStatusReturned()
689  {
690  return $this->status_returned ? $this->status_returned : array(0 => 0);
691  }
692  function setStatusReturned($a_status)
693  {
694  if(is_array($a_status))
695  {
696  $this->status_returned = $a_status;
697  return true;
698  }
699  return false;
700  }*/
701 
705  function lookupStatusReturnedOfUser($a_ass_id, $a_user_id)
706  {
707  return ilExAssignment::lookupAssMemberField($a_ass_id, $a_user_id, "returned");
708  }
709 
713  function updateStatusReturnedForUser($a_ass_id, $a_user_id, $a_status)
714  {
715  global $ilDB;
716 
717  $ilDB->manipulateF("UPDATE exc_mem_ass_status ".
718  "SET returned = %s, status_time= %s ".
719  " WHERE ass_id = %s AND usr_id = %s",
720  array("integer", "timestamp", "integer", "integer"),
721  array((int) $a_status, ilUtil::now(),
722  $a_ass_id, $a_user_id));
723  }
724 
725 /* // feedback functions
726  function setStatusFeedback($a_status)
727  {
728  if(is_array($a_status))
729  {
730  $this->status_feedback = $a_status;
731  return true;
732  }
733  }
734  function getStatusFeedback()
735  {
736  return $this->status_feedback ? $this->status_feedback : array(0 => 0);
737  }*/
738 
742  function lookupStatusFeedbackOfUser($a_ass_id, $a_user_id)
743  {
744  return ilExAssignment::lookupAssMemberField($a_ass_id, $a_user_id, "feedback");
745  }
746 
750  function updateStatusFeedbackForUser($a_ass_id, $a_user_id, $a_status)
751  {
752  global $ilDB;
753 
754  $ilDB->manipulateF("UPDATE exc_mem_ass_status ".
755  "SET feedback = %s, status_time= %s, feedback_time = %s ".
756  " WHERE ass_id = %s AND usr_id = %s",
757  array("integer", "timestamp", "timestamp", "integer", "integer"),
758  array((int) $a_status, ilUtil::now(), ($a_status ? ilUtil::now() : null),
759  $a_ass_id, $a_user_id));
760  }
761 
765  static function lookupSentTimeOfUser($a_ass_id, $a_user_id)
766  {
768  ilExAssignment::lookupAssMemberField($a_ass_id, $a_user_id, "sent_time"));
769  }
770 
774  static function lookupFeedbackTimeOfUser($a_ass_id, $a_user_id)
775  {
777  ilExAssignment::lookupAssMemberField($a_ass_id, $a_user_id, "feedback_time"));
778  }
779 
783  static function lookupStatusTimeOfUser($a_ass_id, $a_user_id)
784  {
786  ilExAssignment::lookupAssMemberField($a_ass_id, $a_user_id, "status_time"));
787  }
788 
789  /*function getNotice()
790  {
791  return $this->notice ? $this->notice : array(0 => 0);
792  }
793 
794  function setNotice($a_notice)
795  {
796  if(is_array($a_notice))
797  {
798  $this->notice = $a_notice;
799  return true;
800  }
801  return false;
802  }*/
803 
807  function lookupNoticeOfUser($a_ass_id, $a_user_id)
808  {
809  return ilExAssignment::lookupAssMemberField($a_ass_id, $a_user_id, "notice");
810  }
811 
815  function hasReturned($a_ass_id, $a_user_id)
816  {
817  global $ilDB;
818 
819  $result = $ilDB->queryF("SELECT returned_id FROM exc_returned WHERE ass_id = %s AND user_id = %s",
820  array("integer", "integer"),
821  array($ass_id, $a_user_id));
822  return $ilDB->numRows($result);
823  }
824 
825 
829  function getAllDeliveredFiles($a_exc_id, $a_ass_id)
830  {
831  global $ilDB;
832 
833  include_once("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
834  $fs = new ilFSStorageExercise($a_exc_id, $a_ass_id);
835 
836  $query = "SELECT * FROM exc_returned WHERE ass_id = ".
837  $ilDB->quote($a_ass_id, "integer");
838 
839  $res = $ilDB->query($query);
840  while($row = $ilDB->fetchAssoc($res))
841  {
842  $row["timestamp"] = $row["ts"];
843  $row["filename"] = $fs->getAbsoluteSubmissionPath().
844  "/".$row["user_id"]."/".basename($row["filename"]);
845  $delivered[] = $row;
846  }
847 
848  //$delivered = ilObjExercise::_fixFilenameArray($delivered);
849 
850  return $delivered ? $delivered : array();
851  }
852 
856  function getDeliveredFiles($a_exc_id, $a_ass_id, $a_user_id, $a_filter_empty_filename = false)
857  {
858  global $ilDB;
859 
860  include_once("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
861  $fs = new ilFSStorageExercise($a_exc_id, $a_ass_id);
862 
863  $result = $ilDB->queryF("SELECT * FROM exc_returned WHERE ass_id = %s AND user_id = %s ORDER BY ts",
864  array("integer", "integer"),
865  array($a_ass_id, $a_user_id));
866 
867  $delivered_files = array();
868  if ($ilDB->numRows($result))
869  {
870  while ($row = $ilDB->fetchAssoc($result))
871  {
872  if($a_filter_empty_filename && !$row["filename"])
873  {
874  continue;
875  }
876  $row["timestamp"] = $row["ts"];
877  $row["timestamp14"] = substr($row["ts"], 0, 4).
878  substr($row["ts"], 5, 2).substr($row["ts"], 8, 2).
879  substr($row["ts"], 11, 2).substr($row["ts"], 14, 2).
880  substr($row["ts"], 17, 2);
881  $row["filename"] = $fs->getAbsoluteSubmissionPath().
882  "/".$row["user_id"]."/".basename($row["filename"]);
883  array_push($delivered_files, $row);
884  }
885  }
886 
887  //$delivered_files = ilObjExercise::_fixFilenameArray($delivered_files);
888  return $delivered_files;
889  }
890 
894  function deleteDeliveredFiles($a_exc_id, $a_ass_id, $file_id_array, $a_user_id)
895  {
896  global $ilDB;
897 
898  include_once("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
899  $fs = new ilFSStorageExercise($a_exc_id, $a_ass_id);
900 
901  if (count($file_id_array))
902  {
903  $result = $ilDB->query("SELECT * FROM exc_returned WHERE user_id = ".
904  $ilDB->quote($a_user_id, "integer")." AND ".
905  $ilDB->in("returned_id", $file_id_array, false, "integer"));
906 
907  if ($ilDB->numRows($result))
908  {
909  $result_array = array();
910  while ($row = $ilDB->fetchAssoc($result))
911  {
912  $row["timestamp"] = $row["ts"];
913  array_push($result_array, $row);
914  }
915  // delete the entries in the database
916  $ilDB->manipulate("DELETE FROM exc_returned WHERE user_id = ".
917  $ilDB->quote($a_user_id, "integer")." AND ".
918  $ilDB->in("returned_id", $file_id_array, false, "integer"));
919  //returned_id IN ("
920  //.implode(ilUtil::quoteArray($file_id_array) ,",").")",
921  //$this->ilias->db->quote($a_member_id . "")
922 
923  // delete the files
924  foreach ($result_array as $key => $value)
925  {
926  if($value["filename"])
927  {
928  $filename = $fs->getAbsoluteSubmissionPath().
929  "/".$value["user_id"]."/".basename($value["filename"]);
930  unlink($filename);
931  }
932  }
933  }
934  }
935  }
936 
943  static function deleteAllDeliveredFilesOfUser($a_exc_id, $a_user_id)
944  {
945  global $ilDB;
946 
947  include_once("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
948 
949  // get the files and...
950  $set = $ilDB->query("SELECT * FROM exc_returned ".
951  " WHERE obj_id = ".$ilDB->quote($a_exc_id, "integer").
952  " AND user_id = ".$ilDB->quote($a_user_id, "integer")
953  );
954  while ($rec = $ilDB->fetchAssoc($set))
955  {
956  $fs = new ilFSStorageExercise($a_exc_id, $rec["ass_id"]);
957 
958  // ...delete files
959  $filename = $fs->getAbsoluteSubmissionPath().
960  "/".$a_user_id."/".basename($rec["filename"]);
961  if (is_file($filename))
962  {
963  unlink($filename);
964  }
965  }
966  // delete exc_returned records
967  $ilDB->manipulate($d = "DELETE FROM exc_returned WHERE ".
968  " obj_id = ".$ilDB->quote($a_exc_id, "integer").
969  " AND user_id = ".$ilDB->quote($a_user_id, "integer")
970  );
971  }
972 
973 
977  function deliverReturnedFiles($a_exc_id, $a_ass_id, $a_user_id, $a_only_new = false)
978  {
979  global $ilUser, $ilDB;
980 
981  // get last download time
982  $and_str = "";
983  if ($a_only_new)
984  {
985  $q = "SELECT download_time FROM exc_usr_tutor WHERE ".
986  " ass_id = ".$ilDB->quote($a_ass_id, "integer")." AND ".
987  " usr_id = ".$ilDB->quote($a_user_id, "integer")." AND ".
988  " tutor_id = ".$ilDB->quote($ilUser->getId(), "integer");
989  $lu_set = $ilDB->query($q);
990  if ($lu_rec = $ilDB->fetchAssoc($lu_set))
991  {
992  if ($lu_rec["download_time"] > 0)
993  {
994  $and_str = " AND ts > ".$ilDB->quote($lu_rec["download_time"], "timestamp");
995  }
996  }
997  }
998 
999  ilExAssignment::updateTutorDownloadTime($a_exc_id, $a_ass_id, $a_user_id);
1000 
1001  $query = sprintf("SELECT * FROM exc_returned WHERE ass_id = %s AND user_id = %s".
1002  $and_str,
1003  $ilDB->quote($a_ass_id, "integer"),
1004  $ilDB->quote($a_user_id, "integer"));
1005  $result = $ilDB->query($query);
1006  $count = $ilDB->numRows($result);
1007  if ($count == 1)
1008  {
1009  $row = $ilDB->fetchAssoc($result);
1010 
1011  switch(self::lookupType($a_ass_id))
1012  {
1013  case self::TYPE_BLOG:
1014  case self::TYPE_PORTFOLIO:
1015  $row["filetitle"] = ilObjUser::_lookupName($a_user_id);
1016  $row["filetitle"] = ilObject::_lookupTitle($a_exc_id)." - ".
1017  self::lookupTitle($a_ass_id)." - ".
1018  $row["filetitle"]["firstname"]." ".
1019  $row["filetitle"]["lastname"]." (".
1020  $row["filetitle"]["login"].").zip";
1021  break;
1022 
1023  default:
1024  break;
1025  }
1026 
1027  ilExAssignment::downloadSingleFile($a_exc_id, $a_ass_id, $a_user_id,
1028  $row["filename"], $row["filetitle"]);
1029  }
1030  else if ($count > 0)
1031  {
1032  $array_files = array();
1033  $filename = "";
1034  while ($row = $ilDB->fetchAssoc($result))
1035  {
1036  array_push($array_files, basename($row["filename"]));
1037 // $filename = $row["filename"];
1038 // $pathinfo = pathinfo($filename);
1039 // $dir = $pathinfo["dirname"];
1040 // $file = $pathinfo["basename"];
1041 // array_push($array_files, $file);
1042  }
1043  $pathinfo = pathinfo($filename);
1044  $dir = $pathinfo["dirname"];
1045 
1046  ilExAssignment::downloadMultipleFiles($a_exc_id, $a_ass_id, $array_files, $a_user_id);
1047  }
1048  else
1049  {
1050  return false;
1051  }
1052 
1053  return true;
1054  }
1055 
1056  // Update the timestamp of the last download of current user (=tutor)
1060  function updateTutorDownloadTime($a_exc_id, $a_ass_id, $a_user_id)
1061  {
1062  global $ilUser, $ilDB;
1063 
1064  $ilDB->manipulateF("DELETE FROM exc_usr_tutor ".
1065  "WHERE ass_id = %s AND usr_id = %s AND tutor_id = %s",
1066  array("integer", "integer", "integer"),
1067  array($a_ass_id, $a_user_id, $ilUser->getId()));
1068 
1069  $ilDB->manipulateF("INSERT INTO exc_usr_tutor (ass_id, obj_id, usr_id, tutor_id, download_time) VALUES ".
1070  "(%s, %s, %s, %s, %s)",
1071  array("integer", "integer", "integer", "integer", "timestamp"),
1072  array($a_ass_id, $a_exc_id, $a_user_id, $ilUser->getId(), ilUtil::now()));
1073  }
1074 
1078  function downloadSelectedFiles($a_exc_id, $a_ass_id, $a_user_id, $array_file_id)
1079  {
1080  global $ilDB;
1081 
1082  if (count($array_file_id))
1083  {
1084  $result = $ilDB->query("SELECT * FROM exc_returned WHERE ".
1085  $ilDB->in("returned_id", $array_file_id, false, "integer").
1086  " AND user_id = ".$ilDB->quote($a_user_id));
1087  if ($ilDB->numRows($result))
1088  {
1089  $array_found = array();
1090  while ($row = $ilDB->fetchAssoc($result))
1091  {
1092  $row["timestamp"] = $row["ts"];
1093  array_push($array_found, $row);
1094  }
1095  if (count($array_found) == 1)
1096  {
1097  // blog/portfolio submission
1098  if(is_numeric($array_found[0]["filetitle"]))
1099  {
1100  $ass = new ilExAssignment($array_found[0]["ass_id"]);
1101  if($ass->getType() == ilExAssignment::TYPE_BLOG ||
1102  $ass->getType() == ilExAssignment::TYPE_PORTFOLIO)
1103  {
1104  $user_data = ilObjUser::_lookupName($array_found[0]["user_id"]);
1105  $array_found[0]["filetitle"] = ilObject::_lookupTitle($array_found[0]["obj_id"])." - ".
1106  $ass->getTitle()." - ".
1107  $user_data["firstname"]." ".
1108  $user_data["lastname"]." (".
1109  $user_data["login"].").zip";
1110  }
1111  }
1112 
1113  ilExAssignment::downloadSingleFile($a_exc_id, $a_ass_id, $a_user_id,
1114  $array_found[0]["filename"], $array_found[0]["filetitle"]);
1115  }
1116  else
1117  {
1118  $filenames = array();
1119  $dir = "";
1120  $file = "";
1121  foreach ($array_found as $key => $value)
1122  {
1123  //$pathinfo = pathinfo(ilObjExercise::_fixFilename($value["filename"]));
1124  //$dir = $pathinfo["dirname"];
1125  //$file = $pathinfo["basename"];
1126  //array_push($filenames, $file);
1127  array_push($filenames, basename($value["filename"]));
1128  }
1129  ilExAssignment::downloadMultipleFiles($a_exc_id, $a_ass_id,
1130  $filenames, $a_user_id);
1131  }
1132  }
1133  }
1134  }
1135 
1139  function downloadSingleFile($a_exc_id, $a_ass_id, $a_user_id, $filename, $filetitle)
1140  {
1141  include_once("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
1142  $fs = new ilFSStorageExercise($a_exc_id, $a_ass_id);
1143 
1144  $filename = $fs->getAbsoluteSubmissionPath().
1145  "/".$a_user_id."/".basename($filename);
1146 
1147  require_once "./Services/Utilities/classes/class.ilUtil.php";
1148  ilUtil::deliverFile($filename, $filetitle);
1149  }
1150 
1154 // @todo: check whether files of multiple users are downloaded this way
1155  function downloadMultipleFiles($a_exc_id, $a_ass_id, $array_filenames,
1156  $a_user_id)
1157  {
1158  global $lng, $ilObjDataCache;
1159 
1160  include_once("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
1161  $fs = new ilFSStorageExercise($a_exc_id, $a_ass_id);
1162  $pathname = $fs->getAbsoluteSubmissionPath().
1163  "/".$a_user_id;
1164 
1165  require_once "./Services/Utilities/classes/class.ilUtil.php";
1166  $cdir = getcwd();
1167 
1168  $zip = PATH_TO_ZIP;
1169  $tmpdir = ilUtil::ilTempnam();
1170  $tmpfile = ilUtil::ilTempnam();
1171  $tmpzipfile = $tmpfile . ".zip";
1172 
1173  ilUtil::makeDir($tmpdir);
1174  chdir($tmpdir);
1175 
1176  $assTitle = ilExAssignment::lookupTitle($a_ass_id);
1177  $deliverFilename = str_replace(" ", "_", $assTitle);
1178  if ($a_user_id > 0)
1179  {
1180  $userName = ilObjUser::_lookupName($a_user_id);
1181  $deliverFilename .= "_".$userName["lastname"]."_".$userName["firstname"];
1182  }
1183  else
1184  {
1185  $deliverFilename .= "_files";
1186  }
1187  $orgDeliverFilename = trim($deliverFilename);
1188  $deliverFilename = ilUtil::getASCIIFilename($orgDeliverFilename);
1189  ilUtil::makeDir($tmpdir."/".$deliverFilename);
1190  chdir($tmpdir."/".$deliverFilename);
1191 
1192  //copy all files to a temporary directory and remove them afterwards
1193  foreach ($array_filenames as $key => $filename)
1194  {
1195  // remove timestamp
1196  $newFilename = trim($filename);
1197  $pos = strpos($newFilename , "_");
1198  if ($pos === false)
1199  {
1200  } else
1201  {
1202  $newFilename= substr($newFilename, $pos + 1);
1203  }
1204  $newFilename = $tmpdir.DIRECTORY_SEPARATOR.$deliverFilename.DIRECTORY_SEPARATOR.$newFilename;
1205  // copy to temporal directory
1206  $oldFilename = $pathname.DIRECTORY_SEPARATOR.$filename;
1207  if (!copy ($oldFilename, $newFilename))
1208  {
1209  echo 'Could not copy '.$oldFilename.' to '.$newFilename;
1210  }
1211  touch($newFilename, filectime($oldFilename));
1212  $array_filenames[$key] = ilUtil::escapeShellArg($deliverFilename.DIRECTORY_SEPARATOR.basename($newFilename)); //$array_filenames[$key]);
1213  }
1214  chdir($tmpdir);
1215  $zipcmd = $zip." ".ilUtil::escapeShellArg($tmpzipfile)." ".join($array_filenames, " ");
1216 //echo getcwd()."<br>";
1217 //echo $zipcmd;
1218  exec($zipcmd);
1219  ilUtil::delDir($tmpdir);
1220 
1221  chdir($cdir);
1222  ilUtil::deliverFile($tmpzipfile, $orgDeliverFilename.".zip", "", false, true);
1223  exit;
1224  }
1225 
1231  function downloadAllDeliveredFiles($a_exc_id, $a_ass_id, $members)
1232  {
1233  global $lng, $ilObjDataCache, $ilias;
1234 
1235  include_once "./Services/Utilities/classes/class.ilUtil.php";
1236  include_once("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
1237 
1238  $storage = new ilFSStorageExercise($a_exc_id, $a_ass_id);
1239  $storage->create();
1240 
1241  ksort($members);
1242  //$savepath = $this->getExercisePath() . "/" . $this->obj_id . "/";
1243  $savepath = $storage->getAbsoluteSubmissionPath();
1244  $cdir = getcwd();
1245 
1246 
1247  // important check: if the directory does not exist
1248  // ILIAS stays in the current directory (echoing only a warning)
1249  // and the zip command below archives the whole ILIAS directory
1250  // (including the data directory) and sends a mega file to the user :-o
1251  if (!is_dir($savepath))
1252  {
1253  return;
1254  }
1255  // Safe mode fix
1256 // chdir($this->getExercisePath());
1257  chdir($storage->getTempPath());
1258  $zip = PATH_TO_ZIP;
1259 
1260  // check first, if we have enough free disk space to copy all files to temporary directory
1261  $tmpdir = ilUtil::ilTempnam();
1262  ilUtil::makeDir($tmpdir);
1263  chdir($tmpdir);
1264 
1265 
1266  $dirsize = 0;
1267  foreach ($members as $id => $object) {
1268  $directory = $savepath.DIRECTORY_SEPARATOR.$id;
1269  $dirsize += ilUtil::dirsize($directory);
1270  }
1271  if ($dirsize > disk_free_space($tmpdir)) {
1272  return -1;
1273  }
1274 
1275  $ass_type = self::lookupType($a_ass_id);
1276 
1277  // copy all member directories to the temporary folder
1278  // switch from id to member name and append the login if the member name is double
1279  // ensure that no illegal filenames will be created
1280  // remove timestamp from filename
1281  $cache = array();
1282  foreach ($members as $id => $user)
1283  {
1284  $sourcedir = $savepath.DIRECTORY_SEPARATOR.$id;
1285  if (!is_dir($sourcedir))
1286  continue;
1287  $userName = ilObjUser::_lookupName($id);
1288  $directory = ilUtil::getASCIIFilename(trim($userName["lastname"])."_".trim($userName["firstname"]));
1289  if (array_key_exists($directory, $cache))
1290  {
1291  // first try is to append the login;
1292  $directory = ilUtil::getASCIIFilename($directory."_".trim(ilObjUser::_lookupLogin($id)));
1293  if (array_key_exists($directory, $cache)) {
1294  // second and secure: append the user id as well.
1295  $directory .= "_".$id;
1296  }
1297  }
1298 
1299  $cache[$directory] = $directory;
1300  ilUtil::makeDir ($directory);
1301  $sourcefiles = scandir($sourcedir);
1302  foreach ($sourcefiles as $sourcefile) {
1303  if ($sourcefile == "." || $sourcefile == "..")
1304  {
1305  continue;
1306  }
1307 
1308  $targetfile = trim(basename($sourcefile));
1309  $pos = strpos($targetfile, "_");
1310  if ($pos !== false)
1311  {
1312  $targetfile= substr($targetfile, $pos + 1);
1313  }
1314 
1315  $targetfile = $directory.DIRECTORY_SEPARATOR.$targetfile;
1316  $sourcefile = $sourcedir.DIRECTORY_SEPARATOR.$sourcefile;
1317 
1318  if (!copy ($sourcefile, $targetfile))
1319  {
1320  //echo 'Could not copy '.$sourcefile.' to '.$targetfile;
1321  $ilias->raiseError('Could not copy '.basename($sourcefile)." to '".$targetfile."'.",
1322  $ilias->error_obj->MESSAGE);
1323  }
1324  else
1325  {
1326  // preserve time stamp
1327  touch($targetfile, filectime($sourcefile));
1328 
1329  // blogs and portfolios are stored as zip and have to be unzipped
1330  if($ass_type == ilExAssignment::TYPE_PORTFOLIO ||
1331  $ass_type == ilExAssignment::TYPE_BLOG)
1332  {
1333  ilUtil::unzip($targetfile);
1334  unlink($targetfile);
1335  }
1336  }
1337 
1338  }
1339  }
1340 
1341  $tmpfile = ilUtil::ilTempnam();
1342  $tmpzipfile = $tmpfile . ".zip";
1343  // Safe mode fix
1344  $zipcmd = $zip." -r ".ilUtil::escapeShellArg($tmpzipfile)." .";
1345  exec($zipcmd);
1346  ilUtil::delDir($tmpdir);
1347 
1348  $assTitle = ilExAssignment::lookupTitle($a_ass_id);
1349  chdir($cdir);
1350  ilUtil::deliverFile($tmpzipfile, (strlen($assTitle) == 0
1351  ? strtolower($lng->txt("exc_assignment"))
1352  : $assTitle). ".zip", "", false, true);
1353  }
1354 
1358  function updateNoticeForUser($a_ass_id, $a_user_id, $a_notice)
1359  {
1360  global $ilDB;
1361 
1362  $ilDB->manipulateF("UPDATE exc_mem_ass_status ".
1363  "SET notice = %s, status_time= %s ".
1364  " WHERE ass_id = %s AND usr_id = %s AND ".
1365  $ilDB->equalsNot("notice", $a_notice, "text", true),
1366  array("text", "timestamp", "integer", "integer"),
1367  array($a_notice, ilUtil::now(), $a_ass_id, $a_user_id));
1368 
1369  }
1370 
1374  function _getReturned($a_ass_id)
1375  {
1376  global $ilDB;
1377 
1378  $query = "SELECT DISTINCT(usr_id) as ud FROM exc_mem_ass_status ".
1379  "WHERE ass_id = ".$ilDB->quote($a_ass_id, "integer")." ".
1380  "AND returned = 1";
1381 
1382  $res = $ilDB->query($query);
1383  while($row = $ilDB->fetchObject($res))
1384  {
1385  $usr_ids[] = $row->ud;
1386  }
1387 
1388  return $usr_ids ? $usr_ids : array();
1389  }
1390 
1398  static function getLastSubmission($a_ass_id, $a_user_id)
1399  {
1400  global $ilDB, $lng;
1401 
1402  $q = "SELECT obj_id,user_id,ts FROM exc_returned ".
1403  "WHERE ass_id = ".$ilDB->quote($a_ass_id, "integer")." AND user_id = ".
1404  $ilDB->quote($a_user_id, "integer").
1405  " ORDER BY ts DESC";
1406 
1407  $usr_set = $ilDB->query($q);
1408 
1409  $array = $ilDB->fetchAssoc($usr_set);
1410  if ($array["ts"]==NULL)
1411  {
1412  return false;
1413  }
1414  else
1415  {
1416  return ilUtil::getMySQLTimestamp($array["ts"]);
1417  }
1418  }
1419 
1423  static function lookupAnyExerciseSent($a_exc_id, $a_ass_id)
1424  {
1425  global $ilDB;
1426 
1427  $q = "SELECT count(*) AS cnt FROM exc_mem_ass_status".
1428  " WHERE NOT sent_time IS NULL".
1429  " AND ass_id = ".$ilDB->quote($a_ass_id, "integer")." ".
1430  " ";
1431  $set = $ilDB->query($q);
1432  $rec = $ilDB->fetchAssoc($set);
1433 
1434  if ($rec["cnt"] > 0)
1435  {
1436  return true;
1437  }
1438  else
1439  {
1440  return false;
1441  }
1442  }
1443 
1448  static function lookupUpdatedSubmission($ass_id, $member_id)
1449  {
1450 
1451  global $ilDB, $lng;
1452 
1453  $q="SELECT exc_mem_ass_status.status_time, exc_returned.ts ".
1454  "FROM exc_mem_ass_status, exc_returned ".
1455  "WHERE exc_mem_ass_status.status_time < exc_returned.ts ".
1456  "AND NOT exc_mem_ass_status.status_time IS NULL ".
1457  "AND exc_returned.ass_id = exc_mem_ass_status.ass_id ".
1458  "AND exc_returned.user_id = exc_mem_ass_status.usr_id ".
1459  "AND exc_returned.ass_id=".$ilDB->quote($ass_id, "integer")." AND exc_returned.user_id=".
1460  $ilDB->quote($member_id, "integer");
1461 
1462  $usr_set = $ilDB->query($q);
1463 
1464  $array = $ilDB->fetchAssoc($usr_set);
1465 
1466  if (count($array)==0)
1467  {
1468  return 0;
1469  }
1470  else
1471  {
1472  return 1;
1473  }
1474 
1475  }
1476 
1481  static function lookupNewFiles($ass_id, $member_id)
1482  {
1483  global $ilDB, $ilUser;
1484 
1485  $q = "SELECT exc_returned.returned_id AS id ".
1486  "FROM exc_usr_tutor, exc_returned ".
1487  "WHERE exc_returned.ass_id = exc_usr_tutor.ass_id ".
1488  " AND exc_returned.user_id = exc_usr_tutor.usr_id ".
1489  " AND exc_returned.ass_id = ".$ilDB->quote($ass_id, "integer").
1490  " AND exc_returned.user_id = ".$ilDB->quote($member_id, "integer").
1491  " AND exc_usr_tutor.tutor_id = ".$ilDB->quote($ilUser->getId(), "integer").
1492  " AND exc_usr_tutor.download_time < exc_returned.ts ";
1493 
1494  $new_up_set = $ilDB->query($q);
1495 
1496  $new_up = array();
1497  while ($new_up_rec = $ilDB->fetchAssoc($new_up_set))
1498  {
1499  $new_up[] = $new_up_rec["id"];
1500  }
1501 
1502  return $new_up;
1503  }
1504 
1508  function getMemberListData($a_exc_id, $a_ass_id)
1509  {
1510  global $ilDB;
1511 
1512  $mem = array();
1513 
1514  // first get list of members from member table
1515  $set = $ilDB->query("SELECT * FROM exc_members ".
1516  "WHERE obj_id = ".$ilDB->quote($a_exc_id, "integer"));
1517  while($rec = $ilDB->fetchAssoc($set))
1518  {
1519  if (ilObject::_exists($rec["usr_id"]) &&
1520  (ilObject::_lookupType($rec["usr_id"]) == "usr"))
1521  {
1522  $name = ilObjUser::_lookupName($rec["usr_id"]);
1523  $login = ilObjUser::_lookupLogin($rec["usr_id"]);
1524  $mem[$rec["usr_id"]] =
1525  array(
1526  "name" => $name["lastname"].", ".$name["firstname"],
1527  "login" => $login,
1528  "usr_id" => $rec["usr_id"],
1529  "lastname" => $name["lastname"],
1530  "firstname" => $name["firstname"]
1531  );
1532  }
1533  }
1534 
1535  $q = "SELECT * FROM exc_mem_ass_status ".
1536  "WHERE ass_id = ".$ilDB->quote($a_ass_id, "integer");
1537  $set = $ilDB->query($q);
1538  while($rec = $ilDB->fetchAssoc($set))
1539  {
1540  if (isset($mem[$rec["usr_id"]]))
1541  {
1542  $mem[$rec["usr_id"]]["sent_time"] = $rec["sent_time"];
1543  $mem[$rec["usr_id"]]["submission"] = ilExAssignment::getLastSubmission($a_ass_id, $rec["usr_id"]);
1544  $mem[$rec["usr_id"]]["status_time"] = $rec["status_time"];
1545  $mem[$rec["usr_id"]]["feedback_time"] = $rec["feedback_time"];
1546  $mem[$rec["usr_id"]]["notice"] = $rec["notice"];
1547  $mem[$rec["usr_id"]]["status"] = $rec["status"];
1548  }
1549  }
1550  return $mem;
1551  }
1552 
1556  static function createNewUserRecords($a_user_id, $a_exc_id)
1557  {
1558  global $ilDB;
1559 
1560  $ass_data = ilExAssignment::getAssignmentDataOfExercise($a_exc_id);
1561  foreach ($ass_data as $ass)
1562  {
1563 //echo "-".$ass["id"]."-".$a_user_id."-";
1564  $ilDB->replace("exc_mem_ass_status", array(
1565  "ass_id" => array("integer", $ass["id"]),
1566  "usr_id" => array("integer", $a_user_id)
1567  ), array(
1568  "status" => array("text", "notgraded")
1569  ));
1570  }
1571  }
1572 
1576  static function createNewAssignmentRecords($a_ass_id, $a_exc)
1577  {
1578  global $ilDB;
1579 
1580  include_once("./Modules/Exercise/classes/class.ilExerciseMembers.php");
1581  $exmem = new ilExerciseMembers($a_exc);
1582  $mems = $exmem->getMembers();
1583 
1584  foreach ($mems as $mem)
1585  {
1586  $ilDB->replace("exc_mem_ass_status", array(
1587  "ass_id" => array("integer", $a_ass_id),
1588  "usr_id" => array("integer", $mem)
1589  ), array(
1590  "status" => array("text", "notgraded")
1591  ));
1592  }
1593  }
1594 
1599  function uploadAssignmentFiles($a_files)
1600  {
1601  include_once("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
1602  $storage = new ilFSStorageExercise($this->getExerciseId(), $this->getId());
1603  $storage->create();
1604  $storage->uploadAssignmentFiles($a_files);
1605  }
1606 
1607 }
1608 
1609 ?>