hotpotatoes.php 10 KB

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