nanogong.lib.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. *
  5. * Files are saved in the path:
  6. *
  7. * courses/XXX/exercises/(session_id)/(exercise_id)/(question_id)/(user_id)/
  8. *
  9. * The file name is composed with
  10. *
  11. * (course_id)/(session_id)/(user_id)/(exercise_id)/(question_id)/(exe_id).wav|mp3|ogg
  12. *
  13. *
  14. */
  15. class Nanogong
  16. {
  17. public $filename;
  18. public $store_filename;
  19. public $store_path;
  20. public $params;
  21. public $can_edit = false;
  22. /* Files allowed to upload */
  23. public $available_extensions = array('mp3', 'wav', 'ogg');
  24. /**
  25. * @param array $params
  26. */
  27. public function __construct($params = array())
  28. {
  29. $this->set_parameters($params);
  30. }
  31. /**
  32. * @return bool
  33. */
  34. public function create_user_folder()
  35. {
  36. //COURSE123/exercises/session_id/exercise_id/question_id/user_id
  37. if (empty($this->store_path)) {
  38. return false;
  39. }
  40. //@todo use an array to create folders
  41. $folders_to_create = array();
  42. // Trying to create the courses/COURSE123/exercises/ dir just in case.
  43. $directoryPermissions = api_get_permissions_for_new_directories();
  44. if (!is_dir($this->store_path)) {
  45. mkdir($this->store_path, $directoryPermissions);
  46. }
  47. if (!is_dir($this->store_path.$this->session_id)) {
  48. mkdir($this->store_path.$this->session_id, $directoryPermissions);
  49. }
  50. if (!is_dir($this->store_path.$this->session_id.'/'.$this->exercise_id)) {
  51. mkdir($this->store_path.$this->session_id.'/'.$this->exercise_id, $directoryPermissions);
  52. }
  53. if (!is_dir($this->store_path.$this->session_id.'/'.$this->exercise_id.'/'.$this->question_id)
  54. ) {
  55. mkdir($this->store_path.$this->session_id.'/'.$this->exercise_id.'/'.$this->question_id, $directoryPermissions);
  56. }
  57. if (!empty($this->user_id) &&
  58. !is_dir($this->store_path.$this->session_id.'/'.$this->exercise_id.'/'.$this->question_id.'/'.$this->user_id)
  59. ) {
  60. mkdir($this->store_path.$this->session_id.'/'.$this->exercise_id.'/'.$this->question_id.'/'.$this->user_id, $directoryPermissions);
  61. }
  62. }
  63. /**
  64. * Setting parameters: course id, session id, etc
  65. * @param array
  66. */
  67. public function set_parameters($params = array())
  68. {
  69. // Setting course id
  70. if (isset($params['course_id'])) {
  71. $this->course_id = intval($params['course_id']);
  72. } else {
  73. $this->course_id = $params['course_id'] = api_get_course_int_id();
  74. }
  75. //Setting course info
  76. if (isset($this->course_id)) {
  77. $this->course_info = api_get_course_info_by_id($this->course_id);
  78. }
  79. //Setting session id
  80. if (isset($params['session_id'])) {
  81. $this->session_id = intval($params['session_id']);
  82. } else {
  83. $this->session_id = $params['session_id'] = api_get_session_id();
  84. }
  85. //Setting user ids
  86. if (isset($params['user_id'])) {
  87. $this->user_id = intval($params['user_id']);
  88. } else {
  89. $this->user_id = $params['user_id'] = api_get_user_id();
  90. }
  91. //Setting user ids
  92. if (isset($params['exercise_id'])) {
  93. $this->exercise_id = intval($params['exercise_id']);
  94. } else {
  95. $this->exercise_id = 0;
  96. }
  97. //Setting user ids
  98. if (isset($params['question_id'])) {
  99. $this->question_id = intval($params['question_id']);
  100. } else {
  101. $this->question_id = 0;
  102. }
  103. $this->can_edit = false;
  104. if (api_is_allowed_to_edit()) {
  105. $this->can_edit = true;
  106. } else {
  107. if ($this->user_id == api_get_user_id()) {
  108. $this->can_edit = true;
  109. }
  110. }
  111. //Settings the params array
  112. $this->params = $params;
  113. $this->store_path = api_get_path(SYS_COURSE_PATH).$this->course_info['path'].'/exercises/';
  114. $this->create_user_folder();
  115. $this->store_path = $this->store_path.implode('/', array($this->session_id, $this->exercise_id, $this->question_id, $this->user_id)).'/';
  116. $this->filename = $this->generate_filename();
  117. $this->store_filename = $this->store_path.$this->filename;
  118. }
  119. /**
  120. * Generates the filename with the next format:
  121. * (course_id)/(session_id)/(user_id)/(exercise_id)/(question_id)/(exe_id)
  122. *
  123. * @return string
  124. */
  125. public function generate_filename()
  126. {
  127. if (!empty($this->params)) {
  128. //filename
  129. //course_id/session_id/user_id/exercise_id/question_id/exe_id
  130. $filename_array = array(
  131. $this->params['course_id'],
  132. $this->params['session_id'],
  133. $this->params['user_id'],
  134. $this->params['exercise_id'],
  135. $this->params['question_id'],
  136. $this->params['exe_id']
  137. );
  138. return implode('-', $filename_array);
  139. } else {
  140. return api_get_unique_id();
  141. }
  142. }
  143. /**
  144. * Delete audio file
  145. * @return int
  146. */
  147. public function delete_files()
  148. {
  149. $delete_found = 0;
  150. if ($this->can_edit) {
  151. $file = $this->load_filename_if_exists();
  152. $path_info = pathinfo($file);
  153. foreach ($this->available_extensions as $extension) {
  154. $file_to_delete = $path_info['dirname'].'/'.$path_info['filename'].'.'.$extension;
  155. if (is_file($file_to_delete)) {
  156. unlink($file_to_delete);
  157. $delete_found = 1;
  158. }
  159. }
  160. }
  161. return $delete_found;
  162. }
  163. /**
  164. *
  165. * Tricky stuff to deal with the feedback = 0 in exercises (all question per page)
  166. * @param int $exe_id
  167. */
  168. public function replace_with_real_exe($exe_id)
  169. {
  170. $filename = null;
  171. //@ugly fix
  172. foreach($this->available_extensions as $extension) {
  173. $items = explode('-', $this->filename);
  174. $items[5] = 'temp_exe';
  175. $filename = implode('-', $items);
  176. if (is_file($this->store_path.$filename.'.'.$extension)) {
  177. $old_name = $this->store_path.$filename.'.'.$extension;
  178. $items = explode('-', $this->filename);
  179. $items[5] = $exe_id;
  180. $filename = $filename = implode('-', $items);
  181. $new_name = $this->store_path.$filename.'.'.$extension;
  182. rename($old_name, $new_name);
  183. break;
  184. }
  185. }
  186. }
  187. /**
  188. * @param bool $load_from_database
  189. * @return null|string
  190. */
  191. public function load_filename_if_exists($load_from_database = false)
  192. {
  193. $filename = null;
  194. //@ugly fix
  195. foreach($this->available_extensions as $extension) {
  196. if (is_file($this->store_path.$this->filename.'.'.$extension)) {
  197. $filename = $this->filename.'.'.$extension;
  198. break;
  199. }
  200. }
  201. // temp_exe
  202. if ($load_from_database) {
  203. //Load the real filename just if exists
  204. if (isset($this->params['exe_id']) && isset($this->params['user_id']) &&
  205. isset($this->params['question_id']) && isset($this->params['session_id']) && isset($this->params['course_id'])
  206. ) {
  207. $attempt_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT);
  208. $sql = "SELECT filename FROM $attempt_table
  209. WHERE
  210. exe_id = ".$this->params['exe_id']." AND
  211. user_id = ".$this->params['user_id']." AND
  212. question_id = ".$this->params['question_id']." AND
  213. session_id = ".$this->params['session_id']." AND
  214. course_code = '".$this->course_info['code']."'
  215. LIMIT 1";
  216. $result = Database::query($sql);
  217. $result = Database::fetch_row($result,'ASSOC');
  218. if (isset($result) && isset($result[0]) && !empty($result[0])) {
  219. $filename = $result[0];
  220. }
  221. }
  222. }
  223. if (is_file($this->store_path.$filename)) {
  224. return $this->store_path.$filename;
  225. }
  226. return null;
  227. }
  228. /**
  229. *
  230. * Get the URL of the file
  231. * path courses/XXX/exercises/(session_id)/(exercise_id)/(question_id)/(user_id)/
  232. * @param int $force_download
  233. *
  234. * @return string
  235. */
  236. public function get_public_url($force_download = 0)
  237. {
  238. $params = $this->get_params();
  239. $filename = basename($this->load_filename_if_exists());
  240. $url = api_get_path(WEB_COURSE_PATH).$this->course_info['path'].'/exercises/'.$params['session_id'].'/'.$params['exercise_id'].'/'.$params['question_id'].'/'.$params['user_id'].'/'.$filename;
  241. return $url;
  242. }
  243. /**
  244. * Uploads the nanogong wav file
  245. * @param bool
  246. */
  247. public function upload_file($is_nano = false)
  248. {
  249. if (!empty($_FILES)) {
  250. $upload_ok = process_uploaded_file($_FILES['file'], false);
  251. if (!is_uploaded_file($_FILES['file']['tmp_name'])) {
  252. return 0;
  253. }
  254. if ($upload_ok) {
  255. // Check if there is enough space to save the file
  256. if (!DocumentManager::enough_space($_FILES['file']['size'], DocumentManager::get_course_quota())) {
  257. return 0;
  258. }
  259. //first we delete everything before uploading the file
  260. $this->delete_files();
  261. //Reload the filename variable
  262. $file_name = add_ext_on_mime($_FILES['file']['name'], $_FILES['file']['type']);
  263. $file_name = strtolower($file_name);
  264. $file_info = pathinfo($file_name);
  265. if ($is_nano == true) {
  266. $file_info['extension'] = 'wav';
  267. }
  268. $file_name = $this->filename.'.'.$file_info['extension'];
  269. if (in_array($file_info['extension'], $this->available_extensions)) {
  270. if (move_uploaded_file($_FILES['file']['tmp_name'], $this->store_path.$file_name)) {
  271. $this->store_filename = $this->store_path.$file_name;
  272. return 1;
  273. }
  274. }
  275. }
  276. }
  277. return 0;
  278. }
  279. /**
  280. * Show the audio file + a button to download
  281. * @param bool
  282. */
  283. public function show_audio_file($show_delete_button = false)
  284. {
  285. $html = '';
  286. $file_path = $this->load_filename_if_exists();
  287. if (!empty($file_path)) {
  288. $url = $this->get_public_url(true);
  289. $actions = Display::url(Display::return_icon('save.png', get_lang('Download'), array(), ICON_SIZE_SMALL), $url, array('target'=>'_blank'));
  290. $download_button = Display::url(get_lang('Download'), $url, array('class' =>'btn btn-default'));
  291. if ($show_delete_button) {
  292. $actions .= ' '.Display::url(Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL), "#", array('onclick'=>'delete_file();'));
  293. }
  294. $basename = basename($file_path);
  295. $path_info = pathinfo($basename);
  296. if ($path_info['extension'] == 'wav') {
  297. $html .= '<script>
  298. $(document).ready( function() {
  299. var java_enabled = navigator.javaEnabled();
  300. if (java_enabled) {
  301. $("#nanogong_warning").hide();
  302. $("#nanogong_player_id").show();
  303. } else {
  304. $("#nanogong_warning").show();
  305. $("#nanogong_player_id").hide();
  306. }
  307. });
  308. </script>';
  309. $html .= '<div id="nanogong_player_id" class="nanogong_player_container">';
  310. $html .= '<div class="action_player">'.$actions.'</div>';
  311. $html .= '<div class="nanogong_player">';
  312. $html .= '<applet id="nanogong_player" archive="'.api_get_path(WEB_LIBRARY_PATH).'nanogong/nanogong.jar" code="gong.NanoGong" width="250" height="95" ALIGN="middle">';
  313. $html .= '<param name="ShowRecordButton" value="false" />'; // default true
  314. $html .= '<param name="ShowSaveButton" value="false" />'; //you can save in local computer | (default true)
  315. //echo '<param name="ShowAudioLevel" value="false" />'; // it displays the audiometer | (default true)
  316. $html .= '<param name="ShowTime" value="true" />'; // default false
  317. $html .= '<param name="Color" value="#FFFFFF" />';
  318. $html .= '<param name="ShowSpeedButton" value="false" />';
  319. //echo '<param name="StartTime" value="10.5" />';
  320. //echo '<param name="EndTime" value="65" />';
  321. $html .= '<param name="AudioFormat" value="ImaADPCM" />';// ImaADPCM (more speed), Speex (more compression)|(default Speex)
  322. //$html .= '<param name="AudioFormat" value="Speex" />';// ImaADPCM (more speed), Speex (more compression)|(default Speex)
  323. //Quality for ImaADPCM (low 8000, medium 11025, normal 22050, hight 44100) OR Quality for Speex (low 8000, medium 16000, normal 32000, hight 44100) | (default 44100)
  324. //echo '<param name="SamplingRate" value="32000" />';
  325. //echo '<param name="MaxDuration" value="60" />';
  326. $html .= '<param name="SoundFileURL" value="'.$url.'" />';//load a file |(default "")
  327. $html .= '</applet>';
  328. $html .= '</div>';
  329. $html .= '</div>';
  330. $html .= '<div id="nanogong_warning">'.Display::return_message(get_lang('BrowserDoesNotSupportNanogongPlayer'), 'warning').$download_button.'</div>';
  331. } elseif(in_array($path_info['extension'],array('mp3', 'ogg','wav'))) {
  332. $js_path = api_get_path(WEB_LIBRARY_PATH).'javascript/';
  333. $html .= '<link rel="stylesheet" href="'.$js_path.'jquery-jplayer/skins/blue/jplayer.blue.monday.css" type="text/css">';
  334. //$html .= '<link rel="stylesheet" href="' . $js_path . 'jquery-jplayer/skins/chamilo/jplayer.blue.monday.css" type="text/css">';
  335. $html .= '<script type="text/javascript" src="'.$js_path.'jquery-jplayer/jquery.jplayer.min.js"></script>';
  336. $html .= '<div class="nanogong_player"></div>';
  337. $html .= '<br /><div class="action_player">'.$actions.'</div><br /><br /><br />';
  338. $params = array(
  339. 'url' => $url,
  340. 'extension' =>$path_info['extension'],
  341. 'count'=> 1
  342. );
  343. $jquery = DocumentManager::generate_jplayer_jquery($params);
  344. $html .= '<script>
  345. $(document).ready( function() {
  346. //Experimental changes to preview mp3, ogg files
  347. '.$jquery.'
  348. });
  349. </script>';
  350. $html .= DocumentManager::generate_media_preview(1, 'advanced');
  351. }
  352. }
  353. return $html;
  354. }
  355. /*
  356. var filename = document.getElementById("audio_title").value+".wav";
  357. var filename = filename.replace(/\s/g, "_");//replace spaces by _
  358. var filename = encodeURIComponent(filename);
  359. var filepath="'.urlencode($filepath).'";
  360. var dir="'.urlencode($dir).'";
  361. var course_code="'.urlencode($course_code).'";
  362. var urlnanogong="'.$url.'?filename="+filename+"&filepath="+filepath+"&dir="+dir+"&course_code="+course_code;
  363. */
  364. /**
  365. * Returns the nanogong javascript code
  366. * @return string
  367. */
  368. public function return_js()
  369. {
  370. $params = $this->get_params(true);
  371. $url = api_get_path(WEB_AJAX_PATH).'nanogong.ajax.php?a=save_file&'.$params.'&is_nano=1';
  372. $url_load_file = api_get_path(WEB_AJAX_PATH).'nanogong.ajax.php?a=show_audio&'.$params;
  373. $url_delete = api_get_path(WEB_AJAX_PATH).'nanogong.ajax.php?a=delete&'.$params;
  374. $js = '<script language="javascript">
  375. //lang vars
  376. var lang_no_applet = "'.get_lang('NanogongNoApplet').'";
  377. var lang_record_before_save = "'.get_lang('NanogongRecordBeforeSave').'";
  378. var lang_give_a_title = "'.get_lang('NanogongGiveTitle').'";
  379. var lang_failed_to_submit = "'.get_lang('NanogongFailledToSubmit').'";
  380. var lang_submitted = "'.get_lang('NanogongSubmitted').'";
  381. var lang_deleted = "'.get_lang('Deleted').'";
  382. var is_nano = 0;
  383. function check_gong() {
  384. //var record = document.getElementById("nanogong");
  385. var recorder;
  386. var java_enabled = navigator.javaEnabled()
  387. return java_enabled;
  388. }
  389. function show_simple_upload_form() {
  390. $("#no_nanogong_div").show();
  391. //$("#nanogong_div").hide();
  392. $("#preview").hide();
  393. }
  394. $(document).ready( function() {
  395. $("#no_nanogong_div").hide();
  396. $("#nanogong_div").hide();
  397. var check_js = check_gong();
  398. if (check_js == true) {
  399. $("#nanogong_div").show();
  400. $("#no_nanogong_div").hide();
  401. is_nano = 1;
  402. $(".nanogong_player").show();
  403. } else {
  404. $("#no_nanogong_div").show();
  405. $("#nanogong_div").hide();
  406. $(".nanogong_player").hide();
  407. }
  408. //show always the mp3/ogg upload form (for dev purposes)
  409. //$("#no_nanogong_div").show();
  410. //$("#nanogong_div").hide();
  411. });
  412. function delete_file() {
  413. $.ajax({
  414. url: "'.$url_delete.'",
  415. success:function(data) {
  416. $("#status_warning").hide();
  417. $("#status_ok").hide();
  418. $("#messages").html(data);
  419. $("#messages").show();
  420. $("#preview").hide();
  421. }
  422. });
  423. }
  424. function upload_file() {
  425. $("#form_nanogong_simple").submit();
  426. }
  427. function send_voice() {
  428. $("#status_warning").hide();
  429. $("#status_ok").hide();
  430. $("#messages").hide();
  431. var check_js = check_gong();
  432. var recorder = document.getElementById("nanogong");
  433. if (!recorder || !check_js) {
  434. //alert(lang_no_applet)
  435. $("#status_warning").html(lang_no_applet);
  436. $("#status_warning").show();
  437. //Show form
  438. $("#no_nanogong_div").show();
  439. $("#nanogong_div").hide();
  440. return false;
  441. }
  442. var duration = parseInt(recorder.sendGongRequest("GetMediaDuration", "audio")) || 0;
  443. if (duration <= 0) {
  444. $("#status_warning").html(lang_record_before_save);
  445. $("#status_warning").show();
  446. return false;
  447. }
  448. var applet = document.getElementById("nanogong");
  449. var ret = applet.sendGongRequest("PostToForm", "'.$url.'", "file", "", "temp"); // PostToForm, postURL, inputname, cookie, filename
  450. if (ret == 1) {
  451. $("#status_ok").html(lang_submitted);
  452. $("#status_ok").show();
  453. $.ajax({
  454. url:"'.$url_load_file.'&is_nano="+is_nano,
  455. success: function(data){
  456. $("#preview").html(data);
  457. $("#preview").show();
  458. }
  459. });
  460. } else {
  461. //alert(lang_submitted+"\n"+ret);
  462. $("#status_warning").html(lang_failed_to_submit);
  463. $("#status_warning").show();
  464. }
  465. return false;
  466. }
  467. </script>';
  468. return $js;
  469. }
  470. /**
  471. * Returns the HTML form to upload a nano file or upload a file
  472. * @param string
  473. */
  474. public function return_form($message = null)
  475. {
  476. $params = $this->get_params(true);
  477. $url = api_get_path(WEB_AJAX_PATH).'nanogong.ajax.php?a=save_file&'.$params;
  478. //check browser support and load form
  479. $array_browser = api_browser_support('check_browser');
  480. $preview_file = $this->show_audio_file(true, true);
  481. $preview_file = Display::div(
  482. $preview_file,
  483. array('id' => 'preview', 'style' => 'text-align:center; padding-left: 25px;')
  484. );
  485. $html = '<center>';
  486. //Use normal upload file
  487. $html .= Display::return_icon('microphone.png', get_lang('PressRecordButton'),'', ICON_SIZE_BIG);
  488. $html .='<br />';
  489. $html .= '<div id="nanogong_div">';
  490. $html .= '<applet id="nanogong" archive="'.api_get_path(WEB_LIBRARY_PATH).'nanogong/nanogong.jar" code="gong.NanoGong" width="250" height="95" align="middle">';
  491. //echo '<param name="ShowRecordButton" value="false" />'; // default true
  492. // echo '<param name="ShowSaveButton" value="false" />'; //you can save in local computer | (default true)
  493. $html .= '<param name="ShowSpeedButton" value="false" />'; // default true
  494. //echo '<param name="ShowAudioLevel" value="false" />'; // it displays the audiometer | (default true)
  495. $html .= '<param name="ShowTime" value="true" />'; // default false
  496. $html .= '<param name="Color" value="#FFFFFF" />'; // default #FFFFFF
  497. //echo '<param name="StartTime" value="10.5" />';
  498. //echo '<param name="EndTime" value="65" />';
  499. $html .= '<param name="AudioFormat" value="ImaADPCM" />';// ImaADPCM (more speed), Speex (more compression)|(default Speex)
  500. //$html .= '<param name="AudioFormat" value="Speex" />';// ImaADPCM (more speed), Speex (more compression)|(default Speex)
  501. //echo '<param name="SamplingRate" value="32000" />';//Quality for ImaADPCM (low 8000, medium 11025, normal 22050, hight 44100) OR Quality for Speex (low 8000, medium 16000, normal 32000, hight 44100) | (default 44100)
  502. //echo '<param name="MaxDuration" value="60" />';
  503. //echo '<param name="SoundFileURL" value="http://somewhere.com/mysoundfile.wav" />';//load a file |(default "")
  504. $html .= '</applet>';
  505. $html .= '<br /><br /><br />';
  506. $html .= '<form name="form_nanogong_advanced">';
  507. $html .= '<input type="hidden" name="is_nano" value="1">';
  508. $html .= '<a href="#" class="btn btn-default" onclick="send_voice()" />'.get_lang('SendRecord').'</a>';
  509. $html .= '</form></div>';
  510. $html .= Display::url(get_lang('ProblemsRecordingUploadYourOwnAudioFile'), 'javascript:void(0)', array('onclick' => 'show_simple_upload_form();'));
  511. $html .= '<br /><br /><div id="no_nanogong_div">';
  512. //$html .= Display::return_message(get_lang('BrowserNotSupportNanogongSend'), 'warning');
  513. $html .= '<form id="form_nanogong_simple" class="form-search" action="'.$url.'" name="form_nanogong" method="POST" enctype="multipart/form-data">';
  514. $html .= '<input type="file" name="file">';
  515. $html .= '<a href="#" class="btn btn-default" onclick="upload_file()" /><i class="fa fa-upload"></i> '.get_lang('UploadFile').'</a>';
  516. $html .= '</form>';
  517. $html .= '</div>';
  518. $html .= '</center>';
  519. $html .= '<div style="display:none" id="status_ok" class="confirmation-message"></div><div style="display:none" id="status_warning" class="warning-message"></div>';
  520. $html .= '<div id="messages">'.$message.'</div>';
  521. $html .= $preview_file;
  522. return $html;
  523. }
  524. /**
  525. * @param bool $return_as_query
  526. * @return bool|string
  527. */
  528. public function get_params($return_as_query = false)
  529. {
  530. if (empty($this->params)) {
  531. return false;
  532. }
  533. if ($return_as_query) {
  534. return http_build_query($this->params);
  535. }
  536. return $this->params;
  537. }
  538. /**
  539. * @param $attribute
  540. * @return mixed
  541. */
  542. public function get_param_value($attribute)
  543. {
  544. if (isset($this->params[$attribute])) {
  545. return $this->params[$attribute];
  546. }
  547. }
  548. /**
  549. * Show a button to load the form
  550. * @return string
  551. */
  552. public function show_button()
  553. {
  554. $params_string = $this->get_params(true);
  555. $url = api_get_path(WEB_AJAX_PATH)
  556. . 'nanogong.ajax.php?a=show_form&'
  557. . $params_string
  558. . '&TB_iframe=true';
  559. $html = Display::url(
  560. get_lang('RecordAnswer'),
  561. $url,
  562. [
  563. 'class' => 'btn btn-default ajax',
  564. 'data-title' => get_lang('RecordAnswer')
  565. ]
  566. );
  567. $html .= '<br /><br />'.Display::return_message(get_lang('UseTheMessageBelowToAddSomeComments'));
  568. return $html;
  569. }
  570. }