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

course/classes/class.ilObjCourseGrouping.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2005 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 
00034 require_once "./classes/class.ilObject.php";
00035 
00036 class ilObjCourseGrouping
00037 {
00038         var $db;
00039 
00046         function ilObjCourseGrouping($a_id = 0)
00047         {
00048                 global $ilDB;
00049 
00050                 $this->setType('crsg');
00051                 $this->db =& $ilDB;
00052 
00053                 $this->setId($a_id);
00054 
00055                 if($a_id)
00056                 {
00057                         $this->read();
00058                 }
00059         }
00060         function setId($a_id)
00061         {
00062                 $this->id = $a_id;
00063         }
00064         function getId()
00065         {
00066                 return $this->id;
00067         }
00068         function setCourseRefId($a_ref_id)
00069         {
00070                 $this->ref_id = $a_ref_id;
00071         }
00072         function getCourseRefId()
00073         {
00074                 return $this->ref_id;
00075         }
00076         function setCourseObjId($a_obj_id)
00077         {
00078                 $this->obj_id = $a_obj_id;
00079         }
00080         function getCourseObjId()
00081         {
00082                 return $this->obj_id;
00083         }
00084         function setType($a_type)
00085         {
00086                 $this->type = $a_type;
00087         }
00088         function getType()
00089         {
00090                 return $this->type;
00091         }
00092 
00093         function setTitle($a_title)
00094         {
00095                 $this->title = $a_title;
00096         }
00097         function getTitle()
00098         {
00099                 return $this->title;
00100         }
00101         function setDescription($a_desc)
00102         {
00103                 $this->description = $a_desc;
00104         }
00105         function getDescription()
00106         {
00107                 return $this->description;
00108         }
00109         function setUniqueField($a_uni)
00110         {
00111                 $this->unique_field = $a_uni;
00112         }
00113         function getUniqueField()
00114         {
00115                 return $this->unique_field;
00116         }
00117 
00118         function getCountAssignedCourses()
00119         {
00120                 return count($this->getAssignedCourses());
00121         }
00122 
00123         function getAssignedCourses()
00124         {
00125                 include_once './classes/class.ilConditionHandler.php';
00126 
00127                 return ilConditionHandler::_getConditionsOfTrigger($this->getType(),$this->getId());
00128         }
00129 
00130 
00131         function delete()
00132         {
00133                 include_once './classes/class.ilConditionHandler.php';
00134 
00135                 if($this->getId() and $this->getType() === 'crsg')
00136                 {
00137                         $query = "DELETE FROM object_data WHERE obj_id = '".$this->getId()."'";
00138                         $this->db->query($query);
00139 
00140                         $query = "DELETE FROM crs_groupings ".
00141                                 "WHERE crs_grp_id = '".$this->getId()."'";
00142                         $this->db->query($query);
00143 
00144                         // Delete conditions
00145                         $condh =& new ilConditionHandler();
00146                         $condh->deleteByObjId($this->getId());
00147 
00148                         return true;
00149                 }
00150                 return false;
00151         }
00152 
00153         function create($a_course_ref_id,$a_course_id)
00154         {
00155                 global $ilUser;
00156 
00157                 // INSERT IN object_data
00158                 $query = "INSERT INTO object_data ".
00159                         "(type,title,description,owner,create_date,last_update,import_id) ".
00160                         "VALUES ".
00161                         "('".$this->type."',".$this->db->quote($this->getTitle()).",'".ilUtil::prepareDBString($this->getDescription())."',".
00162                         "'".$ilUser->getId()."',now(),now(),'')";
00163                         
00164                 $this->db->query($query);
00165 
00166                 // READ this id
00167                 $query = "SELECT LAST_INSERT_ID() as last";
00168                 $res = $this->db->query($query);
00169                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00170                 {
00171                         $this->setId($row->last);
00172                 }
00173 
00174 
00175                 // INSERT in crs_groupings
00176                 $query = "INSERT INTO crs_groupings ".
00177                         "SET crs_ref_id = '".$a_course_ref_id."', ".
00178                         "crs_id = '".$a_course_id."',".
00179                         "crs_grp_id = '".$this->getId()."', ".
00180                         "unique_field = '".$this->getUniqueField()."'";
00181 
00182                 $this->db->query($query);
00183 
00184                 return $this->getId();
00185         }
00186 
00187         function update()
00188         {
00189                 if($this->getId() and $this->getType() === 'crsg')
00190                 {
00191                         // UPDATe object_data
00192                         $query = "UPDATE object_data ".
00193                                 "SET title = '".ilUtil::prepareDBString($this->getTitle())."', ".
00194                                 "description = '".ilUtil::prepareDBString($this->getDescription())."' ".
00195                                 "WHERE obj_id = '".$this->getId()."' ".
00196                                 "AND type = '".$this->getType()."'";
00197 
00198                         $this->db->query($query);
00199 
00200                         // UPDATE crs_groupings
00201                         $query = "UPDATE crs_groupings ".
00202                                 "SET unique_field = '".$this->getUniqueField()."' ".
00203                                 "WHERE crs_grp_id = '".$this->getId()."'";
00204 
00205                         $this->db->query($query);
00206 
00207                         // UPDATE conditions
00208                         $query = "UPDATE conditions ".
00209                                 "SET value = '".$this->getUniqueField()."' ".
00210                                 "WHERE trigger_obj_id = '".$this->getId()."' ".
00211                                 "AND trigger_type = 'crsg'";
00212                         $this->db->query($query);
00213 
00214                         return true;
00215                 }
00216                 return false;
00217         }
00218 
00219         function isAssigned($a_course_id)
00220         {
00221                 foreach($this->getAssignedCourses() as $condition_data)
00222                 {
00223                         if($a_course_id == $condition_data['target_obj_id'])
00224                         {
00225                                 return true;
00226                         }
00227                 }
00228                 return false;
00229         }
00230 
00231         function read()
00232         {
00233                 $query = "SELECT * FROM object_data ".
00234                         "WHERE obj_id = '".$this->getId()."'";
00235 
00236                 $res = $this->db->query($query);
00237                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00238                 {
00239                         $this->setTitle($row->title);
00240                         $this->setDescription($row->description);
00241                 }
00242 
00243                 $query = "SELECT * FROM crs_groupings ".
00244                         "WHERE crs_grp_id = '".$this->getId()."'";
00245                 $res = $this->db->query($query);
00246 
00247                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00248                 {
00249                         $this->setUniqueField($row->unique_field);
00250                         $this->setCourseRefId($row->crs_ref_id);
00251                         $this->setCourseObjId($row->crs_id);
00252                 }
00253 
00254                 return true;
00255         }
00256 
00257         function _getAllGroupings($a_crs_ref_id,$a_check_write = true)
00258         {
00259                 global $ilObjDataCache,$ilUser;
00260 
00261                 if($a_check_write)
00262                 {
00263                         $courses = ilUtil::_getObjectsByOperations('crs','write',$ilUser->getId(),-1);
00264                 }
00265                 else
00266                 {
00267                         $courses = ilUtil::_getObjectsByOperations('crs','visible',$ilUser->getId(),-1);
00268                 }
00269 
00270                 $groupings = array();
00271 
00272                 foreach($courses as $crs_id)
00273                 {
00274                         if($a_crs_ref_id != $crs_id)
00275                         {
00276                                 $groupings = array_merge($groupings,ilObjCourseGrouping::_getGroupings($ilObjDataCache->lookupObjId($crs_id)));
00277                         }
00278                 }
00279                 return $groupings ? $groupings : array();
00280         }
00281 
00282         function assign($a_crs_ref_id,$a_course_id)
00283         {
00284                 // Add the parent course of grouping
00285                 $this->__addCondition($this->getCourseRefId(),$this->getCourseObjId());
00286                 $this->__addCondition($a_crs_ref_id,$a_course_id);
00287 
00288                 return true;
00289         }
00290 
00291         function deassign($a_crs_ref_id,$a_course_id)
00292         {
00293                 include_once './classes/class.ilConditionHandler.php';
00294 
00295 
00296                 $condh =& new ilConditionHandler();
00297 
00298                 // DELETE also original course if its the last
00299                 if($this->getCountAssignedCourses() == 2)
00300                 {
00301                         $condh->deleteByObjId($this->getId());
00302 
00303                         return true;
00304                 }
00305                 
00306                 foreach(ilConditionHandler::_getConditionsOfTrigger('crsg',$this->getId()) as $cond_data)
00307                 {
00308 
00309                         if($cond_data['target_ref_id'] == $a_crs_ref_id and
00310                            $cond_data['target_obj_id'] == $a_course_id)
00311                         {
00312                                 $condh->deleteCondition($cond_data['id']);
00313                         }
00314                 }
00315 
00316                 return true;
00317                         
00318         }
00319         // PRIVATE
00320         function __addCondition($a_target_ref_id,$a_target_obj_id)
00321         {
00322                 include_once './classes/class.ilConditionHandler.php';
00323 
00324                 $tmp_condh =& new ilConditionHandler();
00325                 $tmp_condh->enableAutomaticValidation(false);
00326 
00327                 $tmp_condh->setTargetRefId($a_target_ref_id);
00328                 $tmp_condh->setTargetObjId($a_target_obj_id);
00329                 $tmp_condh->setTargetType('crs');
00330                 $tmp_condh->setTriggerRefId(0);
00331                 $tmp_condh->setTriggerObjId($this->getId());
00332                 $tmp_condh->setTriggerType('crsg');
00333                 $tmp_condh->setOperator('not_member');
00334                 $tmp_condh->setValue($this->getUniqueField());
00335 
00336                 if(!$tmp_condh->checkExists())
00337                 {
00338                         $tmp_condh->storeCondition();
00339 
00340                         return true;
00341                 }
00342                 return false;
00343         }
00344         
00345         // STATIC
00346         function _deleteAll($a_course_id)
00347         {
00348                 global $ilDB;
00349 
00350                 // DELETE CONDITIONS
00351                 foreach($groupings = ilObjCourseGrouping::_getGroupings($a_course_id) as $grouping_id)
00352                 {
00353                         include_once './classes/class.ilConditionHandler.php';
00354 
00355                         $condh =& new ilConditionHandler();
00356                         $condh->deleteByObjId($grouping_id);
00357                 }
00358 
00359                 $query = "DELETE FROM crs_groupings ".
00360                         "WHERE crs_id = '".$a_course_id."'";
00361 
00362                 $ilDB->query($query);
00363 
00364                 return true;
00365         }
00366 
00367         function _getGroupings($a_course_id)
00368         {
00369                 global $ilDB;
00370 
00371                 $query = "SELECT * FROM crs_groupings ".
00372                         "WHERE crs_id = '".$a_course_id."'";
00373 
00374                 $res = $ilDB->query($query);
00375                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00376                 {
00377                         $groupings[] = $row->crs_grp_id;
00378                 }
00379                 return $groupings ? $groupings : array();
00380         }
00381 
00382         function _isInGrouping($a_crs_id)
00383         {
00384                 include_once './classes/class.ilConditionHandler.php';
00385 
00386                 foreach(ilConditionHandler::_getConditionsOfTarget($a_crs_id,'crs') as $cond)
00387                 {
00388                         if($cond['operator'] == 'not_member')
00389                         {
00390                                 return true;
00391                         }
00392                 }
00393                 return false;
00394         }
00395 
00396         function _checkCondition($trigger_obj_id,$operator,$value)
00397         {
00398                 // in the moment i alway return true, there are some problems with presenting the condition if it fails,
00399                 // only course register class check manually if this condition is fullfilled
00400                 return true;
00401         }
00402 
00403 
00410         function _getGroupingCourseIds($a_course_id)
00411         {
00412                 include_once './classes/class.ilConditionHandler.php';
00413 
00414                 // get all grouping ids the course is assigned to
00415                 foreach(ilConditionHandler::_getConditionsOfTarget($a_course_id,'crs') as $condition)
00416                 {
00417                         if($condition['trigger_type'] == 'crsg')
00418                         {
00419                                 foreach(ilConditionHandler::_getConditionsOfTrigger('crsg',$condition['trigger_obj_id']) as $target_condition)
00420                                 {
00421                                         $course_ids[] = array('id'                      => $target_condition['target_obj_id'],
00422                                                                                   'unique'              => $target_condition['value']);
00423                                 }
00424                         }
00425                 }
00426                 return $course_ids ? $course_ids : array();
00427         }       
00428 
00429 
00430 } // END class.ilObjCourseGrouping
00431 ?>

Generated on Fri Dec 13 2013 11:57:58 for ILIAS Release_3_6_x_branch .rev 46809 by  doxygen 1.7.1