1<?php 2/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */ 3 4/** 5* @author Jörg Lützenkirchen <luetzenkirchen@leifos.com> 6* 7* @version $Id: class.ilLPStatusCollectionManual.php 40252 2013-03-01 12:21:49Z jluetzen $ 8* 9* @package ilias-tracking 10* 11*/ 12 13include_once './Services/Tracking/classes/class.ilLPStatus.php'; 14 15class ilLPStatusCollectionManual extends ilLPStatus 16{ 17 public static function _getInProgress($a_obj_id) 18 { 19 $status_info = ilLPStatusWrapper::_getStatusInfo($a_obj_id); 20 21 // find any completed item 22 $users = array(); 23 if (is_array($status_info['completed'])) { 24 foreach ($status_info['completed'] as $in_progress) { 25 $users = array_merge($users, $in_progress); 26 } 27 $users = array_unique($users); 28 } 29 30 // remove all users which have completed ALL items 31 $users = array_diff($users, ilLPStatusWrapper::_getCompleted($a_obj_id)); 32 33 return $users; 34 } 35 36 public static function _getCompleted($a_obj_id) 37 { 38 $status_info = ilLPStatusWrapper::_getStatusInfo($a_obj_id); 39 40 $counter = 0; 41 $users = array(); 42 foreach ($status_info['items'] as $item_id) { 43 $tmp_users = $status_info['completed'][$item_id]; 44 45 if (!$counter++) { 46 $users = $tmp_users; 47 } else { 48 $users = array_intersect($users, $tmp_users); 49 } 50 } 51 $users = array_unique($users); 52 53 return $users; 54 } 55 56 public static function _getStatusInfo($a_obj_id) 57 { 58 $status_info = array(); 59 60 include_once "Services/Object/classes/class.ilObjectLP.php"; 61 $olp = ilObjectLP::getInstance($a_obj_id); 62 $collection = $olp->getCollectionInstance(); 63 if ($collection) { 64 $status_info["items"] = $collection->getItems($a_obj_id); 65 66 foreach ($status_info["items"] as $item_id) { 67 $status_info["completed"][$item_id] = array(); 68 } 69 70 $ref_ids = ilObject::_getAllReferences($a_obj_id); 71 $ref_id = end($ref_ids); 72 $possible_items = $collection->getPossibleItems($ref_id); 73 $chapter_ids = array_intersect( 74 array_keys($possible_items), 75 $status_info["items"] 76 ); 77 78 // fix order (adapt from possible items) 79 $status_info["items"] = $chapter_ids; 80 81 if ($chapter_ids) { 82 $status = self::_getObjectStatus($a_obj_id); 83 84 foreach ($chapter_ids as $item_id) { 85 $status_info["item_titles"][$item_id] = $possible_items[$item_id]["title"]; 86 87 if (isset($status[$item_id])) { 88 foreach ($status[$item_id] as $user_id => $user_status) { 89 if ($user_status) { 90 $status_info["completed"][$item_id][] = $user_id; 91 } 92 } 93 } 94 } 95 } 96 } 97 98 return $status_info; 99 } 100 101 public function determineStatus($a_obj_id, $a_user_id, $a_obj = null) 102 { 103 $info = self::_getStatusInfo($a_obj_id); 104 105 if (is_array($info["completed"])) { 106 $completed = true; 107 $in_progress = false; 108 foreach ($info["completed"] as $user_ids) { 109 // has completed at least 1 item 110 if (in_array($a_user_id, $user_ids)) { 111 $in_progress = true; 112 } 113 // must have completed all items to complete collection 114 else { 115 $completed = false; 116 } 117 } 118 if ($completed) { 119 return self::LP_STATUS_COMPLETED_NUM; 120 } 121 if ($in_progress) { 122 return self::LP_STATUS_IN_PROGRESS_NUM; 123 } 124 } 125 126 return self::LP_STATUS_NOT_ATTEMPTED_NUM; 127 } 128 129 public static function _getObjectStatus($a_obj_id, $a_user_id = null) 130 { 131 global $DIC; 132 133 $ilDB = $DIC['ilDB']; 134 135 $res = array(); 136 137 $sql = "SELECT subitem_id, completed, usr_id, last_change" . 138 " FROM ut_lp_coll_manual" . 139 " WHERE obj_id = " . $ilDB->quote($a_obj_id, "integer"); 140 if ($a_user_id) { 141 $sql .= " AND usr_id = " . $ilDB->quote($a_user_id, "integer"); 142 } 143 $set = $ilDB->query($sql); 144 while ($row = $ilDB->fetchAssoc($set)) { 145 if (!$a_user_id) { 146 $res[$row["subitem_id"]][$row["usr_id"]] = $row["completed"]; 147 } else { 148 $res[$row["subitem_id"]] = array($row["completed"], $row["last_change"]); 149 } 150 } 151 152 return $res; 153 } 154 155 public static function _setObjectStatus($a_obj_id, $a_user_id, array $a_completed = null) 156 { 157 global $DIC; 158 159 $ilDB = $DIC['ilDB']; 160 161 $now = time(); 162 163 if (!$a_completed) { 164 $a_completed = array(); 165 } 166 167 include_once './Services/Object/classes/class.ilObjectLP.php'; 168 $olp = ilObjectLP::getInstance($a_obj_id); 169 $collection = $olp->getCollectionInstance(); 170 if ($collection) { 171 $existing = self::_getObjectStatus($a_obj_id, $a_user_id); 172 173 foreach ($collection->getItems() as $item_id) { 174 if (isset($existing[$item_id])) { 175 // value changed 176 if ((!$existing[$item_id][0] && in_array($item_id, $a_completed)) || 177 ($existing[$item_id][0] && !in_array($item_id, $a_completed))) { 178 $ilDB->manipulate("UPDATE ut_lp_coll_manual SET " . 179 " completed = " . $ilDB->quote(in_array($item_id, $a_completed), "integer") . 180 " , last_change = " . $ilDB->quote($now, "integer") . 181 " WHERE obj_id = " . $ilDB->quote($a_obj_id, "integer") . 182 " AND usr_id = " . $ilDB->quote($a_user_id, "integer") . 183 " AND subitem_id = " . $ilDB->quote($item_id, "integer")); 184 } 185 } elseif (in_array($item_id, $a_completed)) { 186 $ilDB->manipulate("INSERT INTO ut_lp_coll_manual" . 187 "(obj_id,usr_id,subitem_id,completed,last_change)" . 188 " VALUES (" . $ilDB->quote($a_obj_id, "integer") . 189 " , " . $ilDB->quote($a_user_id, "integer") . 190 " , " . $ilDB->quote($item_id, "integer") . 191 " , " . $ilDB->quote(1, "integer") . 192 " , " . $ilDB->quote($now, "integer") . ")"); 193 } 194 } 195 } 196 197 include_once "Services/Tracking/classes/class.ilLPStatusWrapper.php"; 198 ilLPStatusWrapper::_updateStatus($a_obj_id, $a_user_id); 199 } 200} 201