You are here

private function nexcloud::remove_accessmetrics in filedepot 6

Same name and namespace in other branches
  1. 7 nexcloud.class.php \nexcloud::remove_accessmetrics()
2 calls to nexcloud::remove_accessmetrics()
nexcloud::clear_tags in ./nexcloud.class.php
nexcloud::update_tags in ./nexcloud.class.php

File

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

Class

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

Code

private function remove_accessmetrics($itemid, $tagids) {

  // Test that a valid array of tag id's is passed in
  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);

      // Remove if required unused tag related records for this item
      foreach ($tagids as $id) {
        if (!empty($id)) {

          // For each role or group with view access to this item - decrement and if need delete the access metric record count.
          $haveGroupsToRemove = count($perms['groups']) > 0;
          $haveRolesToRemove = count($perms['roles']) > 0;
          if ($haveGroupsToRemove or $haveRolesToRemove) {
            db_query("UPDATE {nextag_words} SET metric=metric-1 WHERE id=%d", $id);
            db_query("DELETE FROM {nextag_words} WHERE id=%d and metric=1", $id);

            // use 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 ($haveGroupsToRemove) {
              $permAccessMetric['groupid'] = $perms['groups'];
            }
            if ($haveRolesToRemove) {
              $permAccessMetric['roleid'] = $perms['roles'];
            }
            foreach ($permAccessMetric as $permKey => $permValue) {
              foreach ($permValue as $permid) {

                // Delete the tag metric access record if metric = 1 else decrement the metric count
                db_query("DELETE FROM {nextag_metrics} WHERE tagid=%d AND type='%s' AND " . $permKey . "=%d AND metric=1", $id, $this->_type, $permid);
                $sql = "UPDATE {nextag_metrics} SET metric=metric-1, last_updated=%d " . "WHERE tagid=%d AND type='%s' AND " . $permKey . "=%d";
                db_query($sql, time(), $id, $this->_type, $permid);
              }
            }
          }
        }
      }
    }
  }
}