hotpotatoes.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Code for Hotpotatoes integration.
  5. * @package chamilo.exercise
  6. * @author Istvan Mandak
  7. * @version $Id: hotpotatoes.php 20798 2009-05-18 18:13:25Z cvargas1 $
  8. */
  9. // name of the language file that needs to be included
  10. $language_file ='exercice';
  11. // including the global Dokeos file
  12. require_once '../inc/global.inc.php';
  13. // include additional libraries
  14. require_once (api_get_path(LIBRARY_PATH).'fileUpload.lib.php');
  15. require_once (api_get_path(LIBRARY_PATH).'document.lib.php');
  16. require_once (api_get_path(LIBRARY_PATH).'fileManage.lib.php');
  17. require_once (api_get_path(LIBRARY_PATH).'pclzip/pclzip.lib.php');
  18. require_once 'hotpotatoes.lib.php';
  19. // section (for the tabs)
  20. $this_section=SECTION_COURSES;
  21. // access restriction: only teachers are allowed here
  22. if(!api_is_allowed_to_edit(null,true)) {
  23. api_not_allowed();
  24. }
  25. if (isset($_SESSION['gradebook'])){
  26. $gradebook= $_SESSION['gradebook'];
  27. }
  28. if (!empty($gradebook) && $gradebook=='view') {
  29. $interbreadcrumb[]= array (
  30. 'url' => '../gradebook/'.$_SESSION['gradebook_dest'],
  31. 'name' => get_lang('ToolGradebook')
  32. );
  33. }
  34. // the breadcrumbs
  35. $interbreadcrumb[]= array ("url"=>"./exercice.php", "name"=> get_lang('Exercices'));
  36. $is_allowedToEdit=api_is_allowed_to_edit(null,true);
  37. // Database table definitions
  38. $dbTable = Database::get_course_table(TABLE_DOCUMENT);
  39. // setting some variables
  40. $baseServDir = $_configuration['root_sys'];
  41. $baseServUrl = $_configuration['url_append']."/";
  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
  53. //create the "HotPotatoes" directory
  54. $doc_id = add_document($_course, '/HotPotatoes_files','folder',0,'HotPotatoes Files');
  55. //update properties in dbase (in any case)
  56. api_item_property_update($_course,TOOL_DOCUMENT,$doc_id,'FolderCreated',$_user['user_id']);
  57. //make invisible(in any case) - why?
  58. api_item_property_update($_course,TOOL_DOCUMENT,$doc_id,'invisible',$_user['user_id']);
  59. }
  60. }
  61. /** display */
  62. // if finish is set; it's because the user came from this script in the first place (displaying hidden "finish" field)
  63. if((api_is_allowed_to_edit(null,true)) && (($finish == 0) || ($finish == 2)))
  64. //if(($is_allowedToEdit) )
  65. {
  66. $nameTools = get_lang('HotPotatoesTests');
  67. //moved this down here as the upload handling functions give output
  68. if (isset($_POST['submit']))
  69. {
  70. //check that the submit button was pressed when the button had the "Download" value
  71. //This should be updated to "upload" here and on the button, and it would be better to
  72. // check something else than a string displayd on a button
  73. if (strcmp($_POST['submit'],get_lang('Send'))===0)
  74. {
  75. //@todo: this value should be moved to the platform admin section
  76. $maxFilledSpace = 100000000;
  77. //initialise $finish
  78. if (!isset($finish)) {$finish = 0;}
  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. {
  82. $dialogBox .= get_lang('SendFileError').'<br />'.get_lang('Notice').' : '.get_lang('MaxFileSize').' '.ini_get('upload_max_filesize');
  83. }
  84. else
  85. {
  86. /* deprecated code
  87. if ($enableDocumentParsing==true)
  88. { $enableDocumentParsing=false;
  89. $oke=1;}
  90. else { $oke = 0; }
  91. */
  92. //$unzip = 'unzip';
  93. $unzip = 0;
  94. if(preg_match('/\.zip$/i',$_FILES['userFile']['name'])){
  95. //if it's a zip, allow zip upload
  96. $unzip = 1;
  97. }
  98. if ($finish==0)
  99. { //generate new test folder if on first step of file upload
  100. $filename = replace_dangerous_char(trim($_FILES['userFile']['name']),'strict');
  101. $fld = GenerateHpFolder($document_sys_path.$uploadPath."/");
  102. @mkdir($document_sys_path.$uploadPath."/".$fld, api_get_permissions_for_new_directories());
  103. $doc_id = add_document($_course, '/HotPotatoes_files/'.$fld,'folder',0,$fld);
  104. api_item_property_update($_course,TOOL_DOCUMENT,$doc_id,'FolderCreated',$_user['user_id']);
  105. }
  106. else
  107. { //it is not the first step... get the filename directly from the system params
  108. $filename = $_FILES['userFile']['name'];
  109. }
  110. /*if (treat_uploaded_file($_FILES['userFile'], $document_sys_path,
  111. $uploadPath."/".$fld, $maxFilledSpace, $unzip))*/
  112. $allow_output_on_success = false;
  113. if (handle_uploaded_document($_course,$_FILES['userFile'],$document_sys_path,$uploadPath."/".$fld,$_user['user_id'],null,null,$maxFilledSpace,$unzip,'',$allow_output_on_success))
  114. {
  115. if ($finish==2)
  116. {
  117. $imgparams = $_POST['imgparams'];
  118. $checked = CheckImageName($imgparams,$filename);
  119. if ($checked)
  120. { $imgcount = $imgcount-1; }
  121. else
  122. {
  123. $dialogBox .= $filename." ".get_lang('NameNotEqual');
  124. my_delete($document_sys_path.$uploadPath."/".$fld."/".$filename);
  125. update_db_info("delete", $uploadPath."/".$fld."/".$filename);
  126. }
  127. if ($imgcount==0) // all image uploaded
  128. {
  129. $finish=1;
  130. }
  131. }
  132. else
  133. { //if we are (still) on the first step of the upload process
  134. if ($finish==0)
  135. {
  136. $finish = 2;
  137. // get number and name of images from the files contents
  138. GetImgParams("/".$filename,$document_sys_path.$uploadPath."/".$fld,$imgparams,$imgcount);
  139. if ($imgcount==0) //there is no img link, so finish the upload process
  140. { $finish = 1; }
  141. else //there is still one or more img missing
  142. { $dialogBox .= get_lang('DownloadEnd'); }
  143. }
  144. }
  145. $newComment = "";
  146. $query = "UPDATE $dbTable SET comment='$newComment' WHERE path=\"".$uploadPath."/".$fld."/".$filename."\"";
  147. /*, visibility='v' */
  148. Database::query($query);
  149. api_item_property_update($_course, TOOL_QUIZ, $id, "QuizAdded", $_user['user_id']);
  150. }
  151. else
  152. {
  153. if ($finish==2)
  154. {
  155. // delete?
  156. //$dialogBox .= get_lang('NoImg');
  157. }
  158. $finish = 0; // error
  159. if (api_failure::get_last_failure() == 'not_enough_space')
  160. {
  161. $dialogBox .= get_lang('NoSpace');
  162. }
  163. elseif (api_failure::get_last_failure() == 'php_file_in_zip_file')
  164. {
  165. $dialogBox .= get_lang('ZipNoPhp');
  166. }
  167. }
  168. /* if ($oke==1)
  169. { $enableDocumentParsing=true; $oke=0;}
  170. */
  171. }
  172. }
  173. }
  174. if ($finish == 1)
  175. { /** ok -> send to main exercises page */
  176. header('Location: exercice.php?'.api_get_cidreq());
  177. exit;
  178. }
  179. Display::display_header($nameTools,get_lang('Exercise'));
  180. echo '<div class="actions">';
  181. echo '<a href="exercice.php?show=test">' . Display :: return_icon('message_reply_forum.png', get_lang('GoBackToQuestionList')) . get_lang('GoBackToQuestionList') . '</a>';
  182. echo '</div>';
  183. if ($finish==2) //if we are in the img upload process
  184. {
  185. $dialogBox.= get_lang('ImgNote_st').$imgcount.get_lang('ImgNote_en')."<br>";
  186. while(list($key,$string)=each($imgparams))
  187. {
  188. $dialogBox.=$string."; ";
  189. }
  190. }
  191. if ($dialogBox)
  192. {
  193. Display::display_normal_message($dialogBox, false); //main API
  194. }
  195. /*--------------------------------------
  196. UPLOAD SECTION
  197. --------------------------------------*/
  198. echo "<!-- upload -->\n",
  199. "<form action=\"".api_get_self()."?".api_get_cidreq()."\" method=\"post\" enctype=\"multipart/form-data\" >\n",
  200. "<input type=\"hidden\" name=\"uploadPath\" value=\"\">\n",
  201. "<input type=\"hidden\" name=\"fld\" value=\"$fld\">\n",
  202. "<input type=\"hidden\" name=\"imgcount\" value=\"$imgcount\">\n",
  203. "<input type=\"hidden\" name=\"finish\" value=\"$finish\">\n";
  204. echo GenerateHiddenList($imgparams);
  205. /*if ($finish==0){ echo get_lang('DownloadFile');}
  206. else {echo get_lang('DownloadImg');}
  207. echo " : ",
  208. "<input type=\"file\" name=\"userFile\">\n",
  209. "<input type=\"submit\" name=\"submit\" value=\"".get_lang('Send')."\"><br/>\n";*/
  210. //Display::display_icon('hotpotatoes.jpg','',array('align'=> 'right', 'style' => 'position: absolute; padding-top: 30px; margin-left: 500px;'));
  211. echo '<div class="row"><div class="form_header">'.$nameTools.'</div></div>';
  212. echo '<div class="row">';
  213. echo '<div class="label" style="padding:10px">';
  214. echo '<span class="form_required">*</span>';
  215. if ($finish==0){
  216. echo get_lang('DownloadFile').' : ';
  217. }
  218. else{
  219. echo get_lang('DownloadImg').' : ';
  220. }
  221. echo '</div>';
  222. echo '<div class="formw">';
  223. echo '<div style="float:left;padding:10px" >
  224. <input type="file" name="userFile"><br /><br />
  225. <button type="submit" class="upload" name="submit" value="'.get_lang('Send').'">'.get_lang('SendFile').'</button>
  226. </div>';
  227. echo '<div>'.Display::display_icon('hotpotatoes.jpg',get_lang('HotPotatoes')).'</div>';
  228. echo '</div></div>';
  229. ?>
  230. <?php
  231. }
  232. // display the footer
  233. Display::display_footer();
  234. ?>