dropbox_class.inc.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Dropbox module for Chamilo
  5. * Classes for the dropbox module.
  6. *
  7. * 3 classes have been defined:
  8. * - Dropbox_Work:
  9. * . id
  10. * . uploader_id => who sent it
  11. * . uploaderName
  12. * . filename => name of file stored on the server
  13. * . filesize
  14. * . title => name of file returned to user. This is the original name of the file
  15. * except when the original name contained spaces. In that case the spaces
  16. * will be replaced by _
  17. * . description
  18. * . author
  19. * . upload_date => date when file was first sent
  20. * . last_upload_date => date when file was last sent
  21. * . isOldWork => has the work already been uploaded before
  22. *
  23. * . feedback_date => date of most recent feedback
  24. * . feedback => feedback text (or HTML?)
  25. *
  26. * - Dropbox_SentWork extends Dropbox_Work
  27. * . recipients => array of ["id"]["name"] lists the recipients of the work
  28. *
  29. * - Dropbox_Person:
  30. * . userId
  31. * . receivedWork => array of Dropbox_Work objects
  32. * . sentWork => array of Dropbox_SentWork objects
  33. * . isCourseTutor
  34. * . isCourseAdmin
  35. * . _orderBy => private property used for determining the field by which the works have to be ordered
  36. *
  37. * @version 1.30
  38. * @copyright 2004
  39. * @author Jan Bols <jan@ivpv.UGent.be>
  40. * with contributions by René Haentjens <rene.haentjens@UGent.be>
  41. * @package chamilo.dropbox
  42. */
  43. class Dropbox_Work
  44. {
  45. public $id;
  46. public $uploader_id;
  47. public $uploaderName;
  48. public $filename;
  49. public $filesize;
  50. public $title;
  51. public $description;
  52. public $author;
  53. public $upload_date;
  54. public $last_upload_date;
  55. public $isOldWork;
  56. public $feedback_date, $feedback;
  57. /**
  58. * Constructor calls private functions to create a new work or retreive an existing work from DB
  59. * depending on the number of parameters
  60. *
  61. * @param unknown_type $arg1
  62. * @param unknown_type $arg2
  63. * @param unknown_type $arg3
  64. * @param unknown_type $arg4
  65. * @param unknown_type $arg5
  66. * @param unknown_type $arg6
  67. * @return Dropbox_Work
  68. */
  69. public function Dropbox_Work($arg1, $arg2 = null, $arg3 = null, $arg4 = null, $arg5 = null, $arg6 = null)
  70. {
  71. if (func_num_args() > 1) {
  72. $this->_createNewWork($arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
  73. } else {
  74. $this->_createExistingWork($arg1);
  75. }
  76. }
  77. /**
  78. * private function creating a new work object
  79. *
  80. * @param int $uploader_id
  81. * @param string $title
  82. * @param string $description
  83. * @param string $author
  84. * @param string $filename
  85. * @param int $filesize
  86. *
  87. * @todo $author was originally a field but this has now been replaced by the first and lastname of the uploader (to prevent anonymous uploads)
  88. * As a consequence this parameter can be removed
  89. */
  90. public function _createNewWork($uploader_id, $title, $description, $author, $filename, $filesize)
  91. {
  92. $dropbox_cnf = getDropboxConf();
  93. // Fill in the properties
  94. $this->uploader_id = intval($uploader_id);
  95. $this->uploaderName = getUserNameFromId($this->uploader_id);
  96. $this->filename = $filename;
  97. $this->filesize = $filesize;
  98. $this->title = $title;
  99. $this->description = $description;
  100. $this->author = $author;
  101. $this->last_upload_date = api_get_utc_datetime();
  102. $course_id = api_get_course_int_id();
  103. // Check if object exists already. If it does, the old object is used
  104. // with updated information (authors, description, upload_date)
  105. $this->isOldWork = false;
  106. $sql = "SELECT id, upload_date FROM ".$dropbox_cnf['tbl_file']."
  107. WHERE c_id = $course_id AND filename = '".Database::escape_string($this->filename)."'";
  108. $result = Database::query($sql);
  109. $res = Database::fetch_array($result);
  110. if ($res) {
  111. $this->isOldWork = true;
  112. }
  113. // Insert or update the dropbox_file table and set the id property
  114. if ($this->isOldWork) {
  115. $this->id = $res['id'];
  116. $this->upload_date = $res['upload_date'];
  117. $params = [
  118. 'filesize' => $this->filesize,
  119. 'title' => $this->title,
  120. 'description' => $this->description,
  121. 'author' => $this->author,
  122. 'last_upload_date' => $this->last_upload_date,
  123. 'session_id' => api_get_session_id(),
  124. ];
  125. Database::update(
  126. $dropbox_cnf['tbl_file'],
  127. $params,
  128. ['c_id = ? AND id = ?' => [$course_id, $this->id]]
  129. );
  130. } else {
  131. $this->upload_date = $this->last_upload_date;
  132. $params = [
  133. 'c_id' => $course_id,
  134. 'uploader_id' => $this->uploader_id,
  135. 'filename' => $this->filename,
  136. 'filesize' => $this->filesize,
  137. 'title' => $this->title,
  138. 'description' => $this->description,
  139. 'author' => $this->author,
  140. 'upload_date' => $this->upload_date,
  141. 'last_upload_date' => $this->last_upload_date,
  142. 'session_id' => api_get_session_id(),
  143. ];
  144. $this->id = Database::insert($dropbox_cnf['tbl_file'], $params);
  145. if ($this->id) {
  146. $sql = "UPDATE ".$dropbox_cnf['tbl_file']." SET id = iid WHERE iid = {$this->id}";
  147. Database::query($sql);
  148. }
  149. }
  150. $sql = "SELECT count(file_id) as count
  151. FROM ".$dropbox_cnf['tbl_person']."
  152. WHERE c_id = $course_id AND file_id = ".intval($this->id)." AND user_id = ".$this->uploader_id;
  153. $result = Database::query($sql);
  154. $row = Database::fetch_array($result);
  155. if ($row['count'] == 0) {
  156. // Insert entries into person table
  157. $sql = "INSERT INTO ".$dropbox_cnf['tbl_person']." (c_id, file_id, user_id)
  158. VALUES ($course_id, ".intval($this->id)." , ".intval($this->uploader_id).")";
  159. Database::query($sql);
  160. }
  161. }
  162. /**
  163. * private function creating existing object by retreiving info from db
  164. *
  165. * @param int $id
  166. */
  167. public function _createExistingWork($id)
  168. {
  169. $course_id = api_get_course_int_id();
  170. $dropbox_cnf = getDropboxConf();
  171. $action = isset($_GET['action']) ? $_GET['action'] : null;
  172. // Do some sanity checks
  173. $id = intval($id);
  174. // Get the data from DB
  175. $sql = "SELECT uploader_id, filename, filesize, title, description, author, upload_date, last_upload_date, cat_id
  176. FROM ".$dropbox_cnf['tbl_file']."
  177. WHERE c_id = $course_id AND id = ".$id."";
  178. $result = Database::query($sql);
  179. $res = Database::fetch_array($result, 'ASSOC');
  180. // Check if uploader is still in Chamilo system
  181. $uploader_id = stripslashes($res['uploader_id']);
  182. $uploaderName = getUserNameFromId($uploader_id);
  183. if (!$uploaderName) {
  184. //deleted user
  185. $this->uploader_id = -1;
  186. $this->uploaderName = get_lang('Unknown', '');
  187. } else {
  188. $this->uploader_id = $uploader_id;
  189. $this->uploaderName = $uploaderName;
  190. }
  191. // Fill in properties
  192. $this->id = $id;
  193. $this->filename = stripslashes($res['filename']);
  194. $this->filesize = stripslashes($res['filesize']);
  195. $this->title = stripslashes($res['title']);
  196. $this->description = stripslashes($res['description']);
  197. $this->author = stripslashes($res['author']);
  198. $this->upload_date = stripslashes($res['upload_date']);
  199. $this->last_upload_date = stripslashes($res['last_upload_date']);
  200. $this->category = $res['cat_id'];
  201. // Getting the feedback on the work.
  202. if ($action == 'viewfeedback' AND $this->id == $_GET['id']) {
  203. $feedback2 = array();
  204. $sql_feedback = "SELECT * FROM ".$dropbox_cnf['tbl_feedback']."
  205. WHERE c_id = $course_id AND file_id='".$id."' ORDER BY feedback_id ASC";
  206. $result = Database::query($sql_feedback);
  207. while ($row_feedback = Database::fetch_array($result)) {
  208. $row_feedback['feedback'] = Security::remove_XSS($row_feedback['feedback']);
  209. $feedback2[] = $row_feedback;
  210. }
  211. $this->feedback2= $feedback2;
  212. }
  213. }
  214. }
  215. class Dropbox_SentWork extends Dropbox_Work
  216. {
  217. public $recipients; //array of ['id']['name'] arrays
  218. /**
  219. * Constructor calls private functions to create a new work or retreive an existing work from DB
  220. * depending on the number of parameters
  221. *
  222. * @param unknown_type $arg1
  223. * @param unknown_type $arg2
  224. * @param unknown_type $arg3
  225. * @param unknown_type $arg4
  226. * @param unknown_type $arg5
  227. * @param unknown_type $arg6
  228. * @param unknown_type $arg7
  229. * @return Dropbox_SentWork
  230. */
  231. function Dropbox_SentWork($arg1, $arg2 = null, $arg3 = null, $arg4 = null, $arg5 = null, $arg6 = null, $arg7 = null)
  232. {
  233. if (func_num_args() > 1) {
  234. $this->_createNewSentWork($arg1, $arg2, $arg3, $arg4, $arg5, $arg6, $arg7);
  235. } else {
  236. $this->_createExistingSentWork($arg1);
  237. }
  238. }
  239. /**
  240. * private function creating a new SentWork object
  241. *
  242. * @param int $uploader_id
  243. * @param string $title
  244. * @param string $description
  245. * @param string $author
  246. * @param string $filename
  247. * @param int $filesize
  248. * @param array $recipient_ids
  249. */
  250. public function _createNewSentWork($uploader_id, $title, $description, $author, $filename, $filesize, $recipient_ids)
  251. {
  252. $dropbox_cnf = getDropboxConf();
  253. $_course = api_get_course_info();
  254. // Call constructor of Dropbox_Work object
  255. $this->Dropbox_Work(
  256. $uploader_id,
  257. $title,
  258. $description,
  259. $author,
  260. $filename,
  261. $filesize
  262. );
  263. $course_id = api_get_course_int_id();
  264. // Do sanity checks on recipient_ids array & property fillin
  265. // The sanity check for ex-coursemembers is already done in base constructor
  266. settype($uploader_id, 'integer') or die(get_lang('GeneralError').' (code 208)'); // Set $uploader_id to correct type
  267. $justSubmit = false;
  268. if ( is_int($recipient_ids)) {
  269. $justSubmit = true;
  270. $recipient_ids = array($recipient_ids + $this->id);
  271. } elseif ( count($recipient_ids) == 0) {
  272. $justSubmit = true;
  273. $recipient_ids = array($uploader_id);
  274. }
  275. if (! is_array($recipient_ids) || count($recipient_ids) == 0) {
  276. die(get_lang('GeneralError').' (code 209)');
  277. }
  278. foreach ($recipient_ids as $rec) {
  279. if (empty($rec)) die(get_lang('GeneralError').' (code 210)');
  280. //if (!isCourseMember($rec)) die(); //cannot sent document to someone outside of course
  281. //this check is done when validating submitted data
  282. $this->recipients[] = array('id' => $rec, 'name' => getUserNameFromId($rec));
  283. }
  284. $table_post = $dropbox_cnf['tbl_post'];
  285. $table_person = $dropbox_cnf['tbl_person'];
  286. $session_id = api_get_session_id();
  287. $uploader_id = $this->uploader_id;
  288. $user = api_get_user_id();
  289. // Insert data in dropbox_post and dropbox_person table for each recipient
  290. foreach ($this->recipients as $rec) {
  291. $file_id = (int)$this->id;
  292. $user_id = (int)$rec['id'];
  293. $sql = "INSERT INTO $table_post (c_id, file_id, dest_user_id, session_id)
  294. VALUES ($course_id, $file_id, $user_id, $session_id)";
  295. Database::query($sql);
  296. // If work already exists no error is generated
  297. /**
  298. * Poster is already added when work is created - not so good to split logic
  299. */
  300. if ($user_id != $user) {
  301. // Insert entries into person table
  302. $sql = "INSERT INTO $table_person (c_id, file_id, user_id)
  303. VALUES ($course_id, $file_id, $user_id)";
  304. // Do not add recipient in person table if mailing zip or just upload.
  305. if (!$justSubmit) {
  306. Database::query($sql); // If work already exists no error is generated
  307. }
  308. }
  309. // Update item_property table for each recipient
  310. if (($ownerid = $this->uploader_id) > $dropbox_cnf['mailingIdBase']) {
  311. $ownerid = getUserOwningThisMailing($ownerid);
  312. }
  313. if (($recipid = $rec["id"]) > $dropbox_cnf['mailingIdBase']) {
  314. $recipid = $ownerid; // mailing file recipient = mailing id, not a person
  315. }
  316. api_item_property_update(
  317. $_course,
  318. TOOL_DROPBOX,
  319. $this->id,
  320. 'DropboxFileAdded',
  321. $ownerid,
  322. null,
  323. $recipid
  324. );
  325. }
  326. }
  327. /**
  328. * private function creating existing object by retreiving info from db
  329. *
  330. * @param unknown_type $id
  331. */
  332. function _createExistingSentWork($id)
  333. {
  334. $dropbox_cnf = getDropboxConf();
  335. $id = intval($id);
  336. $course_id = api_get_course_int_id();
  337. // Call constructor of Dropbox_Work object
  338. $this->Dropbox_Work($id);
  339. // Fill in recipients array
  340. $this->recipients = array();
  341. $sql = "SELECT dest_user_id, feedback_date, feedback
  342. FROM ".$dropbox_cnf['tbl_post']."
  343. WHERE c_id = $course_id AND file_id = ".intval($id)."";
  344. $result = Database::query($sql);
  345. while ($res = Database::fetch_array($result, 'ASSOC')) {
  346. // Check for deleted users
  347. $dest_user_id = $res['dest_user_id'];
  348. $user_info = api_get_user_info($dest_user_id);
  349. //$this->category = $res['cat_id'];
  350. if (!$user_info) {
  351. $this->recipients[] = array('id' => -1, 'name' => get_lang('Unknown', ''));
  352. } else {
  353. $this->recipients[] = array(
  354. 'id' => $dest_user_id,
  355. 'name' => $user_info['complete_name'],
  356. 'user_id' => $dest_user_id,
  357. 'feedback_date' => $res['feedback_date'],
  358. 'feedback' => $res['feedback']
  359. );
  360. }
  361. }
  362. }
  363. }
  364. class Dropbox_Person
  365. {
  366. // The receivedWork and the sentWork arrays are sorted.
  367. public $receivedWork; // an array of Dropbox_Work objects
  368. public $sentWork; // an array of Dropbox_SentWork objects
  369. public $userId = 0;
  370. public $isCourseAdmin = false;
  371. public $isCourseTutor = false;
  372. public $_orderBy = ''; // private property that determines by which field
  373. /**
  374. * Constructor for recreating the Dropbox_Person object
  375. *
  376. * @param int $userId
  377. * @param bool $isCourseAdmin
  378. * @param bool $isCourseTutor
  379. * @return Dropbox_Person
  380. */
  381. function Dropbox_Person($userId, $isCourseAdmin, $isCourseTutor)
  382. {
  383. $course_id = api_get_course_int_id();
  384. // Fill in properties
  385. $this->userId = $userId;
  386. $this->isCourseAdmin = $isCourseAdmin;
  387. $this->isCourseTutor = $isCourseTutor;
  388. $this->receivedWork = array();
  389. $this->sentWork = array();
  390. // Note: perhaps include an ex coursemember check to delete old files
  391. $session_id = api_get_session_id();
  392. $condition_session = api_get_session_condition($session_id);
  393. $post_tbl = Database::get_course_table(TABLE_DROPBOX_POST);
  394. $person_tbl = Database::get_course_table(TABLE_DROPBOX_PERSON);
  395. $file_tbl = Database::get_course_table(TABLE_DROPBOX_FILE);
  396. // Find all entries where this person is the recipient
  397. $sql = "SELECT DISTINCT r.file_id, r.cat_id
  398. FROM $post_tbl r
  399. INNER JOIN $person_tbl p
  400. ON (r.file_id = p.file_id AND r.c_id = $course_id AND p.c_id = $course_id )
  401. WHERE
  402. p.user_id = ".intval($this->userId)." AND
  403. r.dest_user_id = ".intval($this->userId)." $condition_session ";
  404. $result = Database::query($sql);
  405. while ($res = Database::fetch_array($result)) {
  406. $temp = new Dropbox_Work($res['file_id']);
  407. $temp->category = $res['cat_id'];
  408. $this->receivedWork[] = $temp;
  409. }
  410. // Find all entries where this person is the sender/uploader
  411. $sql = "SELECT DISTINCT f.id
  412. FROM $file_tbl f
  413. INNER JOIN $person_tbl p
  414. ON (f.id = p.file_id AND f.c_id = $course_id AND p.c_id = $course_id)
  415. WHERE
  416. f.uploader_id = ".intval($this->userId)." AND
  417. p.user_id = ".intval($this->userId)."
  418. $condition_session
  419. ";
  420. $result = Database::query($sql);
  421. while ($res = Database::fetch_array($result)) {
  422. $this->sentWork[] = new Dropbox_SentWork($res['id']);
  423. }
  424. }
  425. /**
  426. * This private method is used by the usort function in the
  427. * orderSentWork and orderReceivedWork methods.
  428. * It compares 2 work-objects by 1 of the properties of that object, dictated by the
  429. * private property _orderBy
  430. *
  431. * @param unknown_type $a
  432. * @param unknown_type $b
  433. * @return -1, 0 or 1 dependent of the result of the comparison.
  434. */
  435. function _cmpWork($a, $b)
  436. {
  437. $sort = $this->_orderBy;
  438. $aval = $a->$sort;
  439. $bval = $b->$sort;
  440. if ($sort == 'recipients') {
  441. // The recipients property is an array so we do the comparison based
  442. // on the first item of the recipients array
  443. $aval = $aval[0]['name'];
  444. $bval = $bval[0]['name'];
  445. }
  446. if ($sort == 'filesize') { // Filesize is not a string, so we use other comparison technique
  447. return $aval < $bval ? -1 : 1;
  448. } elseif ($sort == 'title') { // Natural order for sorting titles is more "human-friendly"
  449. return api_strnatcmp($aval, $bval);
  450. } else {
  451. return api_strcasecmp($aval, $bval);
  452. }
  453. }
  454. /**
  455. * A method that sorts the objects in the sentWork array, dependent on the $sort parameter.
  456. * $sort can be lastDate, firstDate, title, size, ...
  457. *
  458. * @param unknown_type $sort
  459. */
  460. function orderSentWork($sort)
  461. {
  462. switch($sort) {
  463. case 'lastDate':
  464. $this->_orderBy = 'last_upload_date';
  465. break;
  466. case 'firstDate':
  467. $this->_orderBy = 'upload_date';
  468. break;
  469. case 'title':
  470. $this->_orderBy = 'title';
  471. break;
  472. case 'size':
  473. $this->_orderBy = 'filesize';
  474. break;
  475. case 'author':
  476. $this->_orderBy = 'author';
  477. break;
  478. case 'recipient':
  479. $this->_orderBy = 'recipients';
  480. break;
  481. default:
  482. $this->_orderBy = 'last_upload_date';
  483. }
  484. usort($this->sentWork, array($this, '_cmpWork'));
  485. }
  486. /**
  487. * method that sorts the objects in the receivedWork array, dependent on the $sort parameter.
  488. * $sort can be lastDate, firstDate, title, size, ...
  489. * @param unknown_type $sort
  490. */
  491. function orderReceivedWork($sort)
  492. {
  493. switch($sort) {
  494. case 'lastDate':
  495. $this->_orderBy = 'last_upload_date';
  496. break;
  497. case 'firstDate':
  498. $this->_orderBy = 'upload_date';
  499. break;
  500. case 'title':
  501. $this->_orderBy = 'title';
  502. break;
  503. case 'size':
  504. $this->_orderBy = 'filesize';
  505. break;
  506. case 'author':
  507. $this->_orderBy = 'author';
  508. break;
  509. case 'sender':
  510. $this->_orderBy = 'uploaderName';
  511. break;
  512. default:
  513. $this->_orderBy = 'last_upload_date';
  514. }
  515. usort($this->receivedWork, array($this, '_cmpWork'));
  516. }
  517. /**
  518. * Deletes all the received work of this person
  519. */
  520. function deleteAllReceivedWork()
  521. {
  522. $course_id = api_get_course_int_id();
  523. $dropbox_cnf = getDropboxConf();
  524. // Delete entries in person table concerning received works
  525. foreach ($this->receivedWork as $w) {
  526. $sql = "DELETE FROM ".$dropbox_cnf['tbl_person']."
  527. WHERE c_id = $course_id AND user_id='".$this->userId."' AND file_id='".$w->id."'";
  528. Database::query($sql);
  529. }
  530. // Check for unused files
  531. removeUnusedFiles();
  532. }
  533. /**
  534. * Deletes all the received categories and work of this person
  535. */
  536. function deleteReceivedWorkFolder($id)
  537. {
  538. $dropbox_cnf = getDropboxConf();
  539. $course_id = api_get_course_int_id();
  540. $id = intval($id);
  541. $sql = "DELETE FROM ".$dropbox_cnf['tbl_file']."
  542. WHERE c_id = $course_id AND cat_id = '".$id."' ";
  543. if (!Database::query($sql)) return false;
  544. $sql = "DELETE FROM ".$dropbox_cnf['tbl_category']."
  545. WHERE c_id = $course_id AND cat_id = '".$id."' ";
  546. if (!Database::query($sql)) return false;
  547. $sql = "DELETE FROM ".$dropbox_cnf['tbl_post']."
  548. WHERE c_id = $course_id AND cat_id = '".$id."' ";
  549. if (!Database::query($sql)) return false;
  550. return true;
  551. }
  552. /**
  553. * Deletes a received dropbox file of this person with id=$id
  554. *
  555. * @param integer $id
  556. */
  557. function deleteReceivedWork($id)
  558. {
  559. $course_id = api_get_course_int_id();
  560. $dropbox_cnf = getDropboxConf();
  561. $id = intval($id);
  562. // index check
  563. $found = false;
  564. foreach ($this->receivedWork as $w) {
  565. if ($w->id == $id) {
  566. $found = true;
  567. break;
  568. }
  569. }
  570. if (!$found) {
  571. if (!$this->deleteReceivedWorkFolder($id)) {
  572. die(get_lang('GeneralError').' (code 216)');
  573. }
  574. }
  575. // Delete entries in person table concerning received works
  576. $sql = "DELETE FROM ".$dropbox_cnf['tbl_person']."
  577. WHERE c_id = $course_id AND user_id = '".$this->userId."' AND file_id ='".$id."'";
  578. Database::query($sql);
  579. removeUnusedFiles(); // Check for unused files
  580. }
  581. /**
  582. * Deletes all the sent dropbox files of this person
  583. */
  584. function deleteAllSentWork()
  585. {
  586. $course_id = api_get_course_int_id();
  587. $dropbox_cnf = getDropboxConf();
  588. //delete entries in person table concerning sent works
  589. foreach ($this->sentWork as $w) {
  590. $sql = "DELETE FROM ".$dropbox_cnf['tbl_person']."
  591. WHERE c_id = $course_id AND user_id='".$this->userId."' AND file_id='".$w->id."'";
  592. Database::query($sql);
  593. removeMoreIfMailing($w->id);
  594. }
  595. removeUnusedFiles(); // Check for unused files
  596. }
  597. /**
  598. * Deletes a sent dropbox file of this person with id=$id
  599. *
  600. * @param unknown_type $id
  601. */
  602. function deleteSentWork($id)
  603. {
  604. $course_id = api_get_course_int_id();
  605. $dropbox_cnf = getDropboxConf();
  606. $id = intval($id);
  607. // index check
  608. $found = false;
  609. foreach ($this->sentWork as $w) {
  610. if ($w->id == $id) {
  611. $found = true;
  612. break;
  613. }
  614. }
  615. if (!$found) {
  616. if (!$this->deleteReceivedWorkFolder($id)) {
  617. die(get_lang('GeneralError').' (code 219)');
  618. }
  619. }
  620. //$file_id = $this->sentWork[$index]->id;
  621. // Delete entries in person table concerning sent works
  622. $sql = "DELETE FROM ".$dropbox_cnf['tbl_person']."
  623. WHERE c_id = $course_id AND user_id='".$this->userId."' AND file_id='".$id."'";
  624. Database::query($sql);
  625. removeMoreIfMailing($id);
  626. removeUnusedFiles(); // Check for unused files
  627. }
  628. /**
  629. * Updates feedback for received work of this person with id=$id
  630. *
  631. * @param unknown_type $id
  632. * @param unknown_type $text
  633. */
  634. function updateFeedback($id, $text)
  635. {
  636. $course_id = api_get_course_int_id();
  637. $_course = api_get_course_info();
  638. $dropbox_cnf = getDropboxConf();
  639. $id = intval($id);
  640. // index check
  641. $found = false;
  642. $wi = -1;
  643. foreach ($this->receivedWork as $w) {
  644. $wi++;
  645. if ($w->id == $id){
  646. $found = true;
  647. break;
  648. } // foreach (... as $wi -> $w) gives error 221! (no idea why...)
  649. }
  650. if (!$found) {
  651. die(get_lang('GeneralError').' (code 221)');
  652. }
  653. $feedback_date = api_get_utc_datetime();
  654. $this->receivedWork[$wi]->feedback_date = $feedback_date;
  655. $this->receivedWork[$wi]->feedback = $text;
  656. $params = [
  657. 'feedback_date' => $feedback_date,
  658. 'feedback' => $text,
  659. ];
  660. Database::update(
  661. $dropbox_cnf['tbl_post'],
  662. $params,
  663. [
  664. 'c_id = ? AND dest_user_id = ? AND file_id = ?' => [
  665. $course_id,
  666. $this->userId,
  667. $id,
  668. ],
  669. ]
  670. );
  671. // Update item_property table
  672. if (($ownerid = $this->receivedWork[$wi]->uploader_id) > $dropbox_cnf['mailingIdBase']) {
  673. $ownerid = getUserOwningThisMailing($ownerid);
  674. }
  675. api_item_property_update(
  676. $_course,
  677. TOOL_DROPBOX,
  678. $this->receivedWork[$wi]->id,
  679. 'DropboxFileUpdated',
  680. $this->userId,
  681. null,
  682. $ownerid
  683. );
  684. }
  685. /**
  686. * Filter the received work
  687. * @param string $type
  688. * @param string $value
  689. */
  690. function filter_received_work($type, $value)
  691. {
  692. $dropbox_cnf = getDropboxConf();
  693. $new_received_work = array();
  694. foreach ($this->receivedWork as $work) {
  695. switch ($type) {
  696. case 'uploader_id':
  697. if ($work->uploader_id == $value ||
  698. ($work->uploader_id > $dropbox_cnf['mailingIdBase'] &&
  699. getUserOwningThisMailing($work->uploader_id) == $value)
  700. ) {
  701. $new_received_work[] = $work;
  702. }
  703. break;
  704. default:
  705. $new_received_work[] = $work;
  706. }
  707. }
  708. $this->receivedWork = $new_received_work;
  709. }
  710. }