dropbox_class.inc.php 24 KB

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