Minor - Format code, using Database::insert(), adding error_logs

1.9.x
Julio Montoya 10 years ago
parent 13b50cf459
commit 1de245928c
  1. 419
      main/newscorm/learnpath.class.php

@ -209,9 +209,9 @@ class learnpath
error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - NOT Found previous view', 0); error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - NOT Found previous view', 0);
} }
$this->attempt = 1; $this->attempt = 1;
$sql_ins = "INSERT INTO $lp_table (c_id, lp_id, user_id, view_count, session_id) $sql = "INSERT INTO $lp_table (c_id, lp_id, user_id, view_count, session_id)
VALUES ($course_id, $lp_id, $user_id, 1, $session_id)"; VALUES ($course_id, $lp_id, $user_id, 1, $session_id)";
Database::query($sql_ins); Database::query($sql);
$this->lp_view_id = Database::insert_id(); $this->lp_view_id = Database::insert_id();
if ($this->debug > 2) { if ($this->debug > 2) {
error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - inserting new lp_view: ' . $sql_ins, 0); error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - inserting new lp_view: ' . $sql_ins, 0);
@ -263,6 +263,7 @@ class learnpath
// Don't use reference here as the next loop will make the pointed object change. // Don't use reference here as the next loop will make the pointed object change.
$this->items[$my_item_id] = $oItem; $this->items[$my_item_id] = $oItem;
$this->refs_list[$oItem->ref] = $my_item_id; $this->refs_list[$oItem->ref] = $my_item_id;
if ($this->debug > 2) { if ($this->debug > 2) {
error_log('New LP - object with id ' . $my_item_id . ' set in items[]', 0); error_log('New LP - object with id ' . $my_item_id . ' set in items[]', 0);
@ -354,12 +355,13 @@ class learnpath
$this->items[$item_id]->set_status($this->default_status); $this->items[$item_id]->set_status($this->default_status);
} }
// Add that row to the lp_item_view table so that we have something to show in the stats page. // Add that row to the lp_item_view table so that we have something to show in the stats page.
$sql_ins = "INSERT INTO $lp_item_view_table (c_id, lp_item_id, lp_view_id, view_count, status) $sql = "INSERT INTO $lp_item_view_table (c_id, lp_item_id, lp_view_id, view_count, status)
VALUES ($course_id, ".$item_id . "," . $this->lp_view_id . ", 1, 'not attempted')"; VALUES ($course_id, ".$item_id . "," . $this->lp_view_id . ", 1, 'not attempted')";
if ($this->debug > 2) { if ($this->debug > 2) {
error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - Inserting blank item_view : ' . $sql_ins, 0); error_log('New LP - learnpath::__construct() ' . __LINE__ . ' - Inserting blank item_view : ' . $sql, 0);
} }
Database::query($sql_ins); $this->items[$item_id]->set_lp_view($this->lp_view_id, $course_id);
Database::query($sql);
} }
} }
} }
@ -454,16 +456,13 @@ class learnpath
error_log('New LP - In learnpath::add_item(' . $parent . ',' . $previous . ',' . $type . ',' . $id . ',' . $title . ')', 0); error_log('New LP - In learnpath::add_item(' . $parent . ',' . $previous . ',' . $type . ',' . $id . ',' . $title . ')', 0);
} }
$tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM); $tbl_lp_item = Database :: get_course_table(TABLE_LP_ITEM);
$parent = intval($parent); $parent = intval($parent);
$previous = intval($previous); $previous = intval($previous);
$id = intval($id); $id = intval($id);
$max_time_allowed = Database::escape_string(htmlentities($max_time_allowed)); $max_time_allowed = htmlentities($max_time_allowed);
if (empty ($max_time_allowed)) { if (empty($max_time_allowed)) {
$max_time_allowed = 0; $max_time_allowed = 0;
} }
$title = Database::escape_string($title);
$description = Database::escape_string($description);
$sql_count = " SELECT COUNT(id) AS num $sql_count = " SELECT COUNT(id) AS num
FROM $tbl_lp_item FROM $tbl_lp_item
WHERE WHERE
@ -478,12 +477,13 @@ class learnpath
if ($num > 0) { if ($num > 0) {
if ($previous == 0) { if ($previous == 0) {
$sql = "SELECT id, next_item_id, display_order $sql = "SELECT id, next_item_id, display_order
FROM " . $tbl_lp_item . " FROM " . $tbl_lp_item . "
WHERE c_id = $course_id AND WHERE
lp_id = " . $this->get_id() . " AND c_id = $course_id AND
parent_item_id = " . $parent . " AND lp_id = " . $this->get_id() . " AND
previous_item_id = 0 OR parent_item_id = " . $parent . " AND
previous_item_id=" . $parent; previous_item_id = 0 OR
previous_item_id=" . $parent;
$result = Database::query($sql); $result = Database::query($sql);
$row = Database :: fetch_array($result); $row = Database :: fetch_array($result);
@ -500,7 +500,7 @@ class learnpath
id = " . $previous; id = " . $previous;
$result = Database::query($sql); $result = Database::query($sql);
$row = Database :: fetch_array($result); $row = Database:: fetch_array($result);
$tmp_previous = $row['id']; $tmp_previous = $row['id'];
$next = $row['next_item_id']; $next = $row['next_item_id'];
@ -512,7 +512,6 @@ class learnpath
$display_order = 0; $display_order = 0;
} }
$new_item_id = -1;
$id = Database::escape_string($id); $id = Database::escape_string($id);
$typeCleaned = Database::escape_string($type); $typeCleaned = Database::escape_string($type);
if ($type == 'quiz') { if ($type == 'quiz') {
@ -535,79 +534,34 @@ class learnpath
$max_score = 100; $max_score = 100;
} }
$params = array(
"c_id" => $course_id,
"lp_id" => $this->get_id(),
"item_type" =>$typeCleaned ,
"ref" => '',
"title" =>$title ,
"description" => $description,
"path" => $id,
"max_score" => $max_score,
"parent_item_id" => $parent,
"previous_item_id" => $previous,
"next_item_id" => $next,
"display_order" => $display_order +1,
"prerequisite" => $prerequisites,
"max_time_allowed" => $max_time_allowed
);
if ($prerequisites != 0) { if ($prerequisites != 0) {
$sql_ins = "INSERT INTO " . $tbl_lp_item . " ( $params['prerequisite'] = $prerequisites;
c_id,
lp_id, ".
"item_type, ".
"ref, ".
"title, ".
"description, ".
"path, ".
"max_score, ".
"parent_item_id, ".
"previous_item_id, ".
"next_item_id, ".
"display_order, ".
"prerequisite, ".
"max_time_allowed ".
") VALUES (
$course_id ,
".$this->get_id() . ", ".
"'" . $typeCleaned . "', ".
"'', ".
"'" . $title . "', ".
"'" . $description . "', ".
"'" . $id . "', ".
"'" . $max_score . "', ".
$parent . ", ".
$previous . ", ".
$next . ", ".
($display_order +1) . ", ".
$prerequisites . ", ".
$max_time_allowed .
")";
} else {
// Insert new item.
$sql_ins = "INSERT INTO " . $tbl_lp_item . " ( ".
"c_id, ".
"lp_id, ".
"item_type, ".
"ref, ".
"title, ".
"description, ".
"path, ".
"max_score, ".
"parent_item_id, ".
"previous_item_id, ".
"next_item_id, ".
"display_order, ".
"max_time_allowed ".
") VALUES (".
$course_id. ",".
$this->get_id() . ",".
"'" . $typeCleaned . "',".
"'',".
"'" . $title . "',".
"'" . $description . "',".
"'" . $id . "',".
"'" . $max_score . "',".
$parent . ",".
$previous . ",".
$next . ",".
($display_order +1) . ",".
$max_time_allowed .
")";
} }
$new_item_id = Database::insert($tbl_lp_item, $params);
if ($this->debug > 2) { if ($this->debug > 2) {
error_log('New LP - Inserting dokeos_chapter: ' . $sql_ins, 0); error_log('New LP - Inserting dokeos_chapter: ' . $new_item_id, 0);
} }
$res_ins = Database::query($sql_ins); if ($new_item_id) {
if ($res_ins > 0) {
$new_item_id = Database :: insert_id($res_ins);
// Update the item that should come after the new item. // Update the item that should come after the new item.
$sql_update_next = " $sql_update_next = "
@ -628,7 +582,7 @@ class learnpath
// Update all the items after the new item. // Update all the items after the new item.
$sql_update_order = " $sql_update_order = "
UPDATE " . $tbl_lp_item . " UPDATE " . $tbl_lp_item . "
SET display_order = display_order + 1 SET display_order = display_order + 1
WHERE WHERE
c_id = $course_id AND c_id = $course_id AND
lp_id = " . $this->get_id() . " AND lp_id = " . $this->get_id() . " AND
@ -643,72 +597,72 @@ class learnpath
SET ref = " . $new_item_id . " SET ref = " . $new_item_id . "
WHERE c_id = $course_id AND id = " . $new_item_id; WHERE c_id = $course_id AND id = " . $new_item_id;
Database::query($sql_update_ref); Database::query($sql_update_ref);
}
// Upload audio. // Upload audio.
if (!empty ($_FILES['mp3']['name'])) { if (!empty($_FILES['mp3']['name'])) {
// Create the audio folder if it does not exist yet. // Create the audio folder if it does not exist yet.
global $_course; global $_course;
$filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/'; $filepath = api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document/';
if (!is_dir($filepath . 'audio')) { if (!is_dir($filepath . 'audio')) {
mkdir($filepath . 'audio', api_get_permissions_for_new_directories()); mkdir($filepath . 'audio', api_get_permissions_for_new_directories());
$audio_id = add_document( $audio_id = add_document(
$_course,
'/audio',
'folder',
0,
'audio'
);
api_item_property_update(
$_course,
TOOL_DOCUMENT,
$audio_id,
'FolderCreated',
api_get_user_id(),
null,
null,
null,
null,
api_get_session_id()
);
api_item_property_update(
$_course,
TOOL_DOCUMENT,
$audio_id,
'invisible',
api_get_user_id(),
null,
null,
null,
null,
api_get_session_id()
);
}
// Upload the file in the documents tool.
include_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
$file_path = handle_uploaded_document(
$_course, $_course,
$_FILES['mp3'],
api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document',
'/audio', '/audio',
'folder',
0,
'audio'
);
api_item_property_update(
$_course,
TOOL_DOCUMENT,
$audio_id,
'FolderCreated',
api_get_user_id(),
null,
null,
null,
null,
api_get_session_id()
);
api_item_property_update(
$_course,
TOOL_DOCUMENT,
$audio_id,
'invisible',
api_get_user_id(), api_get_user_id(),
null, '',
null, '',
null, '',
null, '',
api_get_session_id() false
); );
}
// Upload the file in the documents tool. // Getting the filename only.
include_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php'; $file_components = explode('/', $file_path);
$file_path = handle_uploaded_document( $file = $file_components[count($file_components) - 1];
$_course,
$_FILES['mp3'],
api_get_path(SYS_COURSE_PATH) . $_course['path'] . '/document',
'/audio',
api_get_user_id(),
'',
'',
'',
'',
false
);
// Getting the filename only. // Store the mp3 file in the lp_item table.
$file_components = explode('/', $file_path); $sql = "UPDATE $tbl_lp_item SET
$file = $file_components[count($file_components) - 1];
// Store the mp3 file in the lp_item table.
$sql = "UPDATE $tbl_lp_item SET
audio = '" . Database::escape_string($file) . "' audio = '" . Database::escape_string($file) . "'
WHERE id = '" . Database::escape_string($new_item_id) . "'"; WHERE id = '" . Database::escape_string($new_item_id) . "'";
Database::query($sql); Database::query($sql);
}
} }
return $new_item_id; return $new_item_id;
@ -842,66 +796,96 @@ class learnpath
public function autocomplete_parents($item) public function autocomplete_parents($item)
{ {
$debug = $this->debug; $debug = $this->debug;
if ($debug) { if ($debug) {
error_log('Learnpath::autocomplete_parents()', 0); error_log('Learnpath::autocomplete_parents()', 0);
} }
if (empty($item)) { if (empty($item)) {
$item = $this->current; $item = $this->current;
} }
$parent_id = $this->items[$item]->get_parent();
if (isset($this->items[$item]) && is_object($this->items[$item]) and !empty($parent_id)) { $currentItem = $this->getItem($item);
// if $item points to an object and there is a parent. if ($currentItem) {
if ($debug) { $parent_id = $currentItem->get_parent();
error_log('Autocompleting parent of item ' . $item . ' (item ' . $parent_id . ')', 0); $parent = $this->getItem($parent_id);
} if ($parent) {
$current_item = $this->items[$item]; // if $item points to an object and there is a parent.
$parent = $this->items[$parent_id]; // Get the parent.
// New experiment including failed and browsed in completed status.
$current_status = $current_item->get_status();
//if ($current_item->is_done() || $current_status == 'browsed' || $current_status == 'failed') {
// Fixes chapter autocomplete
if (true) {
// If the current item is completed or passes or succeeded.
$completed = true;
if ($debug) { if ($debug) {
error_log('Status of current item is alright', 0); error_log(
'Autocompleting parent of item ' . $item . ' "'.$currentItem->get_title().'" (item ' . $parent_id . ' "'.$parent->get_title().'") ',
0
);
} }
foreach ($parent->get_children() as $child) { // New experiment including failed and browsed in completed status.
// Check all his brothers (parent's children) for completion status. //$current_status = $currentItem->get_status();
if ($child != $item) { //if ($currentItem->is_done() || $current_status == 'browsed' || $current_status == 'failed') {
if ($debug) { // Fixes chapter auto complete
error_log('Looking at brother with ID ' . $child . ', status is ' . $this->items[$child]->get_status(), 0); if (true) {
} // If the current item is completed or passes or succeeded.
//if($this->items[$child]->status_is(array('completed','passed','succeeded'))) $updateParentStatus = true;
// Trying completing parents of failed and browsed items as well. if ($debug) {
if ($this->items[$child]->status_is(array( error_log('Status of current item is alright', 0);
'completed', }
'passed',
'succeeded', foreach ($parent->get_children() as $childItemId) {
'browsed', $childItem = $this->getItem($childItemId);
'failed'
))) { // Check all his brothers (parent's children) for completion status.
// Keep completion status to true. if ($childItemId != $item) {
} else { if ($debug) {
if ($this->debug > 2) { error_log(
error_log('Found one incomplete child of ' . $parent_id . ': ' . $child . ' is ' . $this->items[$child]->get_status(), 0); 'Looking at brother #'.$childItemId . ' "' . $childItem->get_title() . '", status is ' . $childItem->get_status(),
0
);
}
// Trying completing parents of failed and browsed items as well.
if ($childItem->status_is(
array(
'completed',
'passed',
'succeeded',
'browsed',
'failed'
)
)
) {
// Keep completion status to true.
continue;
} else {
if ($debug > 2) {
error_log(
'Found one incomplete child of parent #' . $parent_id . ': child #'.$childItemId . ' "' . $childItem->get_title() . '", is ' . $childItem->get_status().' db_item_view_id:#'.$childItem->db_item_view_id,
0
);
}
$updateParentStatus = false;
break;
} }
$completed = false;
break;
} }
} }
}
if ($completed) { // If all the children were completed: if ($updateParentStatus) {
$parent->set_status('completed'); // If all the children were completed:
$parent->save(false, $this->prerequisites_match($parent->get_id())); $parent->set_status('completed');
$this->update_queue[$parent->get_id()] = $parent->get_status(); $parent->save(false, $this->prerequisites_match($parent->get_id()));
if ($debug) { //$this->update_queue[$parent->get_id()] = $parent->get_status();
error_log('Added parent to update queue ' . print_r($this->update_queue, true), 0); $this->update_queue[$parent->get_id()] = 'completed';
if ($debug) {
error_log(
'Added parent #'.$parent->get_id().' "'.$parent->get_title().'" to update queue status: completed '.
print_r($this->update_queue, 1),
0
);
}
// Recursive call.
$this->autocomplete_parents($parent->get_id());
} }
$this->autocomplete_parents($parent->get_id()); // Recursive call. }
} else {
if ($debug) {
error_log("Parent #$parent_id does not exists");
} }
} }
} else { } else {
@ -4012,49 +3996,61 @@ class learnpath
* and the prerequisite string on error. * and the prerequisite string on error.
* This function is based on the rules for aicc_script language as * This function is based on the rules for aicc_script language as
* described in the SCORM 1.2 CAM documentation page 108. * described in the SCORM 1.2 CAM documentation page 108.
* @param integer Optional item ID. If none given, uses the current open item. * @param integer $itemId Optional item ID. If none given, uses the current open item.
* @return boolean True if prerequisites are matched, false otherwise - * @return boolean True if prerequisites are matched, false otherwise -
* Empty string if true returned, prerequisites string otherwise. * Empty string if true returned, prerequisites string otherwise.
*/ */
public function prerequisites_match($item = null) public function prerequisites_match($itemId = null)
{ {
if ($this->debug > 0) { $debug = $this->debug;
if ($debug > 0) {
error_log('In learnpath::prerequisites_match()', 0); error_log('In learnpath::prerequisites_match()', 0);
} }
if (empty($item)) {
$item = $this->current; if (empty($itemId)) {
$itemId = $this->current;
} }
if (isset($this->items[$item]) && is_object($this->items[$item])) {
$currentItem = $this->getItem($itemId);
if ($currentItem) {
if ($this->type == 2) { if ($this->type == 2) {
//Getting prereq from scorm // Getting prereq from scorm
$prereq_string = $this->get_scorm_prereq_string($item); $prereq_string = $this->get_scorm_prereq_string($itemId);
} else { } else {
$prereq_string = $this->items[$item]->get_prereq_string(); $prereq_string = $currentItem->get_prereq_string();
} }
if (empty($prereq_string)) { if (empty($prereq_string)) {
if ($debug > 0) {
error_log('Found prereq_string is empty return true');
}
return true; return true;
} }
// Clean spaces. // Clean spaces.
$prereq_string = str_replace(' ', '', $prereq_string); $prereq_string = str_replace(' ', '', $prereq_string);
if ($this->debug > 0) { if ($debug > 0) {
error_log('Found prereq_string: ' . $prereq_string, 0); error_log('Found prereq_string: ' . $prereq_string, 0);
} }
// Now send to the parse_prereq() function that will check this component's prerequisites. // Now send to the parse_prereq() function that will check this component's prerequisites.
$result = $this->items[$item]->parse_prereq($prereq_string, $this->items, $this->refs_list, $this->get_user_id()); $result = $currentItem->parse_prereq(
$prereq_string,
$this->items,
$this->refs_list,
$this->get_user_id()
);
if ($result === false) { if ($result === false) {
$this->set_error_msg($this->items[$item]->prereq_alert); $this->set_error_msg($currentItem->prereq_alert);
} }
} else { } else {
$result = true; $result = true;
if ($this->debug > 1) { if ($debug > 1) {
error_log('$this->items[' . $item . '] was not an object', 0); error_log('$this->items[' . $itemId . '] was not an object', 0);
} }
} }
if ($this->debug > 1) { if ($debug > 1) {
error_log('End of prerequisites_match(). Error message is now ' . $this->error, 0); error_log('End of prerequisites_match(). Error message is now ' . $this->error, 0);
} }
return $result; return $result;
@ -9839,6 +9835,19 @@ EOD;
return $learnPath; return $learnPath;
} }
/**
* @param int $itemId
* @return learnpathItem|false
*/
public function getItem($itemId)
{
if (isset($this->items[$itemId]) && is_object($this->items[$itemId])) {
return $this->items[$itemId];
}
return false;
}
} }
if (!function_exists('trim_value')) { if (!function_exists('trim_value')) {

Loading…
Cancel
Save