+ /**
+ * Check if report is private.
+ *
+ * @param int $instance_id
+ *
+ * @return bool
+ */
+ public static function reportIsPrivate($instance_id) {
+ $owner_id = CRM_Core_DAO::getFieldValue('CRM_Report_DAO_ReportInstance', $instance_id, 'owner_id', 'id');
+ if ($owner_id) {
+ return TRUE;
+ }
+ return FALSE;
+ }
+
+ /**
+ * Check if the logged in user is the owner.
+ *
+ * @param int $instance_id
+ *
+ * @return TRUE if contact owns the report, FALSE if not
+ */
+ public static function contactIsOwner($instance_id) {
+ $session = CRM_Core_Session::singleton();
+ $contact_id = $session->get('userID');
+ $owner_id = CRM_Core_DAO::getFieldValue('CRM_Report_DAO_ReportInstance', $instance_id, 'owner_id', 'id');
+ if ($contact_id === $owner_id) {
+ return TRUE;
+ }
+ return FALSE;
+ }
+
+ /**
+ * Check if the logged in contact can administer the report.
+ *
+ * @param int $instance_id
+ *
+ * @return bool
+ * True if contact can edit the private report, FALSE if not.
+ */
+ public static function contactCanAdministerReport($instance_id) {
+ if (self::reportIsPrivate($instance_id)) {
+ if (self::contactIsOwner($instance_id) || CRM_Core_Permission::check('access all private reports')) {
+ return TRUE;
+ }
+ }
+ elseif (CRM_Core_Permission::check('administer Reports')) {
+ return TRUE;
+ }
+ return FALSE;
+ }
+