ILIAS  eassessment Revision 61809
 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 {
16  function __construct($a_id = 0)
17  {
18  if ($a_id > 0)
19  {
20  $this->setId($a_id);
21  $this->read();
22  }
23  }
24 
30  function setId($a_val)
31  {
32  $this->id = $a_val;
33  }
34 
40  function getId()
41  {
42  return $this->id;
43  }
44 
50  function setExerciseId($a_val)
51  {
52  $this->exc_id = $a_val;
53  }
54 
60  function getExerciseId()
61  {
62  return $this->exc_id;
63  }
64 
70  function setStartTime($a_val)
71  {
72  $this->start_time = $a_val;
73  }
74 
80  function getStartTime()
81  {
82  return $this->start_time;
83  }
84 
90  function setDeadline($a_val)
91  {
92  $this->deadline = $a_val;
93  }
94 
100  function getDeadline()
101  {
102  return $this->deadline;
103  }
104 
110  function setInstruction($a_val)
111  {
112  $this->instruction = $a_val;
113  }
114 
120  function getInstruction()
121  {
122  return $this->instruction;
123  }
124 
130  function setTitle($a_val)
131  {
132  $this->title = $a_val;
133  }
134 
140  function getTitle()
141  {
142  return $this->title;
143  }
144 
150  function setMandatory($a_val)
151  {
152  $this->mandatory = $a_val;
153  }
154 
160  function getMandatory()
161  {
162  return $this->mandatory;
163  }
164 
170  function setOrderNr($a_val)
171  {
172  $this->order_nr = $a_val;
173  }
174 
180  function getOrderNr()
181  {
182  return $this->order_nr;
183  }
184 
188  function read()
189  {
190  global $ilDB;
191 
192  $set = $ilDB->query("SELECT * FROM exc_assignment ".
193  " WHERE id = ".$ilDB->quote($this->getId(), "integer")
194  );
195  while ($rec = $ilDB->fetchAssoc($set))
196  {
197  $this->setExerciseId($rec["exc_id"]);
198  $this->setDeadline($rec["time_stamp"]);
199  $this->setInstruction($rec["instruction"]);
200  $this->setTitle($rec["title"]);
201  $this->setStartTime($rec["start_time"]);
202  $this->setOrderNr($rec["order_nr"]);
203  $this->setMandatory($rec["mandatory"]);
204  }
205  }
206 
210  function save()
211  {
212  global $ilDB;
213 
214  if ($this->getOrderNr() == 0)
215  {
216  $this->setOrderNr(
218  + 10);
219  }
220 
221  $next_id = $ilDB->nextId("exc_assignment");
222  $ilDB->insert("exc_assignment", array(
223  "id" => array("integer", $next_id),
224  "exc_id" => array("integer", $this->getExerciseId()),
225  "time_stamp" => array("integer", $this->getDeadline()),
226  "instruction" => array("clob", $this->getInstruction()),
227  "title" => array("text", $this->getTitle()),
228  "start_time" => array("integer", $this->getStartTime()),
229  "order_nr" => array("integer", $this->getOrderNr()),
230  "mandatory" => array("text", $this->getMandatory())
231  ));
232  $this->setId($next_id);
233  $exc = new ilObjExercise($this->getExerciseId(), false);
234  $exc->updateAllUsersStatus();
236  }
237 
241  function update()
242  {
243  global $ilDB;
244 
245  $ilDB->update("exc_assignment",
246  array(
247  "exc_id" => array("integer", $this->getExerciseId()),
248  "time_stamp" => array("integer", $this->getDeadline()),
249  "instruction" => array("clob", $this->getInstruction()),
250  "title" => array("text", $this->getTitle()),
251  "start_time" => array("integer", $this->getStartTime()),
252  "order_nr" => array("integer", $this->getOrderNr()),
253  "mandatory" => array("integer", $this->getMandatory())
254  ),
255  array(
256  "id" => array("integer", $this->getId()),
257  ));
258  $exc = new ilObjExercise($this->getExerciseId(), false);
259  $exc->updateAllUsersStatus();
260  }
261 
265  function delete()
266  {
267  global $ilDB;
268 
269  $ilDB->manipulate("DELETE FROM exc_assignment WHERE ".
270  " id = ".$ilDB->quote($this->getId(), "integer")
271  );
272  $exc = new ilObjExercise($this->getExerciseId(), false);
273  $exc->updateAllUsersStatus();
274  }
275 
276 
280  static function getAssignmentDataOfExercise($a_exc_id)
281  {
282  global $ilDB;
283 
284  $set = $ilDB->query("SELECT * FROM exc_assignment ".
285  " WHERE exc_id = ".$ilDB->quote($a_exc_id, "integer").
286  " ORDER BY order_nr ASC");
287  $data = array();
288 
289  $order_val = 10;
290  while ($rec = $ilDB->fetchAssoc($set))
291  {
292 
293  $data[] = array(
294  "id" => $rec["id"],
295  "exc_id" => $rec["exc_id"],
296  "deadline" => $rec["time_stamp"],
297  "instruction" => $rec["instruction"],
298  "title" => $rec["title"],
299  "start_time" => $rec["start_time"],
300  "order_val" => $order_val,
301  "mandatory" => $rec["mandatory"]
302  );
303  $order_val += 10;
304  }
305  return $data;
306  }
307 
314  function cloneAssignmentsOfExercise($a_old_exc_id, $a_new_exc_id)
315  {
316  $ass_data = ilExAssignment::getAssignmentDataOfExercise($a_old_exc_id);
317  foreach ($ass_data as $d)
318  {
319  // clone assignment
320  $new_ass = new ilExAssignment();
321  $new_ass->setExerciseId($a_new_exc_id);
322  $new_ass->setTitle($d["title"]);
323  $new_ass->setDeadline($d["deadline"]);
324  $new_ass->setInstruction($d["instruction"]);
325  $new_ass->setMandatory($d["mandatory"]);
326  $new_ass->setOrderNr($d["order_val"]);
327  $new_ass->setStartTime($d["start_time"]);
328  $new_ass->save();
329 
330  // clone assignment files
331  include_once("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
332  $old_storage = new ilFSStorageExercise($a_old_exc_id, (int) $d["id"]);
333  $new_storage = new ilFSStorageExercise($a_new_exc_id, (int) $new_ass->getId());
334  $new_storage->create();
335 
336  if (is_dir($old_storage->getPath()))
337  {
338  ilUtil::rCopy($old_storage->getPath(), $new_storage->getPath());
339  }
340  }
341  }
342 
346  static function getFiles($a_exc_id, $a_ass_id)
347  {
348  include_once("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
349  $storage = new ilFSStorageExercise($a_exc_id, $a_ass_id);
350  return $storage->getFiles();
351  }
352 
356  static function lookupMaxOrderNrForEx($a_exc_id)
357  {
358  global $ilDB;
359 
360  $set = $ilDB->query("SELECT MAX(order_nr) mnr FROM exc_assignment ".
361  " WHERE exc_id = ".$ilDB->quote($a_exc_id, "integer")
362  );
363  while ($rec = $ilDB->fetchAssoc($set))
364  {
365  return (int) $rec["mnr"];
366  }
367  return 0;
368  }
369 
375  public static function lookupAssignmentOnline($a_ass_id)
376  {
377  global $ilDB;
378 
379  $query = "SELECT id FROM exc_assignment ".
380  "WHERE start_time <= ".$ilDB->quote(time(),'integer').' '.
381  "AND time_stamp >= ".$ilDB->quote(time(),'integer').' '.
382  "AND id = ".$ilDB->quote($a_ass_id,'integer');
383  $res = $ilDB->query($query);
384 
385  return $res->numRows() ? true : false;
386  }
387 
388 
392  private static function lookup($a_id, $a_field)
393  {
394  global $ilDB;
395 
396  $set = $ilDB->query("SELECT ".$a_field." FROM exc_assignment ".
397  " WHERE id = ".$ilDB->quote($a_id, "integer")
398  );
399 
400  $rec = $ilDB->fetchAssoc($set);
401 
402  return $rec[$a_field];
403  }
404 
408  static function lookupTitle($a_id)
409  {
410  return ilExAssignment::lookup($a_id, "title");
411  }
412 
416  function saveAssOrderOfExercise($a_ex_id, $a_order)
417  {
418  global $ilDB;
419 
420  $result_order = array();
421  asort($a_order);
422  $nr = 10;
423  foreach ($a_order as $k => $v)
424  {
425  // the check for exc_id is for security reasons. ass ids are unique.
426  $ilDB->manipulate($t = "UPDATE exc_assignment SET ".
427  " order_nr = ".$ilDB->quote($nr, "integer").
428  " WHERE id = ".$ilDB->quote((int) $k, "integer").
429  " AND exc_id = ".$ilDB->quote((int) $a_ex_id, "integer")
430  );
431  $nr+=10;
432  }
433  }
434 
438  function orderAssByDeadline($a_ex_id)
439  {
440  global $ilDB;
441 
442  $set = $ilDB->query("SELECT id FROM exc_assignment ".
443  " WHERE exc_id = ".$ilDB->quote($a_ex_id, "integer").
444  " ORDER BY time_stamp ASC"
445  );
446  $nr = 10;
447  while ($rec = $ilDB->fetchAssoc($set))
448  {
449  $ilDB->manipulate("UPDATE exc_assignment SET ".
450  " order_nr = ".$ilDB->quote($nr, "integer").
451  " WHERE id = ".$ilDB->quote($rec["id"], "integer")
452  );
453  $nr += 10;
454  }
455  }
456 
460  function countMandatory($a_ex_id)
461  {
462  global $ilDB;
463 
464  $set = $ilDB->query("SELECT count(*) cntm FROM exc_assignment ".
465  " WHERE exc_id = ".$ilDB->quote($a_ex_id, "integer").
466  " AND mandatory = ".$ilDB->quote(1, "integer")
467  );
468  $rec = $ilDB->fetchAssoc($set);
469  return $rec["cntm"];
470  }
471 
475 
479  private function lookupAssMemberField($a_ass_id, $a_user_id, $a_field)
480  {
481  global $ilDB;
482 
483  $set = $ilDB->query("SELECT ".$a_field." FROM exc_mem_ass_status ".
484  " WHERE ass_id = ".$ilDB->quote($a_ass_id, "integer").
485  " AND usr_id = ".$ilDB->quote($a_user_id, "integer")
486  );
487  $rec = $ilDB->fetchAssoc($set);
488 
489  return $rec[$a_field];
490  }
491 
495  private function updateAssMemberField($a_ass_id, $a_user_id, $a_field, $a_value, $a_type)
496  {
497  global $ilDB;
498 
499  $ilDB->manipulate("UPDATE exc_mem_ass_status SET ".
500  " ".$a_field." = ".$ilDB->quote($a_value, $a_type).
501  " WHERE ass_id = ".$ilDB->quote($a_ass_id, "integer").
502  " AND usr_id = ".$ilDB->quote($a_user_id, "integer")
503  );
504  }
505 
506 
507 /* function setStatus($a_status)
508  {
509  if(is_array($a_status))
510  {
511  $this->status = $a_status;
512  return true;
513  }
514  }
515  function getStatus()
516  {
517  return $this->status ? $this->status : array();
518  }*/
519 
523  function lookupCommentForUser($a_ass_id, $a_user_id)
524  {
525  return ilExAssignment::lookupAssMemberField($a_ass_id, $a_user_id, "u_comment");
526  }
527 
531  function updateCommentForUser($a_ass_id, $a_user_id, $a_value)
532  {
533  ilExAssignment::updateAssMemberField($a_ass_id, $a_user_id,
534  "u_comment", $a_value, "text");
535  }
536 
540  function lookupMarkOfUser($a_ass_id, $a_user_id)
541  {
542  return ilExAssignment::lookupAssMemberField($a_ass_id, $a_user_id, "mark");
543  }
544 
548  function updateMarkOfUser($a_ass_id, $a_user_id, $a_value)
549  {
550  ilExAssignment::updateAssMemberField($a_ass_id, $a_user_id,
551  "mark", $a_value, "text");
552  }
553 
557  function lookupStatusOfUser($a_ass_id, $a_user_id)
558  {
559  $stat = ilExAssignment::lookupAssMemberField($a_ass_id, $a_user_id, "status");
560  if ($stat == "")
561  {
562  $stat = "notgraded";
563  }
564  return $stat;
565  }
566 
570  function updateStatusOfUser($a_ass_id, $a_user_id, $a_status)
571  {
572  global $ilDB;
573 
574  $ilDB->manipulateF("UPDATE exc_mem_ass_status ".
575  "SET status = %s, status_time= %s ".
576  " WHERE ass_id = %s AND usr_id = %s AND status <> %s ",
577  array("text", "timestamp", "integer", "integer", "text"),
578  array($a_status, ilUtil::now(), $a_ass_id, $a_user_id, $a_status));
579 
580  $ass = new ilExAssignment($a_ass_id);
581  $exc = new ilObjExercise($ass->getExerciseId(), false);
582  $exc->updateUserStatus($a_user_id);
583  }
584 
588  function updateStatusTimeOfUser($a_ass_id, $a_user_id)
589  {
590  ilExAssignment::updateAssMemberField($a_ass_id, $a_user_id,
591  "status_time", ilUtil::now(), "timestamp");
592  }
593 
594 
595  /*function setStatusSent($a_status)
596  {
597  if(is_array($a_status))
598  {
599  $this->status_sent = $a_status;
600  return true;
601  }
602  }
603  function getStatusSent()
604  {
605  return $this->status_sent ? $this->status_sent : array(0 => 0);
606  }*/
607 
611  function lookupStatusSentOfUser($a_ass_id, $a_user_id)
612  {
613  return ilExAssignment::lookupAssMemberField($a_ass_id, $a_user_id, "sent");
614  }
615 
619  function updateStatusSentForUser($a_ass_id, $a_user_id, $a_status)
620  {
621  global $ilDB;
622 
623  $ilDB->manipulateF("UPDATE exc_mem_ass_status ".
624  "SET sent = %s, status_time= %s, sent_time = %s ".
625  " WHERE ass_id = %s AND usr_id = %s ",
626  array("integer", "timestamp", "timestamp", "integer", "integer"),
627  array((int) $a_status, ilUtil::now(), ($a_status ? ilUtil::now() : null),
628  $a_ass_id, $a_user_id));
629  }
630 
631  /*function getStatusReturned()
632  {
633  return $this->status_returned ? $this->status_returned : array(0 => 0);
634  }
635  function setStatusReturned($a_status)
636  {
637  if(is_array($a_status))
638  {
639  $this->status_returned = $a_status;
640  return true;
641  }
642  return false;
643  }*/
644 
648  function lookupStatusReturnedOfUser($a_ass_id, $a_user_id)
649  {
650  return ilExAssignment::lookupAssMemberField($a_ass_id, $a_user_id, "returned");
651  }
652 
656  function updateStatusReturnedForUser($a_ass_id, $a_user_id, $a_status)
657  {
658  global $ilDB;
659 
660  $ilDB->manipulateF("UPDATE exc_mem_ass_status ".
661  "SET returned = %s, status_time= %s ".
662  " WHERE ass_id = %s AND usr_id = %s",
663  array("integer", "timestamp", "integer", "integer"),
664  array((int) $a_status, ilUtil::now(),
665  $a_ass_id, $a_user_id));
666  }
667 
668 /* // feedback functions
669  function setStatusFeedback($a_status)
670  {
671  if(is_array($a_status))
672  {
673  $this->status_feedback = $a_status;
674  return true;
675  }
676  }
677  function getStatusFeedback()
678  {
679  return $this->status_feedback ? $this->status_feedback : array(0 => 0);
680  }*/
681 
685  function lookupStatusFeedbackOfUser($a_ass_id, $a_user_id)
686  {
687  return ilExAssignment::lookupAssMemberField($a_ass_id, $a_user_id, "feedback");
688  }
689 
693  function updateStatusFeedbackForUser($a_ass_id, $a_user_id, $a_status)
694  {
695  global $ilDB;
696 
697  $ilDB->manipulateF("UPDATE exc_mem_ass_status ".
698  "SET feedback = %s, status_time= %s, feedback_time = %s ".
699  " WHERE ass_id = %s AND usr_id = %s",
700  array("integer", "timestamp", "timestamp", "integer", "integer"),
701  array((int) $a_status, ilUtil::now(), ($a_status ? ilUtil::now() : null),
702  $a_ass_id, $a_user_id));
703  }
704 
708  static function lookupSentTimeOfUser($a_ass_id, $a_user_id)
709  {
711  ilExAssignment::lookupAssMemberField($a_ass_id, $a_user_id, "sent_time"));
712  }
713 
717  static function lookupFeedbackTimeOfUser($a_ass_id, $a_user_id)
718  {
720  ilExAssignment::lookupAssMemberField($a_ass_id, $a_user_id, "feedback_time"));
721  }
722 
726  static function lookupStatusTimeOfUser($a_ass_id, $a_user_id)
727  {
729  ilExAssignment::lookupAssMemberField($a_ass_id, $a_user_id, "status_time"));
730  }
731 
732  /*function getNotice()
733  {
734  return $this->notice ? $this->notice : array(0 => 0);
735  }
736 
737  function setNotice($a_notice)
738  {
739  if(is_array($a_notice))
740  {
741  $this->notice = $a_notice;
742  return true;
743  }
744  return false;
745  }*/
746 
750  function lookupNoticeOfUser($a_ass_id, $a_user_id)
751  {
752  return ilExAssignment::lookupAssMemberField($a_ass_id, $a_user_id, "notice");
753  }
754 
758  function hasReturned($a_ass_id, $a_user_id)
759  {
760  global $ilDB;
761 
762  $result = $ilDB->queryF("SELECT returned_id FROM exc_returned WHERE ass_id = %s AND user_id = %s",
763  array("integer", "integer"),
764  array($ass_id, $a_user_id));
765  return $ilDB->numRows($result);
766  }
767 
768 
772  function getAllDeliveredFiles($a_exc_id, $a_ass_id)
773  {
774  global $ilDB;
775 
776  include_once("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
777  $fs = new ilFSStorageExercise($a_exc_id, $a_ass_id);
778 
779  $query = "SELECT * FROM exc_returned WHERE ass_id = ".
780  $ilDB->quote($a_ass_id, "integer");
781 
782  $res = $ilDB->query($query);
783  while($row = $ilDB->fetchAssoc($res))
784  {
785  $row["timestamp"] = $row["ts"];
786  $row["filename"] = $fs->getAbsoluteSubmissionPath().
787  "/".$row["user_id"]."/".basename($row["filename"]);
788  $delivered[] = $row;
789  }
790 
791  //$delivered = ilObjExercise::_fixFilenameArray($delivered);
792 
793  return $delivered ? $delivered : array();
794  }
795 
799  function getDeliveredFiles($a_exc_id, $a_ass_id, $a_user_id)
800  {
801  global $ilDB;
802 
803  include_once("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
804  $fs = new ilFSStorageExercise($a_exc_id, $a_ass_id);
805 
806  $result = $ilDB->queryF("SELECT * FROM exc_returned WHERE ass_id = %s AND user_id = %s ORDER BY ts",
807  array("integer", "integer"),
808  array($a_ass_id, $a_user_id));
809 
810  $delivered_files = array();
811  if ($ilDB->numRows($result))
812  {
813  while ($row = $ilDB->fetchAssoc($result))
814  {
815  $row["timestamp"] = $row["ts"];
816  $row["timestamp14"] = substr($row["ts"], 0, 4).
817  substr($row["ts"], 5, 2).substr($row["ts"], 8, 2).
818  substr($row["ts"], 11, 2).substr($row["ts"], 14, 2).
819  substr($row["ts"], 17, 2);
820  $row["filename"] = $fs->getAbsoluteSubmissionPath().
821  "/".$row["user_id"]."/".basename($row["filename"]);
822  array_push($delivered_files, $row);
823  }
824  }
825 
826  //$delivered_files = ilObjExercise::_fixFilenameArray($delivered_files);
827 
828  return $delivered_files;
829  }
830 
834  function deleteDeliveredFiles($a_exc_id, $a_ass_id, $file_id_array, $a_user_id)
835  {
836  global $ilDB;
837 
838  include_once("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
839  $fs = new ilFSStorageExercise($a_exc_id, $a_ass_id);
840 
841  if (count($file_id_array))
842  {
843  $result = $ilDB->query("SELECT * FROM exc_returned WHERE user_id = ".
844  $ilDB->quote($a_user_id, "integer")." AND ".
845  $ilDB->in("returned_id", $file_id_array, false, "integer"));
846 
847  if ($ilDB->numRows($result))
848  {
849  $result_array = array();
850  while ($row = $ilDB->fetchAssoc($result))
851  {
852  $row["timestamp"] = $row["ts"];
853  array_push($result_array, $row);
854  }
855  // delete the entries in the database
856  $ilDB->manipulate("DELETE FROM exc_returned WHERE user_id = ".
857  $ilDB->quote($a_user_id, "integer")." AND ".
858  $ilDB->in("returned_id", $file_id_array, false, "integer"));
859  //returned_id IN ("
860  //.implode(ilUtil::quoteArray($file_id_array) ,",").")",
861  //$this->ilias->db->quote($a_member_id . "")
862 
863  // delete the files
864  foreach ($result_array as $key => $value)
865  {
866  $filename = $fs->getAbsoluteSubmissionPath().
867  "/".$value["user_id"]."/".basename($value["filename"]);
868  unlink($filename);
869  }
870  }
871  }
872  }
873 
877  function deliverReturnedFiles($a_exc_id, $a_ass_id, $a_user_id, $a_only_new = false)
878  {
879  global $ilUser, $ilDB;
880 
881  // get last download time
882  $and_str = "";
883  if ($a_only_new)
884  {
885  $q = "SELECT download_time FROM exc_usr_tutor WHERE ".
886  " ass_id = ".$ilDB->quote($a_ass_id, "integer")." AND ".
887  " usr_id = ".$ilDB->quote($a_user_id, "integer")." AND ".
888  " tutor_id = ".$ilDB->quote($ilUser->getId(), "integer");
889  $lu_set = $ilDB->query($q);
890  if ($lu_rec = $ilDB->fetchAssoc($lu_set))
891  {
892  if ($lu_rec["download_time"] > 0)
893  {
894  $and_str = " AND ts > ".$ilDB->quote($lu_rec["download_time"], "timestamp");
895  }
896  }
897  }
898 
899  ilExAssignment::updateTutorDownloadTime($a_exc_id, $a_ass_id, $a_user_id);
900 
901  $query = sprintf("SELECT * FROM exc_returned WHERE ass_id = %s AND user_id = %s".
902  $and_str,
903  $ilDB->quote($a_ass_id, "integer"),
904  $ilDB->quote($a_user_id, "integer"));
905  $result = $ilDB->query($query);
906  $count = $ilDB->numRows($result);
907  if ($count == 1)
908  {
909  $row = $ilDB->fetchAssoc($result);
910  ilExAssignment::downloadSingleFile($a_exc_id, $a_ass_id, $a_user_id,
911  $row["filename"], $row["filetitle"]);
912  }
913  else if ($count > 0)
914  {
915  $array_files = array();
916  $filename = "";
917  while ($row = $ilDB->fetchAssoc($result))
918  {
919  array_push($array_files, basename($row["filename"]));
920 // $filename = $row["filename"];
921 // $pathinfo = pathinfo($filename);
922 // $dir = $pathinfo["dirname"];
923 // $file = $pathinfo["basename"];
924 // array_push($array_files, $file);
925  }
926  $pathinfo = pathinfo($filename);
927  $dir = $pathinfo["dirname"];
928 
929  ilExAssignment::downloadMultipleFiles($a_exc_id, $a_ass_id, $array_files, $a_user_id);
930  }
931  else
932  {
933  return false;
934  }
935 
936  return true;
937  }
938 
939  // Update the timestamp of the last download of current user (=tutor)
943  function updateTutorDownloadTime($a_exc_id, $a_ass_id, $a_user_id)
944  {
945  global $ilUser, $ilDB;
946 
947  $ilDB->manipulateF("DELETE FROM exc_usr_tutor ".
948  "WHERE ass_id = %s AND usr_id = %s AND tutor_id = %s",
949  array("integer", "integer", "integer"),
950  array($a_ass_id, $a_user_id, $ilUser->getId()));
951 
952  $ilDB->manipulateF("INSERT INTO exc_usr_tutor (ass_id, obj_id, usr_id, tutor_id, download_time) VALUES ".
953  "(%s, %s, %s, %s, %s)",
954  array("integer", "integer", "integer", "integer", "timestamp"),
955  array($a_ass_id, $a_exc_id, $a_user_id, $ilUser->getId(), ilUtil::now()));
956  }
957 
961  function downloadSelectedFiles($a_exc_id, $a_ass_id, $a_user_id, $array_file_id)
962  {
963  global $ilDB;
964 
965  if (count($array_file_id))
966  {
967  $result = $ilDB->query("SELECT * FROM exc_returned WHERE ".
968  $ilDB->in("returned_id", $array_file_id, false, "integer").
969  " AND user_id = ".$ilDB->quote($a_user_id));
970  if ($ilDB->numRows($result))
971  {
972  $array_found = array();
973  while ($row = $ilDB->fetchAssoc($result))
974  {
975  $row["timestamp"] = $row["ts"];
976  array_push($array_found, $row);
977  }
978  if (count($array_found) == 1)
979  {
980  ilExAssignment::downloadSingleFile($a_exc_id, $a_ass_id, $a_user_id,
981  $array_found[0]["filename"], $array_found[0]["filetitle"]);
982  }
983  else
984  {
985  $filenames = array();
986  $dir = "";
987  $file = "";
988  foreach ($array_found as $key => $value)
989  {
990  //$pathinfo = pathinfo(ilObjExercise::_fixFilename($value["filename"]));
991  //$dir = $pathinfo["dirname"];
992  //$file = $pathinfo["basename"];
993  //array_push($filenames, $file);
994  array_push($filenames, basename($value["filename"]));
995  }
996  ilExAssignment::downloadMultipleFiles($a_exc_id, $a_ass_id,
997  $filenames, $a_user_id);
998  }
999  }
1000  }
1001  }
1002 
1006  function downloadSingleFile($a_exc_id, $a_ass_id, $a_user_id, $filename, $filetitle)
1007  {
1008  include_once("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
1009  $fs = new ilFSStorageExercise($a_exc_id, $a_ass_id);
1010 
1011  $filename = $fs->getAbsoluteSubmissionPath().
1012  "/".$a_user_id."/".basename($filename);
1013 
1014  require_once "./Services/Utilities/classes/class.ilUtil.php";
1015  ilUtil::deliverFile($filename, $filetitle);
1016  }
1017 
1021 // @todo: check whether files of multiple users are downloaded this way
1022  function downloadMultipleFiles($a_exc_id, $a_ass_id, $array_filenames,
1023  $a_user_id)
1024  {
1025  global $lng, $ilObjDataCache;
1026 
1027  include_once("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
1028  $fs = new ilFSStorageExercise($a_exc_id, $a_ass_id);
1029  $pathname = $fs->getAbsoluteSubmissionPath().
1030  "/".$a_user_id;
1031 
1032  require_once "./Services/Utilities/classes/class.ilUtil.php";
1033  $cdir = getcwd();
1034 
1035  $zip = PATH_TO_ZIP;
1036  $tmpdir = ilUtil::ilTempnam();
1037  $tmpfile = ilUtil::ilTempnam();
1038  $tmpzipfile = $tmpfile . ".zip";
1039 
1040  ilUtil::makeDir($tmpdir);
1041  chdir($tmpdir);
1042 
1043  $assTitle = ilExAssignment::lookupTitle($a_ass_id);
1044  $deliverFilename = str_replace(" ", "_", $assTitle);
1045  if ($a_user_id > 0)
1046  {
1047  $userName = ilObjUser::_lookupName($a_user_id);
1048  $deliverFilename .= "_".$userName["lastname"]."_".$userName["firstname"];
1049  }
1050  else
1051  {
1052  $deliverFilename .= "_files";
1053  }
1054  $orgDeliverFilename = trim($deliverFilename);
1055  $deliverFilename = ilUtil::getASCIIFilename($orgDeliverFilename);
1056  ilUtil::makeDir($tmpdir."/".$deliverFilename);
1057  chdir($tmpdir."/".$deliverFilename);
1058 
1059  //copy all files to a temporary directory and remove them afterwards
1060  foreach ($array_filenames as $key => $filename)
1061  {
1062  // remove timestamp
1063  $newFilename = trim($filename);
1064  $pos = strpos($newFilename , "_");
1065  if ($pos === false)
1066  {
1067  } else
1068  {
1069  $newFilename= substr($newFilename, $pos + 1);
1070  }
1071  $newFilename = $tmpdir.DIRECTORY_SEPARATOR.$deliverFilename.DIRECTORY_SEPARATOR.$newFilename;
1072  // copy to temporal directory
1073  $oldFilename = $pathname.DIRECTORY_SEPARATOR.$filename;
1074  if (!copy ($oldFilename, $newFilename))
1075  {
1076  echo 'Could not copy '.$oldFilename.' to '.$newFilename;
1077  }
1078  touch($newFilename, filectime($oldFilename));
1079  $array_filenames[$key] = ilUtil::escapeShellArg($deliverFilename.DIRECTORY_SEPARATOR.basename($newFilename)); //$array_filenames[$key]);
1080  }
1081  chdir($tmpdir);
1082  $zipcmd = $zip." ".ilUtil::escapeShellArg($tmpzipfile)." ".join($array_filenames, " ");
1083 //echo getcwd()."<br>";
1084 //echo $zipcmd; exit;
1085  exec($zipcmd);
1086  ilUtil::delDir($tmpdir);
1087 
1088  chdir($cdir);
1089  ilUtil::deliverFile($tmpzipfile, $orgDeliverFilename.".zip");
1090  unlink($tmpzipfile);
1091  }
1092 
1098  function downloadAllDeliveredFiles($a_exc_id, $a_ass_id, $members)
1099  {
1100  global $lng, $ilObjDataCache, $ilias;
1101 
1102  include_once "./Services/Utilities/classes/class.ilUtil.php";
1103  include_once("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
1104 
1105  $storage = new ilFSStorageExercise($a_exc_id, $a_ass_id);
1106  $storage->create();
1107 
1108  ksort($members);
1109  //$savepath = $this->getExercisePath() . "/" . $this->obj_id . "/";
1110  $savepath = $storage->getAbsoluteSubmissionPath();
1111  $cdir = getcwd();
1112 
1113 
1114  // important check: if the directory does not exist
1115  // ILIAS stays in the current directory (echoing only a warning)
1116  // and the zip command below archives the whole ILIAS directory
1117  // (including the data directory) and sends a mega file to the user :-o
1118  if (!is_dir($savepath))
1119  {
1120  return;
1121  }
1122  // Safe mode fix
1123 // chdir($this->getExercisePath());
1124  chdir($storage->getTempPath());
1125  $zip = PATH_TO_ZIP;
1126 
1127  // check first, if we have enough free disk space to copy all files to temporary directory
1128  $tmpdir = ilUtil::ilTempnam();
1129  ilUtil::makeDir($tmpdir);
1130  chdir($tmpdir);
1131 
1132 
1133  $dirsize = 0;
1134  foreach ($members as $id => $object) {
1135  $directory = $savepath.DIRECTORY_SEPARATOR.$id;
1136  $dirsize += ilUtil::dirsize($directory);
1137  }
1138  if ($dirsize > disk_free_space($tmpdir)) {
1139  return -1;
1140  }
1141 
1142  // copy all member directories to the temporary folder
1143  // switch from id to member name and append the login if the member name is double
1144  // ensure that no illegal filenames will be created
1145  // remove timestamp from filename
1146  $cache = array();
1147  foreach ($members as $id => $user)
1148  {
1149  $sourcedir = $savepath.DIRECTORY_SEPARATOR.$id;
1150  if (!is_dir($sourcedir))
1151  continue;
1152  $userName = ilObjUser::_lookupName($id);
1153  $directory = ilUtil::getASCIIFilename(trim($userName["lastname"])."_".trim($userName["firstname"]));
1154  if (array_key_exists($directory, $cache))
1155  {
1156  // first try is to append the login;
1157  $directory = ilUtil::getASCIIFilename($directory."_".trim(ilObjUser::_lookupLogin($id)));
1158  if (array_key_exists($directory, $cache)) {
1159  // second and secure: append the user id as well.
1160  $directory .= "_".$id;
1161  }
1162  }
1163 
1164  $cache[$directory] = $directory;
1165  ilUtil::makeDir ($directory);
1166  $sourcefiles = scandir($sourcedir);
1167  foreach ($sourcefiles as $sourcefile) {
1168  if ($sourcefile == "." || $sourcefile == "..")
1169  continue;
1170  $targetfile = trim(basename($sourcefile));
1171  $pos = strpos($targetfile, "_");
1172  if ($pos === false)
1173  {
1174  } else
1175  {
1176  $targetfile= substr($targetfile, $pos + 1);
1177  }
1178  $targetfile = $directory.DIRECTORY_SEPARATOR.$targetfile;
1179  $sourcefile = $sourcedir.DIRECTORY_SEPARATOR.$sourcefile;
1180 
1181  if (!copy ($sourcefile, $targetfile))
1182  {
1183  //echo 'Could not copy '.$sourcefile.' to '.$targetfile;
1184  $ilias->raiseError('Could not copy '.basename($sourcefile)." to '".$targetfile."'.",
1185  $ilias->error_obj->MESSAGE);
1186  }
1187  else
1188  {
1189  // preserve time stamp
1190  touch($targetfile, filectime($sourcefile));
1191  }
1192 
1193  }
1194  }
1195 
1196  $tmpfile = ilUtil::ilTempnam();
1197  $tmpzipfile = $tmpfile . ".zip";
1198  // Safe mode fix
1199  $zipcmd = $zip." -r ".ilUtil::escapeShellArg($tmpzipfile)." .";
1200  exec($zipcmd);
1201  ilUtil::delDir($tmpdir);
1202 
1203  $assTitle = ilExAssignment::lookupTitle($a_ass_id);
1204  chdir($cdir);
1205  ilUtil::deliverFile($tmpzipfile, (strlen($assTitle) == 0
1206  ? strtolower($lng->txt("exc_assignment"))
1207  : $assTitle). ".zip");
1208  unlink($tmpfile);
1209  unlink($tmpzipfile);
1210  }
1211 
1215  function updateNoticeForUser($a_ass_id, $a_user_id, $a_notice)
1216  {
1217  global $ilDB;
1218 
1219  $ilDB->manipulateF("UPDATE exc_mem_ass_status ".
1220  "SET notice = %s, status_time= %s ".
1221  " WHERE ass_id = %s AND usr_id = %s AND ".
1222  $ilDB->equalsNot("notice", $a_notice, "text", true),
1223  array("text", "timestamp", "integer", "integer"),
1224  array($a_notice, ilUtil::now(), $a_ass_id, $a_user_id));
1225 
1226  }
1227 
1231  function _getReturned($a_ass_id)
1232  {
1233  global $ilDB;
1234 
1235  $query = "SELECT DISTINCT(usr_id) as ud FROM exc_mem_ass_status ".
1236  "WHERE ass_id = ".$ilDB->quote($a_ass_id, "integer")." ".
1237  "AND returned = 1";
1238 
1239  $res = $ilDB->query($query);
1240  while($row = $ilDB->fetchObject($res))
1241  {
1242  $usr_ids[] = $row->ud;
1243  }
1244 
1245  return $usr_ids ? $usr_ids : array();
1246  }
1247 
1255  static function getLastSubmission($a_ass_id, $a_user_id)
1256  {
1257  global $ilDB, $lng;
1258 
1259  $q = "SELECT obj_id,user_id,ts FROM exc_returned ".
1260  "WHERE ass_id = ".$ilDB->quote($a_ass_id, "integer")." AND user_id = ".
1261  $ilDB->quote($a_user_id, "integer").
1262  " ORDER BY ts DESC";
1263 
1264  $usr_set = $ilDB->query($q);
1265 
1266  $array = $ilDB->fetchAssoc($usr_set);
1267  if ($array["ts"]==NULL)
1268  {
1269  return false;
1270  }
1271  else
1272  {
1273  return ilUtil::getMySQLTimestamp($array["ts"]);
1274  }
1275  }
1276 
1280  static function lookupAnyExerciseSent($a_exc_id, $a_ass_id)
1281  {
1282  global $ilDB;
1283 
1284  $q = "SELECT count(*) AS cnt FROM exc_mem_ass_status".
1285  " WHERE NOT sent_time IS NULL".
1286  " AND ass_id = ".$ilDB->quote($a_ass_id, "integer")." ".
1287  " ";
1288  $set = $ilDB->query($q);
1289  $rec = $ilDB->fetchAssoc($set);
1290 
1291  if ($rec["cnt"] > 0)
1292  {
1293  return true;
1294  }
1295  else
1296  {
1297  return false;
1298  }
1299  }
1300 
1305  static function lookupUpdatedSubmission($ass_id, $member_id)
1306  {
1307 
1308  global $ilDB, $lng;
1309 
1310  $q="SELECT exc_mem_ass_status.status_time, exc_returned.ts ".
1311  "FROM exc_mem_ass_status, exc_returned ".
1312  "WHERE exc_mem_ass_status.status_time < exc_returned.ts ".
1313  "AND NOT exc_mem_ass_status.status_time IS NULL ".
1314  "AND exc_returned.ass_id = exc_mem_ass_status.ass_id ".
1315  "AND exc_returned.user_id = exc_mem_ass_status.usr_id ".
1316  "AND exc_returned.ass_id=".$ilDB->quote($ass_id, "integer")." AND exc_returned.user_id=".
1317  $ilDB->quote($member_id, "integer");
1318 
1319  $usr_set = $ilDB->query($q);
1320 
1321  $array = $ilDB->fetchAssoc($usr_set);
1322 
1323  if (count($array)==0)
1324  {
1325  return 0;
1326  }
1327  else
1328  {
1329  return 1;
1330  }
1331 
1332  }
1333 
1338  static function lookupNewFiles($ass_id, $member_id)
1339  {
1340  global $ilDB, $ilUser;
1341 
1342  $q = "SELECT exc_returned.returned_id AS id ".
1343  "FROM exc_usr_tutor, exc_returned ".
1344  "WHERE exc_returned.ass_id = exc_usr_tutor.ass_id ".
1345  " AND exc_returned.user_id = exc_usr_tutor.usr_id ".
1346  " AND exc_returned.ass_id = ".$ilDB->quote($ass_id, "integer").
1347  " AND exc_returned.user_id = ".$ilDB->quote($member_id, "integer").
1348  " AND exc_usr_tutor.tutor_id = ".$ilDB->quote($ilUser->getId(), "integer").
1349  " AND exc_usr_tutor.download_time < exc_returned.ts ";
1350 
1351  $new_up_set = $ilDB->query($q);
1352 
1353  $new_up = array();
1354  while ($new_up_rec = $ilDB->fetchAssoc($new_up_set))
1355  {
1356  $new_up[] = $new_up_rec["id"];
1357  }
1358 
1359  return $new_up;
1360  }
1361 
1365  function getMemberListData($a_exc_id, $a_ass_id)
1366  {
1367  global $ilDB;
1368 
1369  $mem = array();
1370 
1371  // first get list of members from member table
1372  $set = $ilDB->query("SELECT * FROM exc_members ".
1373  "WHERE obj_id = ".$ilDB->quote($a_exc_id, "integer"));
1374  while($rec = $ilDB->fetchAssoc($set))
1375  {
1376  if (ilObject::_exists($rec["usr_id"]) &&
1377  (ilObject::_lookupType($rec["usr_id"]) == "usr"))
1378  {
1379  $name = ilObjUser::_lookupName($rec["usr_id"]);
1380  $login = ilObjUser::_lookupLogin($rec["usr_id"]);
1381  $mem[$rec["usr_id"]] =
1382  array(
1383  "name" => $name["lastname"].", ".$name["firstname"],
1384  "login" => $login,
1385  "usr_id" => $rec["usr_id"],
1386  "lastname" => $name["lastname"],
1387  "firstname" => $name["firstname"]
1388  );
1389  }
1390  }
1391 
1392  $q = "SELECT * FROM exc_mem_ass_status ".
1393  "WHERE ass_id = ".$ilDB->quote($a_ass_id, "integer");
1394  $set = $ilDB->query($q);
1395  while($rec = $ilDB->fetchAssoc($set))
1396  {
1397  if (isset($mem[$rec["usr_id"]]))
1398  {
1399  $mem[$rec["usr_id"]]["sent_time"] = $rec["sent_time"];
1400  $mem[$rec["usr_id"]]["submission"] = ilExAssignment::getLastSubmission($a_ass_id, $rec["usr_id"]);
1401  $mem[$rec["usr_id"]]["status_time"] = $rec["status_time"];
1402  $mem[$rec["usr_id"]]["feedback_time"] = $rec["feedback_time"];
1403  $mem[$rec["usr_id"]]["notice"] = $rec["notice"];
1404  $mem[$rec["usr_id"]]["status"] = $rec["status"];
1405  }
1406  }
1407  return $mem;
1408  }
1409 
1413  static function createNewUserRecords($a_user_id, $a_exc_id)
1414  {
1415  global $ilDB;
1416 
1417  $ass_data = ilExAssignment::getAssignmentDataOfExercise($a_exc_id);
1418  foreach ($ass_data as $ass)
1419  {
1420 //echo "-".$ass["id"]."-".$a_user_id."-";
1421  $ilDB->replace("exc_mem_ass_status", array(
1422  "ass_id" => array("integer", $ass["id"]),
1423  "usr_id" => array("integer", $a_user_id)
1424  ), array(
1425  "status" => array("text", "notgraded")
1426  ));
1427  }
1428  }
1429 
1433  static function createNewAssignmentRecords($a_ass_id, $a_exc)
1434  {
1435  global $ilDB;
1436 
1437  include_once("./Modules/Exercise/classes/class.ilExerciseMembers.php");
1438  $exmem = new ilExerciseMembers($a_exc);
1439  $mems = $exmem->getMembers();
1440 
1441  foreach ($mems as $mem)
1442  {
1443  $ilDB->replace("exc_mem_ass_status", array(
1444  "ass_id" => array("integer", $a_ass_id),
1445  "usr_id" => array("integer", $mem)
1446  ), array(
1447  "status" => array("text", "notgraded")
1448  ));
1449  }
1450  }
1451 
1456  function uploadAssignmentFiles($a_files)
1457  {
1458  include_once("./Modules/Exercise/classes/class.ilFSStorageExercise.php");
1459  $storage = new ilFSStorageExercise($this->getExerciseId(), $this->getId());
1460  $storage->create();
1461  $storage->uploadAssignmentFiles($a_files);
1462  }
1463 
1464 }
1465 
1466 ?>