ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilObjExercise.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once "classes/class.ilObject.php";
5 require_once "./Modules/Exercise/classes/class.ilFileDataExercise.php";
6 require_once "./Modules/Exercise/classes/class.ilExerciseMembers.php";
7 
19 class ilObjExercise extends ilObject
20 {
21  var $file_obj;
23  var $files;
24 
26  var $hour;
27  var $minutes;
28  var $day;
29  var $month;
30  var $year;
32 
39  function ilObjExercise($a_id = 0,$a_call_by_reference = true)
40  {
41  $this->type = "exc";
42  $this->ilObject($a_id,$a_call_by_reference);
43  }
44 
45  // SET, GET METHODS
46  function setDate($a_hour,$a_minutes,$a_day,$a_month,$a_year)
47  {
48  $this->hour = (int) $a_hour;
49  $this->minutes = (int) $a_minutes;
50  $this->day = (int) $a_day;
51  $this->month = (int) $a_month;
52  $this->year = (int) $a_year;
53  $this->timestamp = mktime($this->hour,$this->minutes,0,$this->month,$this->day,$this->year);
54  return true;
55  }
56  function getTimestamp()
57  {
58  return $this->timestamp;
59  }
60  function setTimestamp($a_timestamp)
61  {
62  $this->timestamp = $a_timestamp;
63  }
64  function setInstruction($a_instruction)
65  {
66  $this->instruction = $a_instruction;
67  }
68  function getInstruction()
69  {
70  return $this->instruction;
71  }
72  function getFiles()
73  {
74  return $this->files;
75  }
76 
77  function checkDate()
78  {
79  return $this->hour == (int) date("H",$this->timestamp) and
80  $this->minutes == (int) date("i",$this->timestamp) and
81  $this->day == (int) date("d",$this->timestamp) and
82  $this->month == (int) date("m",$this->timestamp) and
83  $this->year == (int) date("Y",$this->timestamp);
84 
85  }
86 
87  function deliverFile($a_http_post_files, $user_id, $unzip = false)
88  {
89  global $ilDB;
90 
91  $deliver_result = $this->file_obj->deliverFile($a_http_post_files, $user_id, $unzip);
92 //var_dump($deliver_result);
93  if ($deliver_result)
94  {
95  $next_id = $ilDB->nextId("exc_returned");
96  $query = sprintf("INSERT INTO exc_returned ".
97  "(returned_id, obj_id, user_id, filename, filetitle, mimetype, ts) ".
98  "VALUES (%s, %s, %s, %s, %s, %s, %s)",
99  $ilDB->quote($next_id, "integer"),
100  $ilDB->quote($this->getId(), "integer"),
101  $ilDB->quote($user_id, "integer"),
102  $ilDB->quote($deliver_result["fullname"], "text"),
103  $ilDB->quote($a_http_post_files["name"], "text"),
104  $ilDB->quote($deliver_result["mimetype"], "text"),
105  $ilDB->quote(ilUtil::now(), "timestamp")
106  );
107  $ilDB->manipulate($query);
108  if (!$this->members_obj->isAssigned($user_id))
109  {
110  $this->members_obj->assignMember($user_id);
111  }
112  $this->members_obj->setStatusReturnedForMember($user_id, 1);
113  }
114  return true;
115  }
116 
117  function addUploadedFile($a_http_post_files, $unzipUploadedFile = false)
118  {
119  global $lng;
120  if ($unzipUploadedFile && preg_match("/zip/", $a_http_post_files["type"]) == 1)
121  {
122 
123  $this->processUploadedFile($a_http_post_files["tmp_name"], "storeUploadedFile", true);
124  return true;
125 
126 
127  }
128  else
129  {
130  $this->file_obj->storeUploadedFile($a_http_post_files, true);
131  return true;
132  }
133  }
134  function deleteFiles($a_files)
135  {
136  $this->file_obj->unlinkFiles($a_files);
137  }
138 
139  function saveData()
140  {
141  global $ilDB;
142 
143  // SAVE ONLY EXERCISE SPECIFIC DATA
144  /*$query = "INSERT INTO exc_data SET ".
145  "obj_id = ".$ilDB->quote($this->getId()).", ".
146  "instruction = ".$ilDB->quote($this->getInstruction()).", ".
147  "time_stamp = ".$ilDB->quote($this->getTimestamp());
148  $this->ilias->db->query($query);*/
149 
150  $ilDB->insert("exc_data", array(
151  "obj_id" => array("integer", $this->getId()),
152  "instruction" => array("clob", $this->getInstruction()),
153  "time_stamp" => array("integer", $this->getTimestamp())
154  ));
155  return true;
156  }
157 
165  public function cloneObject($a_target_id,$a_copy_id = 0)
166  {
167  global $ilDB;
168 
169  // Copy settings
170  $new_obj = parent::cloneObject($a_target_id,$a_copy_id);
171  $new_obj->setInstruction($this->getInstruction());
172  $new_obj->setTimestamp($this->getTimestamp());
173  $new_obj->saveData();
174 
175  // Copy files
176  $tmp_file_obj =& new ilFileDataExercise($this->getId());
177  $tmp_file_obj->ilClone($new_obj->getId());
178  unset($tmp_file_obj);
179 
180  // Copy learning progress settings
181  include_once('Services/Tracking/classes/class.ilLPObjSettings.php');
182  $obj_settings = new ilLPObjSettings($this->getId());
183  $obj_settings->cloneSettings($new_obj->getId());
184  unset($obj_settings);
185 
186  return $new_obj;
187  }
188 
189 
196  function &getDeliveredFiles($user_id)
197  {
198  $delivered_files =& $this->members_obj->getDeliveredFiles($user_id);
199  return $delivered_files;
200  }
201 
208  function deleteDeliveredFiles($file_id_array, $user_id)
209  {
210  $this->members_obj->deleteDeliveredFiles($file_id_array, $user_id);
211 
212  // Finally update status 'returned' of member if no file exists
213  if(!count($this->members_obj->getDeliveredFiles($user_id)))
214  {
215  $this->members_obj->setStatusReturnedForMember($user_id,0);
216  }
217  }
218 
224  function deliverReturnedFiles($user_id)
225  {
226  require_once "./Services/Utilities/classes/class.ilUtil.php";
227  }
228 
235  function delete()
236  {
237  global $ilDB;
238 
239  // always call parent delete function first!!
240  if (!parent::delete())
241  {
242  return false;
243  }
244  // put here course specific stuff
245  $ilDB->manipulate("DELETE FROM exc_data ".
246  "WHERE obj_id = ".$ilDB->quote($this->getId(), "integer"));
247 
248  //$this->ilias->db->query($query);
249 
250  $this->file_obj->delete();
251  $this->members_obj->delete();
252 
253  return true;
254  }
255 
266  function notify($a_event,$a_ref_id,$a_node_id,$a_params = 0)
267  {
268  // object specific event handling
269 
270  parent::notify($a_event,$a_ref_id,$a_node_id,$a_params);
271  }
272 
273  function read()
274  {
275  global $ilDB;
276 
277  parent::read();
278 
279  $query = "SELECT * FROM exc_data ".
280  "WHERE obj_id = ".$ilDB->quote($this->getId(), "integer");
281 
282  $res = $ilDB->query($query);
283  while($row = $ilDB->fetchObject($res))
284  {
285  $this->setInstruction($row->instruction);
286  $this->setTimestamp($row->time_stamp);
287  }
288  $this->members_obj =& new ilExerciseMembers($this->getId(),$this->getRefId());
289  $this->members_obj->read();
290 
291  // GET FILE ASSIGNED TO EXERCISE
292  $this->file_obj = new ilFileDataExercise($this->getId());
293  $this->files = $this->file_obj->getFiles();
294  $this->files = ilUtil::sortArray($this->files, "name", "asc");
295  return true;
296  }
297 
298  function update()
299  {
300  global $ilDB;
301 
302  parent::update();
303 
304  /*$query = "UPDATE exc_data SET ".
305  "instruction = ".$ilDB->quote($this->getInstruction()).", ".
306  "time_stamp = ".$ilDB->quote($this->getTimestamp())." ".
307  "WHERE obj_id = ".$ilDB->quote($this->getId());
308  */
309 
310  $ilDB->update("exc_data", array(
311  "instruction" => array("clob", $this->getInstruction()),
312  "time_stamp" => array("integer", $this->getTimestamp())
313  ), array(
314  "obj_id" => array("integer", $this->getId())
315  ));
316 
317  //$res = $this->ilias->db->query($query);
318 
319  #$this->members_obj->update();
320  return true;
321  }
322 
326  function getMemberListData()
327  {
328  global $ilDB;
329 
330  $mem = array();
331  $q = "SELECT * FROM exc_members ".
332  "WHERE obj_id = ".$ilDB->quote($this->getId(), "integer");
333  $set = $ilDB->query($q);
334  while($rec = $ilDB->fetchAssoc($set))
335  {
336  if (ilObject::_exists($rec["usr_id"]) &&
337  (ilObject::_lookupType($rec["usr_id"]) == "usr"))
338  {
339  $name = ilObjUser::_lookupName($rec["usr_id"]);
340  $login = ilObjUser::_lookupLogin($rec["usr_id"]);
341  $mem[] =
342  array(
343  "name" => $name["lastname"].", ".$name["firstname"],
344  "login" => $login,
345  "sent_time" => $rec["sent_time"],
346  "submission" => $this->getLastSubmission($rec["usr_id"]),
347  "status_time" => $rec["status_time"],
348  "feedback_time" => $rec["feedback_time"],
349  "usr_id" => $rec["usr_id"],
350  "lastname" => $name["lastname"],
351  "firstname" => $name["firstname"],
352  "notice" => $rec["notice"],
353  "status" => $rec["status"]
354  );
355  }
356  }
357  return $mem;
358  }
359 
366  function getLastSubmission($member_id)
367  {
368  global $ilDB, $lng;
369 
370  $q="SELECT obj_id,user_id,ts FROM exc_returned ".
371  "WHERE obj_id =".$ilDB->quote($this->getId(), "integer")." AND user_id=".
372  $ilDB->quote($member_id, "integer").
373  " ORDER BY ts DESC";
374 
375  $usr_set = $ilDB->query($q);
376 
377  $array = $ilDB->fetchAssoc($usr_set);
378  if ($array["ts"]==NULL)
379  {
380  return false;
381  }
382  else
383  {
384  return ilUtil::getMySQLTimestamp($array["ts"]);
385  }
386  }
387 
391  function send($a_members)
392  {
393  $files = $this->file_obj->getFiles();
394  if(count($files))
395  {
396  include_once "./classes/class.ilFileDataMail.php";
397 
398  $mfile_obj = new ilFileDataMail($_SESSION["AccountId"]);
399  foreach($files as $file)
400  {
401  $mfile_obj->copyAttachmentFile($this->file_obj->getAbsolutePath($file["name"]),$file["name"]);
402  $file_names[] = $file["name"];
403  }
404  }
405 
406  include_once "Services/Mail/classes/class.ilMail.php";
407 
408  $tmp_mail_obj = new ilMail($_SESSION["AccountId"]);
409  $message = $tmp_mail_obj->sendMail($this->__formatRecipients($a_members),"","",$this->__formatSubject(),$this->__formatBody(),
410  count($file_names) ? $file_names : array(),array("normal"));
411 
412  unset($tmp_mail_obj);
413 
414  if(count($file_names))
415  {
416  $mfile_obj->unlinkFiles($file_names);
417  unset($mfile_obj);
418  }
419 
420 
421  // SET STATUS SENT FOR ALL RECIPIENTS
422  foreach($a_members as $member_id => $value)
423  {
424  $this->members_obj->setStatusSentForMember($member_id,1);
425  }
426 
427  return true;
428  }
429 
434  function _lookupUpdatedSubmission($exc_id, $member_id)
435  {
436 
437  global $ilDB, $lng;
438 
439  $q="SELECT exc_members.status_time, exc_returned.ts ".
440  "FROM exc_members, exc_returned ".
441  "WHERE exc_members.status_time < exc_returned.ts ".
442  "AND NOT exc_members.status_time IS NULL ".
443  "AND exc_returned.obj_id = exc_members.obj_id ".
444  "AND exc_returned.user_id = exc_members.usr_id ".
445  "AND exc_returned.obj_id=".$ilDB->quote($exc_id, "integer")." AND exc_returned.user_id=".
446  $ilDB->quote($member_id, "integer");
447 
448  $usr_set = $ilDB->query($q);
449 
450  $array = $ilDB->fetchAssoc($usr_set);
451 
452  if (count($array)==0)
453  {
454  return 0;
455  }
456  else
457  {
458  return 1;
459  }
460 
461  }
462 
463 
467  function _lookupAnyExerciseSent($a_exc_id)
468  {
469  global $ilDB;
470 
471  $q = "SELECT count(*) AS cnt FROM exc_members".
472  " WHERE NOT sent_time IS NULL".
473  " AND obj_id = ".$ilDB->quote($a_exc_id, "integer")." ".
474  " ";
475  $set = $ilDB->query($q);
476  $rec = $ilDB->fetchAssoc($set);
477 
478  if ($rec["cnt"] > 0)
479  {
480  return true;
481  }
482  else
483  {
484  return false;
485  }
486  }
487 
492  function _lookupNewFiles($exc_id, $member_id)
493  {
494  global $ilDB, $ilUser;
495 
496  $q = "SELECT exc_returned.returned_id AS id ".
497  "FROM exc_usr_tutor, exc_returned ".
498  "WHERE exc_returned.obj_id = exc_usr_tutor.obj_id ".
499  " AND exc_returned.user_id = exc_usr_tutor.usr_id ".
500  " AND exc_returned.obj_id = ".$ilDB->quote($exc_id, "integer").
501  " AND exc_returned.user_id = ".$ilDB->quote($member_id, "integer").
502  " AND exc_usr_tutor.tutor_id = ".$ilDB->quote($ilUser->getId(), "integer").
503  " AND exc_usr_tutor.download_time < exc_returned.ts ";
504 
505  $new_up_set = $ilDB->query($q);
506 
507  $new_up = array();
508  while ($new_up_rec = $ilDB->fetchAssoc($new_up_set))
509  {
510  $new_up[] = $new_up_rec["id"];
511  }
512 
513  return $new_up;
514  }
515 
519  function _lookupStatusTime($exc_id, $member_id)
520  {
521 
522  global $ilDB, $lng;
523 
524  $q = "SELECT * ".
525  "FROM exc_members ".
526  "WHERE obj_id= ".$ilDB->quote($exc_id, "integer").
527  " AND usr_id= ".$ilDB->quote($member_id, "integer");
528 
529  $set = $ilDB->query($q);
530  if ($rec = $ilDB->fetchAssoc($set))
531  {
532  return ilUtil::getMySQLTimestamp($rec["status_time"]);
533  }
534  }
535 
539  function _lookupSentTime($exc_id, $member_id)
540  {
541 
542  global $ilDB, $lng;
543 
544  $q = "SELECT * ".
545  "FROM exc_members ".
546  "WHERE obj_id= ".$ilDB->quote($exc_id, "integer").
547  " AND usr_id= ".$ilDB->quote($member_id, "integer");
548 
549  $set = $ilDB->query($q);
550  if ($rec = $ilDB->fetchAssoc($set))
551  {
552  return ilUtil::getMySQLTimestamp($rec["sent_time"]);
553  }
554  }
555 
559  function _lookupFeedbackTime($exc_id, $member_id)
560  {
561 
562  global $ilDB, $lng;
563 
564  $q = "SELECT * ".
565  "FROM exc_members ".
566  "WHERE obj_id= ".$ilDB->quote($exc_id, "integer").
567  " AND usr_id= ".$ilDB->quote($member_id, "integer");
568 
569  $set = $ilDB->query($q);
570  if ($rec = $ilDB->fetchAssoc($set))
571  {
572  return ilUtil::getMySQLTimestamp($rec["feedback_time"]);
573  }
574  }
575 
576  // PRIVATE METHODS
577  function __formatBody()
578  {
579  global $lng;
580 
581  $body = $this->getInstruction();
582  $body .= "\n\n";
583  $body .= $lng->txt("exc_edit_until") . ": ".
584  ilFormat::formatDate(date("Y-m-d H:i:s",$this->getTimestamp()), "datetime", true);
585  $body .= "\n\n";
586  $body .= ILIAS_HTTP_PATH.
587  "/goto.php?target=".
588  $this->getType().
589  "_".$this->getRefId()."&client_id=".CLIENT_ID;
590 
591  return $body;
592  }
593 
594  function __formatSubject()
595  {
596  return $subject = $this->getTitle()." (".$this->getDescription().")";
597  }
598 
599  function __formatRecipients($a_members)
600  {
601  foreach($a_members as $member_id => $value)
602  {
603  $tmp_obj = ilObjectFactory::getInstanceByObjId($member_id);
604  $tmp_members[] = $tmp_obj->getLogin();
605 
606  unset($tmp_obj);
607  }
608 
609  return implode(',',$tmp_members ? $tmp_members : array());
610  }
611 
612  function _checkCondition($a_exc_id,$a_operator,$a_value,$a_usr_id = 0)
613  {
614  global $ilUser;
615 
616  $a_usr_id = $a_usr_id ? $a_usr_id : $ilUser->getId();
617 
618  switch($a_operator)
619  {
620  case 'passed':
621  if (ilExerciseMembers::_lookupStatus($a_exc_id, $a_usr_id) == "passed")
622  {
623  return true;
624  }
625  else
626  {
627  return false;
628  }
629  break;
630 
631  default:
632  return true;
633  }
634  return true;
635  }
636 
643  function processUploadedFile ($fileTmp, $storageMethod, $persistentErrorMessage)
644  {
645  global $lng, $ilUser;
646 
647  // Create unzip-directory
648  $newDir = ilUtil::ilTempnam();
649  ilUtil::makeDir($newDir);
650 
651  include_once ("Services/Utilities/classes/class.ilFileUtils.php");
652 
653  try
654  {
655  $processDone = ilFileUtils::processZipFile($newDir,$fileTmp, false);
656  ilFileUtils::recursive_dirscan($newDir, $filearray);
657 
658  foreach ($filearray["file"] as $key => $filename)
659  {
660  $a_http_post_files["name"] = ilFileUtils::utf8_encode($filename);
661  $a_http_post_files["type"] = "other";
662  $a_http_post_files["tmp_name"] = $filearray["path"][$key]."/".$filename;
663  $a_http_post_files["error"] = 0;
664  $a_http_post_files["size"] = filesize($filearray["path"][$key]."/".$filename);
665 
666  if ($storageMethod == "deliverFile")
667  {
668  $this->$storageMethod($a_http_post_files, $ilUser->id, true);
669  }
670  else if ($storageMethod == "storeUploadedFile")
671  {
672  $this->file_obj->$storageMethod($a_http_post_files, true, true);
673  }
674  }
675  ilUtil::sendSuccess($this->lng->txt("file_added"),$persistentErrorMessage);
676 
677  }
678  catch (ilFileUtilsException $e)
679  {
680  ilUtil::sendFailure($e->getMessage(), $persistentErrorMessage);
681  }
682 
683 
684  ilUtil::delDir($newDir);
685  return $processDone;
686 
687  }
688 
699  static function _fixFilename($a_filename)
700  {
701  $ex_pos = strrpos($a_filename, "/exercise/");
702  if ($ex_pos > 0)
703  {
704  $a_filename = CLIENT_DATA_DIR.substr($a_filename, $ex_pos);
705  }
706  return $a_filename;
707  }
708 
713  static function _fixFilenameArray($a_array)
714  {
715  if (is_array($a_array))
716  {
717  foreach ($a_array as $k => $v)
718  {
719  if ($v["filename"] != "")
720  {
721  $a_array[$k]["filename"] = ilObjExercise::_fixFilename($a_array[$k]["filename"]);
722  }
723  }
724  }
725 
726  return $a_array;
727  }
728 
729 } //END class.ilObjExercise
730 ?>