hotpotatoes.php 8.8 KB

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