Example #1
0
 /**
  * Create a full structured test
  *
  * @access public
  *
  * @param $data id node
  * @return the relative nodes structure or an AMA_Error object
  */
 public static function readTest($id_nodo, $dh = null)
 {
     if (is_null($dh)) {
         $dh = $GLOBALS['dh'];
     }
     //check if $id_nodo param is an integer and retrieve rows from database
     if (intval($id_nodo) > 0) {
         $id_nodo = intval($id_nodo);
         $data = $dh->test_getNodesByRadix($id_nodo);
         if (AMA_DataHandler::isError($data)) {
             return $data;
         } else {
             $objects = array();
             $root = null;
             //ciclying all rows to instantiate and attach nodes to form a three
             //the external loop is used to catch all the nodes that doesn't find a father on first tries
             while (!empty($data)) {
                 foreach ($data as $k => $v) {
                     $tipo = $v['tipo'][0];
                     $parent = $v['id_nodo_parent'];
                     $id = $v['id_nodo'];
                     //this search the root
                     if (is_null($root) && ($tipo == ADA_TYPE_TEST || $tipo == ADA_TYPE_SURVEY)) {
                         $objects[$id] = NodeTest::readNode($v);
                         $root = $objects[$id];
                         self::$nodesArray[$root->id_nodo] = $root;
                         //once the row is attach, it can be deleted
                         unset($data[$k]);
                     } else {
                         if (!is_null($parent) && isset($objects[$parent])) {
                             $objects[$id] = NodeTest::readNode($v, $objects[$parent]);
                             $objects[$parent]->addChild($objects[$id]);
                             //once the row is attach, it can be deleted
                             unset($data[$k]);
                         }
                     }
                 }
             }
             //free resources
             unset($objects);
             //if $root is still null, the test doesn't exists!
             if (is_null($root)) {
                 return new AMA_Error(AMA_ERR_INCONSISTENT_DATA);
             } else {
                 return $root;
             }
         }
     } else {
         return new AMA_Error(AMA_ERR_WRONG_ARGUMENTS);
     }
 }