Browse Source

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

Config added "validate_lp_prerequisite_from_other_session" BT#15930
Julio Montoya 5 years ago
parent
commit
6719ca238f
3 changed files with 99 additions and 24 deletions
  1. 3 0
      main/install/configuration.dist.php
  2. 3 1
      main/lp/learnpath.class.php
  3. 93 23
      main/lp/learnpathItem.class.php

+ 3 - 0
main/install/configuration.dist.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
 // -------- Custom DB changes
 // Add user activation by confirmation email

+ 3 - 1
main/lp/learnpath.class.php

@@ -4443,7 +4443,7 @@ class learnpath
 
         $debug = $this->debug;
         if ($debug > 0) {
-            error_log('In learnpath::prerequisites_match()', 0);
+            error_log('In learnpath::prerequisites_match()');
         }
 
         if (empty($itemId)) {
@@ -4467,11 +4467,13 @@ class learnpath
 
                 return true;
             }
+
             // Clean spaces.
             $prereq_string = str_replace(' ', '', $prereq_string);
             if ($debug > 0) {
                 error_log('Found prereq_string: '.$prereq_string, 0);
             }
+
             // Now send to the parse_prereq() function that will check this component's prerequisites.
             $result = $currentItem->parse_prereq(
                 $prereq_string,

+ 93 - 23
main/lp/learnpathItem.class.php

@@ -539,7 +539,7 @@ class learnpathItem
                     c_id = $course_id AND
                     lp_item_id = ".$this->db_id." AND
                     lp_view_id = ".$this->view_id." AND
-                    view_count = ".$this->attempt_id;
+                    view_count = ".$this->get_view_count();
         $res = Database::query($sql);
         if (Database::num_rows($res) > 0) {
             $row = Database::fetch_array($res);
@@ -688,9 +688,9 @@ class learnpathItem
                 ['\r', '\n', "\\'"],
                 $this->lesson_location
             );
-        } else {
-            return '';
         }
+
+        return '';
     }
 
     /**
@@ -860,8 +860,8 @@ class learnpathItem
             if (!empty($this->lp_id)) {
                 $table = Database::get_course_table(TABLE_LP_MAIN);
                 $sql = "SELECT prevent_reinit
-                    FROM $table
-                    WHERE iid = ".$this->lp_id;
+                        FROM $table
+                        WHERE iid = ".$this->lp_id;
                 $res = Database::query($sql);
                 if (Database::num_rows($res) < 1) {
                     $this->error = "Could not find parent learnpath in lp table";
@@ -2031,6 +2031,7 @@ class learnpathItem
 
         // Deal with &, |, ~, =, <>, {}, ,, X*, () in reverse order.
         $this->prereq_alert = '';
+
         // First parse all parenthesis by using a sequential loop
         //  (looking for less-inclusives first).
         if ($prereqs_string == '_true_') {
@@ -2556,6 +2557,17 @@ class learnpathItem
                                     } else {
                                         $status = $itemToCheck->get_status(false);
                                         $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) {
                                             $explanation = sprintf(
                                                 get_lang('ItemXBlocksThisElement'),
@@ -2577,29 +2589,28 @@ class learnpathItem
                                             }
                                         }
 
-                                        if ($returnstatus && $this->prevent_reinit == 1) {
-                                            // I would prefer check in the database.
-                                            $lp_item_view = Database::get_course_table(TABLE_LP_ITEM_VIEW);
-                                            $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
-                                                        c_id = '.$course_id.' AND
-                                                        user_id = '.$user_id.'  AND
-                                                        lp_id = '.$this->lp_id.' AND
-                                                        session_id = '.$sessionId.'
-                                                    LIMIT 0, 1';
+                                                        c_id = $course_id AND
+                                                        user_id = $user_id  AND
+                                                        lp_id = $this->lp_id AND
+                                                        session_id = $sessionId
+                                                    LIMIT 0, 1";
                                             $rs_lp = Database::query($sql);
                                             if (Database::num_rows($rs_lp)) {
                                                 $lp_id = Database::fetch_row($rs_lp);
                                                 $my_lp_id = $lp_id[0];
 
-                                                $sql = 'SELECT status FROM '.$lp_item_view.'
+                                                $sql = "SELECT status FROM $lp_item_view
                                                         WHERE
-                                                            c_id = '.$course_id.' AND
-                                                            lp_view_id = '.$my_lp_id.' AND
-                                                            lp_item_id = '.$refs_list[$prereqs_string].'
-                                                        LIMIT 0, 1';
+                                                            c_id = $course_id AND
+                                                            lp_view_id = $my_lp_id AND
+                                                            lp_item_id = $refs_list[$prereqs_string]
+                                                        LIMIT 0, 1";
                                                 $rs_lp = Database::query($sql);
                                                 $status_array = Database::fetch_row($rs_lp);
                                                 $status = $status_array[0];
@@ -2619,9 +2630,17 @@ class learnpathItem
                                                         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 {
                                             return $returnstatus;
                                         }
@@ -3780,7 +3799,7 @@ class learnpathItem
                         c_id = $course_id AND
                         lp_item_id = ".$this->db_id." AND
                         lp_view_id = ".$this->view_id." AND
-                        view_count = ".intval($this->get_attempt_id());
+                        view_count = ".$this->get_attempt_id();
             if ($debug) {
                 error_log(
                     'learnpathItem::write_to_db() - Querying item_view: '.$sql,
@@ -3820,7 +3839,7 @@ class learnpathItem
                     Database::query($sql);
                 }
             } else {
-                if ($this->type == 'hotpotatoes') {
+                if ($this->type === 'hotpotatoes') {
                     $params = [
                         'total_time' => $this->get_total_time(),
                         'start_time' => $this->get_current_start_time(),
@@ -4559,4 +4578,55 @@ class learnpathItem
     {
         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;
+        }
+    }
 }