dropbox_functions.inc.php 43 KB

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