From 34d139c0b326e3946b69b72284ae9efa5b7f8791 Mon Sep 17 00:00:00 2001 From: prcdevgitbot Date: Wed, 14 Feb 2024 21:23:48 +0000 Subject: [PATCH] See https://github.com/pewresearch/pewresearch-org/commit/cc9100cedeb60cf4aac951cddbad5862f588ebf2 from refs/heads/release/5.0 --- includes/datasets/class-datasets.php | 3 ++- .../downloads-log/database/class-schema.php | 10 +++++++++- .../downloads-log/database/class-shape.php | 1 + .../downloads-log/database/class-table.php | 1 + .../datasets/downloads-log/database/index.php | 6 +++--- includes/datasets/downloads-log/index.php | 20 ++++++++++++------- 6 files changed, 29 insertions(+), 12 deletions(-) diff --git a/includes/datasets/class-datasets.php b/includes/datasets/class-datasets.php index 7ca31332..7aaeb803 100644 --- a/includes/datasets/class-datasets.php +++ b/includes/datasets/class-datasets.php @@ -230,7 +230,6 @@ public function restfully_download_dataset( WP_REST_Request $request ) { return new WP_Error( 'no_id', 'No dataset ID provided.', array( 'status' => 400 ) ); } $attachment_id = get_post_meta( $id, self::$download_meta_key, true ); - error_log( 'Dataset Download: ' . $attachment_id); if ( $attachment_id ) { $attachment_url = wp_get_attachment_url( $attachment_id ); // Log the download. @@ -238,6 +237,8 @@ public function restfully_download_dataset( WP_REST_Request $request ) { $download_logger->increment_download_total( $id ); $download_logger->log_monthly_download_count( $id ); $download_logger->log_uid_to_dataset( $id, $uid ); + + //@TODO: User accounts, log dataset download to user profile... return rest_ensure_response( array( 'file_url' => $attachment_url, ) ); diff --git a/includes/datasets/downloads-log/database/class-schema.php b/includes/datasets/downloads-log/database/class-schema.php index 6efb0630..76e9b3f4 100644 --- a/includes/datasets/downloads-log/database/class-schema.php +++ b/includes/datasets/downloads-log/database/class-schema.php @@ -8,7 +8,7 @@ class Dataset_Download_Schema extends BerlinDB_Schema { public $columns = array( - // dataset id + // entry id 'id' => array( 'name' => 'id', 'type' => 'int', @@ -18,6 +18,14 @@ class Dataset_Download_Schema extends BerlinDB_Schema { 'sortable' => true, ), + 'dataset_id' => array( + 'name' => 'dataset_id', + 'type' => 'int', + 'unsigned' => true, + 'searchable' => true, + 'sortable' => true, + ), + // total downloads 'total' => array( 'name' => 'total', diff --git a/includes/datasets/downloads-log/database/class-shape.php b/includes/datasets/downloads-log/database/class-shape.php index bc094c4c..d26dabc5 100644 --- a/includes/datasets/downloads-log/database/class-shape.php +++ b/includes/datasets/downloads-log/database/class-shape.php @@ -15,6 +15,7 @@ public function __construct( $item ) { parent::__construct( $item ); $this->id = (int) $this->id; + $this->dataset_id = (int) $this->dataset_id; $this->total = (int) $this->total; $this->breakdown = (string) $this->breakdown; $this->uids = (string) $this->uids; diff --git a/includes/datasets/downloads-log/database/class-table.php b/includes/datasets/downloads-log/database/class-table.php index aa8eb6d0..23eff117 100644 --- a/includes/datasets/downloads-log/database/class-table.php +++ b/includes/datasets/downloads-log/database/class-table.php @@ -55,6 +55,7 @@ class Dataset_Downloads_Log extends BerlinDB_Table { protected function set_schema() { $this->schema = ' id int NOT NULL AUTO_INCREMENT, + dataset_id int NOT NULL, total int NOT NULL, breakdown longtext NOT NULL, uids longtext NOT NULL, diff --git a/includes/datasets/downloads-log/database/index.php b/includes/datasets/downloads-log/database/index.php index f24c2489..f46272db 100644 --- a/includes/datasets/downloads-log/database/index.php +++ b/includes/datasets/downloads-log/database/index.php @@ -16,6 +16,6 @@ } // Uninstall the database. Uncomment this code to force the database to rebuild. -// if( $dataset_downloads->exists() ){ -// $dataset_downloads->uninstall(); -// } +if( $dataset_downloads->exists() ){ + // $dataset_downloads->uninstall(); +} diff --git a/includes/datasets/downloads-log/index.php b/includes/datasets/downloads-log/index.php index 755e5808..2f638606 100644 --- a/includes/datasets/downloads-log/index.php +++ b/includes/datasets/downloads-log/index.php @@ -206,14 +206,13 @@ public function log_monthly_download_count( $dataset_id ) { public function log_uid_to_dataset( $dataset_id, $uid ) { $query_args = array( - 'id' => $dataset_id, - 'orderby' => 'id', - 'order' => 'asc', - 'number' => 1, // Only retrieve a single record. - 'fields' => array( 'id', 'uids' ), + 'dataset_id' => $dataset_id, + 'orderby' => 'id', + 'order' => 'asc', + 'number' => 1, // Only retrieve a single record. + 'fields' => array( 'id', 'uids' ), ); $query = new Dataset_Downloads_Log_Query($query_args); - error_log('log_uid_to_dataset'. $dataset_id . ' ' . $uid); $response = false; @@ -238,12 +237,19 @@ public function log_uid_to_dataset( $dataset_id, $uid ) { // First time, create $response = $query->add_item( array( + 'dataset_id' => $dataset_id, 'uids' => maybe_serialize( array( $uid ) ), ) ); } - error_log("RESPONSE:".print_r($response, true)); return $response; } + + public function get_datasets_by_uid($uid) { + $query_args = array( + 'uids' => $uid, + 'fields' => array( 'id' ), + ); + } }