/ASTRA_Demo_Server/udrive/www/astra/interact/includes/lib/tags.inc.php

https://github.com/shafiqissani/ASTRA-College-Website · PHP · 199 lines · 71 code · 44 blank · 84 comment · 17 complexity · 00594eb666b9e8ddf8f198193b07adfc MD5 · raw file

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | Copyright (c) 2001 Christchurch College of Education |
  4. // +------------------------------------------------------------------------+
  5. // | This file is part of Interact. |
  6. // | |
  7. // | This program is free software; you can redistribute it and/or modify |
  8. // | it under the terms of the GNU General Public License as published by |
  9. // | the Free Software Foundation (version 2) |
  10. // | |
  11. // | This program is distributed in the hope that it will be useful, but |
  12. // | WITHOUT ANY WARRANTY; without even the implied warranty of |
  13. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
  14. // | General Public License for more details. |
  15. // | |
  16. // | You should have received a copy of the GNU General Public License |
  17. // | along with this program; if not, you can view it at |
  18. // | http://www.opensource.org/licenses/gpl-license.php |
  19. // +------------------------------------------------------------------------+
  20. /**
  21. * Tag functions
  22. *
  23. * Contains any functions related to adding, modifying, displaying tags
  24. *
  25. * @package Common
  26. * @author Glen Davies <glen.davies@cce.ac.nz>
  27. * @copyright Christchurch College of Education 2005
  28. * @version $Id: tags.inc.php,v 1.14 2007/05/09 03:02:51 glendavies Exp $
  29. *
  30. */
  31. /**
  32. * A class that contains methods related to tag functions
  33. *
  34. * This class is part of the Interact online learning and collaboration platform.
  35. * It contains methods for retrieving,displying and modifying tags
  36. *
  37. * @package Tags
  38. * @author Glen Davies <glen.davies@cce.ac.nz>
  39. */
  40. class InteractTags {
  41. /**
  42. * A method of class Tags to add a new tag, first checks to see if tag already exists
  43. * @param string $tag_string a string of tags separated by character other than space
  44. * @return array $tag_array an array of individual tags
  45. */
  46. function splitTags($tag_string) {
  47. return preg_split("/,|;/", $tag_string);
  48. } //end splitTags()
  49. /**
  50. * A method of class Tags to add a new tag, first checks to see if tag already exists
  51. * @param string $text text string of tag to add
  52. * @return int $tag_key key of new tag
  53. */
  54. function addTag($text) {
  55. global $CONN, $CONFIG;
  56. //first see if tag already exists
  57. $text = strip_tags($text);
  58. $tag_key = $this->getTagKey($text);
  59. if ($tag_key!=false || empty($text)) {
  60. return $tag_key;
  61. } else {
  62. $CONN->Execute("INSERT INTO {$CONFIG['DB_PREFIX']}tags(text) VALUES ('$text')");
  63. return $CONN->Insert_ID();
  64. }
  65. } //end addTag()
  66. /**
  67. * A method of class Tags to get tag_key for an existing tag
  68. * @param string $text text string to get tag key for
  69. * @return int $tag_key of tag, or false if tag does not exist
  70. */
  71. function getTagKey($text) {
  72. global $CONN, $CONFIG;
  73. $tag_key = $CONN->GetOne("SELECT tag_key FROM {$CONFIG['DB_PREFIX']}tags WHERE text='$text'");
  74. return $tag_key;
  75. } //end getTagKey()
  76. /**
  77. * A method of class Tags to get tag_text for an existing tag
  78. * @param int $tag_key text key to get tag key for
  79. * @return string $text of tag, or false if tag does not exist
  80. */
  81. function getTagText($tag_key) {
  82. global $CONN, $CONFIG;
  83. return $CONN->GetOne("SELECT text FROM {$CONFIG['DB_PREFIX']}tags WHERE tag_key='$tag_key'");
  84. } //end getTagKey()
  85. /**
  86. * A method of class Tags to get tag_key for an existing tag
  87. * @param int $tag_key tag_key of tag to add link for
  88. * @param int $module_key key of module to add tage for
  89. * @param int $user_key key of user to add tag for
  90. * @param int $entry_key key of individual module entry to add tag for
  91. * @param int $url_key key of url to add tag for *
  92. * @return true
  93. */
  94. function addTaglink($tag_key, $module_key=0, $user_key=0, $entry_key=0, $url_key=0) {
  95. global $CONN, $CONFIG;
  96. $date_added = $CONN->DBDate(date('Y-m-d H:i:s'));
  97. $CONN->Execute("INSERT INTO {$CONFIG['DB_PREFIX']}tag_links(tag_key, module_key, user_key, entry_key, url_key, date_added) VALUES ('$tag_key', '$module_key', '$user_key', '$entry_key', '$url_key', $date_added)");
  98. return true;
  99. } //end addTaglink()
  100. /**
  101. * A method of class Tags to add a string of tags
  102. * @param int $tag_string string of tags to add
  103. * @param int $module_key key of module to add tage for
  104. * @param int $user_key key of user to add tag for
  105. * @param int $entry_key key of individual module entry to add tag for
  106. * @param int $url_key key of url to add tag for
  107. * @return true
  108. */
  109. function addTags($tag_string, $module_key=0, $user_key=0, $entry_key=0, $url_key=0) {
  110. global $CONN, $CONFIG;
  111. $tag_array = $this->splitTags($tag_string);
  112. if (is_array($tag_array)) {
  113. $count = count($tag_array);
  114. for ($i=0; $i<$count; $i++) {
  115. $tag_key = $this->addTag(trim(strtolower($tag_array[$i])));
  116. $this->addTaglink($tag_key, $module_key, $user_key, $entry_key, $url_key);
  117. }
  118. }
  119. return true;
  120. } //end addTags()
  121. /**
  122. * A method of class Tags to get an array of tags for given user or module
  123. * @param int $module_key key of module to get tags for
  124. * @param int $user_key key of user to get tags for
  125. * @param int $entry_key key of individual module entry to get tags for
  126. * @return true
  127. */
  128. function getTags($module_key='', $user_key='', $entry_key='', $url_key='') {
  129. global $CONN, $CONFIG;
  130. if (isset($module_key) && $module_key!='') {
  131. $module_limit = "AND module_key='$module_key'";
  132. } else {
  133. $module_limit = '';
  134. }
  135. if (isset($user_key) && $user_key!='') {
  136. $user_limit = "AND user_key='$user_key'";
  137. } else {
  138. $user_limit = '';
  139. }
  140. if (isset($entry_key) && $entry_key!='') {
  141. $entry_limit = "AND entry_key='$entry_key'";
  142. } else {
  143. $entry_limit = '';
  144. }
  145. if (isset($url_key) && $url_key!='') {
  146. $url_limit = "AND url_key='$url_key'";
  147. } else {
  148. $url_limit = '';
  149. }
  150. $CONN->SetFetchMode('ADODB_FETCH_ASSOC');
  151. return $CONN->GetArray("SELECT {$CONFIG['DB_PREFIX']}tags.tag_key, text, COUNT(*) as count FROM {$CONFIG['DB_PREFIX']}tags, {$CONFIG['DB_PREFIX']}tag_links WHERE {$CONFIG['DB_PREFIX']}tags.tag_key={$CONFIG['DB_PREFIX']}tag_links.tag_key $module_limit $user_limit $entry_limit $url_limit GROUP BY {$CONFIG['DB_PREFIX']}tags.tag_key ORDER BY count DESC");
  152. $CONN->SetFetchMode('ADODB_FETCH_NUM');
  153. } //end getTags()
  154. }
  155. ?>