PageRenderTime 72ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 1ms

/application/controllers/front.php

https://bitbucket.org/Guoawei/comsurveyweb
PHP | 1340 lines | 802 code | 152 blank | 386 comment | 48 complexity | 1517aca659c2f4f27a2f49d8e38e87d6 MD5 | raw file
  1. <?php
  2. if (!defined('BASEPATH'))
  3. exit('No direct script access allowed');
  4. /**
  5. * front 首頁控制器
  6. *
  7. * @package
  8. * @subpackage
  9. * @category
  10. * @author Will Tsai
  11. * @link http://sweea.com
  12. */
  13. class Front extends CI_Controller
  14. {
  15. /**
  16. * __construct 建構子
  17. * 取得預設所要的相關資料
  18. */
  19. public function __construct()
  20. {
  21. parent::__construct();
  22. $this->load->model(array(
  23. 'interviewer_model',
  24. 'samplelogic_model',
  25. 'surveyanswer_model',
  26. 'survey_model',
  27. 'front_answer_model'
  28. ));
  29. $this->load->library(array(
  30. 'session',
  31. 'table'
  32. ));
  33. $this->load->helper(array(
  34. 'security',
  35. 'url',
  36. 'html'
  37. ));
  38. }
  39. public function index()
  40. {
  41. //判別是否已經登入
  42. if ($this->session->userdata('interviewer_logged_in'))
  43. {
  44. //是,進入問卷列表頁。
  45. redirect('front/survey_list', 'reflash');
  46. }
  47. else
  48. {
  49. //否,進入登入頁
  50. $this->login();
  51. }
  52. }
  53. /**
  54. * 登入頁面
  55. * @param
  56. * @return html
  57. */
  58. public function login()
  59. {
  60. if ($this->session->userdata('interviewer_logged_in'))
  61. {
  62. //有登入就進入管理頁預設的 article_list
  63. redirect('front/survey_list', 'reflash');
  64. }
  65. else
  66. {
  67. //如果沒有登入,進入登入頁
  68. //取得預設值
  69. $data = null;
  70. $this->load->view('front/htmlstart');
  71. $this->load->view('front/head', $data);
  72. $this->load->view('front/header');
  73. $this->load->view('front/login', $data);
  74. $this->load->view('front/footer');
  75. $this->load->view('front/htmlend');
  76. }
  77. }
  78. /**
  79. * survey_list()
  80. * 問卷列表頁面,根據登入的使用者取得所屬的問卷。
  81. * @param
  82. * @return html
  83. */
  84. public function survey_list()
  85. {
  86. if ($this->session->userdata('interviewer_logged_in'))
  87. {
  88. //如果有登入,就取得session值
  89. $session = $this->session->all_userdata();
  90. //取得登入者資訊
  91. $data['interviewer'] = array(
  92. 'name'=>$session['username'],
  93. 'account'=>$session['account']
  94. );
  95. //echo $session['guid'];
  96. //取得該訪員的所有問卷
  97. $url = base_url('getSurveyList?guid=' . $session['guid']);
  98. $surveys = json_decode(file_get_contents($url));
  99. //print_r($surveys);
  100. //設定表格
  101. $tmpl = array('table_open'=>'<table class="lists">');
  102. $this->table->set_empty("&nbsp;");
  103. $this->table->set_template($tmpl);
  104. $this->table->set_heading('問卷', '填寫人數', '調查人數', '版本', '發佈日期');
  105. if ($surveys->Header->Status == 0)
  106. {
  107. //表讀取成功 0 有問卷
  108. foreach ($surveys->Body->getSurveyList->MetaData as $row)
  109. {
  110. //取得 填寫人數
  111. $surveyAnswerModel = new surveyanswer_model();
  112. $surveyAnswerAlreadyUploadCount = $surveyAnswerModel->getAlreadyUploadCount($session['guid'], $row->SurveyGuid, $row->Ver);
  113. //取得 調查人數
  114. $sampleLogicModel = new samplelogic_model();
  115. //取得樣本清單
  116. $sampleArray = $sampleLogicModel->getSampleInGroup($row->SurveyGuid, $session['guid']);
  117. //總調查數
  118. $sampleTotal = count($sampleArray);
  119. //print_r($row->SurveyGuid);
  120. $this->table->add_row(array(
  121. 'data'=>anchor("front/sample_list/?sguid=" . $row->SurveyGuid, $row->Title, "class='mask'"),
  122. 'class'=>''
  123. ), array(
  124. 'data'=>$surveyAnswerAlreadyUploadCount,
  125. 'class'=>'text-center'
  126. ), array(
  127. 'data'=>$sampleTotal,
  128. 'class'=>'text-center'
  129. ), array(
  130. 'data'=>$row->Ver,
  131. 'class'=>'text-center'
  132. ), array(
  133. 'data'=>$row->CDateTime,
  134. 'class'=>'text-center'
  135. ));
  136. }
  137. }
  138. else
  139. {
  140. //表讀取失敗 1 沒有問卷
  141. $this->table->add_row(array(
  142. 'data'=>'此訪員尚未有問卷',
  143. 'colspan'=>5
  144. ));
  145. }
  146. //產生表格
  147. $data['list_table'] = $this->table->generate();
  148. $this->table->clear();
  149. //重新給予網頁meta描述
  150. $this->load->view('front/htmlstart');
  151. $this->load->view('front/head', $data);
  152. $this->load->view('front/header', $data);
  153. $this->load->view('front/survey_list', $data);
  154. $this->load->view('front/footer');
  155. $this->load->view('front/htmlend');
  156. }
  157. else
  158. {
  159. //否,進入登入頁
  160. $this->login();
  161. }
  162. }
  163. /**
  164. * sample_list()
  165. * 樣本列表頁面,根據登入的使用者取得選取的問卷。
  166. * @param
  167. * @return html
  168. */
  169. public function sample_list()
  170. {
  171. if ($this->session->userdata('interviewer_logged_in'))
  172. {
  173. //如果有登入,就取得session值
  174. $session = $this->session->all_userdata();
  175. //載入 localstorage jquery plugin
  176. $data['js'] = '<script src="' . base_url("style/front/js/jquery.storageapi.min.js") . '"></script>';
  177. //取得登入者資訊
  178. $data['interviewer'] = array(
  179. 'name'=>$session['username'],
  180. 'account'=>$session['account']
  181. );
  182. //取得目前問卷的資訊
  183. $surveyGuid = $this->input->get_post('sguid', TRUE);
  184. $surveyObject = new survey_model();
  185. $surveyObject->setGuid($surveyGuid);
  186. $surveyObject = $surveyObject->getSurvey($surveyObject);
  187. $result_url = "<a href='".base_url('front/sample_list_result/?sguid='.$surveyGuid)."' style='color:#fff'>」</a>";
  188. $data['survey'] = array(
  189. 'guid'=>$surveyObject->getGuid(),
  190. 'title'=>$surveyObject->getTitle().$result_url,
  191. 'ver'=>$surveyObject->getVersion()
  192. );
  193. //取得該訪員的在該問卷底下的樣本
  194. $url = base_url('getSample?surveyguid=' . $surveyGuid . "&interviewerguid=" . $session['guid']);
  195. //echo $url;
  196. $samples = json_decode(file_get_contents($url));
  197. //print_r($samples);
  198. //設定表格
  199. $tmpl = array('table_open'=>'<table class="lists">');
  200. $this->table->set_empty("&nbsp;");
  201. $this->table->set_template($tmpl);
  202. $this->table->set_heading('樣本', '進度', '已鎖定', '版本', '作答日期', '動作');
  203. if ($samples->Header->Status == 0)
  204. {
  205. //產生物件,並設定值,用在非ajax讀取時,取得進度與作答時間
  206. /*
  207. $frontAnswerObject = new front_answer_model();
  208. $frontAnswerObject->setInterViewerGuid($session['guid']);
  209. $frontAnswerObject->setSurveyGuid($surveyGuid);
  210. $frontAnswerObject->setVersion($surveyObject->getVersion());
  211. //取得所有問卷題目數量
  212. $all = $frontAnswerObject->getSurveyQuestionNumber();*/
  213. //表讀取成功 0 有問卷
  214. foreach ($samples->Body->getSample->Sample as $row)
  215. {
  216. //進度、作答日期 產生 div 與 span 標籤,在讓 front.js去ajax取得值在算出,已將降低網頁讀取時間。
  217. /*
  218. $frontAnswerObject->setSampleGuid($row->SampleGuid);
  219. //取得該樣本的作答狀況
  220. $ProgressAndTime = $frontAnswerObject->getSampleProgressAndTime();
  221. $percent = round(($ProgressAndTime[0] / $all) * 100);
  222. $percent = "width:{$percent};";
  223. $noyet = '';
  224. if($percent<100)
  225. $noyet = 'notyet';
  226. */
  227. //先清空百分比與是否還沒完成,因為要透過front.js ajax動態取得
  228. $percent = "";
  229. $noyet='';
  230. //鎖定
  231. if ($row->Locked)
  232. {
  233. $action = "已鎖定,無法作答";
  234. $locked = "<img src='" . base_url('style/front/images/base/lock.gif') . "'/>";
  235. }
  236. else
  237. {
  238. $action = anchor("front/sample_status/?spguid=" . $row->SampleGuid . "&sguid=" . $surveyGuid, '開始(繼續)作答', "class='btn btn-green startAnswer' data-sguid='{$surveyGuid}'") . nbs(2) . "<a href='javascript:void(0);' class='btn lockSample' data-guid='" . $row->SampleGuid . "'>鎖定</a>";
  239. $locked = '';
  240. }
  241. //print_r($row->SurveyGuid);
  242. $this->table->add_row(array(
  243. 'data'=>$row->Name,
  244. //'data'=>anchor("front/sample_status/?spguid=" . $row->SampleGuid . "&sguid=" . $surveyGuid, $row->Name, "class='startAnswer' data-sguid='{}'"),
  245. 'class'=>'text-center'
  246. ), array(
  247. 'data'=>'<div class="sampleProgress" data-spguid="' . $row->SampleGuid . '" data-sguid="' . $surveyGuid . '" data-ivguid="' . $session['guid'] . '" data-ver="' . $surveyObject->getVersion() . '"><span style="'.$percent.'" class="'.$noyet.'"></span></div>',
  248. 'class'=>'text-center'
  249. ), array(
  250. 'data'=>$locked,
  251. 'class'=>'text-center'
  252. ), array(
  253. 'data'=>$data['survey']['ver'],
  254. 'class'=>'text-center'
  255. ), array(
  256. 'data'=>'',
  257. 'class'=>'text-center doAnswerDate'
  258. ), array(
  259. 'data'=>$action,
  260. 'class'=>'text-center'
  261. ));
  262. }
  263. }
  264. else
  265. {
  266. //表讀取失敗 1 沒有問卷
  267. $this->table->add_row(array(
  268. 'data'=>'此訪員這問卷尚未指定樣本',
  269. 'colspan'=>5
  270. ));
  271. }
  272. //產生表格
  273. $data['list_table'] = $this->table->generate();
  274. $this->table->clear();
  275. //重新給予網頁meta描述
  276. $this->load->view('front/htmlstart');
  277. $this->load->view('front/head', $data);
  278. $this->load->view('front/header', $data);
  279. $this->load->view('front/sample_list', $data);
  280. $this->load->view('front/footer');
  281. $this->load->view('front/htmlend');
  282. //20131122 珂: 因為有開啟資料庫快取,所以有更新或新增樣本答案時,都需清掉前台的樣本快取,這樣user在loader資料才會是最新的
  283. //$this->db->cache_delete('front','sample_list');
  284. }
  285. else
  286. {
  287. //否,進入登入頁
  288. $this->login();
  289. }
  290. }
  291. /**
  292. * sample_list_result()
  293. * 樣本列表頁面,根據登入的使用者取得選取的問卷,顯示目前作答情況。
  294. * @param
  295. * @return html
  296. */
  297. public function sample_list_result()
  298. {
  299. if ($this->session->userdata('interviewer_logged_in'))
  300. {
  301. //如果有登入,就取得session值
  302. $session = $this->session->all_userdata();
  303. //載入 localstorage jquery plugin
  304. $data['js'] = '<script src="' . base_url("style/front/js/jquery.storageapi.min.js") . '"></script>';
  305. //取得登入者資訊
  306. $data['interviewer'] = array(
  307. 'name'=>$session['username'],
  308. 'account'=>$session['account']
  309. );
  310. //取得目前問卷的資訊
  311. $surveyGuid = $this->input->get_post('sguid', TRUE);
  312. $surveyObject = new survey_model();
  313. $surveyObject->setGuid($surveyGuid);
  314. $surveyObject = $surveyObject->getSurvey($surveyObject);
  315. $data['survey'] = array(
  316. 'guid'=>$surveyObject->getGuid(),
  317. 'title'=>$surveyObject->getTitle()."(".$surveyGuid.")",
  318. 'ver'=>$surveyObject->getVersion()
  319. );
  320. $data['list_table'] = '';
  321. $table_name = "survey_answer_{$data['survey']['guid']}_{$data['survey']['ver']}";
  322. //取得該訪員在該問卷的樣本
  323. $query = $this->db->get_where('survey_survey_own_sample',array('interviewerGuid' => $session['guid'], 'surveyGuid'=>$surveyGuid));
  324. if($query->num_rows()>0)
  325. {
  326. $tmpl = array('table_open'=>'<table class="lists results">');
  327. $this->table->set_empty("&nbsp;");
  328. $this->table->set_template($tmpl);
  329. $i = 0;
  330. $noshow = array('id','interviewerGuid','sampleGuid');
  331. foreach($query->result_array() as $row)
  332. {
  333. //根據每一個樣本群組找出樣本
  334. $this->db->order_by('survey_sample.mainpriority ASC, survey_sample.subpriority ASC');
  335. $this->db->select('survey_sample.*');
  336. $this->db->join('survey_sample','survey_sample.guid = survey_samplegroup.sampleGuid','left');
  337. $query = $this->db->get_where('survey_samplegroup',array('survey_samplegroup.guid' => $row['groupSampleGuid']));
  338. if($query->num_rows()>0)
  339. {
  340. foreach ($query->result_array() as $row2) {
  341. //根據每一個樣本取得,已經做的答案
  342. $query = $this->db->get_where($table_name,array('interviewerGuid'=> $session['guid'],'sampleGuid' => $row2['guid']));
  343. unset($header);
  344. unset($rowAnwser);
  345. if($query->num_rows() == 1)
  346. {
  347. $a_sample = $query->row_array();
  348. //如果為0就製作表頭
  349. if($i == 0)
  350. {
  351. $header[] = '樣本名稱';
  352. foreach($a_sample as $key=>$value)
  353. {
  354. if(!in_array($key,$noshow))
  355. {
  356. $header[] = $key;
  357. }
  358. }
  359. $this->table->set_heading($header);
  360. $i++;
  361. }
  362. foreach ($noshow as $removeKey) {
  363. unset($a_sample[$removeKey]);
  364. }
  365. //print_r($row2);
  366. $rowAnwser[] = $row2['name']."<br/>".$row2['guid'];
  367. foreach ($a_sample as $anwsers) {
  368. if($anwsers === null)
  369. $anwsers = '<span style="color:#ddd">NULL</span>';
  370. if($anwsers === '')
  371. $anwsers = '<span style="color:#d2d2d2">跳過</span>';
  372. $rowAnwser[] = $anwsers;
  373. }
  374. $this->table->add_row($rowAnwser);
  375. //echo $row2['name'];
  376. //print_r($query->row_array());
  377. }
  378. else
  379. {
  380. //echo $row2['name']."尚未作答";
  381. }
  382. }
  383. }
  384. }
  385. }
  386. //產生表格
  387. $data['list_table'] = $this->table->generate();
  388. $this->table->clear();
  389. //重新給予網頁meta描述
  390. $this->load->view('front/htmlstart');
  391. $this->load->view('front/head', $data);
  392. $this->load->view('front/header', $data);
  393. $this->load->view('front/sample_list', $data);
  394. $this->load->view('front/footer');
  395. $this->load->view('front/htmlend');
  396. //20131122 珂: 因為有開啟資料庫快取,所以有更新或新增樣本答案時,都需清掉前台的樣本快取,這樣user在loader資料才會是最新的
  397. //$this->db->cache_delete('front','sample_list');
  398. }
  399. else
  400. {
  401. //否,進入登入頁
  402. $this->login();
  403. }
  404. }
  405. /**
  406. * sample_status()
  407. * 樣本狀態,開始做問卷前,會先詢問 繼續訪問、需在訪運、不住原址...等問題
  408. * 當結束樣本時,也會出現此狀態
  409. * @param
  410. * @return html
  411. */
  412. public function sample_status()
  413. {
  414. if ($this->session->userdata('interviewer_logged_in'))
  415. {
  416. //如果有登入,就取得session值
  417. $session = $this->session->all_userdata();
  418. //載入 localstorage jquery plugin
  419. $data['js'] = '';
  420. //取得登入者資訊
  421. $data['interviewer'] = array(
  422. 'name'=>$session['username'],
  423. 'account'=>$session['account']
  424. );
  425. $sampleGuid = $this->input->get_post('spguid', TRUE);
  426. $surveyGuid = $this->input->get_post('sguid', TRUE);
  427. //取得目前問卷的資訊
  428. $surveyObject = new survey_model();
  429. $surveyObject->setGuid($surveyGuid);
  430. $surveyObject = $surveyObject->getSurvey($surveyObject);
  431. $data['survey'] = array(
  432. 'guid'=>$surveyObject->getGuid(),
  433. 'title'=>$surveyObject->getTitle(),
  434. 'ver'=>$surveyObject->getVersion()
  435. );
  436. //取得該樣本的資訊
  437. $url = base_url('getSample?sampleguid=' . $sampleGuid);
  438. //echo $url;
  439. $sample = json_decode(file_get_contents($url));
  440. $data['sample'] = array(
  441. 'guid'=>$sampleGuid,
  442. 'name'=>$sample->Body->getSample->Name,
  443. );
  444. //將問卷相關guid存入session中
  445. $guids = array(
  446. 'surveyGuid'=>$surveyGuid,
  447. 'ver'=>$surveyObject->getVersion(),
  448. 'sampleGuid'=>$sampleGuid
  449. );
  450. $this->session->set_userdata($guids);
  451. //載入 front_answer_model
  452. $front_answer = new front_answer_model();
  453. //取得該樣本的開始或停止的問題內容,並產生 ul 以列印出
  454. $situation = $front_answer->getStatusQuestions();
  455. $data['situation'] = '<ul class="situation">';
  456. foreach ($situation as $key=>$row)
  457. {
  458. if ($key == 0)
  459. {
  460. $data['situation'] .= "<li>" . anchor("front/answering/", $row['title'], " class='intoAnswer' data-code='{$row['code']}' data-sguid='{$surveyGuid}'") . "</li>";
  461. }
  462. else
  463. {
  464. $data['situation'] .= "<li><a href='javascript:void(0);' class='dropdown' data-code='{$row['code']}'>{$row['title']}</a>";
  465. $options = array();
  466. foreach ($row['options'] as $val)
  467. {
  468. if ($val['code'] == 23 || $val['code'] == 410)
  469. {
  470. $options[] = "{$val['title']}:<input type='text' class='situationComment{$val['code']} width50' placeholder='請填入原因'/> <button type='button' class='stopSurvey' data-code='{$val['code']}'>確定並離開</button>";
  471. }
  472. else
  473. {
  474. $options[] = "<a href='javascript:void(0);' class='stopSurvey' data-code='{$val['code']}'>{$val['title']}</a>";
  475. }
  476. }
  477. $attributes = array(
  478. 'class'=>'',
  479. 'id'=>''
  480. );
  481. $data['situation'] .= ul($options, $attributes) . "</li>";
  482. }
  483. }
  484. $data['situation'] .= '</ul>';
  485. /*
  486. $data['situation'] = ul($options,$attributes);
  487. */
  488. //重新給予網頁meta描述
  489. $this->load->view('front/htmlstart');
  490. $this->load->view('front/head', $data);
  491. $this->load->view('front/answer_header', $data);
  492. $this->load->view('front/answering', $data);
  493. $this->load->view('front/footer');
  494. $this->load->view('front/htmlend');
  495. }
  496. else
  497. {
  498. //否,進入登入頁
  499. $this->login();
  500. }
  501. }
  502. /**
  503. * answering()
  504. * 完成受訪詢問後,進入作答畫面。
  505. * @param
  506. * @return html
  507. */
  508. public function answering()
  509. {
  510. if ($this->session->userdata('interviewer_logged_in'))
  511. {
  512. //如果有登入,就取得session值
  513. $session = $this->session->all_userdata();
  514. //載入 localstorage jquery plugin
  515. $data['js'] = '<script src="' . base_url("style/front/js/jquery.storageapi.min.js") . '"></script>
  516. <script src="' . base_url("style/front/js/jquery.twzipcode.min.js") . '"></script>';
  517. //取得登入者資訊
  518. $data['interviewer'] = array(
  519. 'name'=>$session['username'],
  520. 'account'=>$session['account']
  521. );
  522. //print_r($session);
  523. $sampleGuid = $session['sampleGuid'];
  524. $surveyGuid = $session['surveyGuid'];
  525. //取得目前問卷的資訊
  526. $surveyObject = new survey_model();
  527. $surveyObject->setGuid($surveyGuid);
  528. $surveyObject = $surveyObject->getSurvey($surveyObject);
  529. $data['survey'] = array(
  530. 'guid'=>$surveyObject->getGuid(),
  531. 'title'=>$surveyObject->getTitle(),
  532. 'ver'=>$surveyObject->getVersion(),
  533. 'greetingText'=>$surveyObject->getGreetingText(),
  534. 'thankText'=>$surveyObject->getThankText()
  535. );
  536. //取得該樣本的資訊
  537. $url = base_url('getSample?sampleguid=' . $sampleGuid);
  538. //echo $url;
  539. $sample = json_decode(file_get_contents($url));
  540. $data['sample'] = array(
  541. 'guid'=>$sampleGuid,
  542. 'name'=>$sample->Body->getSample->Name
  543. );
  544. //重新給予網頁meta描述
  545. $this->load->view('front/htmlstart');
  546. $this->load->view('front/head', $data);
  547. $this->load->view('front/answer_header', $data);
  548. $this->load->view('front/answering', $data);
  549. $this->load->view('front/footer');
  550. $this->load->view('front/htmlend');
  551. }
  552. else
  553. {
  554. //否,進入登入頁
  555. $this->login();
  556. }
  557. }
  558. /**
  559. * updateStartTime()
  560. * 更新問卷開始時間
  561. * @param
  562. * @return json
  563. */
  564. public function updateSurveyStartTime()
  565. {
  566. //取得session值
  567. $session = $this->session->all_userdata();
  568. //產生物件,設定值後
  569. $frontAnswerObject = new front_answer_model();
  570. $frontAnswerObject->setInterViewerGuid($session['guid']);
  571. $frontAnswerObject->setSampleGuid($session['sampleGuid']);
  572. $frontAnswerObject->setSurveyGuid($session['surveyGuid']);
  573. $frontAnswerObject->saveSurveyTime('update');
  574. $json = array(
  575. 'result'=>TRUE,
  576. 'msg'=>'更新成功'
  577. );
  578. //輸出json格式
  579. $this->output->set_content_type('application/json')->set_output(json_encode($json));
  580. }
  581. /**
  582. * uploadSurveySituation()
  583. * 上傳問卷狀態,並判別開始或結束時間,以記錄
  584. * @param
  585. * @return html
  586. */
  587. public function uploadSurveySituation()
  588. {
  589. $code = $this->input->post('c', TRUE);
  590. $comment = $this->input->post('m', TRUE);
  591. //取得session值
  592. $session = $this->session->all_userdata();
  593. //產生物件,設定值後
  594. $frontAnswerObject = new front_answer_model();
  595. $frontAnswerObject->setInterViewerGuid($session['guid']);
  596. $frontAnswerObject->setSampleGuid($session['sampleGuid']);
  597. $frontAnswerObject->setSurveyGuid($session['surveyGuid']);
  598. $frontAnswerObject->setSituationCode($code);
  599. $frontAnswerObject->setSituationComment($comment);
  600. //儲存情況
  601. if ($frontAnswerObject->saveSituation())
  602. {
  603. //如果狀態儲存成功,就繼續儲存開始或結束時間
  604. if ($code == 1)
  605. {
  606. //如果 code 為 1 則為開始做問卷,記錄開始的時間與狀態
  607. //如果沒有已經有開始的時間id,就進行開始時間的儲存。有時間記錄,就不管,繼續作答
  608. if (!$this->session->userdata('surveyTimeID'))
  609. {
  610. $frontAnswerObject->saveSurveyTime('start');
  611. }
  612. //開始狀態與時間儲存完後,進入作答頁面
  613. $json['redirect'] = base_url('front/answering');
  614. }
  615. else
  616. {
  617. //done就是完成問卷
  618. //如果不是1,若已經開始,就記錄結束時間,沒有開始就不記錄。
  619. if ($this->session->userdata('surveyTimeID'))
  620. {
  621. $frontAnswerObject->saveSurveyTime('end');
  622. }
  623. //離開狀態與時間儲存完後,回到樣本列表
  624. $json['redirect'] = base_url('front/sample_list/?sguid=' . $session['surveyGuid']);
  625. }
  626. $json['result'] = TRUE;
  627. $json['msg'] = '狀態記錄成功';
  628. }
  629. else
  630. {
  631. $json = array(
  632. 'result'=>FALSE,
  633. 'msg'=>'狀態儲存失敗'
  634. );
  635. }
  636. //輸出json格式
  637. $this->output->set_content_type('application/json')->set_output(json_encode($json));
  638. }
  639. /**
  640. * lockSample()
  641. * 上傳問卷狀態,並判別開始或結束時間,以記錄
  642. * @param
  643. * @return html
  644. */
  645. public function lockSample()
  646. {
  647. //取得樣本id
  648. $sampleGuid = $this->input->post('s');
  649. if ($sampleGuid)
  650. {
  651. //產生物件,設定值後
  652. $frontAnswerObject = new front_answer_model();
  653. $frontAnswerObject->setSampleGuid($sampleGuid);
  654. if ($frontAnswerObject->changeLockSample(1))
  655. {
  656. $json = array(
  657. 'result'=>TRUE,
  658. 'msg'=>'鎖定成功'
  659. );
  660. }
  661. else
  662. {
  663. $json = array(
  664. 'result'=>FALSE,
  665. 'msg'=>'鎖定失敗,資料庫有問題'
  666. );
  667. }
  668. }
  669. else
  670. {
  671. $json = array(
  672. 'result'=>FALSE,
  673. 'msg'=>'鎖定失敗,沒有樣本id'
  674. );
  675. }
  676. //輸出json格式
  677. $this->output->set_content_type('application/json')->set_output(json_encode($json));
  678. }
  679. /**
  680. * getSampleProgress()
  681. * 取得樣本的進度
  682. * @param
  683. * @return html
  684. */
  685. public function getSampleProgress()
  686. {
  687. //取得post值
  688. $posts = $this->input->post(NULL, TRUE);
  689. //產生物件,並設定值
  690. $frontAnswerObject = new front_answer_model();
  691. $frontAnswerObject->setInterViewerGuid($posts['iv']);
  692. $frontAnswerObject->setSampleGuid($posts['sp']);
  693. $frontAnswerObject->setSurveyGuid($posts['s']);
  694. $frontAnswerObject->setVersion($posts['v']);
  695. //取得所有問卷題目數量
  696. $all = $frontAnswerObject->getSurveyQuestionNumber();
  697. //取得該樣本的作答狀況
  698. $ProgressAndTime = $frontAnswerObject->getSampleProgressAndTime();
  699. //回傳計算後的百分比。
  700. if($ProgressAndTime[0])
  701. {
  702. $json = array(
  703. 'result'=>TRUE,
  704. 'percentage'=>round(($ProgressAndTime[0] / $all) * 100),
  705. 'cDateTime'=>$ProgressAndTime[1]
  706. );
  707. }
  708. else
  709. {
  710. $json = array(
  711. 'result'=>TRUE,
  712. 'percentage'=>0,
  713. 'cDateTime'=>''
  714. );
  715. }
  716. //輸出json格式
  717. $this->output->set_content_type('application/json')->set_output(json_encode($json));
  718. }
  719. /**
  720. * checkCurrentQuestion()
  721. * 檢查此樣本目前做到哪一題題目
  722. * @param
  723. * @return html
  724. */
  725. public function checkSampleCurrentQuestion()
  726. {
  727. //取得session值
  728. $session = $this->session->all_userdata();
  729. //產生物件,設定值後
  730. $frontAnswerObject = new front_answer_model();
  731. $frontAnswerObject->setInterViewerGuid($session['guid']);
  732. $frontAnswerObject->setSampleGuid($session['sampleGuid']);
  733. $frontAnswerObject->setSurveyGuid($session['surveyGuid']);
  734. $frontAnswerObject->setVersion($session['ver']);
  735. //因為localstore記錄的index從0開始,所以結果要少1
  736. $subjectArrayIndex = $frontAnswerObject->checkOnWhichQuestion() - 1;
  737. if($subjectArrayIndex<0)
  738. $subjectArrayIndex = 0;
  739. //回傳計算後的百分比。
  740. $json = array(
  741. 'result'=>TRUE,
  742. 'subjectArrayIndex'=>$subjectArrayIndex,
  743. 'msg'=>'讀取成功'
  744. );
  745. //輸出json格式
  746. $this->output->set_content_type('application/json')->set_output(json_encode($json));
  747. }
  748. /**
  749. * checkHaveSelect()
  750. * 檢查此題目是否已經有選答案了
  751. * @param
  752. * @return html
  753. */
  754. public function checkHaveSelect()
  755. {
  756. //取得session值
  757. $session = $this->session->all_userdata();
  758. //產生物件,設定值後
  759. $frontAnswerObject = new front_answer_model();
  760. $frontAnswerObject->setInterViewerGuid($session['guid']);
  761. $frontAnswerObject->setSampleGuid($session['sampleGuid']);
  762. $frontAnswerObject->setSurveyGuid($session['surveyGuid']);
  763. $frontAnswerObject->setVersion($session['ver']);
  764. //取得是否有答案
  765. $value = $frontAnswerObject->checkHaveValue((string)$this->input->post('sn',TRUE));
  766. //回傳計算後的百分比。
  767. $json = array(
  768. 'result'=>TRUE,
  769. 'value'=>$value,
  770. 'msg'=>'讀取成功'
  771. );
  772. //輸出json格式
  773. $this->output->set_content_type('application/json')->set_output(json_encode($json));
  774. }
  775. /**
  776. * getGroupPassAnswer()
  777. * 取得群組跳題的答案
  778. * @param
  779. * @return josn
  780. */
  781. public function getGroupPassAnswer()
  782. {
  783. //取得session值
  784. $session = $this->session->all_userdata();
  785. //產生物件,設定值後
  786. $frontAnswerObject = new front_answer_model();
  787. $frontAnswerObject->setInterViewerGuid($session['guid']);
  788. $frontAnswerObject->setSampleGuid($session['sampleGuid']);
  789. $frontAnswerObject->setSurveyGuid($session['surveyGuid']);
  790. $frontAnswerObject->setVersion($session['ver']);
  791. //取得是否有答案
  792. $anwser = $frontAnswerObject->gerQuestionAnswer($this->input->post('n',TRUE));
  793. //回傳計算後的百分比。
  794. $json = array(
  795. 'result'=>TRUE,
  796. 'anwser'=>$anwser,
  797. 'msg'=>'讀取成功'
  798. );
  799. //輸出json格式
  800. $this->output->set_content_type('application/json')->set_output(json_encode($json));
  801. }
  802. /**
  803. * creatAnswerTable()
  804. * 建立答案表格
  805. * @param
  806. * @return josn
  807. */
  808. public function creatAnswerTable()
  809. {
  810. //取得session值
  811. $session = $this->session->all_userdata();
  812. //產生物件,設定值後
  813. $frontAnswerObject = new front_answer_model();
  814. $frontAnswerObject->setInterViewerGuid($session['guid']);
  815. $frontAnswerObject->setSampleGuid($session['sampleGuid']);
  816. $frontAnswerObject->setSurveyGuid($session['surveyGuid']);
  817. $frontAnswerObject->setVersion($session['ver']);
  818. //檢查有無表格,沒有就建立
  819. if($frontAnswerObject->creatAnswerTable($this->input->post('i',TRUE))){
  820. //回傳儲存結果
  821. $json = array(
  822. 'result'=>TRUE,
  823. 'msg'=>'檢查與建立完畢'
  824. );
  825. }
  826. //輸出json格式
  827. $this->output->set_content_type('application/json')->set_output(json_encode($json));
  828. }
  829. /**
  830. * saveAnswer()
  831. * 儲存答案
  832. * @param
  833. * @return josn
  834. */
  835. public function saveAnswer()
  836. {
  837. //取得session值
  838. $session = $this->session->all_userdata();
  839. //產生物件,設定值後
  840. $frontAnswerObject = new front_answer_model();
  841. $frontAnswerObject->setInterViewerGuid($session['guid']);
  842. $frontAnswerObject->setSampleGuid($session['sampleGuid']);
  843. $frontAnswerObject->setSurveyGuid($session['surveyGuid']);
  844. $frontAnswerObject->setVersion($session['ver']);
  845. //儲存答案
  846. $anwser = $frontAnswerObject->saveAnswer($this->input->post('n'), $this->input->post('a'));
  847. //回傳儲存結果
  848. $json = array(
  849. 'result'=>TRUE,
  850. 'anwser'=>$this->input->post('n')." => ".join("," ,$this->input->post('a')),
  851. 'msg'=>$anwser
  852. );
  853. //輸出json格式
  854. $this->output->set_content_type('application/json')->set_output(json_encode($json));
  855. }
  856. /**
  857. * saveAnswer()
  858. * 取得之前題目的答案,作為標題置換用
  859. * @param
  860. * @return josn
  861. */
  862. public function getPrevAnswer()
  863. {
  864. //取得session值
  865. $session = $this->session->all_userdata();
  866. //產生物件,設定值後
  867. $frontAnswerObject = new front_answer_model();
  868. $frontAnswerObject->setInterViewerGuid($session['guid']);
  869. $frontAnswerObject->setSampleGuid($session['sampleGuid']);
  870. $frontAnswerObject->setSurveyGuid($session['surveyGuid']);
  871. $frontAnswerObject->setVersion($session['ver']);
  872. //取得該答案
  873. $getAnwser = $frontAnswerObject->getPrevAnswer($this->input->post('sn',TRUE));
  874. //回傳儲存結果
  875. $json = array(
  876. 'result'=>TRUE,
  877. 'anwser'=>$getAnwser,
  878. 'msg'=>'讀取成功'
  879. );
  880. //輸出json格式
  881. $this->output->set_content_type('application/json')->set_output(json_encode($json));
  882. }
  883. /**
  884. * clearJumpAnswer()
  885. * 清圖
  886. * @param
  887. * @return josn
  888. */
  889. public function clearJumpAnswer()
  890. {
  891. //取得session值
  892. $session = $this->session->all_userdata();
  893. //產生物件,設定值後
  894. $frontAnswerObject = new front_answer_model();
  895. $frontAnswerObject->setInterViewerGuid($session['guid']);
  896. $frontAnswerObject->setSampleGuid($session['sampleGuid']);
  897. $frontAnswerObject->setSurveyGuid($session['surveyGuid']);
  898. $frontAnswerObject->setVersion($session['ver']);
  899. //取得該答案
  900. $result = $frontAnswerObject->clearJumpAnswer($this->input->post('c',TRUE));
  901. //回傳儲存結果
  902. $json = array(
  903. 'result'=>TRUE,
  904. 'msg'=>$result
  905. );
  906. //輸出json格式
  907. $this->output->set_content_type('application/json')->set_output(json_encode($json));
  908. }
  909. /**
  910. * cleanSampleRecode()
  911. * 清圖
  912. * @param
  913. * @return josn
  914. */
  915. public function cleanSampleRecode($sampleGUID)
  916. {
  917. $this->db->delete('survey_surveysituation',array('sampleGuid' => $sampleGUID));
  918. $this->db->delete('survey_surveytime',array('sampleGuid' => $sampleGUID));
  919. $json = array('msg'=>$sampleGUID .' 樣本的狀態與時間已刪除,需在刪除survey_answer...的資料');
  920. //輸出json格式
  921. //$this->output->set_content_type('application/json')->set_output(json_encode($json));
  922. }
  923. /**
  924. * cleanSurveyData()
  925. * 清除問卷底下的資料,包含其樣本的相關記錄
  926. * @param
  927. * @return josn
  928. */
  929. public function cleanSurveyData($surveyGUID,$ver)
  930. {
  931. $table = "survey_answer_{$surveyGUID}_{$ver}";
  932. if($this->db->table_exists($table))
  933. {
  934. //取得該問卷底下的所有樣本
  935. $query = $this->db->get($table);
  936. if($query->num_rows()>0)
  937. {
  938. foreach ($query->result_array() as $row) {
  939. //清除該樣本資料
  940. $this->cleanSampleRecode($row['sampleGuid']);
  941. }
  942. //最後清除該表格中資料
  943. $this->load->dbforge();
  944. $this->dbforge->drop_table($table);
  945. $json = array('msg'=>$surveyGUID."_".$ver .' 的樣本已刪除。 All sample has delete.');
  946. }
  947. else
  948. {
  949. //最後清除該表格中資料
  950. $this->load->dbforge();
  951. $this->dbforge->drop_table($table);
  952. $json = array('msg'=>$surveyGUID."_".$ver .' 資料表中沒有作答記錄,已刪除表格');
  953. }
  954. //以及清除快取
  955. $this->db->cache_delete('front','getSampleProgress');
  956. }
  957. else
  958. {
  959. $json = array('msg'=>$surveyGUID."_".$ver .' 資料表不存在 無作答記錄。');
  960. }
  961. //輸出json格式
  962. $this->output->set_content_type('application/json')->set_output(json_encode($json));
  963. }
  964. public function removehtml(){
  965. $query = $this->db->get_where('survey_option',array('surveyGuid' => 'feeb24ec9d36780895de11ee8b1ba38f'));
  966. foreach ($query->result_array() as $row) {
  967. $removedTitle = str_replace(array("<br>","<br/>","<b>","</b>"), '',$row['title']);
  968. //$this->db->where('id',$row['id']);
  969. //$this->db->update('survey_option',array('title'=>$removedTitle));
  970. //echo htmlspecialchars($row['title'])." = ".$removedTitle;
  971. //echo "<hr>";
  972. }
  973. $query = $this->db->get_where('survey_question',array('surveyGuid' => 'feeb24ec9d36780895de11ee8b1ba38f'));
  974. foreach ($query->result_array() as $row) {
  975. $removedSubject = str_replace(array("<br>","<br/>","<b>","</b>"), '', $row['subject']);
  976. //$this->db->where('id',$row['id']);
  977. //$this->db->update('survey_question',array('subject'=>$removedSubject));
  978. //echo htmlspecialchars($row['subject'])." = ".$removedSubject;
  979. //echo "<hr>";
  980. }
  981. }
  982. /**
  983. * 給取的主機的作業系統
  984. * getServerOS()
  985. *
  986. */
  987. function getServerOS()
  988. {
  989. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
  990. {
  991. return 'win';
  992. }
  993. else
  994. {
  995. return 'linux';
  996. }
  997. }
  998. public function db_backup()
  999. {
  1000. //修改php.ini 設定
  1001. ini_set('memory_limit', '10240M');
  1002. ini_set('max_execution_time', 0);
  1003. //檔名設定
  1004. $db_name = 'backup_on_'.date("Y_m_d").".sql.gz";
  1005. $del_name = 'backup_on_'.date("Y_m_d",strtotime("-29 day")).".sql.gz";
  1006. //要上傳的資料夾
  1007. $ftp_backup_folder = 'capi.nctu.edu.tw';
  1008. if($this->getServerOS() == 'linux')
  1009. {
  1010. $local_backup_folder = str_replace('application/controllers', '', dirname(__FILE__)).'db_backup/';
  1011. }
  1012. else
  1013. {
  1014. $local_backup_folder = str_replace('application\controllers', '', dirname(__FILE__)).'db_backup\\';
  1015. }
  1016. $save = $local_backup_folder.$db_name;
  1017. //開始使用資料庫工具
  1018. $this->load->dbutil();
  1019. //備份設定並建立備份檔
  1020. $prefs = array('format'=>'gzip',
  1021. 'filename'=>'capi_db_backup.sql',
  1022. 'foreign_key_checks' => FALSE);
  1023. $backup = $this->dbutil->backup($prefs);
  1024. //將備份資料庫存入本機端
  1025. $this->load->helper('file');
  1026. write_file($save, $backup);
  1027. //檢查本機端是否有超過28天以上的檔案,有就刪除
  1028. $locallist = scandir($local_backup_folder);
  1029. if(in_array($del_name,$locallist)){
  1030. unlink($local_backup_folder.$del_name);
  1031. }
  1032. //上傳ftp
  1033. $this->load->library('ftp');
  1034. $config['hostname'] = 'workshop.sweea.com';
  1035. $config['username'] = 'db_backup';
  1036. $config['password'] = 'ej03xu3admin';
  1037. $config['port'] = 2121;
  1038. $config['passive'] = true; //非預設的21port 就要啟用被動式的 21port 就用false
  1039. $config['debug'] = TRUE;
  1040. $this->ftp->connect($config);
  1041. //檢查有無備份的資料夾,沒有就新增
  1042. $list = $this->ftp->list_files('/');
  1043. if(!in_array("/".$ftp_backup_folder,$list)){
  1044. $this->ftp->mkdir('/'.$ftp_backup_folder,DIR_WRITE_MODE);
  1045. }
  1046. //檢查有無超過28天以上的檔案,有就刪除
  1047. $list = $this->ftp->list_files('/'.$ftp_backup_folder);
  1048. if(in_array($del_name,$list)){
  1049. $this->ftp->delete_file("/{$ftp_backup_folder}/{$del_name}");
  1050. }
  1051. $this->ftp->upload($save,"/{$ftp_backup_folder}/".$db_name,'auto',0755);
  1052. $this->ftp->close();
  1053. $url = 'http://workshop.sweea.com/case_backup/index.php';
  1054. $data = array(
  1055. "note" => "{$ftp_backup_folder}/{$db_name}",
  1056. "domain" => $_SERVER['HTTP_HOST'],
  1057. "status" => "upload done"
  1058. );
  1059. $ch = curl_init();
  1060. curl_setopt($ch, CURLOPT_URL, $url);
  1061. curl_setopt($ch, CURLOPT_POST, true); // 啟用POST
  1062. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query( $data ));
  1063. curl_exec($ch);
  1064. curl_close($ch);
  1065. //立即下載
  1066. //$this->load->helper('download');
  1067. //force_download($db_name, $backup);
  1068. //echo 'done <a href="http://140.113.53.105/db_backup/'.$db_name.'">download '.$db_name.'</a>';
  1069. //完成後發信給我
  1070. //寄發新密碼
  1071. /*
  1072. $message = "
  1073. <body style='background-color:#eee'><p>&nbsp;</p>
  1074. <div style='width:500px;margin:0 auto;padding:10px;color:#454545;font-size:10pt;background:#fff;border-bottom:#5d1e0b solid 1px;border-left:#5d1e0b solid 1px;border-right:#5d1e0b solid 1px;'>
  1075. <br />
  1076. <p>hi 管理者 您好!</p>
  1077. <p>
  1078. 多多系統 {$db_name} 資料庫已備份完畢
  1079. </p>
  1080. </div>
  1081. </body>";
  1082. $subject = "多多".date("Y/m/d").'的資料庫備份通知。';
  1083. //載入發信模組
  1084. $this->load->model('sendmail_m');
  1085. $send = $this->sendmail_m->sendmail('mktsai@sweea.com', $subject, $message);
  1086. if ($send)
  1087. {
  1088. echo '備份完畢,並發送通知給管理者';
  1089. }
  1090. else
  1091. {
  1092. echo '備份完畢,但信件發送失敗。';
  1093. }*/
  1094. }
  1095. function test()
  1096. {
  1097. echo "<meta charset='utf-8'>";
  1098. /*
  1099. echo "問卷題目";
  1100. $this->db->order_by('priority','ASC');
  1101. $query = $this->db->get_where('survey_question',array('surveyGuid' => '591c4bd60fc60256b3a2a0ace24d577f'));
  1102. $result = $query->result_array();
  1103. $keys = 0;
  1104. $subjectNumber = array();
  1105. foreach ($result as $key => $value) {
  1106. $keys++;
  1107. echo $value['subjectNumber']."<br/>";
  1108. $subjectNumber[] = $value['subjectNumber'];
  1109. }
  1110. echo '$keys='.$keys;
  1111. echo "<hr>";
  1112. $query = $this->db->get_where('survey_answer_591c4bd60fc60256b3a2a0ace24d577f_1',array('id' =>2 ));
  1113. $keys = 0;
  1114. echo "已作答問卷題目";
  1115. $result = $query->row_array();
  1116. $i = 0;
  1117. foreach ($result as $key => $value) {
  1118. if($i > 3)
  1119. {
  1120. $keys++;
  1121. echo $key."=>".$value."<br/>";
  1122. if(!in_array($key,$subjectNumber))
  1123. {
  1124. echo "<span style='color:red;'>".$key . " 不存在目前的題目當中</span><br/>";
  1125. }
  1126. }
  1127. $i++;
  1128. }
  1129. echo '$keys='.$keys;
  1130. */
  1131. $interviwer = array("TTR004","TTR025","TTR026","TTR027","TTR028","TTR029","TTR030","TTR031","TTR032","TTR033","TTR034","TTR035","TTR036","TTR037","TTR038","TTR039","TTR040","TTR050");
  1132. foreach($interviwer as $row)
  1133. {
  1134. $query = $this->db->get_where('survey_interviewer',array('name' => $row));
  1135. if($query->num_rows() > 0)
  1136. {
  1137. $a_interviewer = $query->row_array();
  1138. //刪除 survey_interviewer_own_surveys
  1139. $query = $this->db->get_where('survey_interviewer_own_surveys',array('interviewerGuid' => $a_interviewer['guid'],'surveyGuid'=>''));
  1140. if($query->num_rows() > 0)
  1141. {
  1142. foreach($query->result_array() as $row2)
  1143. {
  1144. //$this->db->delete('survey_interviewer_own_surveys',array('id' => $row2['id']));
  1145. echo "需要刪除的:";
  1146. echo $row2['id'];
  1147. echo "<hr>";
  1148. }
  1149. }
  1150. //刪除 survey_survey_own_sample
  1151. $query = $this->db->get_where('survey_survey_own_sample',array('surveyGuid'=>'', 'interviewerGuid' => $a_interviewer['guid']));
  1152. if($query->num_rows() > 0)
  1153. {
  1154. foreach($query->result_array() as $row2)
  1155. {
  1156. //$this->db->delete('survey_survey_own_sample',array('id' => $row2['id']));
  1157. echo "需要刪除的:";
  1158. echo $row2['id'].":".$row2['groupSampleGuid'];
  1159. echo "<hr>";
  1160. //刪除 樣本與樣本群組
  1161. // $this->db->delete('survey_samplegroup_data',array('sampleGroupGuid' => $row2['groupSampleGuid']));
  1162. //$this->db->delete('survey_samplegroup',array('guid' => $row2['groupSampleGuid']));
  1163. }
  1164. }
  1165. }
  1166. }
  1167. }
  1168. }
  1169. /* End of file welcome.php */
  1170. /* Location: ./application/controllers/welcome.php */