function insererCasesDansBaseDeDonnees($cases, $conn) { foreach ($cases as &$case) { // Convertir les enregistrement du format text au arrays associatives $records = array(); foreach ($case['data'] as &$record) { array_push($records, parseRecord($record)); } // Les identifiants seront de meme pour chaque enregistrement dans le cas // alors on peut les prendre chez le premier enregistrement. $firstRecord = $records[0]; // Voir si ce menage existe deja dans la base et trouver son id $stmt = $conn->prepare('SELECT ID FROM menages WHERE PROVINCE=:prov AND DISTRICT=:dist AND VILLAGE=:vill AND EA=:ea AND UR=:ur AND BUILDING=:build AND HU=:hu AND HH=:hh'); $stmt->bindParam(':prov', $firstRecord['PROVINCE']); $stmt->bindParam(':dist', $firstRecord['DISTRICT']); $stmt->bindParam(':vill', $firstRecord['VILLAGE']); $stmt->bindParam(':ea', $firstRecord['EA']); $stmt->bindParam(':ur', $firstRecord['UR']); $stmt->bindParam(':build', $firstRecord['BUILDING']); $stmt->bindParam(':hu', $firstRecord['HU']); $stmt->bindParam(':hh', $firstRecord['HH']); $stmt->execute(); $idMenage = $stmt->fetchColumn(); if (!$idMenage) { // Nouveau menage, inserer le menage dans la base de donnees $stmt = $conn->prepare('INSERT INTO menages (PROVINCE,DISTRICT,VILLAGE,EA,UR,BUILDING,HU,HH) VALUES(:prov, :dist, :vill, :ea, :ur, :build, :hu, :hh)'); $stmt->bindParam(':prov', $firstRecord['PROVINCE']); $stmt->bindParam(':dist', $firstRecord['DISTRICT']); $stmt->bindParam(':vill', $firstRecord['VILLAGE']); $stmt->bindParam(':ea', $firstRecord['EA']); $stmt->bindParam(':ur', $firstRecord['UR']); $stmt->bindParam(':build', $firstRecord['BUILDING']); $stmt->bindParam(':hu', $firstRecord['HU']); $stmt->bindParam(':hh', $firstRecord['HH']); $stmt->execute(); $idMenage = $conn->lastInsertId(); } // Supprimer les individus de ce menage pour les remplacer apres $stmt = $conn->prepare('DELETE FROM individus WHERE ID_MENAGE=:id_men'); $stmt->bindParam(':id_men', $idMenage); $stmt->execute(); // Inserer les individus dans la base de donnes foreach ($records as &$record) { if ($record['TYPE'] == 1) { // person $stmt = $conn->prepare('INSERT INTO individus (ID_MENAGE,AGE,SEXE) VALUES(:id_men, :age, :sexe)'); $stmt->bindParam(':id_men', $idMenage); $stmt->bindParam(':age', $record['AGE']); $stmt->bindParam(':sexe', $record['SEX']); $stmt->execute(); } else { // housing - rien a faire comme on l'a deja inserer } } } }
function parseRecords($xml, $session_epoch) { $records = array(); $timer = new Benchmark_Timer(); $timer->start(); $timer->setMarker('Parse XML to tags - start'); /* Parse the XML into tags */ $parser = xml_parser_create(); xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); xml_parse_into_struct($parser, $xml, $values, $tags); xml_parser_free($parser); // loop through the structures $timer->setMarker('tags to arrays - start'); foreach ($tags as $key => $value) { if ($key == "record") { $molranges = $value; // each contiguous pair of array entries are the // lower and upper range for each molecule definition for ($i = 0; $i < count($molranges); $i += 2) { $offset = $molranges[$i] + 1; $len = $molranges[$i + 1] - $offset; $records[] = parseRecord($values, $offset, $len); } } else { continue; } } $timer->setMarker('tags to array - done'); $i = 0; $timer->setMarker('gradient calcs - start'); /* Gradient calc constants */ $NUM_GRADIENT_SAMPES = 11; $LOW_OFFSET = floor($NUM_GRADIENT_SAMPES / 2); $HIGH_OFFSET = floor($NUM_GRADIENT_SAMPES / 2); /* Create the window function */ /* Tukey window */ $alpha = 0.5; $window = array(); for ($i = 0; $i < $NUM_GRADIENT_SAMPES; $i++) { if ($i <= $alpha * $NUM_GRADIENT_SAMPES / 2) { $window[$i] = 0.5 * (1 + cos(M_PI * (2 * $i / ($alpha * $NUM_GRADIENT_SAMPES) - 1))); } else { if ($i <= $NUM_GRADIENT_SAMPES * (1 - $alpha / 2)) { $window[$i] = 1.0; } else { $window[$i] = 0.5 * (1 + cos(M_PI * (2 * $i / ($alpha * $NUM_GRADIENT_SAMPES) - 2 / $alpha + 1))); } } } $i = 0; $num_records = count($records); foreach ($records as $record) { /* Convert the timestamp into an interval */ $ftime = strptime($record->timestamp, '%FT%T%z'); $record_epoch = mktime($ftime['tm_hour'], $ftime['tm_min'], $ftime['tm_sec'], 1, $ftime['tm_yday'] + 1, $ftime['tm_year'] + 1900); $record->interval = $record_epoch - $session_epoch; if ($i > 0) { $record->delta_distance = $record->distance - $records[$i - 1]->distance; $record->delta_altitude = round($record->altitude - $records[$i - 1]->altitude, 2); } else { $record->delta_distance = 0; $record->delta_altitude = 0; } /* Calculate the average gradient */ $total_rise = 0; $total_distance = 0; unset($first_distance); $last_distance = 0; for ($g = $i - $LOW_OFFSET, $j = 0; $g <= $i + $HIGH_OFFSET; $g++, $j++) { if ($g >= 0 && $g < $num_records) { if (!isset($first_distance)) { $first_distance = $records[$g]->distance; } $total_rise += ($records[$g]->altitude - $record->altitude) * $window[$j]; $last_distance = $records[$g]->distance; } } $avg_rise = $total_rise / $NUM_GRADIENT_SAMPES; $avg_distance = ($last_distance - $first_distance) / $NUM_GRADIENT_SAMPES * 1000; if ($avg_distance) { $record->gradient = round($avg_rise / $avg_distance * 100, 1); } else { $record->gradient = 0; } /* TODO: Calculate the power */ $i++; } $timer->setMarker('gradient calcs - done'); //$timer->display(); return $records; }
function odfSpreadsheet($result) { global $citeKeysArray; // '$citeKeysArray' is made globally available from within this function $exportArray = array(); // array for individually exported records $citeKeysArray = array(); // array of cite keys (used to ensure uniqueness of cite keys among all exported records) // Map ODF indexes to refbase field names, map ODF reference types to refbase types and define search & replace patterns: list($universalSearchReplaceActionsArray, $fieldSpecificSearchReplaceActionsArray, $odfIndexesToRefbaseFieldsArray, $referenceTypesToRefbaseTypesArray) = initializeArrays(); // Generate the export for each record and push them onto an array: while ($row = @mysql_fetch_array($result)) { // Parse the current record into an array of field data that shall be exported to ODF: $recordExportArray = parseRecord($row, $odfIndexesToRefbaseFieldsArray, $referenceTypesToRefbaseTypesArray, $universalSearchReplaceActionsArray, $fieldSpecificSearchReplaceActionsArray); // Export the current record as ODF XML in a spreadsheet table row: $record = odfSpreadsheetTableRow($recordExportArray, "data"); if (!empty($record)) { // unless the record buffer is empty... array_push($exportArray, $record); } // ...add it to an array of exports } $odfSpreadsheet = new XML("office:spreadsheet"); $odfSpreadsheetTable = new XMLBranch("table:table"); $odfSpreadsheetTable->setTagAttribute("table:name", "biblio"); $odfSpreadsheetTable->setTagAttribute("table:style-name", "ta1"); $columnHeadings = odfSpreadsheetTableRow($odfIndexesToRefbaseFieldsArray, "heading"); // export column headings as ODF XML in a spreadsheet table row $odfSpreadsheetTable->addXMLasBranch($columnHeadings); foreach ($exportArray as $tableRowXML) { $odfSpreadsheetTable->addXMLasBranch($tableRowXML); } $odfSpreadsheet->addXMLBranch($odfSpreadsheetTable); return $odfSpreadsheet; }