Allow user to enter a LP item if it was validated in another session

Config added "validate_lp_prerequisite_from_other_session" BT#15930
pull/2970/head
Julio Montoya 6 years ago
parent 4e13c4d58d
commit 6719ca238f
  1. 3
      main/install/configuration.dist.php
  2. 4
      main/lp/learnpath.class.php
  3. 116
      main/lp/learnpathItem.class.php

@ -1274,6 +1274,9 @@ requires extension "php-soap" sudo apt-get install php-soap
// ] // ]
//]; //];
// Allow user to enter a LP item if it was validated in another session.
// $_configuration['validate_lp_prerequisite_from_other_session'] = false;
// KEEP THIS AT THE END // KEEP THIS AT THE END
// -------- Custom DB changes // -------- Custom DB changes
// Add user activation by confirmation email // Add user activation by confirmation email

@ -4443,7 +4443,7 @@ class learnpath
$debug = $this->debug; $debug = $this->debug;
if ($debug > 0) { if ($debug > 0) {
error_log('In learnpath::prerequisites_match()', 0); error_log('In learnpath::prerequisites_match()');
} }
if (empty($itemId)) { if (empty($itemId)) {
@ -4467,11 +4467,13 @@ class learnpath
return true; return true;
} }
// Clean spaces. // Clean spaces.
$prereq_string = str_replace(' ', '', $prereq_string); $prereq_string = str_replace(' ', '', $prereq_string);
if ($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 = $currentItem->parse_prereq( $result = $currentItem->parse_prereq(
$prereq_string, $prereq_string,

@ -539,7 +539,7 @@ class learnpathItem
c_id = $course_id AND c_id = $course_id AND
lp_item_id = ".$this->db_id." AND lp_item_id = ".$this->db_id." AND
lp_view_id = ".$this->view_id." AND lp_view_id = ".$this->view_id." AND
view_count = ".$this->attempt_id; view_count = ".$this->get_view_count();
$res = Database::query($sql); $res = Database::query($sql);
if (Database::num_rows($res) > 0) { if (Database::num_rows($res) > 0) {
$row = Database::fetch_array($res); $row = Database::fetch_array($res);
@ -688,9 +688,9 @@ class learnpathItem
['\r', '\n', "\\'"], ['\r', '\n', "\\'"],
$this->lesson_location $this->lesson_location
); );
} else {
return '';
} }
return '';
} }
/** /**
@ -860,8 +860,8 @@ class learnpathItem
if (!empty($this->lp_id)) { if (!empty($this->lp_id)) {
$table = Database::get_course_table(TABLE_LP_MAIN); $table = Database::get_course_table(TABLE_LP_MAIN);
$sql = "SELECT prevent_reinit $sql = "SELECT prevent_reinit
FROM $table FROM $table
WHERE iid = ".$this->lp_id; WHERE iid = ".$this->lp_id;
$res = Database::query($sql); $res = Database::query($sql);
if (Database::num_rows($res) < 1) { if (Database::num_rows($res) < 1) {
$this->error = "Could not find parent learnpath in lp table"; $this->error = "Could not find parent learnpath in lp table";
@ -2031,6 +2031,7 @@ class learnpathItem
// Deal with &, |, ~, =, <>, {}, ,, X*, () in reverse order. // Deal with &, |, ~, =, <>, {}, ,, X*, () in reverse order.
$this->prereq_alert = ''; $this->prereq_alert = '';
// First parse all parenthesis by using a sequential loop // First parse all parenthesis by using a sequential loop
// (looking for less-inclusives first). // (looking for less-inclusives first).
if ($prereqs_string == '_true_') { if ($prereqs_string == '_true_') {
@ -2556,6 +2557,17 @@ class learnpathItem
} else { } else {
$status = $itemToCheck->get_status(false); $status = $itemToCheck->get_status(false);
$returnstatus = $status == $this->possible_status[2] || $status == $this->possible_status[3]; $returnstatus = $status == $this->possible_status[2] || $status == $this->possible_status[3];
// Check results from another sessions.
$checkOtherSessions = api_get_configuration_value('validate_lp_prerequisite_from_other_session');
if ($checkOtherSessions && !$returnstatus) {
$returnstatus = $this->getStatusFromOtherSessions(
$user_id,
$prereqs_string,
$refs_list
);
}
if (!$returnstatus) { if (!$returnstatus) {
$explanation = sprintf( $explanation = sprintf(
get_lang('ItemXBlocksThisElement'), get_lang('ItemXBlocksThisElement'),
@ -2577,29 +2589,28 @@ class learnpathItem
} }
} }
if ($returnstatus && $this->prevent_reinit == 1) { $lp_item_view = Database::get_course_table(TABLE_LP_ITEM_VIEW);
// I would prefer check in the database. $lp_view = Database::get_course_table(TABLE_LP_VIEW);
$lp_item_view = Database::get_course_table(TABLE_LP_ITEM_VIEW);
$lp_view = Database::get_course_table(TABLE_LP_VIEW);
$sql = 'SELECT iid FROM '.$lp_view.' if ($returnstatus && $this->prevent_reinit == 1) {
$sql = "SELECT iid FROM $lp_view
WHERE WHERE
c_id = '.$course_id.' AND c_id = $course_id AND
user_id = '.$user_id.' AND user_id = $user_id AND
lp_id = '.$this->lp_id.' AND lp_id = $this->lp_id AND
session_id = '.$sessionId.' session_id = $sessionId
LIMIT 0, 1'; LIMIT 0, 1";
$rs_lp = Database::query($sql); $rs_lp = Database::query($sql);
if (Database::num_rows($rs_lp)) { if (Database::num_rows($rs_lp)) {
$lp_id = Database::fetch_row($rs_lp); $lp_id = Database::fetch_row($rs_lp);
$my_lp_id = $lp_id[0]; $my_lp_id = $lp_id[0];
$sql = 'SELECT status FROM '.$lp_item_view.' $sql = "SELECT status FROM $lp_item_view
WHERE WHERE
c_id = '.$course_id.' AND c_id = $course_id AND
lp_view_id = '.$my_lp_id.' AND lp_view_id = $my_lp_id AND
lp_item_id = '.$refs_list[$prereqs_string].' lp_item_id = $refs_list[$prereqs_string]
LIMIT 0, 1'; LIMIT 0, 1";
$rs_lp = Database::query($sql); $rs_lp = Database::query($sql);
$status_array = Database::fetch_row($rs_lp); $status_array = Database::fetch_row($rs_lp);
$status = $status_array[0]; $status = $status_array[0];
@ -2619,9 +2630,17 @@ class learnpathItem
error_log('New LP - Prerequisite '.$prereqs_string.' complete'); error_log('New LP - Prerequisite '.$prereqs_string.' complete');
} }
} }
}
return $returnstatus; if ($checkOtherSessions && $returnstatus === false) {
$returnstatus = $returnstatus = $this->getStatusFromOtherSessions(
$user_id,
$prereqs_string,
$refs_list
);
} }
return $returnstatus;
} else { } else {
return $returnstatus; return $returnstatus;
} }
@ -3780,7 +3799,7 @@ class learnpathItem
c_id = $course_id AND c_id = $course_id AND
lp_item_id = ".$this->db_id." AND lp_item_id = ".$this->db_id." AND
lp_view_id = ".$this->view_id." AND lp_view_id = ".$this->view_id." AND
view_count = ".intval($this->get_attempt_id()); view_count = ".$this->get_attempt_id();
if ($debug) { if ($debug) {
error_log( error_log(
'learnpathItem::write_to_db() - Querying item_view: '.$sql, 'learnpathItem::write_to_db() - Querying item_view: '.$sql,
@ -3820,7 +3839,7 @@ class learnpathItem
Database::query($sql); Database::query($sql);
} }
} else { } else {
if ($this->type == 'hotpotatoes') { if ($this->type === 'hotpotatoes') {
$params = [ $params = [
'total_time' => $this->get_total_time(), 'total_time' => $this->get_total_time(),
'start_time' => $this->get_current_start_time(), 'start_time' => $this->get_current_start_time(),
@ -4559,4 +4578,55 @@ class learnpathItem
{ {
return $this->iId; return $this->iId;
} }
/**
* @param int $user_id
* @param string $prereqs_string
* @param array $refs_list
*
* @return bool
*/
public function getStatusFromOtherSessions($user_id, $prereqs_string, $refs_list)
{
$lp_item_view = Database::get_course_table(TABLE_LP_ITEM_VIEW);
$lp_view = Database::get_course_table(TABLE_LP_VIEW);
$course_id = api_get_course_int_id();
$user_id = (int) $user_id;
// Check results from another sessions:
$checkOtherSessions = api_get_configuration_value('validate_lp_prerequisite_from_other_session');
if ($checkOtherSessions) {
// Check items
$sql = "SELECT iid FROM $lp_view
WHERE
c_id = $course_id AND
user_id = $user_id AND
lp_id = $this->lp_id AND
session_id <> 0
";
$result = Database::query($sql);
$resultFromOtherSessions = false;
while ($row = Database::fetch_array($result)) {
$lpIid = $row['iid'];
$sql = "SELECT status FROM $lp_item_view
WHERE
c_id = $course_id AND
lp_view_id = $lpIid AND
lp_item_id = $refs_list[$prereqs_string]
LIMIT 1";
$resultRow = Database::query($sql);
if (Database::num_rows($resultRow)) {
$statusResult = Database::fetch_array($resultRow);
$status = $statusResult['status'];
$checked = $status == $this->possible_status[2] || $status == $this->possible_status[3];
if ($checked) {
$resultFromOtherSessions = true;
break;
}
}
}
return $resultFromOtherSessions;
}
}
} }

Loading…
Cancel
Save