dropbox_class.inc.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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. public $id;
  45. public $uploader_id;
  46. public $uploaderName;
  47. public $filename;
  48. public $filesize;
  49. public $title;
  50. public $description;
  51. public $author;
  52. public $upload_date;
  53. public $last_upload_date;
  54. public $isOldWork;
  55. public $feedback_date, $feedback;
  56. /**
  57. * Constructor calls private functions to create a new work or retreive an existing work from DB
  58. * depending on the number of parameters
  59. *
  60. * @param unknown_type $arg1
  61. * @param unknown_type $arg2
  62. * @param unknown_type $arg3
  63. * @param unknown_type $arg4
  64. * @param unknown_type $arg5
  65. * @param unknown_type $arg6
  66. * @return Dropbox_Work
  67. */
  68. function Dropbox_Work($arg1, $arg2 = null, $arg3 = null, $arg4 = null, $arg5 = null, $arg6 = null) {
  69. if (func_num_args() > 1) {
  70. $this->_createNewWork($arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
  71. } else {
  72. $this->_createExistingWork($arg1);
  73. }
  74. }
  75. /**
  76. * private function creating a new work object
  77. *
  78. * @param unknown_type $uploader_id
  79. * @param unknown_type $title
  80. * @param unknown_type $description
  81. * @param unknown_type $author
  82. * @param unknown_type $filename
  83. * @param unknown_type $filesize
  84. *
  85. * @todo $author was originally a field but this has now been replaced by the first and lastname of the uploader (to prevent anonymous uploads)
  86. * As a consequence this parameter can be removed
  87. */
  88. function _createNewWork($uploader_id, $title, $description, $author, $filename, $filesize) {
  89. global $_user, $dropbox_cnf;
  90. // Do some sanity checks
  91. settype($uploader_id, 'integer') or die(get_lang('GeneralError').' (code 201)'); //set $uploader_id to correct type
  92. //if (! isCourseMember($uploader_id)) die(); //uploader must be coursemember to be able to upload
  93. //-->this check is done when submitting data so it isn't checked here
  94. // Fill in the properties
  95. $this->uploader_id = $uploader_id;
  96. $this->uploaderName = getUserNameFromId($this->uploader_id);
  97. $this->filename = $filename;
  98. $this->filesize = $filesize;
  99. $this->title = $title;
  100. $this->description = $description;
  101. $this->author = api_get_person_name($_user['firstName'], $_user['lastName']);
  102. $this->last_upload_date = api_get_utc_datetime();
  103. // Check if object exists already. If it does, the old object is used
  104. // with updated information (authors, descriptio, upload_date)
  105. $this->isOldWork = false;
  106. $sql = "SELECT id, upload_date
  107. FROM ".$dropbox_cnf['tbl_file']."
  108. WHERE filename = '".Database::escape_string($this->filename)."'";
  109. $result = Database::query($sql);
  110. $res = Database::fetch_array($result);
  111. if ($res) {
  112. $this->isOldWork = true;
  113. }
  114. // Insert or update the dropbox_file table and set the id property
  115. if ($this->isOldWork) {
  116. $this->id = $res['id'];
  117. $this->upload_date = $res['upload_date'];
  118. $sql = "UPDATE ".$dropbox_cnf["tbl_file"]."
  119. SET filesize = '".Database::escape_string($this->filesize)."'
  120. , title = '".Database::escape_string($this->title)."'
  121. , description = '".Database::escape_string($this->description)."'
  122. , author = '".Database::escape_string($this->author)."'
  123. , last_upload_date = '".Database::escape_string($this->last_upload_date)."'
  124. WHERE id='".Database::escape_string($this->id)."'";
  125. $result = Database::query($sql);
  126. } else {
  127. $this->upload_date = $this->last_upload_date;
  128. $sql = "INSERT INTO ".$dropbox_cnf['tbl_file']."
  129. (uploader_id, filename, filesize, title, description, author, upload_date, last_upload_date, session_id)
  130. VALUES ('".Database::escape_string($this->uploader_id)."'
  131. , '".Database::escape_string($this->filename)."'
  132. , '".Database::escape_string($this->filesize)."'
  133. , '".Database::escape_string($this->title)."'
  134. , '".Database::escape_string($this->description)."'
  135. , '".Database::escape_string($this->author)."'
  136. , '".Database::escape_string($this->upload_date)."'
  137. , '".Database::escape_string($this->last_upload_date)."'
  138. , ".intval($_SESSION['id_session'])."
  139. )";
  140. $result = Database::query($sql);
  141. $this->id = Database::insert_id(); // Get automatically inserted id
  142. }
  143. // Insert entries into person table
  144. $sql = "INSERT INTO ".$dropbox_cnf['tbl_person']."
  145. (file_id, user_id)
  146. VALUES ('".Database::escape_string($this->id)."'
  147. , '".Database::escape_string($this->uploader_id)."'
  148. )";
  149. $result = Database::query($sql); //if work already exists no error is generated
  150. }
  151. /**
  152. * private function creating existing object by retreiving info from db
  153. *
  154. * @param unknown_type $id
  155. */
  156. function _createExistingWork($id) {
  157. global $_user, $dropbox_cnf;
  158. // Do some sanity checks
  159. settype($id, 'integer') or die(get_lang('GeneralError').' (code 205)'); // Set $id to correct type
  160. $id = intval($id);
  161. // Get the data from DB
  162. $sql = "SELECT uploader_id, filename, filesize, title, description, author, upload_date, last_upload_date, cat_id
  163. FROM ".$dropbox_cnf['tbl_file']."
  164. WHERE id='".Database::escape_string($id)."'";
  165. $result = Database::query($sql);
  166. $res = Database::fetch_array($result, 'ASSOC');
  167. // Check if uploader is still in Chamilo system
  168. $uploader_id = stripslashes($res['uploader_id']);
  169. $uploaderName = getUserNameFromId($uploader_id);
  170. if (!$uploaderName) {
  171. //deleted user
  172. $this->uploader_id = -1;
  173. $this->uploaderName = get_lang('Unknown', '');
  174. } else {
  175. $this->uploader_id = $uploader_id;
  176. $this->uploaderName = $uploaderName;
  177. }
  178. // Fill in properties
  179. $this->id = $id;
  180. $this->filename = stripslashes($res['filename']);
  181. $this->filesize = stripslashes($res['filesize']);
  182. $this->title = stripslashes($res['title']);
  183. $this->description = stripslashes($res['description']);
  184. $this->author = stripslashes($res['author']);
  185. $this->upload_date = stripslashes($res['upload_date']);
  186. $this->last_upload_date = stripslashes($res['last_upload_date']);
  187. $this->category = $res['cat_id'];
  188. // Getting the feedback on the work.
  189. if ($_GET['action'] == 'viewfeedback' AND $this->id == $_GET['id']) {
  190. $feedback2 = array();
  191. $sql_feedback = "SELECT * FROM ".$dropbox_cnf['tbl_feedback']." WHERE file_id='".$id."' ORDER BY feedback_id ASC";
  192. $result = Database::query($sql_feedback);
  193. while ($row_feedback = Database::fetch_array($result)) {
  194. $row_feedback['feedback'] = Security::remove_XSS($row_feedback['feedback']);
  195. $feedback2[] = $row_feedback;
  196. }
  197. $this->feedback2= $feedback2;
  198. }
  199. /*
  200. $result = Database::query("SELECT feedback_date, feedback, cat_id FROM ".
  201. dropbox_cnf('tbl_post')." WHERE dest_user_id='".$_user['user_id'].
  202. "' AND file_id='".$id."'");
  203. if ($res = Database::fetch_array($result)) {
  204. $this->feedback_date = $res['feedback_date'];
  205. $this->feedback = $res['feedback'];
  206. $this->category = $res['cat_id'];
  207. } // do not fail if there is no recipient = current user...*/
  208. }
  209. }
  210. class Dropbox_SentWork extends Dropbox_Work
  211. {
  212. public $recipients; //array of ['id']['name'] arrays
  213. /**
  214. * Constructor calls private functions to create a new work or retreive an existing work from DB
  215. * depending on the number of parameters
  216. *
  217. * @param unknown_type $arg1
  218. * @param unknown_type $arg2
  219. * @param unknown_type $arg3
  220. * @param unknown_type $arg4
  221. * @param unknown_type $arg5
  222. * @param unknown_type $arg6
  223. * @param unknown_type $arg7
  224. * @return Dropbox_SentWork
  225. */
  226. function Dropbox_SentWork($arg1, $arg2 = null, $arg3 = null, $arg4 = null, $arg5 = null, $arg6 = null, $arg7 = null) {
  227. if (func_num_args() > 1) {
  228. $this->_createNewSentWork($arg1, $arg2, $arg3, $arg4, $arg5, $arg6, $arg7);
  229. } else {
  230. $this->_createExistingSentWork($arg1);
  231. }
  232. }
  233. /**
  234. * private function creating a new SentWork object
  235. *
  236. * @param unknown_type $uploader_id
  237. * @param unknown_type $title
  238. * @param unknown_type $description
  239. * @param unknown_type $author
  240. * @param unknown_type $filename
  241. * @param unknown_type $filesize
  242. * @param unknown_type $recipient_ids
  243. */
  244. function _createNewSentWork($uploader_id, $title, $description, $author, $filename, $filesize, $recipient_ids) {
  245. global $dropbox_cnf;
  246. // Call constructor of Dropbox_Work object
  247. $this->Dropbox_Work($uploader_id, $title, $description, $author, $filename, $filesize);
  248. // Do sanity checks on recipient_ids array & property fillin
  249. // The sanity check for ex-coursemembers is already done in base constructor
  250. settype($uploader_id, 'integer') or die(get_lang('GeneralError').' (code 208)'); // Set $uploader_id to correct type
  251. $justSubmit = false;
  252. if ( is_int($recipient_ids)) {
  253. $justSubmit = true;
  254. $recipient_ids = array($recipient_ids + $this->id);
  255. } elseif ( count($recipient_ids) == 0) {
  256. $justSubmit = true;
  257. $recipient_ids = array($uploader_id);
  258. }
  259. if (! is_array($recipient_ids) || count($recipient_ids) == 0) {
  260. die(get_lang('GeneralError').' (code 209)');
  261. }
  262. foreach ($recipient_ids as $rec) {
  263. if (empty($rec)) die(get_lang('GeneralError').' (code 210)');
  264. //if (!isCourseMember($rec)) die(); //cannot sent document to someone outside of course
  265. //this check is done when validating submitted data
  266. $this->recipients[] = array('id' => $rec, 'name' => getUserNameFromId($rec));
  267. }
  268. // Insert data in dropbox_post and dropbox_person table for each recipient
  269. foreach ($this->recipients as $rec) {
  270. $sql = "INSERT INTO ".$dropbox_cnf['tbl_post']." (file_id, dest_user_id, session_id)
  271. VALUES ('".Database::escape_string($this->id)."', '".Database::escape_string($rec['id'])."', ".intval($_SESSION['id_session']).")";
  272. $result = Database::query($sql); // If work already exists no error is generated
  273. // Insert entries into person table
  274. $sql = "INSERT INTO ".$dropbox_cnf['tbl_person']." (file_id, user_id)
  275. VALUES ('".Database::escape_string($this->id)."', '".Database::escape_string($rec['id'])."')";
  276. // Do not add recipient in person table if mailing zip or just upload.
  277. if (!$justSubmit) {
  278. $result = Database::query($sql); // If work already exists no error is generated
  279. }
  280. // Update item_property table for each recipient
  281. global $_course, $dropbox_cnf;
  282. if (($ownerid = $this->uploader_id) > $dropbox_cnf['mailingIdBase']) {
  283. $ownerid = getUserOwningThisMailing($ownerid);
  284. }
  285. if (($recipid = $rec["id"]) > $dropbox_cnf['mailingIdBase']) {
  286. $recipid = $ownerid; // mailing file recipient = mailing id, not a person
  287. }
  288. api_item_property_update($_course, TOOL_DROPBOX, $this->id, 'DropboxFileAdded', $ownerid, null, $recipid) ;
  289. }
  290. }
  291. /**
  292. * private function creating existing object by retreiving info from db
  293. *
  294. * @param unknown_type $id
  295. */
  296. function _createExistingSentWork ($id) {
  297. global $dropbox_cnf;
  298. // Call constructor of Dropbox_Work object
  299. $this->Dropbox_Work($id);
  300. // Do sanity check. The sanity check for ex-coursemembers is already done in base constructor
  301. settype($id, 'integer') or die(get_lang('GeneralError').' (code 211)'); // Set $id to correct type
  302. // Fill in recipients array
  303. $this->recipients = array();
  304. $sql = "SELECT dest_user_id, feedback_date, feedback
  305. FROM ".$dropbox_cnf['tbl_post']."
  306. WHERE file_id='".Database::escape_string($id)."'";
  307. $result = Database::query($sql);
  308. while ($res = Database::fetch_array($result)) {
  309. // Check for deleted users
  310. $dest_user_id = $res['dest_user_id'];
  311. $recipientName = getUserNameFromId($dest_user_id);
  312. //$this->category = $res['cat_id'];
  313. if (!$recipientName) {
  314. $this->recipients[] = array('id' => -1, 'name' => get_lang('Unknown', ''));
  315. } else {
  316. $this->recipients[] = array('id' => $dest_user_id, 'name' => $recipientName, 'user_id' => $dest_user_id,
  317. 'feedback_date' => $res['feedback_date'], 'feedback' => $res['feedback']);
  318. }
  319. }
  320. }
  321. }
  322. class Dropbox_Person
  323. {
  324. // The receivedWork and the sentWork arrays are sorted.
  325. public $receivedWork; // an array of Dropbox_Work objects
  326. public $sentWork; // an array of Dropbox_SentWork objects
  327. public $userId = 0;
  328. public $isCourseAdmin = false;
  329. public $isCourseTutor = false;
  330. public $_orderBy = ''; // private property that determines by which field
  331. /**
  332. * Constructor for recreating the Dropbox_Person object
  333. *
  334. * @param unknown_type $userId
  335. * @param unknown_type $isCourseAdmin
  336. * @param unknown_type $isCourseTutor
  337. * @return Dropbox_Person
  338. */
  339. function Dropbox_Person ($userId, $isCourseAdmin, $isCourseTutor) {
  340. // Fill in properties
  341. $this->userId = $userId;
  342. $this->isCourseAdmin = $isCourseAdmin;
  343. $this->isCourseTutor = $isCourseTutor;
  344. $this->receivedWork = array();
  345. $this->sentWork = array();
  346. // Note: perhaps include an ex coursemember check to delete old files
  347. $session_id = api_get_session_id();
  348. $condition_session = api_get_session_condition($session_id);
  349. $post_tbl = Database::get_course_table(TABLE_DROPBOX_POST);
  350. $person_tbl = Database::get_course_table(TABLE_DROPBOX_PERSON);
  351. $file_tbl = Database::get_course_table(TABLE_DROPBOX_FILE);
  352. // Find all entries where this person is the recipient
  353. $sql = "SELECT r.file_id, r.cat_id
  354. FROM $post_tbl r, $person_tbl p
  355. WHERE r.dest_user_id = '".Database::escape_string($this->userId)."'
  356. AND r.dest_user_id = p.user_id
  357. AND r.file_id = p.file_id $condition_session";
  358. //if (intval($_SESSION['id_session']>0)) { $sql .= " AND r.session_id = ".intval($_SESSION['id_session']); }
  359. $result = Database::query($sql);
  360. while ($res = Database::fetch_array($result)) {
  361. $temp = new Dropbox_Work($res['file_id']);
  362. $temp -> category = $res['cat_id'];
  363. $this->receivedWork[] = $temp;
  364. }
  365. // Find all entries where this person is the sender/uploader
  366. $sql = "SELECT f.id
  367. FROM $file_tbl f, $person_tbl p
  368. WHERE f.uploader_id = '".Database::escape_string($this->userId)."'
  369. AND f.uploader_id = p.user_id
  370. AND f.id = p.file_id $condition_session";
  371. //if(intval($_SESSION['id_session']>0)) { $sql .= " AND f.session_id = ".intval($_SESSION['id_session']); }
  372. $result = Database::query($sql);
  373. while ($res = Database::fetch_array($result)) {
  374. $this->sentWork[] = new Dropbox_SentWork($res['id']);
  375. }
  376. }
  377. /**
  378. * This private method is used by the usort function in the
  379. * orderSentWork and orderReceivedWork methods.
  380. * It compares 2 work-objects by 1 of the properties of that object, dictated by the
  381. * private property _orderBy
  382. *
  383. * @param unknown_type $a
  384. * @param unknown_type $b
  385. * @return -1, 0 or 1 dependent of the result of the comparison.
  386. */
  387. function _cmpWork($a, $b) {
  388. $sort = $this->_orderBy;
  389. $aval = $a->$sort;
  390. $bval = $b->$sort;
  391. if ($sort == 'recipients') { // The recipients property is an array so we do the comparison based on the first item of the recipients array
  392. $aval = $aval[0]['name'];
  393. $bval = $bval[0]['name'];
  394. }
  395. if ($sort == 'filesize') { // Filesize is not a string, so we use other comparison technique
  396. return $aval < $bval ? -1 : 1;
  397. } elseif ($sort == 'title') { // Natural order for sorting titles is more "human-friendly"
  398. return api_strnatcmp($aval, $bval);
  399. } else {
  400. return api_strcasecmp($aval, $bval);
  401. }
  402. }
  403. /**
  404. * A method that sorts the objects in the sentWork array, dependent on the $sort parameter.
  405. * $sort can be lastDate, firstDate, title, size, ...
  406. *
  407. * @param unknown_type $sort
  408. */
  409. function orderSentWork($sort) {
  410. switch($sort) {
  411. case 'lastDate':
  412. $this->_orderBy = 'last_upload_date';
  413. break;
  414. case 'firstDate':
  415. $this->_orderBy = 'upload_date';
  416. break;
  417. case 'title':
  418. $this->_orderBy = 'title';
  419. break;
  420. case 'size':
  421. $this->_orderBy = 'filesize';
  422. break;
  423. case 'author':
  424. $this->_orderBy = 'author';
  425. break;
  426. case 'recipient':
  427. $this->_orderBy = 'recipients';
  428. break;
  429. default:
  430. $this->_orderBy = 'last_upload_date';
  431. }
  432. usort($this->sentWork, array($this, '_cmpWork'));
  433. }
  434. /**
  435. * method that sorts the objects in the receivedWork array, dependent on the $sort parameter.
  436. * $sort can be lastDate, firstDate, title, size, ...
  437. * @param unknown_type $sort
  438. */
  439. function orderReceivedWork($sort) {
  440. switch($sort) {
  441. case 'lastDate':
  442. $this->_orderBy = 'last_upload_date';
  443. break;
  444. case 'firstDate':
  445. $this->_orderBy = 'upload_date';
  446. break;
  447. case 'title':
  448. $this->_orderBy = 'title';
  449. break;
  450. case 'size':
  451. $this->_orderBy = 'filesize';
  452. break;
  453. case 'author':
  454. $this->_orderBy = 'author';
  455. break;
  456. case 'sender':
  457. $this->_orderBy = 'uploaderName';
  458. break;
  459. default:
  460. $this->_orderBy = 'last_upload_date';
  461. }
  462. usort($this->receivedWork, array($this, '_cmpWork'));
  463. }
  464. /**
  465. * Deletes all the received work of this person
  466. */
  467. function deleteAllReceivedWork () {
  468. global $dropbox_cnf;
  469. // Delete entries in person table concerning received works
  470. foreach ($this->receivedWork as $w) {
  471. Database::query("DELETE FROM ".$dropbox_cnf['tbl_person']." WHERE user_id='".$this->userId."' AND file_id='".$w->id."'");
  472. }
  473. removeUnusedFiles(); // Check for unused files
  474. }
  475. /**
  476. * Deletes all the received categories and work of this person
  477. */
  478. function deleteReceivedWorkFolder($id) {
  479. global $dropbox_cnf;
  480. $id = intval($id);
  481. $sql = "DELETE FROM ".$dropbox_cnf['tbl_file']." where cat_id = '".$id."' ";
  482. if (!Database::query($sql)) return false;
  483. $sql = "DELETE FROM ".$dropbox_cnf['tbl_category']." where cat_id = '".$id."' ";
  484. if (!Database::query($sql)) return false;
  485. $sql = "DELETE FROM ".$dropbox_cnf['tbl_post']." where cat_id = '".$id."' ";
  486. if (!Database::query($sql)) return false;
  487. return true;
  488. }
  489. /**
  490. * Deletes a received dropbox file of this person with id=$id
  491. *
  492. * @param integer $id
  493. */
  494. function deleteReceivedWork($id) {
  495. global $dropbox_cnf;
  496. $id = intval($id);
  497. // index check
  498. $found = false;
  499. foreach ($this->receivedWork as $w) {
  500. if ($w->id == $id) {
  501. $found = true;
  502. break;
  503. }
  504. }
  505. if (!$found) {
  506. if(!$this->deleteReceivedWorkFolder($id)) {
  507. die(get_lang('GeneralError').' (code 216)');
  508. }
  509. }
  510. // Delete entries in person table concerning received works
  511. Database::query("DELETE FROM ".$dropbox_cnf['tbl_person']." WHERE user_id='".$this->userId."' AND file_id='".$id."'");
  512. removeUnusedFiles(); // Check for unused files
  513. }
  514. /**
  515. * Deletes all the sent dropbox files of this person
  516. */
  517. function deleteAllSentWork() {
  518. global $dropbox_cnf;
  519. //delete entries in person table concerning sent works
  520. foreach ($this->sentWork as $w) {
  521. Database::query("DELETE FROM ".$dropbox_cnf['tbl_person']." WHERE user_id='".$this->userId."' AND file_id='".$w->id."'");
  522. removeMoreIfMailing($w->id);
  523. }
  524. removeUnusedFiles(); // Check for unused files
  525. }
  526. /**
  527. * Deletes a sent dropbox file of this person with id=$id
  528. *
  529. * @param unknown_type $id
  530. */
  531. function deleteSentWork($id) {
  532. global $dropbox_cnf;
  533. $id = intval($id);
  534. // index check
  535. $found = false;
  536. foreach ($this->sentWork as $w) {
  537. if ($w->id == $id) {
  538. $found = true;
  539. break;
  540. }
  541. }
  542. if (!$found) {
  543. if (!$this->deleteReceivedWorkFolder($id)) {
  544. die(get_lang('GeneralError').' (code 219)');
  545. }
  546. }
  547. //$file_id = $this->sentWork[$index]->id;
  548. // Delete entries in person table concerning sent works
  549. Database::query("DELETE FROM ".$dropbox_cnf['tbl_person']." WHERE user_id='".$this->userId."' AND file_id='".$id."'");
  550. removeMoreIfMailing($id);
  551. removeUnusedFiles(); // Check for unused files
  552. }
  553. /**
  554. * Updates feedback for received work of this person with id=$id
  555. *
  556. * @param unknown_type $id
  557. * @param unknown_type $text
  558. */
  559. function updateFeedback($id, $text) {
  560. global $_course, $dropbox_cnf;
  561. $id = intval($id);
  562. // index check
  563. $found = false;
  564. $wi = -1;
  565. foreach ($this->receivedWork as $w) {
  566. $wi++;
  567. if ($w->id == $id){
  568. $found = true;
  569. break;
  570. } // foreach (... as $wi -> $w) gives error 221! (no idea why...)
  571. }
  572. if (!$found) {
  573. die(get_lang('GeneralError').' (code 221)');
  574. }
  575. $feedback_date = date('Y-m-d H:i:s', time());
  576. $this->receivedWork[$wi]->feedback_date = $feedback_date;
  577. $this->receivedWork[$wi]->feedback = $text;
  578. Database::query("UPDATE ".$dropbox_cnf['tbl_post']." SET feedback_date='".
  579. Database::escape_string($feedback_date)."', feedback='".Database::escape_string($text).
  580. "' WHERE dest_user_id='".$this->userId."' AND file_id='".$id."'");
  581. // Update item_property table
  582. if (($ownerid = $this->receivedWork[$wi]->uploader_id) > $dropbox_cnf['mailingIdBase']) {
  583. $ownerid = getUserOwningThisMailing($ownerid);
  584. }
  585. api_item_property_update($_course, TOOL_DROPBOX, $this->receivedWork[$wi]->id, 'DropboxFileUpdated', $this->userId, null, $ownerid) ;
  586. }
  587. /**
  588. * Filter the received work
  589. * @param string $type
  590. * @param string $value
  591. */
  592. function filter_received_work($type, $value) {
  593. global $dropbox_cnf;
  594. $new_received_work = array();
  595. foreach ($this->receivedWork as $index => $work) {
  596. switch ($type) {
  597. case 'uploader_id':
  598. if ($work->uploader_id == $value ||
  599. ($work->uploader_id > $dropbox_cnf['mailingIdBase'] && getUserOwningThisMailing($work->uploader_id) == $value)) {
  600. $new_received_work[] = $work;
  601. }
  602. break;
  603. default:
  604. $new_received_work[] = $work;
  605. }
  606. }
  607. $this->receivedWork = $new_received_work;
  608. }
  609. }