Library.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /* GNU FM -- a free network service for sharing your music listening habits
  3. Copyright (C) 2013 Free Software Foundation, Inc
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Affero General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. require_once($install_path . '/database.php');
  16. require_once($install_path . '/data/sanitize.php');
  17. /**
  18. * Provides access to functions involving a user's library.
  19. */
  20. class Library {
  21. /**
  22. * Remove a scrobble.
  23. *
  24. * @param int userid User ID.
  25. * @param int timestamp Timestamp in Unix time.
  26. * @param string artist Artist name.
  27. * @param string track Track name.
  28. * @return bool True if scrobble was removed, False if not.
  29. */
  30. function removeScrobble($userid, $timestamp, $artist, $track) {
  31. global $adodb;
  32. $delete_query = 'DELETE FROM Scrobbles WHERE userid=? AND time=? AND artist=? AND track=?';
  33. $delete_params = array((int)$userid, (int)$timestamp, $artist, $track);
  34. // TODO Should we have a db trigger for this?
  35. $update_stats_query = 'UPDATE User_Stats SET scrobble_count=scrobble_count-1 WHERE userid=?';
  36. $update_stats_params = array((int)$userid);
  37. $adodb->StartTrans();
  38. try {
  39. $adodb->Execute($delete_query, $delete_params);
  40. $delete_count = $adodb->Affected_Rows();
  41. if($delete_count) {
  42. $adodb->Execute($update_stats_query, $update_stats_params);
  43. }
  44. } catch (Exception $e) {
  45. $adodb->FailTrans();
  46. $adodb->CompleteTrans();
  47. reportError($e->getMessage(), $e->getTraceAsString());
  48. return False;
  49. }
  50. $adodb->CompleteTrans();
  51. return (bool)$delete_count;
  52. }
  53. }