Browse Source

Passage du commit en xhr

Mael ETIENNE 3 years ago
parent
commit
743924d18a
3 changed files with 111 additions and 29 deletions
  1. 23
    10
      Controller/Track2004Controller.php
  2. 33
    5
      Resources/public/js/scorm2004.js
  3. 55
    14
      Services/ScormEngine.php

+ 23
- 10
Controller/Track2004Controller.php View File

@@ -19,6 +19,7 @@ use Logipro\Bundle\SCORMBundle\Tools\UnicodeTools;
19 19
 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
20 20
 use Symfony\Component\HttpFoundation\Request;
21 21
 use Symfony\Component\HttpFoundation\Response;
22
+use Symfony\Component\HttpFoundation\JsonResponse;
22 23
 
23 24
 class Track2004Controller extends AbstractController
24 25
 {
@@ -34,6 +35,12 @@ class Track2004Controller extends AbstractController
34 35
     {
35 36
         $em = $this->getDoctrine()->getManager();
36 37
 
38
+        // Traitement de la requete json
39
+        if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) {
40
+            $data = json_decode($request->getContent(), true);
41
+            $request->request->replace(is_array($data) ? $data : array());
42
+        }
43
+
37 44
         // Récupérer la requete et les objets qu'elle contient
38 45
         $data = null;
39 46
         if ($request->isMethod('POST')) {
@@ -44,8 +51,8 @@ class Track2004Controller extends AbstractController
44 51
        //                       1). Récupération des objets du sco                             //
45 52
       //======================================================================================//
46 53
         // On attrape l'objet CMI et ADL à partir de l'url
47
-        $cmiData = $this->getCMIFromRequest();
48
-        $adlData = $this->getADLFromRequest();
54
+        $cmiData = $this->getCMIFromRequest($request);
55
+        $adlData = $this->getADLFromRequest($request);
49 56
 
50 57
         // sin on a ni l'objet cmi et ni l'objet adl alors on a rien à faire
51 58
         if (!$cmiData && !$adlData) {
@@ -365,7 +372,7 @@ class Track2004Controller extends AbstractController
365 372
 
366 373
         $response = $this->getReaderChanges($reader); */
367 374
 
368
-        $response = array();
375
+        $response = array('todo');
369 376
 
370 377
         return $this->json($response);
371 378
     }
@@ -848,8 +855,14 @@ class Track2004Controller extends AbstractController
848 855
             return ;
849 856
         }
850 857
 
858
+        // Traitement de la requete json
859
+		if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) {
860
+			$data = json_decode($request->getContent(), true);
861
+			$request->request->replace(is_array($data) ? $data : array());
862
+		}
863
+
851 864
         //1. Récuppération du type de la requête envoyée par l'API
852
-        $typeURL = $_REQUEST ['request'];
865
+        $typeURL = $_REQUEST['request'];
853 866
 
854 867
         // 2. Récuppération du plannode
855 868
         // Décryptage de la requête
@@ -961,13 +974,13 @@ class Track2004Controller extends AbstractController
961 974
      *
962 975
      * @return NULL|CMIArray
963 976
      */
