lp_ajax_save_objectives.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This script contains the server part of the xajax interaction process. The client part is located
  5. * in lp_api.php or other api's.
  6. * This is a first attempt at using xajax and AJAX in general, so the code might be a bit unsettling.
  7. * @package chamilo.learnpath
  8. * @author Yannick Warnier <ywarnier@beeznest.org>
  9. */
  10. // Flag to allow for anonymous user - needs to be set before global.inc.php.
  11. $use_anonymous = true;
  12. require_once __DIR__.'/../inc/global.inc.php';
  13. /**
  14. * Writes an item's new values into the database and returns the operation result
  15. * @param integer Learnpath ID
  16. * @param integer User ID
  17. * @param integer View ID
  18. * @param integer Item ID
  19. * @param array Objectives array
  20. */
  21. function save_objectives($lp_id, $user_id, $view_id, $item_id, $objectives = array())
  22. {
  23. $debug = 0;
  24. $return = '';
  25. if ($debug > 0) {
  26. error_log('In xajax_save_objectives('.$lp_id.','.$user_id.','.$view_id.','.$item_id.',"'.(count($objectives) > 0 ? count($objectives) : '').'")', 0);
  27. }
  28. $mylp = learnpath::getLpFromSession(api_get_course_id(), $lp_id, $user_id);
  29. $mylpi = & $mylp->items[$item_id];
  30. if (is_array($objectives) && count($objectives) > 0) {
  31. foreach ($objectives as $index=>$objective) {
  32. $mylpi->add_objective($index, $objectives[$index]);
  33. }
  34. $mylpi->write_objectives_to_db();
  35. }
  36. return $return;
  37. }
  38. $objectives = array();
  39. if (isset($_REQUEST['objectives'])) {
  40. if (is_array($_REQUEST['objectives'])) {
  41. foreach ($_REQUEST['objectives'] as $idx => $ob) {
  42. $objectives[$idx] = explode(',', substr($ob, 1, -1));
  43. if (!isset($objectives[$idx][4])) {
  44. // Make sure there are 7 elements.
  45. $objectives[$idx][4] = '';
  46. }
  47. }
  48. }
  49. }
  50. echo save_objectives(
  51. $_REQUEST['lid'],
  52. $_REQUEST['uid'],
  53. $_REQUEST['vid'],
  54. $_REQUEST['iid'],
  55. $objectives
  56. );