hotpotatoes.php 9.3 KB

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