File: | C4/SocialData.pm |
Coverage: | 19.8% |
line | stmt | bran | cond | sub | time | code |
---|---|---|---|---|---|---|
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 | 1 1 1 | 276 1 11 | use Modern::Perl; | |||
19 | ||||||
20 | 1 1 1 | 131 2 10 | use C4::Context; | |||
21 | 1 1 1 | 4 30 56 | use Business::ISBN; | |||
22 | 1 1 1 | 5 2 1104 | use C4::Koha; | |||
23 | ||||||
24 - 57 | =head1 NAME C4::SocialData - Koha functions for dealing with social datas For now used by babeltheque, a french company providing, for books, comments, upload of videos, scoring (star)... the social_data table could be used and improved by other provides. =head1 SYNOPSIS use C4::SocialData; =head1 DESCRIPTION The functions in this module deal with social datas =head1 FUNCTIONS =head2 get_data Get social data from a biblio params: $isbn = isbn of the biblio (it must be the same in your database, isbn given to babelio) returns: this function returns an hashref with keys isbn = isbn num_critics = number of critics num_critics_pro = number of profesionnal critics num_quotations = number of quotations num_videos = number of videos score_avg = average score num_scores = number of score =cut | |||||
58 | ||||||
59 | sub get_data { | |||||
60 | 0 | my ( $isbn ) = @_; | ||||
61 | 0 | my $dbh = C4::Context->dbh; | ||||
62 | 0 | my $sth = $dbh->prepare( qq{SELECT * FROM social_data WHERE isbn = ? LIMIT 1} ); | ||||
63 | 0 | $sth->execute( $isbn ); | ||||
64 | 0 | my $results = $sth->fetchrow_hashref; | ||||
65 | ||||||
66 | 0 | return $results; | ||||
67 | } | |||||
68 | ||||||
69 - 79 | =head2 update_data Update Social data params: $url = url containing csv file with data data separator : ; (semicolon) data order : isbn ; active ; critics number , critics pro number ; quotations number ; videos number ; average score ; scores number =cut | |||||
80 | ||||||
81 | sub update_data { | |||||
82 | 0 | my ( $output_filepath ) = @_; | ||||
83 | ||||||
84 | 0 | my $dbh = C4::Context->dbh; | ||||
85 | 0 | 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 | 0 | open my $file, '<', $output_filepath or die "File $output_filepath can not be read"; | ||||
92 | 0 | my $sep = qq{;}; | ||||
93 | 0 | my $i = 0; | ||||
94 | 0 | my $unknown = 0; | ||||
95 | 0 | while ( my $line = <$file> ) { | ||||
96 | 0 | my ( $isbn, $active, $num_critics, $num_critics_pro, $num_quotations, $num_videos, $score_avg, $num_scores ) = split $sep, $line; | ||||
97 | 0 | next if not $active; | ||||
98 | 0 | eval { | ||||
99 | 0 | $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 | 0 | if ( $@ ) { | ||||
104 | 0 | warn "Can't insert $isbn ($@)"; | ||||
105 | } else { | |||||
106 | 0 | $i++; | ||||
107 | } | |||||
108 | } | |||||
109 | 0 | say "$i data insered or updated"; | ||||
110 | } | |||||
111 | ||||||
112 - 116 | =head2 get_report Get social data report =cut | |||||
117 | ||||||
118 | sub get_report { | |||||
119 | 0 | my $dbh = C4::Context->dbh; | ||||
120 | ||||||
121 | 0 | my $sth = $dbh->prepare( qq{ | ||||
122 | SELECT biblionumber, isbn FROM biblioitems | |||||
123 | } ); | |||||
124 | 0 | $sth->execute; | ||||
125 | 0 | my %results; | ||||
126 | 0 | while ( my ( $biblionumber, $isbn ) = $sth->fetchrow() ) { | ||||
127 | 0 0 | push @{ $results{no_isbn} }, { biblionumber => $biblionumber } and next if not $isbn; | ||||
128 | 0 | my $original_isbn = $isbn; | ||||
129 | 0 | $isbn =~ s/^\s*(\S*)\s*$/$1/; | ||||
130 | 0 | $isbn = GetNormalizedISBN( $isbn, undef, undef ); | ||||
131 | 0 | $isbn = Business::ISBN->new( $isbn ); | ||||
132 | 0 | next if not $isbn; | ||||
133 | 0 | eval{ | ||||
134 | 0 | $isbn = $isbn->as_isbn13->as_string; | ||||
135 | }; | |||||
136 | 0 | next if $@; | ||||
137 | 0 | $isbn =~ s/-//g; | ||||
138 | 0 | my $social_datas = C4::SocialData::get_data( $isbn ); | ||||
139 | 0 | if ( $social_datas ) { | ||||
140 | 0 0 | push @{ $results{with} }, { biblionumber => $biblionumber, isbn => $isbn, original => $original_isbn }; | ||||
141 | } else { | |||||
142 | 0 0 | push @{ $results{without} }, { biblionumber => $biblionumber, isbn => $isbn, original => $original_isbn }; | ||||
143 | } | |||||
144 | } | |||||
145 | 0 | return \%results; | ||||
146 | } | |||||
147 | ||||||
148 | 1; |