Go to the documentation of this file.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
00035 class ilCourseItems
00036 {
00037 var $course_obj;
00038 var $ilErr;
00039 var $ilDB;
00040 var $tree;
00041 var $lng;
00042
00043 var $items;
00044 var $parent;
00045
00046 var $activation_unlimited;
00047 var $activation_start;
00048 var $activation_end;
00049
00050 function ilCourseItems(&$course_obj,$a_parent = 0)
00051 {
00052 global $ilErr,$ilDB,$lng,$tree;
00053
00054 $this->ilErr =& $ilErr;
00055 $this->ilDB =& $ilDB;
00056 $this->lng =& $lng;
00057 $this->tree =& $tree;
00058
00059 $this->course_obj =& $course_obj;
00060 $this->setParentId($a_parent);
00061
00062 $this->__read();
00063 }
00064
00065 function setParentId($a_parent = 0)
00066 {
00067 $this->parent = $a_parent ? $a_parent : $this->course_obj->getRefId();
00068
00069 $this->__read();
00070
00071 return true;
00072 }
00073 function getParentId()
00074 {
00075 return $this->parent;
00076 }
00077 function setActivationUnlimitedStatus($a_value)
00078 {
00079 $this->activation_unlimited = $a_value ? 1 : 0;
00080 }
00081 function getActivationUnlimitedStatus()
00082 {
00083 return (bool) $this->activation_unlimited;
00084 }
00085 function setActivationStart($a_start)
00086 {
00087 $this->activation_start = $a_start;
00088 }
00089 function getActivationStart()
00090 {
00091 return $this->activation_start;
00092 }
00093 function setActivationEnd($a_end)
00094 {
00095 $this->activation_end = $a_end;
00096 }
00097 function getActivationEnd()
00098 {
00099 return $this->activation_end;
00100 }
00101 function getAllItems()
00102 {
00103 return $this->items ? $this->items : array();
00104 }
00105 function getItems()
00106 {
00107 global $rbacsystem;
00108
00109 foreach($this->items as $item)
00110 {
00111 if($item["type"] != "rolf" and
00112 ($item["activation_unlimited"] or
00113 ($item["activation_start"] <= time() and $item["activation_end"] >= time())))
00114 {
00115 if($rbacsystem->checkAccess('visible',$item['ref_id']))
00116 {
00117 $filtered[] = $item;
00118 }
00119 }
00120 }
00121 return $filtered ? $filtered : array();
00122 }
00123 function getItem($a_item_id)
00124 {
00125 foreach($this->items as $item)
00126 {
00127 if($item["child"] == $a_item_id)
00128 {
00129 return $item;
00130 }
00131 }
00132 return array();
00133 }
00134
00135 function validateActivation()
00136 {
00137 $this->course_obj->setMessage('');
00138
00139 if(!$this->getActivationUnlimitedStatus())
00140 {
00141 if($this->getActivationStart() > $this->getActivationEnd())
00142 {
00143 $this->course_obj->appendMessage($this->lng->txt("crs_activation_start_invalid"));
00144 }
00145 }
00146
00147 if($this->course_obj->getMessage())
00148 {
00149 return false;
00150 }
00151 return true;
00152 }
00153
00154 function update($a_item_id)
00155 {
00156 $query = "UPDATE crs_items SET ".
00157 "activation_unlimited = '".(int) $this->getActivationUnlimitedStatus()."', ".
00158 "activation_start = '".(int) $this->getActivationStart()."', ".
00159 "activation_end = '".(int) $this->getActivationEnd()."' ".
00160 "WHERE parent_id = '".$this->getParentId()."' ".
00161 "AND obj_id = '".$a_item_id."'";
00162
00163 $res = $this->ilDB->query($query);
00164
00165 $this->__read();
00166
00167 return true;
00168 }
00169
00170
00171 function moveUp($item_id)
00172 {
00173 $this->__updateTop($item_id);
00174 $this->__read();
00175
00176 return true;
00177 }
00178 function moveDown($item_id)
00179 {
00180 $this->__updateBottom($item_id);
00181 $this->__read();
00182
00183 return true;
00184 }
00185
00186 function deleteAllEntries()
00187 {
00188 $all_items = $this->tree->getChilds($this->parent);
00189
00190 foreach($all_items as $item)
00191 {
00192 $query = "DELETE FROM crs_items ".
00193 "WHERE parent_id = '".$item["child"]."'";
00194
00195 $this->ilDB->query($query);
00196 }
00197 $query = "DELETE FROM crs_items ".
00198 "WHERE parent_id = '".$this->course_obj->getRefId()."'";
00199
00200 $this->ilDB->query($query);
00201
00202 return true;
00203 }
00204
00205
00206 function __read()
00207 {
00208 $this->items = array();
00209 $all_items = $this->tree->getChilds($this->parent);
00210
00211 foreach($all_items as $item)
00212 {
00213 if($item["type"] != 'rolf')
00214 {
00215 $this->items[] = $item;
00216 }
00217 }
00218
00219 for($i = 0;$i < count($this->items); ++$i)
00220 {
00221 if($this->items[$i]["type"] == 'rolf')
00222 {
00223 unset($this->items[$i]);
00224 continue;
00225 }
00226 $this->items[$i] = $this->__getItemData($this->items[$i]);
00227 }
00228 $this->__purgeDeleted();
00229 $this->__sort();
00230
00231 return true;
00232 }
00233
00234 function __purgeDeleted()
00235 {
00236 $all = array();
00237
00238 $query = "SELECT obj_id FROM crs_items ".
00239 "WHERE parent_id = '".$this->getParentId()."'";
00240
00241 $res = $this->ilDB->query($query);
00242 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00243 {
00244 if(!$this->tree->isInTree($row->obj_id))
00245 {
00246 $this->__delete($row->obj_id);
00247 }
00248 }
00249 }
00250
00251 function __delete($a_obj_id)
00252 {
00253
00254 $query = "SELECT position FROM crs_items ".
00255 "WHERE obj_id = '".$a_obj_id."' ".
00256 "AND parent_id = '".$this->getParentId()."'";
00257
00258 $res = $this->ilDB->query($query);
00259 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00260 {
00261 $position = $row->position;
00262 }
00263
00264
00265 $query = "UPDATE crs_items SET ".
00266 "position = CASE ".
00267 "WHEN position > '".$position."' ".
00268 "THEN position - 1 ".
00269 "ELSE position ".
00270 "END ".
00271 "WHERE parent_id = '".$this->getParentId()."'";
00272
00273 $res = $this->ilDB->query($query);
00274
00275
00276 $query = "DELETE FROM crs_items ".
00277 "WHERE parent_id = '".$this->getParentId()."' ".
00278 "AND obj_id = '".$a_obj_id."'";
00279
00280 $res = $this->ilDB->query($query);
00281
00282 return true;
00283 }
00284
00285 function __getItemData($a_item)
00286 {
00287 $query = "SELECT * FROM crs_items ".
00288 "WHERE parent_id = '".$this->getParentId()."' ".
00289 "AND obj_id = '".$a_item["child"]."'";
00290
00291 $res = $this->ilDB->query($query);
00292 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00293 {
00294 $a_item["activation_unlimited"] = $row->activation_unlimited;
00295 $a_item["activation_start"] = $row->activation_start;
00296 $a_item["activation_end"] = $row->activation_end;
00297 $a_item["position"] = $row->position;
00298 }
00299
00300 if(!isset($a_item["position"]))
00301 {
00302 $a_item = $this->createDefaultEntry($a_item);
00303 }
00304 return $a_item;
00305 }
00306
00307 function createDefaultEntry($a_item)
00308 {
00309 $a_item["activation_unlimited"] = 1;
00310 $a_item["activation_start"] = time();
00311 $a_item["activation_end"] = time();
00312 $a_item["position"] = $this->__getLastPosition() + 1;
00313
00314 $query = "INSERT INTO crs_items ".
00315 "VALUES('".$this->getParentId()."','".
00316 $a_item["child"]."','".
00317 $a_item["activation_unlimited"]."','".
00318 $a_item["activation_start"]."','".
00319 $a_item["activation_end"]."','".
00320 $a_item["position"]."')";
00321
00322 $res = $this->ilDB->query($query);
00323
00324 return $a_item;
00325 }
00326
00327
00328 function __getLastPosition()
00329 {
00330 $query = "SELECT MAX(position) as last_position FROM crs_items ".
00331 "WHERE parent_id = '".$this->getParentId()."'";
00332
00333 $res = $this->ilDB->query($query);
00334 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00335 {
00336 $max = $row->last_position;
00337 }
00338 return $max ? $max : 0;
00339 }
00340
00341 function __updateTop($item_id)
00342 {
00343 $query = "SELECT position,obj_id FROM crs_items ".
00344 "WHERE obj_id = '".$item_id."' ".
00345 "AND parent_id = '".$this->getParentId()."'";
00346
00347 $res = $this->ilDB->query($query);
00348 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00349 {
00350 $node_a["position"] = $row->position;
00351 $node_a["obj_id"] = $row->obj_id;
00352 }
00353
00354 $query = "SELECT position, obj_id FROM crs_items ".
00355 "WHERE position < '".$node_a["position"]."' ".
00356 "AND parent_id = '".$this->getParentId()."' ".
00357 "ORDER BY position DESC";
00358
00359 $res = $this->ilDB->query($query);
00360 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00361 {
00362 $node_b["position"] = $row->position;
00363 $node_b["obj_id"] = $row->obj_id;
00364 break;
00365 }
00366 $this->__switchNodes($node_a,$node_b);
00367
00368 }
00369
00370 function __updateBottom($item_id)
00371 {
00372 $query = "SELECT position,obj_id FROM crs_items ".
00373 "WHERE obj_id = '".$item_id."' ".
00374 "AND parent_id = '".$this->getParentId()."'";
00375
00376 $res = $this->ilDB->query($query);
00377 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00378 {
00379 $node_a["position"] = $row->position;
00380 $node_a["obj_id"] = $row->obj_id;
00381 break;
00382 }
00383 $query = "SELECT position ,obj_id FROM crs_items ".
00384 "WHERE position > '".$node_a["position"]."' ".
00385 "AND parent_id = '".$this->getParentId()."' ".
00386 "ORDER BY position ASC";
00387
00388 $res = $this->ilDB->query($query);
00389 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00390 {
00391 $node_b["position"] = $row->position;
00392 $node_b["obj_id"] = $row->obj_id;
00393 break;
00394 }
00395 $this->__switchNodes($node_a,$node_b);
00396
00397 return true;
00398 }
00399
00400 function __switchNodes($node_a,$node_b)
00401 {
00402 if(!$node_b["obj_id"])
00403 {
00404 return false;
00405 }
00406
00407 $query = "UPDATE crs_items SET ".
00408 "position = '".$node_a["position"]."' ".
00409 "WHERE obj_id = '".$node_b["obj_id"]."' ".
00410 "AND parent_id = '".$this->getParentId()."'";
00411
00412 $res = $this->ilDB->query($query);
00413
00414 $query = "UPDATE crs_items SET ".
00415 "position = '".$node_b["position"]."' ".
00416 "WHERE obj_id = '".$node_a["obj_id"]."' ".
00417 "AND parent_id = '".$this->getParentId()."'";
00418
00419 $res = $this->ilDB->query($query);
00420
00421 return true;
00422 }
00423
00424
00425 function __sort()
00426 {
00427 switch($this->course_obj->getOrderType())
00428 {
00429 case $this->course_obj->SORT_MANUAL:
00430 $this->items = ilUtil::sortArray($this->items,"position","asc",true);
00431 break;
00432
00433 case $this->course_obj->SORT_TITLE:
00434 $this->items = ilUtil::sortArray($this->items,"title","asc");
00435 break;
00436
00437 case $this->course_obj->SORT_ACTIVATION:
00438 $this->items = ilUtil::sortArray($this->items,"activation_end","asc");
00439 break;
00440 }
00441 return true;
00442 }
00443
00444 function _isActivated($a_item_id)
00445 {
00446 global $ilDB;
00447
00448 $query = "SELECT * FROM crs_items ".
00449 "WHERE obj_id = '".$a_item_id."'";
00450
00451 $res = $ilDB->query($query);
00452 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00453 {
00454 if($row->activation_unlimited)
00455 {
00456 return true;
00457 }
00458 if(time() > $row->activation_start and time() < $row->activation_end)
00459 {
00460 return true;
00461 }
00462 }
00463 return false;
00464 }
00465
00466
00467 }
00468 ?>