Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct ids relation in the degree programs overview shortcode #8

Merged
merged 3 commits into from
May 31, 2024

Conversation

zhyian
Copy link
Collaborator

@zhyian zhyian commented May 29, 2024

Please check if the PR fulfills these requirements

  • The commit message follows our guidelines
  • Tests for the changes have been added (for bug fixes/features)
  • Docs have been added/updated (for bug fixes/features)

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
Bug fix

What is the current behavior? (You can also link to an open issue here)
Currently separating HIS codes by a comma in the ids attribute connects them by AND (source). This makes it impossible to display programs using multiple codes.

What is the new behavior (if this is a feature change)?
The relation is changed to OR. Introduced the possibility to set any relation in the taxonomy parameter tax_query.

Does this PR introduce a breaking change? (What changes might users need to make in their application due to this PR?)
No

Other information:

@zhyian zhyian requested review from shvlv, tyrann0us and o-samaras May 29, 2024 09:18
Copy link
Collaborator

@o-samaras o-samaras left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. LGTM!

Copy link
Collaborator

@tyrann0us tyrann0us left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this! Approving because code-wise LGTM. My local data is quite unusable, so could you please confirm that comma-separated combinations of HIS codes with varying campo key values and lengths (i.e., number of campo keys) work as expected? Thank you!

@zhyian zhyian changed the title fix: correct ids relation in the degree programs overview shortcode Correct ids relation in the degree programs overview shortcode May 30, 2024
Copy link
Contributor

@shvlv shvlv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit afraid of such changes. I think the safer way would be to have HIS subquery:

Index: src/Infrastructure/Repository/WpQueryArgsBuilder.php
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/Infrastructure/Repository/WpQueryArgsBuilder.php b/src/Infrastructure/Repository/WpQueryArgsBuilder.php
--- a/src/Infrastructure/Repository/WpQueryArgsBuilder.php	(revision 4a72cbaf0318d8804397f2d94b6884a695bbd7af)
+++ b/src/Infrastructure/Repository/WpQueryArgsBuilder.php	(date 1717078209710)
@@ -87,37 +87,52 @@
             $queryArgs = $this->applyFilter($filter, $queryArgs, $criteria->languageCode());
         }
 
-        foreach ($criteria->hisCodes() as $hisCode) {
-            $queryArgs = $this->applyHisCode($hisCode, $queryArgs);
+        if (count($criteria->hisCodes()) > 0) {
+            $queryArgs = $this->applyHisCodes($criteria->hisCodes(), $queryArgs);
         }
 
         return $queryArgs;
     }
 
-    public function applyHisCode(string $hisCode, WpQueryArgs $queryArgs): WpQueryArgs
+    /**
+     * @param array<string> $hisCodes
+     */
+    public function applyHisCodes(array $hisCodes, WpQueryArgs $queryArgs): WpQueryArgs
     {
-        $taxQueryItem = [
-            'relation' => 'AND',
-        ];
+        $hisCodesQuery = [];
+
+        foreach ($hisCodes as $hisCode) {
+            $taxQueryItem = [
+                'relation' => 'AND',
+            ];
 
-        $taxonomyToTermMapping = $this->campoKeysRepository->taxonomyToTermsMapFromCampoKeys(
-            CampoKeys::fromHisCode($hisCode)
-        );
+            $taxonomyToTermMapping = $this->campoKeysRepository->taxonomyToTermsMapFromCampoKeys(
+                CampoKeys::fromHisCode($hisCode)
+            );
 
-        if (count($taxonomyToTermMapping) === 0) {
-            return $queryArgs;
-        }
+            if (count($taxonomyToTermMapping) === 0) {
+                continue;
+            }
 
-        foreach ($taxonomyToTermMapping as $taxonomy => $termId) {
-            $taxQueryItem[] = [
-                'taxonomy' => $taxonomy,
-                'terms' => [
-                    $termId,
-                ],
-            ];
-        }
+            foreach ($taxonomyToTermMapping as $taxonomy => $termId) {
+                $taxQueryItem[] = [
+                    'taxonomy' => $taxonomy,
+                    'terms' => [
+                        $termId,
+                    ],
+                ];
+            }
 
-        return $queryArgs->withTaxQueryItem($taxQueryItem);
+            $hisCodesQuery[] = $taxQueryItem;
+        }
+
+        if (count($hisCodesQuery) === 0) {
+            return $queryArgs;
+        }
+
+        $hisCodesQuery['relation'] = 'OR';
+
+        return $queryArgs->withTaxQueryItem($hisCodesQuery);
     }
 
     private function applyOrderBy(

Does it work?

@zhyian
Copy link
Collaborator Author

zhyian commented May 30, 2024

Thanks for the suggestion. @tyrann0us, @o-samaras, could you please review updated PR? Thanks a lot.

@zhyian zhyian requested review from tyrann0us and o-samaras May 30, 2024 15:14
Copy link
Collaborator

@tyrann0us tyrann0us left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM; it works as before for my little test set (ids="85,86"). Thanks for taking care of this, @zhyian and @shvlv! 🙏🏽

@zhyian zhyian merged commit 0089bff into dev May 31, 2024
8 checks passed
@zhyian zhyian deleted the bugfix/ids-relation branch May 31, 2024 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants