index.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /* For license terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * This is the main script of the Card Game plugin.
  6. * It is loaded on every page through the inclusion of the plugin in the
  7. * pre_footer region (a mandatory step of the installation).
  8. *
  9. * @author Damien Renou
  10. *
  11. * @package chamilo.plugin.card_game
  12. */
  13. require_once __DIR__.'/../../main/inc/global.inc.php';
  14. // This plugin doesn't work for anonymous users
  15. if (!api_is_anonymous()) {
  16. require_once 'card_game.php';
  17. $cardGame = CardGame::create();
  18. $version = '?v=041';
  19. $interface = 'localhost';
  20. $parsedUrl = parse_url($_SERVER['REQUEST_URI']);
  21. $parsedUrlpath = $parsedUrl['path'];
  22. $pluginPath = api_get_path(WEB_PLUGIN_PATH).'card_game/resources/';
  23. $fh = '<script type="text/javascript" src="'.$pluginPath.'js/cardgame.js'.$version.'" ></script>';
  24. $fh .= '<link href="'.$pluginPath.'css/cardgame.css'.$version.'" rel="stylesheet" type="text/css">';
  25. $fh .= '<div id="cardgamemessage" style="display:none;" >'.$cardGame->get_lang('openDeckCardGame').'</div>';
  26. $fh .= '<div id="cardgameengage" style="display:none;" >'.$cardGame->get_lang('engageDeckCardGame').'</div>';
  27. $fh .= '<div id="cardgameloose" style="display:none;" >'.$cardGame->get_lang('cardgameloose').'</div>';
  28. $fh .= '<div id="linkcardgame" style="display:none;" >'.$pluginPath.'ajax.card.php</div>';
  29. $userId = api_get_user_id();
  30. // Look if the user can still try playing today
  31. $cardGameSession = Session::read('cardgame');
  32. if (!empty($cardGameSession)) {
  33. // If we've already loaded the cardgame in this session, then there's
  34. // a chance we've already played
  35. if (isset($userId)) {
  36. $sqlCount = "SELECT access_date FROM plugin_card_game WHERE user_id = $userId";
  37. $resultCount = Database::query($sqlCount)->rowCount();
  38. if ($resultCount === 0) {
  39. // If there is no database entry for this user, insert one
  40. // without the 'parts' field (because he has not played yet)
  41. // @todo change date call
  42. $sql = "INSERT INTO plugin_card_game (user_id, access_date, pan)
  43. VALUES ($userId, DATE_ADD(CURDATE(), INTERVAL -1 DAY), 1);";
  44. $resultInsert = Database::query($sql);
  45. Session::write('cardgame', 'havedeck');
  46. } else {
  47. // If there is already one or more records in the database,
  48. // get the number of records for today
  49. // @todo change date call
  50. $sqlDate = "SELECT access_date
  51. FROM plugin_card_game
  52. WHERE access_date = CURDATE()
  53. AND user_id = $userId";
  54. $resultDate = Database::query($sqlDate)->rowCount();
  55. if ($resultDate == 0) {
  56. // If there are records, but none for today, set the
  57. // 'cardgame' session variable and add the
  58. // #havedeckcardgame element to the page (it will get
  59. // picked up by JS later on)
  60. Session::write('cardgame', 'havedeck');
  61. $fh .= '<div id="havedeckcardgame" ></div>';
  62. } else {
  63. // If the user already played today, set the session
  64. // 'cardgame' variable to 'done' and do not add
  65. // an #havedeckcardgame element
  66. Session::write('cardgame', 'done');
  67. }
  68. }
  69. }
  70. } else {
  71. Session::write('cardgame', 'havedeck');
  72. $fh .= '<div id="havedeckcardgame" ></div>';
  73. }
  74. $parts = '1';
  75. $pan = '1';
  76. if (isset($userId)) {
  77. try {
  78. $sqlParts = "SELECT parts, pan FROM plugin_card_game WHERE user_id = $userId";
  79. $resultParts = Database::query($sqlParts);
  80. while ($part = Database::fetch_array($resultParts)) {
  81. $parts = $part['parts'];
  82. $pan = $part['pan'];
  83. }
  84. } catch (Exception $e) {
  85. echo 'Exception: ', $e->getMessage(), "\n";
  86. }
  87. }
  88. echo '<div id="memocardgame" style="display:none;" >'.$parts.'</div>';
  89. echo '<div id="pancardgame" style="display:none;" >'.$pan.'</div>';
  90. echo $fh;
  91. }