/**
  * Function to retreive all user access records for a specific category and user
  * @param type $cid
  * @param type $userid
  * @return filedepot_permission_object
  */
 function getPermissionObject($cid, $userid = 0)
 {
     global $user;
     if (intval($cid) < 1) {
         return filedepot_permission_object::createNoPermissionsObject($cid);
     }
     // Using a supplied userid or the current global one
     if ($userid == 0) {
         if (empty($user->uid) or $user->uid == 0) {
             $uid = 0;
         } else {
             $uid = $user->uid;
         }
     } else {
         $uid = $userid;
     }
     $account = user_load($uid);
     if (user_access('administer filedepot', $account) === TRUE) {
         return filedepot_permission_object::createFullPermissionObject($cid);
     } else {
         // Check to see if a permission object already exists
         if (isset(self::$_permission_objects[$uid][$cid])) {
             return self::$_permission_objects[$uid][$cid];
         }
         $po = new filedepot_permission_object($cid);
         // Check user access records
         $sql = "SELECT view,upload,upload_direct,upload_ver,approval,admin from {filedepot_access} WHERE catid=:cid AND permtype='user' AND permid=:uid";
         $query = db_query($sql, array('cid' => $cid, 'uid' => $uid));
         while ($rec = $query->fetchAssoc()) {
             list($view, $upload, $upload_dir, $upload_ver, $approval, $admin) = array_values($rec);
             $po->setTruePermissions($view, $upload, $upload_dir, $upload_ver, $approval, $admin);
         }
         if ($this->ogenabled) {
             // Retrieve all the Organic Groups this user is a member of
             $groupids = $this->get_user_groups($uid);
             foreach ($groupids as $gid) {
                 $sql = "SELECT view,upload,upload_direct,upload_ver,approval,admin from {filedepot_access} WHERE catid=:cid AND permtype='group' AND permid=:gid";
                 $query = db_query($sql, array(':cid' => $cid, ':gid' => $gid));
                 while ($rec = $query->fetchAssoc()) {
                     list($view, $upload, $upload_dir, $upload_ver, $approval, $admin) = array_values($rec);
                     $po->setTruePermissions($view, $upload, $upload_dir, $upload_ver, $approval, $admin);
                 }
             }
         }
         // For each role that the user is a member of - check if they have the right
         foreach ($account->roles as $rid => $role) {
             $sql = "SELECT view,upload,upload_direct,upload_ver,approval,admin from {filedepot_access} WHERE catid=:cid AND permtype='role' AND permid=:uid";
             $query = db_query($sql, array('cid' => $cid, 'uid' => $rid));
             while ($rec = $query->fetchAssoc()) {
                 list($view, $upload, $upload_dir, $upload_ver, $approval, $admin) = array_values($rec);
                 $po->setTruePermissions($view, $upload, $upload_dir, $upload_ver, $approval, $admin);
             }
         }
         self::$_permission_objects[$uid][$cid] = $po;
         return $po;
     }
 }
 /**
  * Create and return a new instance of the filedepot_permission_object with all permissions set
  * @param type $cid
  * @return \filedepot_permission_object
  */
 public static function createFullPermissionObject($cid)
 {
     $obj = new filedepot_permission_object($cid);
     $obj->setPermissions(true, true, true, true, true, true);
     return $obj;
 }