964
-    protected function getCMIFromRequest()
977
+    protected function getCMIFromRequest(Request $request)
965 978
     {
966
-        if (! isset($_REQUEST['cmi'])) {
979
+        if (empty($request->request->get('cmi'))) {
967 980
             return null;
968 981
         }
969 982
 
970
-        $cmiData = $_REQUEST['cmi'];
983
+        $cmiData = $request->request->get('cmi');
971 984
         if (! is_array($cmiData)) {
972 985
             return null;
973 986
         }
@@ -981,13 +994,13 @@ class Track2004Controller extends AbstractController
981 994
      *
982 995
      * @return NULL|CMIArray
983 996
      */
984
-    protected function getADLFromRequest()
997
+    protected function getADLFromRequest(Request $request)
985 998
     {
986
-        if (!isset($_REQUEST['adl'])) {
999
+        if (empty($request->request->get('adl'))) {
987 1000
             return null;
988 1001
         }
989 1002
 
990
-        $adlData = $_REQUEST['adl'];
1003
+        $adlData = $request->request->get('adl');
991 1004
         if (!is_array($adlData)) {
992 1005
             return null;
993 1006
         }

+ 33
- 5
Resources/public/js/scorm2004.js View File

@@ -424,7 +424,21 @@ ScormApi.prototype.processRequest = (function (){
424 424
 			redirection.item = item;
425 425
 		}
426 426
 
427
-		$.post(this.ajaxURLRedirection, redirection, success, 'text');
427
+		//$.post(this.ajaxURLRedirection, redirection, success, 'text');
428
+
429
+		var xhr = new XMLHttpRequest();
430
+		xhr.open('POST', this.ajaxURLRedirection, false);
431
+		xhr.setRequestHeader('Content-Type', 'application/json');
432
+		xhr.onload = function() {
433
+   			if (xhr.status === 200) {
434
+				this.printConsoleLog('Redirection OK');
435
+				this.success();
436
+    		}
437
+    		else if (xhr.status !== 200) {
438
+        		this.printConsoleLog('Redirection failed.  Returned status of ' + xhr.status);
439
+    		}
440
+		};
441
+		xhr.send(JSON.stringify(redirection));
428 442
 	}
429 443
 });
430 444
 
@@ -514,7 +528,7 @@ ScormApi.prototype.LMSGetValue = (function(param){
514 528
 
515 529
 	} catch (err) {
516 530
 		this.printConsoleLog("erreur dans la fonction getValue : dans myGet ");
517
-		this.logDebug(param + ' >> ???');
531
+		this.printConsoleLog(param + ' >> ???');
518 532
 		this.setError(err.code);
519 533
 
520 534
 		return "";
@@ -694,14 +708,28 @@ ScormApi.prototype.LMSCommit = (function() {
694 708
 	this.printConsoleLog("Les données à commiter... ");
695 709
 	this.printConsoleLog(commit);
696 710
 
697
-	$.ajax({
711
+	/* $.ajax({
698 712
 		  type: 'POST',
699 713
 		  url: this.ajaxURL,
700 714
 		  data: commit,
701 715
 		  async:false,
702 716
 		  success: commitCallback,
703 717
 		  dataType: 'json'
704
-		});
718
+		}); */
719
+
720
+	var xhr = new XMLHttpRequest();
721
+	xhr.open('POST', this.ajaxURL, false);
722
+	xhr.setRequestHeader('Content-Type', 'application/json');
723
+	xhr.onload = function() {
724
+		if (xhr.status === 200) {
725
+			this.printConsoleLog('Commit OK');
726
+			this.commitCallback();
727
+		}
728
+		else if (xhr.status !== 200) {
729
+			this.printConsoleLog('Commit failed.  Returned status of ' + xhr.status);
730
+		}
731
+	};
732
+	xhr.send(JSON.stringify(commit));
705 733
 
706 734
 	this.setError(gNoError);
707 735
 
@@ -1853,7 +1881,7 @@ ScormApi.prototype.printConsoleLog = (function(message) {
1853 1881
 });
1854 1882
 
1855 1883
 
1856
-ScormApi.prototype.logDebug = (function(str) {
1884
+ScormApi.prototype.printConsoleLog = (function(str) {
1857 1885
 	if (this.debugMode) {
1858 1886
 		if (typeof console !== 'object') {
1859 1887
 			return;

+ 55
- 14
Services/ScormEngine.php View File

@@ -513,6 +513,18 @@ class ScormEngine implements ContainerAwareInterface
513 513
      */
514 514
     public function createLearner(string $learnerKey, string $familyName, string $givenName)
515 515
     {
516
+        // Création de la structure de la réponse
517
+        $response = array(
518
+            'code' => 400,
519
+            'isAlreadyExisting' => false,
520
+            'error' => '',
521
+            'learner' => array(
522
+                'learnerKey' => '',
523
+                'familyName' => '',
524
+                'givenName' => ''
525
+            )
526
+        );
527
+
516 528
         if (!empty($learnerKey)) {
517 529
             $learner = $this->requireLearner($learnerKey);
518 530
 
@@ -526,12 +538,19 @@ class ScormEngine implements ContainerAwareInterface
526 538
 
527 539
                 $em->persist($learner);
528 540
                 $em->flush();
529
-
530
-                return $learner->getLearnerId();
541
+            } else {
542
+                $response['isAlreadyExisting'] = true;
531 543
             }
544
+
545
+            $response['code'] = 200;
546
+            $response['learner']['learnerKey'] = $learner->getLearnerKey();
547
+            $response['learner']['familyName'] = $learner->getFamilyName();
548
+            $response['learner']['givenName'] = $learner->getGivenName();
549
+        } else {
550
+            $response['error'] = 'La clef d\'identification est necessaire';
532 551
         }
533 552
         
534
-        return null;
553
+        return $response;
535 554
     }
536 555
 
537 556
     /**
@@ -618,27 +637,49 @@ class ScormEngine implements ContainerAwareInterface
618 637
      */
619 638
     public function createRegistration(string $registrationKey, string $courseKey, string $learnerKey)
620 639
     {
640
+        // Création de la structure de la réponse
641
+        $response = array(
642
+            'code' => 400,
643
+            'isAlreadyExisting' => false,
644
+            'error' => '',
645
+            'registration' => array(
646
+                'registrationKey' => '',
647
+                'courseKey' => '',
648
+                'learnerKey' => ''
649
+            )
650
+        );
651
+
621 652
         if (!empty($registrationKey)) {
622 653
             $registration = $this->requireRegistration($registrationKey);
623 654
             $course = $this->requireCourse($courseKey);
624 655
             $learner = $this->requireLearner($learnerKey);
625 656
 
626
-            if (empty($registration) && $course && $learner) {
627
-                $em = $this->getDoctrine()->getManager();
628
-
629
-                $registration = new Registration();
630
-                $registration->setRegistrationKey($registrationKey);
631
-                $registration->setCourse($course);
632
-                $registration->setLearner($learner);
657
+            if ($course && $learner) {
658
+                if (empty($registration)) {
659
+                    $em = $this->getDoctrine()->getManager();
633 660
 
634
-                $em->persist($registration);
635
-                $em->flush();
661
+                    $registration = new Registration();
662
+                    $registration->setRegistrationKey($registrationKey);
663
+                    $registration->setCourse($course);
664
+                    $registration->setLearner($learner);
636 665
 
637
-                return $registration->getRegistrationKey();
666
+                    $em->persist($registration);
667
+                    $em->flush();
668
+                } else {
669
+                    $response['isAlreadyExisting'] = true;
670
+                }
671
+                $response['code'] = 200;
672
+                $response['registration']['registrationKey'] = $registration->getRegistrationKey();
673
+                $response['registration']['courseKey'] = $registration->getCourse();
674
+                $response['registration']['learnerKey'] = $registration->getLearner();
675
+            } else {
676
+                $response['error'] = 'Le cours et l\'apprenant doivent exister';
638 677
             }
678
+        } else {
679
+            $response['error'] = 'La clef d\'identification est necessaire';
639 680
         }
640 681
         
641
-        return null;
682
+        return $response;
642 683
     }
643 684
 
644 685
     /**

Loading…
Cancel
Save