Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add_courses_session code is wrong for this file #5504

Open
etree2024 opened this issue May 13, 2024 · 1 comment
Open

add_courses_session code is wrong for this file #5504

etree2024 opened this issue May 13, 2024 · 1 comment

Comments

@etree2024
Copy link

Describe
REST API V2 does not work for Adding Courses to a Session. The code in the example file is the same as adding users to a session, which is working fine. As you can see in the screen shot the code even here on the project is wrong.

Screenshots
Screenshot 2024-05-13 at 20 09 53

Expected behavior
I expected an error when navigating to the REST API to send back some message after adding a course or courses to using the example file for this

Desktop (please complete the following information):

  • OS: MAC
  • Browser any and all browsers

**Server

  • OS: MAMP
  • Version of Chamilo: 1.11.26
  • Version of PHP: 7.4

Additional context
code is obviously wrong

@etree2024
Copy link
Author

etree2024 commented May 13, 2024

I assume this should look like this but I am not sure its working,

`<?php

/* For licensing terms, see /license.txt */

require_once DIR.'/../../../../vendor/autoload.php';
/**

  • Test example to user API v2.php.
  • Using Guzzle' HTTP client to call the API endpoint and make requests.
  • Change URL on the first lines of createUser() below to suit your needs.
    */

use GuzzleHttp\Client as Client;

// set your URL, username and password here to use it for all webservices in this test file.
$webserviceURL = 'http://myurl.com/main/webservices/api/';
$webserviceUsername = 'USERNAME';
$webservicePassword = 'PASSWORD';

/**

  • Make a request to get the API key for admin user.

  • @throws Exception

  • @return string
    */
    function authenticate()
    {
    global $webserviceURL;
    global $webserviceUsername;
    global $webservicePassword;
    $client = new Client([
    'base_uri' => $webserviceURL,
    ]);

    $response = $client->post('v2.php', [
    'form_params' => [
    'action' => 'authenticate',
    'username' => $webserviceUsername,
    'password' => $webservicePassword,
    ],
    ]);

    if ($response->getStatusCode() !== 200) {
    throw new Exception('Entry denied with code : '.$response->getStatusCode());
    }

    $jsonResponse = json_decode($response->getBody()->getContents());

    if ($jsonResponse->error) {
    throw new Exception('Authentication failed because : '.$jsonResponse->message);
    }

    return $jsonResponse->data->apiKey;
    }

/**

  • @param $apiKey

  • @throws Exception

  • @return int
    */
    function addCoursesToSession($apiKey)
    {
    global $webserviceURL;
    global $webserviceUsername;
    $client = new Client([
    'base_uri' => $webserviceURL,
    ]);

    $response = $client->post(
    'v2.php',
    [
    'form_params' => [
    // data for the user who makes the request
    'action' => 'add_courses_to_session',
    'username' => $webserviceUsername,
    'api_key' => $apiKey,
    // data for users and session
    'id_session' => 2,
    'list_courses' => [
    '1',
    '2',

             ],
         ],
     ]
    

    );

    if ($response->getStatusCode() !== 200) {
    throw new Exception('Entry denied with code : '.$response->getStatusCode());
    }

    $jsonResponse = json_decode($response->getBody()->getContents());

    if ($jsonResponse->error) {
    throw new Exception('Courses not assigned to session because : '.$jsonResponse->message);
    }

    return $jsonResponse->data[0];
    }

$apiKey = authenticate();

//adding courses with id 1 and 2 to session 2
if (addUsersToSession($apiKey)) {
echo 'Courses successfully added to Session';
}
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant