Fixed a problem with double indexed elements in the GetValue method
authorcsantossaenz <csantossaenz>
Tue, 19 Dec 2006 08:37:44 +0000 (08:37 +0000)
committercsantossaenz <csantossaenz>
Tue, 19 Dec 2006 08:37:44 +0000 (08:37 +0000)
mod/scorm/datamodels/scorm_13.js.php

index 70c130a..6967417 100644 (file)
@@ -268,20 +268,56 @@ function SCORMapi1_3() {
     function GetValue (element) {
         errorCode = "0";
         diagnostic = "";
-        if ((Initialized) && (!Terminated)) {
+        if ((Initialized) && (!Terminated)) {//si está inicializado y no terminado
             if (element !="") {
                 expression = new RegExp(CMIIndex,'g');
-                elementmodel = element.replace(expression,'.n.');
+                elementmodel = element.replace(expression,'.n.');//creamos su elementmodel
+
+                               if (element=='cmi.interactions.0.objectives.0.id'){
+                                       alert("Estamos aquí, con el elemntmodel "+elementmodel);
+                               }
+                               
                 if ((typeof eval('datamodel["'+elementmodel+'"]')) != "undefined") {
-                    if (eval('datamodel["'+elementmodel+'"].mod') != 'w') {
+                    if (eval('datamodel["'+elementmodel+'"].mod') != 'w') {//compruebo que está definido el modelo
+                                       //y compruebo que no es de solo escritura
+                                               if (element=='cmi.interactions.0.objectives.0.id'){
+                                                       alert("Entro");
+                                               }
                         element = element.replace(/\.(\d+)\./, ".N$1.");
+                                               element = element.replace(/\.(\d+)\./, ".N$1.");
+
                         elementIndexes = element.split('.');
                         subelement = element.substr(0,3);
                         i = 1;
+                                               
+                                               if (elementmodel=='cmi.interactions.n.objectives.n.id'){
+                                                       alert ("el element vale "+element+"y el subelement vale"+subelement);
+                                               }
+                                               
                         while ((i < elementIndexes.length) && (typeof eval(subelement) != "undefined")) {
                             subelement += '.'+elementIndexes[i++];
+       
+                                                       if (elementmodel=='cmi.interactions.n.objectives.n.id'){
+                                                               alert ("el element vale "+element+"y el subelement vale"+subelement);
+                                                                       if((typeof eval(subelement) != "undefined")){
+                                                                               alert("está definido");
+                                                                       }
+                                                                       else {
+                                                                               alert("no definido");
+                                                                       }
+
+                                                       }
+                                               
                         }
+                                               if (elementmodel=='cmi.interactions.n.objectives.n.id'){
+                                                               alert ("Tras el bucle el element vale "+element+"y el subelement vale"+subelement);
+                                                       }
                         if (subelement == element) {
+
+                                                       if (elementmodel=='cmi.interactions.n.objectives.n.id'){
+                                                               alert ("Tras el bucle, si element y sub son iguales el element vale "+element+"y el subelement vale"+subelement);
+                                                       }
+
                             if ((typeof eval(subelement) != "undefined") && (eval(subelement) != null)) {
                                 errorCode = "0";
                                 <?php 
@@ -294,6 +330,10 @@ function SCORMapi1_3() {
                                 errorCode = "403";
                             }
                         } else {
+
+                                                       if (elementmodel=='cmi.interactions.n.objectives.n.id'){
+                                                               alert("element y subelement no son iguales");
+                                                       }
                             errorCode = "301";
                         }
                     } else {
@@ -359,50 +399,75 @@ function SCORMapi1_3() {
     function SetValue (element,value) {
         errorCode = "0";
         diagnostic = "";
-        if ((Initialized) && (!Terminated)) {
-            if (element != "") {
+               if (element== 'cmi.interactions.0.learner_response' && value=='Dependency on cmi.interaction.n.type not established'){
+                       //alert("entramos");
+                       errorCode="408";
+               }
+        if ((Initialized) && (!Terminated)) {//Si está inicializado y no terminado
+            if (element != "") {//Si hay un elemento
                 expression = new RegExp(CMIIndex,'g');
-                elementmodel = element.replace(expression,'.n.');
-                if ((typeof eval('datamodel["'+elementmodel+'"]')) != "undefined") {
-                    if (eval('datamodel["'+elementmodel+'"].mod') != 'r') {
-                        expression = new RegExp(eval('datamodel["'+elementmodel+'"].format'));
+                elementmodel = element.replace(expression,'.n.');//cambio los números por n
+                if ((typeof eval('datamodel["'+elementmodel+'"]')) != "undefined") {//miro si elementmodel definido
+                    if (eval('datamodel["'+elementmodel+'"].mod') != 'r') {//si no es de sólo lectura
+                        expression = new RegExp(eval('datamodel["'+elementmodel+'"].format'));//ajusto a expReg
                         value = value+'';
                         matches = value.match(expression);
                         if ((matches != null) && ((matches.join('').length > 0) || (value.length == 0))) {
+                                               //si el valor a asignar se ajusta a la expresión regular
                             //Create dynamic data model element
                             if (element != elementmodel) {
-                                elementIndexes = element.split('.');
+                                elementIndexes = element.split('.');//divido el elemento en trozos
                                 subelement = 'cmi';
                                 parentelement = 'cmi';
-                                for (i=1;(i < elementIndexes.length-1) && (errorCode=="0");i++) {
+                                for (i=1;(i < elementIndexes.length-1) && (errorCode=="0");i++) {//recorro trozos
                                     elementIndex = elementIndexes[i];
-                                    if (elementIndexes[i+1].match(/^\d+$/)) {
-                                        if ((parseInt(elementIndexes[i+1]) > 0) && (elementIndexes[i+1].charAt(0) == 0)) {
+                                    if (elementIndexes[i+1].match(/^\d+$/)) {//si el trozo es un dígito (índice)
+                                        if ((parseInt(elementIndexes[i+1]) > 0) && (elementIndexes[i+1].charAt(0) == 0)) {//si no es un número da error
                                             // Index has a leading 0 (zero), this is not a number
                                             errorCode = "351";
                                         }
                                         parentelement = subelement+'.'+elementIndex;
                                         if (elementIndexes[i+1] > eval(parentelement+'._count')) {
+                                                                               //si el número (índice) es mayor que el count, se desborda
                                             errorCode = "351";
                                             diagnostic = "Data Model Element Collection Set Out Of Order";
                                         }
                                         subelement = subelement.concat('.'+elementIndex+'.N'+elementIndexes[i+1]);
                                         i++;
+                                                                               //formo la cadena y aumento el índice para saltar
                                         if (((typeof eval(subelement)) == "undefined") && (i < elementIndexes.length-2)) {
+                                                                               //si el elemento no está definido, salta
                                             errorCode="408";
                                         }
-                                    } else {
+                                    } else {//si no es un dígito, sólo lo concateno a lo que había
                                         subelement = subelement.concat('.'+elementIndex);
                                     }
                                 }
-                                if (errorCode == "0") {
+                                if (errorCode == "0") {//si no se produce ningún error en los índices
+                                                                       //if (element=='cmi.interactions.0.learner_response'){
+                                                                               //alert ("Ha llegado con el subelement "+subelement+" y el elementIndexes //"+elementIndexes[elementIndexes.length-1]); 
+                                                                       //}
                                     element = subelement.concat('.'+elementIndexes[elementIndexes.length-1]);
                                     elemlen = element.length;
+                                                                       //if (element=='cmi.interactions.N0.learner_response'){
+                                                                               //alert ("Ha llegado2");
+                                                                       //}
+                                                                       //le añado la terminación (el learner response)
                                     if (((typeof eval(subelement)) == "undefined") && (errorCode == "0")) {
+                                                                       //si el cmi.interactions.N0 no está definido
+                                                                               //if (element=='cmi.interactions.N0.learner_response'){
+                                                                                 //            alert ("Ha llegado3");
+                                                                               //}
+
+                                                                               //es porque es un cmi.objectives
                                         parentmodel = 'cmi.objectives';
                                         maxmodel = 'cmi.objectives.Nxxx.id';
                                         if (subelement.substr(0,parentmodel.length) == parentmodel) {
+                                                                               //si el subelement es un cmi.objectives
                                              if ((elemlen <= maxmodel.length) && (element.substr(elemlen-2) == 'id') && (errorCode=="0")) { 
+                                                                                        /*if (element=='cmi.interactions.N0.learner_response'){
+                                                                                             alert ("Ha llegado32");
+                                                                                       }*/
                                                 //This is a parentmodel.n.id element
                                                 if (!duplicatedID(parentmodel,value)) {
                                                     if (elementIndexes[elementIndexes.length-2] == eval(parentmodel+'._count')) {
@@ -424,6 +489,10 @@ function SCORMapi1_3() {
                                                     diagnostic = "Data Model Element ID Already Exists";
                                                 }
                                             } else {
+                                                                                       
+                                                                                       /*if (element=='cmi.interactions.N0.learner_response'){
+                                                                                             alert ("Ha llegado33");
+                                                                               }*/
                                                 if (typeof eval(subelement) == "undefined") {
                                                     errorCode="408";
                                                 } else {
@@ -434,15 +503,26 @@ function SCORMapi1_3() {
                                                 }
                                             }
                                         } else {
+                                                                                       //si no es un cmi.objectives es porque hay que construir el interactions
+                                                                                       /*if (element=='cmi.interactions.N0.learner_response'){
+                                                                                             alert ("Ha llegado4");
+                                                                                       }*/
                                             parentmodel = 'cmi.interactions';
                                             maxmodel = 'cmi.interactions.Nxxx.id';
+                                                                                       //construyo el interactions
                                             if (subelement.substr(0,parentmodel.length) == parentmodel) {
+                                                                                       //si el subelement es un interactions
                                                 if ((elemlen <= maxmodel.length) && (element.substr(elemlen-2) == 'id') && (errorCode=="0")) { 
+                                                                                                       //si es un interactions.id
                                                     //This is a parentmodel.n.id element
                                                     if (!duplicatedID(parentmodel,value)) {
+                                                                                                       //si no está duplicado, es porque es uno nuevo
                                                         if (elementIndexes[elementIndexes.length-2] == eval(parentmodel+'._count')) {
+                                                                                                               //si es el ultimo elemento que hay hasta ahora
                                                             eval(parentmodel+'._count++;');
+                                                                                                                       //aumentamos el contador que indica el númElem
                                                             eval(subelement+' = new Object();');
+                                                                                                                       //creamos el nuevo objeto, el objectives y el corr_resp
                                                             subobject = eval(subelement);
                                                             subobject.objectives = new Object();
                                                             subobject.objectives._count = 0;
@@ -452,10 +532,12 @@ function SCORMapi1_3() {
                                                             
                                                         } 
                                                     } else {
+                                                                                                       //si esta duplicado da un error
                                                         errorCode="351";
                                                         diagnostic = "Data Model Element ID Already Exists";
                                                     }
                                                 } else {
+                                                                                               //si no es un interactions y vemos que no esta definido dicho //elemento
                                                     if (typeof eval(subelement) == "undefined") {
                                                         errorCode="408";
                                                     } else {
@@ -464,10 +546,15 @@ function SCORMapi1_3() {
                                                             subobject = eval(subelement);
                                                            //subobject.correct_responses = new Object();
                                                            //subobject.correct_responses._count = 0;
+                                                                                                                 subobject.learner_response = new Object();
                                                         } 
                                                     }
                                                 }
-                                            } else { 
+                                            } else {
+                                                                                       /*if (element=='cmi.interactions.N0.learner_response'){
+                                                                                             alert ("Ha llegado38");
+                                                                                       }*/
+                                                                                       //si no es ni objectives ni interactions lo construyo
                                                 if (errorCode == "0") {
                                                     if (elementIndexes[elementIndexes.length-2] == eval(parentelement+'._count')) {
                                                         eval(parentelement+'._count++;');
@@ -477,20 +564,45 @@ function SCORMapi1_3() {
                                             }
                                         }
                                      } else {
+
+                                                                        //si ya está definido el subelement
+
+                                                                       /* if (element=='cmi.interactions.N0.learner_response'){//Aqui entra
+                                                                                             alert ("Ha llegado5");
+                                                                               }*/
                                          parentmodel = 'cmi.objectives';
                                          maxmodel = 'cmi.objectives.Nxxx.id';
                                          if (subelement.substr(0,parentmodel.length) == parentmodel) {
-                                             if ((elemlen <= maxmodel.length) && (element.substr(elemlen-2) == 'id') && (errorCode=="0")) { 
+                                                                                                       
+                                                                                                       /*if (element=='cmi.interactions.N0.learner_response'){
+                                                                                             alert ("Ha llegado6");
+                                                                                                       }*/
+
+                                                                                       //si es un objectives
+                                             if ((elemlen <= maxmodel.length) && (element.substr(elemlen-2) == 'id') && (errorCode=="0")) {
+                                                                                               //si es id y es distinto de value da fallo porque no se cambia
                                                  if (eval(element) != value) {
                                                      errorCode = "351";
                                                      diagnostic = "Write Once Violation";
                                                  }
                                              }
                                          } else {
+                                                                                
+                                                                                                       /*if (element=='cmi.interactions.N0.learner_response'){//llega 
+                                                                                             alert ("Ha llegado63");
+                                                                                                       }*/
+
+                                                                                       //si no es objectives miro si es interactions
                                              parentmodel = 'cmi.interactions';
                                              maxmodel = 'cmi.interactions.Nxxx.id';
                                              if (subelement.substr(0,parentmodel.length) == parentmodel) {
                                                  if ((elemlen <= maxmodel.length) && (element.substr(elemlen-2) == 'id') && (errorCode=="0")) { 
+
+                                                                                                               /*if (element=='cmi.interactions.N0.learner_response'){
+                                                                                                                       alert ("Ha llegado64");
+                                                                                                               }*/
+
+                                                                                                //compruebo si intentan modificar su id
                                                      if (eval(element) != value) {
                                                          errorCode = "351";
                                                          diagnostic = "Write Once Violation";
@@ -503,6 +615,11 @@ function SCORMapi1_3() {
                             }
                             //Store data
                             if (errorCode == "0") {
+                                                       //si no ha habido errores                                                               
+                                                               /*if (element=='cmi.interactions.N0.learner_response'){
+                                                                                             alert ("Ha llegado9");
+                                                                               }*/
+
                                 if ((typeof eval('datamodel["'+elementmodel+'"].range')) != "undefined") {
                                     range = eval('datamodel["'+elementmodel+'"].range');
                                     ranges = range.split('#');
@@ -548,7 +665,10 @@ function SCORMapi1_3() {
             } else {
                 errorCode = "351";
             }
-        } else {
+        }
+               //Si está inicializado y no terminado termina aquí 
+
+               else {
             if (Terminated) {
                 errorCode = "133";
             } else {