Merge branch 'bug_8931' into 3.12-master
[koha.git] / C4 / SocialData.pm
1 package C4::SocialData;
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
13 #
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
17
18 use Modern::Perl;
19
20 use C4::Context;
21 use Business::ISBN;
22 use C4::Koha;
23
24 =head1 NAME
25
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.
29
30 =head1 SYNOPSIS
31
32 use C4::SocialData;
33
34 =head1 DESCRIPTION
35
36 The functions in this module deal with social datas
37
38 =head1 FUNCTIONS
39
40 =head2 get_data
41
42 Get social data from a biblio
43
44 params:
45   $isbn = isbn of the biblio (it must be the same in your database, isbn given to babelio)
46
47 returns:
48   this function returns an hashref with keys
49
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
58
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;
65
66     return $results;
67 }
68
69 =head2 update_data
70
71 Update Social data
72
73 params:
74   $url = url containing csv file with data
75
76 data separator : ; (semicolon)
77 data order : isbn ; active ; critics number , critics pro number ; quotations number ; videos number ; average score ; scores number
78
79 =cut
80
81 sub update_data {
82     my ( $output_filepath ) = @_;
83
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     } );
90
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
101             );
102         };
103         if ( $@ ) {
104             warn "Can't insert $isbn ($@)";
105         } else {
106             $i++;
107         }
108     }
109     say "$i data insered or updated";
110 }
111
112 =head2 get_report
113
114 Get social data report
115
116 =cut
117
118 sub get_report {
119     my $dbh = C4::Context->dbh;
120
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;
135         };
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 };
143         }
144     }
145     return \%results;
146 }
147
148 1;