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