dropbox_functions.inc.php 45 KB

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