dropbox_functions.inc.php 47 KB

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