Bug 3134: [Follow-up] Ability to select multiple reports to delete at once
[koha.git] / C4 / SocialData.pm
blobebf2da6ce3db7595c20cf813e1512ce4848a6ff0
1 package C4::SocialData;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA 02111-1307 USA
18 use Modern::Perl;
20 use C4::Context;
21 use Business::ISBN;
22 use C4::Koha;
24 =head1 NAME
26 C4::SocialData - Koha functions for dealing with social datas
27 For now used by babeltheque, a french company providing, for books, comments, upload of videos, scoring (star)...
28 the social_data table could be used and improved by other provides.
30 =head1 SYNOPSIS
32 use C4::SocialData;
34 =head1 DESCRIPTION
36 The functions in this module deal with social datas
38 =head1 FUNCTIONS
40 =head2 get_data
42 Get social data from a biblio
44 params:
45 $isbn = isbn of the biblio (it must be the same in your database, isbn given to babelio)
47 returns:
48 this function returns an hashref with keys
50 isbn = isbn
51 num_critics = number of critics
52 num_critics_pro = number of profesionnal critics
53 num_quotations = number of quotations
54 num_videos = number of videos
55 score_avg = average score
56 num_scores = number of score
57 =cut
59 sub get_data {
60 my ( $isbn ) = @_;
61 my $dbh = C4::Context->dbh;
62 my $sth = $dbh->prepare( qq{SELECT * FROM social_data WHERE isbn = ? LIMIT 1} );
63 $sth->execute( $isbn );
64 my $results = $sth->fetchrow_hashref;
66 return $results;
69 =head2 update_data
71 Update Social data
73 params:
74 $url = url containing csv file with data
76 data separator : ; (semicolon)
77 data order : isbn ; active ; critics number , critics pro number ; quotations number ; videos number ; average score ; scores number
79 =cut
81 sub update_data {
82 my ( $output_filepath ) = @_;
84 my $dbh = C4::Context->dbh;
85 my $sth = $dbh->prepare( qq{INSERT INTO social_data (
86 `isbn`, `num_critics`, `num_critics_pro`, `num_quotations`, `num_videos`, `score_avg`, `num_scores`
87 ) VALUES ( ?, ?, ?, ?, ?, ?, ? )
88 ON DUPLICATE KEY UPDATE `num_critics`=?, `num_critics_pro`=?, `num_quotations`=?, `num_videos`=?, `score_avg`=?, `num_scores`=?
89 } );
91 open my $file, '<', $output_filepath or die "File $output_filepath can not be read";
92 my $sep = qq{;};
93 my $i = 0;
94 my $unknown = 0;
95 while ( my $line = <$file> ) {
96 my ( $isbn, $active, $num_critics, $num_critics_pro, $num_quotations, $num_videos, $score_avg, $num_scores ) = split $sep, $line;
97 next if not $active;
98 eval {
99 $sth->execute( $isbn, $num_critics, $num_critics_pro, $num_quotations, $num_videos, $score_avg, $num_scores,
100 $num_critics, $num_critics_pro, $num_quotations, $num_videos, $score_avg, $num_scores
103 if ( $@ ) {
104 warn "Can't insert $isbn ($@)";
105 } else {
106 $i++;
109 say "$i data insered or updated";
112 =head2 get_report
114 Get social data report
116 =cut
118 sub get_report {
119 my $dbh = C4::Context->dbh;
121 my $sth = $dbh->prepare( qq{
122 SELECT biblionumber, isbn FROM biblioitems
123 } );
124 $sth->execute;
125 my %results;
126 while ( my ( $biblionumber, $isbn ) = $sth->fetchrow() ) {
127 push @{ $results{no_isbn} }, { biblionumber => $biblionumber } and next if not $isbn;
128 my $original_isbn = $isbn;
129 $isbn =~ s/^\s*(\S*)\s*$/$1/;
130 $isbn = GetNormalizedISBN( $isbn, undef, undef );
131 $isbn = Business::ISBN->new( $isbn );
132 next if not $isbn;
133 eval{
134 $isbn = $isbn->as_isbn13->as_string;
136 next if $@;
137 $isbn =~ s/-//g;
138 my $social_datas = C4::SocialData::get_data( $isbn );
139 if ( $social_datas ) {
140 push @{ $results{with} }, { biblionumber => $biblionumber, isbn => $isbn, original => $original_isbn };
141 } else {
142 push @{ $results{without} }, { biblionumber => $biblionumber, isbn => $isbn, original => $original_isbn };
145 return \%results;