dropbox_functions.inc.php 50 KB

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