hotpotatoes.php 8.4 KB

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