dropbox_functions.inc.php 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365
  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('add_new_category', 'post', api_get_self().'?view='.Security::remove_XSS($_GET['view']));
  400. $form->addElement('header', $title);
  401. if (isset($id) && $id != '') {
  402. $form->addElement('hidden', 'edit_id', intval($id));
  403. }
  404. $form->addElement('hidden', 'action', Security::remove_XSS($action));
  405. $form->addElement('hidden', 'target', Security::remove_XSS($target));
  406. $form->addElement('text', 'category_name', get_lang('CategoryName'));
  407. $form->addRule('category_name', get_lang('ThisFieldIsRequired'), 'required');
  408. $form->addButtonSave($text, 'StoreCategory');
  409. $defaults = array();
  410. $defaults['category_name'] = $category_name;
  411. $form->setDefaults($defaults);
  412. $form->display();
  413. }
  414. /**
  415. * this function displays the form to upload a new item to the dropbox.
  416. *
  417. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  418. * @version march 2006
  419. */
  420. function display_add_form($dropbox_unid, $viewReceivedCategory, $viewSentCategory, $view)
  421. {
  422. $course_info = api_get_course_info();
  423. $_user = api_get_user_info();
  424. $is_courseAdmin = api_is_course_admin();
  425. $is_courseTutor = api_is_course_tutor();
  426. $origin = isset($_GET['origin']) ? $_GET['origin'] : null;
  427. $token = Security::get_token();
  428. $dropbox_person = new Dropbox_Person(
  429. api_get_user_id(),
  430. $is_courseAdmin,
  431. $is_courseTutor
  432. );
  433. $form = new FormValidator(
  434. 'sent_form',
  435. 'post',
  436. api_get_self().'?view_received_category='.$viewReceivedCategory.'&view_sent_category='.$viewSentCategory.'&view='.$view.'&'.api_get_cidreq(),
  437. null,
  438. array('enctype' => 'multipart/form-data', 'onsubmit' => 'javascript: return checkForm(this);')
  439. );
  440. $form->addElement('header', get_lang('UploadNewFile'));
  441. $maxFileSize = api_get_setting('dropbox_max_filesize');
  442. $form->addElement('hidden', 'MAX_FILE_SIZE', $maxFileSize);
  443. $form->addElement('hidden', 'dropbox_unid', $dropbox_unid);
  444. $form->addElement('hidden', 'sec_token', $token);
  445. $form->addElement('hidden', 'origin', $origin);
  446. $form->addElement('file', 'file', get_lang('UploadFile'), array('onChange' => 'javascript: checkfile(this.value);'));
  447. $allowOverwrite = api_get_setting('dropbox_allow_overwrite');
  448. if ($allowOverwrite == 'true') {
  449. $form->addElement('checkbox', 'cb_overwrite', null, get_lang('OverwriteFile'), array('id' => 'cb_overwrite'));
  450. }
  451. // List of all users in this course and all virtual courses combined with it
  452. if (api_get_session_id()) {
  453. $complete_user_list_for_dropbox = array();
  454. if (api_get_setting('dropbox_allow_student_to_student') == 'true' || $_user['status'] != STUDENT) {
  455. $complete_user_list_for_dropbox = CourseManager:: get_user_list_from_course_code(
  456. $course_info['code'],
  457. api_get_session_id(),
  458. null,
  459. null,
  460. 0
  461. );
  462. }
  463. $complete_user_list2 = CourseManager::get_coach_list_from_course_code(
  464. $course_info['code'],
  465. api_get_session_id()
  466. );
  467. $generalCoachList = array();
  468. $courseCoachList = array();
  469. foreach ($complete_user_list2 as $coach) {
  470. if ($coach['type'] == 'general_coach') {
  471. $generalCoachList[] = $coach;
  472. } else {
  473. $courseCoachList[] = $coach;
  474. }
  475. }
  476. $hideCourseCoach = api_get_setting('dropbox_hide_course_coach');
  477. if ($hideCourseCoach == 'false') {
  478. $complete_user_list_for_dropbox = array_merge(
  479. $complete_user_list_for_dropbox,
  480. $courseCoachList
  481. );
  482. }
  483. $hideGeneralCoach = api_get_setting('dropbox_hide_general_coach');
  484. if ($hideGeneralCoach == 'false') {
  485. $complete_user_list_for_dropbox = array_merge(
  486. $complete_user_list_for_dropbox,
  487. $generalCoachList
  488. );
  489. }
  490. } else {
  491. if (api_get_setting('dropbox_allow_student_to_student') == 'true' || $_user['status'] != STUDENT) {
  492. $complete_user_list_for_dropbox = CourseManager::get_user_list_from_course_code(
  493. $course_info['code'],
  494. api_get_session_id()
  495. );
  496. } else {
  497. $complete_user_list_for_dropbox = CourseManager::getTeacherListFromCourse(
  498. $course_info['real_id'],
  499. false
  500. );
  501. }
  502. }
  503. if (!empty($complete_user_list_for_dropbox)) {
  504. foreach ($complete_user_list_for_dropbox as $k => $e) {
  505. $complete_user_list_for_dropbox[$k] = $e + array('lastcommafirst' => api_get_person_name($e['firstname'], $e['lastname']));
  506. }
  507. $complete_user_list_for_dropbox = TableSort::sort_table($complete_user_list_for_dropbox, 'lastcommafirst');
  508. }
  509. /*
  510. Create the options inside the select box:
  511. List all selected users their user id as value and a name string as display
  512. */
  513. $current_user_id = '';
  514. $allowStudentToStudent = api_get_setting('dropbox_allow_student_to_student');
  515. $options = array();
  516. $userGroup = new UserGroup();
  517. foreach ($complete_user_list_for_dropbox as $current_user) {
  518. if (($dropbox_person -> isCourseTutor
  519. || $dropbox_person -> isCourseAdmin
  520. || $allowStudentToStudent == 'true'
  521. || $current_user['status'] != 5 // Always allow teachers.
  522. || $current_user['is_tutor'] == 1 // Always allow tutors.
  523. ) && $current_user['user_id'] != $_user['user_id']) { // Don't include yourself.
  524. if ($current_user['user_id'] == $current_user_id) {
  525. continue;
  526. }
  527. $userId = $current_user['user_id'];
  528. $userInfo = api_get_user_info($userId);
  529. if ($userInfo['status'] != INVITEE) {
  530. $groupNameListToString = '';
  531. if (!empty($groups)) {
  532. $groupNameList = array_column($groups, 'name');
  533. $groupNameListToString = ' - ['.implode(', ', $groupNameList).']';
  534. }
  535. $groups = $userGroup->getUserGroupListByUser($userId);
  536. $full_name = $userInfo['complete_name'].$groupNameListToString;
  537. $current_user_id = $current_user['user_id'];
  538. $options['user_' . $current_user_id] = $full_name;
  539. }
  540. }
  541. }
  542. /*
  543. * Show groups
  544. */
  545. $allowGroups = api_get_setting('dropbox_allow_group');
  546. if (($dropbox_person->isCourseTutor || $dropbox_person->isCourseAdmin)
  547. && $allowGroups == 'true' || $allowStudentToStudent == 'true'
  548. ) {
  549. $complete_group_list_for_dropbox = GroupManager::get_group_list(null, $course_info['code']);
  550. if (count($complete_group_list_for_dropbox) > 0) {
  551. foreach ($complete_group_list_for_dropbox as $current_group) {
  552. if ($current_group['number_of_members'] > 0) {
  553. $options['group_'.$current_group['id']] = 'G: '.$current_group['name'].' - '.$current_group['number_of_members'].' '.get_lang('Users');
  554. }
  555. }
  556. }
  557. }
  558. $allowUpload = api_get_setting('dropbox_allow_just_upload');
  559. if ($allowUpload == 'true') {
  560. $options['user_'.$_user['user_id']] = get_lang('JustUploadInSelect');
  561. }
  562. $form->addSelect(
  563. 'recipients',
  564. get_lang('SendTo'),
  565. $options,
  566. array(
  567. 'multiple' => 'multiple',
  568. 'size' => '10'
  569. )
  570. );
  571. $form->addButtonUpload(get_lang('Upload'), 'submitWork');
  572. $headers = array(
  573. get_lang('Upload'),
  574. get_lang('Upload').' ('.get_lang('Simple').')',
  575. );
  576. $multipleForm = new FormValidator(
  577. 'sent_multiple',
  578. 'post',
  579. '#',
  580. null,
  581. array('enctype' => 'multipart/form-data', 'id' => 'fileupload')
  582. );
  583. $multipleForm->addSelect(
  584. 'recipients',
  585. get_lang('SendTo'),
  586. $options,
  587. array(
  588. 'multiple' => 'multiple',
  589. 'size' => '10',
  590. 'id' => 'recipient_form'
  591. )
  592. );
  593. $url = api_get_path(WEB_AJAX_PATH).'dropbox.ajax.php?'.api_get_cidreq().'&a=upload_file&id=';
  594. $multipleForm->addHtml('<div id="multiple_form" style="display:none">');
  595. $multipleForm->addMultipleUpload($url);
  596. $multipleForm->addHtml('</div>');
  597. echo Display::tabs(
  598. $headers,
  599. array($multipleForm->returnForm(), $form->returnForm()),
  600. 'tabs'
  601. );
  602. }
  603. /**
  604. * @param string $user_id
  605. * @return boolean indicating if user with user_id=$user_id is a course member
  606. * @todo check if this function is still necessary. There might be a library function for this.
  607. */
  608. function isCourseMember($user_id)
  609. {
  610. $_course = api_get_course_info();
  611. $course_code = $_course['code'];
  612. $is_course_member = CourseManager::is_user_subscribed_in_course($user_id, $course_code, true);
  613. return $is_course_member;
  614. }
  615. /**
  616. * Checks if there are files in the dropbox_file table that aren't used anymore in dropbox_person table.
  617. * If there are, all entries concerning the file are deleted from the db + the file is deleted from the server
  618. */
  619. function removeUnusedFiles()
  620. {
  621. $_course = api_get_course_info();
  622. $course_id = $_course['real_id'];
  623. // select all files that aren't referenced anymore
  624. $sql = "SELECT DISTINCT f.id, f.filename
  625. FROM " . Database::get_course_table(TABLE_DROPBOX_FILE) . " f
  626. LEFT JOIN " . Database::get_course_table(TABLE_DROPBOX_PERSON) . " p
  627. ON (f.id = p.file_id)
  628. WHERE p.user_id IS NULL AND
  629. f.c_id = $course_id
  630. ";
  631. $result = Database::query($sql);
  632. while ($res = Database::fetch_array($result)) {
  633. //delete the selected files from the post and file tables
  634. $sql = "DELETE FROM " . Database::get_course_table(TABLE_DROPBOX_POST) . "
  635. WHERE c_id = $course_id AND file_id = '" . $res['id'] . "'";
  636. Database::query($sql);
  637. $sql = "DELETE FROM " . Database::get_course_table(TABLE_DROPBOX_FILE) . "
  638. WHERE c_id = $course_id AND id ='" . $res['id'] . "'";
  639. Database::query($sql);
  640. //delete file from server
  641. @unlink( api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/dropbox/' . $res['filename']);
  642. }
  643. }
  644. /**
  645. *
  646. * Mailing zip-file is posted to (dest_user_id = ) mailing pseudo_id
  647. * and is only visible to its uploader (user_id).
  648. *
  649. * Mailing content files have uploader_id == mailing pseudo_id, a normal recipient,
  650. * and are visible initially to recipient and pseudo_id.
  651. *
  652. * @author René Haentjens, Ghent University
  653. *
  654. * @todo check if this function is still necessary.
  655. */
  656. function getUserOwningThisMailing($mailingPseudoId, $owner = 0, $or_die = '')
  657. {
  658. $course_id = api_get_course_int_id();
  659. $mailingPseudoId = intval($mailingPseudoId);
  660. $sql = "SELECT f.uploader_id
  661. FROM " . Database::get_course_table(TABLE_DROPBOX_FILE) . " f
  662. LEFT JOIN " . Database::get_course_table(TABLE_DROPBOX_POST) . " p
  663. ON (f.id = p.file_id AND f.c_id = $course_id AND p.c_id = $course_id)
  664. WHERE
  665. p.dest_user_id = '" . $mailingPseudoId . "' AND
  666. p.c_id = $course_id
  667. ";
  668. $result = Database::query($sql);
  669. if (!($res = Database::fetch_array($result)))
  670. die(get_lang('GeneralError').' (code 901)');
  671. if ($owner == 0) return $res['uploader_id'];
  672. if ($res['uploader_id'] == $owner) return true;
  673. die(get_lang('GeneralError').' (code '.$or_die.')');
  674. }
  675. /**
  676. * @author René Haentjens, Ghent University
  677. * @todo check if this function is still necessary.
  678. */
  679. function removeMoreIfMailing($file_id)
  680. {
  681. $course_id = api_get_course_int_id();
  682. // when deleting a mailing zip-file (posted to mailingPseudoId):
  683. // 1. the detail window is no longer reachable, so
  684. // for all content files, delete mailingPseudoId from person-table
  685. // 2. finding the owner (getUserOwningThisMailing) is no longer possible, so
  686. // for all content files, replace mailingPseudoId by owner as uploader
  687. $file_id = intval($file_id);
  688. $sql = "SELECT p.dest_user_id
  689. FROM " . Database::get_course_table(TABLE_DROPBOX_POST) . " p
  690. WHERE c_id = $course_id AND p.file_id = '" . $file_id . "'";
  691. $result = Database::query($sql);
  692. if ($res = Database::fetch_array($result)) {
  693. $mailingPseudoId = $res['dest_user_id'];
  694. $mailId = get_mail_id_base();
  695. if ($mailingPseudoId > $mailId) {
  696. $sql = "DELETE FROM " . Database::get_course_table(TABLE_DROPBOX_PERSON) . "
  697. WHERE c_id = $course_id AND user_id='" . $mailingPseudoId . "'";
  698. Database::query($sql);
  699. $sql = "UPDATE " . Database::get_course_table(TABLE_DROPBOX_FILE) ."
  700. SET uploader_id='" . api_get_user_id() . "'
  701. WHERE c_id = $course_id AND uploader_id='" . $mailingPseudoId . "'";
  702. Database::query($sql);
  703. }
  704. }
  705. }
  706. /**
  707. * @param array $file
  708. *
  709. * @return array|null|string
  710. */
  711. function store_add_dropbox($file = [])
  712. {
  713. $_course = api_get_course_info();
  714. $_user = api_get_user_info();
  715. $dropbox_cnf = getDropboxConf();
  716. if (empty($file)) {
  717. $file = isset($_FILES['file']) ? $_FILES['file'] : null;
  718. }
  719. // Validating the form data
  720. // there are no recipients selected
  721. if (!isset($_POST['recipients']) || count($_POST['recipients']) <= 0) {
  722. return get_lang('YouMustSelectAtLeastOneDestinee');
  723. } else {
  724. // Check if all the recipients are valid
  725. $thisIsAMailing = false;
  726. $thisIsJustUpload = false;
  727. foreach ($_POST['recipients'] as $rec) {
  728. if ($rec == 'mailing') {
  729. $thisIsAMailing = true;
  730. } elseif ($rec == 'upload') {
  731. $thisIsJustUpload = true;
  732. } elseif (strpos($rec, 'user_') === 0 && !isCourseMember(substr($rec, strlen('user_')))) {
  733. Display::addFlash(Display::return_message(get_lang('InvalideUserDetected'), 'warning'));
  734. return false;
  735. } elseif (strpos($rec, 'group_') !== 0 && strpos($rec, 'user_') !== 0) {
  736. Display::addFlash(Display::return_message(get_lang('InvalideGroupDetected'), 'warning'));
  737. return false;
  738. }
  739. }
  740. }
  741. // we are doing a mailing but an additional recipient is selected
  742. if ($thisIsAMailing && (count($_POST['recipients']) != 1)) {
  743. Display::addFlash(Display::return_message(get_lang('MailingSelectNoOther'), 'warning'));
  744. return false;
  745. }
  746. // we are doing a just upload but an additional recipient is selected.
  747. // note: why can't this be valid? It is like sending a document to
  748. // yourself AND to a different person (I do this quite often with my e-mails)
  749. if ($thisIsJustUpload && (count($_POST['recipients']) != 1)) {
  750. Display::addFlash(Display::return_message(get_lang('MailingJustUploadSelectNoOther'), 'warning'));
  751. return false;
  752. }
  753. if (empty($file['name'])) {
  754. Display::addFlash(Display::return_message(get_lang('NoFileSpecified'), 'warning'));
  755. return false;
  756. }
  757. // are we overwriting a previous file or sending a new one
  758. $dropbox_overwrite = false;
  759. if (isset($_POST['cb_overwrite']) && $_POST['cb_overwrite']) {
  760. $dropbox_overwrite = true;
  761. }
  762. // doing the upload
  763. $dropbox_filename = $file['name'];
  764. $dropbox_filesize = $file['size'];
  765. $dropbox_filetype = $file['type'];
  766. $dropbox_filetmpname = $file['tmp_name'];
  767. // check if the filesize does not exceed the allowed size.
  768. $maxFileSize = api_get_setting('dropbox_max_filesize');
  769. if ($dropbox_filesize <= 0 || $dropbox_filesize > $maxFileSize) {
  770. Display::addFlash(Display::return_message(get_lang('DropboxFileTooBig'), 'warning'));
  771. return false;
  772. }
  773. // check if the file is actually uploaded
  774. if (!is_uploaded_file($dropbox_filetmpname)) { // check user fraud : no clean error msg.
  775. Display::addFlash(Display::return_message(get_lang('TheFileIsNotUploaded'), 'warning'));
  776. return false;
  777. }
  778. $upload_ok = process_uploaded_file($file, true);
  779. if (!$upload_ok) {
  780. return null;
  781. }
  782. // Try to add an extension to the file if it hasn't got one
  783. $dropbox_filename = add_ext_on_mime($dropbox_filename, $dropbox_filetype);
  784. // Replace dangerous characters
  785. $dropbox_filename = api_replace_dangerous_char($dropbox_filename);
  786. // Transform any .php file in .phps fo security
  787. $dropbox_filename = php2phps($dropbox_filename);
  788. //filter extension
  789. if (!filter_extension($dropbox_filename)) {
  790. Display::addFlash(Display::return_message(get_lang('UplUnableToSaveFileFilteredExtension'), 'warning'));
  791. return false;
  792. }
  793. // set title
  794. $dropbox_title = $dropbox_filename;
  795. // set author
  796. // note: I think we could better migrate everything from here on to
  797. // separate functions: store_new_dropbox, store_new_mailing, store_just_upload
  798. if ($dropbox_overwrite) {
  799. $dropbox_person = new Dropbox_Person(
  800. $_user['user_id'],
  801. api_is_course_admin(),
  802. api_is_course_tutor()
  803. );
  804. $mailId = get_mail_id_base();
  805. foreach ($dropbox_person->sentWork as $w) {
  806. if ($w->title == $dropbox_filename) {
  807. if (($w->recipients[0]['id'] > $mailId) xor $thisIsAMailing) {
  808. Display::addFlash(Display::return_message(get_lang('MailingNonMailingError'), 'warning'));
  809. return false;
  810. }
  811. if (($w->recipients[0]['id'] == $_user['user_id']) xor $thisIsJustUpload) {
  812. Display::addFlash(Display::return_message(get_lang('MailingJustUploadSelectNoOther'), 'warning'));
  813. return false;
  814. }
  815. $dropbox_filename = $w->filename;
  816. $found = true; // note: do we still need this?
  817. break;
  818. }
  819. }
  820. } else { // rename file to login_filename_uniqueId format
  821. $dropbox_filename = $_user['username'] . "_" . $dropbox_filename . "_".uniqid('');
  822. }
  823. // creating the array that contains all the users who will receive the file
  824. $new_work_recipients = array();
  825. foreach ($_POST['recipients'] as $rec) {
  826. if (strpos($rec, 'user_') === 0) {
  827. $new_work_recipients[] = substr($rec, strlen('user_'));
  828. } elseif (strpos($rec, 'group_') === 0) {
  829. $groupInfo = GroupManager::get_group_properties(substr($rec, strlen('group_')));
  830. $userList = GroupManager::get_subscribed_users($groupInfo['iid']);
  831. foreach ($userList as $usr) {
  832. if (!in_array($usr['user_id'], $new_work_recipients) && $usr['user_id'] != $_user['user_id']) {
  833. $new_work_recipients[] = $usr['user_id'];
  834. }
  835. }
  836. }
  837. }
  838. @move_uploaded_file($dropbox_filetmpname, api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/dropbox/' . $dropbox_filename);
  839. $b_send_mail = api_get_course_setting('email_alert_on_new_doc_dropbox');
  840. if ($b_send_mail) {
  841. foreach ($new_work_recipients as $recipient_id) {
  842. $recipent_temp = api_get_user_info($recipient_id);
  843. $additionalParameters = array(
  844. 'smsType' => SmsPlugin::NEW_FILE_SHARED_COURSE_BY,
  845. 'userId' => $recipient_id,
  846. 'courseTitle' => $_course['title'],
  847. 'userUsername' => $recipent_temp['username']
  848. );
  849. api_mail_html(
  850. api_get_person_name(
  851. $recipent_temp['firstname'].' '.$recipent_temp['lastname'],
  852. null,
  853. PERSON_NAME_EMAIL_ADDRESS
  854. ),
  855. $recipent_temp['email'],
  856. get_lang('NewDropboxFileUploaded'),
  857. get_lang('NewDropboxFileUploadedContent').' <a href="'.api_get_path(WEB_CODE_PATH).'dropbox/index.php?' . api_get_cidreq() . '">'.get_lang('SeeFile').'</a>'.
  858. "\n\n".
  859. api_get_person_name(
  860. $_user['firstName'],
  861. $_user['lastName'],
  862. null,
  863. PERSON_NAME_EMAIL_ADDRESS
  864. )."\n". get_lang('Email') ." : ".$_user['mail'],
  865. api_get_person_name(
  866. $_user['firstName'],
  867. $_user['lastName'],
  868. null,
  869. PERSON_NAME_EMAIL_ADDRESS
  870. ),
  871. $_user['mail'],
  872. null,
  873. null,
  874. null,
  875. $additionalParameters
  876. );
  877. }
  878. }
  879. $result = new Dropbox_SentWork(
  880. $_user['user_id'],
  881. $dropbox_title,
  882. isset($_POST['description']) ? $_POST['description'] : '',
  883. api_get_user_id(),
  884. $dropbox_filename,
  885. $dropbox_filesize,
  886. $new_work_recipients
  887. );
  888. Security::clear_token();
  889. Display::addFlash(Display::return_message(get_lang('FileUploadSucces')));
  890. return $result;
  891. }
  892. /**
  893. * Transforms the array containing all the feedback into something visually attractive.
  894. *
  895. * @param an array containing all the feedback about the given message.
  896. *
  897. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  898. * @version march 2006
  899. */
  900. function feedback($array) {
  901. $output = null;
  902. foreach ($array as $value) {
  903. $output .= format_feedback($value);
  904. }
  905. $output .= feedback_form();
  906. return $output;
  907. }
  908. /**
  909. * This function returns the html code to display the feedback messages on a given dropbox file
  910. * @param $feedback_array an array that contains all the feedback messages about the given document.
  911. * @return string code
  912. * @todo add the form for adding new comment (if the other party has not deleted it yet).
  913. *
  914. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  915. * @version march 2006
  916. */
  917. function format_feedback($feedback)
  918. {
  919. $userInfo = api_get_user_info($feedback['author_user_id']);
  920. $output = UserManager::getUserProfileLink($userInfo);
  921. $output .= '&nbsp;&nbsp;'.api_convert_and_format_date($feedback['feedback_date'], DATE_TIME_FORMAT_LONG).'<br />';
  922. $output .= '<div style="padding-top:6px">'.nl2br($feedback['feedback']).'</div><hr size="1" noshade/><br />';
  923. return $output;
  924. }
  925. /**
  926. * this function returns the code for the form for adding a new feedback message to a dropbox file.
  927. * @return string code
  928. *
  929. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  930. * @version march 2006
  931. */
  932. function feedback_form()
  933. {
  934. $return = get_lang('AddNewFeedback').'<br />';
  935. $number_users_who_see_file = check_if_file_exist($_GET['id']);
  936. if ($number_users_who_see_file) {
  937. $token = Security::get_token();
  938. $return .= '<textarea name="feedback" style="width: 80%; height: 80px;"></textarea>';
  939. $return .= '<input type="hidden" name="sec_token" value="'.$token.'"/>';
  940. $return .= '<br /><button type="submit" class="add" name="store_feedback" value="'.get_lang('Ok').'"
  941. onclick="javascript: document.form_dropbox.attributes.action.value = document.location;">'.get_lang('AddComment').'</button>';
  942. } else {
  943. $return .= get_lang('AllUsersHaveDeletedTheFileAndWillNotSeeFeedback');
  944. }
  945. return $return;
  946. }
  947. function user_can_download_file($id, $user_id)
  948. {
  949. $course_id = api_get_course_int_id();
  950. $id = intval($id);
  951. $user_id = intval($user_id);
  952. $sql = "SELECT file_id FROM ". Database::get_course_table(TABLE_DROPBOX_PERSON) ."
  953. WHERE c_id = $course_id AND user_id = $user_id AND file_id = ".$id;
  954. $result = Database::query($sql);
  955. $number_users_who_see_file = Database::num_rows($result);
  956. $sql = "SELECT file_id FROM ". Database::get_course_table(TABLE_DROPBOX_POST) ."
  957. WHERE c_id = $course_id AND dest_user_id = $user_id AND file_id = ".$id;
  958. $result = Database::query($sql);
  959. $count = Database::num_rows($result);
  960. return $number_users_who_see_file > 0 || $count > 0;
  961. }
  962. // we now check if the other users have not delete this document yet.
  963. // If this is the case then it is useless to see the
  964. // add feedback since the other users will never get to see the feedback.
  965. function check_if_file_exist($id)
  966. {
  967. $id = intval($id);
  968. $course_id = api_get_course_int_id();
  969. $sql = "SELECT file_id FROM ". Database::get_course_table(TABLE_DROPBOX_PERSON) ."
  970. WHERE c_id = $course_id AND file_id = ".$id;
  971. $result = Database::query($sql);
  972. $number_users_who_see_file = Database::num_rows($result);
  973. $sql = "SELECT file_id FROM ". Database::get_course_table(TABLE_DROPBOX_POST) ."
  974. WHERE c_id = $course_id AND file_id = ".$id;
  975. $result = Database::query($sql);
  976. $count = Database::num_rows($result);
  977. return $number_users_who_see_file > 0 || $count > 0;
  978. }
  979. /**
  980. * @return string language string (depending on the success or failure.
  981. *
  982. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  983. * @version march 2006
  984. */
  985. function store_feedback()
  986. {
  987. if (!is_numeric($_GET['id'])) {
  988. return get_lang('FeedbackError');
  989. }
  990. $course_id = api_get_course_int_id();
  991. if (empty($_POST['feedback'])) {
  992. return get_lang('PleaseTypeText');
  993. } else {
  994. $params = [
  995. 'c_id' => $course_id,
  996. 'file_id' => $_GET['id'],
  997. 'author_user_id' => api_get_user_id(),
  998. 'feedback' => $_POST['feedback'],
  999. 'feedback_date' => api_get_utc_datetime(),
  1000. ];
  1001. $id = Database::insert(Database::get_course_table(TABLE_DROPBOX_FEEDBACK), $params);
  1002. if ($id) {
  1003. $sql = "UPDATE ". Database::get_course_table(TABLE_DROPBOX_FEEDBACK) ." SET feedback_id = iid WHERE iid = $id";
  1004. Database::query($sql);
  1005. }
  1006. return get_lang('DropboxFeedbackStored');
  1007. }
  1008. }
  1009. /**
  1010. * This function downloads all the files of the input array into one zip
  1011. * @param array $fileList containing all the ids of the files that have to be downloaded.
  1012. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1013. * @todo consider removing the check if the user has received or sent this file (zip download of a folder already sufficiently checks for this).
  1014. * @todo integrate some cleanup function that removes zip files that are older than 2 days
  1015. *
  1016. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1017. * @author Julio Montoya Addin c_id support
  1018. * @version march 2006
  1019. */
  1020. function zip_download($fileList)
  1021. {
  1022. $_course = api_get_course_info();
  1023. $course_id = api_get_course_int_id();
  1024. $fileList = array_map('intval', $fileList);
  1025. // note: we also have to add the check if the user has received or sent this file.
  1026. $sql = "SELECT DISTINCT file.filename, file.title, file.author, file.description
  1027. FROM ". Database::get_course_table(TABLE_DROPBOX_FILE) ." file
  1028. INNER JOIN ". Database::get_course_table(TABLE_DROPBOX_PERSON) ." person
  1029. ON (person.file_id=file.id AND file.c_id = $course_id AND person.c_id = $course_id)
  1030. INNER JOIN ". Database::get_course_table(TABLE_DROPBOX_POST) ." post
  1031. ON (post.file_id = file.id AND post.c_id = $course_id AND file.c_id = $course_id)
  1032. WHERE
  1033. file.id IN (".implode(', ', $fileList).") AND
  1034. file.id = person.file_id AND
  1035. (
  1036. person.user_id = '".api_get_user_id()."' OR
  1037. post.dest_user_id = '".api_get_user_id()."'
  1038. ) ";
  1039. $result = Database::query($sql);
  1040. $files = array();
  1041. while ($row = Database::fetch_array($result)) {
  1042. $files[$row['filename']] = array(
  1043. 'filename' => $row['filename'],
  1044. 'title' => $row['title'],
  1045. 'author' => $row['author'],
  1046. 'description' => $row['description']
  1047. );
  1048. }
  1049. // Step 3: create the zip file and add all the files to it
  1050. $temp_zip_file = api_get_path(SYS_ARCHIVE_PATH).api_get_unique_id().".zip";
  1051. Session::write('dropbox_files_to_download', $files);
  1052. $zip = new PclZip($temp_zip_file);
  1053. foreach ($files as $value) {
  1054. $zip->add(
  1055. api_get_path(SYS_COURSE_PATH).$_course['path'].'/dropbox/'.$value['filename'],
  1056. PCLZIP_OPT_REMOVE_ALL_PATH,
  1057. PCLZIP_CB_PRE_ADD,
  1058. 'my_pre_add_callback'
  1059. );
  1060. }
  1061. Session::erase('dropbox_files_to_download');
  1062. $name = 'dropbox-'.api_get_utc_datetime().'.zip';
  1063. $result = DocumentManager::file_send_for_download($temp_zip_file, true, $name);
  1064. if ($result === false) {
  1065. api_not_allowed(true);
  1066. }
  1067. @unlink($temp_zip_file);
  1068. exit;
  1069. }
  1070. /**
  1071. * This is a callback function to decrypt the files in the zip file to their normal filename (as stored in the database)
  1072. * @param array $p_event a variable of PCLZip
  1073. * @param array $p_header a variable of PCLZip
  1074. *
  1075. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1076. * @version march 2006
  1077. */
  1078. function my_pre_add_callback($p_event, &$p_header)
  1079. {
  1080. $files = Session::read('dropbox_files_to_download');
  1081. $p_header['stored_filename'] = $files[$p_header['stored_filename']]['title'];
  1082. return 1;
  1083. }
  1084. /**
  1085. * @desc Generates the contents of a html file that gives an overview of all the files in the zip file.
  1086. * This is to know the information of the files that are inside the zip file (who send it, the comment, ...)
  1087. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, March 2006
  1088. * @author Ivan Tcholakov, 2010, code for html metadata has been added.
  1089. */
  1090. function generate_html_overview($files, $dont_show_columns = array(), $make_link = array())
  1091. {
  1092. $return = '<!DOCTYPE html'."\n";
  1093. $return .= "\t".'PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'."\n";
  1094. $return .= "\t".'"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'."\n";
  1095. $return .= '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="'.api_get_language_isocode().'" lang="'.api_get_language_isocode().'">'."\n";
  1096. $return .= "<head>\n\t<title>".get_lang('OverviewOfFilesInThisZip')."</title>\n";
  1097. $return .= "\t".'<meta http-equiv="Content-Type" content="text/html; charset='.api_get_system_encoding().'" />'."\n";
  1098. $return .= "</head>\n\n";
  1099. $return .= '<body dir="'.api_get_text_direction().'">'."\n\n";
  1100. $return .= "<table border=\"1px\">\n";
  1101. $counter = 0;
  1102. foreach ($files as $value) {
  1103. // Adding the header.
  1104. if ($counter == 0) {
  1105. $columns_array = array_keys($value);
  1106. $return .= "\n<tr>";
  1107. foreach ($columns_array as $columns_array_key => $columns_array_value) {
  1108. if (!in_array($columns_array_value, $dont_show_columns)) {
  1109. $return .= "\n\t<th>".$columns_array_value."</th>";
  1110. }
  1111. $column[] = $columns_array_value;
  1112. }
  1113. $return .= "\n</tr>\n";
  1114. }
  1115. $counter++;
  1116. // Adding the content.
  1117. $return .= "\n<tr>";
  1118. foreach ($column as $column_key => $column_value) {
  1119. if (!in_array($column_value,$dont_show_columns)) {
  1120. $return .= "\n\t<td>";
  1121. if (in_array($column_value, $make_link)) {
  1122. $return .= '<a href="'.$value[$column_value].'">'.$value[$column_value].'</a>';
  1123. } else {
  1124. $return .= $value[$column_value];
  1125. }
  1126. $return .= "</td>";
  1127. }
  1128. }
  1129. $return .= "\n</tr>\n";
  1130. }
  1131. $return .= "\n</table>\n\n</body>";
  1132. $return .= "\n</html>";
  1133. return $return;
  1134. }
  1135. /**
  1136. * @desc This function retrieves the number of feedback messages on every document. This function might become obsolete when
  1137. * the feedback becomes user individual.
  1138. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1139. * @version march 2006
  1140. */
  1141. function get_total_number_feedback($file_id = '')
  1142. {
  1143. $course_id = api_get_course_int_id();
  1144. $sql = "SELECT COUNT(feedback_id) AS total, file_id
  1145. FROM ". Database::get_course_table(TABLE_DROPBOX_FEEDBACK) ."
  1146. WHERE c_id = $course_id GROUP BY file_id";
  1147. $result = Database::query($sql);
  1148. $return = array();
  1149. while ($row=Database::fetch_array($result)) {
  1150. $return[$row['file_id']] = $row['total'];
  1151. }
  1152. return $return;
  1153. }
  1154. /**
  1155. * @desc this function checks if the key exists. If this is the case it returns the value, if not it returns 0
  1156. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1157. * @version march 2006
  1158. */
  1159. function check_number_feedback($key, $array)
  1160. {
  1161. if (is_array($array)) {
  1162. if (array_key_exists($key, $array)) {
  1163. return $array[$key];
  1164. } else {
  1165. return 0;
  1166. }
  1167. } else {
  1168. return 0;
  1169. }
  1170. }
  1171. /**
  1172. * Get the last access to a given tool of a given user
  1173. * @param $tool string the tool constant
  1174. * @param $courseId the course_id
  1175. * @param $user_id the id of the user
  1176. * @return string last tool access date
  1177. *
  1178. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University
  1179. * @version march 2006
  1180. *
  1181. * @todo consider moving this function to a more appropriate place.
  1182. */
  1183. function get_last_tool_access($tool, $courseId = null, $user_id = null)
  1184. {
  1185. // The default values of the parameters
  1186. if (empty($courseId)) {
  1187. $courseId = api_get_course_int_id();
  1188. }
  1189. if (empty($user_id)) {
  1190. $user_id = api_get_user_id();
  1191. }
  1192. // the table where the last tool access is stored (=track_e_lastaccess)
  1193. $table_last_access = Database::get_main_table('track_e_lastaccess');
  1194. $sql = "SELECT access_date FROM $table_last_access
  1195. WHERE
  1196. access_user_id = ".intval($user_id)." AND
  1197. c_id='".intval($courseId)."' AND
  1198. access_tool='".Database::escape_string($tool)."'
  1199. ORDER BY access_date DESC
  1200. LIMIT 1";
  1201. $result = Database::query($sql);
  1202. $row = Database::fetch_array($result);
  1203. return $row['access_date'];
  1204. }
  1205. /**
  1206. * Previously $dropbox_cnf['mailingIdBase'], returns a mailing ID to generate a mail ID
  1207. * @return int
  1208. */
  1209. function get_mail_id_base()
  1210. {
  1211. // false = no mailing functionality
  1212. //$dropbox_cnf['mailingIdBase'] = 10000000; // bigger than any user_id,
  1213. // allowing enough space for pseudo_ids as uploader_id, dest_user_id, user_id:
  1214. // mailing pseudo_id = dropbox_cnf('mailingIdBase') + mailing id
  1215. return 10000000;
  1216. }