dropbox_functions.inc.php 51 KB

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