Ver código fonte

Allow add hooks when creating course - refs BT#14552

Angel Fernando Quiroz Campos 6 anos atrás
pai
commit
6a21f38c32

+ 8 - 0
main/inc/lib/course.lib.php

@@ -41,6 +41,9 @@ class CourseManager
     public static function create_course($params, $authorId = 0)
     {
         global $_configuration;
+
+        $hook = HookCreateCourse::create();
+
         // Check portal limits
         $access_url_id = 1;
         if (api_get_multiple_access_url()) {
@@ -98,6 +101,11 @@ class CourseManager
                 $course_id = AddCourse::register_course($params);
                 $course_info = api_get_course_info_by_id($course_id);
 
+                if ($hook) {
+                    $hook->setEventData(['course_info' => $course_info]);
+                    $hook->notifyCreateCourse(HOOK_EVENT_TYPE_POST);
+                }
+
                 if (!empty($course_info)) {
                     self::fillCourse($course_info, $params, $authorId);
 

+ 37 - 0
main/inc/lib/hook/HookCreateCourse.php

@@ -0,0 +1,37 @@
+<?php
+/* For licensing terms, see /license.txt */
+
+/**
+ * Class HookCreateCourse.
+ */
+class HookCreateCourse extends HookEvent implements HookCreateCourseEventInterface
+{
+    /**
+     * HookCreateCourse constructor.
+     *
+     * @throws Exception
+     */
+    protected function __construct()
+    {
+        parent::__construct('HookCreateCourse');
+    }
+
+    /**
+     * Update all the observers.
+     *
+     * @param int $type
+     *
+     * @return int
+     */
+    public function notifyCreateCourse($type)
+    {
+        $this->eventData['type'] = $type;
+
+        /** @var HookCreateCourseObserverInterface $observer */
+        foreach ($this->observers as $observer) {
+            $observer->hookCreateCourse($this);
+        }
+
+        return 1;
+    }
+}

+ 23 - 0
main/inc/lib/hook/interfaces/HookCreateCourseEventInterface.php

@@ -0,0 +1,23 @@
+<?php
+/* For licensing terms, see /license.txt */
+/**
+ * This file contains all Hook interfaces and their relation.
+ * They are used for Hook classes.
+ *
+ * @package chamilo.library.hook
+ */
+
+/**
+ * Interface HookCreateUserEventInterface.
+ */
+interface HookCreateCourseEventInterface extends HookEventInterface
+{
+    /**
+     * Update all the observers.
+     *
+     * @param int $type
+     *
+     * @return int
+     */
+    public function notifyCreateCourse($type);
+}

+ 21 - 0
main/inc/lib/hook/interfaces/HookCreateCourseObserverInterface.php

@@ -0,0 +1,21 @@
+<?php
+/* For licensing terms, see /license.txt */
+/**
+ * This file contains all Hook interfaces and their relation.
+ * They are used for Hook classes.
+ *
+ * @package chamilo.library.hook
+ */
+
+/**
+ * Interface CreateUserHookInterface.
+ */
+interface HookCreateCourseObserverInterface extends HookObserverInterface
+{
+    /**
+     * @param HookCreateCourseEventInterface $hook
+     *
+     * @return int
+     */
+    public function hookCreateCourse(HookCreateCourseEventInterface $hook);
+}