Нема описа

importQuestionnaire.php 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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, id_questionnair) values (%s,%s)",
  119. GetSQLValueString($cat["catText"],"text"),
  120. GetSQLValueString($id_cuestionario,"int")
  121. );
  122. // print($sql);print "\n";
  123. mysqli_query($connection, $sql);
  124. $id_cat_db=mysqli_insert_id($connection);
  125. $cat["id_cat_db"]=$id_cat_db;
  126. // $id_cat_db++;
  127. }
  128. // $id_subcat_db=2345;
  129. foreach($subcategoria as &$subcat)
  130. {
  131. $id_cat_db=$categoria[$subcat["cat"]]["id_cat_db"];
  132. $sql=sprintf("insert into subcategory (subcategoria,id_categoria) values (%s,%s)",
  133. GetSQLValueString($subcat["subcat"],"text"),
  134. GetSQLValueString($id_cat_db,"int")
  135. );
  136. // print($sql);print "\n";
  137. mysqli_query($connection, $sql);
  138. $id_subcat_db=mysqli_insert_id($connection);
  139. $subcat["id_subcat_db"]=$id_subcat_db;
  140. // $id_subcat_db++;
  141. }
  142. // $id_ref_db=5678;
  143. foreach($referencias as &$ref)
  144. {
  145. $sql=sprintf("insert into reference (referencia) values (%s)",
  146. GetSQLValueString($ref["refTexto"],"text")
  147. );
  148. // print($sql);print "\n";
  149. mysqli_query($connection, $sql);
  150. $id_ref_db=mysqli_insert_id($connection);
  151. $ref["id_ref_db"]=$id_ref_db;
  152. // $id_ref_db++;
  153. }
  154. // $id_q_db=3456;
  155. $n=count($q);
  156. foreach($q as $k=>$preg)
  157. {
  158. // $preg=$q[$k];
  159. $id_cat_db=$categoria[$preg[1]]["id_cat_db"];
  160. $id_subcat_db=$subcategoria[$preg[2]]["id_subcat_db"];
  161. $id_ref_db=$referencias[$preg[3]]["id_ref_db"];
  162. $sql=sprintf("insert into question (premise, id_category, id_subcategory, id_type, id_referencia) values (%s,%s,%s,%s,%s)",
  163. GetSQLValueString($preg[9],"text"),
  164. GetSQLValueString($id_cat_db,"int"),
  165. GetSQLValueString($id_subcat_db,"int"),
  166. GetSQLValueString($preg[4],"int"),
  167. GetSQLValueString($id_ref_db,"int")
  168. );
  169. // print($sql);print "\n";
  170. mysqli_query($connection, $sql);
  171. $id_q_db=mysqli_insert_id($connection);
  172. $q[$k]["id_q_db"]=$id_q_db;
  173. if($preg[4]==1)
  174. {
  175. $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)",
  176. GetSQLValueString($preg[4],"int"),
  177. GetSQLValueString($id_q_db,"int"),
  178. GetSQLValueString("min_val","text"),
  179. GetSQLValueString($preg[5],"int"),
  180. GetSQLValueString($preg[4],"int"),
  181. GetSQLValueString($id_q_db,"int"),
  182. GetSQLValueString("min_texto","text"),
  183. GetSQLValueString($preg[6],"text"),
  184. GetSQLValueString($preg[4],"int"),
  185. GetSQLValueString($id_q_db,"int"),
  186. GetSQLValueString("max_val","text"),
  187. GetSQLValueString($preg[7],"int"),
  188. GetSQLValueString($preg[4],"int"),
  189. GetSQLValueString($id_q_db,"int"),
  190. GetSQLValueString("max_texto","text"),
  191. GetSQLValueString($preg[8],"text")
  192. );
  193. // print($sql);print "\n";
  194. mysqli_query($connection, $sql);
  195. }
  196. $sql=sprintf("INSERT INTO `questionnair_question`(`id_questionnair`, `id_question`) VALUES (%s, %s)
  197. ",
  198. GetSQLValueString($id_cuestionario,"int"),
  199. GetSQLValueString($id_q_db,"int")
  200. );
  201. mysqli_query($connection, $sql);
  202. // $id_q_db++;
  203. }
  204. // print_r($q);
  205. // exit();
  206. // $id_subq=6789;
  207. // $title="Pre Test";
  208. // $descripcion="$title of $q_title";
  209. // $sql=sprintf("insert into subquestionnair (`title`, `description`, `id_questionnair`, `date_to_administer`) values (%s,%s,%s,%s)",
  210. // GetSQLValueString($title,"text"),
  211. // GetSQLValueString($descripcion,"text"),
  212. // GetSQLValueString($id_cuestionario,"int"),
  213. // GetSQLValueString($pretest_date,"text")
  214. // );
  215. // print($sql);print "\n";
  216. // mysqli_query($connection, $sql);
  217. // $id_subq=mysqli_insert_id($connection);
  218. // $sql=sprintf("INSERT INTO `experience_subquestionnair`( `id_experience`, `id_subquestionnair`) VALUES (%s,%s)",
  219. // GetSQLValueString($id_experience,"int"),
  220. // GetSQLValueString($id_subq,"int")
  221. // );
  222. // mysqli_query($connection, $sql);
  223. // // print_r($q);
  224. // // exit();
  225. // $n=count($q);
  226. // foreach($q as $k=>$preg)
  227. // {
  228. // //print_r($data);print_r($q[$data[0]]);exit();
  229. // // print_r($preg);
  230. // $sql=sprintf("insert into subquestionnair_question (`id_subquestionnair`, `id_question`) values (%s,%s)",
  231. // GetSQLValueString($id_subq,"int"),
  232. // GetSQLValueString($q[$k]["id_q_db"],"int")
  233. // );
  234. // print($sql);print "\n";
  235. // mysqli_query($connection, $sql);
  236. // }
  237. // exit();
  238. // $id_subq++;
  239. foreach($archivosCSV as $filename)
  240. {
  241. if(strpos($filename, "questions")===false and strpos($filename, "instructions")===false)
  242. {
  243. $row = 1;
  244. $title=explode(".",explode("-", $filename)[1])[0];
  245. if (($handle = fopen($filename, "r")) !== FALSE)
  246. {
  247. // print $filename."\n";
  248. $j=0;
  249. while (($data = fgetcsv($handle)) !== FALSE)
  250. {
  251. // if($j==2)print_r($data);print"<br>";
  252. if($j==0)
  253. {
  254. $descripcion=$data[1];
  255. $fecha=$data[3];
  256. $sql=sprintf("insert into subquestionnair (`title`, `description`, `id_questionnair`, `date_to_administer`) values (%s,%s,%s,%s)",
  257. GetSQLValueString($title,"text"),
  258. GetSQLValueString($descripcion,"text"),
  259. GetSQLValueString($id_cuestionario,"int"),
  260. GetSQLValueString($fecha,"text")
  261. );
  262. // print($sql);print "\n";
  263. mysqli_query($connection, $sql);
  264. $id_subq=mysqli_insert_id($connection);
  265. $sql=sprintf("INSERT INTO `experience_subquestionnair`( `id_experience`, `id_subquestionnair`) VALUES (%s,%s)",
  266. GetSQLValueString($id_experience,"int"),
  267. GetSQLValueString($id_subq,"int")
  268. );
  269. mysqli_query($connection, $sql);
  270. }
  271. if($j>1)
  272. {
  273. // if($j==2)print_r($data);print"<br>";
  274. //print_r($data);print_r($q[$data[0]]);exit();
  275. $sql=sprintf("insert into subquestionnair_question (`id_subquestionnair`, `id_question`) values (%s,%s)",
  276. GetSQLValueString($id_subq,"int"),
  277. GetSQLValueString($q[$data[0]]["id_q_db"],"int")
  278. );
  279. // print($sql);print "\n";
  280. mysqli_query($connection, $sql);
  281. // $q[$data[0]]
  282. }
  283. $j++;
  284. }
  285. }
  286. }
  287. // $id_subq++;
  288. fclose($handle);
  289. }
  290. // $title="Post Test";
  291. // $descripcion="$title of $q_title";
  292. // $sql=sprintf("insert into subquestionnair (`title`, `description`, `id_questionnair`, `date_to_administer`) values (%s,%s,%s,%s)",
  293. // GetSQLValueString($title,"text"),
  294. // GetSQLValueString($descripcion,"text"),
  295. // GetSQLValueString($id_cuestionario,"int"),
  296. // GetSQLValueString($pretest_date,"text")
  297. // );
  298. // print($sql);print "\n";
  299. // mysqli_query($connection, $sql);
  300. // $id_subq=mysqli_insert_id($connection);
  301. // $sql=sprintf("INSERT INTO `experience_subquestionnair`( `id_experience`, `id_subquestionnair`) VALUES (%s,%s)",
  302. // GetSQLValueString($id_experience,"int"),
  303. // GetSQLValueString($id_subq,"int")
  304. // );
  305. // mysqli_query($connection, $sql);
  306. //
  307. //
  308. // $n=count($q);
  309. // foreach($q as $k=>$preg)
  310. // {
  311. // //print_r($data);print_r($q[$data[0]]);exit();
  312. // $sql=sprintf("insert into subquestionnair_question (`id_subquestionnair`, `id_question`) values (%s,%s)",
  313. // GetSQLValueString($id_subq,"int"),
  314. // GetSQLValueString($q[$k]["id_q_db"],"int")
  315. // );
  316. // print($sql);print "\n";
  317. // mysqli_query($connection, $sql);
  318. // }
  319. foreach($archivosCSV as $filename)
  320. {
  321. exec("rm '$filename'");
  322. }
  323. header("Location: ".$_SERVER['HTTP_REFERER']."#questionnaires");
  324. ?>