hotpotatoes.php 8.6 KB

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