Parcourir la source

Generate canvas_id upon creating a canvas, not saving it

Unknown il y a 7 ans
Parent
commit
844ff50930
2 fichiers modifiés avec 97 ajouts et 69 suppressions
  1. 32 4
      canvas/js/canvas.js
  2. 65 65
      canvas/php/save-canvas.php

+ 32 - 4
canvas/js/canvas.js

@@ -32,6 +32,31 @@ $(document).ready(function() {
         }
     });
     
+    /* ================================================
+    Generate canvas id
+    ================================================= */
+    
+    // Generate a random string of a specific length to be used as canvas_id
+    if(canvasId === "") {
+        var i = 0;
+        var length = 10;
+        var characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
+        var charactersLength = characters.length;
+        var randomString = "";
+        var minimum = 0;
+        var maximum = charactersLength - 1;
+        
+        for (i = 0; i < length; i++) {
+            randomString += characters[Math.floor(Math.random() * (maximum - minimum + 1)) + minimum];
+        }
+        
+        // Save canvas ID
+        canvasId = randomString;
+        $("input[name='canvas_id']").val(randomString);
+    }
+    
+    console.log(canvasId);
+    
     /* ================================================
     Rearrange fields numerically if 1 column is displayed
     ================================================= */
@@ -1926,6 +1951,8 @@ $(document).ready(function() {
     CLICK ON #EXPORT JSON# form button
     ================================================= */
     
+    // ååå
+    
     /* ----------------------------------------------
     If the user clicks on the SAVE CANVAS button
     ----------------------------------------------- */
@@ -1941,18 +1968,19 @@ $(document).ready(function() {
         var save_canvas_obj = {
             'email_save_canvas': email_save_canvas,
             'name_save_canvas': name_save_canvas,
-            'date_save_canvas': date_save_canvas
+            'date_save_canvas': date_save_canvas,
+            'id_save_canvas': canvasId
         };
         
         var save_canvas = $.param(save_canvas_obj);
-        /*  Post the JSON stringified object to the php file
-        (the php script will save it in a .json file )*/
+        /* Post the JSON stringified object to the php file
+        (the php script will save it in a .json file ) */
         var save_reg_url = "php/save-canvas.php";
         
         $.post(save_reg_url, { save_canvas: save_canvas }, function(data, status) {
             //the returned data is successful, is the $canvas_id
             var canvas_id = data;
-            // send this canvas_id with the next ajax requestedto the php/canvas.php file and use it as the name of the json file to be saved
+            // Send this canvas_id with the next ajax requestedto the php/canvas.php file and use it as the name of the json file to be saved
             
             // Give the user feedback that the canvas has been saved
             if (data !== 400 || data !== 401) {

+ 65 - 65
canvas/php/save-canvas.php

@@ -1,67 +1,67 @@
 <?php
- /* Receives the email of the user, the name of the canvas and the date
-    and stores it in the database with a randomly-generated canvas_id,
-    that is returned.                                                */
- 
- session_start();
- $params = array();
- parse_str($_POST['save_canvas'], $params);
-
- if(!array_key_exists('email_save_canvas', $params) OR
-    !array_key_exists('name_save_canvas', $params) OR
-    !array_key_exists('date_save_canvas', $params)) {
-   echo 400; // Missing parameters
- }
-
- else {
-
-   if(isset($_SESSION['canvas_id'])) {
-     // Canvas already exists. Return canvas_id to overwrite JSON file.
-     echo $_SESSION['canvas_id'];
-   }
-
-   else { // New canvas in the database
-
-     // Retrieve user credentials
-     $email = $params['email_save_canvas'];
-     $canvas_name = $params['name_save_canvas'];
-     $date = $params['date_save_canvas'];
-
-     require_once('../../php/db_utils.php');
-     $conn = db_connect(); // Connect to the database
-
-     // Check if the username already exists
-     if(!($result = mysqli_query($conn, "SELECT name FROM user WHERE username = '$email'"))) {
-       echo 400; // Wrong query
-     }
-     else if(mysqli_num_rows($result) != 1) { // User not registered or duplicated
-       echo 401;
-     }
-     else { // User registered
-       // Save this canvas
-       $canvas_id = generateRandomString();
-       if(!mysqli_query($conn, "INSERT INTO canvas (canvas_id, user_id, canvas_name, canvas_date) VALUES ('$canvas_id', '$email', '$canvas_name', '$date')")) {
-         echo 400; // Wrong query
-         echo " #Wrong query :/ ";
-       }
-       else { // Return canvas_id and save it in the current session
-         $_SESSION['canvas_id'] = $canvas_id;
-         echo $canvas_id;
-       }
-     }
-   }
-}
-
-
-/* Generate a random string of a specific length to be used as canvas_id */
-function generateRandomString($length = 10) {
-    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
-    $charactersLength = strlen($characters);
-    $randomString = '';
-    for ($i = 0; $i < $length; $i++) {
-        $randomString .= $characters[rand(0, $charactersLength - 1)];
+    /* Receives the email of the user, the name of the canvas and the date
+        and stores it in the database with a randomly-generated canvas_id,
+        that is returned.                                                */
+    
+    session_start();
+    $params = array();
+    parse_str($_POST['save_canvas'], $params);
+    
+    if(!array_key_exists('email_save_canvas', $params) OR
+       !array_key_exists('name_save_canvas', $params) OR
+       !array_key_exists('date_save_canvas', $params)) {
+        echo 400; // Missing parameters
     }
-    return $randomString;
-}
-
-?>
+    
+    else {
+        if(isset($_SESSION['canvas_id'])) {
+            // Canvas already exists. Return canvas_id to overwrite JSON file.
+            echo $_SESSION['canvas_id'];
+        }
+        
+        else { // New canvas in the database
+            // Retrieve user credentials
+            $email = $params['email_save_canvas'];
+            $canvas_name = $params['name_save_canvas'];
+            $date = $params['date_save_canvas'];
+            $canvas_id = $params['id_save_canvas'];
+            
+            require_once('../../php/db_utils.php');
+            $conn = db_connect(); // Connect to the database
+            
+            // Check if the username already exists
+            if(!($result = mysqli_query($conn, "SELECT name FROM user WHERE username = '$email'"))) {
+                echo 400; // Wrong query
+            }
+            else if(mysqli_num_rows($result) != 1) { // User not registered or duplicated
+                echo 401;
+            }
+            else { // User registered
+                // Save this canvas
+                // $canvas_id = generateRandomString();
+                
+                if(!mysqli_query($conn, "INSERT INTO canvas (canvas_id, user_id, canvas_name, canvas_date) VALUES ('$canvas_id', '$email', '$canvas_name', '$date')")) {
+                    echo 400; // Wrong query
+                    echo " #Wrong query :/ ";
+                }
+                else { // Return canvas_id and save it in the current session
+                    $_SESSION['canvas_id'] = $canvas_id;
+                    echo $canvas_id;
+                }
+            }
+        }
+    }
+    
+    /*
+    // Generate a random string of a specific length to be used as canvas_id
+    function generateRandomString($length = 10) {
+        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
+        $charactersLength = strlen($characters);
+        $randomString = '';
+        for ($i = 0; $i < $length; $i++) {
+            $randomString .= $characters[rand(0, $charactersLength - 1)];
+        }
+        return $randomString;
+    }
+     */
+?>