dropbox_functions.inc.php 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * This file contains additional dropbox functions. Initially there were some
  6. * functions in the init files also but I have moved them over
  7. * to one file -- Patrick Cool <patrick.cool@UGent.be>, Ghent University
  8. * @author Julio Montoya adding c_id support
  9. */
  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 string|null 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.'_') && $key != 'view_received_category' && $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' || 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
  104. * in this category: move them to the root, download them as a zip, delete them
  105. *
  106. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  107. * @version march 2006
  108. */
  109. function delete_category($action, $id, $user_id = null)
  110. {
  111. $course_id = api_get_course_int_id();
  112. $is_courseAdmin = api_is_course_admin();
  113. $is_courseTutor = api_is_course_tutor();
  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 = Database::get_course_table(TABLE_DROPBOX_POST);
  128. $id_field = 'file_id';
  129. $return_message = get_lang('ReceivedCatgoryDeleted');
  130. } elseif ($action == 'deletesentcategory') {
  131. $sentreceived = 'sent';
  132. $entries_table = Database::get_course_table(TABLE_DROPBOX_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 ". Database::get_course_table(TABLE_DROPBOX_CATEGORY) ."
  140. WHERE c_id = $course_id AND cat_id='".intval($id)."' AND $sentreceived='1'";
  141. Database::query($sql);
  142. // step 2: delete all the documents in this category
  143. $sql = "SELECT * FROM ".$entries_table."
  144. WHERE c_id = $course_id AND cat_id='".intval($id)."'";
  145. $result = Database::query($sql);
  146. while ($row = Database::fetch_array($result)) {
  147. $dropboxfile = new Dropbox_Person($user_id, $is_courseAdmin, $is_courseTutor);
  148. if ($action == 'deletereceivedcategory') {
  149. $dropboxfile->deleteReceivedWork($row[$id_field]);
  150. }
  151. if ($action == 'deletesentcategory') {
  152. $dropboxfile->deleteSentWork($row[$id_field]);
  153. }
  154. }
  155. return $return_message;
  156. }
  157. /**
  158. * Displays the form to move one individual file to a category
  159. *@ return html code of the form that appears in a message box.
  160. * @author Julio Montoya - function rewritten
  161. */
  162. function display_move_form($part, $id, $target = array(), $extra_params = array(), $viewReceivedCategory, $viewSentCategory, $view)
  163. {
  164. $form = new FormValidator(
  165. 'form1',
  166. 'post',
  167. api_get_self().'?view_received_category='.$viewReceivedCategory.'&view_sent_category='.$viewSentCategory.'&view='.$view.'&'.$extra_params
  168. );
  169. $form->addElement('header', get_lang('MoveFileTo'));
  170. $form->addElement('hidden', 'id', intval($id));
  171. $form->addElement('hidden', 'part', Security::remove_XSS($part));
  172. $options = array('0' => get_lang('Root'));
  173. foreach ($target as $category) {
  174. $options[$category['cat_id']] = $category['cat_name'];
  175. }
  176. $form->addElement('select', 'move_target', get_lang('MoveFileTo'), $options);
  177. $form->addElement('button', 'do_move', get_lang('MoveFile'));
  178. $form->display();
  179. }
  180. /**
  181. * This function moves a file to a different category
  182. *
  183. * @param $id the id of the file we are moving
  184. * @param $target the id of the folder we are moving to
  185. * @param string $part are we moving a received file or a sent file?
  186. *
  187. * @return string string
  188. *
  189. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  190. * @version march 2006
  191. */
  192. function store_move($id, $target, $part)
  193. {
  194. $_user = api_get_user_info();
  195. $course_id = api_get_course_int_id();
  196. if ((isset($id) && $id != '') &&
  197. (isset($target) && $target != '') &&
  198. (isset($part) && $part != '')
  199. ) {
  200. if ($part == 'received') {
  201. $sql = "UPDATE ".Database::get_course_table(TABLE_DROPBOX_POST)."
  202. SET cat_id = ".intval($target)."
  203. WHERE c_id = $course_id AND dest_user_id = ".intval($_user['user_id'])."
  204. AND file_id = ".intval($id)."";
  205. Database::query($sql);
  206. $return_message = get_lang('ReceivedFileMoved');
  207. }
  208. if ($part == 'sent') {
  209. $sql = "UPDATE ". Database::get_course_table(TABLE_DROPBOX_FILE) ."
  210. SET cat_id = ".intval($target)."
  211. WHERE
  212. c_id = $course_id AND
  213. uploader_id = ".intval($_user['user_id'])." AND
  214. id = ".intval($id);
  215. Database::query($sql);
  216. $return_message = get_lang('SentFileMoved');
  217. }
  218. } else {
  219. $return_message = get_lang('NotMovedError');
  220. }
  221. return $return_message;
  222. }
  223. /**
  224. * This function retrieves all dropbox categories and returns them as an array
  225. *
  226. * @param $filter default '', when we need only the categories of the sent or the received part.
  227. *
  228. * @return array
  229. *
  230. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  231. * @version march 2006
  232. */
  233. function get_dropbox_categories($filter = '')
  234. {
  235. $course_id = api_get_course_int_id();
  236. $_user = api_get_user_info();
  237. $return_array = array();
  238. $session_id = api_get_session_id();
  239. $condition_session = api_get_session_condition($session_id);
  240. $sql = "SELECT * FROM ". Database::get_course_table(TABLE_DROPBOX_CATEGORY) ."
  241. WHERE c_id = $course_id AND user_id='".$_user['user_id']."' $condition_session";
  242. $result = Database::query($sql);
  243. while ($row = Database::fetch_array($result)) {
  244. if (($filter == 'sent' && $row['sent'] == 1) || ($filter == 'received' && $row['received'] == 1) || $filter == '') {
  245. $return_array[$row['cat_id']] = $row;
  246. }
  247. }
  248. return $return_array;
  249. }
  250. /**
  251. * Get a dropbox category details
  252. * @param int The category ID
  253. * @return array The details of this category
  254. */
  255. function get_dropbox_category($id)
  256. {
  257. $course_id = api_get_course_int_id();
  258. if (empty($id) or $id != intval($id)) { return array(); }
  259. $sql = "SELECT * FROM ". Database::get_course_table(TABLE_DROPBOX_CATEGORY) ."
  260. WHERE c_id = $course_id AND cat_id='".$id."'";
  261. $res = Database::query($sql);
  262. if ($res === false) {
  263. return array();
  264. }
  265. $row = Database::fetch_assoc($res);
  266. return $row;
  267. }
  268. /**
  269. * This functions stores a new dropboxcategory
  270. *
  271. * @var it might not seem very elegant if you create a category in sent
  272. * and in received with the same name that you get two entries in the
  273. * dropbox_category table but it is the easiest solution. You get
  274. * cat_name | received | sent | user_id
  275. * test | 1 | 0 | 237
  276. * test | 0 | 1 | 237
  277. * more elegant would be
  278. * test | 1 | 1 | 237
  279. *
  280. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  281. * @version march 2006
  282. */
  283. function store_addcategory()
  284. {
  285. $course_id = api_get_course_int_id();
  286. $_user = api_get_user_info();
  287. // check if the target is valid
  288. if ($_POST['target'] == 'sent') {
  289. $sent = 1;
  290. $received = 0;
  291. } elseif ($_POST['target'] == 'received') {
  292. $sent = 0;
  293. $received = 1;
  294. } else {
  295. return get_lang('Error');
  296. }
  297. // check if the category name is valid
  298. if ($_POST['category_name'] == '') {
  299. return array('type' => 'error', 'message' => get_lang('ErrorPleaseGiveCategoryName'));
  300. }
  301. if (!isset($_POST['edit_id'])) {
  302. $session_id = api_get_session_id();
  303. // step 3a, we check if the category doesn't already exist
  304. $sql = "SELECT * FROM ". Database::get_course_table(TABLE_DROPBOX_CATEGORY) ."
  305. WHERE
  306. c_id = $course_id AND
  307. user_id='".$_user['user_id']."' AND
  308. cat_name='".Database::escape_string($_POST['category_name'])."' AND
  309. received='".$received."' AND
  310. sent='$sent' AND
  311. session_id='$session_id'";
  312. $result = Database::query($sql);
  313. // step 3b, we add the category if it does not exist yet.
  314. if (Database::num_rows($result) == 0) {
  315. $params = [
  316. 'cat_id' => 0,
  317. 'c_id' => $course_id,
  318. 'cat_name' => $_POST['category_name'],
  319. 'received' => $received,
  320. 'sent' => $sent,
  321. 'user_id' => $_user['user_id'],
  322. 'session_id' => $session_id,
  323. ];
  324. $id = Database::insert(Database::get_course_table(TABLE_DROPBOX_CATEGORY), $params);
  325. if ($id) {
  326. $sql = "UPDATE ". Database::get_course_table(TABLE_DROPBOX_CATEGORY) ." SET cat_id = iid WHERE iid = $id";
  327. Database::query($sql);
  328. }
  329. return array('type' => 'confirmation', 'message' => get_lang('CategoryStored'));
  330. } else {
  331. return array('type' => 'error', 'message' => get_lang('CategoryAlreadyExistsEditIt'));
  332. }
  333. } else {
  334. $params = [
  335. 'cat_name' => $_POST['category_name'],
  336. 'received' => $received,
  337. 'sent' => $sent
  338. ];
  339. Database::update(
  340. Database::get_course_table(TABLE_DROPBOX_CATEGORY),
  341. $params,
  342. [
  343. 'c_id = ? AND user_id = ? AND cat_id = ?' => [
  344. $course_id,
  345. $_user['user_id'],
  346. $_POST['edit_id'],
  347. ],
  348. ]
  349. );
  350. return array('type' => 'confirmation', 'message' => get_lang('CategoryModified'));
  351. }
  352. }
  353. /**
  354. * This function displays the form to add a new category.
  355. *
  356. * @param $category_name this parameter is the name of the category (used when no section is selected)
  357. * @param $id this is the id of the category we are editing.
  358. *
  359. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  360. @author Julio Montoya UI changes
  361. *
  362. * @version march 2006
  363. */
  364. function display_addcategory_form($category_name = '', $id = '', $action)
  365. {
  366. $course_id = api_get_course_int_id();
  367. $title = get_lang('AddNewCategory');
  368. if (isset($id) && $id != '') {
  369. // retrieve the category we are editing
  370. $sql = "SELECT * FROM ". Database::get_course_table(TABLE_DROPBOX_CATEGORY) ."
  371. WHERE c_id = $course_id AND cat_id = ".intval($id);
  372. $result = Database::query($sql);
  373. $row = Database::fetch_array($result);
  374. if (empty($category_name)) {
  375. // after an edit with an error we do not want to return to the
  376. // original name but the name we already modified.
  377. // (happens when createinrecievedfiles AND createinsentfiles are not checked)
  378. $category_name = $row['cat_name'];
  379. }
  380. if ($row['received'] == '1') {
  381. $target = 'received';
  382. }
  383. if ($row['sent'] == '1') {
  384. $target = 'sent';
  385. }
  386. $title = get_lang('EditCategory');
  387. }
  388. if ($action == 'addreceivedcategory') {
  389. $target = 'received';
  390. }
  391. if ($action == 'addsentcategory') {
  392. $target = 'sent';
  393. }
  394. if ($action == 'editcategory') {
  395. $text = get_lang('ModifyCategory');
  396. } elseif ($action == 'addreceivedcategory' || $action == 'addsentcategory') {
  397. $text = get_lang('CreateCategory');
  398. }
  399. $form = new FormValidator(
  400. 'add_new_category',
  401. 'post',
  402. api_get_self().'?'.api_get_cidreq().'&view='.Security::remove_XSS($_GET['view'])
  403. );
  404. $form->addElement('header', $title);
  405. if (isset($id) && $id != '') {
  406. $form->addElement('hidden', 'edit_id', intval($id));
  407. }
  408. $form->addElement('hidden', 'action', Security::remove_XSS($action));
  409. $form->addElement('hidden', 'target', Security::remove_XSS($target));
  410. $form->addElement('text', 'category_name', get_lang('CategoryName'));
  411. $form->addRule('category_name', get_lang('ThisFieldIsRequired'), 'required');
  412. $form->addButtonSave($text, 'StoreCategory');
  413. $defaults = array();
  414. $defaults['category_name'] = $category_name;
  415. $form->setDefaults($defaults);
  416. $form->display();
  417. }
  418. /**
  419. * this function displays the form to upload a new item to the dropbox.
  420. *
  421. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  422. * @version march 2006
  423. */
  424. function display_add_form($dropbox_unid, $viewReceivedCategory, $viewSentCategory, $view)
  425. {
  426. $course_info = api_get_course_info();
  427. $_user = api_get_user_info();
  428. $is_courseAdmin = api_is_course_admin();
  429. $is_courseTutor = api_is_course_tutor();
  430. $origin = isset($_GET['origin']) ? $_GET['origin'] : null;
  431. $token = Security::get_token();
  432. $dropbox_person = new Dropbox_Person(
  433. api_get_user_id(),
  434. $is_courseAdmin,
  435. $is_courseTutor
  436. );
  437. $form = new FormValidator(
  438. 'sent_form',
  439. 'post',
  440. api_get_self().'?view_received_category='.$viewReceivedCategory.'&view_sent_category='.$viewSentCategory.'&view='.$view.'&'.api_get_cidreq(),
  441. null,
  442. array('enctype' => 'multipart/form-data', 'onsubmit' => 'javascript: return checkForm(this);')
  443. );
  444. $form->addElement('header', get_lang('UploadNewFile'));
  445. $maxFileSize = api_get_setting('dropbox_max_filesize');
  446. $form->addElement('hidden', 'MAX_FILE_SIZE', $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. $allowOverwrite = api_get_setting('dropbox_allow_overwrite');
  452. if ($allowOverwrite == 'true') {
  453. $form->addElement('checkbox', 'cb_overwrite', null, get_lang('OverwriteFile'), array('id' => 'cb_overwrite'));
  454. }
  455. // List of all users in this course and all virtual courses combined with it
  456. if (api_get_session_id()) {
  457. $complete_user_list_for_dropbox = array();
  458. if (api_get_setting('dropbox_allow_student_to_student') == 'true' || $_user['status'] != STUDENT) {
  459. $complete_user_list_for_dropbox = CourseManager:: get_user_list_from_course_code(
  460. $course_info['code'],
  461. api_get_session_id(),
  462. null,
  463. null,
  464. 0
  465. );
  466. }
  467. $complete_user_list2 = CourseManager::get_coach_list_from_course_code(
  468. $course_info['code'],
  469. api_get_session_id()
  470. );
  471. $generalCoachList = array();
  472. $courseCoachList = array();
  473. foreach ($complete_user_list2 as $coach) {
  474. if ($coach['type'] == 'general_coach') {
  475. $generalCoachList[] = $coach;
  476. } else {
  477. $courseCoachList[] = $coach;
  478. }
  479. }
  480. $hideCourseCoach = api_get_setting('dropbox_hide_course_coach');
  481. if ($hideCourseCoach == 'false') {
  482. $complete_user_list_for_dropbox = array_merge(
  483. $complete_user_list_for_dropbox,
  484. $courseCoachList
  485. );
  486. }
  487. $hideGeneralCoach = api_get_setting('dropbox_hide_general_coach');
  488. if ($hideGeneralCoach == 'false') {
  489. $complete_user_list_for_dropbox = array_merge(
  490. $complete_user_list_for_dropbox,
  491. $generalCoachList
  492. );
  493. }
  494. } else {
  495. if (api_get_setting('dropbox_allow_student_to_student') == 'true' || $_user['status'] != STUDENT) {
  496. $complete_user_list_for_dropbox = CourseManager::get_user_list_from_course_code(
  497. $course_info['code'],
  498. api_get_session_id()
  499. );
  500. } else {
  501. $complete_user_list_for_dropbox = CourseManager::get_teacher_list_from_course_code(
  502. $course_info['code'],
  503. false
  504. );
  505. }
  506. }
  507. if (!empty($complete_user_list_for_dropbox)) {
  508. foreach ($complete_user_list_for_dropbox as $k => $e) {
  509. $complete_user_list_for_dropbox[$k] = $e + array('lastcommafirst' => api_get_person_name($e['firstname'], $e['lastname']));
  510. }
  511. $complete_user_list_for_dropbox = TableSort::sort_table($complete_user_list_for_dropbox, 'lastcommafirst');
  512. }
  513. /*
  514. Create the options inside the select box:
  515. List all selected users their user id as value and a name string as display
  516. */
  517. $current_user_id = '';
  518. $allowStudentToStudent = api_get_setting('dropbox_allow_student_to_student');
  519. $options = array();
  520. $userGroup = new UserGroup();
  521. foreach ($complete_user_list_for_dropbox as $current_user) {
  522. if (($dropbox_person -> isCourseTutor
  523. || $dropbox_person -> isCourseAdmin
  524. || $allowStudentToStudent == 'true'
  525. || $current_user['status'] != 5 // Always allow teachers.
  526. || $current_user['is_tutor'] == 1 // Always allow tutors.
  527. ) && $current_user['user_id'] != $_user['user_id']) { // Don't include yourself.
  528. if ($current_user['user_id'] == $current_user_id) {
  529. continue;
  530. }
  531. $userId = $current_user['user_id'];
  532. $userInfo = api_get_user_info($userId);
  533. if ($userInfo['status'] != INVITEE) {
  534. $groupNameListToString = '';
  535. if (!empty($groups)) {
  536. $groupNameList = array_column($groups, 'name');
  537. $groupNameListToString = ' - ['.implode(', ', $groupNameList).']';
  538. }
  539. $groups = $userGroup->getUserGroupListByUser($userId);
  540. $full_name = $userInfo['complete_name'].$groupNameListToString;
  541. $current_user_id = $current_user['user_id'];
  542. $options['user_' . $current_user_id] = $full_name;
  543. }
  544. }
  545. }
  546. /*
  547. * Show groups
  548. */
  549. $allowGroups = api_get_setting('dropbox_allow_group');
  550. if (($dropbox_person->isCourseTutor || $dropbox_person->isCourseAdmin)
  551. && $allowGroups == 'true' || $allowStudentToStudent == 'true'
  552. ) {
  553. $complete_group_list_for_dropbox = GroupManager::get_group_list(null, $course_info['code']);
  554. if (count($complete_group_list_for_dropbox) > 0) {
  555. foreach ($complete_group_list_for_dropbox as $current_group) {
  556. if ($current_group['number_of_members'] > 0) {
  557. $options['group_'.$current_group['id']] = 'G: '.$current_group['name'].' - '.$current_group['number_of_members'].' '.get_lang('Users');
  558. }
  559. }
  560. }
  561. }
  562. $allowUpload = api_get_setting('dropbox_allow_just_upload');
  563. if ($allowUpload == 'true') {
  564. $options['user_'.$_user['user_id']] = get_lang('JustUploadInSelect');
  565. }
  566. $form->addSelect(
  567. 'recipients',
  568. get_lang('SendTo'),
  569. $options,
  570. array(
  571. 'multiple' => 'multiple',
  572. 'size' => '10'
  573. )
  574. );
  575. $form->addButtonUpload(get_lang('Upload'), 'submitWork');
  576. $headers = array(
  577. get_lang('Upload'),
  578. get_lang('Upload').' ('.get_lang('Simple').')',
  579. );
  580. $multipleForm = new FormValidator(
  581. 'sent_multiple',
  582. 'post',
  583. '#',
  584. null,
  585. array('enctype' => 'multipart/form-data', 'id' => 'fileupload')
  586. );
  587. $multipleForm->addSelect(
  588. 'recipients',
  589. get_lang('SendTo'),
  590. $options,
  591. array(
  592. 'multiple' => 'multiple',
  593. 'size' => '10',
  594. 'id' => 'recipient_form'
  595. )
  596. );
  597. $url = api_get_path(WEB_AJAX_PATH).'dropbox.ajax.php?'.api_get_cidreq().'&a=upload_file&id=';
  598. $multipleForm->addHtml('<div id="multiple_form" style="display:none">');
  599. $multipleForm->addMultipleUpload($url);
  600. $multipleForm->addHtml('</div>');
  601. echo Display::tabs(
  602. $headers,
  603. array($multipleForm->returnForm(), $form->returnForm()),
  604. 'tabs'
  605. );
  606. }
  607. /**
  608. * @param string $user_id
  609. * @return boolean indicating if user with user_id=$user_id is a course member
  610. * @todo check if this function is still necessary. There might be a library function for this.
  611. */
  612. function isCourseMember($user_id)
  613. {
  614. $_course = api_get_course_info();
  615. $course_code = $_course['code'];
  616. $is_course_member = CourseManager::is_user_subscribed_in_course($user_id, $course_code, true);
  617. return $is_course_member;
  618. }
  619. /**
  620. * Checks if there are files in the dropbox_file table that aren't used anymore in dropbox_person table.
  621. * If there are, all entries concerning the file are deleted from the db + the file is deleted from the server
  622. */
  623. function removeUnusedFiles()
  624. {
  625. $_course = api_get_course_info();
  626. $course_id = $_course['real_id'];
  627. // select all files that aren't referenced anymore
  628. $sql = "SELECT DISTINCT f.id, f.filename
  629. FROM " . Database::get_course_table(TABLE_DROPBOX_FILE) . " f
  630. LEFT JOIN " . Database::get_course_table(TABLE_DROPBOX_PERSON) . " p
  631. ON (f.id = p.file_id)
  632. WHERE p.user_id IS NULL AND
  633. f.c_id = $course_id
  634. ";
  635. $result = Database::query($sql);
  636. while ($res = Database::fetch_array($result)) {
  637. //delete the selected files from the post and file tables
  638. $sql = "DELETE FROM " . Database::get_course_table(TABLE_DROPBOX_POST) . "
  639. WHERE c_id = $course_id AND file_id = '" . $res['id'] . "'";
  640. Database::query($sql);
  641. $sql = "DELETE FROM " . Database::get_course_table(TABLE_DROPBOX_FILE) . "
  642. WHERE c_id = $course_id AND id ='" . $res['id'] . "'";
  643. Database::query($sql);
  644. //delete file from server
  645. @unlink( api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/dropbox/' . $res['filename']);
  646. }
  647. }
  648. /**
  649. *
  650. * Mailing zip-file is posted to (dest_user_id = ) mailing pseudo_id
  651. * and is only visible to its uploader (user_id).
  652. *
  653. * Mailing content files have uploader_id == mailing pseudo_id, a normal recipient,
  654. * and are visible initially to recipient and pseudo_id.
  655. *
  656. * @author René Haentjens, Ghent University
  657. *
  658. * @todo check if this function is still necessary.
  659. */
  660. function getUserOwningThisMailing($mailingPseudoId, $owner = 0, $or_die = '')
  661. {
  662. $course_id = api_get_course_int_id();
  663. $mailingPseudoId = intval($mailingPseudoId);
  664. $sql = "SELECT f.uploader_id
  665. FROM " . Database::get_course_table(TABLE_DROPBOX_FILE) . " f
  666. LEFT JOIN " . Database::get_course_table(TABLE_DROPBOX_POST) . " p
  667. ON (f.id = p.file_id AND f.c_id = $course_id AND p.c_id = $course_id)
  668. WHERE
  669. p.dest_user_id = '" . $mailingPseudoId . "' AND
  670. p.c_id = $course_id
  671. ";
  672. $result = Database::query($sql);
  673. if (!($res = Database::fetch_array($result)))
  674. die(get_lang('GeneralError').' (code 901)');
  675. if ($owner == 0) return $res['uploader_id'];
  676. if ($res['uploader_id'] == $owner) return true;
  677. die(get_lang('GeneralError').' (code '.$or_die.')');
  678. }
  679. /**
  680. * @author René Haentjens, Ghent University
  681. * @todo check if this function is still necessary.
  682. */
  683. function removeMoreIfMailing($file_id)
  684. {
  685. $course_id = api_get_course_int_id();
  686. // when deleting a mailing zip-file (posted to mailingPseudoId):
  687. // 1. the detail window is no longer reachable, so
  688. // for all content files, delete mailingPseudoId from person-table
  689. // 2. finding the owner (getUserOwningThisMailing) is no longer possible, so
  690. // for all content files, replace mailingPseudoId by owner as uploader
  691. $file_id = intval($file_id);
  692. $sql = "SELECT p.dest_user_id
  693. FROM " . Database::get_course_table(TABLE_DROPBOX_POST) . " p
  694. WHERE c_id = $course_id AND p.file_id = '" . $file_id . "'";
  695. $result = Database::query($sql);
  696. if ($res = Database::fetch_array($result)) {
  697. $mailingPseudoId = $res['dest_user_id'];
  698. $mailId = get_mail_id_base();
  699. if ($mailingPseudoId > $mailId) {
  700. $sql = "DELETE FROM " . Database::get_course_table(TABLE_DROPBOX_PERSON) . "
  701. WHERE c_id = $course_id AND user_id='" . $mailingPseudoId . "'";
  702. Database::query($sql);
  703. $sql = "UPDATE " . Database::get_course_table(TABLE_DROPBOX_FILE) ."
  704. SET uploader_id='" . api_get_user_id() . "'
  705. WHERE c_id = $course_id AND uploader_id='" . $mailingPseudoId . "'";
  706. Database::query($sql);
  707. }
  708. }
  709. }
  710. /**
  711. * @param array $file
  712. *
  713. * @return array|null|string
  714. */
  715. function store_add_dropbox($file = [])
  716. {
  717. $_course = api_get_course_info();
  718. $_user = api_get_user_info();
  719. $dropbox_cnf = getDropboxConf();
  720. if (empty($file)) {
  721. $file = isset($_FILES['file']) ? $_FILES['file'] : null;
  722. }
  723. // Validating the form data
  724. // there are no recipients selected
  725. if (!isset($_POST['recipients']) || count($_POST['recipients']) <= 0) {
  726. return get_lang('YouMustSelectAtLeastOneDestinee');
  727. } else {
  728. // Check if all the recipients are valid
  729. $thisIsAMailing = false;
  730. $thisIsJustUpload = false;
  731. foreach ($_POST['recipients'] as $rec) {
  732. if ($rec == 'mailing') {
  733. $thisIsAMailing = true;
  734. } elseif ($rec == 'upload') {
  735. $thisIsJustUpload = true;
  736. } elseif (strpos($rec, 'user_') === 0 && !isCourseMember(substr($rec, strlen('user_')))) {
  737. Display::addFlash(Display::return_message(get_lang('InvalideUserDetected'), 'warning'));
  738. return false;
  739. } elseif (strpos($rec, 'group_') !== 0 && strpos($rec, 'user_') !== 0) {
  740. Display::addFlash(Display::return_message(get_lang('InvalideGroupDetected'), 'warning'));
  741. return false;
  742. }
  743. }
  744. }
  745. // we are doing a mailing but an additional recipient is selected
  746. if ($thisIsAMailing && (count($_POST['recipients']) != 1)) {
  747. Display::addFlash(Display::return_message(get_lang('MailingSelectNoOther'), 'warning'));
  748. return false;
  749. }
  750. // we are doing a just upload but an additional recipient is selected.
  751. // note: why can't this be valid? It is like sending a document to
  752. // yourself AND to a different person (I do this quite often with my e-mails)
  753. if ($thisIsJustUpload && (count($_POST['recipients']) != 1)) {
  754. Display::addFlash(Display::return_message(get_lang('MailingJustUploadSelectNoOther'), 'warning'));
  755. return false;
  756. }
  757. if (empty($file['name'])) {
  758. Display::addFlash(Display::return_message(get_lang('NoFileSpecified'), 'warning'));
  759. return false;
  760. }
  761. // are we overwriting a previous file or sending a new one
  762. $dropbox_overwrite = false;
  763. if (isset($_POST['cb_overwrite']) && $_POST['cb_overwrite']) {
  764. $dropbox_overwrite = true;
  765. }
  766. // doing the upload
  767. $dropbox_filename = $file['name'];
  768. $dropbox_filesize = $file['size'];
  769. $dropbox_filetype = $file['type'];
  770. $dropbox_filetmpname = $file['tmp_name'];
  771. // check if the filesize does not exceed the allowed size.
  772. $maxFileSize = api_get_setting('dropbox_max_filesize');
  773. if ($dropbox_filesize <= 0 || $dropbox_filesize > $maxFileSize) {
  774. Display::addFlash(Display::return_message(get_lang('DropboxFileTooBig'), 'warning'));
  775. return false;
  776. }
  777. // check if the file is actually uploaded
  778. if (!is_uploaded_file($dropbox_filetmpname)) { // check user fraud : no clean error msg.
  779. Display::addFlash(Display::return_message(get_lang('TheFileIsNotUploaded'), 'warning'));
  780. return false;
  781. }
  782. $upload_ok = process_uploaded_file($file, true);
  783. if (!$upload_ok) {
  784. return null;
  785. }
  786. // Try to add an extension to the file if it hasn't got one
  787. $dropbox_filename = add_ext_on_mime($dropbox_filename, $dropbox_filetype);
  788. // Replace dangerous characters
  789. $dropbox_filename = api_replace_dangerous_char($dropbox_filename);
  790. // Transform any .php file in .phps fo security
  791. $dropbox_filename = php2phps($dropbox_filename);
  792. //filter extension
  793. if (!filter_extension($dropbox_filename)) {
  794. Display::addFlash(Display::return_message(get_lang('UplUnableToSaveFileFilteredExtension'), 'warning'));
  795. return false;
  796. }
  797. // set title
  798. $dropbox_title = $dropbox_filename;
  799. // note: I think we could better migrate everything from here on to
  800. // separate functions: store_new_dropbox, store_new_mailing, store_just_upload
  801. if ($dropbox_overwrite) {
  802. $dropbox_person = new Dropbox_Person(
  803. $_user['user_id'],
  804. api_is_course_admin(),
  805. api_is_course_tutor()
  806. );
  807. $mailId = get_mail_id_base();
  808. foreach ($dropbox_person->sentWork as $w) {
  809. if ($w->title == $dropbox_filename) {
  810. if (($w->recipients[0]['id'] > $mailId) xor $thisIsAMailing) {
  811. Display::addFlash(Display::return_message(get_lang('MailingNonMailingError'), 'warning'));
  812. return false;
  813. }
  814. if (($w->recipients[0]['id'] == $_user['user_id']) xor $thisIsJustUpload) {
  815. Display::addFlash(Display::return_message(get_lang('MailingJustUploadSelectNoOther'), 'warning'));
  816. return false;
  817. }
  818. $dropbox_filename = $w->filename;
  819. $found = true; // note: do we still need this?
  820. break;
  821. }
  822. }
  823. } else { // rename file to login_filename_uniqueId format
  824. $dropbox_filename = $_user['username'] . "_" . $dropbox_filename . "_".uniqid('');
  825. }
  826. // creating the array that contains all the users who will receive the file
  827. $new_work_recipients = array();
  828. foreach ($_POST['recipients'] as $rec) {
  829. if (strpos($rec, 'user_') === 0) {
  830. $new_work_recipients[] = substr($rec, strlen('user_'));
  831. } elseif (strpos($rec, 'group_') === 0) {
  832. $groupInfo = GroupManager::get_group_properties(substr($rec, strlen('group_')));
  833. $userList = GroupManager::get_subscribed_users($groupInfo['iid']);
  834. foreach ($userList as $usr) {
  835. if (!in_array($usr['user_id'], $new_work_recipients) && $usr['user_id'] != $_user['user_id']) {
  836. $new_work_recipients[] = $usr['user_id'];
  837. }
  838. }
  839. }
  840. }
  841. @move_uploaded_file($dropbox_filetmpname, api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/dropbox/' . $dropbox_filename);
  842. $b_send_mail = api_get_course_setting('email_alert_on_new_doc_dropbox');
  843. if ($b_send_mail) {
  844. foreach ($new_work_recipients as $recipient_id) {
  845. $recipent_temp = api_get_user_info($recipient_id);
  846. $additionalParameters = array(
  847. 'smsType' => SmsPlugin::NEW_FILE_SHARED_COURSE_BY,
  848. 'userId' => $recipient_id,
  849. 'courseTitle' => $_course['title'],
  850. 'userUsername' => $recipent_temp['username']
  851. );
  852. api_mail_html(
  853. api_get_person_name(
  854. $recipent_temp['firstname'].' '.$recipent_temp['lastname'],
  855. null,
  856. PERSON_NAME_EMAIL_ADDRESS
  857. ),
  858. $recipent_temp['email'],
  859. get_lang('NewDropboxFileUploaded'),
  860. get_lang('NewDropboxFileUploadedContent').' <a href="'.api_get_path(WEB_CODE_PATH).'dropbox/index.php?' . api_get_cidreq() . '">'.get_lang('SeeFile').'</a>'.
  861. "\n\n".
  862. api_get_person_name(
  863. $_user['firstName'],
  864. $_user['lastName'],
  865. null,
  866. PERSON_NAME_EMAIL_ADDRESS
  867. )."\n". get_lang('Email') ." : ".$_user['mail'],
  868. api_get_person_name(
  869. $_user['firstName'],
  870. $_user['lastName'],
  871. null,
  872. PERSON_NAME_EMAIL_ADDRESS
  873. ),
  874. $_user['mail'],
  875. null,
  876. null,
  877. null,
  878. $additionalParameters
  879. );
  880. }
  881. }
  882. $result = new Dropbox_SentWork(
  883. $_user['user_id'],
  884. $dropbox_title,
  885. isset($_POST['description']) ? $_POST['description'] : '',
  886. api_get_user_id(),
  887. $dropbox_filename,
  888. $dropbox_filesize,
  889. $new_work_recipients
  890. );
  891. Security::clear_token();
  892. Display::addFlash(Display::return_message(get_lang('FileUploadSucces')));
  893. return $result;
  894. }
  895. /**
  896. * Transforms the array containing all the feedback into something visually attractive.
  897. *
  898. * @param an array containing all the feedback about the given message.
  899. *
  900. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  901. * @version march 2006
  902. */
  903. function feedback($array) {
  904. $output = null;
  905. foreach ($array as $value) {
  906. $output .= format_feedback($value);
  907. }
  908. $output .= feedback_form();
  909. return $output;
  910. }
  911. /**
  912. * This function returns the html code to display the feedback messages on a given dropbox file
  913. * @param $feedback_array an array that contains all the feedback messages about the given document.
  914. * @return string code
  915. * @todo add the form for adding new comment (if the other party has not deleted it yet).
  916. *
  917. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  918. * @version march 2006
  919. */
  920. function format_feedback($feedback)
  921. {
  922. $userInfo = api_get_user_info($feedback['author_user_id']);
  923. $output = UserManager::getUserProfileLink($userInfo);
  924. $output .= '&nbsp;&nbsp;'.api_convert_and_format_date($feedback['feedback_date'], DATE_TIME_FORMAT_LONG).'<br />';
  925. $output .= '<div style="padding-top:6px">'.nl2br($feedback['feedback']).'</div><hr size="1" noshade/><br />';
  926. return $output;
  927. }
  928. /**
  929. * this function returns the code for the form for adding a new feedback message to a dropbox file.
  930. * @return string code
  931. *
  932. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  933. * @version march 2006
  934. */
  935. function feedback_form()
  936. {
  937. $return = get_lang('AddNewFeedback').'<br />';
  938. $number_users_who_see_file = check_if_file_exist($_GET['id']);
  939. if ($number_users_who_see_file) {
  940. $token = Security::get_token();
  941. $return .= '<textarea name="feedback" style="width: 80%; height: 80px;"></textarea>';
  942. $return .= '<input type="hidden" name="sec_token" value="'.$token.'"/>';
  943. $return .= '<br /><button type="submit" class="add" name="store_feedback" value="'.get_lang('Ok').'"
  944. onclick="javascript: document.form_dropbox.attributes.action.value = document.location;">'.get_lang('AddComment').'</button>';
  945. } else {
  946. $return .= get_lang('AllUsersHaveDeletedTheFileAndWillNotSeeFeedback');
  947. }
  948. return $return;
  949. }
  950. function user_can_download_file($id, $user_id)
  951. {
  952. $course_id = api_get_course_int_id();
  953. $id = intval($id);
  954. $user_id = intval($user_id);
  955. $sql = "SELECT file_id FROM ". Database::get_course_table(TABLE_DROPBOX_PERSON) ."
  956. WHERE c_id = $course_id AND user_id = $user_id AND file_id = ".$id;
  957. $result = Database::query($sql);
  958. $number_users_who_see_file = Database::num_rows($result);
  959. $sql = "SELECT file_id FROM ". Database::get_course_table(TABLE_DROPBOX_POST) ."
  960. WHERE c_id = $course_id AND dest_user_id = $user_id AND file_id = ".$id;
  961. $result = Database::query($sql);
  962. $count = Database::num_rows($result);
  963. return $number_users_who_see_file > 0 || $count > 0;
  964. }
  965. // we now check if the other users have not delete this document yet.
  966. // If this is the case then it is useless to see the
  967. // add feedback since the other users will never get to see the feedback.
  968. function check_if_file_exist($id)
  969. {
  970. $id = intval($id);
  971. $course_id = api_get_course_int_id();
  972. $sql = "SELECT file_id FROM ". Database::get_course_table(TABLE_DROPBOX_PERSON) ."
  973. WHERE c_id = $course_id AND file_id = ".$id;
  974. $result = Database::query($sql);
  975. $number_users_who_see_file = Database::num_rows($result);
  976. $sql = "SELECT file_id FROM ". Database::get_course_table(TABLE_DROPBOX_POST) ."
  977. WHERE c_id = $course_id AND file_id = ".$id;
  978. $result = Database::query($sql);
  979. $count = Database::num_rows($result);
  980. return $number_users_who_see_file > 0 || $count > 0;
  981. }
  982. /**
  983. * @return string language string (depending on the success or failure.
  984. *
  985. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  986. * @version march 2006
  987. */
  988. function store_feedback()
  989. {
  990. if (!is_numeric($_GET['id'])) {
  991. return get_lang('FeedbackError');
  992. }
  993. $course_id = api_get_course_int_id();
  994. if (empty($_POST['feedback'])) {
  995. return get_lang('PleaseTypeText');
  996. } else {
  997. $params = [
  998. 'c_id' => $course_id,
  999. 'file_id' => $_GET['id'],
  1000. 'author_user_id' => api_get_user_id(),
  1001. 'feedback' => $_POST['feedback'],
  1002. 'feedback_date' => api_get_utc_datetime(),
  1003. ];
  1004. $id = Database::insert(Database::get_course_table(TABLE_DROPBOX_FEEDBACK), $params);
  1005. if ($id) {
  1006. $sql = "UPDATE ". Database::get_course_table(TABLE_DROPBOX_FEEDBACK) ." SET feedback_id = iid WHERE iid = $id";
  1007. Database::query($sql);
  1008. }
  1009. return get_lang('DropboxFeedbackStored');
  1010. }
  1011. }
  1012. /**
  1013. * This function downloads all the files of the input array into one zip
  1014. * @param array $fileList containing all the ids of the files that have to be downloaded.
  1015. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1016. * @todo consider removing the check if the user has received or sent this file (zip download of a folder already sufficiently checks for this).
  1017. * @todo integrate some cleanup function that removes zip files that are older than 2 days
  1018. *
  1019. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1020. * @author Julio Montoya Addin c_id support
  1021. * @version march 2006
  1022. */
  1023. function zip_download($fileList)
  1024. {
  1025. $_course = api_get_course_info();
  1026. $course_id = api_get_course_int_id();
  1027. $fileList = array_map('intval', $fileList);
  1028. // note: we also have to add the check if the user has received or sent this file.
  1029. $sql = "SELECT DISTINCT file.filename, file.title, file.author, file.description
  1030. FROM ". Database::get_course_table(TABLE_DROPBOX_FILE) ." file
  1031. INNER JOIN ". Database::get_course_table(TABLE_DROPBOX_PERSON) ." person
  1032. ON (person.file_id=file.id AND file.c_id = $course_id AND person.c_id = $course_id)
  1033. INNER JOIN ". Database::get_course_table(TABLE_DROPBOX_POST) ." post
  1034. ON (post.file_id = file.id AND post.c_id = $course_id AND file.c_id = $course_id)
  1035. WHERE
  1036. file.id IN (".implode(', ', $fileList).") AND
  1037. file.id = person.file_id AND
  1038. (
  1039. person.user_id = '".api_get_user_id()."' OR
  1040. post.dest_user_id = '".api_get_user_id()."'
  1041. ) ";
  1042. $result = Database::query($sql);
  1043. $files = array();
  1044. while ($row = Database::fetch_array($result)) {
  1045. $files[$row['filename']] = array(
  1046. 'filename' => $row['filename'],
  1047. 'title' => $row['title'],
  1048. 'author' => $row['author'],
  1049. 'description' => $row['description']
  1050. );
  1051. }
  1052. // Step 3: create the zip file and add all the files to it
  1053. $temp_zip_file = api_get_path(SYS_ARCHIVE_PATH).api_get_unique_id().".zip";
  1054. Session::write('dropbox_files_to_download', $files);
  1055. $zip = new PclZip($temp_zip_file);
  1056. foreach ($files as $value) {
  1057. $zip->add(
  1058. api_get_path(SYS_COURSE_PATH).$_course['path'].'/dropbox/'.$value['filename'],
  1059. PCLZIP_OPT_REMOVE_ALL_PATH,
  1060. PCLZIP_CB_PRE_ADD,
  1061. 'my_pre_add_callback'
  1062. );
  1063. }
  1064. Session::erase('dropbox_files_to_download');
  1065. $name = 'dropbox-'.api_get_utc_datetime().'.zip';
  1066. $result = DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  1067. if ($result === false) {
  1068. api_not_allowed(true);
  1069. }
  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 array $p_event a variable of PCLZip
  1076. * @param array $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. $files = Session::read('dropbox_files_to_download');
  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().'" lang="'.api_get_language_isocode().'">'."\n";
  1099. $return .= "<head>\n\t<title>".get_lang('OverviewOfFilesInThisZip')."</title>\n";
  1100. $return .= "\t".'<meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />'."\n";
  1101. $return .= "</head>\n\n";
  1102. $return .= '<body dir="'.api_get_text_direction().'">'."\n\n";
  1103. $return .= "<table border=\"1px\">\n";
  1104. $counter = 0;
  1105. foreach ($files as $value) {
  1106. // Adding the header.
  1107. if ($counter == 0) {
  1108. $columns_array = array_keys($value);
  1109. $return .= "\n<tr>";
  1110. foreach ($columns_array as $columns_array_key => $columns_array_value) {
  1111. if (!in_array($columns_array_value, $dont_show_columns)) {
  1112. $return .= "\n\t<th>".$columns_array_value."</th>";
  1113. }
  1114. $column[] = $columns_array_value;
  1115. }
  1116. $return .= "\n</tr>\n";
  1117. }
  1118. $counter++;
  1119. // Adding the content.
  1120. $return .= "\n<tr>";
  1121. foreach ($column as $column_key => $column_value) {
  1122. if (!in_array($column_value,$dont_show_columns)) {
  1123. $return .= "\n\t<td>";
  1124. if (in_array($column_value, $make_link)) {
  1125. $return .= '<a href="'.$value[$column_value].'">'.$value[$column_value].'</a>';
  1126. } else {
  1127. $return .= $value[$column_value];
  1128. }
  1129. $return .= "</td>";
  1130. }
  1131. }
  1132. $return .= "\n</tr>\n";
  1133. }
  1134. $return .= "\n</table>\n\n</body>";
  1135. $return .= "\n</html>";
  1136. return $return;
  1137. }
  1138. /**
  1139. * @desc This function retrieves the number of feedback messages on every document. This function might become obsolete when
  1140. * the feedback becomes user individual.
  1141. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1142. * @version march 2006
  1143. */
  1144. function get_total_number_feedback($file_id = '')
  1145. {
  1146. $course_id = api_get_course_int_id();
  1147. $sql = "SELECT COUNT(feedback_id) AS total, file_id
  1148. FROM ". Database::get_course_table(TABLE_DROPBOX_FEEDBACK) ."
  1149. WHERE c_id = $course_id GROUP BY file_id";
  1150. $result = Database::query($sql);
  1151. $return = array();
  1152. while ($row=Database::fetch_array($result)) {
  1153. $return[$row['file_id']] = $row['total'];
  1154. }
  1155. return $return;
  1156. }
  1157. /**
  1158. * @desc this function checks if the key exists. If this is the case it returns the value, if not it returns 0
  1159. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1160. * @version march 2006
  1161. */
  1162. function check_number_feedback($key, $array)
  1163. {
  1164. if (is_array($array)) {
  1165. if (array_key_exists($key, $array)) {
  1166. return $array[$key];
  1167. } else {
  1168. return 0;
  1169. }
  1170. } else {
  1171. return 0;
  1172. }
  1173. }
  1174. /**
  1175. * Get the last access to a given tool of a given user
  1176. * @param $tool string the tool constant
  1177. * @param $courseId the course_id
  1178. * @param $user_id the id of the user
  1179. * @return string last tool access date
  1180. *
  1181. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1182. * @version march 2006
  1183. *
  1184. * @todo consider moving this function to a more appropriate place.
  1185. */
  1186. function get_last_tool_access($tool, $courseId = null, $user_id = null)
  1187. {
  1188. // The default values of the parameters
  1189. if (empty($courseId)) {
  1190. $courseId = api_get_course_int_id();
  1191. }
  1192. if (empty($user_id)) {
  1193. $user_id = api_get_user_id();
  1194. }
  1195. // the table where the last tool access is stored (=track_e_lastaccess)
  1196. $table_last_access = Database::get_main_table('track_e_lastaccess');
  1197. $sql = "SELECT access_date FROM $table_last_access
  1198. WHERE
  1199. access_user_id = ".intval($user_id)." AND
  1200. c_id='".intval($courseId)."' AND
  1201. access_tool='".Database::escape_string($tool)."'
  1202. ORDER BY access_date DESC
  1203. LIMIT 1";
  1204. $result = Database::query($sql);
  1205. $row = Database::fetch_array($result);
  1206. return $row['access_date'];
  1207. }
  1208. /**
  1209. * Previously $dropbox_cnf['mailingIdBase'], returns a mailing ID to generate a mail ID
  1210. * @return int
  1211. */
  1212. function get_mail_id_base()
  1213. {
  1214. // false = no mailing functionality
  1215. //$dropbox_cnf['mailingIdBase'] = 10000000; // bigger than any user_id,
  1216. // allowing enough space for pseudo_ids as uploader_id, dest_user_id, user_id:
  1217. // mailing pseudo_id = dropbox_cnf('mailingIdBase') + mailing id
  1218. return 10000000;
  1219. }