public function test_private_list() { global $db; $db->do_query('select idUsers from User LIMIT 1'); $uid = Null; while ($arr = $db->get_row_array()) { $uid = $arr['idUsers']; } $projects = EcProject::getUserProjects($uid); $this->assertInternalType('array', $projects); for ($i = 0; $i < count($projects); $i++) { $this->assertInternalType('array', $projects[$i]); $this->assertTrue(array_key_exists('name', $projects[$i])); $this->assertInternalType('string', $projects[0]['name']); $this->assertTrue(array_key_exists('ttl', $projects[$i])); $this->assertTrue(array_key_exists('ttl24', $projects[$i])); $this->assertTrue(array_key_exists('listed', $projects[$i])); $this->assertRegexp('/public|private/', $projects[$i]['listed']); //$this->assertInternalType('int', $projects[0]['ttl']); //$this->assertInternalType('int', $projects[0]['ttl24']); } }
function projectUsage() { global $url, $auth; $prj = new EcProject(); $prj->name = substr($url, 0, strpos($url, "/")); $prj->fetch(); if (!$prj->isPublic && $prj->checkPermission($auth->getEcUserId()) < 2) { return "access denied"; } $sum = $prj->getUsage(); header("Content-type: text/plain"); echo $sum; //"{\"forms\" : ". json_encode($sum) . "}"; }
public function test_title_validation() { for ($p = 0; $p < count($this->projects); $p++) { $prj = new EcProject(); $prj->name = $this->projects[$p]['name']; $prj->fetch(); $tbls = array_keys($prj->tables); for ($i = 0; $i < count($tbls); $i++) { $title_fields = array(); $req = $prj->tables[$tbls[$i]]->ask(); $this->assertTrue($req); $ttlfields = $prj->tables[$tbls[$i]]->titleFields; if (count($ttlfields) == 0) { continue; } $this->assertInternalType('array', $ttlfields); while ($obj = $prj->tables[$tbls[$i]]->recieve()) { $ttl_vals = array(); for ($f = 0; $f < count($ttlfields); $f++) { if (array_key_exists($ttlfields[$f], $obj[0])) { array_push($ttl_vals, $obj[0][$ttlfields[$f]]); } } array_push($title_fields, $ttl_vals); } for ($t = 0; $t < count($title_fields); $t++) { if (count($title_fields[$t]) == 0) { continue; } $args = array_combine($prj->tables[$tbls[$i]]->titleFields, $title_fields[$t]); $r = $prj->tables[$tbls[$i]]->ask($args); $this->assertTrue($r); while ($prj->tables[$tbls[$i]]->recieve()) { } $ttl = implode(', ', $title_fields[$t]); $response = $prj->tables[$tbls[$i]]->validateTitle($ttl); /*if($response['valid'] === false) { print ' xxx ' . $prj->name . ' > ' . $tbls[$i] . ' > ' .$ttl; }*/ $this->assertTrue($response['valid']); } } } }
public static function postEntries($entries) { global $db; $qry = 'INSERT INTO entry (form, projectName, formName, DeviceId, created, uploaded, user, bulk_insert_key) VALUES '; $len = count($entries); $sessId = session_id(); $prj = new EcProject(); $prj->name = $entries[0]->projectName; $prj->fetch(); $keyfield = $prj->tables[$entries[0]->formName]->key; for ($i = 0; $i < $len; ++$i) { if (!$entries[$i]->created || $entries[$i]->created == "NULL") { $entries[$i]->created = getTimestamp(); } else { if (!is_numeric($entries[$i]->created)) { $entries[$i]->created = EcTable::unformatCreated($entries[$i]->created); } } if ($prj->tables[$entries[$i]->formName]->checkExists($entries[$i]->values[$keyfield])) { throw new Exception(sprintf('Your data could not be uploaded, there was a duplicate key for entry %s on line %s of your CSV file', $entries[$i]->values[$keyfield], $i + 2)); } $entries[$i]->insert_key = sprintf('%s%s', $sessId, $i); $qry .= sprintf('%s (%s, %s, %s, %s, %s, \'%s\', 0, \'%s\')', $i > 0 ? ',' : '', $entries[$i]->form->id, $db->stringVal($entries[$i]->projectName), $db->stringVal($entries[$i]->formName), $db->stringVal($entries[$i]->deviceId), $entries[$i]->created, getTimestamp("Y-m-d H:i:s"), $entries[$i]->insert_key); //echo $_SERVER['REQUEST_TIME'] . '<br />\r\n'; } $res = $db->do_query($qry); if ($res !== true) { die($res); } $qry = sprintf('SELECT bulk_insert_key, idEntry FROM entry where bulk_insert_key Like \'%s%%\'', $sessId); $res = $db->do_query($qry); if ($res !== true) { die($res); } $insert_keys = array(); while ($arr = $db->get_row_array()) { $insert_keys[$arr["bulk_insert_key"]] = $arr["idEntry"]; } $qry = 'INSERT INTO entryvalue (field, projectName, formName, fieldName, value, entry) VALUES '; for ($i = 0; $i < $len; ++$i) { if (trim($entries[$i]->values[$entries[$i]->form->key]) == '') { return 'Key values cannot be blank'; } $keys = array_keys($entries[$i]->values); $length = count($keys); for ($j = 0; $j < $length; ++$j) { if ($entries[$i]->form->fields[$keys[$j]]) { if (($entries[$i]->form->fields[$keys[$j]]->type == 'gps' || $entries[$i]->form->fields[$keys[$j]]->type == 'location') && !is_string($entries[$i]->values[$keys[$j]])) { $qry .= sprintf('%s ( %s, \'%s\', \'%s\', \'%s\', %s, %s )', $i > 0 || $j > 0 ? ',' : '', $entries[$i]->form->fields[$keys[$j]]->idField, $entries[$i]->form->survey->name, $entries[$i]->form->name, $keys[$j], $db->stringVal(json_encode($entries[$i]->values[$keys[$j]])), $insert_keys[$entries[$i]->insert_key]); } else { $qry .= sprintf('%s ( %s, \'%s\', \'%s\', \'%s\', %s, %s)', $i > 0 || $j > 0 ? ',' : '', $entries[$i]->form->fields[$keys[$j]]->idField, $entries[$i]->form->survey->name, $entries[$i]->form->name, $keys[$j], $db->stringVal($entries[$i]->values[$keys[$j]]), $insert_keys[$entries[$i]->insert_key]); } } } } $res = $db->do_query($qry); if ($res !== true) { die($res); } $qry = sprintf('UPDATE entry SET bulk_insert_key = NULL WHERE bulk_insert_key Like \'%s%%\'', $sessId); $res = $db->do_query($qry); return $res; }