1<?php 2/* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */ 3 4include_once("Services/Classification/classes/class.ilClassificationProvider.php"); 5 6/** 7 * Tag classification provider 8 * 9 * @author Jörg Lützenkirchen <luetzenkirchen@leifos.com> 10 * @version $Id$ 11 * 12 * @ingroup ServicesTagging 13 */ 14class ilTaggingClassificationProvider extends ilClassificationProvider 15{ 16 /** 17 * @var ilLanguage 18 */ 19 protected $lng; 20 21 /** 22 * @var ilObjUser 23 */ 24 protected $user; 25 26 /** 27 * @var ilTree 28 */ 29 protected $tree; 30 31 32 /** 33 * Constructor 34 */ 35 public function __construct($a_parent_ref_id, $a_parent_obj_id, $a_parent_obj_type) 36 { 37 global $DIC; 38 parent::__construct($a_parent_ref_id, $a_parent_obj_id, $a_parent_obj_type); 39 40 $this->lng = $DIC->language(); 41 $this->user = $DIC->user(); 42 $this->tree = $DIC->repositoryTree(); 43 } 44 45 protected $enable_all_users; // [bool] 46 protected $selection; // [string] 47 48 protected function init() 49 { 50 $tags_set = new ilSetting("tags"); 51 $this->enable_all_users = (bool) $tags_set->get("enable_all_users", false); 52 } 53 54 public static function isActive($a_parent_ref_id, $a_parent_obj_id, $a_parent_obj_type) 55 { 56 global $DIC; 57 58 $ilUser = $DIC->user(); 59 60 // we currently only check for the parent object setting 61 // might change later on (parent containers) 62 include_once "Services/Object/classes/class.ilObjectServiceSettingsGUI.php"; 63 $valid = ilContainer::_lookupContainerSetting( 64 $a_parent_obj_id, 65 ilObjectServiceSettingsGUI::TAG_CLOUD, 66 false 67 ); 68 69 if ($valid) { 70 $tags_set = new ilSetting("tags"); 71 if (!$tags_set->get("enable_all_users", false) && 72 $ilUser->getId() == ANONYMOUS_USER_ID) { 73 $valid = false; 74 } 75 } 76 77 return $valid; 78 } 79 80 public function render(array &$a_html, $a_parent_gui) 81 { 82 $lng = $this->lng; 83 84 $lng->loadLanguageModule("tagging"); 85 86 $all_tags = $this->getSubTreeTags(); 87 if ($all_tags) { 88 // see ilPDTaggingBlockGUI::getTagCloud(); 89 90 $map = array( 91 "personal" => $lng->txt("tagging_my_tags"), 92 "other" => $lng->txt("tagging_other_users") 93 ); 94 foreach ($map as $type => $title) { 95 $tags = $all_tags[$type]; 96 if ($tags) { 97 $max = 1; 98 foreach ($tags as $tag => $counter) { 99 $max = max($counter, $max); 100 } 101 reset($tags); 102 103 $tpl = new ilTemplate("tpl.tag_cloud_block.html", true, true, "Services/Tagging"); 104 105 $tpl->setCurrentBlock("tag_bl"); 106 foreach ($tags as $tag => $counter) { 107 $tpl->setVariable("TAG_TYPE", $type); 108 $tpl->setVariable("TAG_TITLE", $tag); 109 $tpl->setVariable("TAG_CODE", md5($tag)); 110 $tpl->setVariable( 111 "REL_CLASS", 112 ilTagging::getRelevanceClass($counter, $max) 113 ); 114 if (is_array($this->selection[$type]) && 115 in_array($tag, $this->selection[$type])) { 116 $tpl->setVariable("HIGHL_CLASS", ' ilHighlighted'); 117 } 118 119 $tpl->parseCurrentBlock(); 120 } 121 122 $a_html[] = array( 123 "title" => $title, 124 "html" => $tpl->get() 125 ); 126 } 127 } 128 129 /* 130 if($this->selection) 131 { 132 $a_html[] = array( 133 "title" => "Related Tags", 134 "html" => ":TODO:" 135 ); 136 } 137 */ 138 } 139 } 140 141 142 public function importPostData($a_saved = null) 143 { 144 $type = trim($_REQUEST["tag_type"]); 145 $tag_code = trim($_REQUEST["tag"]); // using codes to avoid encoding issues 146 if ($type && $tag_code) { 147 // code to tag 148 $found = null; 149 foreach ($this->getSubTreeTags() as $tags) { 150 foreach (array_keys($tags) as $tag) { 151 if (md5($tag) == $tag_code) { 152 $found = $tag; 153 break(2); 154 } 155 } 156 } 157 if ($found) { 158 /* single select 159 if(is_array($a_saved[$type]) && 160 in_array($found, $a_saved[$type])) 161 { 162 return; 163 } 164 return array($type=>array($found)); 165 */ 166 // multi select 167 if (is_array($a_saved[$type]) && 168 in_array($found, $a_saved[$type])) { 169 $key = array_search($found, $a_saved[$type]); 170 unset($a_saved[$type][$key]); 171 if (!sizeof($a_saved[$type])) { 172 unset($a_saved[$type]); 173 } 174 } else { 175 $a_saved[$type][] = $found; 176 } 177 } 178 return $a_saved; 179 } 180 } 181 182 public function setSelection($a_value) 183 { 184 $this->selection = $a_value; 185 } 186 187 public function getFilteredObjects() 188 { 189 $ilUser = $this->user; 190 191 if (!$this->selection) { 192 return; 193 } 194 195 include_once "Services/Tagging/classes/class.ilTagging.php"; 196 197 $types = array("personal"); 198 if ($this->enable_all_users) { 199 $types[] = "other"; 200 } 201 202 $found = array(); 203 foreach ($types as $type) { 204 if (is_array($this->selection[$type])) { 205 $invert = ($type == "personal") 206 ? false 207 : true; 208 209 foreach ($this->selection[$type] as $tag) { 210 $found[$tag] = array_keys(ilTagging::_findObjectsByTag($tag, $ilUser->getId(), $invert)); 211 } 212 } 213 } 214 215 /* OR 216 $res = array(); 217 foreach($found as $tag => $ids) 218 { 219 $res = array_merge($res, $ids); 220 } 221 */ 222 223 // AND 224 $res = null; 225 foreach ($found as $tag => $ids) { 226 if ($res === null) { 227 $res = $ids; 228 } else { 229 $res = array_intersect($res, $ids); 230 } 231 } 232 233 if (sizeof($res)) { 234 return array_unique($res); 235 } 236 } 237 238 protected function getSubTreeTags() 239 { 240 $tree = $this->tree; 241 $ilUser = $this->user; 242 243 $sub_ids = array(); 244 foreach ($tree->getSubTree($tree->getNodeData($this->parent_ref_id)) as $sub_item) { 245 if ($sub_item["ref_id"] != $this->parent_ref_id && 246 $sub_item["type"] != "rolf" && 247 !$tree->isDeleted($sub_item["ref_id"])) { 248 $sub_ids[$sub_item["obj_id"]] = $sub_item["type"]; 249 } 250 } 251 252 if ($sub_ids) { 253 $only_user = $this->enable_all_users 254 ? null 255 : $ilUser->getId(); 256 257 include_once "Services/Tagging/classes/class.ilTagging.php"; 258 return ilTagging::_getTagCloudForObjects($sub_ids, $only_user, $ilUser->getId()); 259 } 260 } 261 262 public function initListGUI(ilObjectListGUI $a_list_gui) 263 { 264 $a_list_gui->enableTags(true); 265 } 266} 267