dropbox_class.inc.php 23 KB

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