hotpotatoes.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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('Assessments'),
  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('Tests'),
  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. DocumentManager::addDocument(
  52. $_course,
  53. '/HotPotatoes_files',
  54. 'folder',
  55. 0,
  56. get_lang('HotPotatoes files')
  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('Import Hotpotatoes tests');
  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('Upload Image file to the server').' : ';
  93. if ($finish == 0) {
  94. $label = get_lang('Download file').' : ';
  95. }
  96. $form->addElement('file', 'userFile', $label);
  97. $form->addButtonSend(get_lang('Upload document'));
  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('Upload documentError').'<br />'.get_lang('Notice').' : '.get_lang('Maximum file size').' '.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 = DocumentManager::addDocument($_course, '/HotPotatoes_files/'.$fld, 'folder', 0, $fld);
  121. } else {
  122. // It is not the first step... get the filename directly from the system params.
  123. $filename = $_FILES['userFile']['name'];
  124. }
  125. $allow_output_on_success = false;
  126. if (handle_uploaded_document(
  127. $_course,
  128. $_FILES['userFile'],
  129. $document_sys_path,
  130. $uploadPath.'/'.$fld,
  131. api_get_user_id(),
  132. null,
  133. null,
  134. $unzip,
  135. '',
  136. $allow_output_on_success
  137. )) {
  138. if ($finish == 2) {
  139. $imgparams = $_POST['imgparams'];
  140. $checked = CheckImageName($imgparams, $filename);
  141. if ($checked) {
  142. $imgcount = $imgcount - 1;
  143. } else {
  144. $dialogBox .= $filename.' '.get_lang('is not the valid file !');
  145. my_delete($document_sys_path.$uploadPath.'/'.$fld.'/'.$filename);
  146. DocumentManager::updateDbInfo('delete', $uploadPath.'/'.$fld.'/'.$filename);
  147. }
  148. if ($imgcount == 0) { // all image uploaded
  149. $finish = 1;
  150. }
  151. } else {
  152. // If we are (still) on the first step of the upload process.
  153. if ($finish == 0) {
  154. $finish = 2;
  155. // Get number and name of images from the files contents.
  156. GetImgParams('/'.$filename, $document_sys_path.$uploadPath.'/'.$fld, $imgparams, $imgcount);
  157. if ($imgcount == 0) {
  158. // There is no img link, so finish the upload process.
  159. $finish = 1;
  160. } else {
  161. // There is still one or more img missing.
  162. $dialogBox .= get_lang('The upload is finished');
  163. }
  164. }
  165. }
  166. // Set the HotPotatoes title in the document record's "comment" field
  167. $title = @htmlspecialchars(
  168. GetQuizName(
  169. $filename,
  170. $document_sys_path.$uploadPath.'/'.$fld.'/'
  171. ),
  172. ENT_COMPAT,
  173. api_get_system_encoding()
  174. );
  175. $sessionAnd = ' AND session_id IS NULL ';
  176. if ($sessionId) {
  177. $sessionAnd = " AND session_id = $sessionId ";
  178. }
  179. $path = $uploadPath.'/'.$fld.'/'.$filename;
  180. // Find the proper record
  181. $select = "SELECT iid FROM $dbTable
  182. WHERE c_id = $course_id
  183. $sessionAnd
  184. AND path = '$path'";
  185. $query = Database::query($select);
  186. if (Database::num_rows($query)) {
  187. $row = Database::fetch_array($query);
  188. $hotPotatoesDocumentId = $row['iid'];
  189. // Update the record with the 'comment' (HP title)
  190. $query = "UPDATE $dbTable
  191. SET comment='".Database::escape_string($title)."'
  192. WHERE iid = $hotPotatoesDocumentId";
  193. Database::query($query);
  194. // Mark the addition of the HP quiz in the item_property table
  195. api_item_property_update(
  196. $_course,
  197. TOOL_QUIZ,
  198. $hotPotatoesDocumentId,
  199. 'QuizAdded',
  200. api_get_user_id()
  201. );
  202. }
  203. } else {
  204. $finish = 0;
  205. }
  206. }
  207. }
  208. if ($finish == 1) {
  209. /** ok -> send to main exercises page */
  210. header('Location: exercise.php?'.api_get_cidreq());
  211. exit;
  212. }
  213. Display::display_header($nameTools, get_lang('Test'));
  214. echo '<div class="actions">';
  215. echo '<a href="exercise.php?show=test">'.
  216. Display::return_icon('back.png', get_lang('BackToTestsList'), '', ICON_SIZE_MEDIUM).
  217. '</a>';
  218. echo '</div>';
  219. if ($finish == 2) {
  220. // If we are in the img upload process.
  221. $dialogBox .= get_lang('<br> You still have to upload').$imgcount.get_lang(' image(s) :').'<br />';
  222. foreach ($imgparams as $key => $string) {
  223. $dialogBox .= $string.'; ';
  224. }
  225. }
  226. if ($dialogBox) {
  227. echo Display::return_message($dialogBox, 'normal', false);
  228. }
  229. $form->display();
  230. }
  231. // Display the footer.
  232. Display::display_footer();