00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00033 class ilExerciseMembers
00034 {
00035 var $ilias;
00036
00037 var $ref_id;
00038 var $obj_id;
00039 var $members;
00040 var $status;
00041 var $status_feedback;
00042 var $status_sent;
00043 var $status_returned;
00044 var $notice;
00045
00046 function ilExerciseMembers($a_obj_id,$a_ref_id)
00047 {
00048 global $ilias;
00049
00050 $this->ilias =& $ilias;
00051 $this->obj_id = $a_obj_id;
00052 $this->ref_id = $a_ref_id;
00053 }
00054
00055
00056 function getRefId()
00057 {
00058 return $this->ref_id;
00059 }
00060 function getObjId()
00061 {
00062 return $this->obj_id;
00063 }
00064 function setObjId($a_obj_id)
00065 {
00066 $this->obj_id = $a_obj_id;
00067 }
00068 function getMembers()
00069 {
00070 return $this->members ? $this->members : array();
00071 }
00072 function setMembers($a_members)
00073 {
00074 $this->members = $a_members;
00075 }
00076 function assignMember($a_usr_id)
00077 {
00078 $tmp_user = ilObjectFactory::getInstanceByObjId($a_usr_id);
00079 $tmp_user->addDesktopItem($this->getRefId(),"exc");
00080
00081
00082 $query = "REPLACE INTO exc_members ".
00083 "SET obj_id = '".$this->getObjId()."', ".
00084 "usr_id = '".$a_usr_id."', ".
00085 "status = 'notgraded', sent = '0', feedback='0'";
00086
00087 $res = $this->ilias->db->query($query);
00088 $this->read();
00089
00090 return true;
00091 }
00092 function isAssigned($a_id)
00093 {
00094 return in_array($a_id,$this->getMembers());
00095 }
00096
00097 function assignMembers($a_members)
00098 {
00099 $assigned = 0;
00100 if(is_array($a_members))
00101 {
00102 foreach($a_members as $member)
00103 {
00104 if(!$this->isAssigned($member))
00105 {
00106 $this->assignMember($member);
00107 }
00108 else
00109 {
00110 ++$assigned;
00111 }
00112 }
00113 }
00114 if($assigned == count($a_members))
00115 {
00116 return false;
00117 }
00118 else
00119 {
00120 return true;
00121 }
00122 }
00123 function deassignMember($a_usr_id)
00124 {
00125 $tmp_user = ilObjectFactory::getInstanceByObjId($a_usr_id);
00126 $tmp_user->dropDesktopItem($this->getRefId(),"exc");
00127
00128 $query = "DELETE FROM exc_members ".
00129 "WHERE obj_id = '".$this->getObjId()." '".
00130 "AND usr_id = '".$a_usr_id."'";
00131
00132 $this->ilias->db->query($query);
00133 $this->read();
00134
00135 $delivered_files =& $this->getDeliveredFiles($a_usr_id);
00136 $files_to_delete = array();
00137 $userfile = "";
00138 foreach ($delivered_files as $key => $value)
00139 {
00140 array_push($files_to_delete, $value["returned_id"]);
00141 $userfile = $value["filename"];
00142 }
00143 $this->deleteDeliveredFiles($files_to_delete, $a_usr_id);
00144
00145 if ($userfile)
00146 {
00147 $pathinfo = pathinfo($userfile);
00148 $dir = $pathinfo["dirname"];
00149 }
00150 if (is_dir($dir))
00151 {
00152 rmdir($dir);
00153 }
00154 return false;
00155 }
00156
00157 function deassignMembers($a_members)
00158 {
00159 if(is_array($a_members))
00160 {
00161 foreach($a_members as $member)
00162 {
00163 $this->deassignMember($member);
00164 }
00165 }
00166 else
00167 {
00168 return false;
00169 }
00170 }
00171 function setStatus($a_status)
00172 {
00173 if(is_array($a_status))
00174 {
00175 $this->status = $a_status;
00176 return true;
00177 }
00178 }
00179 function getStatus()
00180 {
00181 return $this->status ? $this->status : array();
00182 }
00183 function getStatusByMember($a_member_id)
00184 {
00185 if(isset($this->status[$a_member_id]))
00186 {
00187 return $this->status[$a_member_id];
00188 }
00189 return false;
00190 }
00191
00198 function setStatusForMember($a_member_id,$a_status)
00199 {
00200 $query = "UPDATE exc_members ".
00201 "SET status = '".$a_status."', ".
00202 "status_time= '".date("Y-m-d H:i:s")."'".
00203 " WHERE obj_id = '".$this->getObjId()."' ".
00204 "AND usr_id = '".$a_member_id."'".
00205 " AND status <> '".$a_status."'";
00206
00207 $this->ilias->db->query($query);
00208 $this->read();
00209
00210 return true;
00211 }
00212
00218 function updateStatusTimeForMember($a_member_id)
00219 {
00220 $query = "UPDATE exc_members ".
00221 "SET status_time= '".date("Y-m-d H:i:s")."'".
00222 " WHERE obj_id = '".$this->getObjId()."' ".
00223 "AND usr_id = '".$a_member_id."'";
00224
00225 $this->ilias->db->query($query);
00226 $this->read();
00227
00228 return true;
00229 }
00230
00231
00232 function setStatusSent($a_status)
00233 {
00234 if(is_array($a_status))
00235 {
00236 $this->status_sent = $a_status;
00237 return true;
00238 }
00239 }
00240 function getStatusSent()
00241 {
00242 return $this->status_sent ? $this->status_sent : array(0 => 0);
00243 }
00244 function getStatusSentByMember($a_member_id)
00245 {
00246 if(isset($this->status_sent[$a_member_id]))
00247 {
00248 return $this->status_sent[$a_member_id];
00249 }
00250 return false;
00251 }
00252 function setStatusSentForMember($a_member_id,$a_status)
00253 {
00254 $query = "UPDATE exc_members ".
00255 "SET sent = '".($a_status ? 1 : 0)."', ".
00256 "sent_time=".($a_status ? ("'".date("Y-m-d H:i:s")."'") : ("'0000-00-00 00:00:00'")).
00257 " WHERE obj_id = '".$this->getObjId()."' ".
00258 "AND usr_id = '".$a_member_id."'";
00259
00260 $this->ilias->db->query($query);
00261 $this->read();
00262
00263 return true;
00264 }
00265
00266 function getStatusReturned()
00267 {
00268 return $this->status_returned ? $this->status_returned : array(0 => 0);
00269 }
00270 function setStatusReturned($a_status)
00271 {
00272 if(is_array($a_status))
00273 {
00274 $this->status_returned = $a_status;
00275 return true;
00276 }
00277 return false;
00278 }
00279
00280 function getStatusReturnedByMember($a_member_id)
00281 {
00282 if(isset($this->status_returned[$a_member_id]))
00283 {
00284 return $this->status_returned[$a_member_id];
00285 }
00286 return false;
00287 }
00288 function setStatusReturnedForMember($a_member_id,$a_status)
00289 {
00290 $query = "UPDATE exc_members ".
00291 "SET returned = '".($a_status ? 1 : 0)."' ".
00292 "WHERE obj_id = '".$this->getObjId()."' ".
00293 "AND usr_id = '".$a_member_id."'";
00294
00295 $this->ilias->db->query($query);
00296 $this->read();
00297
00298 return true;
00299 }
00300
00301
00302 function setStatusFeedback($a_status)
00303 {
00304 if(is_array($a_status))
00305 {
00306 $this->status_feedback = $a_status;
00307 return true;
00308 }
00309 }
00310 function getStatusFeedback()
00311 {
00312 return $this->status_feedback ? $this->status_feedback : array(0 => 0);
00313 }
00314 function getStatusFeedbackByMember($a_member_id)
00315 {
00316 if(isset($this->status_feedback[$a_member_id]))
00317 {
00318 return $this->status_feedback[$a_member_id];
00319 }
00320 return false;
00321 }
00322 function setStatusFeedbackForMember($a_member_id,$a_status)
00323 {
00324
00325 $query = "UPDATE exc_members ".
00326 "SET feedback = '".($a_status ? 1 : 0)."', ".
00327 "feedback_time=".($a_status ? ("'".date("Y-m-d H:i:s")."'") : ("'0000-00-00 00:00:00'")).
00328 " WHERE obj_id = '".$this->getObjId()."' ".
00329 "AND usr_id = '".$a_member_id."'";
00330
00331 $this->ilias->db->query($query);
00332 $this->read();
00333
00334 return true;
00335 }
00336
00337 function getNotice()
00338 {
00339 return $this->notice ? $this->notice : array(0 => 0);
00340 }
00341
00342 function setNotice($a_notice)
00343 {
00344 if(is_array($a_notice))
00345 {
00346 $this->notice = $a_notice;
00347 return true;
00348 }
00349 return false;
00350 }
00351
00352 function getNoticeByMember($a_member_id)
00353 {
00354 if(isset($this->notice[$a_member_id]))
00355 {
00356 return $this->notice[$a_member_id];
00357 }
00358 else
00359 {
00360 return "";
00361 }
00362 }
00363
00364 function hasReturned($a_member_id)
00365 {
00366 $query = sprintf("SELECT returned_id FROM exc_returned WHERE obj_id = %s AND user_id = %s",
00367 $this->ilias->db->quote($this->getObjId() . ""),
00368 $this->ilias->db->quote($a_member_id . "")
00369 );
00370 $result = $this->ilias->db->query($query);
00371 return $result->numRows();
00372 }
00373
00374 function getAllDeliveredFiles()
00375 {
00376 $query = "SELECT * FROM exc_returned WHERE obj_id = '".$this->getObjId()."'";
00377
00378 $res = $this->ilias->db->query($query);
00379 while($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
00380 {
00381 $delivered[] = $row;
00382 }
00383 return $delivered ? $delivered : array();
00384 }
00385
00393 function &getDeliveredFiles($a_member_id)
00394 {
00395 $query = sprintf("SELECT *, TIMESTAMP + 0 AS TIMESTAMP14 FROM exc_returned WHERE obj_id = %s AND user_id = %s ORDER BY TIMESTAMP14",
00396 $this->ilias->db->quote($this->getObjId() . ""),
00397 $this->ilias->db->quote($a_member_id . "")
00398 );
00399 $result = $this->ilias->db->query($query);
00400 $delivered_files = array();
00401 if ($result->numRows())
00402 {
00403 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00404 {
00405 array_push($delivered_files, $row);
00406 }
00407 }
00408 return $delivered_files;
00409 }
00410
00417 function deleteDeliveredFiles($file_id_array, $a_member_id)
00418 {
00419 if (count($file_id_array))
00420 {
00421 $query = sprintf("SELECT * FROM exc_returned WHERE user_id = %s AND returned_id IN (" . join($file_id_array, ",") . ")",
00422 $this->ilias->db->quote($a_member_id . "")
00423 );
00424 $result = $this->ilias->db->query($query);
00425 if ($result->numRows())
00426 {
00427 $result_array = array();
00428 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00429 {
00430 array_push($result_array, $row);
00431 }
00432
00433 $query = sprintf("DELETE FROM exc_returned WHERE user_id = %s AND returned_id IN (" . join($file_id_array, ",") . ")",
00434 $this->ilias->db->quote($a_member_id . "")
00435 );
00436 $result = $this->ilias->db->query($query);
00437
00438 foreach ($result_array as $key => $value)
00439 {
00440 unlink($value["filename"]);
00441 }
00442 }
00443 }
00444 }
00445
00451 function deliverReturnedFiles($a_member_id, $a_only_new = false)
00452 {
00453 global $ilUser, $ilDB;
00454
00455
00456 $and_str = "";
00457 if ($a_only_new)
00458 {
00459 $q = "SELECT download_time FROM exc_usr_tutor WHERE ".
00460 " obj_id = ".$ilDB->quote($this->getObjId())." AND ".
00461 " usr_id = ".$ilDB->quote($a_member_id)." AND ".
00462 " tutor_id = ".$ilDB->quote($ilUser->getId());
00463 $lu_set = $ilDB->query($q);
00464 if ($lu_rec = $lu_set->fetchRow(DB_FETCHMODE_ASSOC))
00465 {
00466 if ($lu_rec["download_time"] > 0)
00467 {
00468 $and_str = " AND timestamp > ".$ilDB->quote($lu_rec["download_time"]);
00469 }
00470 }
00471 }
00472
00473 $this->updateTutorDownloadTime($a_member_id);
00474
00475 $query = sprintf("SELECT * FROM exc_returned WHERE obj_id = %s AND user_id = %s".
00476 $and_str,
00477 $this->ilias->db->quote($this->getObjId() . ""),
00478 $this->ilias->db->quote($a_member_id . "")
00479 );
00480 $result = $this->ilias->db->query($query);
00481 $count = $result->numRows();
00482 if ($count == 1)
00483 {
00484 $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00485 $this->downloadSingleFile($row["filename"], $row["filetitle"]);
00486 }
00487 else
00488 {
00489 $array_files = array();
00490 $filename = "";
00491 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00492 {
00493 $filename = $row["filename"];
00494 $pathinfo = pathinfo($filename);
00495 $dir = $pathinfo["dirname"];
00496 $file = $pathinfo["basename"];
00497 array_push($array_files, $file);
00498 }
00499 $pathinfo = pathinfo($filename);
00500 $dir = $pathinfo["dirname"];
00501 $this->downloadMultipleFiles($array_files, $dir);
00502 }
00503 }
00504
00511 function updateTutorDownloadTime($a_member_id)
00512 {
00513 global $ilUser, $ilDB;
00514
00515
00516 $q = "REPLACE INTO exc_usr_tutor (obj_id, usr_id, tutor_id, download_time) VALUES ".
00517 "(".$ilDB->quote($this->getObjId()).",".$ilDB->quote($a_member_id).
00518 ",".$ilDB->quote($ilUser->getId()).",now())";
00519 $ilDB->query($q);
00520 }
00521
00522 function downloadSelectedFiles($array_file_id)
00523 {
00524 if (count($array_file_id))
00525 {
00526 $query = "SELECT * FROM exc_returned WHERE returned_id IN (" . join($array_file_id, ",") . ")";
00527 $result = $this->ilias->db->query($query);
00528 if ($result->numRows())
00529 {
00530 $array_found = array();
00531 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00532 {
00533 array_push($array_found, $row);
00534 }
00535 if (count($array_found) == 1)
00536 {
00537 $this->downloadSingleFile($array_found[0]["filename"], $array_found[0]["filetitle"]);
00538 }
00539 else
00540 {
00541 $filenames = array();
00542 $dir = "";
00543 $file = "";
00544 foreach ($array_found as $key => $value)
00545 {
00546 $pathinfo = pathinfo($value["filename"]);
00547 $dir = $pathinfo["dirname"];
00548 $file = $pathinfo["basename"];
00549 array_push($filenames, $file);
00550 }
00551 $this->downloadMultipleFiles($filenames, $dir);
00552 }
00553 }
00554 }
00555 }
00556
00557 function downloadSingleFile($filename, $filetitle)
00558 {
00559 require_once "./classes/class.ilUtil.php";
00560 ilUtil::deliverFile($filename, $filetitle);
00561 }
00562
00563 function downloadMultipleFiles($array_filenames, $pathname)
00564 {
00565 global $lng;
00566 require_once "./classes/class.ilUtil.php";
00567 $cdir = getcwd();
00568 chdir($pathname);
00569 $zip = PATH_TO_ZIP;
00570 $tmpfile = ilUtil::ilTempnam();
00571 $tmpzipfile = $tmpfile . ".zip";
00572 foreach ($array_filenames as $key => $filename)
00573 {
00574 $array_filenames[$key] = ilUtil::escapeShellArg($array_filenames[$key]);
00575 }
00576 $zipcmd = $zip." ".ilUtil::escapeShellArg($tmpzipfile)." ".join($array_filenames, " ");
00577 exec($zipcmd);
00578 ilUtil::deliverFile($tmpzipfile, strtolower($lng->txt("excs")) . ".zip");
00579 chdir($cdir);
00580 unlink($tmpfile);
00581 unlink($tmpzipfile);
00582 }
00583
00584 function setNoticeForMember($a_member_id,$a_notice)
00585 {
00586
00587 $query = "UPDATE exc_members ".
00588 "SET notice = '".addslashes($a_notice)."' ".
00589 "WHERE obj_id = '".$this->getObjId()."' ".
00590 "AND usr_id = '".$a_member_id."'";
00591
00592 $this->ilias->db->query($query);
00593 $this->read();
00594
00595 return true;
00596 }
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645 function read()
00646 {
00647 $tmp_arr_members = array();
00648 $tmp_arr_status = array();
00649 $tmp_arr_sent = array();
00650 $tmp_arr_notice = array();
00651 $tmp_arr_returned = array();
00652 $tmp_arr_feedback = array();
00653
00654 $query = "SELECT * FROM exc_members ".
00655 "WHERE obj_id = '".$this->getObjId()."' ";
00656
00657 $res = $this->ilias->db->query($query);
00658 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00659 {
00660 $tmp_arr_members[] = $row->usr_id;
00661 $tmp_arr_notice[$row->usr_id] = $row->notice;
00662 $tmp_arr_returned[$row->usr_id] = $row->returned;
00663 $tmp_arr_status[$row->usr_id] = $row->status;
00664 $tmp_arr_sent[$row->usr_id] = $row->sent;
00665 $tmp_arr_feedback[$row->usr_id] = $row->feedback;
00666 }
00667 $this->setMembers($tmp_arr_members);
00668 $this->setNotice($tmp_arr_notice);
00669 $this->setStatus($tmp_arr_status);
00670 $this->setStatusSent($tmp_arr_sent);
00671 $this->setStatusReturned($tmp_arr_returned);
00672 $this->setStatusFeedback($tmp_arr_feedback);
00673
00674 return true;
00675 }
00676 function ilClone($a_new_id)
00677 {
00678 $data = array();
00679
00680 $query = "SELECT * FROM exc_members ".
00681 "WHERE obj_id = '".$this->getObjId()."'";
00682
00683 $res = $this->ilias->db->query($query);
00684 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00685 {
00686 $data[] = array("usr_id" => $row->usr_id,
00687 "notice" => $row->notice,
00688 "returned" => $row->returned,
00689 "status" => $row->status,
00690 "sent" => $row->sent,
00691 "feedback" => $row->feedback
00692 );
00693 }
00694 foreach($data as $row)
00695 {
00696 $query = "INSERT INTO exc_members ".
00697 "SET obj_id = '".$a_new_id."', ".
00698 "usr_id = '".$row["usr_id"]."', ".
00699 "notice = '".addslashes($row["notice"])."', ".
00700 "returned = '".$row["returned"]."', ".
00701 "status = '".$row["status"]."', ".
00702 "feedback = '".$row["feedback"]."', ".
00703 "sent = '".$row["sent"]."'";
00704
00705 $res = $this->ilias->db->query($query);
00706 }
00707 return true;
00708 }
00709
00710 function delete()
00711 {
00712 $query = "DELETE FROM exc_members WHERE obj_id = '".$this->getObjId()."'";
00713 $this->ilias->db->query($query);
00714
00715 return true;
00716 }
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752 function _getMembers($a_obj_id)
00753 {
00754 global $ilDB;
00755
00756 $query = "SELECT DISTINCT(usr_id) as ud FROM exc_members ".
00757 "WHERE obj_id = '".$a_obj_id."'";
00758
00759 $res = $ilDB->query($query);
00760 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00761 {
00762 $usr_ids[] = $row->ud;
00763 }
00764
00765 return $usr_ids ? $usr_ids : array();
00766 }
00767
00768 function _getReturned($a_obj_id)
00769 {
00770 global $ilDB;
00771
00772 $query = "SELECT DISTINCT(usr_id) as ud FROM exc_members ".
00773 "WHERE obj_id = '".$a_obj_id."' ".
00774 "AND returned = 1";
00775
00776 $res = $ilDB->query($query);
00777 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00778 {
00779 $usr_ids[] = $row->ud;
00780 }
00781
00782 return $usr_ids ? $usr_ids : array();
00783 }
00784
00785
00786
00787
00788
00789 function _getPassedUsers($a_obj_id)
00790 {
00791 global $ilDB;
00792
00793 $query = "SELECT DISTINCT(usr_id) FROM exc_members ".
00794 "WHERE obj_id = '".$a_obj_id."' ".
00795 "AND status = 'passed'";
00796 $res = $ilDB->query($query);
00797 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00798 {
00799 $usr_ids[] = $row->usr_id;
00800 }
00801 return $usr_ids ? $usr_ids : array();
00802 }
00803
00804 function _getFailedUsers($a_obj_id)
00805 {
00806 global $ilDB;
00807
00808 $query = "SELECT DISTINCT(usr_id) FROM exc_members ".
00809 "WHERE obj_id = '".$a_obj_id."' ".
00810 "AND status = 'failed'";
00811 $res = $ilDB->query($query);
00812 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00813 {
00814 $usr_ids[] = $row->usr_id;
00815 }
00816 return $usr_ids ? $usr_ids : array();
00817 }
00818
00825 function _lookupStatus($a_obj_id, $a_user_id)
00826 {
00827 global $ilDB;
00828
00829 $query = "SELECT status FROM exc_members ".
00830 "WHERE obj_id = ".$ilDB->quote($a_obj_id).
00831 "AND usr_id = ".$ilDB->quote($a_user_id);
00832
00833 $res = $ilDB->query($query);
00834 if($row = $res->fetchRow(DB_FETCHMODE_ASSOC))
00835 {
00836 return $row["status"];
00837 }
00838
00839 return false;
00840 }
00841
00842 }
00843 ?>