hotpotatoes.php 9.3 KB

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