Açıklama Yok

importQuestionnaire.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. include "/var/www/html/funciones.php";
  3. // include "/var/www/html/conection_test.php";
  4. require_once 'config.php';
  5. require_once 'dbh.inc.php';
  6. require_once 'checkLogin.php';
  7. // print_r($_FILES);
  8. // print_r($_SERVER);
  9. // print_r($_POST);
  10. // print_r($_GET);
  11. //
  12. // exit();
  13. // $archivo="Cuestionario perfecto.xlsx";
  14. $id_experience= mysqli_real_escape_string($connection, trim($_POST['id_experience']));
  15. // $id_experience= mysqli_real_escape_string($connection, $_GET['id_experience']);;
  16. $archivo=$_FILES["import"]["tmp_name"];
  17. $archivoOut="uploaderTemp";
  18. exec("ssconvert -S '$archivo' ../temp/$archivoOut-%s.csv");
  19. // print "ssconvert -S -D ../temp '$archivo' $archivoOut-%s.csv";print"<br>";
  20. // print getcwd();
  21. $q_title=explode(".", $_FILES["import"]["name"])[0];
  22. $data_dir="../temp/";
  23. $archivosCSV=glob("$data_dir$archivoOut*.csv");
  24. print_r($archivosCSV);
  25. // exit();
  26. // $n=count($archivosCSV);
  27. // $n=2;
  28. // foreach (glob($archivoOut."*.csv") as $filename)
  29. $categoria=array();
  30. $subcategoria=array();
  31. $referencias=array();
  32. $tipo=array(1=>"SCALE",2=>"OPEN");
  33. $q=array();
  34. $i=0;
  35. $filename="$data_dir$archivoOut-questions.csv";
  36. $row = 1;
  37. if (($handle = fopen($filename, "r")) !== FALSE)
  38. {
  39. // print $filename."\n";
  40. $j=0;
  41. while (($data = fgetcsv($handle)) !== FALSE)
  42. {
  43. // print_r($data);
  44. if($j==0)
  45. {
  46. $descripcion=$data[1];
  47. $pretest_date=$data[3];
  48. $posttest_date=$data[5];
  49. $sql=sprintf("insert into questionnair (q_title,description) values (%s,%s)",
  50. GetSQLValueString($q_title,"text"),
  51. GetSQLValueString($descripcion,"text")
  52. );
  53. mysqli_query($connection, $sql);
  54. $id_cuestionario=mysqli_insert_id($connection);
  55. $sql=sprintf("INSERT INTO `experience_questionnair`(`id_experience`, `id_questionnair`) VALUES (%s, %s)
  56. ",
  57. GetSQLValueString($id_experience,"int"),
  58. GetSQLValueString($id_cuestionario,"int")
  59. );
  60. mysqli_query($connection, $sql);
  61. }
  62. if($j>1)
  63. {
  64. // $num = count($data);
  65. // print_r($data);
  66. // $cat=array_search($data[1],$categoria);
  67. $cat=array_search($data[1], array_column($categoria, 'catText'));
  68. if($cat===false)
  69. {
  70. $categoria[]=array("catText"=>$data[1],"id_cat_db"=>"0");
  71. $cat=array_search($data[1], array_column($categoria, 'catText'));
  72. }
  73. // $subcat=array_search($data[2],$subcategoria);
  74. $subcat=array_search($data[2], array_column($subcategoria, 'subcat'));
  75. if($subcat===false)
  76. {
  77. $subcategoria[]=array('subcat'=>$data[2],"cat"=>$cat,"id_sub_db"=>0);
  78. $subcat=array_search($data[2], array_column($subcategoria, 'subcat'));
  79. }
  80. $ref=array_search($data[7],$referencias);
  81. $ref=array_search($data[7], array_column($referencias, 'refTexto'));
  82. if($ref===false)
  83. {
  84. $referencias[]=array("refTexto"=>$data[7],"id_ref_db"=>0);
  85. $ref=array_search($data[7], array_column($referencias, 'refTexto'));
  86. }
  87. $t=array_search($data[4],$tipo);
  88. // if($t===false)
  89. // {
  90. // $tipo[]=$data[4];
  91. // $t=array_search($data[4],$tipo);
  92. // }
  93. $vals=explode("-", $data[5]);
  94. $minval=$vals[0];
  95. if(isset($vals[1]))$mintext=trim($vals[1]);else $mintext="";
  96. $vals=explode("-", $data[6]);
  97. $maxval=$vals[0];
  98. if(isset($vals[1]))$maxtext=trim($vals[1]);else $maxtext="";
  99. $q[$data[0]]=array($data[0],$cat,$subcat,$ref,$t,$minval,$mintext,$maxval,$maxtext,$data[3],"id_q_db"=>"0");
  100. }
  101. $j++;
  102. }
  103. fclose($handle);
  104. }
  105. // exit();
  106. // print_r($categoria);
  107. // print_r($subcategoria);
  108. // print_r($referencias);
  109. // print_r($q);
  110. // print_r($tipo);
  111. //
  112. // exit();
  113. // $id_cuestionario=1000;
  114. // $id_cat_db=1234;
  115. // $id_cat_db=2345;
  116. foreach($categoria as &$cat)
  117. {
  118. $sql=sprintf("insert into category (categoria) values (%s)",
  119. GetSQLValueString($cat["catText"],"text")
  120. );
  121. // print($sql);print "\n";
  122. mysqli_query($connection, $sql);
  123. $id_cat_db=mysqli_insert_id($connection);
  124. $cat["id_cat_db"]=$id_cat_db;
  125. // $id_cat_db++;
  126. }
  127. // $id_subcat_db=2345;
  128. foreach($subcategoria as &$subcat)
  129. {
  130. $id_cat_db=$categoria[$subcat["cat"]]["id_cat_db"];
  131. $sql=sprintf("insert into subcategory (subcategoria,id_categoria) values (%s,%s)",
  132. GetSQLValueString($subcat["subcat"],"text"),
  133. GetSQLValueString($id_cat_db,"int")
  134. );
  135. // print($sql);print "\n";
  136. mysqli_query($connection, $sql);
  137. $id_subcat_db=mysqli_insert_id($connection);
  138. $subcat["id_subcat_db"]=$id_subcat_db;
  139. // $id_subcat_db++;
  140. }
  141. // $id_ref_db=5678;
  142. foreach($referencias as &$ref)
  143. {
  144. $sql=sprintf("insert into reference (referencia) values (%s)",
  145. GetSQLValueString($ref["refTexto"],"text")
  146. );
  147. // print($sql);print "\n";
  148. mysqli_query($connection, $sql);
  149. $id_ref_db=mysqli_insert_id($connection);
  150. $ref["id_ref_db"]=$id_ref_db;
  151. // $id_ref_db++;
  152. }
  153. // $id_q_db=3456;
  154. $n=count($q);
  155. foreach($q as $k=>$preg)
  156. {
  157. // $preg=$q[$k];
  158. $id_cat_db=$categoria[$preg[1]]["id_cat_db"];
  159. $id_subcat_db=$subcategoria[$preg[2]]["id_subcat_db"];
  160. $id_ref_db=$referencias[$preg[3]]["id_ref_db"];
  161. $sql=sprintf("insert into question (premise, id_category, id_subcategory, id_type, id_referencia) values (%s,%s,%s,%s,%s)",
  162. GetSQLValueString($preg[9],"text"),
  163. GetSQLValueString($id_cat_db,"int"),
  164. GetSQLValueString($id_subcat_db,"int"),
  165. GetSQLValueString($preg[4],"int"),
  166. GetSQLValueString($id_ref_db,"int")
  167. );
  168. // print($sql);print "\n";
  169. mysqli_query($connection, $sql);
  170. $id_q_db=mysqli_insert_id($connection);
  171. $q[$k]["id_q_db"]=$id_q_db;
  172. if($preg[4]==1)
  173. {
  174. $sql=sprintf("insert into question_type (id_type, id_question, label, value) values (%s,%s,%s,%s),(%s,%s,%s,%s),(%s,%s,%s,%s),(%s,%s,%s,%s)",
  175. GetSQLValueString($preg[4],"int"),
  176. GetSQLValueString($id_q_db,"int"),
  177. GetSQLValueString("min_val","text"),
  178. GetSQLValueString($preg[5],"int"),
  179. GetSQLValueString($preg[4],"int"),
  180. GetSQLValueString($id_q_db,"int"),
  181. GetSQLValueString("min_texto","text"),
  182. GetSQLValueString($preg[6],"text"),
  183. GetSQLValueString($preg[4],"int"),
  184. GetSQLValueString($id_q_db,"int"),
  185. GetSQLValueString("max_val","text"),
  186. GetSQLValueString($preg[7],"int"),
  187. GetSQLValueString($preg[4],"int"),
  188. GetSQLValueString($id_q_db,"int"),
  189. GetSQLValueString("max_texto","text"),
  190. GetSQLValueString($preg[8],"text")
  191. );
  192. // print($sql);print "\n";
  193. mysqli_query($connection, $sql);
  194. }
  195. $sql=sprintf("INSERT INTO `questionnair_question`(`id_questionnair`, `id_question`) VALUES (%s, %s)
  196. ",
  197. GetSQLValueString($id_cuestionario,"int"),
  198. GetSQLValueString($id_q_db,"int")
  199. );
  200. mysqli_query($connection, $sql);
  201. // $id_q_db++;
  202. }
  203. // print_r($q);
  204. // exit();
  205. // $id_subq=6789;
  206. // $title="Pre Test";
  207. // $descripcion="$title of $q_title";
  208. // $sql=sprintf("insert into subquestionnair (`title`, `description`, `id_questionnair`, `date_to_administer`) values (%s,%s,%s,%s)",
  209. // GetSQLValueString($title,"text"),
  210. // GetSQLValueString($descripcion,"text"),
  211. // GetSQLValueString($id_cuestionario,"int"),
  212. // GetSQLValueString($pretest_date,"text")
  213. // );
  214. // print($sql);print "\n";
  215. // mysqli_query($connection, $sql);
  216. // $id_subq=mysqli_insert_id($connection);
  217. // $sql=sprintf("INSERT INTO `experience_subquestionnair`( `id_experience`, `id_subquestionnair`) VALUES (%s,%s)",
  218. // GetSQLValueString($id_experience,"int"),
  219. // GetSQLValueString($id_subq,"int")
  220. // );
  221. // mysqli_query($connection, $sql);
  222. // // print_r($q);
  223. // // exit();
  224. // $n=count($q);
  225. // foreach($q as $k=>$preg)
  226. // {
  227. // //print_r($data);print_r($q[$data[0]]);exit();
  228. // // print_r($preg);
  229. // $sql=sprintf("insert into subquestionnair_question (`id_subquestionnair`, `id_question`) values (%s,%s)",
  230. // GetSQLValueString($id_subq,"int"),
  231. // GetSQLValueString($q[$k]["id_q_db"],"int")
  232. // );
  233. // print($sql);print "\n";
  234. // mysqli_query($connection, $sql);
  235. // }
  236. // exit();
  237. // $id_subq++;
  238. foreach($archivosCSV as $filename)
  239. {
  240. if(strpos($filename, "questions")===false and strpos($filename, "instructions")===false)
  241. {
  242. $row = 1;
  243. $title=explode(".",explode("-", $filename)[1])[0];
  244. if (($handle = fopen($filename, "r")) !== FALSE)
  245. {
  246. // print $filename."\n";
  247. $j=0;
  248. while (($data = fgetcsv($handle)) !== FALSE)
  249. {
  250. // if($j==2)print_r($data);print"<br>";
  251. if($j==0)
  252. {
  253. $descripcion=$data[1];
  254. $fecha=$data[3];
  255. $sql=sprintf("insert into subquestionnair (`title`, `description`, `id_questionnair`, `date_to_administer`) values (%s,%s,%s,%s)",
  256. GetSQLValueString($title,"text"),
  257. GetSQLValueString($descripcion,"text"),
  258. GetSQLValueString($id_cuestionario,"int"),
  259. GetSQLValueString($fecha,"text")
  260. );
  261. // print($sql);print "\n";
  262. mysqli_query($connection, $sql);
  263. $id_subq=mysqli_insert_id($connection);
  264. $sql=sprintf("INSERT INTO `experience_subquestionnair`( `id_experience`, `id_subquestionnair`) VALUES (%s,%s)",
  265. GetSQLValueString($id_experience,"int"),
  266. GetSQLValueString($id_subq,"int")
  267. );
  268. mysqli_query($connection, $sql);
  269. }
  270. if($j>1)
  271. {
  272. // if($j==2)print_r($data);print"<br>";
  273. //print_r($data);print_r($q[$data[0]]);exit();
  274. $sql=sprintf("insert into subquestionnair_question (`id_subquestionnair`, `id_question`) values (%s,%s)",
  275. GetSQLValueString($id_subq,"int"),
  276. GetSQLValueString($q[$data[0]]["id_q_db"],"int")
  277. );
  278. // print($sql);print "\n";
  279. mysqli_query($connection, $sql);
  280. // $q[$data[0]]
  281. }
  282. $j++;
  283. }
  284. }
  285. }
  286. // $id_subq++;
  287. fclose($handle);
  288. }
  289. // $title="Post Test";
  290. // $descripcion="$title of $q_title";
  291. // $sql=sprintf("insert into subquestionnair (`title`, `description`, `id_questionnair`, `date_to_administer`) values (%s,%s,%s,%s)",
  292. // GetSQLValueString($title,"text"),
  293. // GetSQLValueString($descripcion,"text"),
  294. // GetSQLValueString($id_cuestionario,"int"),
  295. // GetSQLValueString($pretest_date,"text")
  296. // );
  297. // print($sql);print "\n";
  298. // mysqli_query($connection, $sql);
  299. // $id_subq=mysqli_insert_id($connection);
  300. // $sql=sprintf("INSERT INTO `experience_subquestionnair`( `id_experience`, `id_subquestionnair`) VALUES (%s,%s)",
  301. // GetSQLValueString($id_experience,"int"),
  302. // GetSQLValueString($id_subq,"int")
  303. // );
  304. // mysqli_query($connection, $sql);
  305. //
  306. //
  307. // $n=count($q);
  308. // foreach($q as $k=>$preg)
  309. // {
  310. // //print_r($data);print_r($q[$data[0]]);exit();
  311. // $sql=sprintf("insert into subquestionnair_question (`id_subquestionnair`, `id_question`) values (%s,%s)",
  312. // GetSQLValueString($id_subq,"int"),
  313. // GetSQLValueString($q[$k]["id_q_db"],"int")
  314. // );
  315. // print($sql);print "\n";
  316. // mysqli_query($connection, $sql);
  317. // }
  318. foreach($archivosCSV as $filename)
  319. {
  320. exec("rm '$filename'");
  321. }
  322. header("Location: ".$_SERVER['HTTP_REFERER']."#questionnaires");
  323. ?>