dropbox_functions.inc.php 49 KB

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