dropbox_functions.inc.php 49 KB

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