lp_ajax_save_objectives.php 1.9 KB

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