MDL-49470 mod_lesson: Triggering just 1 event
authorDavid Monllao <davidm@moodle.com>
Wed, 25 Mar 2015 07:32:54 +0000 (15:32 +0800)
committerDavid Monllao <davidm@moodle.com>
Wed, 25 Mar 2015 07:33:07 +0000 (15:33 +0800)
mod/lesson/lesson.php
mod/lesson/locallib.php
mod/lesson/tests/events_test.php

index fb4314f..390c29c 100644 (file)
@@ -126,45 +126,7 @@ switch ($action) {
     case 'moveit':
         $after = (int)required_param('after', PARAM_INT); // target page
 
-        $pages = $lesson->load_all_pages();
-
-        if (!array_key_exists($pageid, $pages) || ($after!=0 && !array_key_exists($after, $pages))) {
-            print_error('cannotfindpages', 'lesson', "$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id");
-        }
-        $pagetomove = clone($pages[$pageid]);
-        unset($pages[$pageid]);
-
-        $pageids = array();
-        if ($after === 0) {
-            $pageids['p0'] = $pageid;
-        }
-        foreach ($pages as $page) {
-            $pageids[] = $page->id;
-            if ($page->id == $after) {
-                $pageids[] = $pageid;
-            }
-        }
-
-        $pageidsref = $pageids;
-        reset($pageidsref);
-        $prev = 0;
-        $next = next($pageidsref);
-        foreach ($pageids as $pid) {
-            if ($pid === $pageid) {
-                $page = $pagetomove;
-            } else {
-                $page = $pages[$pid];
-            }
-            if ($page->prevpageid != $prev || $page->nextpageid != $next) {
-                $page->move($next, $prev);
-            }
-            $prev = $page->id;
-            $next = next($pageidsref);
-            if (!$next) {
-                $next = 0;
-            }
-        }
-
+        $lesson->resort_pages($pageid, $after);
         redirect("$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id");
         break;
     default:
index f272c52..f678345 100644 (file)
@@ -1645,6 +1645,82 @@ class lesson extends lesson_base {
             $pageid = $pages[$pageid]->prevpageid;
         }
     }
+
+    /**
+     * Move a page resorting all other pages.
+     *
+     * @param int $pageid
+     * @param int $after
+     * @return void
+     */
+    public function resort_pages($pageid, $after) {
+        global $CFG;
+
+        $cm = get_coursemodule_from_instance('lesson', $this->properties->id, $this->properties->course);
+        $context = context_module::instance($cm->id);
+
+        $pages = $this->load_all_pages();
+
+        if (!array_key_exists($pageid, $pages) || ($after!=0 && !array_key_exists($after, $pages))) {
+            print_error('cannotfindpages', 'lesson', "$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id");
+        }
+
+        $pagetomove = clone($pages[$pageid]);
+        unset($pages[$pageid]);
+
+        $pageids = array();
+        if ($after === 0) {
+            $pageids['p0'] = $pageid;
+        }
+        foreach ($pages as $page) {
+            $pageids[] = $page->id;
+            if ($page->id == $after) {
+                $pageids[] = $pageid;
+            }
+        }
+
+        $pageidsref = $pageids;
+        reset($pageidsref);
+        $prev = 0;
+        $next = next($pageidsref);
+        foreach ($pageids as $pid) {
+            if ($pid === $pageid) {
+                $page = $pagetomove;
+            } else {
+                $page = $pages[$pid];
+            }
+            if ($page->prevpageid != $prev || $page->nextpageid != $next) {
+                $page->move($next, $prev);
+
+                if ($pid === $pageid) {
+                    // We will trigger an event.
+                    $pageupdated = array('next' => $next, 'prev' => $prev);
+                }
+            }
+
+            $prev = $page->id;
+            $next = next($pageidsref);
+            if (!$next) {
+                $next = 0;
+            }
+        }
+
+        // Trigger an event: page moved.
+        if (!empty($pageupdated)) {
+            $eventparams = array(
+                'context' => $context,
+                'objectid' => $pageid,
+                'other' => array(
+                    'pagetype' => $page->get_typestring(),
+                    'prevpageid' => $pageupdated['prev'],
+                    'nextpageid' => $pageupdated['next']
+                )
+            );
+            $event = \mod_lesson\event\page_moved::create($eventparams);
+            $event->trigger();
+        }
+
+    }
 }
 
 
@@ -2022,22 +2098,6 @@ abstract class lesson_page extends lesson_base {
         $obj->prevpageid = $prevpageid;
         $obj->nextpageid = $nextpageid;
         $DB->update_record('lesson_pages', $obj);
-
-        $cm = get_coursemodule_from_instance('lesson', $this->lesson->id, $this->lesson->course);
-        $context = context_module::instance($cm->id);
-
-        // Trigger an event: page moved.
-        $eventparams = array(
-            'context' => $context,
-            'objectid' => $this->properties->id,
-            'other' => array(
-                'pagetype' => $this->get_typestring(),
-                'prevpageid' => $prevpageid,
-                'nextpageid' => $nextpageid
-                )
-            );
-        $event = \mod_lesson\event\page_moved::create($eventparams);
-        $event->trigger();
     }
 
     /**
index 012e550..1bb5025 100644 (file)
@@ -84,6 +84,7 @@ class mod_lesson_events_testcase extends advanced_testcase {
     public function test_page_moved() {
 
         // Set up a generator to create content.
+        // paga3 is the first one and page1 the last one.
         $generator = $this->getDataGenerator()->get_plugin_generator('mod_lesson');
         $pagerecord1 = $generator->create_content($this->lesson);
         $page1 = $this->lesson->load_page($pagerecord1->id);
@@ -93,16 +94,17 @@ class mod_lesson_events_testcase extends advanced_testcase {
         $page3 = $this->lesson->load_page($pagerecord3->id);
         // Trigger and capture the event.
         $sink = $this->redirectEvents();
-        $page3->move($pagerecord2->id, $pagerecord1->id);
+        $this->lesson->resort_pages($page3->id, $pagerecord2->id);
         // Get our event event.
         $events = $sink->get_events();
         $event = reset($events);
 
+        $this->assertCount(1, $events);
         // Check that the event data is valid.
         $this->assertInstanceOf('\mod_lesson\event\page_moved', $event);
         $this->assertEquals($page3->id, $event->objectid);
-        $this->assertEquals($pagerecord2->id, $event->other['nextpageid']);
-        $this->assertEquals($pagerecord1->id, $event->other['prevpageid']);
+        $this->assertEquals($pagerecord1->id, $event->other['nextpageid']);
+        $this->assertEquals($pagerecord2->id, $event->other['prevpageid']);
         $this->assertEventContextNotUsed($event);
         $this->assertDebuggingNotCalled();
     }