From cc3f864a972d23569c97c9c335926b3688e7d4f2 Mon Sep 17 00:00:00 2001
From: Andrew Downes <mrdownes@hotmail.com>
Date: Mon, 7 Jan 2019 17:00:02 +0000
Subject: [PATCH] fix(mod_book/chapter_viewed): Statements for chapters without
 parents (#388)

---
 .../events/mod_book/chapter_viewed.php        |  2 +-
 .../data.json                                 |  0
 .../event.json                                |  0
 .../statements.json                           |  0
 .../test.php                                  |  2 +-
 .../data.json                                 | 46 ++++++++++
 .../event.json                                | 10 +++
 .../statements.json                           | 84 +++++++++++++++++++
 .../test.php                                  | 24 ++++++
 9 files changed, 166 insertions(+), 2 deletions(-)
 rename tests/mod_book/chapter_viewed/{existing_chapter_viewed => existing_chapter_viewed_with_parent}/data.json (100%)
 rename tests/mod_book/chapter_viewed/{existing_chapter_viewed => existing_chapter_viewed_with_parent}/event.json (100%)
 rename tests/mod_book/chapter_viewed/{existing_chapter_viewed => existing_chapter_viewed_with_parent}/statements.json (100%)
 rename tests/mod_book/chapter_viewed/{existing_chapter_viewed => existing_chapter_viewed_with_parent}/test.php (91%)
 create mode 100644 tests/mod_book/chapter_viewed/existing_chapter_viewed_without_parent/data.json
 create mode 100644 tests/mod_book/chapter_viewed/existing_chapter_viewed_without_parent/event.json
 create mode 100644 tests/mod_book/chapter_viewed/existing_chapter_viewed_without_parent/statements.json
 create mode 100644 tests/mod_book/chapter_viewed/existing_chapter_viewed_without_parent/test.php

diff --git a/src/transformer/events/mod_book/chapter_viewed.php b/src/transformer/events/mod_book/chapter_viewed.php
index ca17de5..87dcc20 100644
--- a/src/transformer/events/mod_book/chapter_viewed.php
+++ b/src/transformer/events/mod_book/chapter_viewed.php
@@ -61,7 +61,7 @@ function chapter_viewed(array $config, \stdClass $event) {
         ]
     ];
 
