From 4771018dd679301ecfdfc4dab879593ea02585a6 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Tue, 5 Feb 2019 14:43:04 -0500 Subject: [PATCH] GraphQL: Allow delete course #2644 Remove code from webservices --- main/webservices/cm_soap_course.php | 46 --------- main/webservices/cm_webservice_course.php | 90 ------------------ main/webservices/soap_course.php | 52 ----------- main/webservices/webservice_course.php | 93 ------------------- src/GraphQlBundle/Map/MutationMap.php | 30 ++++++ .../Resources/config/schema.types.graphql | 1 + 6 files changed, 31 insertions(+), 281 deletions(-) diff --git a/main/webservices/cm_soap_course.php b/main/webservices/cm_soap_course.php index 7eb6619f51..1c52cf27a8 100755 --- a/main/webservices/cm_soap_course.php +++ b/main/webservices/cm_soap_course.php @@ -12,52 +12,6 @@ require_once __DIR__.'/cm_soap.php'; */ $s = WSCMSoapServer::singleton(); -$s->wsdl->addComplexType( - 'course_id', - 'complexType', - 'struct', - 'all', - '', - [ - 'course_id_field_name' => ['name' => 'course_id_field_name', 'type' => 'xsd:string'], - 'course_id_value' => ['name' => 'course_id_value', 'type' => 'xsd:string'], - ] -); - -$s->wsdl->addComplexType( - 'course_result', - 'complexType', - 'struct', - 'all', - '', - [ - 'course_id_value' => ['name' => 'course_id_value', 'type' => 'xsd:string'], - 'result' => ['name' => 'result', 'type' => 'tns:result'], - ] -); - -$s->wsdl->addComplexType( - 'course_result_array', - 'complexType', - 'array', - '', - 'SOAP-ENC:Array', - [], - [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:course_result[]']], - 'tns:course_result' -); - -$s->register( - 'WSCMCourse.DeleteCourse', - ['secret_key' => 'xsd:string', 'course_id_field_name' => 'xsd:string', 'course_id_value' => 'xsd:string'] -); - -$s->register( - 'WSCMCourse.DeleteCourses', - ['secret_key' => 'xsd:string', 'courses' => 'tns:course_id[]'], - ['return' => 'tns:course_result_array'] -); - $s->wsdl->addComplexType( 'course', 'complexType', diff --git a/main/webservices/cm_webservice_course.php b/main/webservices/cm_webservice_course.php index 8ccf6ba996..bab5c71d92 100755 --- a/main/webservices/cm_webservice_course.php +++ b/main/webservices/cm_webservice_course.php @@ -12,68 +12,6 @@ require_once __DIR__.'/cm_webservice.php'; */ class WSCMCourse extends WSCM { - /** - * Deletes a course. - * - * @param string API secret key - * @param string Course id field name - * @param string Course id value - */ - public function DeleteCourse( - $secret_key, - $course_id_field_name, - $course_id_value - ) { - $verifKey = $this->verifyKey($secret_key); - if ($verifKey instanceof WSError) { - $this->handleError($verifKey); - } else { - $result = $this->deleteCourseHelper( - $course_id_field_name, - $course_id_value - ); - if ($result instanceof WSError) { - $this->handleError($result); - } - } - } - - /** - * Deletes multiple courses. - * - * @param string API secret key - * @param array Array of courses with elements of the form - * array('course_id_field_name' => 'name_of_field', 'course_id_value' => 'value') - * - * @return array Array with elements like - * array('course_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). - * Note that if the result array contains a code different - * than 0, an error occured - */ - public function DeleteCourses($secret_key, $courses) - { - $verifKey = $this->verifyKey($secret_key); - if ($verifKey instanceof WSError) { - $this->handleError($verifKey); - } else { - $results = []; - foreach ($courses as $course) { - $result_tmp = []; - $result_op = $this->deleteCourseHelper($course['course_id_field_name'], $course['course_id_value']); - $result_tmp['course_id_value'] = $course['course_id_value']; - if ($result_op instanceof WSCMError) { - // Return the error in the results - $result_tmp['result'] = $result_op->toArray(); - } else { - $result_tmp['result'] = $this->getSuccessfulResult(); - } - $results[] = $result_tmp; - } - - return $results; - } - } - /** * List courses. * @@ -322,34 +260,6 @@ class WSCMCourse extends WSCM return $username; } - /** - * Deletes a course (helper method). - * - * @param string Course id field name - * @param string Course id value - * - * @return mixed True if the course was successfully deleted, WSError otherwise - */ - protected function deleteCourseHelper( - $course_id_field_name, - $course_id_value - ) { - $course_id = $this->getCourseId( - $course_id_field_name, - $course_id_value - ); - if ($course_id instanceof WSCMError) { - return $course_id; - } else { - $course_code = CourseManager::get_course_code_from_course_id( - $course_id - ); - CourseManager::delete_course($course_code); - - return true; - } - } - /** * Subscribe or unsubscribe user to a course (helper method). * diff --git a/main/webservices/soap_course.php b/main/webservices/soap_course.php index 1a0ecf13f7..1cfcbe0d9b 100755 --- a/main/webservices/soap_course.php +++ b/main/webservices/soap_course.php @@ -13,58 +13,6 @@ require_once __DIR__.'/soap.php'; */ $s = WSSoapServer::singleton(); -$s->wsdl->addComplexType( - 'course_id', - 'complexType', - 'struct', - 'all', - '', - [ - 'course_id_field_name' => ['name' => 'course_id_field_name', 'type' => 'xsd:string'], - 'course_id_value' => ['name' => 'course_id_value', 'type' => 'xsd:string'], - ] -); - -$s->wsdl->addComplexType( - 'course_result', - 'complexType', - 'struct', - 'all', - '', - [ - 'course_id_value' => ['name' => 'course_id_value', 'type' => 'xsd:string'], - 'result' => ['name' => 'result', 'type' => 'tns:result'], - ] -); - -$s->wsdl->addComplexType( - 'course_result_array', - 'complexType', - 'array', - '', - 'SOAP-ENC:Array', - [], - [['ref' => 'SOAP-ENC:arrayType', 'wsdl:arrayType' => 'tns:course_result[]']], - 'tns:course_result' -); - -$s->register( - 'WSCourse.DeleteCourse', - ['secret_key' => 'xsd:string', 'course_id_field_name' => 'xsd:string', 'course_id_value' => 'xsd:string'], - [], - 'urn:WSService', // namespace - 'urn:WSService#WSCourse.DeleteCourse', // soapaction - 'rpc', // style - 'encoded', // use - 'Delete a course in chamilo' // documentation -); - -$s->register( - 'WSCourse.DeleteCourses', - ['secret_key' => 'xsd:string', 'courses' => 'tns:course_id[]'], - ['return' => 'tns:course_result_array'] -); - $s->wsdl->addComplexType( 'course', 'complexType', diff --git a/main/webservices/webservice_course.php b/main/webservices/webservice_course.php index a8e11ec560..40d8705dbf 100755 --- a/main/webservices/webservice_course.php +++ b/main/webservices/webservice_course.php @@ -11,71 +11,6 @@ require_once __DIR__.'/webservice.php'; */ class WSCourse extends WS { - /** - * Deletes a course. - * - * @param string API secret key - * @param string Course id field name - * @param string Course id value - */ - public function DeleteCourse( - $secret_key, - $course_id_field_name, - $course_id_value - ) { - $verifKey = $this->verifyKey($secret_key); - if ($verifKey instanceof WSError) { - $this->handleError($verifKey); - } else { - $result = $this->deleteCourseHelper( - $course_id_field_name, - $course_id_value - ); - if ($result instanceof WSError) { - $this->handleError($result); - } - } - } - - /** - * Deletes multiple courses. - * - * @param string API secret key - * @param array Array of courses with elements of the form - * array('course_id_field_name' => 'name_of_field', 'course_id_value' => 'value') - * - * @return array Array with elements like - * array('course_id_value' => 'value', 'result' => array('code' => 0, 'message' => 'Operation was successful')). - * Note that if the result array contains a code different - * than 0, an error occured - */ - public function DeleteCourses($secret_key, $courses) - { - $verifKey = $this->verifyKey($secret_key); - if ($verifKey instanceof WSError) { - $this->handleError($verifKey); - } else { - $results = []; - foreach ($courses as $course) { - $result_tmp = []; - $result_op = $this->deleteCourseHelper( - $course['course_id_field_name'], - $course['course_id_value'] - ); - $result_tmp['course_id_value'] = $course['course_id_value']; - if ($result_op instanceof WSError) { - // Return the error in the results - $result_tmp['result'] = $result_op->toArray(); - } else { - $result_tmp['result'] = $this->getSuccessfulResult(); - } - $results[] = $result_tmp; - } - - return $results; - } - } - /** * List courses. * @@ -318,34 +253,6 @@ class WSCourse extends WS } } - /** - * Deletes a course (helper method). - * - * @param string Course id field name - * @param string Course id value - * - * @return mixed True if the course was successfully deleted, WSError otherwise - */ - protected function deleteCourseHelper( - $course_id_field_name, - $course_id_value - ) { - $course_id = $this->getCourseId( - $course_id_field_name, - $course_id_value - ); - if ($course_id instanceof WSError) { - return $course_id; - } else { - $course_code = CourseManager::get_course_code_from_course_id( - $course_id - ); - CourseManager::delete_course($course_code); - - return true; - } - } - /** * Subscribe or unsubscribe user to a course (helper method). * diff --git a/src/GraphQlBundle/Map/MutationMap.php b/src/GraphQlBundle/Map/MutationMap.php index 9c18b7fabd..9bdb4b234a 100644 --- a/src/GraphQlBundle/Map/MutationMap.php +++ b/src/GraphQlBundle/Map/MutationMap.php @@ -696,6 +696,36 @@ class MutationMap extends ResolverMap implements ContainerAwareInterface return $this->em->find('ChamiloUserBundle:User', $userId); } + /** + * @param Argument $args + * + * @return bool + */ + protected function resolveDeleteCourse(Argument $args): bool + { + $this->checkAuthorization(); + + if (false === $this->securityChecker->isGranted('ROLE_ADMIN')) { + throw new UserError($this->translator->trans('Not allowed')); + } + + $itemIdInput = array_map('trim', $args['itemId']); + + if (empty($itemIdInput['name']) || empty($itemIdInput['value'])) { + throw new UserError($this->translator->trans('Missing parameters')); + } + + $courseInfo = \CourseManager::getCourseInfoFromOriginalId($itemIdInput['value'], $itemIdInput['name']); + + if (empty($courseInfo)) { + throw new UserError($this->translator->trans("Course doesn't exists")); + } + + \CourseManager::delete_course($courseInfo['code']); + + return true; + } + /** * @param string $userIdName * @param string $userIdValue diff --git a/src/GraphQlBundle/Resources/config/schema.types.graphql b/src/GraphQlBundle/Resources/config/schema.types.graphql index 4db69e6267..826bbdd9ec 100644 --- a/src/GraphQlBundle/Resources/config/schema.types.graphql +++ b/src/GraphQlBundle/Resources/config/schema.types.graphql @@ -49,6 +49,7 @@ type Mutation { deleteUser(itemId: ItemIdInput!): Boolean editUser(user: EditUserInput!, itemId: ItemIdInput!): User editCourse(course: EditCourseInput!, itemId: ItemIdInput!): Course + deleteCourse(itemId: ItemIdInput!): Boolean } # Objects