dropbox_functions.inc.php 48 KB

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