You are here

private function nexcloud::remove_accessmetrics in filedepot 7

Same name and namespace in other branches
  1. 6 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 205
nexcloud.class.php Tag Cloud class for the filedepot module

Class

nexcloud
@file nexcloud.class.php Tag Cloud class for the filedepot 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_query("SELECT count(itemid) FROM {nextag_items} WHERE type=:type AND itemid=:item", array(
      ':type' => $this->_type,
      ':item' => $itemid,
    ))
      ->fetchField() > 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=:id", array(
              ':id' => $id,
            ));
            db_query("DELETE FROM {nextag_words} WHERE id=:id and metric=1", array(
              ':id' => $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=:tid AND type=:type AND " . $permKey . "=:permkey AND metric=1", array(
                  ':tid' => $id,
                  ':type' => $this->_type,
                  ':permkey' => $permid,
                ));
                $sql = "UPDATE {nextag_metrics} SET metric=metric-1, last_updated=:time " . "WHERE tagid=:tid AND type=:type AND " . $permKey . "=:permkey";
                db_query($sql, array(
                  ':time' => time(),
                  ':tid' => $id,
                  ':type' => $this->_type,
                  ':permkey' => $permid,
                ));
              }
            }
          }
        }
      }
    }
  }
}