From 41e8315257336c337e861e93c062b175e9fcf113 Mon Sep 17 00:00:00 2001
From: Mike Henry <henry_mike@bah.com>
Date: Fri, 31 Mar 2023 12:31:38 -0400
Subject: [PATCH] Issue #80 move $commentid loop

---
 lib.php | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/lib.php b/lib.php
index 84281b8..9800f1c 100644
--- a/lib.php
+++ b/lib.php
@@ -376,13 +376,13 @@ function pdfannotator_pluginfile($course, $cm, $context, $filearea, $args, $forc
         return false;
     }
 
-    $commentid = 0;
-    foreach ($args as $param) {
-        if ($DB->record_exists('pdfannotator_comments', ['id' => $param])) {
-            $commentid = $param;
-            break;
-        }
-    }
+    // $commentid = 0;
+    // foreach ($args as $param) {
+    //     if ($DB->record_exists('pdfannotator_comments', ['id' => $param])) {
+    //         $commentid = $param;
+    //         break;
+    //     }
+    // }
     array_shift($args); // Ignore revision - designed to prevent caching problems only.
 
     $fs = get_file_storage();
@@ -423,6 +423,14 @@ function pdfannotator_pluginfile($course, $cm, $context, $filearea, $args, $forc
     }
 
     if ($filearea === 'post') {
+        $commentid = 0;
+        foreach ($args as $param) {
+            if ($DB->record_exists('pdfannotator_comments', ['id' => $param])) {
+                $commentid = $param;
+                break;
+            }
+        }
+           
         $fullpath = rtrim("/$context->id/mod_pdfannotator/$filearea/$commentid/$relativepath", '/');
         if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
             //Annotations from other documents might have another contextid.
-- 
GitLab