hotpotatoes.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Code for HotPotatoes integration.
  5. * @package chamilo.exercise
  6. * @author Istvan Mandak (original author)
  7. */
  8. require_once '../inc/global.inc.php';
  9. require_once 'hotpotatoes.lib.php';
  10. // Section (for the tabs).
  11. $this_section = SECTION_COURSES;
  12. // Access restriction: only teachers are allowed here.
  13. if (!api_is_allowed_to_edit(null, true)) {
  14. api_not_allowed();
  15. }
  16. if (isset($_SESSION['gradebook'])) {
  17. $gradebook = $_SESSION['gradebook'];
  18. }
  19. if (!empty($gradebook) && $gradebook == 'view') {
  20. $interbreadcrumb[] = array(
  21. 'url' => '../gradebook/'.$_SESSION['gradebook_dest'],
  22. 'name' => get_lang('ToolGradebook')
  23. );
  24. }
  25. // The breadcrumbs.
  26. $interbreadcrumb[] = array('url' => './exercise.php', 'name' => get_lang('Exercises'));
  27. $is_allowedToEdit = api_is_allowed_to_edit(null, true);
  28. // Database table definitions.
  29. $dbTable = Database::get_course_table(TABLE_DOCUMENT);
  30. $course_id = api_get_course_int_id();
  31. // Setting some variables.
  32. $document_sys_path = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  33. $uploadPath = '/HotPotatoes_files';
  34. $finish = (!empty($_POST['finish']) ? $_POST['finish'] : 0);
  35. $imgcount = (!empty($_POST['imgcount']) ? $_POST['imgcount'] : null);
  36. $fld = (!empty($_POST['fld']) ? $_POST['fld'] : null);
  37. $imgparams = [];
  38. $dialogBox = '';
  39. if ($finish == 2 && isset($_POST['imgparams'])) {
  40. $imgparams = $_POST['imgparams'];
  41. }
  42. // If user is allowed to edit...
  43. if (api_is_allowed_to_edit(null, true)) {
  44. if (hotpotatoes_init($document_sys_path.$uploadPath)) {
  45. // If the directory doesn't exist, create the "HotPotatoes" directory.
  46. $doc_id = add_document(
  47. $_course,
  48. '/HotPotatoes_files',
  49. 'folder',
  50. 0,
  51. get_lang('HotPotatoesFiles')
  52. );
  53. // Update properties in dbase (in any case).
  54. api_item_property_update(
  55. $_course,
  56. TOOL_DOCUMENT,
  57. $doc_id,
  58. 'FolderCreated',
  59. api_get_user_id()
  60. );
  61. // Make invisible (in any case) - why?
  62. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'invisible', api_get_user_id());
  63. }
  64. }
  65. /** Display */
  66. $nameTools = get_lang('HotPotatoesTests');
  67. $form = new FormValidator(
  68. 'hotpotatoes',
  69. 'post',
  70. api_get_self()."?".api_get_cidreq(),
  71. null,
  72. array('enctype' => 'multipart/form-data')
  73. );
  74. $form->addElement('header', $nameTools);
  75. $form->addElement('hidden', 'uploadPath');
  76. $form->addElement('hidden', 'fld', $fld);
  77. $form->addElement('hidden', 'imgcount', $imgcount);
  78. $form->addElement('hidden', 'finish', $finish);
  79. $form->addElement('html', GenerateHiddenList($imgparams));
  80. $form->addElement('label', '', Display::return_icon('hotpotatoes.jpg', get_lang('HotPotatoes')));
  81. $label = get_lang('DownloadImg').' : ';
  82. if ($finish == 0) {
  83. $label = get_lang('DownloadFile').' : ';
  84. }
  85. $form->addElement('file', 'userFile', $label);
  86. $form->addButtonSend(get_lang('SendFile'));
  87. // If finish is set; it's because the user came from this script in the first place (displaying hidden "finish" field).
  88. if ((api_is_allowed_to_edit(null, true)) && (($finish == 0) || ($finish == 2))) {
  89. // Moved this down here as the upload handling functions give output.
  90. if ($form->validate()) {
  91. // Initialise $finish
  92. if (!isset($finish)) {
  93. $finish = 0;
  94. }
  95. //if the size is not defined, it's probably because there has been an error or no file was submitted
  96. if (!$_FILES['userFile']['size']) {
  97. $dialogBox .= get_lang('SendFileError').'<br />'.get_lang('Notice').' : '.get_lang('MaxFileSize').' '.ini_get('upload_max_filesize');
  98. } else {
  99. $unzip = 0;
  100. if (preg_match('/\.zip$/i', $_FILES['userFile']['name'])) {
  101. //if it's a zip, allow zip upload
  102. $unzip = 1;
  103. }
  104. if ($finish == 0) {
  105. // Generate new test folder if on first step of file upload.
  106. $filename = api_replace_dangerous_char(trim($_FILES['userFile']['name']));
  107. $fld = GenerateHpFolder($document_sys_path.$uploadPath.'/');
  108. //$doc_id = add_document($_course, '/HotPotatoes_files/'.$fld, 'folder', 0, $fld);
  109. //api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'FolderCreated', api_get_user_id());
  110. @mkdir($document_sys_path.$uploadPath.'/'.$fld, api_get_permissions_for_new_directories());
  111. $doc_id = add_document($_course, '/HotPotatoes_files/'.$fld, 'folder', 0, $fld);
  112. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'FolderCreated', api_get_user_id());
  113. } else {
  114. // It is not the first step... get the filename directly from the system params.
  115. $filename = $_FILES['userFile']['name'];
  116. }
  117. $allow_output_on_success = false;
  118. if (handle_uploaded_document(
  119. $_course,
  120. $_FILES['userFile'],
  121. $document_sys_path,
  122. $uploadPath . '/' . $fld,
  123. api_get_user_id(),
  124. null,
  125. null,
  126. $unzip,
  127. '',
  128. $allow_output_on_success
  129. )) {
  130. if ($finish == 2) {
  131. $imgparams = $_POST['imgparams'];
  132. $checked = CheckImageName($imgparams, $filename);
  133. if ($checked) {
  134. $imgcount = $imgcount-1;
  135. } else {
  136. $dialogBox .= $filename.' '.get_lang('NameNotEqual');
  137. my_delete($document_sys_path.$uploadPath.'/'.$fld.'/'.$filename);
  138. update_db_info('delete', $uploadPath.'/'.$fld.'/'.$filename);
  139. }
  140. if ($imgcount == 0) { // all image uploaded
  141. $finish = 1;
  142. }
  143. } else {
  144. // If we are (still) on the first step of the upload process.
  145. if ($finish == 0) {
  146. $finish = 2;
  147. // Get number and name of images from the files contents.
  148. GetImgParams('/'.$filename, $document_sys_path.$uploadPath.'/'.$fld, $imgparams, $imgcount);
  149. if ($imgcount == 0) {
  150. // There is no img link, so finish the upload process.
  151. $finish = 1;
  152. } else {
  153. // There is still one or more img missing.
  154. $dialogBox .= get_lang('DownloadEnd');
  155. }
  156. }
  157. }
  158. $title = @htmlspecialchars(GetQuizName($filename, $document_sys_path.$uploadPath.'/'.$fld.'/'), ENT_COMPAT, api_get_system_encoding());
  159. $query = "UPDATE $dbTable
  160. SET comment='".Database::escape_string($title)."'
  161. WHERE c_id = $course_id AND path=\"".$uploadPath."/".$fld."/".$filename."\"";
  162. Database::query($query);
  163. api_item_property_update($_course, TOOL_QUIZ, $id, 'QuizAdded', api_get_user_id());
  164. } else {
  165. if ($finish == 2) {
  166. // delete?
  167. //$dialogBox .= get_lang('NoImg');
  168. }
  169. $finish = 0;
  170. // error
  171. if (api_failure::get_last_failure() == 'not_enough_space') {
  172. $dialogBox .= get_lang('NoSpace');
  173. } elseif (api_failure::get_last_failure() == 'php_file_in_zip_file') {
  174. $dialogBox .= get_lang('ZipNoPhp');
  175. }
  176. }
  177. }
  178. }
  179. if ($finish == 1) { /** ok -> send to main exercises page */
  180. header('Location: exercise.php?'.api_get_cidreq());
  181. exit;
  182. }
  183. Display::display_header($nameTools, get_lang('Exercise'));
  184. echo '<div class="actions">';
  185. echo '<a href="exercise.php?show=test">'.
  186. Display :: return_icon('back.png', get_lang('BackToExercisesList'), '', ICON_SIZE_MEDIUM).
  187. '</a>';
  188. echo '</div>';
  189. if ($finish == 2) {
  190. // If we are in the img upload process.
  191. $dialogBox .= get_lang('ImgNote_st').$imgcount.get_lang('ImgNote_en').'<br />';
  192. while (list($key, $string) = each($imgparams)) {
  193. $dialogBox .= $string.'; ';
  194. }
  195. }
  196. if ($dialogBox) {
  197. Display::display_normal_message($dialogBox, false);
  198. }
  199. $form->display();
  200. }
  201. // Display the footer.
  202. Display::display_footer();