dropbox_functions.inc.php 45 KB

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