dropbox_functions.inc.php 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  1. <?php //$id: $
  2. /* For licensing terms, see /dokeos_license.txt */
  3. /**
  4. * This file contains additional dropbox functions. Initially there were some
  5. * functions in the init files also but I have moved them over
  6. * to one file -- Patrick Cool <patrick.cool@UGent.be>, Ghent University
  7. */
  8. /**
  9. * This function is a wrapper function for the multiple actions feature.
  10. * @return Mixed If there is a problem, return a string message, otherwise nothing
  11. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  12. * @version march 2006
  13. */
  14. function handle_multiple_actions()
  15. {
  16. global $_user, $is_courseAdmin, $is_courseTutor;
  17. // STEP 1: are we performing the actions on the received or on the sent files?
  18. if($_POST['action']=='delete_received' || $_POST['action']=='download_received')
  19. {
  20. $part = 'received';
  21. }
  22. elseif($_POST['action']=='delete_sent' || $_POST['action']=='download_sent')
  23. {
  24. $part = 'sent';
  25. }
  26. // STEP 2: at least one file has to be selected. If not we return an error message
  27. foreach ($_POST as $key=>$value)
  28. {
  29. if (strstr($value,$part.'_') AND $key!='view_received_category' AND $key!='view_sent_category')
  30. {
  31. $checked_files=true;
  32. $checked_file_ids[]=intval(substr($value,strrpos($value,'_')));
  33. }
  34. }
  35. $checked_file_ids = $_POST['id'];
  36. if (!is_array($checked_file_ids) || count($checked_file_ids)==0)
  37. {
  38. return get_lang('CheckAtLeastOneFile');
  39. }
  40. // STEP 3A: deleting
  41. if ($_POST['action']=='delete_received' || $_POST['action']=='delete_sent')
  42. {
  43. $dropboxfile=new Dropbox_Person( $_user['user_id'], $is_courseAdmin, $is_courseTutor);
  44. foreach ($checked_file_ids as $key=>$value)
  45. {
  46. if ($_GET['view']=='received')
  47. {
  48. $dropboxfile->deleteReceivedWork($value);
  49. $message=get_lang('ReceivedFileDeleted');
  50. }
  51. if ($_GET['view']=='sent' OR empty($_GET['view']))
  52. {
  53. $dropboxfile->deleteSentWork($value);
  54. $message=get_lang('SentFileDeleted');
  55. }
  56. }
  57. return $message;
  58. }
  59. // STEP 3B: giving comment
  60. if ($_POST['actions']=='comment')
  61. {
  62. // This has not been implemented.
  63. // The idea was that it would be possible to write the same feedback for the selected documents.
  64. }
  65. // STEP 3C: moving
  66. if (strstr($_POST['action'], 'move_'))
  67. {
  68. // check move_received_n or move_sent_n command
  69. if (strstr($_POST['action'],'received')){
  70. $part = 'received';
  71. $to_cat_id = str_replace('move_received_','',$_POST['action']);
  72. }
  73. else {
  74. $part = 'sent';
  75. $to_cat_id = str_replace('move_sent_','',$_POST['action']);
  76. }
  77. foreach ($checked_file_ids as $key=>$value)
  78. {
  79. var_dump($value);
  80. var_dump($to_cat_id);
  81. var_dump($part);
  82. store_move($value, $to_cat_id, $part);
  83. }
  84. return get_lang('FilesMoved');
  85. }
  86. // STEP 3D: downloading
  87. if ($_POST['action']=='download_sent' || $_POST['action']=='download_received')
  88. {
  89. zip_download($checked_file_ids);
  90. }
  91. }
  92. /**
  93. * This function deletes a dropbox category
  94. *
  95. * @todo give the user the possibility what needs to be done with the files in this category: move them to the root, download them as a zip, delete them
  96. *
  97. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  98. * @version march 2006
  99. */
  100. function delete_category($action, $id)
  101. {
  102. global $dropbox_cnf;
  103. global $_user, $is_courseAdmin, $is_courseTutor;
  104. // an additional check that might not be necessary
  105. if ($action=='deletereceivedcategory')
  106. {
  107. $sentreceived='received';
  108. $entries_table=$dropbox_cnf['tbl_post'];
  109. $id_field='file_id';
  110. $return_message = get_lang('ReceivedCatgoryDeleted');
  111. }
  112. elseif ($action=='deletesentcategory')
  113. {
  114. $sentreceived='sent';
  115. $entries_table=$dropbox_cnf['tbl_file'];
  116. $id_field='id';
  117. $return_message = get_lang('SentCatgoryDeleted');
  118. }
  119. else
  120. {
  121. return get_lang('Error');
  122. }
  123. // step 1: delete the category
  124. $sql="DELETE FROM ".$dropbox_cnf['tbl_category']." WHERE cat_id='".Database::escape_string($id)."' AND $sentreceived='1'";
  125. $result=Database::query($sql,__FILE__,__LINE__);
  126. // step 2: delete all the documents in this category
  127. $sql="SELECT * FROM ".$entries_table." WHERE cat_id='".Database::escape_string($id)."'";
  128. $result=Database::query($sql,__FILE__,__LINE__);
  129. while ($row=Database::fetch_array($result))
  130. {
  131. $dropboxfile=new Dropbox_Person( $_user['user_id'], $is_courseAdmin, $is_courseTutor);
  132. if ($action=='deletereceivedcategory')
  133. {
  134. $dropboxfile->deleteReceivedWork($row[$id_field]);
  135. }
  136. if ($action=='deletesentcategory')
  137. {
  138. $dropboxfile->deleteSentWork($row[$id_field]);
  139. }
  140. }
  141. return $return_message;
  142. }
  143. /**
  144. * Displays the form to move one individual file to a category
  145. *
  146. * @return html code of the form that appears in a dokeos message box.
  147. *
  148. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  149. * @version march 2006
  150. */
  151. function display_move_form($part, $id, $target=array())
  152. {
  153. echo '<div class="row"><div class="form_header">'.get_lang('MoveFileTo').'</div></div>';
  154. echo '<form name="form1" method="post" action="'.api_get_self().'?view_received_category='.$_GET['view_received_category'].'&view_sent_category='.$_GET['view_sent_category'].'&view='.$_GET['view'].'">';
  155. echo '<input type="hidden" name="id" value="'.Security::remove_XSS($id).'">';
  156. echo '<input type="hidden" name="part" value="'.Security::remove_XSS($part).'">';
  157. echo '
  158. <div class="row">
  159. <div class="label">
  160. <span class="form_required">*</span> '.get_lang('MoveFileTo').'
  161. </div>
  162. <div class="formw">';
  163. echo '<select name="move_target">';
  164. echo '<option value="0">'.get_lang('Root').'</option>';
  165. foreach ($target as $key=>$category)
  166. {
  167. echo '<option value="'.$category['cat_id'].'">'.$category['cat_name'].'</option>';
  168. }
  169. echo '</select>';
  170. echo ' </div>
  171. </div>';
  172. echo '
  173. <div class="row">
  174. <div class="label">
  175. </div>
  176. <div class="formw">
  177. <button class="next" type="submit" name="do_move" value="'.get_lang('Ok').'">'.get_lang('MoveFile').'</button>
  178. </div>
  179. </div>
  180. ';
  181. echo '</form>';
  182. echo '<div style="clear: both;"></div>';
  183. }
  184. /**
  185. * This function moves a file to a different category
  186. *
  187. * @param $id the id of the file we are moving
  188. * @param $target the id of the folder we are moving to
  189. * @param $part are we moving a received file or a sent file?
  190. *
  191. * @return language string
  192. *
  193. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  194. * @version march 2006
  195. */
  196. function store_move($id, $target, $part)
  197. {
  198. global $_user;
  199. global $dropbox_cnf;
  200. if ((isset($id) AND $id<>'') AND (isset($target) AND $target<>'') AND (isset($part) AND $part<>''))
  201. {
  202. if ($part=='received')
  203. {
  204. $sql="UPDATE ".$dropbox_cnf["tbl_post"]." SET cat_id='".Database::escape_string($target)."'
  205. WHERE dest_user_id='".Database::escape_string($_user['user_id'])."'
  206. AND file_id='".Database::escape_string($id)."'
  207. ";
  208. Database::query($sql,__FILE__,__LINE__);
  209. $return_message=get_lang('ReceivedFileMoved');
  210. }
  211. if ($part=='sent')
  212. {
  213. $sql="UPDATE ".$dropbox_cnf["tbl_file"]." SET cat_id='".Database::escape_string($target)."'
  214. WHERE uploader_id='".Database::escape_string($_user['user_id'])."'
  215. AND id='".Database::escape_string($id)."'
  216. ";
  217. Database::query($sql,__FILE__,__LINE__);
  218. $return_message=get_lang('SentFileMoved');
  219. }
  220. }
  221. else
  222. {
  223. $return_message=get_lang('NotMovedError');
  224. }
  225. return $return_message;
  226. }
  227. /**
  228. * This functions displays all teh possible actions that can be performed on multiple files. This is the dropdown list that
  229. * appears below the sortable table of the sent / or received files.
  230. *
  231. * @return html value for the dropdown list
  232. *
  233. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  234. * @version march 2006
  235. */
  236. function display_action_options($part, $categories, $current_category=0)
  237. {
  238. echo '<select name="actions">';
  239. echo '<option value="download">'.get_lang('Download').'</option>';
  240. echo '<option value="delete">'.get_lang('Delete').'</option>';
  241. if(is_array($categories))
  242. {
  243. echo '<optgroup label="'.get_lang('MoveTo').'">';
  244. if ($current_category<>0)
  245. {
  246. echo '<option value="move_0">'.get_lang('Root').'</a>';
  247. }
  248. foreach ($categories as $key=>$value)
  249. {
  250. if( $current_category<>$value['cat_id'])
  251. {
  252. echo '<option value="move_'.$value['cat_id'].'">'.$value['cat_name'].'</option>';
  253. }
  254. }
  255. echo '</optgroup>';
  256. }
  257. echo '</select>';
  258. echo '<input type="submit" name="do_actions_'.Security::remove_XSS($part).'" value="'.get_lang('Ok').'" />';
  259. }
  260. /**
  261. * this function returns the html code that displays the checkboxes next to the files so that
  262. * multiple actions on one file are possible.
  263. *
  264. * @param $id the unique id of the file
  265. * @param $part are we dealing with a sent or with a received file?
  266. *
  267. * @return html code
  268. *
  269. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  270. * @version march 2006
  271. */
  272. function display_file_checkbox($id, $part)
  273. {
  274. if (isset($_GET['selectall']))
  275. {
  276. $checked='checked';
  277. }
  278. $return_value='<input type="checkbox" name="'.Security::remove_XSS($part).'_'.Security::remove_XSS($id).'" value="'.Security::remove_XSS($id).'" '.$checked.' />';
  279. return $return_value;
  280. }
  281. /**
  282. * This function retrieves all the dropbox categories and returns them as an array
  283. *
  284. * @param $filter default '', when we need only the categories of the sent or the received part.
  285. *
  286. * @return array
  287. *
  288. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  289. * @version march 2006
  290. */
  291. function get_dropbox_categories($filter='')
  292. {
  293. global $_user;
  294. global $dropbox_cnf;
  295. //echo '<h1>'.$filter.'</h1>';
  296. $return_array=array();
  297. $sql="SELECT * FROM ".$dropbox_cnf['tbl_category']." WHERE user_id='".$_user['user_id']."'";
  298. $result=Database::query($sql,__FILE__,__LINE__);
  299. while ($row=Database::fetch_array($result))
  300. {
  301. if(($filter=='sent' AND $row['sent']==1) OR ($filter=='received' AND $row['received']==1) OR $filter=='')
  302. {
  303. $return_array[$row['cat_id']]=$row;
  304. }
  305. }
  306. return $return_array;
  307. }
  308. /**
  309. * This functions stores a new dropboxcategory
  310. *
  311. * @var it might not seem very elegant if you create a category in sent and in received with the same name that you get two entries in the
  312. * dropbox_category table but it is the easiest solution. You get
  313. * cat_name | received | sent | user_id
  314. * test | 1 | 0 | 237
  315. * test | 0 | 1 | 237
  316. * more elegant would be
  317. * test | 1 | 1 | 237
  318. *
  319. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  320. * @version march 2006
  321. */
  322. function store_addcategory()
  323. {
  324. global $_user;
  325. global $dropbox_cnf;
  326. // check if the target is valid
  327. if ($_POST['target']=='sent')
  328. {
  329. $sent=1;
  330. $received=0;
  331. }
  332. elseif ($_POST['target']=='received')
  333. {
  334. $sent=0;
  335. $received=1;
  336. }
  337. else
  338. {
  339. return get_lang('Error');
  340. }
  341. // check if the category name is valid
  342. if ($_POST['category_name']=='')
  343. {
  344. return array('type' => 'error', 'message'=>get_lang('ErrorPleaseGiveCategoryName'));
  345. }
  346. if (!$_POST['edit_id'])
  347. {
  348. // step 3a, we check if the category doesn't already exist
  349. $sql="SELECT * FROM ".$dropbox_cnf['tbl_category']." WHERE user_id='".$_user['user_id']."' AND cat_name='".Database::escape_string(Security::remove_XSS($_POST['category_name']))."' AND received='".$received."' AND sent='".$sent."'";
  350. $result=Database::query($sql,__FILE__,__LINE__);
  351. // step 3b, we add the category if it does not exist yet.
  352. if (Database::num_rows($result)==0)
  353. {
  354. $sql="INSERT INTO ".$dropbox_cnf['tbl_category']." (cat_name, received, sent, user_id)
  355. VALUES ('".Database::escape_string(Security::remove_XSS($_POST['category_name']))."', '".Database::escape_string($received)."', '".Database::escape_string($sent)."', '".Database::escape_string($_user['user_id'])."')";
  356. Database::query($sql,__FILE__,__LINE__);
  357. return array('type' => 'confirmation', 'message'=>get_lang('CategoryStored'));
  358. }
  359. else
  360. {
  361. return array('type' => 'error', 'message'=>get_lang('CategoryAlreadyExistsEditIt'));
  362. }
  363. }
  364. else
  365. {
  366. $sql="UPDATE ".$dropbox_cnf['tbl_category']." SET cat_name='".Database::escape_string(Security::remove_XSS($_POST['category_name']))."', received='".Database::escape_string($received)."' , sent='".Database::escape_string($sent)."'
  367. WHERE user_id='".Database::escape_string($_user['user_id'])."'
  368. AND cat_id='".Database::escape_string(Security::remove_XSS($_POST['edit_id']))."'";
  369. Database::query($sql,__FILE__,__LINE__);
  370. return array('type' => 'confirmation', 'message'=>get_lang('CategoryModified'));
  371. }
  372. }
  373. /**
  374. * This function displays the form to add a new category.
  375. *
  376. * @param $category_name this parameter is the name of the category (used when no section is selected)
  377. * @param $id this is the id of the category we are editing.
  378. *
  379. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  380. * @version march 2006
  381. */
  382. function display_addcategory_form($category_name='', $id='',$action)
  383. {
  384. global $dropbox_cnf;
  385. $title=get_lang('AddNewCategory');
  386. if (isset($id) AND $id<>'')
  387. {
  388. // retrieve the category we are editing
  389. $sql="SELECT * FROM ".$dropbox_cnf['tbl_category']." WHERE cat_id='".Database::escape_string($id)."'";
  390. $result=Database::query($sql,__FILE__,__LINE__);
  391. $row=Database::fetch_array($result);
  392. if ($category_name=='') // after an edit with an error we do not want to return to the original name but the name we already modified. (happens when createinrecievedfiles AND createinsentfiles are not checked)
  393. {
  394. $category_name=$row['cat_name'];
  395. }
  396. if ($row['received']=='1')
  397. {
  398. $target='received';
  399. }
  400. if ($row['sent']=='1')
  401. {
  402. $target='sent';
  403. }
  404. $title=get_lang('EditCategory');
  405. }
  406. if ($action=='addreceivedcategory') {
  407. $target='received';
  408. }
  409. if ($action=='addsentcategory') {
  410. $target='sent';
  411. }
  412. if ($action=='editcategory') {
  413. $text=get_lang('ModifyCategory');
  414. $class='save';
  415. } else if ($action=='addreceivedcategory' or $action=='addsentcategory') {
  416. $text=get_lang('CreateCategory');
  417. $class='add';
  418. }
  419. echo "<form name=\"add_new_category\" method=\"post\" action=\"".api_get_self()."?view=".Security::remove_XSS($_GET['view'])."\">\n";
  420. if (isset($id) AND $id<>'')
  421. {
  422. echo '<input name="edit_id" type="hidden" value="'.Security::remove_XSS($id).'">';
  423. }
  424. echo '<input name="action" type="hidden" value="'.Security::remove_XSS($action).'">';
  425. echo '<input name="target" type="hidden" value="'.$target.'">';
  426. echo '<div class="row"><div class="form_header">'.$title.'</div></div>';
  427. echo ' <div class="row">
  428. <div class="label">
  429. <span class="form_required">*</span> '.get_lang('CategoryName').'
  430. </div>
  431. <div class="formw">';
  432. if ($_POST AND empty($_POST['category_name']))
  433. {
  434. echo '<span class="form_error">'.get_lang('ThisFieldIsRequired').'. '.get_lang('ErrorPleaseGiveCategoryName').'<span><br />';
  435. }
  436. if ($_POST AND !empty($_POST['category_name']))
  437. {
  438. echo '<span class="form_error">'.get_lang('CategoryAlreadyExistsEditIt').'<span><br />';
  439. }
  440. echo ' <input type="text" name="category_name" value="'.Security::remove_XSS($category_name).'" />
  441. </div>
  442. </div>';
  443. echo ' <div class="row">
  444. <div class="label">
  445. </div>
  446. <div class="formw">
  447. <button class="'.$class.'" type="submit" name="StoreCategory">'.$text.'</button>
  448. </div>
  449. </div>';
  450. echo ' <div class="row">
  451. <div class="label">
  452. </div>
  453. <div class="formw">
  454. <span class="form_required">*</span> <small>'.get_lang('ThisFieldIsRequired').'</small>
  455. </div>
  456. </div>';
  457. echo '</form>';
  458. echo '<div style="clear: both;"></div>';
  459. }
  460. /**
  461. * this function displays the form to upload a new item to the dropbox.
  462. *
  463. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  464. * @version march 2006
  465. */
  466. function display_add_form()
  467. {
  468. global $_user, $is_courseAdmin, $is_courseTutor, $course_info, $origin, $dropbox_unid;
  469. $token = Security::get_token();
  470. $dropbox_person = new Dropbox_Person( $_user['user_id'], $is_courseAdmin, $is_courseTutor);
  471. ?>
  472. <form method="post" action="index.php?view_received_category=<?php echo Security::remove_XSS($_GET['view_received_category']); ?>&view_sent_category=<?php echo Security::remove_XSS($_GET['view_sent_category']); ?>&view=<?php echo Security::remove_XSS($_GET['view']); ?>&<?php echo "origin=$origin"."&".api_get_cidreq(); ?>" enctype="multipart/form-data" onsubmit="return checkForm(this)">
  473. <div class="row"><div class="form_header"><?php echo get_lang('UploadNewFile'); ?></div></div>
  474. <div class="row">
  475. <div class="label">
  476. <span class="form_required">*</span><?php echo dropbox_lang("uploadFile")?>:
  477. </div>
  478. <div class="formw">
  479. <input type="hidden" name="MAX_FILE_SIZE" value='<?php echo dropbox_cnf("maxFilesize")?>' />
  480. <input type="file" name="file" size="20" <?php if (dropbox_cnf("allowOverwrite")) echo 'onChange="checkfile(this.value)"'; ?> />
  481. <input type="hidden" name="dropbox_unid" value="<?php echo $dropbox_unid ?>" />
  482. <input type="hidden" name="sec_token" value="<?php echo $token ?>" />
  483. <?php
  484. if ($origin=='learnpath')
  485. {
  486. echo '<input type="hidden" name="origin" value="learnpath" />';
  487. }
  488. ?>
  489. </div>
  490. </div>
  491. <?php
  492. if (dropbox_cnf("allowOverwrite"))
  493. {
  494. ?>
  495. <div class="row">
  496. <div class="label">
  497. </div>
  498. <div class="formw">
  499. <input type="checkbox" name="cb_overwrite" id="cb_overwrite" value="true" /><?php echo dropbox_lang("overwriteFile")?>
  500. </div>
  501. </div>
  502. <?php
  503. }
  504. ?>
  505. <div class="row">
  506. <div class="label">
  507. <?php echo dropbox_lang("sendTo")?>
  508. </div>
  509. <div class="formw">
  510. <?php
  511. //list of all users in this course and all virtual courses combined with it
  512. if(isset($_SESSION['id_session'])){
  513. $complete_user_list_for_dropbox = array();
  514. if(api_get_setting('dropbox_allow_student_to_student')=='true' || $_user['status'] != STUDENT)
  515. {
  516. $complete_user_list_for_dropbox = CourseManager :: get_user_list_from_course_code($course_info['code'],true,$_SESSION['id_session']);
  517. }
  518. $complete_user_list2 = CourseManager :: get_coach_list_from_course_code($course_info['code'],$_SESSION['id_session']);
  519. $complete_user_list_for_dropbox = array_merge($complete_user_list_for_dropbox,$complete_user_list2);
  520. }
  521. else{
  522. if(api_get_setting('dropbox_allow_student_to_student')=='true' || $_user['status'] != STUDENT)
  523. {
  524. $complete_user_list_for_dropbox = CourseManager :: get_user_list_from_course_code($course_info['code'],true,$_SESSION['id_session']);
  525. }
  526. else
  527. {
  528. $complete_user_list_for_dropbox = CourseManager :: get_teacher_list_from_course_code($course_info['code']);
  529. }
  530. }
  531. foreach ($complete_user_list_for_dropbox as $k => $e) {
  532. $complete_user_list_for_dropbox[$k] = $e + array('lastcommafirst' => api_get_person_name($e['firstname'], $e['lastname']));
  533. }
  534. $complete_user_list_for_dropbox = TableSort::sort_table($complete_user_list_for_dropbox, 'lastcommafirst');
  535. ?>
  536. <select name="recipients[]" size="
  537. <?php
  538. if ( $dropbox_person -> isCourseTutor || $dropbox_person -> isCourseAdmin)
  539. {
  540. echo 10;
  541. }
  542. else
  543. {
  544. echo 6;
  545. }
  546. ?>" multiple style="width: 350px;">
  547. <?php
  548. /*
  549. Create the options inside the select box:
  550. List all selected users their user id as value and a name string as display
  551. */
  552. foreach ($complete_user_list_for_dropbox as $current_user)
  553. {
  554. if ( ($dropbox_person -> isCourseTutor
  555. || $dropbox_person -> isCourseAdmin
  556. || dropbox_cnf("allowStudentToStudent") // RH: also if option is set
  557. || $current_user['status']!=5 // always allow teachers
  558. || $current_user['tutor_id']==1 // always allow tutors
  559. ) && $current_user['user_id'] != $_user['user_id'] ) // don't include yourself
  560. {
  561. $full_name = $current_user['lastcommafirst'];
  562. echo '<option value="user_' . $current_user['user_id'] . '">' . $full_name . '</option>';
  563. }
  564. }
  565. /*
  566. * Show groups
  567. */
  568. if ( ($dropbox_person -> isCourseTutor || $dropbox_person -> isCourseAdmin)
  569. && dropbox_cnf("allowGroup") || dropbox_cnf("allowStudentToStudent"))
  570. {
  571. $complete_group_list_for_dropbox = GroupManager::get_group_list(null,dropbox_cnf("courseId"));
  572. if (count($complete_group_list_for_dropbox) > 0)
  573. {
  574. foreach ($complete_group_list_for_dropbox as $current_group)
  575. {
  576. if ($current_group['number_of_members'] > 0)
  577. {
  578. echo '<option value="group_'.$current_group['id'].'">G: '.$current_group['name'].' - '.$current_group['number_of_members'].' '.get_lang('Users').'</option>';
  579. }
  580. }
  581. }
  582. }
  583. if ( ($dropbox_person -> isCourseTutor || $dropbox_person -> isCourseAdmin) && dropbox_cnf("allowMailing")) // RH: Mailing starting point
  584. {
  585. // echo '<option value="mailing">'.dropbox_lang("mailingInSelect").'</option>';
  586. }
  587. if ( dropbox_cnf("allowJustUpload")) // RH
  588. {
  589. //echo '<option value="upload">'.dropbox_lang("justUploadInSelect").'</option>';
  590. echo '<option value="user_'.$_user['user_id'].'">'.dropbox_lang("justUploadInSelect").'</option>';
  591. }
  592. echo '</select>
  593. </div>
  594. </div>';
  595. echo '
  596. <div class="row">
  597. <div class="label">
  598. </div>
  599. <div class="formw">
  600. <button type="Submit" class="upload" name="submitWork">'.dropbox_lang("upload", "noDLTT").'</button>
  601. </div>
  602. </div>
  603. ';
  604. echo "</form>";
  605. }
  606. /**
  607. * returns username or false if user isn't registered anymore
  608. * @todo check if this function is still necessary. There might be a library function for this.
  609. */
  610. function getUserNameFromId ( $id) // RH: Mailing: return 'Mailing ' + id
  611. {
  612. $mailingId = $id - dropbox_cnf("mailingIdBase");
  613. if ( $mailingId > 0)
  614. {
  615. return dropbox_lang("mailingAsUsername", "noDLTT") . $mailingId;
  616. }
  617. $id = intval($id);
  618. $sql = "SELECT ".(api_is_western_name_order() ? "CONCAT(firstname,' ', lastname)" : "CONCAT(lastname,' ', firstname)")." AS name
  619. FROM " . dropbox_cnf("tbl_user") . "
  620. WHERE user_id='$id'";
  621. $result = Database::query($sql,__FILE__,__LINE__);
  622. $res = Database::fetch_array( $result);
  623. if ( $res == FALSE) return FALSE;
  624. return stripslashes( $res["name"]);
  625. }
  626. /**
  627. * returns loginname or false if user isn't registered anymore
  628. * @todo check if this function is still necessary. There might be a library function for this.
  629. */
  630. function getLoginFromId ( $id)
  631. {
  632. $id = intval($id);
  633. $sql = "SELECT username
  634. FROM " . dropbox_cnf("tbl_user") . "
  635. WHERE user_id='$id'";
  636. $result =Database::query($sql,__FILE__,__LINE__);
  637. $res = Database::fetch_array( $result);
  638. if ( $res == FALSE) return FALSE;
  639. return stripslashes( $res["username"]);
  640. }
  641. /**
  642. * @return boolean indicating if user with user_id=$user_id is a course member
  643. * @todo eliminate global
  644. * @todo check if this function is still necessary. There might be a library function for this.
  645. */
  646. function isCourseMember( $user_id)
  647. {
  648. global $_course;
  649. $course_code = $_course['sysCode'];
  650. $is_course_member = CourseManager::is_user_subscribed_in_course($user_id, $course_code,true);
  651. return $is_course_member;
  652. }
  653. /**
  654. * Checks if there are files in the dropbox_file table that aren't used anymore in dropbox_person table.
  655. * If there are, all entries concerning the file are deleted from the db + the file is deleted from the server
  656. */
  657. function removeUnusedFiles( )
  658. {
  659. // select all files that aren't referenced anymore
  660. $sql = "SELECT DISTINCT f.id, f.filename
  661. FROM " . dropbox_cnf("tbl_file") . " f
  662. LEFT JOIN " . dropbox_cnf("tbl_person") . " p ON f.id = p.file_id
  663. WHERE p.user_id IS NULL";
  664. $result = Database::query($sql,__FILE__,__LINE__);
  665. while ( $res = Database::fetch_array( $result))
  666. {
  667. //delete the selected files from the post and file tables
  668. $sql = "DELETE FROM " . dropbox_cnf("tbl_post") . " WHERE file_id='" . $res['id'] . "'";
  669. $result1 = Database::query($sql,__FILE__,__LINE__);
  670. $sql = "DELETE FROM " . dropbox_cnf("tbl_file") . " WHERE id='" . $res['id'] . "'";
  671. $result1 = Database::query($sql,__FILE__,__LINE__);
  672. //delete file from server
  673. @unlink( dropbox_cnf("sysPath") . "/" . $res["filename"]);
  674. }
  675. }
  676. /**
  677. * RH: Mailing (2 new functions)
  678. *
  679. * Mailing zip-file is posted to (dest_user_id = ) mailing pseudo_id
  680. * and is only visible to its uploader (user_id).
  681. *
  682. * Mailing content files have uploader_id == mailing pseudo_id, a normal recipient,
  683. * and are visible initially to recipient and pseudo_id.
  684. *
  685. * @author René Haentjens, Ghent University
  686. *
  687. * @todo check if this function is still necessary.
  688. */
  689. function getUserOwningThisMailing($mailingPseudoId, $owner = 0, $or_die = '')
  690. {
  691. $mailingPseudoId = intval($mailingPseudoId);
  692. $sql = "SELECT f.uploader_id
  693. FROM " . dropbox_cnf("tbl_file") . " f
  694. LEFT JOIN " . dropbox_cnf("tbl_post") . " p ON f.id = p.file_id
  695. WHERE p.dest_user_id = '" . $mailingPseudoId . "'";
  696. $result = Database::query($sql,__FILE__,__LINE__);
  697. if (!($res = Database::fetch_array($result)))
  698. die(dropbox_lang("generalError")." (code 901)");
  699. if ($owner == 0) return $res['uploader_id'];
  700. if ($res['uploader_id'] == $owner) return TRUE;
  701. die(dropbox_lang("generalError")." (code ".$or_die.")");
  702. }
  703. /**
  704. * @author Ren� Haentjens, Ghent University
  705. * @todo check if this function is still necessary.
  706. */
  707. function removeMoreIfMailing($file_id)
  708. {
  709. // when deleting a mailing zip-file (posted to mailingPseudoId):
  710. // 1. the detail window is no longer reachable, so
  711. // for all content files, delete mailingPseudoId from person-table
  712. // 2. finding the owner (getUserOwningThisMailing) is no longer possible, so
  713. // for all content files, replace mailingPseudoId by owner as uploader
  714. $file_id = intval($file_id);
  715. $sql = "SELECT p.dest_user_id
  716. FROM " . dropbox_cnf("tbl_post") . " p
  717. WHERE p.file_id = '" . $file_id . "'";
  718. $result = Database::query($sql,__FILE__,__LINE__);
  719. if ( $res = Database::fetch_array( $result))
  720. {
  721. $mailingPseudoId = $res['dest_user_id'];
  722. if ( $mailingPseudoId > dropbox_cnf("mailingIdBase"))
  723. {
  724. $sql = "DELETE FROM " . dropbox_cnf("tbl_person") . " WHERE user_id='" . $mailingPseudoId . "'";
  725. $result1 = Database::query($sql,__FILE__,__LINE__);
  726. $sql = "UPDATE " . dropbox_cnf("tbl_file") .
  727. " SET uploader_id='" . api_get_user_id() . "' WHERE uploader_id='" . $mailingPseudoId . "'";
  728. $result1 = Database::query($sql,__FILE__,__LINE__);
  729. }
  730. }
  731. }
  732. /**
  733. * The dropbox has a deviant naming scheme for language files so it needs an additional language function
  734. *
  735. * @todo check if this function is still necessary.
  736. *
  737. * @author René Haentjens, Ghent University
  738. */
  739. function dropbox_lang($variable, $notrans = 'DLTT')
  740. {
  741. global $charset;
  742. return (api_get_setting('server_type') == 'test' ?
  743. get_lang('dropbox_lang["'.$variable.'"]', $notrans) :
  744. api_html_entity_decode(api_to_system_encoding(str_replace("\\'", "'", $GLOBALS['dropbox_lang'][$variable]), null, true), ENT_QUOTES, $charset));
  745. }
  746. /**
  747. * Function that finds a given config setting
  748. *
  749. * @author René Haentjens, Ghent University
  750. */
  751. function dropbox_cnf($variable)
  752. {
  753. return $GLOBALS['dropbox_cnf'][$variable];
  754. }
  755. /**
  756. *
  757. */
  758. function store_add_dropbox()
  759. {
  760. global $dropbox_cnf;
  761. global $_user;
  762. global $_course;
  763. // ----------------------------------------------------------
  764. // Validating the form data
  765. // ----------------------------------------------------------
  766. // the author is
  767. /*
  768. if (!isset( $_POST['authors']))
  769. {
  770. return get_lang('AuthorFieldCannotBeEmpty');
  771. }
  772. */
  773. // there are no recipients selected
  774. if ( !isset( $_POST['recipients']) || count( $_POST['recipients']) <= 0)
  775. {
  776. return get_lang('YouMustSelectAtLeastOneDestinee');
  777. }
  778. // Check if all the recipients are valid
  779. else
  780. {
  781. $thisIsAMailing = FALSE; // RH: Mailing selected as destination
  782. $thisIsJustUpload = FALSE; // RH
  783. foreach( $_POST['recipients'] as $rec)
  784. {
  785. if ( $rec == 'mailing')
  786. {
  787. $thisIsAMailing = TRUE;
  788. }
  789. elseif ( $rec == 'upload')
  790. {
  791. $thisIsJustUpload = TRUE;
  792. }
  793. elseif (strpos($rec, 'user_') === 0 && !isCourseMember(substr($rec, strlen('user_') ) ))
  794. {
  795. return get_lang('InvalideUserDetected');
  796. }
  797. elseif (strpos($rec, 'group_') !== 0 && strpos($rec, 'user_') !== 0)
  798. {
  799. return get_lang('InvalideGroupDetected');
  800. }
  801. }
  802. }
  803. // we are doing a mailing but an additional recipient is selected
  804. if ( $thisIsAMailing && ( count($_POST['recipients']) != 1))
  805. {
  806. return get_lang('MailingSelectNoOther');
  807. }
  808. // we are doing a just upload but an additional recipient is selected.
  809. // note: why can't this be valid? It is like sending a document to yourself AND to a different person (I do this quite often with my e-mails)
  810. if ( $thisIsJustUpload && ( count($_POST['recipients']) != 1))
  811. {
  812. return get_lang('mailingJustUploadSelectNoOther');
  813. }
  814. if ( empty( $_FILES['file']['name']))
  815. {
  816. $error = TRUE;
  817. return get_lang('NoFileSpecified');
  818. }
  819. // ----------------------------------------------------------
  820. // are we overwriting a previous file or sending a new one
  821. // ----------------------------------------------------------
  822. $dropbox_overwrite = false;
  823. if ( isset($_POST['cb_overwrite']) && $_POST['cb_overwrite']==true)
  824. {
  825. $dropbox_overwrite = true;
  826. }
  827. // ----------------------------------------------------------
  828. // doing the upload
  829. // ----------------------------------------------------------
  830. $dropbox_filename = $_FILES['file']['name'];
  831. $dropbox_filesize = $_FILES['file']['size'];
  832. $dropbox_filetype = $_FILES['file']['type'];
  833. $dropbox_filetmpname = $_FILES['file']['tmp_name'];
  834. // check if the filesize does not exceed the allowed size.
  835. if ( $dropbox_filesize <= 0 || $dropbox_filesize > $dropbox_cnf["maxFilesize"])
  836. {
  837. return get_lang('DropboxFileTooBig');
  838. }
  839. // check if the file is actually uploaded
  840. if ( !is_uploaded_file( $dropbox_filetmpname)) // check user fraud : no clean error msg.
  841. {
  842. return get_lang('TheFileIsNotUploaded');
  843. }
  844. // Try to add an extension to the file if it hasn't got one
  845. $dropbox_filename = add_ext_on_mime( $dropbox_filename,$dropbox_filetype);
  846. // Replace dangerous characters
  847. $dropbox_filename = replace_dangerous_char( $dropbox_filename);
  848. // Transform any .php file in .phps fo security
  849. $dropbox_filename = php2phps ( $dropbox_filename);
  850. //filter extension
  851. if(!filter_extension($dropbox_filename))
  852. {
  853. return get_lang('UplUnableToSaveFileFilteredExtension');
  854. }
  855. // set title
  856. $dropbox_title = $dropbox_filename;
  857. // set author
  858. if ( $_POST['authors'] == '')
  859. {
  860. $_POST['authors'] = getUserNameFromId( $_user['user_id']);
  861. }
  862. // note: I think we could better migrate everything from here on to separate functions: store_new_dropbox, store_new_mailing, store_just_upload
  863. if ($dropbox_overwrite) // RH: Mailing: adapted
  864. {
  865. $dropbox_person = new Dropbox_Person( $_user['user_id'], api_is_course_admin(), api_is_course_tutor());
  866. foreach($dropbox_person->sentWork as $w)
  867. {
  868. if ($w->title == $dropbox_filename)
  869. {
  870. if ( ($w->recipients[0]['id'] > dropbox_cnf("mailingIdBase")) xor $thisIsAMailing)
  871. {
  872. return get_lang('MailingNonMailingError');
  873. }
  874. if ( ($w->recipients[0]['id'] == $_user['user_id']) xor $thisIsJustUpload)
  875. {
  876. return get_lang('MailingJustUploadSelectNoOther');
  877. }
  878. $dropbox_filename = $w->filename;
  879. $found = true; // note: do we still need this?
  880. break;
  881. }
  882. }
  883. }
  884. else // rename file to login_filename_uniqueId format
  885. {
  886. $dropbox_filename = getLoginFromId( $_user['user_id']) . "_" . $dropbox_filename . "_".uniqid('');
  887. }
  888. // creating the array that contains all the users who will receive the file
  889. $new_work_recipients = array();
  890. foreach ($_POST["recipients"] as $rec)
  891. {
  892. if (strpos($rec, 'user_') === 0)
  893. {
  894. $new_work_recipients[] = substr($rec, strlen('user_') );
  895. }
  896. elseif (strpos($rec, 'group_') === 0 )
  897. {
  898. $userList = GroupManager::get_subscribed_users(substr($rec, strlen('group_') ));
  899. foreach ($userList as $usr)
  900. {
  901. if (! in_array($usr['user_id'], $new_work_recipients) && $usr['user_id'] != $_user['user_id'])
  902. {
  903. $new_work_recipients[] = $usr['user_id'];
  904. }
  905. }
  906. }
  907. }
  908. @move_uploaded_file( $dropbox_filetmpname, dropbox_cnf("sysPath") . '/' . $dropbox_filename);
  909. $b_send_mail = api_get_course_setting('email_alert_on_new_doc_dropbox');
  910. if($b_send_mail)
  911. {
  912. foreach($new_work_recipients as $recipient_id)
  913. {
  914. include_once(api_get_path(LIBRARY_PATH) . 'usermanager.lib.php');
  915. $recipent_temp=UserManager :: get_user_info_by_id($recipient_id);
  916. api_mail(api_get_person_name($recipent_temp['firstname'].' '.$recipent_temp['lastname'], null, PERSON_NAME_EMAIL_ADDRESS), $recipent_temp['email'],
  917. get_lang('NewDropboxFileUploaded'),
  918. get_lang('NewDropboxFileUploadedContent').' '.api_get_path(WEB_CODE_PATH).'dropbox/index.php?cidReq='.$_course['sysCode']."\n\n".api_get_person_name($_user['firstName'], $_user['lastName'], null, PERSON_NAME_EMAIL_ADDRESS)."\n". get_lang('Email') ." : ".$_user['mail'], api_get_person_name($_user['firstName'], $_user['lastName'], null, PERSON_NAME_EMAIL_ADDRESS), $_user['mail']);
  919. //get_lang('NewDropboxFileUploadedContent').' '.api_get_path(WEB_CODE_PATH).'dropbox/index.php?cidReq='.$_course['sysCode']."\n\n".api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS)."\n". get_lang('Manager'). " ".api_get_setting('siteName')."\n" .get_lang('Email') ." : ".api_get_setting('emailAdministrator'), api_get_person_name(api_get_setting('administratorName')." ".api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS), api_get_setting('emailAdministrator'));
  920. }
  921. }
  922. new Dropbox_SentWork( $_user['user_id'], $dropbox_title, $_POST['description'], strip_tags($_POST['authors']), $dropbox_filename, $dropbox_filesize, $new_work_recipients);
  923. Security::clear_token();
  924. return get_lang('FileUploadSucces');
  925. }
  926. /**
  927. * This function displays the firstname and lastname of the user as a link to the user tool.
  928. *
  929. * @see this is the same function as in the new forum, so this probably has to move to a user library.
  930. *
  931. * @todo move this function to the user library
  932. *
  933. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  934. * @version march 2006
  935. */
  936. function display_user_link($user_id, $name='')
  937. {
  938. global $_otherusers;
  939. if ($user_id<>0)
  940. {
  941. if ($name=='')
  942. {
  943. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  944. $sql="SELECT * FROM $table_user WHERE user_id='".Database::escape_string($user_id)."'";
  945. $result=Database::query($sql,__FILE__,__LINE__);
  946. $row=Database::fetch_array($result);
  947. return "<a href=\"../user/userInfo.php?uInfo=".$row['user_id']."\">".api_get_person_name($row['firstname'], $row['lastname'])."</a>";
  948. }
  949. else
  950. {
  951. $user_id = intval($user_id);
  952. return "<a href=\"../user/userInfo.php?uInfo=".$user_id."\">".Security::remove_XSS($name)."</a>";
  953. }
  954. }
  955. else
  956. {
  957. return $name.' ('.get_lang('Anonymous').')';
  958. }
  959. }
  960. /**
  961. * this function transforms the array containing all the feedback into something visually attractive.
  962. *
  963. * @param an array containing all the feedback about the given message.
  964. *
  965. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  966. * @version march 2006
  967. */
  968. function feedback($array)
  969. {
  970. foreach ($array as $key=>$value)
  971. {
  972. $output.=format_feedback($value);
  973. }
  974. $output.=feedback_form();
  975. return $output;
  976. }
  977. /**
  978. * This function returns the html code to display the feedback messages on a given dropbox file
  979. * @param $feedback_array an array that contains all the feedback messages about the given document.
  980. * @return html code
  981. * @todo add the form for adding new comment (if the other party has not deleted it yet).
  982. *
  983. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  984. * @version march 2006
  985. */
  986. function format_feedback($feedback)
  987. {
  988. $output.=display_user_link($feedback['author_user_id']);
  989. $output.='&nbsp;&nbsp;['.$feedback['feedback_date'].']<br />';
  990. $output.='<div style="padding-top:6px">'.nl2br($feedback['feedback']).'</div><hr size="1" noshade/><br />';
  991. return $output;
  992. }
  993. /**
  994. * this function returns the code for the form for adding a new feedback message to a dropbox file.
  995. * @return html code
  996. *
  997. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  998. * @version march 2006
  999. */
  1000. function feedback_form()
  1001. {
  1002. global $dropbox_cnf;
  1003. $return = get_lang('AddNewFeedback').'<br />';
  1004. // we now check if the other users have not delete this document yet. If this is the case then it is useless to see the
  1005. // add feedback since the other users will never get to see the feedback.
  1006. $sql="SELECT * FROM ".$dropbox_cnf["tbl_person"]." WHERE file_id='".Database::escape_string($_GET['id'])."'";
  1007. $result=Database::query($sql,__LINE__, __FILE__);
  1008. $number_users_who_see_file=Database::num_rows($result);
  1009. if ($number_users_who_see_file>1)
  1010. {
  1011. $return .= '<textarea name="feedback" style="width: 80%; height: 80px;"></textarea><br /><button type="submit" class="add" name="store_feedback" value="'.get_lang('Ok').'"
  1012. onclick="document.form_tablename.attributes.action.value = document.location;">'.get_lang('AddComment').'</button>';
  1013. }
  1014. else
  1015. {
  1016. $return .= get_lang('AllUsersHaveDeletedTheFileAndWillNotSeeFeedback');
  1017. }
  1018. return $return;
  1019. }
  1020. /**
  1021. * @return a language string (depending on the success or failure.
  1022. *
  1023. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1024. * @version march 2006
  1025. */
  1026. function store_feedback()
  1027. {
  1028. global $dropbox_cnf;
  1029. global $_user;
  1030. if (!is_numeric($_GET['id']))
  1031. {
  1032. return get_lang('FeedbackError');
  1033. }
  1034. if ($_POST['feedback']=='')
  1035. {
  1036. return get_lang('PleaseTypeText');
  1037. }
  1038. else
  1039. {
  1040. $sql="INSERT INTO ".$dropbox_cnf['tbl_feedback']." (file_id, author_user_id, feedback, feedback_date) VALUES
  1041. ('".Database::escape_string($_GET['id'])."','".Database::escape_string($_user['user_id'])."','".Database::escape_string($_POST['feedback'])."',NOW())";
  1042. Database::query($sql,__FILE__,__LINE__);
  1043. return get_lang('DropboxFeedbackStored');
  1044. }
  1045. }
  1046. /**
  1047. * This function downloads all the files of the inputarray into one zip
  1048. * @param $array an array containing all the ids of the files that have to be downloaded.
  1049. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1050. * @todo consider removing the check if the user has received or sent this file (zip download of a folder already sufficiently checks for this).
  1051. * @todo integrate some cleanup function that removes zip files that are older than 2 days
  1052. *
  1053. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1054. * @version march 2006
  1055. */
  1056. function zip_download ($array)
  1057. {
  1058. global $_course;
  1059. global $dropbox_cnf;
  1060. global $_user;
  1061. global $files;
  1062. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  1063. // zip library for creation of the zipfile
  1064. include(api_get_path(LIBRARY_PATH)."/pclzip/pclzip.lib.php");
  1065. // place to temporarily stash the zipfiles
  1066. $temp_zip_dir = api_get_path(SYS_COURSE_PATH).$_course['path']."/temp/";
  1067. // create the directory if it does not exist yet.
  1068. if(!is_dir($temp_zip_dir))
  1069. {
  1070. mkdir($temp_zip_dir);
  1071. }
  1072. cleanup_temp_dropbox();
  1073. $files='';
  1074. // note: we also have to add the check if the user has received or sent this file. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  1075. $sql="SELECT distinct file.filename, file.title, file.author, file.description
  1076. FROM ".$dropbox_cnf["tbl_file"]." file, ".$dropbox_cnf["tbl_person"]." person
  1077. WHERE file.id IN (".implode(', ',$array).")
  1078. AND file.id=person.file_id
  1079. AND person.user_id='".$_user['user_id']."'";
  1080. $result=Database::query($sql,__FILE__,__LINE__);
  1081. while ($row=Database::fetch_array($result))
  1082. {
  1083. $files[$row['filename']]=array('filename'=>$row['filename'],'title'=>$row['title'], 'author'=>$row['author'], 'description'=>$row['description']);
  1084. }
  1085. //$alternative is a variable that uses an alternative method to create the zip
  1086. // because the renaming of the files inside the zip causes error on php5 (unexpected end of archive)
  1087. $alternative=true;
  1088. if ($alternative)
  1089. {
  1090. zip_download_alternative($files);
  1091. exit;
  1092. }
  1093. // create the zip file
  1094. $name = 'dropboxdownload-'.$_user['user_id'].'-'.mktime().'.zip';
  1095. $temp_zip_file=$temp_zip_dir.'/'.$name;
  1096. $zip_folder=new PclZip($temp_zip_file);
  1097. foreach ($files as $key=>$value)
  1098. {
  1099. // met hernoemen van de files in de zip
  1100. $zip_folder->add(api_get_path(SYS_COURSE_PATH).$_course['path']."/dropbox/".$value['filename'],PCLZIP_OPT_REMOVE_PATH, api_get_path(SYS_COURSE_PATH).$_course['path']."/dropbox", PCLZIP_CB_PRE_ADD, 'my_pre_add_callback');
  1101. // zonder hernoemen van de files in de zip
  1102. //$zip_folder->add(api_get_path(SYS_COURSE_PATH).$_course['path']."/dropbox/".$value['filename'],PCLZIP_OPT_REMOVE_PATH, api_get_path(SYS_COURSE_PATH).$_course['path']."/dropbox");
  1103. }
  1104. // create the overview file
  1105. $overview_file_content=generate_html_overview($files, array('filename'), array('title'));
  1106. $overview_file=$temp_zip_dir.'/overview.html';
  1107. $handle=fopen($overview_file,'w');
  1108. fwrite($handle,$overview_file_content);
  1109. // send the zip file
  1110. DocumentManager::file_send_for_download($temp_zip_file,true,$name);
  1111. exit;
  1112. }
  1113. /**
  1114. * This is a callback function to decrypt the files in the zip file to their normal filename (as stored in the database)
  1115. * @param $p_event a variable of PCLZip
  1116. * @param $p_header a variable of PCLZip
  1117. *
  1118. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1119. * @version march 2006
  1120. */
  1121. function my_pre_add_callback($p_event, &$p_header)
  1122. {
  1123. global $files;
  1124. $p_header['stored_filename']=$files[$p_header['stored_filename']]['title'];
  1125. return 1;
  1126. }
  1127. /**
  1128. * This function is an alternative zip download. It was added because PCLZip causes problems on PHP5 when using PCLZIP_CB_PRE_ADD and a callback function to rename
  1129. * the files inside the zip file (dropbox scrambles the files to prevent
  1130. * @todo consider using a htaccess that denies direct access to the file but only allows the php file to access it. This would remove the scrambling requirement
  1131. * but it would require additional checks to see if the filename of the uploaded file is not used yet.
  1132. * @param $files is an associative array that contains the files that the user wants to download (check to see if the user is allowed to download these files already
  1133. * happened so the array is clean!!. The key is the filename on the filesystem. The value is an array that contains both the filename on the filesystem and
  1134. * the original filename (that will be used in the zip file)
  1135. * @todo when we copy the files there might be two files with the same name. We need a function that (recursively) checks this and changes the name
  1136. *
  1137. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1138. * @version march 2006
  1139. */
  1140. function zip_download_alternative($files)
  1141. {
  1142. global $_course;
  1143. global $_user;
  1144. $temp_zip_dir = api_get_path(SYS_COURSE_PATH).$_course['path']."/temp/";
  1145. // Step 2: we copy all the original dropbox files to the temp folder and change their name into the original name
  1146. foreach ($files as $key=>$value)
  1147. {
  1148. $value['title']=check_file_name(api_strtolower($value['title']));
  1149. $files[$value['filename']]['title']=$value['title'];
  1150. copy(api_get_path(SYS_COURSE_PATH).$_course['path']."/dropbox/".$value['filename'], api_get_path(SYS_COURSE_PATH).$_course['path']."/temp/".$value['title']);
  1151. }
  1152. // Step 3: create the zip file and add all the files to it
  1153. $temp_zip_file=$temp_zip_dir.'/dropboxdownload-'.$_user['user_id'].'-'.mktime().'.zip';
  1154. $zip_folder=new PclZip($temp_zip_file);
  1155. foreach ($files as $key=>$value)
  1156. {
  1157. $zip_folder->add(api_get_path(SYS_COURSE_PATH).$_course['path']."/temp/".$value['title'],PCLZIP_OPT_REMOVE_PATH, api_get_path(SYS_COURSE_PATH).$_course['path']."/temp");
  1158. }
  1159. // Step 1: create the overview file and add it to the zip
  1160. $overview_file_content=generate_html_overview($files, array('filename'), array('title'));
  1161. $overview_file=$temp_zip_dir.'overview'.replace_dangerous_char(api_is_western_name_order() ? $_user['firstname'].$_user['lastname'] : $_user['lastname'].$_user['firstname'], 'strict').'.html';
  1162. $handle=fopen($overview_file,'w');
  1163. fwrite($handle,$overview_file_content);
  1164. // todo: find a different solution for this because even 2 seconds is no guarantee.
  1165. sleep(2);
  1166. // Step 4: we add the overview file
  1167. $zip_folder->add($overview_file,PCLZIP_OPT_REMOVE_PATH, api_get_path(SYS_COURSE_PATH).$_course['path']."/temp");
  1168. // Step 5: send the file for download;
  1169. DocumentManager::file_send_for_download($temp_zip_file,true);
  1170. // Step 6: remove the files in the temp dir
  1171. foreach ($files as $key=>$value)
  1172. {
  1173. unlink(api_get_path(SYS_COURSE_PATH).$_course['path']."/temp/".$value['title']);
  1174. }
  1175. //unlink($overview_file);
  1176. exit;
  1177. }
  1178. /**
  1179. * @desc This function checks if the real filename of the dropbox files doesn't already exist in the temp folder. If this is the case then
  1180. * it will generate a different filename;
  1181. *
  1182. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1183. * @version march 2006
  1184. */
  1185. function check_file_name($file_name_2_check, $counter=0)
  1186. {
  1187. global $_course;
  1188. $new_file_name=$file_name_2_check;
  1189. if ($counter<>0)
  1190. {
  1191. $new_file_name=$counter.$new_file_name;
  1192. }
  1193. if (!file_exists(api_get_path(SYS_COURSE_PATH).$_course['path']."/temp/".$new_file_name))
  1194. {
  1195. return $new_file_name;
  1196. }
  1197. else
  1198. {
  1199. $counter++;
  1200. $new_file_name=check_file_name($file_name_2_check,$counter);
  1201. return $new_file_name;
  1202. }
  1203. }
  1204. /**
  1205. * @desc Cleans the temp zip files that were created when users download several files or a whole folder at once.
  1206. * T
  1207. * @return true
  1208. * @todo
  1209. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1210. * @version march 2006
  1211. */
  1212. function cleanup_temp_dropbox()
  1213. {
  1214. global $_course;
  1215. $handle=opendir(api_get_path(SYS_COURSE_PATH).$_course['path']."/temp");
  1216. while (false !== ($file = readdir($handle)))
  1217. {
  1218. if ($file<>'.' OR $file<>'..')
  1219. {
  1220. $name=str_replace('.zip', '',$file);
  1221. $name_part=explode('-',$name);
  1222. $timestamp_of_file=$name_part[count($name_part)-1];
  1223. // if it is a dropboxdownloadfile and the file is older than one day then we delete it
  1224. if (strstr($file, 'dropboxdownload') AND $timestamp_of_file<(mktime()-86400))
  1225. {
  1226. unlink(api_get_path(SYS_COURSE_PATH).$_course['path']."/temp/".$file);
  1227. }
  1228. }
  1229. }
  1230. closedir($handle);
  1231. return true;
  1232. }
  1233. /**
  1234. * @desc generates the contents of a html file that gives an overview of all the files in the zip file.
  1235. * This is to know the information of the files that are inside the zip file (who send it, the comment, ...)
  1236. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1237. * @version march 2006
  1238. */
  1239. function generate_html_overview($files, $dont_show_columns=array(), $make_link=array())
  1240. {
  1241. $return="<html>\n<head>\n\t<title>".get_lang('OverviewOfFilesInThisZip')."</title>\n</head>";
  1242. $return.="\n\n<body>\n<table border=\"1px\">";
  1243. $counter=0;
  1244. foreach ($files as $key=>$value)
  1245. {
  1246. // We add the header
  1247. if ($counter==0)
  1248. {
  1249. $columns_array=array_keys($value);
  1250. $return.="\n<tr>";
  1251. foreach ($columns_array AS $columns_array_key=>$columns_array_value)
  1252. {
  1253. if (!in_array($columns_array_value,$dont_show_columns))
  1254. {
  1255. $return.="\n\t<th>".$columns_array_value."</th>";
  1256. }
  1257. $column[]=$columns_array_value;
  1258. }
  1259. $return.="</tr><n";
  1260. }
  1261. $counter++;
  1262. // We add the content
  1263. $return.="\n<tr>";
  1264. foreach ($column AS $column_key=>$column_value)
  1265. {
  1266. if (!in_array($column_value,$dont_show_columns))
  1267. {
  1268. $return.="\n\t<td>";
  1269. if (in_array($column_value, $make_link))
  1270. {
  1271. $return.='<a href="'.$value[$column_value].'">'.$value[$column_value].'</a>';
  1272. }
  1273. else
  1274. {
  1275. $return.=$value[$column_value];
  1276. }
  1277. $return.="</td>";
  1278. }
  1279. }
  1280. $return.="</tr><n";
  1281. }
  1282. $return.="\n</table>\n\n</body>";
  1283. $return.="\n</html>";
  1284. return $return;
  1285. }
  1286. /**
  1287. * @desc This function retrieves the number of feedback messages on every document. This function might become obsolete when
  1288. * the feedback becomes user individual.
  1289. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1290. * @version march 2006
  1291. */
  1292. function get_total_number_feedback($file_id='')
  1293. {
  1294. global $dropbox_cnf;
  1295. $sql="SELECT COUNT(feedback_id) AS total, file_id FROM ".$dropbox_cnf['tbl_feedback']." GROUP BY file_id";
  1296. $result=Database::query($sql, __FILE__, __LINE__);
  1297. while ($row=Database::fetch_array($result))
  1298. {
  1299. $return[$row['file_id']]=$row['total'];
  1300. }
  1301. return $return;
  1302. }
  1303. /**
  1304. * @desc this function checks if the key exists. If this is the case it returns the value, if not it returns 0
  1305. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1306. * @version march 2006
  1307. */
  1308. function check_number_feedback($key, $array)
  1309. {
  1310. if (is_array($array))
  1311. {
  1312. if (key_exists($key,$array))
  1313. {
  1314. return $array[$key];
  1315. }
  1316. else
  1317. {
  1318. return 0;
  1319. }
  1320. }
  1321. else
  1322. {
  1323. return 0;
  1324. }
  1325. }
  1326. /**
  1327. * Get the last access to a given tool of a given user
  1328. * @param $tool string the tool constant
  1329. * @param $course_code the course_id
  1330. * @param $user_id the id of the user
  1331. * @return string last tool access date
  1332. *
  1333. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1334. * @version march 2006
  1335. *
  1336. * @todo consider moving this function to a more appropriate place.
  1337. */
  1338. function get_last_tool_access($tool, $course_code='', $user_id='')
  1339. {
  1340. global $_course, $_user;
  1341. // The default values of the parameters
  1342. if ($course_code=='')
  1343. {
  1344. $course_code=$_course['id'];
  1345. }
  1346. if ($user_id=='')
  1347. {
  1348. $user_id=$_user['user_id'];
  1349. }
  1350. // the table where the last tool access is stored (=track_e_lastaccess)
  1351. $table_last_access=Database::get_statistic_table('track_e_lastaccess');
  1352. $sql="SELECT access_date FROM $table_last_access WHERE access_user_id='".Database::escape_string($user_id)."'
  1353. AND access_cours_code='".Database::escape_string($course_code)."'
  1354. AND access_tool='".Database::escape_string($tool)."'
  1355. ORDER BY access_date DESC
  1356. LIMIT 1";
  1357. $result=Database::query($sql,__FILE__,__LINE__);
  1358. $row=Database::fetch_array($result);
  1359. return $row['access_date'];
  1360. }