hotpotatoes.php 8.5 KB

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