Browse Source

GraphQL: allow edit course #2644

Remove code from webservices
Angel Fernando Quiroz Campos 6 years ago
parent
commit
3e65c4375d

+ 0 - 19
main/webservices/cm_soap_course.php

@@ -58,25 +58,6 @@ $s->register(
     ['return' => 'tns:course_result_array']
 );
 
-$s->register(
-    'WSCMCourse.EditCourse',
-    [
-        'secret_key' => 'xsd:string',
-        'course_id_field_name' => 'xsd:string',
-        'course_id_value' => 'xsd:string',
-        'title' => 'xsd:string',
-        'category_code' => 'xsd:string',
-        'department_name' => 'xsd:string',
-        'department_url' => 'xsd:string',
-        'language' => 'xsd:string',
-        'visibility' => 'xsd:int',
-        'subscribe' => 'xsd:int',
-        'unsubscribe' => 'xsd:int',
-        'visual_code' => 'xsd:string',
-        'extras' => 'tns:extra_field[]',
-    ]
-);
-
 $s->wsdl->addComplexType(
     'course',
     'complexType',

+ 0 - 138
main/webservices/cm_webservice_course.php

@@ -74,62 +74,6 @@ class WSCMCourse extends WSCM
         }
     }
 
-    /**
-     * Edits a course.
-     *
-     * @param string API secret key
-     * @param string Course id field name
-     * @param string Course id value
-     * @param string Title
-     * @param string Category code
-     * @param string Department name
-     * @param string Department url
-     * @param string Course language
-     * @param int Visibility
-     * @param int Subscribe (0 = denied, 1 = allowed)
-     * @param int Unsubscribe (0 = denied, 1 = allowed)
-     * @param string Visual code
-     * @param array Course extra fields
-     */
-    public function EditCourse(
-        $secret_key,
-        $course_id_field_name,
-        $course_id_value,
-        $title,
-        $category_code,
-        $department_name,
-        $department_url,
-        $language,
-        $visibility,
-        $subscribe,
-        $unsubscribe,
-        $visual_code,
-        $extras
-    ) {
-        $verifKey = $this->verifyKey($secret_key);
-        if ($verifKey instanceof WSCMError) {
-            $this->handleError($verifKey);
-        } else {
-            $result = $this->editCourseHelper(
-                $course_id_field_name,
-                $course_id_value,
-                $title,
-                $category_code,
-                $department_name,
-                $department_url,
-                $language,
-                $visibility,
-                $subscribe,
-                $unsubscribe,
-                $visual_code,
-                $extras
-            );
-            if ($result instanceof WSCMError) {
-                $this->handleError($result);
-            }
-        }
-    }
-
     /**
      * List courses.
      *
@@ -406,88 +350,6 @@ class WSCMCourse extends WSCM
         }
     }
 
-    /**
-     * Edits a course (helper method).
-     *
-     * @param string Course id field name
-     * @param string Course id value
-     * @param string Title
-     * @param string Category code
-     * @param string Department name
-     * @param string Department url
-     * @param string Course language
-     * @param int Visibility
-     * @param int Subscribe (0 = denied, 1 = allowed)
-     * @param int Unsubscribe (0 = denied, 1 = allowed)
-     * @param string Visual code
-     * @param array Course extra fields
-     *
-     * @return mixed True in case of success, WSError otherwise
-     */
-    protected function editCourseHelper(
-        $course_id_field_name,
-        $course_id_value,
-        $title,
-        $category_code,
-        $department_name,
-        $department_url,
-        $language,
-        $visibility,
-        $subscribe,
-        $unsubscribe,
-        $visual_code,
-        $extras
-    ) {
-        $course_id = $this->getCourseId($course_id_field_name, $course_id_value);
-        if ($course_id instanceof WSCMError) {
-            return $course_id;
-        } else {
-            $attributes = [];
-            if (!empty($title)) {
-                $attributes['title'] = $title;
-            }
-            if (!empty($category_code)) {
-                $attributes['category_code'] = $category_code;
-            }
-            if (!empty($department_name)) {
-                $attributes['department_name'] = $department_name;
-            }
-            if (!empty($department_url)) {
-                $attributes['department_url'] = $department_url;
-            }
-            if (!empty($language)) {
-                $attributes['course_language'] = $language;
-            }
-            if ($visibility != '') {
-                $attributes['visibility'] = (int) $visibility;
-            }
-            if ($subscribe != '') {
-                $attributes['subscribe'] = (int) $subscribe;
-            }
-            if ($unsubscribe != '') {
-                $attributes['unsubscribe'] = (int) $unsubscribe;
-            }
-            if (!empty($visual_code)) {
-                $attributes['visual_code'] = $visual_code;
-            }
-            if (!empty($attributes)) {
-                CourseManager::update_attributes($course_id, $attributes);
-            }
-            if (!empty($extras)) {
-                $course_code = CourseManager::get_course_code_from_course_id($course_id);
-                $extras_associative = [];
-                foreach ($extras as $extra) {
-                    $extras_associative[$extra['field_name']] = $extra['field_value'];
-                }
-                foreach ($extras_associative as $fname => $fvalue) {
-                    CourseManager::update_extra_field_value($course_code, $fname, $fvalue);
-                }
-            }
-
-            return true;
-        }
-    }
-
     /**
      * Subscribe or unsubscribe user to a course (helper method).
      *

+ 0 - 19
main/webservices/soap_course.php

@@ -65,25 +65,6 @@ $s->register(
     ['return' => 'tns:course_result_array']
 );
 
-$s->register(
-    'WSCourse.EditCourse',
-    [
-        'secret_key' => 'xsd:string',
-        'course_id_field_name' => 'xsd:string',
-        'course_id_value' => 'xsd:string',
-        'title' => 'xsd:string',
-        'category_code' => 'xsd:string',
-        'department_name' => 'xsd:string',
-        'department_url' => 'xsd:string',
-        'language' => 'xsd:string',
-        'visibility' => 'xsd:int',
-        'subscribe' => 'xsd:int',
-        'unsubscribe' => 'xsd:int',
-        'visual_code' => 'xsd:string',
-        'extras' => 'tns:extra_field',
-    ]
-);
-
 $s->wsdl->addComplexType(
     'course',
     'complexType',

+ 0 - 147
main/webservices/webservice_course.php

@@ -76,62 +76,6 @@ class WSCourse extends WS
         }
     }
 
-    /**
-     * Edits a course.
-     *
-     * @param string API secret key
-     * @param string Course id field name
-     * @param string Course id value
-     * @param string Title
-     * @param string Category code
-     * @param string Department name
-     * @param string Department url
-     * @param string Course language
-     * @param int Visibility
-     * @param int Subscribe (0 = denied, 1 = allowed)
-     * @param int Unsubscribe (0 = denied, 1 = allowed)
-     * @param string Visual code
-     * @param array Course extra fields
-     */
-    public function EditCourse(
-        $secret_key,
-        $course_id_field_name,
-        $course_id_value,
-        $title,
-        $category_code,
-        $department_name,
-        $department_url,
-        $language,
-        $visibility,
-        $subscribe,
-        $unsubscribe,
-        $visual_code,
-        $extras
-    ) {
-        $verifKey = $this->verifyKey($secret_key);
-        if ($verifKey instanceof WSError) {
-            $this->handleError($verifKey);
-        } else {
-            $result = $this->editCourseHelper(
-                $course_id_field_name,
-                $course_id_value,
-                $title,
-                $category_code,
-                $department_name,
-                $department_url,
-                $language,
-                $visibility,
-                $subscribe,
-                $unsubscribe,
-                $visual_code,
-                $extras
-            );
-            if ($result instanceof WSError) {
-                $this->handleError($result);
-            }
-        }
-    }
-
     /**
      * List courses.
      *
@@ -402,97 +346,6 @@ class WSCourse extends WS
         }
     }
 
-    /**
-     * Edits a course (helper method).
-     *
-     * @param string Course id field name
-     * @param string Course id value
-     * @param string Title
-     * @param string Category code
-     * @param string Department name
-     * @param string Department url
-     * @param string Course language
-     * @param int Visibility
-     * @param int Subscribe (0 = denied, 1 = allowed)
-     * @param int Unsubscribe (0 = denied, 1 = allowed)
-     * @param string Visual code
-     * @param array Course extra fields
-     *
-     * @return mixed True in case of success, WSError otherwise
-     */
-    protected function editCourseHelper(
-        $course_id_field_name,
-        $course_id_value,
-        $title,
-        $category_code,
-        $department_name,
-        $department_url,
-        $language,
-        $visibility,
-        $subscribe,
-        $unsubscribe,
-        $visual_code,
-        $extras
-    ) {
-        $course_id = $this->getCourseId(
-            $course_id_field_name,
-            $course_id_value
-        );
-        if ($course_id instanceof WSError) {
-            return $course_id;
-        } else {
-            $attributes = [];
-            if (!empty($title)) {
-                $attributes['title'] = $title;
-            }
-            if (!empty($category_code)) {
-                $attributes['category_code'] = $category_code;
-            }
-            if (!empty($department_name)) {
-                $attributes['department_name'] = $department_name;
-            }
-            if (!empty($department_url)) {
-                $attributes['department_url'] = $department_url;
-            }
-            if (!empty($language)) {
-                $attributes['course_language'] = $language;
-            }
-            if ($visibility != '') {
-                $attributes['visibility'] = (int) $visibility;
-            }
-            if ($subscribe != '') {
-                $attributes['subscribe'] = (int) $subscribe;
-            }
-            if ($unsubscribe != '') {
-                $attributes['unsubscribe'] = (int) $unsubscribe;
-            }
-            if (!empty($visual_code)) {
-                $attributes['visual_code'] = $visual_code;
-            }
-            if (!empty($attributes)) {
-                CourseManager::update_attributes($course_id, $attributes);
-            }
-            if (!empty($extras)) {
-                $course_code = CourseManager::get_course_code_from_course_id(
-                    $course_id
-                );
-                $extras_associative = [];
-                foreach ($extras as $extra) {
-                    $extras_associative[$extra['field_name']] = $extra['field_value'];
-                }
-                foreach ($extras_associative as $fname => $fvalue) {
-                    CourseManager::update_extra_field_value(
-                        $course_code,
-                        $fname,
-                        $fvalue
-                    );
-                }
-            }
-
-            return true;
-        }
-    }
-
     /**
      * Subscribe or unsubscribe user to a course (helper method).
      *

+ 73 - 0
src/GraphQlBundle/Map/MutationMap.php

@@ -190,6 +190,79 @@ class MutationMap extends ResolverMap implements ContainerAwareInterface
         return $this->em->find('ChamiloCoreBundle:Course', $courseInfo['real_id']);
     }
 
+    /**
+     * @param Argument $args
+     *
+     * @throws \Doctrine\ORM\ORMException
+     * @throws \Doctrine\ORM\OptimisticLockException
+     * @throws \Doctrine\ORM\TransactionRequiredException
+     *
+     * @return Course
+     */
+    protected function resolveEditCourse(Argument $args): Course
+    {
+        $this->checkAuthorization();
+
+        if (false === $this->securityChecker->isGranted('ROLE_ADMIN')) {
+            throw new UserError($this->translator->trans('Not allowed'));
+        }
+
+        $courseInput = array_map('trim', $args['course']);
+        $itemIdInput = array_map('trim', $args['itemId']);
+
+        if (empty($itemIdInput['name']) || empty($itemIdInput['value'])) {
+            throw new UserError($this->translator->trans('Missing parameters'));
+        }
+
+        $courseId = \CourseManager::get_course_id_from_original_id($itemIdInput['value'], $itemIdInput['name']);
+
+        if (empty($courseId)) {
+            throw new UserError($this->translator->trans("Course doesn't exists"));
+        }
+
+        /** @var Course $course */
+        $course = $this->em->find('ChamiloCoreBundle:Course', $courseId);
+
+        if (!empty($courseInput['title'])) {
+            $course->setTitle($courseInput['title']);
+        }
+
+        if (isset($courseInput['categoryCode'])) {
+            $course->setCategoryCode($courseInput['categoryCode']);
+        }
+
+        if (!empty($courseInput['visualCode'])) {
+            $course->setVisualCode($courseInput['visualCode']);
+        }
+
+        if (!empty($courseInput['language'])) {
+            $course->setCourseLanguage($courseInput['language']);
+        }
+
+        if (!empty($courseInput['diskQuota'])) {
+            $course->setDiskQuota($courseInput['diskQuota'] * 1024 * 1024);
+        }
+
+        if (isset($courseInput['visibility'])) {
+            $course->setVisibility($courseInput['visibility']);
+        }
+
+        if ($course->getSubscribe() !== $courseInput['allowSubscription']) {
+            $course->setSubscribe($courseInput['allowSubscription']);
+        }
+
+        if ($course->getUnsubscribe() !== $courseInput['allowUnsubscription']) {
+            $course->setUnsubscribe($courseInput['allowUnsubscription']);
+        }
+
+        $course->setCurrentUrl($this->currentAccessUrl);
+
+        $this->em->persist($course);
+        $this->em->flush();
+
+        return $course;
+    }
+
     /**
      * @param Argument $args
      *

+ 12 - 0
src/GraphQlBundle/Resources/config/schema.types.graphql

@@ -48,6 +48,7 @@ type Mutation {
     enableUser(itemId: ItemIdInput!): Boolean
     deleteUser(itemId: ItemIdInput!): Boolean
     editUser(user: EditUserInput!, itemId: ItemIdInput!): User
+    editCourse(course: EditCourseInput!, itemId: ItemIdInput!): Course
 }
 
 # Objects
@@ -401,6 +402,17 @@ input CreateCourseInput {
     allowUnsubscription: Boolean = false
 }
 
+input EditCourseInput {
+    title: String,
+    categoryCode: String,
+    visualCode: String,
+    language: String = "en",
+    diskQuota: Int = 100,
+    visibility: CourseVisibility,
+    allowSubscription: Boolean = true,
+    allowUnsubscription: Boolean = false
+}
+
 input CreateUserInput {
     firstname: String!,
     lastname: String!,