hotpotatoes.php 8.9 KB

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