Przeglądaj źródła

Plugin: Card Game: Fix condition preventing scissors from appearing + add a lot of documentation

Yannick Warnier 6 lat temu
rodzic
commit
de4a69810e

+ 17 - 5
plugin/card_game/index.php

@@ -14,6 +14,7 @@ use ChamiloSession as Session;
  */
 require_once __DIR__.'/../../main/inc/global.inc.php';
 
+// This plugin doesn't work for anonymous users
 if (!api_is_anonymous()) {
     require_once 'card_game.php';
     $cardGame = CardGame::create();
@@ -33,22 +34,28 @@ if (!api_is_anonymous()) {
 
     $fh .= '<div id="linkcardgame" style="display:none;" >'.$pluginPath.'ajax.card.php</div>';
 
-    $user = api_get_user_info();
-    $userId = (int) $user['id'];
+    $userId = api_get_user_id();
 
+    // Look if the user can still try playing today
     $cardGameSession = Session::read('cardgame');
     if (!empty($cardGameSession)) {
+        // If we've already loaded the cardgame in this session, then there's
+        // a chance we've already played
         if (isset($userId)) {
             $sqlCount = "SELECT access_date FROM plugin_card_game WHERE user_id = $userId";
             $resultCount = Database::query($sqlCount)->rowCount();
 
             if ($resultCount === 0) {
+                // If there is no database entry for this user, insert one
+                // without the 'parts' field (because he has not played yet)
                 // @todo change date call
                 $sql = "INSERT INTO plugin_card_game (user_id, access_date, pan) 
                         VALUES ($userId, DATE_ADD(CURDATE(), INTERVAL -1 DAY), 1);";
                 $resultInsert = Database::query($sql);
                 Session::write('cardgame', 'havedeck');
             } else {
+                // If there is already one or more records in the database,
+                // get the number of records for today
                 // @todo change date call
                 $sqlDate = "SELECT access_date 
                           FROM plugin_card_game 
@@ -57,17 +64,22 @@ if (!api_is_anonymous()) {
                 $resultDate = Database::query($sqlDate)->rowCount();
 
                 if ($resultDate == 0) {
+                    // If there are records, but none for today, set the
+                    // 'cardgame' session variable and add the
+                    // #havedeckcardgame element to the page (it will get
+                    // picked up by JS later on)
                     Session::write('cardgame', 'havedeck');
                     $fh .= '<div id="havedeckcardgame" ></div>';
                 } else {
+                    // If the user already played today, set the session
+                    // 'cardgame' variable to 'done' and do not add
+                    // an #havedeckcardgame element
                     Session::write('cardgame', 'done');
                 }
             }
         }
     } else {
-        if ($cardGameSession == 'havedeck') {
-            $fh .= '<div id="havedeckcardgame" ></div>';
-        }
+        $fh .= '<div id="havedeckcardgame" ></div>';
     }
 
     $parts = '';

+ 12 - 2
plugin/card_game/resources/js/cardgame.js

@@ -1,4 +1,10 @@
 /* For license terms, see /license.txt */
+/**
+ * Whenever the cardgame.js file is included into the loaded JavaScript,
+ * search for the user picture in the left menu and add a div
+ * "enjoy-cardgame" to it to show a little drawing cards icon to enable
+ * this plugin
+ */
 $(document).ready(function ($) {
 
   if (!document.getElementById('havedeckcardgame')) {
@@ -63,6 +69,9 @@ function installCardView () {
 
 }
 
+/**
+ * Animate the scissors
+ */
 function animationOpenCardView () {
 
   $('#scissors').addClass('scissorsrightinitfinal')
@@ -168,11 +177,12 @@ function randomOpenCardView () {
 }
 
 /**
- * This function changes the visibility of blocks inside the puzzle area
+ * This function adds the cardgame area block (in invisible state), then
+ * changes the visibility of blocks inside the puzzle area to show them
  */
 function onlyOpenCardView () {
 
-  installCardView()
+  installCardView();
 
   var memocardgame = $('#memocardgame').html()