hotpotatoes.php 8.8 KB

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