dropbox_functions.inc.php 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. <?php
  2. /* For licensing terms, see /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. $this_section = SECTION_COURSES;
  10. $htmlHeadXtra[] = '<script type="text/javascript">
  11. function setFocus(){
  12. $("#category_title").focus();
  13. }
  14. $(document).ready(function () {
  15. setFocus();
  16. });
  17. </script>';
  18. /**
  19. * This function is a wrapper function for the multiple actions feature.
  20. * @return Mixed If there is a problem, return a string message, otherwise nothing
  21. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  22. * @version march 2006
  23. */
  24. function handle_multiple_actions() {
  25. global $_user, $is_courseAdmin, $is_courseTutor;
  26. // STEP 1: are we performing the actions on the received or on the sent files?
  27. if ($_POST['action'] == 'delete_received' || $_POST['action'] == 'download_received') {
  28. $part = 'received';
  29. } elseif ($_POST['action'] == 'delete_sent' || $_POST['action'] == 'download_sent') {
  30. $part = 'sent';
  31. }
  32. // STEP 2: at least one file has to be selected. If not we return an error message
  33. foreach ($_POST as $key => $value) {
  34. if (strstr($value, $part.'_') AND $key != 'view_received_category' AND $key != 'view_sent_category') {
  35. $checked_files = true;
  36. $checked_file_ids[] = intval(substr($value, strrpos($value, '_')));
  37. }
  38. }
  39. $checked_file_ids = $_POST['id'];
  40. if (!is_array($checked_file_ids) || count($checked_file_ids) == 0) {
  41. return get_lang('CheckAtLeastOneFile');
  42. }
  43. // STEP 3A: deleting
  44. if ($_POST['action'] == 'delete_received' || $_POST['action'] == 'delete_sent') {
  45. $dropboxfile = new Dropbox_Person($_user['user_id'], $is_courseAdmin, $is_courseTutor);
  46. foreach ($checked_file_ids as $key => $value) {
  47. if ($_GET['view'] == 'received') {
  48. $dropboxfile->deleteReceivedWork($value);
  49. $message = get_lang('ReceivedFileDeleted');
  50. }
  51. if ($_GET['view'] == 'sent' OR empty($_GET['view'])) {
  52. $dropboxfile->deleteSentWork($value);
  53. $message = get_lang('SentFileDeleted');
  54. }
  55. }
  56. return $message;
  57. }
  58. // STEP 3B: giving comment
  59. if ($_POST['actions'] == 'comment') {
  60. // This has not been implemented.
  61. // The idea was that it would be possible to write the same feedback for the selected documents.
  62. }
  63. // STEP 3C: moving
  64. if (strstr($_POST['action'], 'move_')) {
  65. // check move_received_n or move_sent_n command
  66. if (strstr($_POST['action'], 'received')) {
  67. $part = 'received';
  68. $to_cat_id = str_replace('move_received_', '', $_POST['action']);
  69. } else {
  70. $part = 'sent';
  71. $to_cat_id = str_replace('move_sent_', '', $_POST['action']);
  72. }
  73. foreach ($checked_file_ids as $key => $value) {
  74. store_move($value, $to_cat_id, $part);
  75. }
  76. return get_lang('FilesMoved');
  77. }
  78. // STEP 3D: downloading
  79. if ($_POST['action'] == 'download_sent' || $_POST['action'] == 'download_received') {
  80. zip_download($checked_file_ids);
  81. }
  82. }
  83. /**
  84. * This function deletes a dropbox category
  85. *
  86. * @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
  87. *
  88. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  89. * @version march 2006
  90. */
  91. function delete_category($action, $id) {
  92. $course_id = api_get_course_int_id();
  93. global $dropbox_cnf;
  94. global $_user, $is_courseAdmin, $is_courseTutor;
  95. // an additional check that might not be necessary
  96. if ($action == 'deletereceivedcategory') {
  97. $sentreceived = 'received';
  98. $entries_table = $dropbox_cnf['tbl_post'];
  99. $id_field = 'file_id';
  100. $return_message = get_lang('ReceivedCatgoryDeleted');
  101. } elseif ($action == 'deletesentcategory') {
  102. $sentreceived = 'sent';
  103. $entries_table = $dropbox_cnf['tbl_file'];
  104. $id_field = 'id';
  105. $return_message = get_lang('SentCatgoryDeleted');
  106. } else {
  107. return get_lang('Error');
  108. }
  109. // step 1: delete the category
  110. $sql = "DELETE FROM ".$dropbox_cnf['tbl_category']." WHERE c_id = $course_id AND cat_id='".Database::escape_string($id)."' AND $sentreceived='1'";
  111. $result = Database::query($sql);
  112. // step 2: delete all the documents in this category
  113. $sql = "SELECT * FROM ".$entries_table." WHERE c_id = $course_id AND cat_id='".Database::escape_string($id)."'";
  114. $result = Database::query($sql);
  115. while($row = Database::fetch_array($result)) {
  116. $dropboxfile = new Dropbox_Person($_user['user_id'], $is_courseAdmin, $is_courseTutor);
  117. if ($action == 'deletereceivedcategory') {
  118. $dropboxfile->deleteReceivedWork($row[$id_field]);
  119. }
  120. if ($action == 'deletesentcategory') {
  121. $dropboxfile->deleteSentWork($row[$id_field]);
  122. }
  123. }
  124. return $return_message;
  125. }
  126. /**
  127. * Displays the form to move one individual file to a category
  128. *
  129. * @return html code of the form that appears in a message box.
  130. *
  131. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  132. * @version march 2006
  133. */
  134. function display_move_form($part, $id, $target = array(), $extra_params) {
  135. echo '<form name="form1" method="post" action="'.api_get_self().'?view_received_category='.Security::remove_XSS($_GET['view_received_category']).'&view_sent_category='.Security::remove_XSS($_GET['view_sent_category']).'&view='.Security::remove_XSS($_GET['view']).'&'.$extra_params.'">';
  136. echo '<legend>'.get_lang('MoveFileTo').'</legend>';
  137. echo '<input type="hidden" name="id" value="'.Security::remove_XSS($id).'">';
  138. echo '<input type="hidden" name="part" value="'.Security::remove_XSS($part).'">';
  139. echo '
  140. <div class="row">
  141. <div class="label">
  142. <span class="form_required">*</span> '.get_lang('MoveFileTo').'
  143. </div>
  144. <div class="formw">';
  145. echo '<select name="move_target">';
  146. echo '<option value="0">'.get_lang('Root').'</option>';
  147. foreach ($target as $key => $category) {
  148. echo '<option value="'.$category['cat_id'].'">'.$category['cat_name'].'</option>';
  149. }
  150. echo '</select>';
  151. echo ' </div>
  152. </div>';
  153. echo '<div class="row">
  154. <div class="label">
  155. </div>
  156. <div class="formw">
  157. <button class="next" type="submit" name="do_move" value="'.get_lang('Ok').'">'.get_lang('MoveFile').'</button>
  158. </div>
  159. </div>';
  160. echo '</form>';
  161. echo '<div style="clear: both;"></div>';
  162. }
  163. /**
  164. * This function moves a file to a different category
  165. *
  166. * @param $id the id of the file we are moving
  167. * @param $target the id of the folder we are moving to
  168. * @param $part are we moving a received file or a sent file?
  169. *
  170. * @return language string
  171. *
  172. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  173. * @version march 2006
  174. */
  175. function store_move($id, $target, $part) {
  176. global $_user;
  177. global $dropbox_cnf;
  178. $course_id = api_get_course_int_id();
  179. if ((isset($id) AND $id != '') AND (isset($target) AND $target != '') AND (isset($part) AND $part != '')) {
  180. if ($part == 'received') {
  181. $sql = "UPDATE ".$dropbox_cnf["tbl_post"]." SET cat_id='".Database::escape_string($target)."'
  182. WHERE c_id = $course_id AND dest_user_id='".Database::escape_string($_user['user_id'])."'
  183. AND file_id='".Database::escape_string($id)."'";
  184. Database::query($sql);
  185. $return_message = get_lang('ReceivedFileMoved');
  186. }
  187. if ($part == 'sent') {
  188. $sql = "UPDATE ".$dropbox_cnf["tbl_file"]." SET cat_id='".Database::escape_string($target)."'
  189. WHERE c_id = $course_id AND uploader_id='".Database::escape_string($_user['user_id'])."'
  190. AND id='".Database::escape_string($id)."'";
  191. Database::query($sql);
  192. $return_message = get_lang('SentFileMoved');
  193. }
  194. } else {
  195. $return_message = get_lang('NotMovedError');
  196. }
  197. return $return_message;
  198. }
  199. /**
  200. * This functions displays all teh possible actions that can be performed on multiple files. This is the dropdown list that
  201. * appears below the sortable table of the sent / or received files.
  202. *
  203. * @return html value for the dropdown list
  204. *
  205. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  206. * @version march 2006
  207. */
  208. function display_action_options($part, $categories, $current_category = 0) {
  209. echo '<select name="actions">';
  210. echo '<option value="download">'.get_lang('Download').'</option>';
  211. echo '<option value="delete">'.get_lang('Delete').'</option>';
  212. if (is_array($categories)) {
  213. echo '<optgroup label="'.get_lang('MoveTo').'">';
  214. if ($current_category != 0) {
  215. echo '<option value="move_0">'.get_lang('Root').'</a>';
  216. }
  217. foreach ($categories as $key => $value) {
  218. if ($current_category != $value['cat_id']) {
  219. echo '<option value="move_'.$value['cat_id'].'">'.$value['cat_name'].'</option>';
  220. }
  221. }
  222. echo '</optgroup>';
  223. }
  224. echo '</select>';
  225. echo '<input type="submit" name="do_actions_'.Security::remove_XSS($part).'" value="'.get_lang('Ok').'" />';
  226. }
  227. /**
  228. * this function returns the html code that displays the checkboxes next to the files so that
  229. * multiple actions on one file are possible.
  230. *
  231. * @param $id the unique id of the file
  232. * @param $part are we dealing with a sent or with a received file?
  233. *
  234. * @return html code
  235. *
  236. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  237. * @version march 2006
  238. */
  239. function display_file_checkbox($id, $part) {
  240. if (isset($_GET['selectall'])) {
  241. $checked = 'checked';
  242. }
  243. $return_value = '<input type="checkbox" name="'.Security::remove_XSS($part).'_'.Security::remove_XSS($id).'" value="'.Security::remove_XSS($id).'" '.$checked.' />';
  244. return $return_value;
  245. }
  246. /**
  247. * This function retrieves all the dropbox categories and returns them as an array
  248. *
  249. * @param $filter default '', when we need only the categories of the sent or the received part.
  250. *
  251. * @return array
  252. *
  253. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  254. * @version march 2006
  255. */
  256. function get_dropbox_categories($filter = '') {
  257. $course_id = api_get_course_int_id();
  258. global $_user;
  259. global $dropbox_cnf;
  260. $return_array = array();
  261. $session_id = api_get_session_id();
  262. $condition_session = api_get_session_condition($session_id);
  263. $sql = "SELECT * FROM ".$dropbox_cnf['tbl_category']." WHERE c_id = $course_id AND user_id='".$_user['user_id']."' $condition_session";
  264. $result = Database::query($sql);
  265. while ($row = Database::fetch_array($result)) {
  266. if (($filter == 'sent' AND $row['sent'] == 1) OR ($filter == 'received' AND $row['received'] == 1) OR $filter == '') {
  267. $return_array[$row['cat_id']] = $row;
  268. }
  269. }
  270. return $return_array;
  271. }
  272. /**
  273. * This functions stores a new dropboxcategory
  274. *
  275. * @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
  276. * dropbox_category table but it is the easiest solution. You get
  277. * cat_name | received | sent | user_id
  278. * test | 1 | 0 | 237
  279. * test | 0 | 1 | 237
  280. * more elegant would be
  281. * test | 1 | 1 | 237
  282. *
  283. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  284. * @version march 2006
  285. */
  286. function store_addcategory() {
  287. $course_id = api_get_course_int_id();
  288. global $_user;
  289. global $dropbox_cnf;
  290. // check if the target is valid
  291. if ($_POST['target'] == 'sent') {
  292. $sent = 1;
  293. $received = 0;
  294. } elseif ($_POST['target'] == 'received') {
  295. $sent = 0;
  296. $received = 1;
  297. } else {
  298. return get_lang('Error');
  299. }
  300. // check if the category name is valid
  301. if ($_POST['category_name'] == '') {
  302. return array('type' => 'error', 'message' => get_lang('ErrorPleaseGiveCategoryName'));
  303. }
  304. if (!$_POST['edit_id']) {
  305. $session_id = api_get_session_id();
  306. // step 3a, we check if the category doesn't already exist
  307. $sql = "SELECT * FROM ".$dropbox_cnf['tbl_category']." WHERE c_id = $course_id AND user_id='".$_user['user_id']."' AND cat_name='".Database::escape_string($_POST['category_name'])."' AND received='".$received."' AND sent='$sent' AND session_id='$session_id'";
  308. $result = Database::query($sql);
  309. // step 3b, we add the category if it does not exist yet.
  310. if (Database::num_rows($result) == 0) {
  311. $sql = "INSERT INTO ".$dropbox_cnf['tbl_category']." (c_id, cat_name, received, sent, user_id, session_id)
  312. VALUES ($course_id, '".Database::escape_string($_POST['category_name'])."', '".Database::escape_string($received)."', '".Database::escape_string($sent)."', '".Database::escape_string($_user['user_id'])."',$session_id)";
  313. Database::query($sql);
  314. return array('type' => 'confirmation', 'message' => get_lang('CategoryStored'));
  315. } else {
  316. return array('type' => 'error', 'message' => get_lang('CategoryAlreadyExistsEditIt'));
  317. }
  318. } else {
  319. $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)."'
  320. WHERE c_id = $course_id AND user_id='".Database::escape_string($_user['user_id'])."'
  321. AND cat_id='".Database::escape_string($_POST['edit_id'])."'";
  322. Database::query($sql);
  323. return array('type' => 'confirmation', 'message' => get_lang('CategoryModified'));
  324. }
  325. }
  326. /**
  327. * This function displays the form to add a new category.
  328. *
  329. * @param $category_name this parameter is the name of the category (used when no section is selected)
  330. * @param $id this is the id of the category we are editing.
  331. *
  332. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  333. * @version march 2006
  334. */
  335. function display_addcategory_form($category_name = '', $id = '', $action) {
  336. $course_id = api_get_course_int_id();
  337. global $dropbox_cnf;
  338. $course_id = api_get_course_int_id();
  339. $title = get_lang('AddNewCategory');
  340. if (isset($id) AND $id != '') {
  341. // retrieve the category we are editing
  342. $sql = "SELECT * FROM ".$dropbox_cnf['tbl_category']." WHERE c_id = $course_id AND cat_id='".Database::escape_string($id)."'";
  343. $result = Database::query($sql);
  344. $row = Database::fetch_array($result);
  345. if (empty($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)
  346. $category_name = $row['cat_name'];
  347. }
  348. if ($row['received'] == '1') {
  349. $target = 'received';
  350. }
  351. if ($row['sent'] == '1') {
  352. $target = 'sent';
  353. }
  354. $title = get_lang('EditCategory');
  355. }
  356. if ($action == 'addreceivedcategory') {
  357. $target = 'received';
  358. }
  359. if ($action == 'addsentcategory') {
  360. $target = 'sent';
  361. }
  362. if ($action == 'editcategory') {
  363. $text = get_lang('ModifyCategory');
  364. $class = 'save';
  365. } elseif ($action == 'addreceivedcategory' or $action == 'addsentcategory') {
  366. $text = get_lang('CreateCategory');
  367. $class = 'add';
  368. }
  369. echo '<form name="add_new_category" method="post" action="'.api_get_self().'?view="'.Security::remove_XSS($_GET['view']).'">'."\n";
  370. echo '<legend>'.$title.'</legend>';
  371. if (isset($id) AND $id != '') {
  372. echo '<input name="edit_id" type="hidden" value="'.Security::remove_XSS($id).'">';
  373. }
  374. echo '<input name="action" type="hidden" value="'.Security::remove_XSS($action).'">';
  375. echo '<input name="target" type="hidden" value="'.$target.'">';
  376. echo ' <div class="row">
  377. <div class="label">
  378. <span class="form_required">*</span> '.get_lang('CategoryName').'
  379. </div>
  380. <div class="formw">';
  381. if ($_POST AND empty($_POST['category_name'])) {
  382. echo '<span class="form_error">'.get_lang('ThisFieldIsRequired').'. '.get_lang('ErrorPleaseGiveCategoryName').'<span><br />';
  383. }
  384. if ($_POST AND !empty($_POST['category_name'])) {
  385. echo '<span class="form_error">'.get_lang('CategoryAlreadyExistsEditIt').'<span><br />';
  386. }
  387. echo ' <input type="text" id="category_title" name="category_name" value="'.Security::remove_XSS($category_name).'" />
  388. </div>
  389. </div>';
  390. echo ' <div class="row">
  391. <div class="label">
  392. </div>
  393. <div class="formw">
  394. <button class="'.$class.'" type="submit" name="StoreCategory">'.$text.'</button>
  395. </div>
  396. </div>';
  397. echo ' <div class="row">
  398. <div class="label">
  399. </div>
  400. <div class="formw">
  401. <span class="form_required">*</span> <small>'.get_lang('ThisFieldIsRequired').'</small>
  402. </div>
  403. </div>';
  404. echo '</form>';
  405. echo '<div style="clear: both;"></div>';
  406. }
  407. /**
  408. * this function displays the form to upload a new item to the dropbox.
  409. *
  410. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  411. * @version march 2006
  412. */
  413. function display_add_form() {
  414. global $_user, $is_courseAdmin, $is_courseTutor, $course_info, $origin, $dropbox_unid;
  415. $token = Security::get_token();
  416. $dropbox_person = new Dropbox_Person(api_get_user_id(), $is_courseAdmin, $is_courseTutor);
  417. ?>
  418. <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="javascript: return checkForm(this);">
  419. <legend><?php echo get_lang('UploadNewFile'); ?></legend>
  420. <div class="row">
  421. <div class="label">
  422. <span class="form_required">*</span><?php echo get_lang('UploadFile'); ?>:
  423. </div>
  424. <div class="formw">
  425. <input type="hidden" name="MAX_FILE_SIZE" value='<?php echo dropbox_cnf('maxFilesize'); ?>' />
  426. <input type="file" name="file" size="20" <?php if (dropbox_cnf('allowOverwrite')) echo 'onChange="javascript: checkfile(this.value);"'; ?> />
  427. <input type="hidden" name="dropbox_unid" value="<?php echo $dropbox_unid; ?>" />
  428. <input type="hidden" name="sec_token" value="<?php echo $token; ?>" />
  429. <?php
  430. if ($origin == 'learnpath') {
  431. echo '<input type="hidden" name="origin" value="learnpath" />';
  432. }
  433. ?>
  434. </div>
  435. </div>
  436. <?php
  437. if (dropbox_cnf('allowOverwrite')) {
  438. ?>
  439. <div class="row">
  440. <div class="label">
  441. </div>
  442. <div class="formw">
  443. <input type="checkbox" name="cb_overwrite" id="cb_overwrite" value="true" />
  444. <label for="cb_overwrite">
  445. <?php echo get_lang('OverwriteFile'); ?>
  446. </label>
  447. </div>
  448. </div>
  449. <?php
  450. }
  451. ?>
  452. <div class="row">
  453. <div class="label">
  454. <?php echo get_lang('SendTo'); ?>
  455. </div>
  456. <div class="formw">
  457. <?php
  458. //list of all users in this course and all virtual courses combined with it
  459. if (api_get_session_id()) {
  460. $complete_user_list_for_dropbox = array();
  461. if (api_get_setting('dropbox_allow_student_to_student')=='true' || $_user['status'] != STUDENT) {
  462. $complete_user_list_for_dropbox = CourseManager :: get_user_list_from_course_code($course_info['code'], api_get_session_id());
  463. }
  464. $complete_user_list2 = CourseManager::get_coach_list_from_course_code($course_info['code'], api_get_session_id());
  465. $complete_user_list_for_dropbox = array_merge($complete_user_list_for_dropbox, $complete_user_list2);
  466. } else {
  467. if (api_get_setting('dropbox_allow_student_to_student') == 'true' || $_user['status'] != STUDENT) {
  468. $complete_user_list_for_dropbox = CourseManager :: get_user_list_from_course_code($course_info['code'], api_get_session_id());
  469. } else {
  470. $complete_user_list_for_dropbox = CourseManager :: get_teacher_list_from_course_code($course_info['code'], false);
  471. }
  472. }
  473. if (!empty($complete_user_list_for_dropbox)) {
  474. foreach ($complete_user_list_for_dropbox as $k => $e) {
  475. $complete_user_list_for_dropbox[$k] = $e + array('lastcommafirst' => api_get_person_name($e['firstname'], $e['lastname']));
  476. }
  477. $complete_user_list_for_dropbox = TableSort::sort_table($complete_user_list_for_dropbox, 'lastcommafirst');
  478. }
  479. ?>
  480. <select name="recipients[]" size="
  481. <?php
  482. if ($dropbox_person -> isCourseTutor || $dropbox_person -> isCourseAdmin) {
  483. echo 10;
  484. } else {
  485. echo 6;
  486. }
  487. ?>" multiple style="width: 350px;">
  488. <?php
  489. /*
  490. Create the options inside the select box:
  491. List all selected users their user id as value and a name string as display
  492. */
  493. $current_user_id = '';
  494. foreach ($complete_user_list_for_dropbox as $current_user) {
  495. if (($dropbox_person -> isCourseTutor
  496. || $dropbox_person -> isCourseAdmin
  497. || dropbox_cnf('allowStudentToStudent')
  498. || $current_user['status'] != 5 // Always allow teachers.
  499. || $current_user['tutor_id'] == 1 // Always allow tutors.
  500. ) && $current_user['user_id'] != $_user['user_id']) { // Don't include yourself.
  501. if ($current_user['user_id'] == $current_user_id) {
  502. continue;
  503. }
  504. $full_name = $current_user['lastcommafirst'];
  505. $current_user_id = $current_user['user_id'];
  506. echo '<option value="user_' . $current_user_id . '">' . $full_name . '</option>';
  507. }
  508. }
  509. /*
  510. * Show groups
  511. */
  512. if (($dropbox_person -> isCourseTutor || $dropbox_person -> isCourseAdmin)
  513. && dropbox_cnf('allowGroup') || dropbox_cnf('allowStudentToStudent')) {
  514. $complete_group_list_for_dropbox = GroupManager::get_group_list(null, dropbox_cnf('courseId'));
  515. if (count($complete_group_list_for_dropbox) > 0) {
  516. foreach ($complete_group_list_for_dropbox as $current_group) {
  517. if ($current_group['number_of_members'] > 0) {
  518. echo '<option value="group_'.$current_group['id'].'">G: '.$current_group['name'].' - '.$current_group['number_of_members'].' '.get_lang('Users').'</option>';
  519. }
  520. }
  521. }
  522. }
  523. if (($dropbox_person -> isCourseTutor || $dropbox_person -> isCourseAdmin) && dropbox_cnf('allowMailing')) {
  524. // echo '<option value="mailing">'.get_lang('MailingInSelect').'</option>';
  525. }
  526. if (dropbox_cnf('allowJustUpload')) {
  527. //echo '<option value="upload">'.get_lang('JustUploadInSelect').'</option>';
  528. echo '<option value="user_'.$_user['user_id'].'">'.get_lang('JustUploadInSelect').'</option>';
  529. }
  530. echo '
  531. </select>
  532. </div>
  533. </div>';
  534. echo '
  535. <div class="row">
  536. <div class="label">
  537. </div>
  538. <div class="formw">
  539. <button type="Submit" class="upload" name="submitWork">'.get_lang('Upload', '').'</button>
  540. </div>
  541. </div>
  542. ';
  543. echo '</form>';
  544. }
  545. /**
  546. * returns username or false if user isn't registered anymore
  547. * @todo check if this function is still necessary. There might be a library function for this.
  548. */
  549. function getUserNameFromId($id) {
  550. global $dropbox_cnf;
  551. $mailingId = $id - dropbox_cnf('mailingIdBase');
  552. if ($mailingId > 0) {
  553. return get_lang('MailingAsUsername', '') . $mailingId;
  554. }
  555. $id = intval($id);
  556. $sql = "SELECT ".(api_is_western_name_order() ? "CONCAT(firstname,' ', lastname)" : "CONCAT(lastname,' ', firstname)")." AS name
  557. FROM " . $dropbox_cnf['tbl_user'] . "
  558. WHERE user_id='$id'";
  559. $result = Database::query($sql);
  560. $res = Database::fetch_array($result);
  561. if (!$res) return false;
  562. return stripslashes($res['name']);
  563. }
  564. /**
  565. * returns loginname or false if user isn't registered anymore
  566. * @todo check if this function is still necessary. There might be a library function for this.
  567. */
  568. function getLoginFromId($id) {
  569. $id = intval($id);
  570. $sql = "SELECT username
  571. FROM " . dropbox_cnf('tbl_user') . "
  572. WHERE user_id='$id'";
  573. $result = Database::query($sql);
  574. $res = Database::fetch_array($result);
  575. if (!$res) return false;
  576. return stripslashes($res['username']);
  577. }
  578. /**
  579. * @return boolean indicating if user with user_id=$user_id is a course member
  580. * @todo eliminate global
  581. * @todo check if this function is still necessary. There might be a library function for this.
  582. */
  583. function isCourseMember($user_id) {
  584. global $_course;
  585. $course_code = $_course['sysCode'];
  586. $is_course_member = CourseManager::is_user_subscribed_in_course($user_id, $course_code, true);
  587. return $is_course_member;
  588. }
  589. /**
  590. * Checks if there are files in the dropbox_file table that aren't used anymore in dropbox_person table.
  591. * If there are, all entries concerning the file are deleted from the db + the file is deleted from the server
  592. */
  593. function removeUnusedFiles() {
  594. $course_id = api_get_course_int_id();
  595. // select all files that aren't referenced anymore
  596. $sql = "SELECT DISTINCT f.id, f.filename
  597. FROM " . dropbox_cnf('tbl_file') . " f
  598. LEFT JOIN " . dropbox_cnf('tbl_person') . " p ON f.id = p.file_id
  599. WHERE f.c_id = $course_id AND p.c_id = $course_id AND p.user_id IS NULL";
  600. $result = Database::query($sql);
  601. while ($res = Database::fetch_array($result)) {
  602. //delete the selected files from the post and file tables
  603. $sql = "DELETE FROM " . dropbox_cnf('tbl_post') . " WHERE c_id = $course_id AND file_id='" . $res['id'] . "'";
  604. $result1 = Database::query($sql);
  605. $sql = "DELETE FROM " . dropbox_cnf('tbl_file') . " WHERE c_id = $course_id AND id='" . $res['id'] . "'";
  606. $result1 = Database::query($sql);
  607. //delete file from server
  608. @unlink( dropbox_cnf('sysPath') . '/' . $res['filename']);
  609. }
  610. }
  611. /**
  612. *
  613. * Mailing zip-file is posted to (dest_user_id = ) mailing pseudo_id
  614. * and is only visible to its uploader (user_id).
  615. *
  616. * Mailing content files have uploader_id == mailing pseudo_id, a normal recipient,
  617. * and are visible initially to recipient and pseudo_id.
  618. *
  619. * @author René Haentjens, Ghent University
  620. *
  621. * @todo check if this function is still necessary.
  622. */
  623. function getUserOwningThisMailing($mailingPseudoId, $owner = 0, $or_die = '') {
  624. $course_id = api_get_course_int_id();
  625. global $dropbox_cnf;
  626. $mailingPseudoId = intval($mailingPseudoId);
  627. $sql = "SELECT f.uploader_id
  628. FROM " . $dropbox_cnf['tbl_file'] . " f
  629. LEFT JOIN " . $dropbox_cnf['tbl_post'] . " p ON f.id = p.file_id
  630. WHERE f.c_id = $course_id AND p.c_id = $course_id AND
  631. p.dest_user_id = '" . $mailingPseudoId . "'";
  632. $result = Database::query($sql);
  633. if (!($res = Database::fetch_array($result)))
  634. die(get_lang('GeneralError').' (code 901)');
  635. if ($owner == 0) return $res['uploader_id'];
  636. if ($res['uploader_id'] == $owner) return true;
  637. die(get_lang('GeneralError').' (code '.$or_die.')');
  638. }
  639. /**
  640. * @author René Haentjens, Ghent University
  641. * @todo check if this function is still necessary.
  642. */
  643. function removeMoreIfMailing($file_id) {
  644. $course_id = api_get_course_int_id();
  645. global $dropbox_cnf;
  646. // when deleting a mailing zip-file (posted to mailingPseudoId):
  647. // 1. the detail window is no longer reachable, so
  648. // for all content files, delete mailingPseudoId from person-table
  649. // 2. finding the owner (getUserOwningThisMailing) is no longer possible, so
  650. // for all content files, replace mailingPseudoId by owner as uploader
  651. $file_id = intval($file_id);
  652. $sql = "SELECT p.dest_user_id
  653. FROM " . $dropbox_cnf['tbl_post'] . " p
  654. WHERE c_id = $course_id AND p.file_id = '" . $file_id . "'";
  655. $result = Database::query($sql);
  656. if ($res = Database::fetch_array($result)) {
  657. $mailingPseudoId = $res['dest_user_id'];
  658. if ($mailingPseudoId > dropbox_cnf('mailingIdBase')) {
  659. $sql = "DELETE FROM " . dropbox_cnf('tbl_person') . " WHERE c_id = $course_id AND user_id='" . $mailingPseudoId . "'";
  660. $result1 = Database::query($sql);
  661. $sql = "UPDATE " . dropbox_cnf('tbl_file') .
  662. " SET uploader_id='" . api_get_user_id() . "' WHERE c_id = $course_id AND uploader_id='" . $mailingPseudoId . "'";
  663. $result1 = Database::query($sql);
  664. }
  665. }
  666. }
  667. /**
  668. * Function that finds a given config setting
  669. *
  670. * @author René Haentjens, Ghent University
  671. */
  672. function dropbox_cnf($variable) {
  673. return $GLOBALS['dropbox_cnf'][$variable];
  674. }
  675. /**
  676. *
  677. */
  678. function store_add_dropbox() {
  679. global $dropbox_cnf;
  680. global $_user;
  681. global $_course;
  682. // Validating the form data
  683. // the author is
  684. /*
  685. if (!isset($_POST['authors'])) {
  686. return get_lang('AuthorFieldCannotBeEmpty');
  687. }
  688. */
  689. // there are no recipients selected
  690. if (!isset($_POST['recipients']) || count( $_POST['recipients']) <= 0) {
  691. return get_lang('YouMustSelectAtLeastOneDestinee');
  692. }
  693. // Check if all the recipients are valid
  694. else {
  695. $thisIsAMailing = false;
  696. $thisIsJustUpload = false;
  697. foreach ($_POST['recipients'] as $rec) {
  698. if ($rec == 'mailing') {
  699. $thisIsAMailing = true;
  700. } elseif ($rec == 'upload') {
  701. $thisIsJustUpload = true;
  702. } elseif (strpos($rec, 'user_') === 0 && !isCourseMember(substr($rec, strlen('user_')))) {
  703. return get_lang('InvalideUserDetected');
  704. } elseif (strpos($rec, 'group_') !== 0 && strpos($rec, 'user_') !== 0) {
  705. return get_lang('InvalideGroupDetected');
  706. }
  707. }
  708. }
  709. // we are doing a mailing but an additional recipient is selected
  710. if ($thisIsAMailing && (count($_POST['recipients']) != 1)) {
  711. return get_lang('MailingSelectNoOther');
  712. }
  713. // we are doing a just upload but an additional recipient is selected.
  714. // 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)
  715. if ($thisIsJustUpload && (count($_POST['recipients']) != 1)) {
  716. return get_lang('MailingJustUploadSelectNoOther');
  717. }
  718. if (empty($_FILES['file']['name'])) {
  719. $error = true;
  720. return get_lang('NoFileSpecified');
  721. }
  722. // are we overwriting a previous file or sending a new one
  723. $dropbox_overwrite = false;
  724. if (isset($_POST['cb_overwrite']) && $_POST['cb_overwrite']) {
  725. $dropbox_overwrite = true;
  726. }
  727. // doing the upload
  728. $dropbox_filename = $_FILES['file']['name'];
  729. $dropbox_filesize = $_FILES['file']['size'];
  730. $dropbox_filetype = $_FILES['file']['type'];
  731. $dropbox_filetmpname = $_FILES['file']['tmp_name'];
  732. // check if the filesize does not exceed the allowed size.
  733. if ($dropbox_filesize <= 0 || $dropbox_filesize > $dropbox_cnf['maxFilesize']) {
  734. return get_lang('DropboxFileTooBig'); // TODO: The "too big" message does not fit in the case of uploading zero-sized file.
  735. }
  736. // check if the file is actually uploaded
  737. if (!is_uploaded_file($dropbox_filetmpname)) { // check user fraud : no clean error msg.
  738. return get_lang('TheFileIsNotUploaded');
  739. }
  740. // Try to add an extension to the file if it hasn't got one
  741. $dropbox_filename = add_ext_on_mime($dropbox_filename, $dropbox_filetype);
  742. // Replace dangerous characters
  743. $dropbox_filename = replace_dangerous_char($dropbox_filename);
  744. // Transform any .php file in .phps fo security
  745. $dropbox_filename = php2phps($dropbox_filename);
  746. //filter extension
  747. if (!filter_extension($dropbox_filename)) {
  748. return get_lang('UplUnableToSaveFileFilteredExtension');
  749. }
  750. // set title
  751. $dropbox_title = $dropbox_filename;
  752. // set author
  753. if ($_POST['authors'] == '') {
  754. $_POST['authors'] = getUserNameFromId($_user['user_id']);
  755. }
  756. // note: I think we could better migrate everything from here on to separate functions: store_new_dropbox, store_new_mailing, store_just_upload
  757. if ($dropbox_overwrite) {
  758. $dropbox_person = new Dropbox_Person($_user['user_id'], api_is_course_admin(), api_is_course_tutor());
  759. foreach ($dropbox_person->sentWork as $w) {
  760. if ($w->title == $dropbox_filename) {
  761. if (($w->recipients[0]['id'] > dropbox_cnf('mailingIdBase')) xor $thisIsAMailing) {
  762. return get_lang('MailingNonMailingError');
  763. }
  764. if (($w->recipients[0]['id'] == $_user['user_id']) xor $thisIsJustUpload) {
  765. return get_lang('MailingJustUploadSelectNoOther');
  766. }
  767. $dropbox_filename = $w->filename;
  768. $found = true; // note: do we still need this?
  769. break;
  770. }
  771. }
  772. } else { // rename file to login_filename_uniqueId format
  773. $dropbox_filename = getLoginFromId($_user['user_id']) . "_" . $dropbox_filename . "_".uniqid('');
  774. }
  775. // creating the array that contains all the users who will receive the file
  776. $new_work_recipients = array();
  777. foreach ($_POST['recipients'] as $rec) {
  778. if (strpos($rec, 'user_') === 0) {
  779. $new_work_recipients[] = substr($rec, strlen('user_') );
  780. } elseif (strpos($rec, 'group_') === 0) {
  781. $userList = GroupManager::get_subscribed_users(substr($rec, strlen('group_')));
  782. foreach ($userList as $usr) {
  783. if (!in_array($usr['user_id'], $new_work_recipients) && $usr['user_id'] != $_user['user_id']) {
  784. $new_work_recipients[] = $usr['user_id'];
  785. }
  786. }
  787. }
  788. }
  789. @move_uploaded_file($dropbox_filetmpname, dropbox_cnf('sysPath') . '/' . $dropbox_filename);
  790. $b_send_mail = api_get_course_setting('email_alert_on_new_doc_dropbox');
  791. if ($b_send_mail) {
  792. foreach ($new_work_recipients as $recipient_id) {
  793. $recipent_temp = UserManager :: get_user_info_by_id($recipient_id);
  794. @api_mail(api_get_person_name($recipent_temp['firstname'].' '.$recipent_temp['lastname'], null, PERSON_NAME_EMAIL_ADDRESS), $recipent_temp['email'],
  795. get_lang('NewDropboxFileUploaded'),
  796. 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']);
  797. }
  798. }
  799. new Dropbox_SentWork( $_user['user_id'], $dropbox_title, $_POST['description'], strip_tags($_POST['authors']), $dropbox_filename, $dropbox_filesize, $new_work_recipients);
  800. Security::clear_token();
  801. return get_lang('FileUploadSucces');
  802. }
  803. /**
  804. * This function displays the firstname and lastname of the user as a link to the user tool.
  805. *
  806. * @see this is the same function as in the new forum, so this probably has to move to a user library.
  807. *
  808. * @todo move this function to the user library (there is a duplicate in work.lib.php)
  809. *
  810. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  811. * @version march 2006
  812. */
  813. function display_user_link_work($user_id, $name = '') {
  814. if ($user_id != 0) {
  815. if (empty($name)) {
  816. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  817. $sql = "SELECT * FROM $table_user WHERE user_id='".Database::escape_string($user_id)."'";
  818. $result = Database::query($sql);
  819. $row = Database::fetch_array($result);
  820. return '<a href="../user/userInfo.php?uInfo='.$row['user_id'].'">'.api_get_person_name($row['firstname'], $row['lastname']).'</a>';
  821. } else {
  822. $user_id = intval($user_id);
  823. return '<a href="../user/userInfo.php?uInfo='.$user_id.'">'.Security::remove_XSS($name).'</a>';
  824. }
  825. } else {
  826. return $name.' ('.get_lang('Anonymous').')';
  827. }
  828. }
  829. /**
  830. * this function transforms the array containing all the feedback into something visually attractive.
  831. *
  832. * @param an array containing all the feedback about the given message.
  833. *
  834. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  835. * @version march 2006
  836. */
  837. function feedback($array) {
  838. foreach ($array as $key => $value) {
  839. $output .= format_feedback($value);
  840. }
  841. $output .= feedback_form();
  842. return $output;
  843. }
  844. /**
  845. * This function returns the html code to display the feedback messages on a given dropbox file
  846. * @param $feedback_array an array that contains all the feedback messages about the given document.
  847. * @return html code
  848. * @todo add the form for adding new comment (if the other party has not deleted it yet).
  849. *
  850. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  851. * @version march 2006
  852. */
  853. function format_feedback($feedback) {
  854. $output .= display_user_link_work($feedback['author_user_id']);
  855. $output .= '&nbsp;&nbsp;'.api_convert_and_format_date($feedback['feedback_date'], DATE_TIME_FORMAT_LONG).'<br />';
  856. $output .= '<div style="padding-top:6px">'.nl2br($feedback['feedback']).'</div><hr size="1" noshade/><br />';
  857. return $output;
  858. }
  859. /**
  860. * this function returns the code for the form for adding a new feedback message to a dropbox file.
  861. * @return html code
  862. *
  863. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  864. * @version march 2006
  865. */
  866. function feedback_form() {
  867. $course_id = api_get_course_int_id();
  868. global $dropbox_cnf;
  869. $return = get_lang('AddNewFeedback').'<br />';
  870. // 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
  871. // add feedback since the other users will never get to see the feedback.
  872. $sql = "SELECT * FROM ".$dropbox_cnf['tbl_person']." WHERE c_id = $course_id AND file_id = ".intval($_GET['id']);
  873. $result = Database::query($sql);
  874. $number_users_who_see_file = Database::num_rows($result);
  875. if ($number_users_who_see_file > 1) {
  876. $token = Security::get_token();
  877. $return .= '<textarea name="feedback" style="width: 80%; height: 80px;"></textarea>';
  878. $return .= '<input type="hidden" name="sec_token" value="'.$token.'"/>';
  879. $return .= '<br /><button type="submit" class="add" name="store_feedback" value="'.get_lang('Ok').'"
  880. onclick="javascript: document.form_dropbox.attributes.action.value = document.location;">'.get_lang('AddComment').'</button>';
  881. } else {
  882. $return .= get_lang('AllUsersHaveDeletedTheFileAndWillNotSeeFeedback');
  883. }
  884. return $return;
  885. }
  886. /**
  887. * @return a language string (depending on the success or failure.
  888. *
  889. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  890. * @version march 2006
  891. */
  892. function store_feedback() {
  893. global $dropbox_cnf;
  894. if (!is_numeric($_GET['id'])) {
  895. return get_lang('FeedbackError');
  896. }
  897. $course_id = api_get_course_int_id();
  898. if (empty($_POST['feedback'])) {
  899. return get_lang('PleaseTypeText');
  900. } else {
  901. $sql="INSERT INTO ".$dropbox_cnf['tbl_feedback']." (c_id, file_id, author_user_id, feedback, feedback_date) VALUES
  902. ($course_id, '".intval($_GET['id'])."','".api_get_user_id()."','".Database::escape_string($_POST['feedback'])."', '".api_get_utc_datetime()."')";
  903. Database::query($sql);
  904. return get_lang('DropboxFeedbackStored');
  905. }
  906. }
  907. /**
  908. * This function downloads all the files of the inputarray into one zip
  909. * @param $array an array containing all the ids of the files that have to be downloaded.
  910. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  911. * @todo consider removing the check if the user has received or sent this file (zip download of a folder already sufficiently checks for this).
  912. * @todo integrate some cleanup function that removes zip files that are older than 2 days
  913. *
  914. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  915. * @version march 2006
  916. */
  917. function zip_download($array) {
  918. global $_course;
  919. global $dropbox_cnf;
  920. global $files;
  921. $course_id = api_get_course_int_id();
  922. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  923. // zip library for creation of the zipfile
  924. require api_get_path(LIBRARY_PATH).'pclzip/pclzip.lib.php';
  925. // place to temporarily stash the zipfiles
  926. $temp_zip_dir = api_get_path(SYS_COURSE_PATH);
  927. array_map('intval', $array);
  928. // note: we also have to add the check if the user has received or sent this file.
  929. $sql = "SELECT distinct file.filename, file.title, file.author, file.description
  930. FROM ".$dropbox_cnf['tbl_file']." file, ".$dropbox_cnf['tbl_person']." person
  931. WHERE file.c_id = $course_id AND
  932. person.c_id = $course_id AND
  933. file.id IN (".implode(', ',$array).")
  934. AND file.id=person.file_id
  935. AND person.user_id='".api_get_user_id()."'";
  936. $result = Database::query($sql);
  937. $files = array();
  938. while ($row = Database::fetch_array($result)) {
  939. $files[$row['filename']] = array('filename' => $row['filename'],'title' => $row['title'], 'author' => $row['author'], 'description' => $row['description']);
  940. }
  941. // Step 3: create the zip file and add all the files to it
  942. $temp_zip_file = api_get_path(SYS_ARCHIVE_PATH).api_get_unique_id().".zip";
  943. $zip_folder = new PclZip($temp_zip_file);
  944. foreach ($files as $key => $value) {
  945. $zip_folder->add(api_get_path(SYS_COURSE_PATH).$_course['path'].'/dropbox/'.$value['filename'], PCLZIP_OPT_REMOVE_ALL_PATH, PCLZIP_CB_PRE_ADD, 'my_pre_add_callback');
  946. }
  947. /*
  948. * @todo if you want the overview code fix it by yourself
  949. *
  950. // Step 1: create the overview file and add it to the zip
  951. $overview_file_content = generate_html_overview($files, array('filename'), array('title'));
  952. $overview_file = $temp_zip_dir.'overview'.replace_dangerous_char(api_is_western_name_order() ? $_user['firstname'].' '.$_user['lastname'] : $_user['lastname'].' '.$_user['firstname'], 'strict').'.html';
  953. $handle = fopen($overview_file, 'w');
  954. fwrite($handle, $overview_file_content);
  955. // todo: find a different solution for this because even 2 seconds is no guarantee.
  956. sleep(2);*/
  957. // Step 4: we add the overview file
  958. //$zip_folder->add($overview_file, PCLZIP_OPT_REMOVE_PATH, api_get_path(SYS_COURSE_PATH).$_course['path'].'/temp');
  959. // Step 5: send the file for download;
  960. $name = 'dropbox-'.api_get_utc_datetime().'.zip';
  961. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  962. @unlink($temp_zip_file);
  963. exit;
  964. }
  965. /**
  966. * This is a callback function to decrypt the files in the zip file to their normal filename (as stored in the database)
  967. * @param $p_event a variable of PCLZip
  968. * @param $p_header a variable of PCLZip
  969. *
  970. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  971. * @version march 2006
  972. */
  973. function my_pre_add_callback($p_event, &$p_header) {
  974. global $files;
  975. $p_header['stored_filename'] = $files[$p_header['stored_filename']]['title'];
  976. return 1;
  977. }
  978. /**
  979. * @desc Generates the contents of a html file that gives an overview of all the files in the zip file.
  980. * This is to know the information of the files that are inside the zip file (who send it, the comment, ...)
  981. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, March 2006
  982. * @author Ivan Tcholakov, 2010, code for html metadata has been added.
  983. */
  984. function generate_html_overview($files, $dont_show_columns = array(), $make_link = array()) {
  985. $return = '<!DOCTYPE html'."\n";
  986. $return .= "\t".'PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'."\n";
  987. $return .= "\t".'"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'."\n";
  988. $return .= '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="'.api_get_language_isocode().'" lang="'.api_get_language_isocode().'">'."\n";
  989. $return .= "<head>\n\t<title>".get_lang('OverviewOfFilesInThisZip')."</title>\n";
  990. $return .= "\t".'<meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />'."\n";
  991. $return .= "</head>\n\n";
  992. $return .= '<body dir="'.api_get_text_direction().'">'."\n\n";
  993. $return .= "<table border=\"1px\">\n";
  994. $counter = 0;
  995. foreach ($files as $key => $value) {
  996. // Adding the header.
  997. if ($counter == 0) {
  998. $columns_array = array_keys($value);
  999. $return .= "\n<tr>";
  1000. foreach ($columns_array as $columns_array_key => $columns_array_value) {
  1001. if (!in_array($columns_array_value, $dont_show_columns)) {
  1002. $return .= "\n\t<th>".$columns_array_value."</th>";
  1003. }
  1004. $column[] = $columns_array_value;
  1005. }
  1006. $return .= "\n</tr>\n";
  1007. }
  1008. $counter++;
  1009. // Adding the content.
  1010. $return .= "\n<tr>";
  1011. foreach ($column as $column_key => $column_value) {
  1012. if (!in_array($column_value,$dont_show_columns)) {
  1013. $return .= "\n\t<td>";
  1014. if (in_array($column_value, $make_link)) {
  1015. $return .= '<a href="'.$value[$column_value].'">'.$value[$column_value].'</a>';
  1016. } else {
  1017. $return .= $value[$column_value];
  1018. }
  1019. $return .= "</td>";
  1020. }
  1021. }
  1022. $return .= "\n</tr>\n";
  1023. }
  1024. $return .= "\n</table>\n\n</body>";
  1025. $return .= "\n</html>";
  1026. return $return;
  1027. }
  1028. /**
  1029. * @desc This function retrieves the number of feedback messages on every document. This function might become obsolete when
  1030. * the feedback becomes user individual.
  1031. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1032. * @version march 2006
  1033. */
  1034. function get_total_number_feedback($file_id = '') {
  1035. global $dropbox_cnf;
  1036. $course_id = api_get_course_int_id();
  1037. $sql = "SELECT COUNT(feedback_id) AS total, file_id FROM ".$dropbox_cnf['tbl_feedback']."
  1038. WHERE c_id = $course_id GROUP BY file_id";
  1039. $result = Database::query($sql);
  1040. while ($row=Database::fetch_array($result)) {
  1041. $return[$row['file_id']] = $row['total'];
  1042. }
  1043. return $return;
  1044. }
  1045. /**
  1046. * @desc this function checks if the key exists. If this is the case it returns the value, if not it returns 0
  1047. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1048. * @version march 2006
  1049. */
  1050. function check_number_feedback($key, $array) {
  1051. if (is_array($array)) {
  1052. if (key_exists($key, $array)) {
  1053. return $array[$key];
  1054. } else {
  1055. return 0;
  1056. }
  1057. } else {
  1058. return 0;
  1059. }
  1060. }
  1061. /**
  1062. * Get the last access to a given tool of a given user
  1063. * @param $tool string the tool constant
  1064. * @param $course_code the course_id
  1065. * @param $user_id the id of the user
  1066. * @return string last tool access date
  1067. *
  1068. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1069. * @version march 2006
  1070. *
  1071. * @todo consider moving this function to a more appropriate place.
  1072. */
  1073. function get_last_tool_access($tool, $course_code='', $user_id='') {
  1074. global $_course, $_user;
  1075. // The default values of the parameters
  1076. if ($course_code == '') {
  1077. $course_code = $_course['id'];
  1078. }
  1079. if ($user_id == '') {
  1080. $user_id = $_user['user_id'];
  1081. }
  1082. // the table where the last tool access is stored (=track_e_lastaccess)
  1083. $table_last_access = Database::get_statistic_table('track_e_lastaccess');
  1084. $sql = "SELECT access_date FROM $table_last_access WHERE access_user_id='".Database::escape_string($user_id)."'
  1085. AND access_cours_code='".Database::escape_string($course_code)."'
  1086. AND access_tool='".Database::escape_string($tool)."'
  1087. ORDER BY access_date DESC
  1088. LIMIT 1";
  1089. $result = Database::query($sql);
  1090. $row = Database::fetch_array($result);
  1091. return $row['access_date'];
  1092. }