[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/tag/ -> locallib.php (source)

   1  <?php // $Id: locallib.php,v 1.1.2.18 2008/09/24 08:04:01 scyrma Exp $
   2  
   3  /**
   4   * locallib.php - moodle tag local library - output functions
   5   *
   6   * @version: $Id: locallib.php,v 1.1.2.18 2008/09/24 08:04:01 scyrma Exp $
   7   * @licence http://www.gnu.org/copyleft/gpl.html GNU Public License
   8   * @package moodlecore
   9   *
  10   */
  11  
  12  /**
  13   * Prints a tag cloud
  14   *
  15   * @param array $tagcloud array of tag objects (fields: id, name, rawname, count and flag)
  16   * @param $return if true return html string
  17   */
  18  function tag_print_cloud($nr_of_tags=150, $return=false) {
  19  
  20      global $CFG;
  21  
  22      $can_manage_tags = has_capability('moodle/tag:manage', get_context_instance(CONTEXT_SYSTEM));
  23  
  24      if ( !$tagsincloud = get_records_sql('SELECT tg.rawname, tg.id, tg.name, tg.tagtype, COUNT(ti.id) AS count, tg.flag '.
  25          'FROM '. $CFG->prefix .'tag_instance ti INNER JOIN '. $CFG->prefix .'tag tg ON tg.id = ti.tagid '.
  26          'WHERE ti.itemtype != \'tag\' '.
  27          'GROUP BY tg.id, tg.rawname, tg.name, tg.flag, tg.tagtype '.
  28          'ORDER BY count DESC, tg.name ASC', 0, $nr_of_tags) ) {
  29          $tagsincloud = array();
  30      }
  31  
  32      $tagkeys = array_keys($tagsincloud);
  33      $firsttagkey = $tagkeys[0];
  34      $maxcount = $tagsincloud[$firsttagkey]->count;
  35  
  36      $etags = array();
  37  
  38      foreach ($tagsincloud as $tag) {
  39          $size = (int) (( $tag->count / $maxcount) * 20);
  40          $tag->class = "$tag->tagtype s$size";
  41          $etags[] = $tag;
  42      }
  43  
  44      usort($etags, "tag_cloud_sort");
  45      $output = '';
  46      $output .= "\n<ul class='tag_cloud inline-list'>\n";
  47      foreach ($etags as $tag) {
  48          if ($tag->flag > 0 && $can_manage_tags) {
  49              $tagname = '<span class="flagged-tag">'. tag_display_name($tag) .'</span>';
  50          } else {
  51              $tagname = tag_display_name($tag);
  52          }
  53  
  54          $link = $CFG->wwwroot .'/tag/index.php?tag='. rawurlencode($tag->name);
  55          $output .= '<li><a href="'. $link .'" class="'. $tag->class .'" '.
  56              'title="'. get_string('numberofentries', 'blog', $tag->count) .'">'.
  57              $tagname .'</a></li> ';
  58      }
  59      $output .= "\n</ul>\n";
  60  
  61      if ($return) {
  62          return $output;
  63      } else {
  64          echo $output;
  65      }
  66  }
  67  
  68  /**
  69   * This function is used by print_tag_cloud, to usort() the tags in the cloud.  
  70   * See php.net/usort for the parameters documentation. This was originally in
  71   * blocks/blog_tags/block_blog_tags.php, named blog_tags_sort().
  72   */
  73  function tag_cloud_sort($a, $b) {
  74      global $CFG;
  75  
  76      if (empty($CFG->tagsort)) {
  77          $tagsort = 'name'; // by default, sort by name
  78      } else {
  79          $tagsort = $CFG->tagsort;
  80      }
  81  
  82      if (is_numeric($a->$tagsort)) {
  83          return ($a->$tagsort == $b->$tagsort) ? 0 : ($a->$tagsort > $b->$tagsort) ? 1 : -1;
  84      } elseif (is_string($a->$tagsort)) {
  85          return strcmp($a->$tagsort, $b->$tagsort);
  86      } else {
  87          return 0;
  88      }
  89  }
  90  
  91  /**
  92   * Prints a box with the description of a tag and its related tags
  93   *
  94   * @param unknown_type $tag_object
  95   * @param $return if true return html string
  96   */
  97  function tag_print_description_box($tag_object, $return=false) {
  98  
  99      global $USER, $CFG;
 100  
 101      $max_tags_displayed = 10; // todo: turn this into a system setting
 102  
 103      $tagname  = tag_display_name($tag_object);
 104      $related_tags = tag_get_related_tags($tag_object->id, TAG_RELATED_ALL, $max_tags_displayed+1); // this gets one more than we want
 105  
 106      $content = !empty($tag_object->description) || $related_tags;
 107      $output = '';
 108  
 109      if ($content) {
 110          $output .= print_box_start('generalbox', 'tag-description', true);
 111      }
 112  
 113      if (!empty($tag_object->description)) {
 114          $options = new object();
 115          $options->para = false;
 116          $output .= format_text($tag_object->description, $tag_object->descriptionformat, $options);
 117      }
 118  
 119      if ($related_tags) {
 120          $more_links = false;
 121          if (count($related_tags) > $max_tags_displayed) {
 122              array_pop($related_tags);
 123              $more_links = true;
 124          }
 125          $output .= '<br /><br /><strong>'. get_string('relatedtags', 'tag') .': </strong>'. tag_get_related_tags_csv($related_tags);
 126          if ($more_links) {
 127              $output .= ' ...';
 128          }
 129      }
 130  
 131      if ($content) {
 132          $output .= print_box_end(true);
 133      }
 134  
 135      if ($return) {
 136          return $output;
 137      } else {
 138          echo $output;
 139      }
 140  }
 141  
 142  /**
 143   * Prints a box that contains the management links of a tag
 144   *
 145   * @param $tagid
 146   * @param $return if true return html string
 147   */
 148  function tag_print_management_box($tag_object, $return=false) {
 149  
 150      global $USER, $CFG;
 151  
 152      $tagname  = tag_display_name($tag_object);
 153      $output = '';
 154  
 155      if (!isguestuser()) {
 156          $output .= print_box_start('box','tag-management-box', true);
 157          $systemcontext   = get_context_instance(CONTEXT_SYSTEM);
 158          $links = array();
 159          
 160          // Add a link for users to add/remove this from their interests
 161          if (tag_record_tagged_with('user', $USER->id, $tag_object->name)) {
 162              $links[] = '<a href="'. $CFG->wwwroot .'/tag/user.php?action=removeinterest&amp;sesskey='. sesskey() .'&amp;tag='. rawurlencode($tag_object->name) .'">'. get_string('removetagfrommyinterests', 'tag', $tagname) .'</a>';
 163          } else {
 164              $links[] = '<a href="'. $CFG->wwwroot .'/tag/user.php?action=addinterest&amp;sesskey='. sesskey() .'&amp;tag='. rawurlencode($tag_object->name) .'">'. get_string('addtagtomyinterests', 'tag', $tagname) .'</a>';
 165          }
 166  
 167          // flag as inappropriate link
 168          $links[] = '<a href="'. $CFG->wwwroot .'/tag/user.php?action=flaginappropriate&amp;sesskey='. sesskey() .'&amp;tag='. rawurlencode($tag_object->name) .'">'. get_string('flagasinappropriate', 'tag', rawurlencode($tagname)) .'</a>';
 169  
 170          // Edit tag: Only people with moodle/tag:edit capability who either have it as an interest or can manage tags
 171          if (has_capability('moodle/tag:edit', $systemcontext) || 
 172              has_capability('moodle/tag:manage', $systemcontext)) {
 173              $links[] = '<a href="'. $CFG->wwwroot .'/tag/edit.php?tag='. rawurlencode($tag_object->name) .'">'. get_string('edittag', 'tag') .'</a>';
 174          }
 175  
 176          $output .= implode(' | ', $links);
 177          $output .= print_box_end(true);
 178      }
 179  
 180      if ($return) {
 181          return $output;
 182      } else {
 183          echo $output;
 184      }
 185  }
 186  
 187  /**
 188   * Prints the tag search box
 189   *
 190   * @param bool $return if true return html string
 191   */
 192  function tag_print_search_box($return=false) {
 193      global $CFG;
 194  
 195      $output = print_box_start('','tag-search-box', true);
 196      $output .= '<form action="'.$CFG->wwwroot.'/tag/search.php" style="display:inline">';
 197      $output .= '<div>';
 198      $output .= '<input id="searchform_search" name="query" type="text" size="40" />';
 199      $output .= '<button id="searchform_button" type="submit">'. get_string('search', 'tag') .'</button><br />';
 200      $output .= '</div>';
 201      $output .= '</form>';
 202      $output .= print_box_end(true);
 203  
 204      if ($return) {
 205          return $output;
 206      }
 207      else {
 208          echo $output;
 209      }
 210  }
 211  
 212  /**
 213   * Prints the tag search results
 214   *
 215   * @param string $query text that tag names will be matched against
 216   * @param int $page current page
 217   * @param int $perpage nr of users displayed per page
 218   * @param $return if true return html string
 219   */
 220  function tag_print_search_results($query,  $page, $perpage, $return=false) {
 221  
 222      global $CFG, $USER;
 223  
 224      $query = array_shift(tag_normalize($query, TAG_CASE_ORIGINAL));
 225  
 226      $count = sizeof(tag_find_tags($query, false));
 227      $tags = array();
 228  
 229      if ( $found_tags = tag_find_tags($query, true,  $page * $perpage, $perpage) ) {
 230          $tags = array_values($found_tags);
 231      }
 232  
 233      $baseurl = $CFG->wwwroot.'/tag/search.php?query='. rawurlencode($query);
 234      $output = '';
 235  
 236      // link "Add $query to my interests"
 237      $addtaglink = '';
 238      if( !tag_record_tagged_with('user', $USER->id, $query) ) {
 239          $addtaglink = '<a href="'. $CFG->wwwroot .'/tag/user.php?action=addinterest&amp;sesskey='. sesskey() .'&amp;tag='. rawurlencode($query) .'">';
 240          $addtaglink .= get_string('addtagtomyinterests', 'tag', htmlspecialchars($query)) .'</a>';
 241      }
 242  
 243      if ( !empty($tags) ) { // there are results to display!!
 244          $output .= print_heading(get_string('searchresultsfor', 'tag', htmlspecialchars($query)) ." : {$count}", '', 3, 'main', true);
 245  
 246          //print a link "Add $query to my interests"
 247          if (!empty($addtaglink)) {
 248              $output .= print_box($addtaglink, 'box', 'tag-management-box', true);
 249          }
 250  
 251          $nr_of_lis_per_ul = 6;
 252          $nr_of_uls = ceil( sizeof($tags) / $nr_of_lis_per_ul );
 253  
 254          $output .= '<ul id="tag-search-results">';
 255          for($i = 0; $i < $nr_of_uls; $i++) {
 256              $output .= '<li>';
 257              foreach (array_slice($tags, $i * $nr_of_lis_per_ul, $nr_of_lis_per_ul) as $tag) {
 258                  $tag_link = ' <a href="'. $CFG->wwwroot .'/tag/index.php?id='. $tag->id .'">'. tag_display_name($tag) .'</a>';
 259                  $output .= '&#8226;'. $tag_link .'<br/>';
 260              }
 261              $output .= '</li>';
 262          }
 263          $output .= '</ul>';
 264          $output .= '<div>&nbsp;</div>'; // <-- small layout hack in order to look good in Firefox
 265  
 266          $output .= print_paging_bar($count, $page, $perpage, $baseurl .'&amp;', 'page', false, true);
 267      }
 268      else { //no results were found!!
 269          $output .= print_heading(get_string('noresultsfor', 'tag', htmlspecialchars($query)), '', 3, 'main' , true);
 270  
 271          //print a link "Add $query to my interests"
 272          if (!empty($addtaglink)) {
 273              $output .= print_box($addtaglink, 'box', 'tag-management-box', true);
 274          }
 275      }
 276  
 277      if ($return) {
 278          return $output;
 279      }
 280      else {
 281          echo $output;
 282      }
 283  }
 284  
 285  /**
 286   * Prints a table of the users tagged with the tag passed as argument
 287   *
 288   * @param $tag_object
 289   * @param int $users_per_row number of users per row to display
 290   * @param int $limitfrom prints users starting at this point (optional, required if $limitnum is set).
 291   * @param int $limitnum prints this many users (optional, required if $limitfrom is set).
 292   * @param $return if true return html string
 293   */
 294  function tag_print_tagged_users_table($tag_object, $limitfrom='' , $limitnum='', $return=false) {
 295  
 296      //List of users with this tag
 297      $userlist = tag_find_records($tag_object->name, 'user', $limitfrom, $limitnum);
 298  
 299      $output = tag_print_user_list($userlist, true);
 300  
 301      if ($return) {
 302          return $output;
 303      }
 304      else {
 305          echo $output;
 306      }
 307  }
 308  
 309  /**
 310   * Prints an individual user box
 311   *
 312   * @param $user user object (contains the following fields: id, firstname, lastname and picture)
 313   * @param $return if true return html string
 314   */
 315  function tag_print_user_box($user, $return=false) {
 316      global $CFG;
 317  
 318      $textlib = textlib_get_instance();
 319      $usercontext = get_context_instance(CONTEXT_USER, $user->id);
 320      $profilelink = '';
 321  
 322      if ( has_capability('moodle/user:viewdetails', $usercontext) || isteacherinanycourse($user->id) ) {
 323          $profilelink = $CFG->wwwroot .'/user/view.php?id='. $user->id;
 324      }
 325  
 326      $output = print_box_start('user-box', 'user'. $user->id, true);
 327      $fullname = fullname($user);
 328      $alt = '';
 329  
 330      if (!empty($profilelink)) {
 331          $output .= '<a href="'. $profilelink .'">';
 332          $alt = $fullname;
 333      }
 334  
 335      //print user image - if image is only content of link it needs ALT text!
 336      if ($user->picture) {
 337          $output .= '<img alt="'. $alt .'" class="user-image" src="'. $CFG->wwwroot .'/user/pix.php/'. $user->id .'/f1.jpg" />';
 338      } else {
 339          $output .= '<img alt="'. $alt .'" class="user-image" src="'. $CFG->wwwroot .'/pix/u/f1.png" />';
 340      }
 341      
 342      $output .= '<br />';
 343  
 344      if (!empty($profilelink)) {
 345          $output .= '</a>';
 346      }
 347  
 348      //truncate name if it's too big
 349      if ($textlib->strlen($fullname) > 26) {
 350          $fullname = $textlib->substr($fullname, 0, 26) .'...';
 351      }
 352  
 353      $output .= '<strong>'. $fullname .'</strong>';
 354      $output .= print_box_end(true);
 355  
 356      if ($return) {
 357          return $output;
 358      }
 359      else {
 360          echo $output;
 361      }
 362  }
 363  /**
 364   * Prints a list of users
 365   *
 366   * @param array $userlist an array of user objects
 367   * @param $return if true return html string
 368   */
 369  function tag_print_user_list($userlist, $return=false) {
 370  
 371      $output = '<ul class="inline-list">';
 372  
 373      foreach ($userlist as $user){
 374          $output .= '<li>'. tag_print_user_box($user, true) ."</li>\n";
 375      }
 376      $output .= "</ul>\n";
 377  
 378      if ($return) {
 379          return $output;
 380      }
 381      else {
 382          echo $output;
 383      }
 384  }
 385  
 386  
 387  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7