dropbox_functions.inc.php 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377
  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_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'], api_is_course_admin(), $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_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'], api_is_course_admin(), $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_courseTutor, $course_info, $origin, $dropbox_unid;
  438. $token = Security::get_token();
  439. $dropbox_person = new Dropbox_Person(api_get_user_id(), api_is_course_admin(), $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['real_id'],
  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($course_info['real_id'], false);
  511. }
  512. }
  513. if (!empty($complete_user_list_for_dropbox)) {
  514. foreach ($complete_user_list_for_dropbox as $k => $e) {
  515. $complete_user_list_for_dropbox[$k] = $e + array(
  516. 'lastcommafirst' => api_get_person_name(
  517. $e['firstname'],
  518. $e['lastname']
  519. )
  520. );
  521. }
  522. $complete_user_list_for_dropbox = TableSort::sort_table($complete_user_list_for_dropbox, 'lastcommafirst');
  523. }
  524. echo '<select name="recipients[]" size="10" multiple class="span4">';
  525. /*
  526. Create the options inside the select box:
  527. List all selected users their user id as value and a name string as display
  528. */
  529. $current_user_id = '';
  530. foreach ($complete_user_list_for_dropbox as $current_user) {
  531. if (($dropbox_person->isCourseTutor
  532. || $dropbox_person->isCourseAdmin
  533. || dropbox_cnf('allowStudentToStudent')
  534. || $current_user['status'] != 5 // Always allow teachers.
  535. || $current_user['tutor_id'] == 1 // Always allow tutors.
  536. ) && $current_user['user_id'] != $_user['user_id']
  537. ) { // Don't include yourself.
  538. if ($current_user['user_id'] == $current_user_id) {
  539. continue;
  540. }
  541. $full_name = $current_user['lastcommafirst'];
  542. $current_user_id = $current_user['user_id'];
  543. echo '<option value="user_'.$current_user_id.'">'.$full_name.'</option>';
  544. }
  545. }
  546. /*
  547. * Show groups
  548. */
  549. if (($dropbox_person->isCourseTutor || $dropbox_person->isCourseAdmin)
  550. && dropbox_cnf('allowGroup') || dropbox_cnf('allowStudentToStudent')
  551. ) {
  552. $complete_group_list_for_dropbox = GroupManager::get_group_list(null, dropbox_cnf('courseId'));
  553. if (count($complete_group_list_for_dropbox) > 0) {
  554. foreach ($complete_group_list_for_dropbox as $current_group) {
  555. if ($current_group['number_of_members'] > 0) {
  556. echo '<option value="group_'.$current_group['id'].'">G: '.$current_group['name'].' - '.$current_group['number_of_members'].' '.get_lang(
  557. 'Users'
  558. ).'</option>';
  559. }
  560. }
  561. }
  562. }
  563. if (($dropbox_person->isCourseTutor || $dropbox_person->isCourseAdmin) && dropbox_cnf('allowMailing')) {
  564. // echo '<option value="mailing">'.get_lang('MailingInSelect').'</option>';
  565. }
  566. if (dropbox_cnf('allowJustUpload')) {
  567. //echo '<option value="upload">'.get_lang('JustUploadInSelect').'</option>';
  568. echo '<option value="user_'.$_user['user_id'].'">'.get_lang('JustUploadInSelect').'</option>';
  569. }
  570. echo '</select>
  571. </div>
  572. </div>';
  573. echo '
  574. <div class="control-group">
  575. <div class="controls">
  576. <button type="Submit" class="upload" name="submitWork">'.get_lang('Upload', '').'</button>
  577. </div>
  578. </div>
  579. ';
  580. echo '</form>';
  581. }
  582. /**
  583. * returns username or false if user isn't registered anymore
  584. * @todo check if this function is still necessary. There might be a library function for this.
  585. */
  586. function getUserNameFromId($id)
  587. {
  588. global $dropbox_cnf;
  589. $mailingId = $id - dropbox_cnf('mailingIdBase');
  590. if ($mailingId > 0) {
  591. return get_lang('MailingAsUsername', '').$mailingId;
  592. }
  593. $id = intval($id);
  594. $sql = "SELECT ".(api_is_western_name_order(
  595. ) ? "CONCAT(firstname,' ', lastname)" : "CONCAT(lastname,' ', firstname)")." AS name
  596. FROM ".$dropbox_cnf['tbl_user']."
  597. WHERE user_id='$id'";
  598. $result = Database::query($sql);
  599. $res = Database::fetch_array($result);
  600. if (!$res) {
  601. return false;
  602. }
  603. return stripslashes($res['name']);
  604. }
  605. /**
  606. * returns loginname or false if user isn't registered anymore
  607. * @todo check if this function is still necessary. There might be a library function for this.
  608. */
  609. function getLoginFromId($id)
  610. {
  611. $id = intval($id);
  612. $sql = "SELECT username
  613. FROM ".dropbox_cnf('tbl_user')."
  614. WHERE user_id='$id'";
  615. $result = Database::query($sql);
  616. $res = Database::fetch_array($result);
  617. if (!$res) {
  618. return false;
  619. }
  620. return stripslashes($res['username']);
  621. }
  622. /**
  623. * @return boolean indicating if user with user_id=$user_id is a course member
  624. * @todo eliminate global
  625. * @todo check if this function is still necessary. There might be a library function for this.
  626. */
  627. function isCourseMember($user_id)
  628. {
  629. $is_course_member = CourseManager::is_user_subscribed_in_course($user_id, api_get_course_int_id(), true);
  630. return $is_course_member;
  631. }
  632. /**
  633. * Checks if there are files in the dropbox_file table that aren't used anymore in dropbox_person table.
  634. * If there are, all entries concerning the file are deleted from the db + the file is deleted from the server
  635. */
  636. function removeUnusedFiles()
  637. {
  638. $course_id = api_get_course_int_id();
  639. // select all files that aren't referenced anymore
  640. $sql = "SELECT DISTINCT f.id, f.filename
  641. FROM ".dropbox_cnf('tbl_file')." f
  642. LEFT JOIN ".dropbox_cnf('tbl_person')." p
  643. ON (f.id = p.file_id)
  644. WHERE p.user_id IS NULL AND
  645. f.c_id = $course_id
  646. ";
  647. $result = Database::query($sql);
  648. while ($res = Database::fetch_array($result)) {
  649. //delete the selected files from the post and file tables
  650. $sql = "DELETE FROM ".dropbox_cnf('tbl_post')." WHERE c_id = $course_id AND file_id = '".$res['id']."'";
  651. Database::query($sql);
  652. $sql = "DELETE FROM ".dropbox_cnf('tbl_file')." WHERE c_id = $course_id AND id ='".$res['id']."'";
  653. Database::query($sql);
  654. //delete file from server
  655. @unlink(dropbox_cnf('sysPath').'/'.$res['filename']);
  656. }
  657. }
  658. /**
  659. *
  660. * Mailing zip-file is posted to (dest_user_id = ) mailing pseudo_id
  661. * and is only visible to its uploader (user_id).
  662. *
  663. * Mailing content files have uploader_id == mailing pseudo_id, a normal recipient,
  664. * and are visible initially to recipient and pseudo_id.
  665. *
  666. * @author René Haentjens, Ghent University
  667. *
  668. * @todo check if this function is still necessary.
  669. */
  670. function getUserOwningThisMailing($mailingPseudoId, $owner = 0, $or_die = '')
  671. {
  672. $course_id = api_get_course_int_id();
  673. global $dropbox_cnf;
  674. $mailingPseudoId = intval($mailingPseudoId);
  675. $sql = "SELECT f.uploader_id
  676. FROM ".$dropbox_cnf['tbl_file']." f
  677. 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)
  678. WHERE
  679. p.dest_user_id = '".$mailingPseudoId."' AND
  680. p.c_id = $course_id
  681. ";
  682. $result = Database::query($sql);
  683. if (!($res = Database::fetch_array($result))) {
  684. die(get_lang('GeneralError').' (code 901)');
  685. }
  686. if ($owner == 0) {
  687. return $res['uploader_id'];
  688. }
  689. if ($res['uploader_id'] == $owner) {
  690. return true;
  691. }
  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. global $dropbox_cnf;
  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(
  716. 'tbl_person'
  717. )." WHERE c_id = $course_id AND user_id='".$mailingPseudoId."'";
  718. Database::query($sql);
  719. $sql = "UPDATE ".dropbox_cnf('tbl_file').
  720. " SET uploader_id='".api_get_user_id(
  721. )."' WHERE c_id = $course_id AND uploader_id='".$mailingPseudoId."'";
  722. Database::query($sql);
  723. }
  724. }
  725. }
  726. /**
  727. * Function that finds a given config setting
  728. *
  729. * @author René Haentjens, Ghent University
  730. */
  731. function dropbox_cnf($variable)
  732. {
  733. return $GLOBALS['dropbox_cnf'][$variable];
  734. }
  735. function store_add_dropbox()
  736. {
  737. global $dropbox_cnf;
  738. global $_user;
  739. $_course = api_get_course_info();
  740. // Validating the form data
  741. // there are no recipients selected
  742. if (!isset($_POST['recipients']) || count($_POST['recipients']) <= 0) {
  743. return get_lang('YouMustSelectAtLeastOneDestinee');
  744. } // Check if all the recipients are valid
  745. else {
  746. $thisIsAMailing = false;
  747. $thisIsJustUpload = false;
  748. foreach ($_POST['recipients'] as $rec) {
  749. if ($rec == 'mailing') {
  750. $thisIsAMailing = true;
  751. } elseif ($rec == 'upload') {
  752. $thisIsJustUpload = true;
  753. } elseif (strpos($rec, 'user_') === 0 && !isCourseMember(substr($rec, strlen('user_')))) {
  754. return get_lang('InvalideUserDetected');
  755. } elseif (strpos($rec, 'group_') !== 0 && strpos($rec, 'user_') !== 0) {
  756. return get_lang('InvalideGroupDetected');
  757. }
  758. }
  759. }
  760. // we are doing a mailing but an additional recipient is selected
  761. if ($thisIsAMailing && (count($_POST['recipients']) != 1)) {
  762. return get_lang('MailingSelectNoOther');
  763. }
  764. // we are doing a just upload but an additional recipient is selected.
  765. // 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)
  766. if ($thisIsJustUpload && (count($_POST['recipients']) != 1)) {
  767. return get_lang('MailingJustUploadSelectNoOther');
  768. }
  769. if (empty($_FILES['file']['name'])) {
  770. $error = true;
  771. return get_lang('NoFileSpecified');
  772. }
  773. // are we overwriting a previous file or sending a new one
  774. $dropbox_overwrite = false;
  775. if (isset($_POST['cb_overwrite']) && $_POST['cb_overwrite']) {
  776. $dropbox_overwrite = true;
  777. }
  778. // doing the upload
  779. $dropbox_filename = $_FILES['file']['name'];
  780. $dropbox_filesize = $_FILES['file']['size'];
  781. $dropbox_filetype = $_FILES['file']['type'];
  782. $dropbox_filetmpname = $_FILES['file']['tmp_name'];
  783. // check if the filesize does not exceed the allowed size.
  784. if ($dropbox_filesize <= 0 || $dropbox_filesize > $dropbox_cnf['maxFilesize']) {
  785. return get_lang(
  786. 'DropboxFileTooBig'
  787. ); // TODO: The "too big" message does not fit in the case of uploading zero-sized file.
  788. }
  789. // check if the file is actually uploaded
  790. if (!is_uploaded_file($dropbox_filetmpname)) { // check user fraud : no clean error msg.
  791. return get_lang('TheFileIsNotUploaded');
  792. }
  793. $upload_ok = FileManager::process_uploaded_file($_FILES['file'], true);
  794. if (!$upload_ok) {
  795. return null;
  796. }
  797. // Try to add an extension to the file if it hasn't got one
  798. $dropbox_filename = FileManager::add_ext_on_mime($dropbox_filename, $dropbox_filetype);
  799. // Replace dangerous characters
  800. $dropbox_filename = api_replace_dangerous_char($dropbox_filename);
  801. // Transform any .php file in .phps fo security
  802. $dropbox_filename = FileManager::php2phps($dropbox_filename);
  803. //filter extension
  804. if (!FileManager::filter_extension($dropbox_filename)) {
  805. return get_lang('UplUnableToSaveFileFilteredExtension');
  806. }
  807. // set title
  808. $dropbox_title = $dropbox_filename;
  809. // set author
  810. if ($_POST['authors'] == '') {
  811. $_POST['authors'] = getUserNameFromId($_user['user_id']);
  812. }
  813. // note: I think we could better migrate everything from here on to separate functions: store_new_dropbox, store_new_mailing, store_just_upload
  814. if ($dropbox_overwrite) {
  815. $dropbox_person = new Dropbox_Person($_user['user_id'], api_is_course_admin(), api_is_course_tutor());
  816. foreach ($dropbox_person->sentWork as $w) {
  817. if ($w->title == $dropbox_filename) {
  818. if (($w->recipients[0]['id'] > dropbox_cnf('mailingIdBase')) xor $thisIsAMailing) {
  819. return get_lang('MailingNonMailingError');
  820. }
  821. if (($w->recipients[0]['id'] == $_user['user_id']) xor $thisIsJustUpload) {
  822. return get_lang('MailingJustUploadSelectNoOther');
  823. }
  824. $dropbox_filename = $w->filename;
  825. $found = true; // note: do we still need this?
  826. break;
  827. }
  828. }
  829. } else { // rename file to login_filename_uniqueId format
  830. $dropbox_filename = getLoginFromId($_user['user_id'])."_".$dropbox_filename."_".uniqid('');
  831. }
  832. // creating the array that contains all the users who will receive the file
  833. $new_work_recipients = array();
  834. foreach ($_POST['recipients'] as $rec) {
  835. if (strpos($rec, 'user_') === 0) {
  836. $new_work_recipients[] = substr($rec, strlen('user_'));
  837. } elseif (strpos($rec, 'group_') === 0) {
  838. $userList = GroupManager::get_subscribed_users(substr($rec, strlen('group_')));
  839. foreach ($userList as $usr) {
  840. if (!in_array($usr['user_id'], $new_work_recipients) && $usr['user_id'] != $_user['user_id']) {
  841. $new_work_recipients[] = $usr['user_id'];
  842. }
  843. }
  844. }
  845. }
  846. @move_uploaded_file($dropbox_filetmpname, dropbox_cnf('sysPath').'/'.$dropbox_filename);
  847. $b_send_mail = api_get_course_setting('email_alert_on_new_doc_dropbox');
  848. if ($b_send_mail) {
  849. foreach ($new_work_recipients as $recipient_id) {
  850. $recipent_temp = UserManager :: get_user_info_by_id($recipient_id);
  851. api_mail_html(
  852. api_get_person_name(
  853. $recipent_temp['firstname'].' '.$recipent_temp['lastname'],
  854. null,
  855. PERSON_NAME_EMAIL_ADDRESS
  856. ),
  857. $recipent_temp['email'],
  858. get_lang('NewDropboxFileUploaded'),
  859. get_lang('NewDropboxFileUploadedContent').' '.api_get_path(
  860. WEB_CODE_PATH
  861. ).'dropbox/index.php?cidReq='.$_course['sysCode']."\n\n".api_get_person_name(
  862. $_user['firstName'],
  863. $_user['lastName'],
  864. null,
  865. PERSON_NAME_EMAIL_ADDRESS
  866. )."\n".get_lang('Email')." : ".$_user['mail'],
  867. api_get_person_name($_user['firstName'], $_user['lastName'], null, PERSON_NAME_EMAIL_ADDRESS),
  868. $_user['mail']
  869. );
  870. }
  871. }
  872. new Dropbox_SentWork($_user['user_id'], $dropbox_title, $_POST['description'], strip_tags(
  873. $_POST['authors']
  874. ), $dropbox_filename, $dropbox_filesize, $new_work_recipients);
  875. Security::clear_token();
  876. return get_lang('FileUploadSucces');
  877. }
  878. /**
  879. * This function displays the firstname and lastname of the user as a link to the user tool.
  880. *
  881. * @see this is the same function as in the new forum, so this probably has to move to a user library.
  882. *
  883. * @todo move this function to the user library (there is a duplicate in work.lib.php)
  884. *
  885. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  886. * @version march 2006
  887. */
  888. function display_user_link_work($user_id, $name = '')
  889. {
  890. if ($user_id != 0) {
  891. if (empty($name)) {
  892. $table_user = Database::get_main_table(TABLE_MAIN_USER);
  893. $sql = "SELECT * FROM $table_user WHERE user_id='".Database::escape_string($user_id)."'";
  894. $result = Database::query($sql);
  895. $row = Database::fetch_array($result);
  896. return '<a href="../user/userInfo.php?uInfo='.$row['user_id'].'">'.api_get_person_name(
  897. $row['firstname'],
  898. $row['lastname']
  899. ).'</a>';
  900. } else {
  901. $user_id = intval($user_id);
  902. return '<a href="../user/userInfo.php?uInfo='.$user_id.'">'.Security::remove_XSS($name).'</a>';
  903. }
  904. } else {
  905. return $name.' ('.get_lang('Anonymous').')';
  906. }
  907. }
  908. /**
  909. * this function transforms the array containing all the feedback into something visually attractive.
  910. *
  911. * @param an array containing all the feedback about the given message.
  912. *
  913. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  914. * @version march 2006
  915. */
  916. function feedback($array)
  917. {
  918. foreach ($array as $key => $value) {
  919. $output .= format_feedback($value);
  920. }
  921. $output .= feedback_form();
  922. return $output;
  923. }
  924. /**
  925. * This function returns the html code to display the feedback messages on a given dropbox file
  926. * @param $feedback_array an array that contains all the feedback messages about the given document.
  927. * @return html code
  928. * @todo add the form for adding new comment (if the other party has not deleted it yet).
  929. *
  930. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  931. * @version march 2006
  932. */
  933. function format_feedback($feedback)
  934. {
  935. $output .= display_user_link_work($feedback['author_user_id']);
  936. $output .= '&nbsp;&nbsp;'.api_convert_and_format_date($feedback['feedback_date'], DATE_TIME_FORMAT_LONG).'<br />';
  937. $output .= '<div style="padding-top:6px">'.nl2br($feedback['feedback']).'</div><hr size="1" noshade/><br />';
  938. return $output;
  939. }
  940. /**
  941. * this function returns the code for the form for adding a new feedback message to a dropbox file.
  942. * @return html code
  943. *
  944. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  945. * @version march 2006
  946. */
  947. function feedback_form()
  948. {
  949. $return = get_lang('AddNewFeedback').'<br />';
  950. $number_users_who_see_file = check_if_file_exist($_GET['id']);
  951. if ($number_users_who_see_file) {
  952. $token = Security::get_token();
  953. $return .= '<textarea name="feedback" style="width: 80%; height: 80px;"></textarea>';
  954. $return .= '<input type="hidden" name="sec_token" value="'.$token.'"/>';
  955. $return .= '<br /><button type="submit" class="add" name="store_feedback" value="'.get_lang('Ok').'"
  956. onclick="javascript: document.form_dropbox.attributes.action.value = document.location;">'.get_lang(
  957. 'AddComment'
  958. ).'</button>';
  959. } else {
  960. $return .= get_lang('AllUsersHaveDeletedTheFileAndWillNotSeeFeedback');
  961. }
  962. return $return;
  963. }
  964. function user_can_download_file($id, $user_id)
  965. {
  966. global $dropbox_cnf;
  967. $course_id = api_get_course_int_id();
  968. $id = intval($id);
  969. $user_id = intval($user_id);
  970. $sql = "SELECT file_id FROM ".$dropbox_cnf['tbl_person']." WHERE c_id = $course_id AND user_id = $user_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"]." WHERE c_id = $course_id AND dest_user_id = $user_id AND file_id = ".$id;
  974. $result = Database::query($sql);
  975. $count = Database::num_rows($result);
  976. return $number_users_who_see_file > 0 || $count > 0;
  977. }
  978. // 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
  979. // add feedback since the other users will never get to see the feedback.
  980. function check_if_file_exist($id)
  981. {
  982. global $dropbox_cnf;
  983. $id = intval($id);
  984. $course_id = api_get_course_int_id();
  985. $sql = "SELECT file_id FROM ".$dropbox_cnf['tbl_person']." WHERE c_id = $course_id AND file_id = ".$id;
  986. $result = Database::query($sql);
  987. $number_users_who_see_file = Database::num_rows($result);
  988. $sql = "SELECT file_id FROM ".$dropbox_cnf["tbl_post"]." WHERE c_id = $course_id AND file_id = ".$id;
  989. $result = Database::query($sql);
  990. $count = Database::num_rows($result);
  991. return $number_users_who_see_file > 0 || $count > 0;
  992. }
  993. /**
  994. * @return a language string (depending on the success or failure.
  995. *
  996. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  997. * @version march 2006
  998. */
  999. function store_feedback()
  1000. {
  1001. global $dropbox_cnf;
  1002. if (!is_numeric($_GET['id'])) {
  1003. return get_lang('FeedbackError');
  1004. }
  1005. $course_id = api_get_course_int_id();
  1006. if (empty($_POST['feedback'])) {
  1007. return get_lang('PleaseTypeText');
  1008. } else {
  1009. $sql = "INSERT INTO ".$dropbox_cnf['tbl_feedback']." (c_id, file_id, author_user_id, feedback, feedback_date) VALUES
  1010. ($course_id, '".intval($_GET['id'])."','".api_get_user_id()."','".Database::escape_string(
  1011. $_POST['feedback']
  1012. )."', '".api_get_utc_datetime()."')";
  1013. Database::query($sql);
  1014. return get_lang('DropboxFeedbackStored');
  1015. }
  1016. }
  1017. /**
  1018. * This function downloads all the files of the inputarray into one zip
  1019. * @param $array an array containing all the ids of the files that have to be downloaded.
  1020. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1021. * @todo consider removing the check if the user has received or sent this file (zip download of a folder already sufficiently checks for this).
  1022. * @todo integrate some cleanup function that removes zip files that are older than 2 days
  1023. *
  1024. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1025. * @author Julio Montoya Addin c_id support
  1026. * @version march 2006
  1027. */
  1028. function zip_download($array)
  1029. {
  1030. $_course = api_get_course_info();
  1031. global $dropbox_cnf;
  1032. global $files;
  1033. $course_id = api_get_course_int_id();
  1034. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  1035. // place to temporarily stash the zipfiles
  1036. $temp_zip_dir = api_get_path(SYS_COURSE_PATH);
  1037. $array = array_map('intval', $array);
  1038. // note: we also have to add the check if the user has received or sent this file.
  1039. $sql = "SELECT DISTINCT file.filename, file.title, file.author, file.description
  1040. FROM ".$dropbox_cnf['tbl_file']." file INNER JOIN ".$dropbox_cnf['tbl_person']." person
  1041. ON (person.file_id=file.id AND file.c_id = $course_id AND person.c_id = $course_id)
  1042. INNER JOIN ".$dropbox_cnf['tbl_post']." post
  1043. ON (post.file_id = file.id AND post.c_id = $course_id AND file.c_id = $course_id)
  1044. WHERE file.id IN (".implode(', ', $array).") AND
  1045. file.id = person.file_id AND
  1046. (person.user_id = '".api_get_user_id()."' OR post.dest_user_id = '".api_get_user_id()."' ) ";
  1047. $result = Database::query($sql);
  1048. $files = array();
  1049. while ($row = Database::fetch_array($result)) {
  1050. $files[$row['filename']] = array(
  1051. 'filename' => $row['filename'],
  1052. 'title' => $row['title'],
  1053. 'author' => $row['author'],
  1054. 'description' => $row['description']
  1055. );
  1056. }
  1057. // Step 3: create the zip file and add all the files to it
  1058. $temp_zip_file = api_get_path(SYS_ARCHIVE_PATH).api_get_unique_id().".zip";
  1059. $zip_folder = new PclZip($temp_zip_file);
  1060. foreach ($files as $key => $value) {
  1061. $zip_folder->add(
  1062. api_get_path(SYS_COURSE_PATH).$_course['path'].'/dropbox/'.$value['filename'],
  1063. PCLZIP_OPT_REMOVE_ALL_PATH,
  1064. PCLZIP_CB_PRE_ADD,
  1065. 'my_pre_add_callback'
  1066. );
  1067. }
  1068. $name = 'dropbox-'.api_get_utc_datetime().'.zip';
  1069. DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  1070. @unlink($temp_zip_file);
  1071. exit;
  1072. }
  1073. /**
  1074. * This is a callback function to decrypt the files in the zip file to their normal filename (as stored in the database)
  1075. * @param $p_event a variable of PCLZip
  1076. * @param $p_header a variable of PCLZip
  1077. *
  1078. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1079. * @version march 2006
  1080. */
  1081. function my_pre_add_callback($p_event, &$p_header)
  1082. {
  1083. global $files;
  1084. $p_header['stored_filename'] = $files[$p_header['stored_filename']]['title'];
  1085. return 1;
  1086. }
  1087. /**
  1088. * @desc Generates the contents of a html file that gives an overview of all the files in the zip file.
  1089. * This is to know the information of the files that are inside the zip file (who send it, the comment, ...)
  1090. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, March 2006
  1091. * @author Ivan Tcholakov, 2010, code for html metadata has been added.
  1092. */
  1093. function generate_html_overview($files, $dont_show_columns = array(), $make_link = array())
  1094. {
  1095. $return = '<!DOCTYPE html'."\n";
  1096. $return .= "\t".'PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'."\n";
  1097. $return .= "\t".'"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'."\n";
  1098. $return .= '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="'.api_get_language_isocode(
  1099. ).'" lang="'.api_get_language_isocode().'">'."\n";
  1100. $return .= "<head>\n\t<title>".get_lang('OverviewOfFilesInThisZip')."</title>\n";
  1101. $return .= "\t".'<meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding(
  1102. ).'" />'."\n";
  1103. $return .= "</head>\n\n";
  1104. $return .= '<body dir="'.api_get_text_direction().'">'."\n\n";
  1105. $return .= "<table border=\"1px\">\n";
  1106. $counter = 0;
  1107. foreach ($files as $key => $value) {
  1108. // Adding the header.
  1109. if ($counter == 0) {
  1110. $columns_array = array_keys($value);
  1111. $return .= "\n<tr>";
  1112. foreach ($columns_array as $columns_array_key => $columns_array_value) {
  1113. if (!in_array($columns_array_value, $dont_show_columns)) {
  1114. $return .= "\n\t<th>".$columns_array_value."</th>";
  1115. }
  1116. $column[] = $columns_array_value;
  1117. }
  1118. $return .= "\n</tr>\n";
  1119. }
  1120. $counter++;
  1121. // Adding the content.
  1122. $return .= "\n<tr>";
  1123. foreach ($column as $column_key => $column_value) {
  1124. if (!in_array($column_value, $dont_show_columns)) {
  1125. $return .= "\n\t<td>";
  1126. if (in_array($column_value, $make_link)) {
  1127. $return .= '<a href="'.$value[$column_value].'">'.$value[$column_value].'</a>';
  1128. } else {
  1129. $return .= $value[$column_value];
  1130. }
  1131. $return .= "</td>";
  1132. }
  1133. }
  1134. $return .= "\n</tr>\n";
  1135. }
  1136. $return .= "\n</table>\n\n</body>";
  1137. $return .= "\n</html>";
  1138. return $return;
  1139. }
  1140. /**
  1141. * @desc This function retrieves the number of feedback messages on every document. This function might become obsolete when
  1142. * the feedback becomes user individual.
  1143. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1144. * @version march 2006
  1145. */
  1146. function get_total_number_feedback($file_id = '')
  1147. {
  1148. global $dropbox_cnf;
  1149. $course_id = api_get_course_int_id();
  1150. $sql = "SELECT COUNT(feedback_id) AS total, file_id FROM ".$dropbox_cnf['tbl_feedback']."
  1151. WHERE c_id = $course_id GROUP BY file_id";
  1152. $result = Database::query($sql);
  1153. while ($row = Database::fetch_array($result)) {
  1154. $return[$row['file_id']] = $row['total'];
  1155. }
  1156. return $return;
  1157. }
  1158. /**
  1159. * @desc this function checks if the key exists. If this is the case it returns the value, if not it returns 0
  1160. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1161. * @version march 2006
  1162. */
  1163. function check_number_feedback($key, $array)
  1164. {
  1165. if (is_array($array)) {
  1166. if (key_exists($key, $array)) {
  1167. return $array[$key];
  1168. } else {
  1169. return 0;
  1170. }
  1171. } else {
  1172. return 0;
  1173. }
  1174. }
  1175. /**
  1176. * Get the last access to a given tool of a given user
  1177. * @param $tool string the tool constant
  1178. * @param $course_code the course_id
  1179. * @param $user_id the id of the user
  1180. * @return string last tool access date
  1181. *
  1182. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1183. * @version march 2006
  1184. *
  1185. * @todo consider moving this function to a more appropriate place.
  1186. */
  1187. function get_last_tool_access($tool, $courseId = '', $user_id = '')
  1188. {
  1189. global $_course, $_user;
  1190. // The default values of the parameters
  1191. if ($courseId == '') {
  1192. $courseId = api_get_course_int_id();
  1193. }
  1194. $courseId = intval($courseId);
  1195. if ($user_id == '') {
  1196. $user_id = $_user['user_id'];
  1197. }
  1198. // the table where the last tool access is stored (=track_e_lastaccess)
  1199. $table_last_access = Database::get_main_table('track_e_lastaccess');
  1200. $sql = "SELECT access_date FROM $table_last_access WHERE access_user_id='".Database::escape_string($user_id)."'
  1201. AND c_id ='".Database::escape_string($courseId)."'
  1202. AND access_tool='".Database::escape_string($tool)."'
  1203. ORDER BY access_date DESC
  1204. LIMIT 1";
  1205. $result = Database::query($sql);
  1206. $row = Database::fetch_array($result);
  1207. return $row['access_date'];
  1208. }