dropbox_functions.inc.php 48 KB

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