dropbox_functions.inc.php 51 KB

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