hotpotatoes.php 8.3 KB

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