• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

classes/class.ilExerciseMembers.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2001 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021         +-----------------------------------------------------------------------------+
00022 */
00023 
00024 
00034 class ilExerciseMembers
00035 {
00036         var $ilias;
00037 
00038         var $ref_id;
00039         var $obj_id;
00040         var $members;
00041         var $status_solved;
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         // GET SET METHODS
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                         "solved = '0', sent = '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                 // delete all delivered files of the member
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                 // delete the user directory if existing
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 setStatusSolved($a_status)
00172         {
00173                 if(is_array($a_status))
00174                 {
00175                         $this->status_solved = $a_status;
00176                         return true;
00177                 }
00178         }
00179         function getStatusSolved()
00180         {
00181                 return $this->status_solved ? $this->status_solved : array(0 => 0);
00182         }
00183         function getStatusSolvedByMember($a_member_id)
00184         {
00185                 if(isset($this->status_solved[$a_member_id]))
00186                 {
00187                         return $this->status_solved[$a_member_id];
00188                 }
00189                 return false;
00190         }
00191         function setStatusSolvedForMember($a_member_id,$a_status)
00192         {
00193                 $query = "UPDATE exc_members ".
00194                         "SET solved = '".($a_status ? 1 : 0)."' ".
00195                         "WHERE obj_id = '".$this->getObjId()."' ".
00196                         "AND usr_id = '".$a_member_id."'";
00197 
00198                 $this->ilias->db->query($query);
00199                 $this->read();
00200 
00201                 return true;
00202         }
00203 
00204         function setStatusSent($a_status)
00205         {
00206                 if(is_array($a_status))
00207                 {
00208                         $this->status_sent = $a_status;
00209                         return true;
00210                 }
00211         }
00212         function getStatusSent()
00213         {
00214                 return $this->status_sent ? $this->status_sent : array(0 => 0);
00215         }
00216         function getStatusSentByMember($a_member_id)
00217         {
00218                 if(isset($this->status_sent[$a_member_id]))
00219                 {
00220                         return $this->status_sent[$a_member_id];
00221                 }
00222                 return false;
00223         }
00224         function setStatusSentForMember($a_member_id,$a_status)
00225         {
00226                 $query = "UPDATE exc_members ".
00227                         "SET sent = '".($a_status ? 1 : 0)."' ".
00228                         "WHERE obj_id = '".$this->getObjId()."' ".
00229                         "AND usr_id = '".$a_member_id."'";
00230 
00231                 $this->ilias->db->query($query);
00232                 $this->read();
00233 
00234                 return true;
00235         }
00236 
00237         function getStatusReturned()
00238         {
00239                 return $this->status_returned ? $this->status_returned : array(0 => 0);
00240         }
00241         function setStatusReturned($a_status)
00242         {
00243                 if(is_array($a_status))
00244                 {
00245                         $this->status_returned = $a_status;
00246                         return true;
00247                 }
00248                 return false;
00249         }
00250 
00251         function getStatusReturnedByMember($a_member_id)
00252         {
00253                 if(isset($this->status_returned[$a_member_id]))
00254                 {
00255                         return $this->status_returned[$a_member_id];
00256                 }
00257                 return false;
00258         }
00259         function setStatusReturnedForMember($a_member_id,$a_status)
00260         {
00261                 $query = "UPDATE exc_members ".
00262                         "SET returned = '".($a_status ? 1 : 0)."' ".
00263                         "WHERE obj_id = '".$this->getObjId()."' ".
00264                         "AND usr_id = '".$a_member_id."'";
00265 
00266                 $this->ilias->db->query($query);
00267                 $this->read();
00268 
00269                 return true;
00270         }
00271 
00272         function getNotice()
00273         {
00274                 return $this->notice ? $this->notice : array(0 => 0);
00275         }
00276 
00277         function setNotice($a_notice)
00278         {
00279                 if(is_array($a_notice))
00280                 {
00281                         $this->notice = $a_notice;
00282                         return true;
00283                 }
00284                 return false;
00285         }
00286 
00287         function getNoticeByMember($a_member_id)
00288         {
00289                 if(isset($this->notice[$a_member_id]))
00290                 {
00291                         return $this->notice[$a_member_id];
00292                 }
00293                 else
00294                 {
00295                         return "";
00296                 }
00297         }
00298 
00299         function hasReturned($a_member_id)
00300         {
00301                 $query = sprintf("SELECT returned_id FROM exc_returned WHERE obj_id = %s AND user_id = %s",
00302                         $this->ilias->db->quote($this->getObjId() . ""),
00303                         $this->ilias->db->quote($a_member_id . "")
00304                 );
00305                 $result = $this->ilias->db->query($query);
00306                 return $result->numRows();
00307         }
00308 
00316         function &getDeliveredFiles($a_member_id)
00317         {
00318                 $query = sprintf("SELECT *, TIMESTAMP + 0 AS TIMESTAMP14 FROM exc_returned WHERE obj_id = %s AND user_id = %s ORDER BY TIMESTAMP14",
00319                         $this->ilias->db->quote($this->getObjId() . ""),
00320                         $this->ilias->db->quote($a_member_id . "")
00321                 );
00322                 $result = $this->ilias->db->query($query);
00323                 $delivered_files = array();
00324                 if ($result->numRows())
00325                 {
00326                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00327                         {
00328                                 array_push($delivered_files, $row);
00329                         }
00330                 }
00331                 return $delivered_files;
00332         }
00333         
00340         function deleteDeliveredFiles($file_id_array, $a_member_id)
00341         {
00342                 if (count($file_id_array))
00343                 {
00344                         $query = sprintf("SELECT * FROM exc_returned WHERE user_id = %s AND returned_id IN (" . join($file_id_array, ",") . ")",
00345                                 $this->ilias->db->quote($a_member_id . "")
00346                         );
00347                         $result = $this->ilias->db->query($query);
00348                         if ($result->numRows())
00349                         {
00350                                 $result_array = array();
00351                                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00352                                 {
00353                                         array_push($result_array, $row);
00354                                 }
00355                                 // delete the entries in the database
00356                                 $query = sprintf("DELETE FROM exc_returned WHERE user_id = %s AND returned_id IN (" . join($file_id_array, ",") . ")",
00357                                         $this->ilias->db->quote($a_member_id . "")
00358                                 );
00359                                 $result = $this->ilias->db->query($query);
00360                                 // delete the files
00361                                 foreach ($result_array as $key => $value)
00362                                 {
00363                                         unlink($value["filename"]);
00364                                 }
00365                         }
00366                 }
00367         }
00368 
00374         function deliverReturnedFiles($a_member_id)
00375         {
00376                 $query = sprintf("SELECT * FROM exc_returned WHERE obj_id = %s AND user_id = %s",
00377                         $this->ilias->db->quote($this->getObjId() . ""),
00378                         $this->ilias->db->quote($a_member_id . "")
00379                 );
00380                 $result = $this->ilias->db->query($query);
00381                 $count = $result->numRows();
00382                 if ($count == 1)
00383                 {
00384                         $row = $result->fetchRow(DB_FETCHMODE_ASSOC);
00385                         $this->downloadSingleFile($row["filename"], $row["filetitle"]);
00386                 }
00387                 else
00388                 {
00389                         $array_files = array();
00390                         $filename = "";
00391                         while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00392                         {
00393                                 $filename = $row["filename"];
00394                                 $pathinfo = pathinfo($filename);
00395                                 $dir = $pathinfo["dirname"];
00396                                 $file = $pathinfo["basename"];
00397                                 array_push($array_files, $file);
00398                         }
00399                         $pathinfo = pathinfo($filename);
00400                         $dir = $pathinfo["dirname"];
00401                         $this->downloadMultipleFiles($array_files, $dir);
00402                 }
00403         }
00404         
00405         function downloadSelectedFiles($array_file_id)
00406         {
00407                 if (count($array_file_id))
00408                 {
00409                         $query = "SELECT * FROM exc_returned WHERE returned_id IN (" . join($array_file_id, ",") . ")";
00410                         $result = $this->ilias->db->query($query);
00411                         if ($result->numRows())
00412                         {
00413                                 $array_found = array();
00414                                 while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
00415                                 {
00416                                         array_push($array_found, $row);
00417                                 }
00418                                 if (count($array_found) == 1)
00419                                 {
00420                                         $this->downloadSingleFile($array_found[0]["filename"], $array_found[0]["filetitle"]);
00421                                 }
00422                                 else
00423                                 {
00424                                         $filenames = array();
00425                                         $dir = "";
00426                                         $file = "";
00427                                         foreach ($array_found as $key => $value)
00428                                         {
00429                                                 $pathinfo = pathinfo($value["filename"]);
00430                                                 $dir = $pathinfo["dirname"];
00431                                                 $file = $pathinfo["basename"];
00432                                                 array_push($filenames, $file); 
00433                                         }
00434                                         $this->downloadMultipleFiles($filenames, $dir);
00435                                 }
00436                         }
00437                 }
00438         }
00439         
00440         function downloadSingleFile($filename, $filetitle)
00441         {
00442                 require_once "./classes/class.ilUtil.php";
00443                 ilUtil::deliverFile($filename, $filetitle);
00444         }
00445         
00446         function downloadMultipleFiles($array_filenames, $pathname)
00447         {
00448                 global $lng;
00449                 require_once "./classes/class.ilUtil.php";
00450                 $cdir = getcwd();
00451                 chdir($pathname);
00452                 $zip = PATH_TO_ZIP;
00453                 $tmpfile = ilUtil::ilTempnam();
00454                 $tmpzipfile = $tmpfile . ".zip";
00455                 foreach ($array_filenames as $key => $filename)
00456                 {
00457                         $array_filenames[$key] = ilUtil::escapeShellArg($array_filenames[$key]);
00458                 }
00459                 $zipcmd = $zip." ".ilUtil::escapeShellArg($tmpzipfile)." ".join($array_filenames, " ");
00460                 exec($zipcmd);
00461                 ilUtil::deliverFile($tmpzipfile, strtolower($lng->txt("excs")) . ".zip");
00462                 chdir($cdir);
00463                 unlink($tmpfile);
00464                 unlink($tmpzipfile);
00465         }
00466         
00467         function setNoticeForMember($a_member_id,$a_notice)
00468         {
00469 
00470                 $query = "UPDATE exc_members ".
00471                         "SET notice = '".addslashes($a_notice)."' ".
00472                         "WHERE obj_id = '".$this->getObjId()."' ".
00473                         "AND usr_id = '".$a_member_id."'";
00474 
00475                 $this->ilias->db->query($query);
00476                 $this->read();
00477 
00478                 return true;
00479         }
00480 /*
00481         function update()
00482         {
00483                 $save_members = $this->getMembers();
00484                 $save_notice = $this->getNotice();
00485                 $saved_st_solved = $this->getStatusSolved();
00486                 $saved_st_sent = $this->getStatusSent();
00487                 $saved_st_return = $this->getStatusReturned();
00488 
00489                 $this->read();
00490 
00491                 // UPDATE MEMBERS
00492                 foreach(array_diff($this->getMembers(),$save_members) as $member)
00493                 {
00494                         $query  = "DELETE FROM exc_members ".
00495                                 "WHERE obj_id = '".$this->getObjId()."' ".
00496                                 "AND usr_id = '".$member."'";
00497                         $this->ilias->db->query($query);
00498                 }
00499                 foreach(array_diff($save_members,$this->getMembers()) as $member)
00500                 {
00501                         $query  = "INSERT INTO exc_members ".
00502                                 "SET obj_id = '".$this->getObjId()."', ".
00503                                 "usr_id = '".$member."', ".
00504                                 "sent = '0', ".
00505                                 "solved = '0'";
00506                         $this->ilias->db->query($query);
00507                 }
00508                 $this->setMembers($save_members);
00509                 $this->setNotice($save_notice);
00510                 $this->setStatusSent($saved_st_sent);
00511                 $this->setStatusSolved($saved_st_solved);
00512                 $this->setStatusReturned($saved_st_return);
00513 
00514                 
00515                 // UPDATE SOLVED AND SENT
00516                 foreach($this->getMembers() as $member)
00517                 {
00518                         $query = "UPDATE exc_members ".
00519                                 "SET solved = '".$this->getStatusSolvedByMember($member)."', ".
00520                                 "notice = '".addslashes($this->getNoticeByMember($member))."', ".
00521                                 "returned = '".$this->getStatusReturnedByMember($member)."', ".
00522                             "sent = '".$this->getStatusSentByMember($member)."'";
00523                         $this->ilias->db->query($query);
00524                 }
00525                 return true;
00526         }
00527 */
00528         function read()
00529         {
00530                 $tmp_arr_members = array();
00531                 $tmp_arr_solved = array();
00532                 $tmp_arr_sent = array();
00533                 $tmp_arr_notice = array();
00534                 $tmp_arr_returned = array();
00535 
00536                 $query = "SELECT * FROM exc_members ".
00537                         "WHERE obj_id = '".$this->getObjId()."' ";
00538 
00539                 $res = $this->ilias->db->query($query);
00540                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00541                 {
00542                         $tmp_arr_members[] = $row->usr_id;
00543                         $tmp_arr_notice[$row->usr_id] = $row->notice;
00544                         $tmp_arr_returned[$row->usr_id] = $row->returned;
00545                         $tmp_arr_solved[$row->usr_id] = $row->solved;
00546                         $tmp_arr_sent[$row->usr_id] = $row->sent;
00547                 }
00548                 $this->setMembers($tmp_arr_members);
00549                 $this->setNotice($tmp_arr_notice);
00550                 $this->setStatusSolved($tmp_arr_solved);
00551                 $this->setStatusSent($tmp_arr_sent);
00552                 $this->setStatusReturned($tmp_arr_returned);
00553                 
00554                 return true;
00555         }
00556         function ilClone($a_new_id)
00557         {
00558                 $data = array();
00559 
00560                 $query = "SELECT * FROM exc_members ".
00561                         "WHERE obj_id = '".$this->getObjId()."'";
00562 
00563                 $res = $this->ilias->db->query($query);
00564                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00565                 {
00566                         $data[] = array("usr_id" => $row->usr_id,
00567                                                         "notice" => $row->notice,
00568                                                         "returned" => $row->returned,
00569                                                         "solved" => $row->solved,
00570                                                         "sent"   => $row->sent);
00571                 }
00572                 foreach($data as $row)
00573                 {
00574                         $query = "INSERT INTO exc_members ".
00575                                 "SET obj_id = '".$a_new_id."', ".
00576                                 "usr_id = '".$row["usr_id"]."', ".
00577                                 "notice = '".addslashes($row["notice"])."', ".
00578                                 "returned = '".$row["returned"]."', ".
00579                                 "solved = '".$row["solved"]."', ".
00580                                 "sent = '".$row["sent"]."'";
00581 
00582                         $res = $this->ilias->db->query($query);
00583                 }
00584                 return true;
00585         }
00586 
00587         function delete()
00588         {
00589                 $query = "DELETE FROM exc_members WHERE obj_id = '".$this->getObjId()."'";
00590                 $this->ilias->db->query($query);
00591 
00592                 return true;
00593         }
00594 
00595         function _hasSolved($a_exc_id,$a_usr_id)
00596         {
00597                 global $ilDB;
00598 
00599                 // check if user is owner
00600                 $query = "SELECT owner FROM object_data ".
00601                         "WHERE obj_id = '".$a_exc_id."'";
00602 
00603                 $res = $ilDB->query($query);
00604                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00605                 {
00606                         if($row->owner == $a_usr_id)
00607                         {
00608                                 return true;
00609                         }
00610                 }
00611 
00612                 // check if user has solved exercise
00613                 $query = "SELECT solved FROM exc_members ".
00614                         "WHERE obj_id = '".$a_exc_id."' ".
00615                         "AND usr_id = '".$a_usr_id."'";
00616 
00617                 $res = $ilDB->query($query);
00618                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00619                 {
00620                         if($row->solved == 1)
00621                         {
00622                                 return true;
00623                         }
00624                 }
00625                 return false;
00626         }
00627 
00628 } //END class.ilObjExercise
00629 ?>

Generated on Fri Dec 13 2013 09:06:33 for ILIAS Release_3_4_x_branch .rev 46804 by  doxygen 1.7.1