ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilObjExercise.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 require_once "classes/class.ilObject.php";
25 require_once "./Modules/Exercise/classes/class.ilFileDataExercise.php";
26 require_once "./Modules/Exercise/classes/class.ilExerciseMembers.php";
27 
39 class ilObjExercise extends ilObject
40 {
41  var $file_obj;
43  var $files;
44 
46  var $hour;
47  var $minutes;
48  var $day;
49  var $month;
50  var $year;
52 
59  function ilObjExercise($a_id = 0,$a_call_by_reference = true)
60  {
61  $this->type = "exc";
62  $this->ilObject($a_id,$a_call_by_reference);
63  }
64 
65  // SET, GET METHODS
66  function setDate($a_hour,$a_minutes,$a_day,$a_month,$a_year)
67  {
68  $this->hour = (int) $a_hour;
69  $this->minutes = (int) $a_minutes;
70  $this->day = (int) $a_day;
71  $this->month = (int) $a_month;
72  $this->year = (int) $a_year;
73  $this->timestamp = mktime($this->hour,$this->minutes,0,$this->month,$this->day,$this->year);
74  return true;
75  }
76  function getTimestamp()
77  {
78  return $this->timestamp;
79  }
80  function setTimestamp($a_timestamp)
81  {
82  $this->timestamp = $a_timestamp;
83  }
84  function setInstruction($a_instruction)
85  {
86  $this->instruction = $a_instruction;
87  }
88  function getInstruction()
89  {
90  return $this->instruction;
91  }
92  function getFiles()
93  {
94  return $this->files;
95  }
96 
97  function checkDate()
98  {
99  return $this->hour == (int) date("H",$this->timestamp) and
100  $this->minutes == (int) date("i",$this->timestamp) and
101  $this->day == (int) date("d",$this->timestamp) and
102  $this->month == (int) date("m",$this->timestamp) and
103  $this->year == (int) date("Y",$this->timestamp);
104 
105  }
106 
107  function deliverFile($a_http_post_files, $user_id, $unzip = false)
108  {
109  $deliver_result = $this->file_obj->deliverFile($a_http_post_files, $user_id, $unzip);
110 //var_dump($deliver_result);
111  if ($deliver_result)
112  {
113  $query = sprintf("INSERT INTO exc_returned ".
114  "(returned_id, obj_id, user_id, filename, filetitle, mimetype, TIMESTAMP) ".
115  "VALUES (NULL, %s, %s, %s, %s, %s, NULL)",
116  $this->ilias->db->quote($this->getId() . ""),
117  $this->ilias->db->quote($user_id . ""),
118  $this->ilias->db->quote($deliver_result["fullname"]),
119  $this->ilias->db->quote($a_http_post_files["name"]),
120  $this->ilias->db->quote($deliver_result["mimetype"])
121  );
122  $this->ilias->db->query($query);
123  if (!$this->members_obj->isAssigned($user_id))
124  {
125  $this->members_obj->assignMember($user_id);
126  }
127  $this->members_obj->setStatusReturnedForMember($user_id, 1);
128  }
129  return true;
130  }
131 
132  function addUploadedFile($a_http_post_files, $unzipUploadedFile = false)
133  {
134  global $lng;
135  if ($unzipUploadedFile && preg_match("/zip/", $a_http_post_files["type"]) == 1)
136  {
137 
138  $this->processUploadedFile($a_http_post_files["tmp_name"], "storeUploadedFile", true);
139  return true;
140 
141 
142  }
143  else
144  {
145  $this->file_obj->storeUploadedFile($a_http_post_files, true);
146  return true;
147  }
148  }
149  function deleteFiles($a_files)
150  {
151  $this->file_obj->unlinkFiles($a_files);
152  }
153 
154  function saveData()
155  {
156  global $ilDB;
157 
158  // SAVE ONLY EXERCISE SPECIFIC DATA
159  $query = "INSERT INTO exc_data SET ".
160  "obj_id = ".$ilDB->quote($this->getId()).", ".
161  "instruction = ".$ilDB->quote($this->getInstruction()).", ".
162  "time_stamp = ".$ilDB->quote($this->getTimestamp());
163  $this->ilias->db->query($query);
164  return true;
165  }
166 
174  public function cloneObject($a_target_id,$a_copy_id = 0)
175  {
176  global $ilDB;
177 
178  // Copy settings
179  $new_obj = parent::cloneObject($a_target_id,$a_copy_id);
180  $new_obj->setInstruction($this->getInstruction());
181  $new_obj->setTimestamp($this->getTimestamp());
182  $new_obj->saveData();
183 
184  // Copy files
185  $tmp_file_obj =& new ilFileDataExercise($this->getId());
186  $tmp_file_obj->ilClone($new_obj->getId());
187  unset($tmp_file_obj);
188 
189  // Copy learning progress settings
190  include_once('Services/Tracking/classes/class.ilLPObjSettings.php');
191  $obj_settings = new ilLPObjSettings($this->getId());
192  $obj_settings->cloneSettings($new_obj->getId());
193  unset($obj_settings);
194 
195  return $new_obj;
196  }
197 
198 
205  function &getDeliveredFiles($user_id)
206  {
207  $delivered_files =& $this->members_obj->getDeliveredFiles($user_id);
208  return $delivered_files;
209  }
210 
217  function deleteDeliveredFiles($file_id_array, $user_id)
218  {
219  $this->members_obj->deleteDeliveredFiles($file_id_array, $user_id);
220 
221  // Finally update status 'returned' of member if no file exists
222  if(!count($this->members_obj->getDeliveredFiles($user_id)))
223  {
224  $this->members_obj->setStatusReturnedForMember($user_id,0);
225  }
226 
227  }
228 
234  function deliverReturnedFiles($user_id)
235  {
236  require_once "./Services/Utilities/classes/class.ilUtil.php";
237  }
238 
245  function delete()
246  {
247  global $ilDB;
248 
249  // always call parent delete function first!!
250  if (!parent::delete())
251  {
252  return false;
253  }
254  // put here course specific stuff
255  $query = "DELETE FROM exc_data ".
256  "WHERE obj_id = ".$ilDB->quote($this->getId());
257 
258  $this->ilias->db->query($query);
259 
260  $this->file_obj->delete();
261  $this->members_obj->delete();
262 
263  return true;
264  }
265 
276  function notify($a_event,$a_ref_id,$a_node_id,$a_params = 0)
277  {
278  // object specific event handling
279 
280  parent::notify($a_event,$a_ref_id,$a_node_id,$a_params);
281  }
282 
283  function read()
284  {
285  global $ilDB;
286 
287  parent::read();
288 
289  $query = "SELECT * FROM exc_data ".
290  "WHERE obj_id = ".$ilDB->quote($this->getId());
291 
292  $res = $this->ilias->db->query($query);
293  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
294  {
295  $this->setInstruction($row->instruction);
296  $this->setTimestamp($row->time_stamp);
297  }
298  $this->members_obj =& new ilExerciseMembers($this->getId(),$this->getRefId());
299  $this->members_obj->read();
300 
301  // GET FILE ASSIGNED TO EXERCISE
302  $this->file_obj = new ilFileDataExercise($this->getId());
303  $this->files = $this->file_obj->getFiles();
304  $this->files = ilUtil::sortArray($this->files, "name", "asc");
305  return true;
306  }
307 
308  function update()
309  {
310  global $ilDB;
311 
312  parent::update();
313 
314  $query = "UPDATE exc_data SET ".
315  "instruction = ".$ilDB->quote($this->getInstruction()).", ".
316  "time_stamp = ".$ilDB->quote($this->getTimestamp())." ".
317  "WHERE obj_id = ".$ilDB->quote($this->getId());
318 
319  $res = $this->ilias->db->query($query);
320 
321  #$this->members_obj->update();
322  return true;
323  }
324 
328  function getMemberListData()
329  {
330  global $ilDB;
331 
332  $mem = array();
333  $q = "SELECT * FROM exc_members ".
334  "WHERE obj_id = ".$ilDB->quote($this->getId());
335  $set = $ilDB->query($q);
336  while($rec = $set->fetchRow(DB_FETCHMODE_ASSOC))
337  {
338  if (ilObject::_exists($rec["usr_id"]) &&
339  (ilObject::_lookupType($rec["usr_id"]) == "usr"))
340  {
341  $name = ilObjUser::_lookupName($rec["usr_id"]);
342  $login = ilObjUser::_lookupLogin($rec["usr_id"]);
343  $mem[] =
344  array(
345  "name" => $name["lastname"].", ".$name["firstname"],
346  "login" => $login,
347  "sent_time" => $rec["sent_time"],
348  "submission" => $this->getLastSubmission($rec["usr_id"]),
349  "status_time" => $rec["status_time"],
350  "feedback_time" => $rec["feedback_time"],
351  "usr_id" => $rec["usr_id"],
352  "lastname" => $name["lastname"],
353  "firstname" => $name["firstname"],
354  "notice" => $rec["notice"],
355  "status" => $rec["status"]
356  );
357  }
358  }
359  return $mem;
360  }
361 
368  function getLastSubmission($member_id)
369  {
370  global $ilDB, $lng;
371 
372  $q="SELECT obj_id,user_id,timestamp FROM exc_returned ".
373  "WHERE obj_id =".$ilDB->quote($this->getId())." AND user_id=".
374  $ilDB->quote($member_id).
375  " ORDER BY timestamp DESC";
376 
377  $usr_set = $ilDB->query($q);
378 
379  $array=$usr_set->fetchRow(DB_FETCHMODE_ASSOC);
380  if ($array["timestamp"]==NULL)
381  {
382  return false;
383  }
384  else
385  {
386  return ilUtil::getMySQLTimestamp($array["timestamp"]);
387  }
388  }
389 
393  function send($a_members)
394  {
395  $files = $this->file_obj->getFiles();
396  if(count($files))
397  {
398  include_once "./classes/class.ilFileDataMail.php";
399 
400  $mfile_obj = new ilFileDataMail($_SESSION["AccountId"]);
401  foreach($files as $file)
402  {
403  $mfile_obj->copyAttachmentFile($this->file_obj->getAbsolutePath($file["name"]),$file["name"]);
404  $file_names[] = $file["name"];
405  }
406  }
407 
408  include_once "Services/Mail/classes/class.ilMail.php";
409 
410  $tmp_mail_obj = new ilMail($_SESSION["AccountId"]);
411  $message = $tmp_mail_obj->sendMail($this->__formatRecipients($a_members),"","",$this->__formatSubject(),$this->__formatBody(),
412  count($file_names) ? $file_names : array(),array("normal"));
413 
414  unset($tmp_mail_obj);
415 
416  if(count($file_names))
417  {
418  $mfile_obj->unlinkFiles($file_names);
419  unset($mfile_obj);
420  }
421 
422 
423  // SET STATUS SENT FOR ALL RECIPIENTS
424  foreach($a_members as $member_id => $value)
425  {
426  $this->members_obj->setStatusSentForMember($member_id,1);
427  }
428 
429  return true;
430  }
431 
436  function _lookupUpdatedSubmission($exc_id, $member_id)
437  {
438 
439  global $ilDB, $lng;
440 
441  $q="SELECT exc_members.status_time, exc_returned.timestamp ".
442  "FROM exc_members, exc_returned ".
443  "WHERE exc_members.status_time < exc_returned.timestamp ".
444  "AND exc_members.status_time <> '0000-00-00 00:00:00' ".
445  "AND exc_returned.obj_id = exc_members.obj_id ".
446  "AND exc_returned.user_id = exc_members.usr_id ".
447  "AND exc_returned.obj_id=".$ilDB->quote($exc_id)." AND exc_returned.user_id=".
448  $ilDB->quote($member_id);
449 
450  $usr_set = $ilDB->query($q);
451 
452  $array=$usr_set->fetchRow(DB_FETCHMODE_ASSOC);
453 
454  if (count($array)==0)
455  {
456  return 0;
457  }
458  else
459  {
460  return 1;
461  }
462 
463  }
464 
465 
469  function _lookupAnyExerciseSent($a_exc_id)
470  {
471  global $ilDB;
472 
473  $q = "SELECT count(*) AS cnt FROM exc_members".
474  " WHERE sent_time <> '0000-00-00 00:00:00'".
475  " AND obj_id = ".$ilDB->quote($a_exc_id);
476  $set = $ilDB->query($q);
477  $rec = $set->fetchRow(DB_FETCHMODE_ASSOC);
478 
479  if ($rec["cnt"] > 0)
480  {
481  return true;
482  }
483  else
484  {
485  return false;
486  }
487  }
488 
493  function _lookupNewFiles($exc_id, $member_id)
494  {
495  global $ilDB, $ilUser;
496 
497  $q = "SELECT exc_returned.returned_id AS id ".
498  "FROM exc_usr_tutor, exc_returned ".
499  "WHERE exc_returned.obj_id = exc_usr_tutor.obj_id ".
500  "AND exc_returned.user_id = exc_usr_tutor.usr_id ".
501  "AND exc_returned.obj_id = ".$ilDB->quote($exc_id).
502  "AND exc_returned.user_id = ".$ilDB->quote($member_id).
503  "AND exc_usr_tutor.tutor_id = ".$ilDB->quote($ilUser->getId()).
504  "AND exc_usr_tutor.download_time < exc_returned.timestamp ";
505 
506  $new_up_set = $ilDB->query($q);
507 
508  $new_up = array();
509  while ($new_up_rec = $new_up_set->fetchRow(DB_FETCHMODE_ASSOC))
510  {
511  $new_up[] = $new_up_rec["id"];
512  }
513 
514  return $new_up;
515  }
516 
520  function _lookupStatusTime($exc_id, $member_id)
521  {
522 
523  global $ilDB, $lng;
524 
525  $q = "SELECT * ".
526  "FROM exc_members ".
527  "WHERE obj_id= ".$ilDB->quote($exc_id)." AND usr_id= ".$ilDB->quote($member_id);
528 
529  $set = $ilDB->query($q);
530  if ($rec = $set->fetchRow(DB_FETCHMODE_ASSOC))
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)." AND usr_id= ".$ilDB->quote($member_id);
547 
548  $set = $ilDB->query($q);
549  if ($rec = $set->fetchRow(DB_FETCHMODE_ASSOC))
550  {
551  return ilUtil::getMySQLTimestamp($rec["sent_time"]);
552  }
553  }
554 
558  function _lookupFeedbackTime($exc_id, $member_id)
559  {
560 
561  global $ilDB, $lng;
562 
563  $q = "SELECT * ".
564  "FROM exc_members ".
565  "WHERE obj_id= ".$ilDB->quote($exc_id)." AND usr_id= ".$ilDB->quote($member_id);
566 
567  $set = $ilDB->query($q);
568  if ($rec = $set->fetchRow(DB_FETCHMODE_ASSOC))
569  {
570  return ilUtil::getMySQLTimestamp($rec["feedback_time"]);
571  }
572  }
573 
574  // PRIVATE METHODS
575  function __formatBody()
576  {
577  global $lng;
578 
579  $body = $this->getInstruction();
580  $body .= "\n\n";
581  $body .= $lng->txt("exc_edit_until") . ": ".
582  ilFormat::formatDate(date("Y-m-d H:i:s",$this->getTimestamp()), "datetime", true);
583  $body .= "\n\n";
584  $body .= ILIAS_HTTP_PATH.
585  "/goto.php?target=".
586  $this->getType().
587  "_".$this->getRefId()."&client_id=".CLIENT_ID;
588 
589  return $body;
590  }
591 
592  function __formatSubject()
593  {
594  return $subject = $this->getTitle()." (".$this->getDescription().")";
595  }
596 
597  function __formatRecipients($a_members)
598  {
599  foreach($a_members as $member_id => $value)
600  {
601  $tmp_obj = ilObjectFactory::getInstanceByObjId($member_id);
602  $tmp_members[] = $tmp_obj->getLogin();
603 
604  unset($tmp_obj);
605  }
606 
607  return implode(',',$tmp_members ? $tmp_members : array());
608  }
609 
610  function _checkCondition($a_exc_id,$a_operator,$a_value,$a_usr_id = 0)
611  {
612  global $ilUser;
613 
614  $a_usr_id = $a_usr_id ? $a_usr_id : $ilUser->getId();
615 
616  switch($a_operator)
617  {
618  case 'passed':
619  if (ilExerciseMembers::_lookupStatus($a_exc_id, $a_usr_id) == "passed")
620  {
621  return true;
622  }
623  else
624  {
625  return false;
626  }
627  break;
628 
629  default:
630  return true;
631  }
632  return true;
633  }
634 
641  function processUploadedFile ($fileTmp, $storageMethod, $persistentErrorMessage)
642  {
643  global $lng, $ilUser;
644 
645  // Create unzip-directory
646  $newDir = ilUtil::ilTempnam();
647  ilUtil::makeDir($newDir);
648 
649  include_once ("Services/Utilities/classes/class.ilFileUtils.php");
650 
651  try
652  {
653  $processDone = ilFileUtils::processZipFile($newDir,$fileTmp, false);
654  ilFileUtils::recursive_dirscan($newDir, $filearray);
655 
656  foreach ($filearray["file"] as $key => $filename)
657  {
658  $a_http_post_files["name"] = ilFileUtils::utf8_encode($filename);
659  $a_http_post_files["type"] = "other";
660  $a_http_post_files["tmp_name"] = $filearray["path"][$key]."/".$filename;
661  $a_http_post_files["error"] = 0;
662  $a_http_post_files["size"] = filesize($filearray["path"][$key]."/".$filename);
663 
664  if ($storageMethod == "deliverFile")
665  {
666  $this->$storageMethod($a_http_post_files, $ilUser->id, true);
667  }
668  else if ($storageMethod == "storeUploadedFile")
669  {
670  $this->file_obj->$storageMethod($a_http_post_files, true, true);
671  }
672  }
673  ilUtil::sendInfo($this->lng->txt("file_added"),$persistentErrorMessage);
674 
675  }
676  catch (ilFileUtilsException $e)
677  {
678  ilUtil::sendInfo($e->getMessage(), $persistentErrorMessage);
679  }
680 
681 
682  ilUtil::delDir($newDir);
683  return $processDone;
684 
685  }
686 
697  static function _fixFilename($a_filename)
698  {
699  $ex_pos = strrpos($a_filename, "/exercise/");
700  if ($ex_pos > 0)
701  {
702  $a_filename = CLIENT_DATA_DIR.substr($a_filename, $ex_pos);
703  }
704  return $a_filename;
705  }
706 
711  static function _fixFilenameArray($a_array)
712  {
713  if (is_array($a_array))
714  {
715  foreach ($a_array as $k => $v)
716  {
717  if ($v["filename"] != "")
718  {
719  $a_array[$k]["filename"] = ilObjExercise::_fixFilename($a_array[$k]["filename"]);
720  }
721  }
722  }
723 
724  return $a_array;
725  }
726 
727 } //END class.ilObjExercise
728 ?>