Skip to content

Commit

Permalink
See pewresearch/pewresearch-org@cc9100c from refs/heads/release/5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
prcdevgitbot committed Feb 14, 2024
1 parent 60329ea commit 34d139c
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 12 deletions.
3 changes: 2 additions & 1 deletion includes/datasets/class-datasets.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,15 @@ 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.
$download_logger = new Datasets_Download_Logger();
$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,
) );
Expand Down
10 changes: 9 additions & 1 deletion includes/datasets/downloads-log/database/class-schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class Dataset_Download_Schema extends BerlinDB_Schema {

public $columns = array(
// dataset id
// entry id
'id' => array(
'name' => 'id',
'type' => 'int',
Expand All @@ -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',
Expand Down
1 change: 1 addition & 0 deletions includes/datasets/downloads-log/database/class-shape.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions includes/datasets/downloads-log/database/class-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions includes/datasets/downloads-log/database/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
20 changes: 13 additions & 7 deletions includes/datasets/downloads-log/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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' ),
);
}
}

0 comments on commit 34d139c

Please sign in to comment.