You are here

function nexcloud::update_accessmetrics in filedepot 6

Same name and namespace in other branches
  1. 7 nexcloud.class.php \nexcloud::update_accessmetrics()
1 call to nexcloud::update_accessmetrics()
filedepotTagCloud::update_accessmetrics in ./nexcloud.class.php
1 method overrides nexcloud::update_accessmetrics()
filedepotTagCloud::update_accessmetrics in ./nexcloud.class.php

File

./nexcloud.class.php, line 239
nexcloud.class.php Tag Cloud class for the fildepot module

Class

nexcloud
@file nexcloud.class.php Tag Cloud class for the fildepot module

Code

function update_accessmetrics($itemid, $tagids = '') {
  if (empty($tagids)) {

    // Retrieve the tags field and convert into an array
    $tags = db_result(db_query("SELECT tags FROM {nextag_items} WHERE type='%s' AND itemid=%d", $this->_type, $itemid));
    if (!empty($tags)) {
      $tagids = explode(',', $tags);
    }
    else {
      $tagids = array();
    }
  }

  // Test that we now have a valid array of tag id's
  if (is_array($tagids) and count($tagids) > 0) {

    // Test that a valid item record exist
    if (db_result(db_query("SELECT count(itemid) FROM {nextag_items} WHERE type='%s' AND itemid=%d", $this->_type, $itemid)) > 0) {

      // Get item permissions to determine what rights to use for tag metrics record
      $perms = $this
        ->get_itemperms($itemid, true);

      // For each role or group with view access to this item - create or update the access metric record count.
      $haveGroupsToUpdate = count($perms['groups']) > 0;
      $haveRolesToUpdate = count($perms['roles']) > 0;

      // For each tag word - we need to update (add new or remove any un-used tag metric associated with this permission and tagword
      $metricRecords = array();

      // Will contain the processed metric records
      foreach ($tagids as $id) {
        if (!empty($id)) {
          if ($haveGroupsToUpdate or $haveRolesToUpdate) {

            // An array to handle the logic of whether to process groups, roles, or both
            // Use the key to track the field to update and the value to track the values
            $permAccessMetric = array();
            if ($haveGroupsToUpdate) {
              $permAccessMetric['groupid'] = $perms['groups'];
            }
            if ($haveRolesToUpdate) {
              $permAccessMetric['roleid'] = $perms['roles'];
            }
            foreach ($permAccessMetric as $permKey => $permValue) {
              foreach ($permValue as $permid) {
                if ($permid > 0) {
                  $result = db_query("SELECT id FROM {nextag_metrics} WHERE tagid = %d AND type = '%s' AND %s = %d", $id, $this->_type, $permKey, $permid);
                  $numrecs = 0;
                  if ($result) {
                    while ($rec = db_fetch_object($result)) {
                      $numrecs++;
                      if (!in_array($rec->id, $metricRecords)) {
                        $metricRecords[] = $rec->id;
                      }
                    }
                  }
                  if ($numrecs == 0) {
                    $sql = "INSERT INTO {nextag_metrics} (tagid,type," . $permKey . ",metric,last_updated) " . "VALUES (%d,'%s',%d,%d,%d)";
                    db_query($sql, $id, $this->_type, $permid, 1, time());
                    $metricRecords[] = db_last_insert_id('nextag_metrics', 'id');
                  }
                }
              }
            }
          }
        }

        /* Now we should delete any metric records that are not in the processed $metricRecords array
         * They would be records for access permissions that were removed
         */
        if (count($metricRecords) > 0) {
          $recids = implode(',', $metricRecords);
          db_query("DELETE FROM {nextag_metrics} WHERE tagid = %d and id NOT IN (%s)", $id, $recids);
        }
        else {
          db_query("DELETE FROM {nextag_metrics} WHERE tagid = %d ", $id);
        }

        // Delete any tagword records that are no longer used
        $result = db_query("SELECT id FROM {nextag_words} WHERE metric = 0");
        if ($result) {

          // Let's do one more test and make sure no items are using this tagword
          while ($A = db_fetch_array($result)) {

            /* REGEX - search for id that is the first id or has a leading comma
             *  must then have a trailing , or be the end of the field
             */
            $sql = "SELECT itemid FROM {nextag_items} WHERE type='{$this->_type}' AND ";
            $sql .= "tags REGEXP '(^|,){$A['id']}(,|\$)' ";
            if (db_result(db_query($sql)) == 0) {
              db_query("DELETE FROM {nextag_words} WHERE id=%s", $A['id']);
            }
          }
        }
      }
    }
  }
}