Ver Mensaje Individual
  #10 (permalink)  
Antiguo 13/06/2014, 22:13
Avatar de Italico76
Italico76
 
Fecha de Ingreso: abril-2007
Mensajes: 3.303
Antigüedad: 17 años, 1 mes
Puntos: 292
Respuesta: Consulta imprimir Datos

Cita:
Iniciado por hhs Ver Mensaje
Italico, perdon se me fue mal la redaccion lo ultimo eran recomendaciones pero no para ti. De echo ya ayudaste mucho con el ejemplo
Si yo se amigo @hhs ... que tu eres de los que quieren que los usuarios hagan el maximo esfuerzo.

Publico de todas formas la implementacion de tu idea de mejora:

Código PHP:
Ver original
  1. <?php
  2. /*
  3.     @author: Pablo Bozzolo (italico76) 
  4. */
  5. Class PreguntaCuestionario implements Countable, Iterator
  6. {
  7.     private $_pregunta = NULL;
  8.     private $_respuestas  = [];
  9.     private $_respuesta_correcta = null;
  10.  
  11.    
  12.     public function __construct($pregunta)
  13.     {
  14.         // texto de la pregunta
  15.         $this->_pregunta=$pregunta;
  16.     }
  17.        
  18.     public function getPregunta()
  19.     {
  20.         return $this->_pregunta;
  21.     }
  22.    
  23.     // alias de getPregunta
  24.     public function getNombre()
  25.     {
  26.         return $this->_pregunta;
  27.     }
  28.    
  29.     // una posible repuesta
  30.     public function addRespuesta($respuesta)
  31.     {
  32.         $this->_respuestas[] = $respuesta;
  33.         return $this;
  34.     }
  35.    
  36.     // posibles repuestas
  37.     public function addRespuestas(array $respuestas)
  38.     {
  39.         foreach ($respuestas as $respuesta)
  40.             $this->_respuestas[] = $respuesta; 
  41.            
  42.         return $this;
  43.     }
  44.        
  45.     // la respuesta considerada 'correcta', si la hay
  46.     // posicion como rango {1...N}
  47.     public function setRespuestaCorrecta($posicion)
  48.     {
  49.         $this->_respuesta_correcta = $this->_respuestas[$posicion-1];
  50.         return $this;
  51.     }
  52.    
  53.     public function getRespuestaCorrecta()
  54.     {
  55.         return $this->_respuesta_correcta;
  56.     }
  57.  
  58.     // desordena respuestas
  59.     public  function desordenar()
  60.     {
  61.         shuffle($this->_respuestas);
  62.         return $this;
  63.     }
  64.    
  65.     /* Iterator */
  66.     function rewind() {        
  67.         reset($this->_respuestas);
  68.     }
  69.  
  70.     function current()
  71.     {
  72.         return current($this->_respuestas);
  73.     }
  74.  
  75.     function key()
  76.     {        
  77.         return key($this->_respuestas);
  78.     }
  79.  
  80.     function next()
  81.     {
  82.         next($this->_respuestas);        
  83.     }
  84.  
  85.     function valid()
  86.     {  
  87.         return isset($this->_respuestas[$this->key()]);
  88.     }
  89.    
  90.     /* Countable */
  91.     function count()
  92.     {  
  93.         return count($this->_respuestas);
  94.     }  
  95.    
  96. }
  97.  
  98.  
  99. Class Cuestionario implements Countable, Iterator
  100. {
  101.     private $_nombre = NULL;
  102.     private $_preguntas= array();
  103.     private $_mode = NULL;
  104.        
  105.     /*
  106.         @param nombre del cuestionario     
  107.         @return void   
  108.     */ 
  109.     public function __construct($nombre)
  110.     {          
  111.         $this->_nombre=$nombre;
  112.     }
  113.    
  114.     public function getNombre()
  115.     {
  116.         return $this->_nombre;
  117.     }
  118.  
  119.     public function addPregunta(PreguntaCuestionario $pregunta)
  120.     {  
  121.         $this->_preguntas[]= $pregunta;    
  122.         return $this;
  123.     }
  124.  
  125.     // desordena preguntas
  126.     public  function desordenar($tambien_respuestas=true)
  127.     {
  128.         shuffle($this->_preguntas);
  129.         if ($tambien_respuestas)
  130.             foreach ($this->_preguntas as $pregunta)
  131.                 $pregunta->desordenar();
  132.        
  133.         return $this;
  134.     }
  135.  
  136.     /* Iterator */
  137.     function rewind() {        
  138.         reset($this->_preguntas);
  139.     }
  140.  
  141.     function current()
  142.     {
  143.         return current($this->_preguntas);
  144.     }
  145.  
  146.     function key()
  147.     {        
  148.         return key($this->_preguntas);
  149.     }
  150.  
  151.     function next()
  152.     {
  153.         next($this->_preguntas);        
  154.     }
  155.  
  156.     function valid()
  157.     {  
  158.         return isset($this->_preguntas[$this->key()]);
  159.     }
  160.    
  161.     /* Countable */
  162.     function count()
  163.     {  
  164.         return count($this->_preguntas);
  165.     }
  166.    
  167.        
  168.     // para consola
  169.     function __toString()
  170.     {
  171.         $out = "Cuestionario: {$this->_nombre}\n\n";
  172.        
  173.         foreach ($this->_preguntas as $pregunta)
  174.         {  
  175.             $out .= $pregunta->getNombre()."\n\n";
  176.    
  177.             foreach ($pregunta as $option)         
  178.                 $out .= "( ) $option\n";
  179.        
  180.                 $out .= "\n(RTA: ".$pregunta->getRespuestaCorrecta().")\n\n";
  181.             $out .= "\n";  
  182.         }
  183.        
  184.         return $out;   
  185.     }
  186.  
  187. }
  188.  
  189. // creo Cuestionario:
  190.  
  191. $pregs = new Cuestionario("America");
  192.  
  193. $pregs  ->addPregunta(
  194.             (new PreguntaCuestionario("Cuando los europeos descubrieron oficialmente de America ?"))
  195.             ->addRespuesta(1942)->addRespuesta(1542)->addRespuesta(1492)
  196.             ->setRespuestaCorrecta(3)
  197.             )
  198.  
  199.         ->addPregunta( 
  200.             (new PreguntaCuestionario("Quien era Atahuala ?"))
  201.             ->addRespuestas(["un espanol conquistador","un jeque americano"])      
  202.             ->setRespuestaCorrecta(2)
  203.             )
  204.            
  205.         ->addPregunta( 
  206.             (new PreguntaCuestionario("De que nacionalidad era Cristobal Colon ?"))
  207.             ->addRespuestas(["No se sabe con certeza","italiano","portugues","ingles"])    
  208.             ->setRespuestaCorrecta(1)
  209.            
  210.             )->desordenar();
  211.  
  212.            
  213.            
  214.  
  215. // Lo recupero:
  216. $nombre = $pregs->getNombre();
  217.  
  218. foreach ($pregs as $pregunta)
  219. {  
  220.     echo "<span>".$pregunta->getNombre()."</span><p/>";
  221.    
  222.     foreach ($pregunta as $option)         
  223.         echo "<input  type=\"radio\" name=\"{$nombre}[]\" value=\"\"/>{$option}</option><p/>";
  224.        
  225.     //echo $pregunta->getRespuestaCorrecta();
  226.     echo "<br/>";  
  227. }
  228.  
  229. // recupero respuestas
  230. foreach ($pregs as $pregunta)  
  231.     echo $pregunta->getRespuestaCorrecta()."<br/>";
  232.  
  233. // para consola:
  234. echo $pregs;

Ahora se puede desordenar todo el formulario tanto preguntas como respuestas con llamado al metodo desordenar() en el cuestionario
__________________
Salu2!