dropbox_class.inc.php 23 KB

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