Source for file learnpathItem.class.php
Documentation is available at learnpathItem.class.php
* This file contains the lp_item class, that inherits from the learnpath class
* @package dokeos.learnpath
* @author Yannick Warnier <ywarnier@beeznest.org>
* @license GNU/GPL - See Dokeos license directory for details
* lp_item defines items belonging to a learnpath. Each item has a name, a score, a use time and additional
* information that enables tracking a user's progress in a learning path
* @package dokeos.learnpath
var $children = array(); //contains the ids of children items
var $condition; //if this item has a special condition embedded
//at the moment, interactions are just an array of arrays with a structure of 8 text fields
//id(0), type(1), time(2), weighting(3),correct_responses(4),student_response(5),result(6),latency(7)
//var $location; //only set this for SCORM?
var $possible_status = array('not attempted','incomplete','completed','passed','failed','browsed');
var $type; // this attribute can contain chapter|link|student_publication|module|quiz|document|forum|thread
var $debug = 0; //logging param
* Class constructor. Prepares the learnpath_item for later launch
* Don't forget to use set_lp_view() if applicable after creating the item.
* Setting an lp_view will finalise the item_view data collection
* @param integer Learnpath item ID
* @return boolean True on success, false on failure
if($this->debug> 0){error_log('New LP - In learnpathItem constructor: '. $db_id. ','. $user_id,0);}
$sql = "SELECT * FROM $items_table WHERE id = $id";
//error_log('New LP - Creating item object from DB: '.$sql,0);
$this->error = "Could not find given learnpath item in learnpath_item table";
//error_log('New LP - '.$this->error,0);
$this->lp_id = $row['lp_id'];
$this->name = $row['title'];
$this->type = $row['item_type'];
$this->ref = $row['ref'];
$this->title = $row['title'];
$this->path = $row['path'];
$this->parent = $row['parent_item_id'];
$this->next = $row['next_item_id'];
$this->previous = $row['previous_item_id'];
$this->display_order = $row['display_order'];
if(isset ($row['launch_data'])){
//error_log('New LP - End of learnpathItem constructor for item '.$id,0);
* Adds a child to the current item
if($this->debug> 0){error_log('New LP - In learnpathItem::add_child()',0);}
//do not check in DB as we expect the call to come from the learnpath class which should
* Adds an interaction to the current item
* @param int Index (order ID) of the interaction inside this item
* @param array Array of parameters: id(0), type(1), time(2), weighting(3),correct_responses(4),student_response(5),result(6),latency(7)
//take the current maximum index to generate the interactions_count
if(is_array($this->interactions[$index]) && count($this->interactions[$index])>0){
$this->interactions[$index] = $params;
if(count($params)==8){//we rely on the calling script to provide parameters in the right order
$this->interactions[$index] = $params;
* Adds an objective to the current item
* @param array Array of parameters: id(0), status(1), score_raw(2), score_max(3), score_min(4)
if(empty($params[0])){return null;}
//take the current maximum index to generate the objectives_count
* Closes/stops the item viewing. Finalises runtime values. If required, save to DB.
* @return boolean True on success, false otherwise
if($this->debug> 0){error_log('New LP - In learnpathItem::close()',0);}
$this->get_status(true,true);//update status (second option forces the update)
* Deletes all traces of this item in the database
* @return boolean true. Doesn't check for errors yet.
if($this->debug> 0){error_log('New LP - In learnpath_item::delete() for item '. $this->db_id,0);}
$sql_del_view = "DELETE FROM $lp_item_view WHERE item_id = ". $this->db_id;
//error_log('New LP - Deleting from lp_item_view: '.$sql_del_view,0);
$sql_del_item = "DELETE FROM $lp_item WHERE id = ". $this->db_id;
//error_log('New LP - Deleting from lp_item: '.$sql_del_view,0);
* Drops a child from the children array
* @param string index of child item to drop
if($this->debug> 0){error_log('New LP - In learnpathItem::drop_child()',0);}
foreach($this->children as $index => $child)
* Gets the current attempt_id for this user on this item
* @return integer The attempt_id for this item view by this user, or 1 if none defined
if($this->debug> 0){error_log('New LP - In learnpathItem::get_attempt_id() on item '. $this->db_id,0);}
if($this->debug> 0){error_log('New LP - End of learnpathItem::get_attempt_id() on item '. $this->db_id. ' - Returning '. $res,0);}
* Gets a list of the item's children
* @return array Array of children items IDs
if($this->debug> 0){error_log('New LP - In learnpathItem::get_children()',0);}
//error_log('New LP - Found '.$child,0);
* Gets the core_exit value from the database
* Gets the credit information (rather scorm-stuff) based on current status and reinit
* autorization. Credit tells the sco(content) if Dokeos will record the data it is sent (credit) or not (no-credit)
* @return string 'credit' or 'no-credit'. Defaults to 'credit' because if we don't know enough about this item, it's probably because it was never used before.
if(!empty($this->debug) && $this->debug> 1){error_log('New LP - In learnpathItem::get_credit()',0);}
//now check the value of prevent_reinit (if it's 0, return credit as the default was)
//if status is not attempted or incomplete, credit anyway. Otherwise:
//check the status in the database rather than in the object, as checking in the object
//would always return "no-credit" when we want to set it to completed
if(!empty($this->debug) && $this->debug> 2){error_log('New LP - In learnpathItem::get_credit() - get_prevent_reinit!=0 and status is '. $status,0);}
* Gets the current start time property
* @return integer Current start time, or current time if none
if($this->debug> 0){error_log('New LP - In learnpathItem::get_current_start_time()',0);}
* Gets the item's description
* @return string Description
if($this->debug> 0){error_log('New LP - In learnpathItem::get_description()',0);}
* Gets the file path from the course's root directory, no matter what tool it is from.
* @return string The file path, or an empty string if there is no file attached, or '-1' if the file must be replaced by an error page
if($this->debug> 0){error_log('New LP - In learnpathItem::get_file_path()',0);}
if($type == 'dokeos_chapter' OR $type== 'chapter' OR $type == 'dir')
//the path is numeric, so it is a reference to a Dokeos object
$sql = 'SELECT path FROM '. $table_doc. ' WHERE id = '. $path;
$real_path = 'document'. $row['path'];
if(!empty($path_to_scorm_dir))
$path = $path_to_scorm_dir. $path;
* @return integer Database ID for the current item
if($this->debug> 1){error_log('New LP - In learnpathItem::get_id()',0);}
//TODO check this return value is valid for children classes (SCORM?)
* Gets the current count of interactions recorded in the database
* @return int The current number of interactions recorder
if($this->debug> 1){error_log('New LP - In learnpathItem::get_interactions_count()',0);}
* Gets the current count of objectives recorded in the database
* @return int The current number of objectives recorder
if($this->debug> 1){error_log('New LP - In learnpathItem::get_objectives_count()',0);}
* Gets the launch_data field found in imsmanifests (this is SCORM- or AICC-related, really)
* @return string Launch data as found in imsmanifest and stored in Dokeos (read only). Defaults to ''.
if($this->debug> 0){error_log('New LP - In learnpathItem::get_launch_data()',0);}
* Gets the lesson location
* @return string lesson location as recorded by the SCORM and AICC elements. Defaults to ''
if($this->debug> 0){error_log('New LP - In learnpathItem::get_lesson_location()',0);}
* Gets the lesson_mode (scorm feature, but might be used by aicc as well as dokeos paths)
* The "browse" mode is not supported yet (because there is no such way of seeing a sco in Dokeos)
* @return string 'browse','normal' or 'review'. Defaults to 'normal'
* @return int Level. Defaults to 0
if($this->debug> 0){error_log('New LP - In learnpathItem::get_level()',0);}
if(empty($this->level)){return 0;}
if($this->debug> 0){error_log('New LP - In learnpathItem::get_mastery_score()',0);}
* Gets the maximum (score)
* @return int Maximum score. Defaults to 100 if nothing else is defined
if($this->debug> 0){error_log('New LP - In learnpathItem::get_max()',0);}
if(!empty($this->view_max_score) and $this->view_max_score> 0)
return $this->view_max_score;
elseif($this->view_max_score === '')
return $this->view_max_score;
* Gets the maximum time allowed for this user in this attempt on this item
* @return string Time string in SCORM format (HH:MM:SS or HH:MM:SS.SS or HHHH:MM:SS.SS)
if($this->debug> 0){error_log('New LP - In learnpathItem::get_max_time_allowed()',0);}
* Gets the minimum (score)
* @return int Minimum score. Defaults to 0
if($this->debug> 0){error_log('New LP - In learnpathItem::get_min()',0);}
* @return int Parent ID. Defaults to null
if($this->debug> 0){error_log('New LP - In learnpathItem::get_parent()',0);}
//TODO check this return value is valid for children classes (SCORM?)
* Gets the path attribute.
* @return string Path. Defaults to ''
if($this->debug> 0){error_log('New LP - In learnpathItem::get_path()',0);}
if(empty($this->path)){return '';}
* Gets the prerequisites string
* @return string Empty string or prerequisites string if defined. Defaults to
if($this->debug> 0){error_log('New LP - In learnpathItem::get_prereq_string()',0);}
* Gets the prevent_reinit attribute value (and sets it if not set already)
* @return int 1 or 0 (defaults to 1)
if($this->debug> 2){error_log('New LP - In learnpathItem::get_prevent_reinit()',0);}
if(!empty($this->lp_id)){
$sql = "SELECT * FROM $db WHERE id = ". $this->lp_id;
$this->error = "Could not find parent learnpath in learnpath table";
if($this->debug> 2){error_log('New LP - End of learnpathItem::get_prevent_reinit() - Returning false',0);}
$this->prevent_reinit = 1;//prevent reinit is always 1 by default - see learnpath.class.php
* Gets the item's reference column
* @return string The item's reference field (generally used for SCORM identifiers)
* Gets the list of included resources as a list of absolute or relative paths of
* resources included in the current item. This allows for a better SCORM export.
* The list will generally include pictures, flash objects, java applets, or any other
* stuff included in the source of the current item. The current item is expected
* to be an HTML file. If it is not, then the function will return and empty list.
* @param string type (one of the Dokeos tools) - optional (otherwise takes the current item's type)
* @param string path (absolute file path) - optional (otherwise takes the current item's path)
* @param int level of recursivity we're in
* @return array List of file paths. An additional field containing 'local' or 'remote' helps determine if the file should be copied into the zip or just linked
//echo "Abs path coming from item : ".$abs_path."<br/>\n";
echo "Abs path coming from param: ".$abs_path."<br/>\n";
//error_log(str_repeat(' ',$recursivity).'Analyse file '.$abs_path,0);
//get the document and, if HTML, open it
//for now, read the whole file in one go (that's gonna be a problem when the file is too big)
$ext = $info['extension'];
$wanted_attributes = array('src','url','@import','href','value');
//parse it for included resources
//get an array of attributes from the HTML source
//look at 'src' attributes in this file
foreach($wanted_attributes as $attr)
if(isset ($attributes[$attr]))
//find which kind of path these are (local or remote)
$sources = $attributes[$attr];
foreach($sources as $source)
//skip what is obviously not a resource
if(strpos($source,"+this.")) continue; //javascript code - will still work unaltered
if(strpos($source,'.')=== false) continue; //no dot, should not be an external file anyway
if(strpos($source,'mailto:')) continue; //mailto link
if(strpos($source,';') && !strpos($source,'&')) continue; //avoid code - that should help
if(strpos($source , 'mp3file'))
$files_list[] = array(substr($source, 0, strpos($source , '.swf')+ 4),'local','abs');
$mp3file = substr($source , strpos($source , 'mp3file=')+ 8);
if(substr($mp3file,0,1) == '/')
$files_list[] = array($mp3file,'local','abs');
$files_list[] = array($mp3file,'local','rel');
elseif(strpos($source, 'flv=')=== 0)
//we found the current portal url
$files_list[] = array($source,'local','url');
//we didn't find any trace of current portal
$files_list[] = array($source,'remote','url');
$files_list[] = array($source,'local','abs');
continue; //skipping anything else to avoid two entries (while the others can have sub-files in their url, flv's can't)
//cut at '?' in a URL with params
if(strpos($second_part,'://')> 0)
{//if the second part of the url contains a url too, treat the second one before cutting
$pos1 = strpos($second_part,'=');
$pos2 = strpos($second_part,'&');
$second_part = substr($second_part,$pos1+ 1,$pos2- ($pos1+ 1));
//we found the current portal url
$files_list[] = array($second_part,'local','url');
if(count($in_files_list)> 0)
//we didn't find any trace of current portal
$files_list[] = array($second_part,'remote','url');
elseif(strpos($second_part,'=')> 0)
if(substr($second_part,0,1) === '/')
{ //link starts with a /, making it absolute (relative to DocumentRoot)
$files_list[] = array($second_part,'local','abs');
if(count($in_files_list)> 0)
elseif(strstr($second_part,'..') === 0)
{ //link is relative but going back in the hierarchy
$files_list[] = array($second_part,'local','rel');
$new_abs_path = realpath($dir. '/'. $second_part);
if(count($in_files_list)> 0)
{ //no starting '/', making it relative to current document's path
if(substr($second_part,0,2) == './')
$second_part = substr($second_part,2);
$files_list[] = array($second_part,'local','rel');
$new_abs_path = realpath($dir. '/'. $second_part);
if(count($in_files_list)> 0)
//leave that second part behind now
//we found the current portal url
$files_list[] = array($source,'local','url');
if(count($in_files_list)> 0)
//we didn't find any trace of current portal
$files_list[] = array($source,'remote','url');
//no protocol found, make link local
if(substr($source,0,1) === '/')
{ //link starts with a /, making it absolute (relative to DocumentRoot)
$files_list[] = array($source,'local','abs');
if(count($in_files_list)> 0)
elseif(strstr($source,'..') === 0)
{ //link is relative but going back in the hierarchy
$files_list[] = array($source,'local','rel');
$new_abs_path = realpath($dir. '/'. $source);
if(count($in_files_list)> 0)
{ //no starting '/', making it relative to current document's path
if(substr($source,0,2) == './')
$files_list[] = array($source,'local','rel');
$new_abs_path = realpath($dir. '/'. $source);
if(count($in_files_list)> 0)
//found some protocol there
//we found the current portal url
$files_list[] = array($source,'local','url');
if(count($in_files_list)> 0)
//we didn't find any trace of current portal
$files_list[] = array($source,'remote','url');
//no protocol found, make link local
if(substr($source,0,1) === '/')
{ //link starts with a /, making it absolute (relative to DocumentRoot)
$files_list[] = array($source,'local','abs');
if(count($in_files_list)> 0)
elseif(strstr($source,'..') === 0)
{ //link is relative but going back in the hierarchy
$files_list[] = array($source,'local','rel');
$new_abs_path = realpath($dir. '/'. $source);
if(count($in_files_list)> 0)
{ //no starting '/', making it relative to current document's path
if(substr($source,0,2) == './')
$files_list[] = array($source,'local','rel');
$new_abs_path = realpath($dir. '/'. $source);
if(count($in_files_list)> 0)
//the file could not be found
//error_log(str_repeat(' ',$recursivity),'found files '.print_r($files_list,true),0);
$checked_files_list = array();
$checked_array_list = array();
foreach($files_list as $idx => $file)
if(!in_array($file[0],$checked_files_list))
$checked_files_list[] = $files_list[$idx][0];
$checked_array_list[] = $files_list[$idx];
return $checked_array_list;
* @return float The current score or 0 if no score set yet
if($this->debug> 0){error_log('New LP - In learnpathItem::get_score()',0);}
if($this->debug> 1){error_log('New LP - Out of learnpathItem::get_score() - returning '. $res,0);}
* @param boolean Do or don't check into the database for the latest value. Optional. Default is true
* @param boolean Do or don't update the local attribute value with what's been found in DB
* @return string Current status or 'Nnot attempted' if no status set yet
function get_status($check_db= true,$update_local= false)
if($this->debug> 0){error_log('New LP - In learnpathItem::get_status() on item '. $this->db_id,0);}
if($this->debug> 2){error_log('New LP - In learnpathItem::get_status(): checking db',0);}
if($this->debug> 2){error_log('New LP - In learnpathItem::get_status() - Checking DB: '. $sql,0);}
if($this->debug> 2){error_log('New LP - In learnpathItem::get_status() - Returning db value '. $row['status'],0);}
if($this->debug> 2){error_log('New LP - In learnpathItem::get_status() - in get_status: using attrib',0);}
if($this->debug> 2){error_log('New LP - In learnpathItem::get_status() - Returning attrib: '. $this->status,0);}
if($this->debug> 0){error_log('New LP - In learnpathItem::get_suspend_data()',0);}
//TODO : improve cleaning of breaklines ... it works but is it really a beautiful way to do it ?
* Gets the total time spent on this item view so far
* @param string Origin of the request. If coming from PHP, send formatted as xxhxx'xx", otherwise use scorm format 00:00:00
* @param integer Given time is a default time to return formatted
//if($this->debug>0){error_log('New LP - In learnpathItem::get_scorm_time()',0);}
return '00'. $h. '00\'00"';
$scorm_time = trim(sprintf("%4d:%02d:%02d",$hours,$mins,$secs));
$scorm_time = trim(sprintf("%4d$h%02d'%02d\"",$hours,$mins,$secs));
* Returns the item's title
if($this->debug> 0){error_log('New LP - In learnpathItem::get_title()',0);}
if(empty($this->title)){return '';}
* Returns the total time used to see that item
* @return integer Total time
if($this->debug> 0){error_log('New LP - In learnpathItem::get_total_time()',0);}
* @return string The item type (can be doc, dir, sco, asset)
if($this->debug> 0){error_log('New LP - In learnpathItem::get_type() on item '. $this->db_id,0);}
//error_log('In item::get_type() - returning '.$this->type,0);
if($this->debug> 2){error_log('New LP - In learnpathItem::get_type() - Returning '. $res. ' for item '. $this->db_id,0);}
* Gets the view count for this item
if($this->debug> 0){error_log('New LP - In learnpathItem::get_view_count()',0);}
* Tells if an item is done ('completed','passed','succeeded') or not
* @return bool True if the item is done ('completed','passed','succeeded'), false otherwise
if($this->status_is(array('completed','passed','succeeded'))){
if($this->debug> 2){error_log('New LP - In learnpath::is_done() - Item '. $this->get_id(). ' is complete',0);}
if($this->debug> 2){error_log('New LP - In learnpath::is_done() - Item '. $this->get_id(). ' is not complete',0);}
* Tells if a restart is allowed (take it from $this->prevent_reinit and $this->status)
* @return integer -1 if retaking the sco another time for credit is not allowed,
* 0 if it is not allowed but the item has to be finished
* 1 if it is allowed. Defaults to 1
if($this->debug> 2){error_log('New LP - In learnpathItem::is_restart_allowed()',0);}
//if status is not attempted or incomplete, authorize retaking (of the same) anyway. Otherwise:
if($this->debug> 2){error_log('New LP - End of learnpathItem::is_restart_allowed() - Returning '. $restart,0);}
* Opens/launches the item. Initialises runtime values.
* @return boolean True on success, false on failure.
function open($allow_new_attempt= false){
//In this case, as we are opening the item, what is important to us
//is the database status, in order to know if this item has already
//been used in the past (rather than just loaded and modified by
//some javascript but not written in the database).
//If the database status is different from 'not attempted', we can
//consider this item has already been used, and as such we can
//open a new attempt. Otherwise, we'll just reuse the current
//attempt, which is generally created the first time the item is
//loaded (for example as part of the table of contents)
if($allow_new_attempt && isset ($stat) && ($stat != $this->possible_status[0]))
{ //small exception for start time, to avoid amazing values
//error_log('New LP - reinit blocked by setting',0);
* Outputs the item contents
* @return string HTML file (displayable in an <iframe>) or empty string if no path defined
if($this->debug> 0){error_log('New LP - In learnpathItem::output()',0);}
* Parses the prerequisites string with the AICC logic language
* @param string The prerequisites string as it figures in imsmanifest.xml
* @param Array Array of items in the current learnpath object. Although we're in the learnpathItem object, it's necessary to have a list of all items to be able to check the current item's prerequisites
* @param Array List of references (the "ref" column in the lp_item table) that are strings used in the expression of prerequisites.
* @param integer The user ID. In some cases like Dokeos quizzes, it's necessary to have the user ID to query other tables (like the results of quizzes)
* @return boolean True if the list of prerequisites given is entirely satisfied, false otherwise
function parse_prereq($prereqs_string, $items, $refs_list,$user_id){
if($this->debug> 0){error_log('New LP - In learnpathItem::parse_prereq() for learnpath '. $this->lp_id. ' with string '. $prereqs_string,0);}
//deal with &, |, ~, =, <>, {}, ,, X*, () in reverse order
// First parse all parenthesis by using a sequential loop (looking for less-inclusives first)
if($prereqs_string == '_true_'){return true;}
if($prereqs_string == '_false_'){
while(strpos($prereqs_string,'(')!== false){
//remove any () set and replace with its value
foreach($matches[2] as $id=> $match){
$str_res = $this->parse_prereq($match,$items,$refs_list,$user_id);
$prereqs_string = str_replace($matches[1][$id],'_true_',$prereqs_string);
$prereqs_string = str_replace($matches[1][$id],'_false_',$prereqs_string);
//parenthesis removed, now look for ORs as it is the lesser-priority binary operator (= always uses one text operand)
if(strpos($prereqs_string,"|")=== false){
if($this->debug> 1){error_log('New LP - Didnt find any OR, looking for AND',0);}
if(strpos($prereqs_string,"&")!== false){
$list = split("&",$prereqs_string);
foreach($list as $condition){
$andstatus = $andstatus && $this->parse_prereq($condition,$items,$refs_list,$user_id);
if($this->debug> 1){error_log('New LP - One condition in AND was false, short-circuit',0);}
if(isset ($items[$refs_list[$list[0]]])){
$status = $items[$refs_list[$list[0]]]->get_status(true);
//no ORs found, now look for ANDs
if($this->debug> 1){error_log('New LP - Didnt find any AND, looking for =',0);}
if(strpos($prereqs_string,"=")!== false){
//we assume '=' signs only appear when there's nothing else around
$params = split('=',$prereqs_string);
//right number of operands
if(isset ($items[$refs_list[$params[0]]])){
$status = $items[$refs_list[$params[0]]]->get_status(true);
$returnstatus = ($status == $params[1]);
//No ANDs found, look for <>
if($this->debug> 1){error_log('New LP - Didnt find any =, looking for <>',0);}
if(strpos($prereqs_string,"<>")!== false){
if($this->debug> 1){error_log('New LP - Found <>, looking into it',0);}
//we assume '<>' signs only appear when there's nothing else around
$params = split('<>',$prereqs_string);
//right number of operands
if(isset ($items[$refs_list[$params[0]]])){
$status = $items[$refs_list[$params[0]]]->get_status(true);
$returnstatus = ($status != $params[1]);
//No <> found, look for ~ (unary)
if($this->debug> 1){error_log('New LP - Didnt find any =, looking for ~',0);}
//only remains: ~ and X*{}
if(strpos($prereqs_string,"~")!== false){
$myres = preg_match('/~([^(\d+\*)\{]*)/',$prereqs_string,$list);
$returnstatus = !$this->parse_prereq($list[1],$items,$refs_list,$user_id);
if($this->debug> 1){error_log('New LP - Found ~ but strange string: '. $prereqs_string,0);}
//Finally, look for sets/groups
if($this->debug> 1){error_log('New LP - Didnt find any ~, looking for groups',0);}
$groups_there = preg_match_all('/((\d+\*)?\{([^\}]+)\}+)/',$prereqs_string,$groups);
foreach($groups[1] as $gr) //only take the results that correspond to the big brackets-enclosed condition
if(preg_match('/(\d+)\*\{([^\}]+)\}/',$gr,$multi)){
if($this->debug> 1){error_log('New LP - Found multiplier '. $multi[0],0);}
$list = split(',',$multi[2]);
if(isset ($items[$refs_list[$cond]])){
$status = $items[$refs_list[$cond]]->get_status(true);
if($this->debug> 1){error_log('New LP - Found true item, counting.. ('. ($mytrue). ')',0);}
if($this->debug> 1){error_log('New LP - item '. $cond. ' does not exist in items list',0);}
if($this->debug> 1){error_log('New LP - Got enough true results, return true',0);}
if(isset ($items[$refs_list[$cond]])){
$status = $items[$refs_list[$cond]]->get_status(true);
if($this->debug> 1){error_log('New LP - Found false item, the set is not true, return false',0);}
if($this->debug> 1){error_log('New LP - item '. $cond. ' does not exist in items list',0);}
if($this->debug> 1){error_log('New LP - Found false item, the set is not true, return false',0);}
//Nothing found there either. Now return the value of the corresponding resource completion status
if($this->debug> 1){error_log('New LP - Didnt find any group, returning value for '. $prereqs_string,0);}
if(isset ($items[$refs_list[$prereqs_string]])){
if($items[$refs_list[$prereqs_string]]->type == 'quiz')
$sql = 'SELECT exe_result, exe_weighting
WHERE exe_exo_id = '. $items[$refs_list[$prereqs_string]]->path. '
AND exe_user_id = '. $user_id. '
if($quiz['exe_result'] >= $items[$refs_list[$prereqs_string]]->get_mastery_score())
$status = $items[$refs_list[$prereqs_string]]->get_status(true);
if($this->debug> 1){error_log('New LP - Prerequisite '. $prereqs_string. ' not complete',0);}
if($this->debug> 1){error_log('New LP - Prerequisite '. $prereqs_string. ' complete',0);}
if($this->debug> 1){error_log('New LP - Could not find '. $prereqs_string. ' in '. print_r($refs_list,true),0);}
$list = split("\|",$prereqs_string);
if($this->debug> 1){error_log('New LP - Found OR, looking into it',0);}
foreach($list as $condition){
if($this->debug> 1){error_log('New LP - Found OR, adding it ('. $condition. ')',0);}
$orstatus = $orstatus || $this->parse_prereq($condition,$items,$refs_list,$user_id);
if($this->debug> 1){error_log('New LP - One condition in OR was true, short-circuit',0);}
if($this->debug> 1){error_log('New LP - OR was found but only one elem present !?',0);}
if(isset ($items[$refs_list[$list[0]]])){
$status = $items[$refs_list[$list[0]]]->get_status(true);
$returnstatus = (($status == 'completed') OR ($status == 'passed'));
* Parses the HTML attributes given as string.
* @param string HTML attribute string
* @param array List of attributes that we want to get back
* @return array An associative array of attributes
* @author Based on a function from the HTML_Common2 PEAR module
//Find all occurences of something that looks like a URL
// The structure of this regexp is:
// (optionally find some kind of space 1 or more times) then
// find (either an equal sign or a bracket) followed by an optional space
// followed by some text without quotes (between quotes itself or not)
// then possible closing brackets if we were in the opening bracket case
// OR something like @import()
'/(((([A-Za-z_:])([A-Za-z0-9_:\.-]*))' .
// '/(((([A-Za-z_:])([A-Za-z0-9_:\.-]|[^\x00-\x7F])*)' . -> seems to be taking too much
// '/(((([A-Za-z_:])([^\x00-\x7F])*)' . -> takes only last letter of parameter name
// '(=([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+))' . -> doesn't restrict close enough to the url itself
'(=([ \n\t\r]+)?("[^"\)]+"|\'[^\'\)]+\'|[^ \n\t\r\)]+))' .
// '(\(([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+)\))' . -> doesn't restrict close enough to the url itself
'(\(([ \n\t\r]+)?("[^"\)]+"|\'[^\'\)]+\'|[^ \n\t\r\)]+)\))' .
// '(@import([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+)))?/', -> takes a lot (like 100's of thousands of empty possibilities)
'(@import([ \n\t\r]+)?("[^"]+"|\'[^\']+\'|[^ \n\t\r]+)))/',
error_log('Caught exception: '. $e->getMessage(),0) ;
for ($i = 0; $i < count($regs[1]); $i++ ) {
$name = trim($regs[3][$i]);
$check = trim($regs[0][$i]);
$value = trim($regs[10][$i]);
if(empty($value) and !empty($regs[13][$i]))
if(empty($name) && !empty($regs[16][$i]))
$value = trim($regs[16][$i]);
if (!empty($value) && ($value[0] == '\'' || $value[0] == '"')) {
$value = substr($value, 1, - 1);
error_log('preg_match did not find anything',0);
* Reinits all local values as the learnpath is restarted
* @return boolean True on success, false otherwise
if($this->debug> 0){error_log('New LP - In learnpathItem::restart()',0);}
//nothing allowed, do nothing
//restart as new attempt is allowed, record a new attempt
//restart current element is allowed (because it's not finished yet),
* Saves data in the database
* @param boolean Save from URL params (1) or from object attributes (0)
* @param boolean The results of a check on prerequisites for this item. True if prerequisites are completed, false otherwise. Defaults to false. Only used if not sco or au
* @return boolean True on success, false on failure
function save($from_outside= true,$prereqs_complete= false)
//$item_view_table = Database::get_course_table(COURSEID,LEARNPATH_ITEM_VIEW_TABLE);
//first check if parameters passed via GET can be saved here
//in case it's a SCORM, we should get:
if($this->type == 'sco' || $this->type== 'au'){
if($this->debug> 1){error_log('New LP - In learnpathItem::save() - save reinit blocked by setting',0);}
//do nothing because the status has already been set. Don't allow it to change.
//TODO check there isn't a special circumstance where this should be saved
if($this->debug> 1){error_log('New LP - In learnpathItem::save() - SCORM save request received',0);}
//get all new settings from the URL
if($this->debug> 1){error_log('New LP - In learnpathItem::save() - Getting item data from outside',0);}
foreach($_GET as $param => $value)
if($this->debug> 2){error_log('New LP - In learnpathItem::save() - setting score to '. $value,0);}
if($this->debug> 2){error_log('New LP - In learnpathItem::save() - setting view_max_score to '. $value,0);}
if($this->debug> 2){error_log('New LP - In learnpathItem::save() - setting min_score to '. $value,0);}
if($this->debug> 2){error_log('New LP - In learnpathItem::save() - setting status to '. $value,0);}
if($this->debug> 2){error_log('New LP - In learnpathItem::save() - setting time to '. $value,0);}
if($this->debug> 2){error_log('New LP - In learnpathItem::save() - setting suspend_data to '. $value,0);}
if($this->debug> 2){error_log('New LP - In learnpathItem::save() - setting lesson_location to '. $value,0);}
if($this->debug> 2){error_log('New LP - In learnpathItem::save() - setting core_exit to '. $value,0);}
//$interactions = unserialize($value);
//foreach($interactions as $interaction){
// $this->set_max_time_allowed($value);
case 'objectives._count':
$this->attempt_id = $value;
if($this->debug> 1){error_log('New LP - In learnpathItem::save() - Using inside item status',0);}
//do nothing, just let the local attributes be used
}else{ //if not SCO, such messages should not be expected
//for now, everything that is not sco and not asset is set to
if($this->debug> 1){error_log('New LP - End of learnpathItem::save() - Calling write_to_db()',0);}
* Sets the number of attempt_id to a given value
* @param integer The given value to set attempt_id to
* @return boolean TRUE on success, FALSE otherwise
if($this->debug> 0){error_log('New LP - In learnpathItem::set_attempt_id()',0);}
* Sets the core_exit value to the one given
$this->core_exit = 'suspend';
$this->core_exit = 'none';
* Sets the item's description
* @param string Description
if($this->debug> 0){error_log('New LP - In learnpathItem::set_description()',0);}
* Sets the lesson_location value
* @param string lesson_location as provided by the SCO
* @return boolean True on success, false otherwise
if($this->debug> 0){error_log('New LP - In learnpathItem::set_lesson_location()',0);}
* Sets the item's depth level in the LP tree (0 is at root)
if($this->debug> 0){error_log('New LP - In learnpathItem::set_level('. $int. ')',0);}
* Sets the lp_view id this item view is registered to
* @param integer lp_view DB ID
* @todo //todo insert into lp_item_view if lp_view not exists
if($this->debug> 0){error_log('New LP - In learnpathItem::set_lp_view('. $lp_view_id. ')',0);}
if(!empty($lp_view_id) and $lp_view_id = intval(strval($lp_view_id)))
//get the lp_item_view with the highest view_count
$sql = "SELECT * FROM $item_view_table WHERE lp_item_id = ". $this->get_id(). " " .
" AND lp_view_id = ". $lp_view_id. " ORDER BY view_count DESC";
if($this->debug> 2){error_log('New LP - In learnpathItem::set_lp_view() - Querying lp_item_view: '. $sql,0);}
$this->view_max_score = $row['max_score'];
//$this->view_min_score = $row['min_score'];
$this->status = $row['status'];
$this->core_exit = $row['core_exit'];
if($this->debug> 2){error_log('New LP - In learnpathItem::set_lp_view() - Updated item object with database values',0);}
//now get the number of interactions for this little guy
$sql = "SELECT * FROM $item_view_interaction_table WHERE lp_iv_id = '". $this->db_item_view_id. "'";
//now get the number of objectives for this little guy
$sql = "SELECT * FROM $item_view_objective_table WHERE lp_iv_id = '". $this->db_item_view_id. "'";
if($this->debug> 2){error_log('New LP - End of learnpathItem::set_lp_view()',0);}
if($this->debug> 0){error_log('New LP - In learnpathItem::set_path()',0);}
if(!empty($string)){$this->path = $string;}
* Sets the prevent_reinit attribute. This is based on the LP value and is set at creation time for
* each learnpathItem. It is a (bad?) way of avoiding a reference to the LP when saving an item.
* @param integer 1 for "prevent", 0 for "don't prevent" saving freshened values (new "not attempted" status etc)
if($this->debug> 0){error_log('New LP - In learnpathItem::set_prevent_reinit()',0);}
* Sets the score value. If the mastery_score is set and the score reaches
* it, then set the status to 'passed'.
* @return boolean True on success, false otherwise
if($this->debug> 0){error_log('New LP - In learnpathItem::set_score('. $score. ')',0);}
//if mastery_score is set AND the current score reaches the mastery score AND the current status is different from 'completed', then set it to 'passed'
* Sets the maximum score for this item
* @param int Maximum score - must be a decimal or an empty string
* @return boolean True on success, false on error
if($this->debug> 0){error_log('New LP - In learnpathItem::set_max_score('. $score. ')',0);}
if(is_int($score) or $score == '')
if($this->debug> 1){error_log('New LP - In learnpathItem::set_max_score() - Updated object score of item '. $this->db_id. ' to '. $this->view_max_score,0);}
* Sets the status for this item
* @param string Status - must be one of the values defined in $this->possible_status
* @return boolean True on success, false on error
if($this->debug> 0){error_log('New LP - In learnpathItem::set_status('. $status. ')',0);}
//if(in_array($status,$this->possible_status))
if($this->debug> 1){error_log('New LP - In learnpathItem::set_status() - Updated object status of item '. $this->db_id. ' to '. $this->status,0);}
//error_log('New LP - '.$status.' was not in the possible status',0);
* Sets the item viewing time in a usable form, given that SCORM packages often give it as 00:00:00.0000
* @param string Time as given by SCORM
function set_time($scorm_time,$format= 'scorm')
if($this->debug> 0){error_log('New LP - In learnpathItem::set_time('. $scorm_time. ')',0);}
if($this->debug> 0){error_log('New LP - In learnpathItem::set_time('. $scorm_time. ') - found asset - set time to '. $my_time,0);}
if(preg_match('/^(\d{1,4}):(\d{2}):(\d{2})(\.\d{1,4})?/',$scorm_time,$res)){
//getting total number of seconds spent
$total_sec = $hour* 3600 + $min* 60 + $sec;
}elseif($format == 'int'){
if($this->debug> 0){error_log('New LP - In learnpathItem::set_title()',0);}
if(!empty($string)){$this->title = $string;}
if($this->debug> 0){error_log('New LP - In learnpathItem::set_type()',0);}
if(!empty($string)){$this->type = $string;}
* Checks if the current status is part of the list of status given
* @param strings_array An array of status to check for. If the current status is one of the strings, return true
* @return boolean True if the status was one of the given strings, false otherwise
foreach($list as $status)
if($this->debug> 2){error_log('New LP - learnpathItem::status_is() - Found status '. $status. ' corresponding to current status',0);}
if($this->debug> 2){error_log('New LP - learnpathItem::status_is() - Status '. $mystatus. ' did not match request',0);}
* Updates the time info according to the given session_time
* @param integer Time in seconds
* //TODO @TODO make this method better by allowing better/multiple time slices
if($this->debug> 0){error_log('New LP - In learnpathItem::update_time('. $total_sec. ')',0);}
//getting start time from finish time. The only problem in the calculation is it might be
//modified by the scripts processing time
$start = $now- $total_sec;
/*if(empty($this->current_start_time)){
$this->current_start_time = $start;
$this->current_stop_time = $now;
//if($this->current_stop_time != $this->current_start_time){
//if the stop time has already been set before to something else
//than the start time, add the given time to what's already been
//This is the SCORM way of doing things, because the time comes from
//core.session_time, not core.total_time
// UPDATE: adding time to previous time is only done on SCORM's finish()
// call, not normally, so for now ignore this section
// $this->current_stop_time = $this->current_stop_time + $stop;
// error_log('New LP - Adding '.$stop.' seconds - now '.$this->current_stop_time,0);
//if no previous stop time set, use the one just calculated now from
//$this->current_start_time = $start;
//$this->current_stop_time = $now;
//error_log('New LP - Setting '.$stop.' seconds - now '.$this->current_stop_time,0);
* Write objectives to DB. This method is separate from write_to_db() because otherwise
* objectives are lost as a side effect to AJAX and session concurrent access
* @return boolean True or false on error
if($this->debug> 0){error_log('New LP - In learnpathItem::write_objectives_to_db()',0);}
$sql = "SELECT id FROM $tbl " .
"WHERE lp_item_id = ". $this->db_id. " " .
"AND lp_view_id = ". $this->view_id. " " .
if($this->debug> 2){error_log('New LP - In learnpathItem::write_to_db() - Got item_view_id '. $lp_iv_id. ', now checking objectives ',0);}
foreach($this->objectives as $index => $objective){
$iva_sql = "SELECT id FROM $iva_table " .
"WHERE lp_iv_id = $lp_iv_id " .
//"AND order_id = $index";
//also check for the objective ID as it must be unique for this SCO view
"AND objective_id = '". $objective[0]. "'";
//id(0), type(1), time(2), weighting(3),correct_responses(4),student_response(5),result(6),latency(7)
$ivau_sql = "UPDATE $iva_table " .
"SET objective_id = '". $objective[0]. "'," .
"status = '". $objective[1]. "'," .
"score_raw = '". $objective[2]. "'," .
"score_min = '". $objective[4]. "'," .
"score_max = '". $objective[3]. "' " .
//error_log($ivau_sql,0);
$ivai_sql = "INSERT INTO $iva_table " .
"(lp_iv_id, order_id, objective_id, status, score_raw, score_min, score_max )" .
"(". $lp_iv_id. ", ". $index. ",'". $objective[0]. "','". $objective[1]. "'," .
"'". $objective[2]. "','". $objective[4]. "','". $objective[3]. "')";
//error_log('no objective to save: '.print_r($this->objectives,1));
* Writes the current data to the database
* @return boolean Query result
if($this->debug> 0){error_log('New LP - In learnpathItem::write_to_db()',0);}
if(($this->type == 'sco') && ($credit == 'no-credit' OR $mode == 'review' OR $mode == 'browse'))
//this info shouldn't be saved as the credit or lesson mode info prevent it
if($this->debug> 1){error_log('New LP - In learnpathItem::write_to_db() - credit('. $credit. ') or lesson_mode('. $mode. ') prevent recording!',0);}
$check = "SELECT * FROM $item_view_table " .
"WHERE lp_item_id = ". $this->db_id. " " .
"AND lp_view_id = ". $this->view_id. " ".
if($this->debug> 2){error_log('New LP - In learnpathItem::write_to_db() - Querying item_view: '. $check,0);}
//depending on what we want (really), we'll update or insert a new row
$sql = "INSERT INTO $item_view_table " .
//"'".$this->get_max_time_allowed()."'," .
if($this->debug> 2){error_log('New LP - In learnpathItem::write_to_db() - Inserting into item_view: '. $sql,0);}
if($this->type== 'hotpotatoes')
{ //make an exception for HotPotatoes, don't update the score
//because it has been saved outside of this tool
$sql = "UPDATE $item_view_table " .
" max_score = '". $this->get_max(). "'," .
"WHERE lp_item_id = ". $this->db_id. " " .
"AND lp_view_id = ". $this->view_id. " " .
{ //for all other content types...
$sql = "UPDATE $item_view_table " .
" max_score = '". $this->get_max(). "'," .
//" max_time_allowed = '".$this->get_max_time_allowed()."'," .
"WHERE lp_item_id = ". $this->db_id. " " .
"AND lp_view_id = ". $this->view_id. " " .
if($this->debug> 2){error_log('New LP - In learnpathItem::write_to_db() - Updating item_view: '. $sql,0);}
// $this->error = 'Could not update item_view table...'.mysql_error();
$sql = "SELECT id FROM $tbl " .
"WHERE lp_item_id = ". $this->db_id. " " .
"AND lp_view_id = ". $this->view_id. " " .
if($this->debug> 2){error_log('New LP - In learnpathItem::write_to_db() - Got item_view_id '. $lp_iv_id. ', now checking interactions ',0);}
if(is_array($interaction[4]) && !empty($interaction[4][0]) ){
foreach($interaction[4] as $resp){
$correct_resp .= $resp. ',';
$correct_resp = substr($correct_resp,0,strlen($correct_resp)- 1);
$iva_sql = "SELECT id FROM $iva_table " .
"WHERE lp_iv_id = $lp_iv_id " .
// "AND order_id = $index";
//also check for the interaction ID as it must be unique for this SCO view
"AND (order_id = $index " .
"OR interaction_id = '". $interaction[0]. "')";
//id(0), type(1), time(2), weighting(3),correct_responses(4),student_response(5),result(6),latency(7)
$ivau_sql = "UPDATE $iva_table " .
"SET interaction_id = '". $interaction[0]. "'," .
"interaction_type = '". $interaction[1]. "'," .
"weighting = '". $interaction[3]. "'," .
"completion_time = '". $interaction[2]. "'," .
"correct_responses = '". $correct_resp. "'," .
"student_response = '". $interaction[5]. "'," .
"result = '". $interaction[6]. "'," .
"latency = '". $interaction[7]. "'" .
$ivai_sql = "INSERT INTO $iva_table " .
"(order_id, lp_iv_id, interaction_id, interaction_type, " .
"weighting, completion_time, correct_responses, " .
"student_response, result, latency)" .
"(". $index. ",". $lp_iv_id. ",'". $interaction[0]. "','". $interaction[1]. "'," .
"'". $interaction[3]. "','". $interaction[2]. "','". $correct_resp. "'," .
"'". $interaction[5]. "','". $interaction[6]. "','". $interaction[7]. "'" .
if($this->debug> 2){error_log('New LP - End of learnpathItem::write_to_db()',0);}
|