dropbox_functions.inc.php 49 KB

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