You are here

private function nexcloud::add_accessmetrics in filedepot 7

Same name and namespace in other branches
  1. 6 nexcloud.class.php \nexcloud::add_accessmetrics()
1 call to nexcloud::add_accessmetrics()
nexcloud::update_tags in ./nexcloud.class.php

File

./nexcloud.class.php, line 147
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 add_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=:iid", array(
      ':type' => $this->_type,
      ':iid' => $itemid,
    ))
      ->fetchField() > 0) {

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

      // Add any new tags
      foreach ($tagids as $id) {
        if (!empty($id)) {

          // 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;
          if ($haveGroupsToUpdate or $haveRolesToUpdate) {
            db_query("UPDATE {nextag_words} SET metric=metric+1 WHERE id=:id", 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 ($haveGroupsToUpdate) {
              $permAccessMetric['groupid'] = $perms['groups'];
            }
            if ($haveRolesToUpdate) {
              $permAccessMetric['roleid'] = $perms['roles'];
            }
            foreach ($permAccessMetric as $permKey => $permValue) {
              foreach ($permValue as $permid) {
                if ($permid > 0) {
                  $sql = "SELECT count(tagid) FROM {nextag_metrics} WHERE tagid = :tid AND type = :type AND " . $permKey . " = :permkey";
                  if (db_query($sql, array(
                    ':tid' => $id,
                    ':type' => $this->_type,
                    ':permkey' => $permid,
                  ))
                    ->fetchField() > 0) {
                    $sql = "UPDATE {nextag_metrics} SET metric=metric+1, last_updated=:updated " . "WHERE tagid=:tid AND type=:type AND " . $permKey . "=:permkey";
                    db_query($sql, array(
                      'updated' => time(),
                      ':tid' => $id,
                      ':type' => $this->_type,
                      ':permkey' => $permid,
                    ));
                  }
                  else {
                    $sql = "INSERT INTO {nextag_metrics} (tagid,type," . $permKey . ",metric,last_updated) " . "VALUES (:id, :type, :permkey, :alias, :time)";
                    db_query($sql, array(
                      ':id' => $id,
                      ':type' => $this->_type,
                      ':permkey' => $permid,
                      ':alias' => 1,
                      ':time' => time(),
                    ));
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}