-    if ($chapter->subchapter !== 0) {
+    if ($chapter->subchapter != '0') {
         $parentchapter = $repo->read_record_by_id('book_chapters', $chapter->subchapter);
         $statement['context']['contextActivities']['parent'] = [
             utils\get_activity\book_chapter($config, $course, $parentchapter, $event->contextinstanceid)
diff --git a/tests/mod_book/chapter_viewed/existing_chapter_viewed/data.json b/tests/mod_book/chapter_viewed/existing_chapter_viewed_with_parent/data.json
similarity index 100%
rename from tests/mod_book/chapter_viewed/existing_chapter_viewed/data.json
rename to tests/mod_book/chapter_viewed/existing_chapter_viewed_with_parent/data.json
diff --git a/tests/mod_book/chapter_viewed/existing_chapter_viewed/event.json b/tests/mod_book/chapter_viewed/existing_chapter_viewed_with_parent/event.json
similarity index 100%
rename from tests/mod_book/chapter_viewed/existing_chapter_viewed/event.json
rename to tests/mod_book/chapter_viewed/existing_chapter_viewed_with_parent/event.json
diff --git a/tests/mod_book/chapter_viewed/existing_chapter_viewed/statements.json b/tests/mod_book/chapter_viewed/existing_chapter_viewed_with_parent/statements.json
similarity index 100%
rename from tests/mod_book/chapter_viewed/existing_chapter_viewed/statements.json
rename to tests/mod_book/chapter_viewed/existing_chapter_viewed_with_parent/statements.json
diff --git a/tests/mod_book/chapter_viewed/existing_chapter_viewed/test.php b/tests/mod_book/chapter_viewed/existing_chapter_viewed_with_parent/test.php
similarity index 91%
rename from tests/mod_book/chapter_viewed/existing_chapter_viewed/test.php
rename to tests/mod_book/chapter_viewed/existing_chapter_viewed_with_parent/test.php
index ea64fbc..4fed29c 100644
--- a/tests/mod_book/chapter_viewed/existing_chapter_viewed/test.php
+++ b/tests/mod_book/chapter_viewed/existing_chapter_viewed_with_parent/test.php
@@ -14,7 +14,7 @@
 // You should have received a copy of the GNU General Public License
 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
 
-namespace tests\mod_book\chapter_viewed\existing_module;
+namespace tests\mod_book\chapter_viewed\existing_chapter_viewed_with_parent;
 defined('MOODLE_INTERNAL') || die();
 
 class test extends \tests\xapi_test_case {
diff --git a/tests/mod_book/chapter_viewed/existing_chapter_viewed_without_parent/data.json b/tests/mod_book/chapter_viewed/existing_chapter_viewed_without_parent/data.json
new file mode 100644
index 0000000..54e5450
--- /dev/null
+++ b/tests/mod_book/chapter_viewed/existing_chapter_viewed_without_parent/data.json
@@ -0,0 +1,46 @@
+{
+    "user": [
+        {
+            "id": 1,
+            "firstname": "test_fullname",
+            "email": "test@test.com"
+        }
+    ],
+    "course": [
+        {
+            "id": 1,
+            "fullname": "test_name",
+            "lang": "en"
+        }
+    ],
+    "course_modules": [
+        {
+            "id": 1,
+            "course": 1,
+            "module": 1,
+            "instance": 1
+        }
+    ],
+    "modules": [
+        {
+            "id": 1,
+            "name": "book"
+        }
+    ],
+    "book": [
+        {
+            "id": 1,
+            "name": "test_book_name"
+        }
+    ],
+    "book_chapters": [
+        {
+            "id": 1,
+            "bookid": "1",
+            "pagenum": "1",
+            "subchapter": "0",
+            "title": "test_book_chapter_title",
+            "content": "<p>test_book_chapter_content</p>"
+        }
+    ]
+}
\ No newline at end of file
diff --git a/tests/mod_book/chapter_viewed/existing_chapter_viewed_without_parent/event.json b/tests/mod_book/chapter_viewed/existing_chapter_viewed_without_parent/event.json
new file mode 100644
index 0000000..a688f8f
--- /dev/null
+++ b/tests/mod_book/chapter_viewed/existing_chapter_viewed_without_parent/event.json
@@ -0,0 +1,10 @@
+{
+    "id": 1,
+    "userid": 1,
+    "courseid": 1,
+    "timecreated": 1433946701,
+    "objecttable": "book_chapters",
+    "objectid": 1,
+    "contextinstanceid": 1,
+    "eventname": "\\mod_book\\event\\chapter_viewed"
+}
\ No newline at end of file
diff --git a/tests/mod_book/chapter_viewed/existing_chapter_viewed_without_parent/statements.json b/tests/mod_book/chapter_viewed/existing_chapter_viewed_without_parent/statements.json
new file mode 100644
index 0000000..dc400ff
--- /dev/null
+++ b/tests/mod_book/chapter_viewed/existing_chapter_viewed_without_parent/statements.json
@@ -0,0 +1,84 @@
+[
+    {
+        "actor": {
+            "name": "test_fullname",
+            "account": {
+                "homePage": "http:\/\/www.example.org",
+                "name": "1"
+            }
+        },
+        "verb": {
+            "id": "http:\/\/id.tincanapi.com\/verb\/viewed",
+            "display": {
+                "en": "viewed"
+            }
+        },
+        "object": {
+            "id": "http:\/\/www.example.org\/mod\/book\/view.php?id=1&chapterid=1",
+            "definition": {
+                "type": "http:\/\/id.tincanapi.com\/activitytype\/chapter",
+                "name": {
+                    "en": "test_book_chapter_title"
+                },
+                "description": {
+                    "en": "test_book_chapter_content"
+                }
+            }
+        },
+        "timestamp": "2015-06-10T15:31:41+01:00",
+        "context": {
+            "platform": "Moodle",
+            "language": "en",
+            "extensions": {
+                "http:\/\/lrs.learninglocker.net\/define\/extensions\/info": {
+                    "http:\/\/moodle.org": "1.0.0",
+                    "https:\/\/github.com\/xAPI-vle\/moodle-logstore_xapi": "0.0.0-development",
+                    "event_name": "\\mod_book\\event\\chapter_viewed",
+                    "event_function": "\\src\\transformer\\events\\mod_book\\chapter_viewed"
+                }
+            },
+            "contextActivities": {
+                "grouping": [
+                    {
+                        "id": "http:\/\/www.example.org",
+                        "definition": {
+                            "type": "http:\/\/id.tincanapi.com\/activitytype\/lms",
+                            "name": {
+                                "en": "test_name"
+                            }
+                        }
+                    },
+                    {
+                        "id": "http:\/\/www.example.org\/course\/view.php?id=1",
+                        "definition": {
+                            "type": "http:\/\/id.tincanapi.com\/activitytype\/lms\/course",
+                            "name": {
+                                "en": "test_name"
+                            }
+                        }
+                    },
+                    {
+                        "id": "http:\/\/www.example.org\/mod\/book\/view.php?id=1",
+                        "definition": {
+                            "type": "http:\/\/id.tincanapi.com\/activitytype\/book",
+                            "name": {
+                                "en": "test_book_name"
+                            }
+                        }
+                    }
+                ],
+                "category": [
+                    {
+                        "id": "http:\/\/moodle.org",
+                        "definition": {
+                            "type": "http:\/\/id.tincanapi.com\/activitytype\/source",
+                            "name": {
+                                "en": "Moodle"
+                            }
+                        }
+                    }
+                ]
+            }
+        }
+    }
+]
\ No newline at end of file
diff --git a/tests/mod_book/chapter_viewed/existing_chapter_viewed_without_parent/test.php b/tests/mod_book/chapter_viewed/existing_chapter_viewed_without_parent/test.php
new file mode 100644
index 0000000..0694f3d
--- /dev/null
+++ b/tests/mod_book/chapter_viewed/existing_chapter_viewed_without_parent/test.php
@@ -0,0 +1,24 @@
+<?php
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+namespace tests\mod_book\chapter_viewed\existing_chapter_viewed_without_parent;
+defined('MOODLE_INTERNAL') || die();
+
+class test extends \tests\xapi_test_case {
+    protected function get_test_dir() {
+        return __DIR__;
+    }
+}
\ No newline at end of file
-- 
GitLab