hotpotatoes.php 8.5 KB

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