dokeos-learnpath
[ class tree: dokeos-learnpath ] [ index: dokeos-learnpath ] [ all elements ]

Source for file lp_comm.server.php

Documentation is available at lp_comm.server.php

  1. <?php //$id: $
  2. /**
  3.  * This script contains the server part of the xajax interaction process. The client part is located
  4.  * in lp_api.php or other api's.
  5.  * This is a first attempt at using xajax and AJAX in general, so the code might be a bit unsettling.
  6.  * @package dokeos.learnpath
  7.  * @author Yannick Warnier <ywarnier@beeznest.org>
  8.  */
  9. /**
  10.  * Script
  11.  */
  12. //flag to allow for anonymous user - needs to be set before global.inc.php
  13. $use_anonymous true;
  14. // name of the language file that needs to be included 
  15. $language_file['learnpath';
  16. require_once('back_compat.inc.php');
  17. /**
  18.  * Backup an item's values into the javascript API as "old" values (so we still have them at hand)
  19.  * @param    integer    Learnpath ID
  20.  * @param    integer    User ID
  21.  * @param    integer View ID
  22.  * @param    integer    Item ID
  23.  * @param    double    Current score
  24.  * @param    double    Maximum score
  25.  * @param    double    Minimum score
  26.  * @param    string    Lesson status
  27.  * @param    string    Session time
  28.  * @param    string    Suspend data
  29.  * @param    string    Lesson location
  30.  */
  31. function backup_item_details($lp_id,$user_id,$view_id,$item_id,$score=-1,$max=-1,$min=-1,$status='',$time='',$suspend='',$location='')
  32. {
  33.     $objResponse new xajaxResponse();
  34.     $objResponse->addScript(
  35.             "old_score=".$score.";" .
  36.             "old_max=".$max.";" .
  37.             "old_min=".$min.";" .
  38.             "old_lesson_status='".$status."';" .
  39.             "old_session_time='".$time."';" .
  40.             "lms_old_item_id='".$item_id."';" .
  41.             "old_suspend_data='".$suspend."';" .
  42.             "old_lesson_location='".$location."';");
  43.     //$objResponse->addAlert('data for item '.$item_id.', user '.$user_id.' backed up');
  44.     return $objResponse;
  45. }
  46. /**
  47.  * Writes an item's new values into the database and returns the operation result
  48.  * @param    integer    Learnpath ID
  49.  * @param    integer    User ID
  50.  * @param    integer View ID
  51.  * @param    integer    Item ID
  52.  * @param    double    Current score
  53.  * @param    double    Maximum score
  54.  * @param    double    Minimum score
  55.  * @param    string    Lesson status
  56.  * @param    string    Session time
  57.  * @param    string    Suspend data
  58.  * @param    string    Lesson location
  59.  * @param    string    Core exit SCORM string
  60.  */
  61. function save_item($lp_id,$user_id,$view_id,$item_id,$score=-1,$max=-1,$min=-1,$status='',$time=0,$suspend='',$location='',$interactions=array(),$core_exit='none')
  62. {
  63.     global $_configuration;
  64.     $debug=0;
  65.     if($debug>0){error_log('In xajax_save_item('.$lp_id.','.$user_id.','.$view_id.','.$item_id.','.$score.','.$max.','.$min.',"'.$status.'",'.$time.',"'.$suspend.'","'.$location.'","'.(count($interactions)>0?$interactions[0]:'').'","'.$core_exit.'")',0);}
  66.     $objResponse new xajaxResponse();
  67.     require_once('learnpath.class.php');
  68.     require_once('scorm.class.php');
  69.     require_once('aicc.class.php');
  70.     require_once('learnpathItem.class.php');
  71.     require_once('scormItem.class.php');
  72.     require_once('aiccItem.class.php');
  73.     $mylp '';
  74.     if(isset($_SESSION['lpobject']))
  75.     {
  76.         if($debug>1){error_log('////$_SESSION[lpobject] is set',0);}
  77.         $oLP =unserialize($_SESSION['lpobject']);
  78.         if(!is_object($oLP)){
  79.             if($debug>2){error_log(print_r($oLP,true),0);}
  80.             if($debug>2){error_log('////Building new lp',0);}
  81.             unset($oLP);
  82.             $code api_get_course_id();
  83.             $mylp new learnpath($code,$lp_id,$user_id);
  84.         }else{
  85.             if($debug>2){error_log('////Reusing session lp',0);}
  86.             $mylp $oLP;
  87.         }
  88.     }
  89.     //$objResponse->addAlert(api_get_path(REL_CODE_PATH).'newscorm/learnpathItem.class.php');
  90.     
  91.     
  92.     $prereq_check $mylp->prerequisites_match($item_id);
  93.     if($prereq_check === true//launch the prerequisites check and set error if needed
  94.     {
  95.     
  96.         $mylpi =$mylp->items[$item_id];
  97.         //$mylpi->set_lp_view($view_id);
  98.         if($max!=-1)
  99.         {
  100.             $mylpi->max_score=$max;
  101.         }
  102.         if($min!=-1)
  103.         {
  104.             $mylpi->min_score=$min;
  105.         }
  106.         if($score!=-1)
  107.         {
  108.             $mylpi->set_score($score);
  109.         }
  110.         if($status!='')
  111.         {
  112.             if($debug>1){error_log('Calling set_status('.$status.') from xajax',0);}
  113.             $mylpi->set_status($status);
  114.             if($debug>1){error_log('Done calling set_status from xajax',0);}
  115.         }
  116.         if($time!='')
  117.         {
  118.             //if big integer, then it's a timestamp, otherwise it's normal scorm time
  119.             if($time == intval(strval($time)) && $time>1000000){
  120.                 $real_time time($time;
  121.                 //$real_time += $mylpi->get_total_time();
  122.                 $mylpi->set_time($real_time,'int');
  123.             }else{
  124.                 $mylpi->set_time($time);
  125.             }
  126.         }
  127.         if($suspend!='')
  128.         {
  129.             $mylpi->current_data $suspend;//escapetxt($suspend);
  130.         }
  131.         if($location!='')
  132.         {
  133.             $mylpi->set_lesson_location($location);
  134.         }
  135.         //deal with interactions provided in arrays in the following format
  136.         //id(0), type(1), time(2), weighting(3),correct_responses(4),student_response(5),result(6),latency(7)
  137.         if(is_array($interactions&& count($interactions)>0){
  138.             foreach($interactions as $index=>$interaction){
  139.                 $mylpi->add_interaction($index,$interactions[$index]);
  140.             }
  141.         }
  142.         $mylpi->set_core_exit($core_exit);
  143.         $mylp->save_item($item_id,false);
  144.     }
  145.     
  146.     $mystatus $mylpi->get_status(false);
  147.     $mytotal $mylp->get_total_items_count_without_chapters();
  148.     $mycomplete $mylp->get_complete_items_count();
  149.     $myprogress_mode $mylp->get_progress_bar_mode();
  150.     $myprogress_mode ($myprogress_mode==''?'%':$myprogress_mode);
  151.     //$mylpi->write_to_db();
  152.     $_SESSION['lpobject'serialize($mylp);
  153.     if($mylpi->get_type()!='sco')
  154.     //if this object's JS status has not been updated by the SCORM API, update now 
  155.         $objResponse->addScript("lesson_status='".$mystatus."';");
  156.     }
  157.     $objResponse->addScript("update_toc('".$mystatus."','".$item_id."');");
  158.     $update_list $mylp->get_update_queue();
  159.     foreach($update_list as $my_upd_id => $my_upd_status)
  160.     {
  161.         if($my_upd_id != $item_id)//only update the status from other items (i.e. parents and brothers), do not update current as we just did it already
  162.             $objResponse->addScript("update_toc('".$my_upd_status."','".$my_upd_id."');");
  163.         }        
  164.     }
  165.     $objResponse->addScript("update_progress_bar('$mycomplete','$mytotal','$myprogress_mode');");
  166.  
  167.     if($debug>0){
  168.         $objResponse->addScript("logit_lms('Saved data for item ".$item_id.", user ".$user_id." (status=".$mystatus.")',2)");
  169.         if($debug>1){error_log('End of xajax_save_item()',0);}
  170.     }
  171.     
  172.     if($_configuration['tracking_enabled'&& !isset($_SESSION['login_as']))
  173.     // if $_SESSION['login_as'] is set, then the user is an admin logged as the user
  174.     
  175.         $tbl_track_login Database :: get_statistic_table(TABLE_STATISTIC_TRACK_E_LOGIN);
  176.         
  177.         $sql_last_connection="SELECT login_id, login_date FROM $tbl_track_login WHERE login_user_id='".api_get_user_id()."' ORDER BY login_date DESC LIMIT 0,1";
  178.         
  179.         $q_last_connection=api_sql_query($sql_last_connection);
  180.         if(Database::num_rows($q_last_connection0)
  181.         {
  182.             $row Database::fetch_array($q_last_connection);
  183.             $i_id_last_connection=$row['login_id'];
  184.             $s_sql_update_logout_date="UPDATE $tbl_track_login SET logout_date=NOW() WHERE login_id='$i_id_last_connection'";
  185.             api_sql_query($s_sql_update_logout_date);
  186.         }
  187.         
  188.     }
  189.     
  190.     return $objResponse;
  191. }
  192. /**
  193.  * Writes an item's new values into the database and returns the operation result
  194.  * @param    integer    Learnpath ID
  195.  * @param    integer    User ID
  196.  * @param    integer View ID
  197.  * @param    integer    Item ID
  198.  * @param    array    Objectives array
  199.  */
  200. function save_objectives($lp_id,$user_id,$view_id,$item_id,$objectives=array())
  201. {
  202.     global $_configuration;
  203.     $debug=0;
  204.     if($debug>0){error_log('In xajax_save_objectives('.$lp_id.','.$user_id.','.$view_id.','.$item_id.',"'.(count($objectives)>0?count($objectives):'').'")',0);}
  205.     $objResponse new xajaxResponse();
  206.     require_once('learnpath.class.php');
  207.     require_once('scorm.class.php');
  208.     require_once('aicc.class.php');
  209.     require_once('learnpathItem.class.php');
  210.     require_once('scormItem.class.php');
  211.     require_once('aiccItem.class.php');
  212.     $mylp '';
  213.     if(isset($_SESSION['lpobject']))
  214.     {
  215.         if($debug>1){error_log('////$_SESSION[lpobject] is set',0);}
  216.         $oLP =unserialize($_SESSION['lpobject']);
  217.         if(!is_object($oLP)){
  218.             if($debug>2){error_log(print_r($oLP,true),0);}
  219.             if($debug>2){error_log('////Building new lp',0);}
  220.             unset($oLP);
  221.             $code api_get_course_id();
  222.             $mylp new learnpath($code,$lp_id,$user_id);
  223.         }else{
  224.             if($debug>2){error_log('////Reusing session lp',0);}
  225.             $mylp $oLP;
  226.         }
  227.     }
  228.     $mylpi =$mylp->items[$item_id];
  229.     //error_log(__FILE__.' '.__LINE__.' '.print_r($objectives,true),0);
  230.     if(is_array($objectives&& count($objectives)>0){
  231.         foreach($objectives as $index=>$objective){
  232.             //error_log(__FILE__.' '.__LINE__.' '.$objectives[$index][0],0);
  233.             $mylpi->add_objective($index,$objectives[$index]);
  234.             $mylpi->write_objectives_to_db();
  235.         }
  236.     }
  237.     return $objResponse;
  238. }
  239. /**
  240.  * Get one item's details
  241.  * @param    integer    LP ID
  242.  * @param    integer    user ID
  243.  * @param    integer    View ID
  244.  * @param    integer    Current item ID
  245.  * @param    integer New item ID
  246.  */
  247. function switch_item_details($lp_id,$user_id,$view_id,$current_item,$next_item)
  248. {
  249.     $debug=0;
  250.     if($debug>0){error_log('In xajax_switch_item_details('.$lp_id.','.$user_id.','.$view_id.','.$current_item.','.$next_item.')',0);}
  251.     $objResponse new xajaxResponse();
  252.     /*$item_id may be one of:
  253.      * -'next'
  254.      * -'previous'
  255.      * -'first'
  256.      * -'last'
  257.      * - a real item ID
  258.      */
  259.     require_once('learnpath.class.php');
  260.     require_once('scorm.class.php');
  261.     require_once('aicc.class.php');
  262.     require_once('learnpathItem.class.php');
  263.     require_once('scormItem.class.php');
  264.     require_once('aiccItem.class.php');
  265.     $mylp '';
  266.     if(isset($_SESSION['lpobject']))
  267.     {
  268.         if($debug>1){error_log('////$_SESSION[lpobject] is set',0);}
  269.         $oLP =unserialize($_SESSION['lpobject']);
  270.         if(!is_object($oLP)){
  271.             if($debug>1){error_log(print_r($oLP,true),0);}
  272.             if($debug>2){error_log('////Building new lp',0);}
  273.             unset($oLP);
  274.             $code api_get_course_id();
  275.             $mylp new learnpath($code,$lp_id,$user_id);
  276.         }else{
  277.             if($debug>1){error_log('////Reusing session lp',0);}
  278.             $mylp $oLP;
  279.         }
  280.     }
  281.     $new_item_id 0;
  282.     switch($next_item){
  283.         case 'next':
  284.             $mylp->set_current_item($current_item);
  285.             $mylp->next();
  286.             $new_item_id $mylp->get_current_item_id();
  287.             if($debug>1){error_log('In {next} - next item is '.$new_item_id.'(current: '.$current_item.')',0);}
  288.             break;
  289.         case 'previous':
  290.             $mylp->set_current_item($current_item);
  291.             $mylp->previous();
  292.             $new_item_id $mylp->get_current_item_id();            
  293.             if($debug>1){error_log('In {previous} - next item is '.$new_item_id.'(current: '.$current_item.')',0);}
  294.             break;
  295.         case 'first':
  296.             $mylp->set_current_item($current_item);
  297.             $mylp->first();
  298.             $new_item_id $mylp->get_current_item_id();
  299.             if($debug>1){error_log('In {first} - next item is '.$new_item_id.'(current: '.$current_item.')',0);}
  300.             break;
  301.         case 'last':
  302.             break;
  303.         default:
  304.             //should be filtered to check it's not hacked
  305.             if($next_item == $current_item){
  306.                 //if we're opening the same item again
  307.                 $mylp->items[$current_item]->restart();
  308.             }
  309.             $new_item_id $next_item;
  310.             $mylp->set_current_item($new_item_id);
  311.             if($debug>1){error_log('In {default} - next item is '.$new_item_id.'(current: '.$current_item.')',0);}
  312.             break;
  313.     }
  314.     $mylp->start_current_item(true);
  315.     if($mylp->force_commit){
  316.         $mylp->save_current();
  317.     }
  318.     //$objResponse->addAlert(api_get_path(REL_CODE_PATH).'newscorm/learnpathItem.class.php');
  319.     if(is_object($mylp->items[$new_item_id])){
  320.         $mylpi $mylp->items[$new_item_id];
  321.     }else{
  322.         if($debug>1){error_log('In switch_item_details - generating new item object',0);}
  323.         $mylpi =new learnpathItem($new_item_id,$user_id);
  324.         $mylpi->set_lp_view($view_id);
  325.     }
  326.     /*
  327.      * now get what's needed by the SCORM API:
  328.      * -score
  329.      * -max
  330.      * -min
  331.      * -lesson_status
  332.      * -session_time
  333.      * -suspend_data
  334.      */
  335.     $myscore $mylpi->get_score();
  336.     $mymax $mylpi->get_max();
  337.     $mymin $mylpi->get_min();
  338.     $mylesson_status $mylpi->get_status();
  339.     $mylesson_location $mylpi->get_lesson_location();
  340.     $mytotal_time $mylpi->get_scorm_time('js');
  341.     $mymastery_score $mylpi->get_mastery_score();
  342.     $mymax_time_allowed $mylpi->get_max_time_allowed();
  343.     $mylaunch_data $mylpi->get_launch_data();
  344.     /*
  345.     if($mylpi->get_type() == 'asset'){
  346.         //temporary measure to save completion of an asset. Later on, Dokeos should trigger something on unload, maybe... (even though that would mean the last item cannot be completed)
  347.         $mylesson_status = 'completed';
  348.         $mylpi->set_status('completed');
  349.         $mylpi->save();
  350.     }
  351.     */
  352.     $mysession_time $mylpi->get_total_time();
  353.     $mysuspend_data $mylpi->get_suspend_data();
  354.     $mylesson_location $mylpi->get_lesson_location();
  355.     $objResponse->addScript(
  356.             "score=".$myscore.";" .
  357.             "max=".$mymax.";" .
  358.             "min=".$mymin.";" .
  359.             "lesson_status='".$mylesson_status."';" .
  360.             "lesson_location='".$mylesson_location."';" .
  361.             "session_time='".$mysession_time."';" .
  362.             "suspend_data='".$mysuspend_data."';" .
  363.             "lesson_location='".$mylesson_location."';" .
  364.             "total_time = '".$mytotal_time."';" .
  365.             "mastery_score = '".$mymastery_score."';" .
  366.             "max_time_allowed = '".$mymax_time_allowed."';" .
  367.             "launch_data = '".$mylaunch_data."';" .
  368.             "interactions = new Array();" .
  369.             "item_objectives = new Array();" .
  370.             "G_lastError = 0;" .
  371.             "G_LastErrorMessage = 'No error';");
  372.     /*
  373.      * and re-initialise the rest
  374.      * -saved_lesson_status = 'not attempted'
  375.      * -lms_lp_id
  376.      * -lms_item_id
  377.      * -lms_old_item_id
  378.      * -lms_new_item_id
  379.      * -lms_been_synchronized
  380.      * -lms_initialized
  381.      * -lms_total_lessons
  382.      * -lms_complete_lessons
  383.      * -lms_progress_bar_mode
  384.      * -lms_view_id
  385.      * -lms_user_id
  386.      */
  387.     $mytotal $mylp->get_total_items_count_without_chapters();
  388.     $mycomplete $mylp->get_complete_items_count();
  389.     $myprogress_mode $mylp->get_progress_bar_mode();
  390.     $myprogress_mode ($myprogress_mode==''?'%':$myprogress_mode);
  391.     $mynext $mylp->get_next_item_id();
  392.     $myprevious $mylp->get_previous_item_id();
  393.     $myitemtype $mylpi->get_type();
  394.     $mylesson_mode $mylpi->get_lesson_mode();
  395.     $mycredit $mylpi->get_credit();
  396.     $mylaunch_data $mylpi->get_launch_data();
  397.     $myinteractions_count $mylpi->get_interactions_count();
  398.     $myobjectives_count $mylpi->get_objectives_count();
  399.     $mycore_exit $mylpi->get_core_exit();
  400.     $objResponse->addScript(
  401.             "saved_lesson_status='not attempted';" .
  402.             "lms_lp_id=".$lp_id.";" .
  403.             "lms_item_id=".$new_item_id.";" .
  404.             "lms_old_item_id=0;" .
  405.             "lms_been_synchronized=0;" .
  406.             "lms_initialized=0;" .
  407.             "lms_total_lessons=".$mytotal.";" .
  408.             "lms_complete_lessons=".$mycomplete.";" .
  409.             "lms_progress_bar_mod='".$myprogress_mode."';" .
  410.             "lms_view_id=".$view_id.";" .
  411.             "lms_user_id=".$user_id.";" .
  412.             "next_item=".$new_item_id.";" //this one is very important to replace possible literal strings
  413.             "lms_next_item=".$mynext.";" .
  414.             "lms_previous_item=".$myprevious.";" .
  415.             "lms_item_type = '".$myitemtype."';" .
  416.             "lms_item_credit = '".$mycredit."';" .
  417.             "lms_item_lesson_mode = '".$mylesson_mode."';" .
  418.             "lms_item_launch_data = '".$mylaunch_data."';" .
  419.             "lms_item_interactions_count = '".$myinteractions_count."';" .
  420.             "lms_item_objectives_count = '".$myinteractions_count."';" .
  421.             "lms_item_core_exit = '".$mycore_exit."';" .
  422.             "asset_timer = 0;"
  423.             );
  424.     $objResponse->addScript("update_toc('unhighlight','".$current_item."');");
  425.     $objResponse->addScript("update_toc('highlight','".$new_item_id."');");
  426.     $objResponse->addScript("update_toc('$mylesson_status','".$new_item_id."');");
  427.     $objResponse->addScript("update_progress_bar('$mycomplete','$mytotal','$myprogress_mode');");
  428.  
  429.     $mylp->set_error_msg('');
  430.     $mylp->prerequisites_match()//check the prerequisites are all complete
  431.     if($debug>1){error_log('Prereq_match() returned '.htmlentities($mylp->error),0);}
  432.     $objResponse->addScript("update_message_frame('".str_replace("'","\'",htmlentities($mylp->error))."');");
  433.     $_SESSION['scorm_item_id'$new_item_id;//Save the new item ID for the exercise tool to use
  434.     $_SESSION['lpobject'serialize($mylp);
  435.     return $objResponse;
  436. }
  437. /**
  438.  * Start a timer and hand it back to the JS by assigning the current time (of start) to
  439.  * var asset_timer
  440.  */
  441. function start_timer()
  442. {
  443.     $objResponse new xajaxResponse();
  444.     $time time();
  445.     $objResponse->addScript("asset_timer='$time';asset_timer_total=0;");
  446.     return $objResponse;
  447. }
  448.  
  449. require('lp_comm.common.php');
  450. $xajax->processRequests();
  451. ?>

Documentation generated on Thu, 12 Jun 2008 14:02:02 -0500 by phpDocumentor 1.4.1