chamilo-lms icon indicating copy to clipboard operation
chamilo-lms copied to clipboard

add_courses_session code is wrong for this file

Open etree2024 opened this issue 1 year ago • 1 comments

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 avatar May 13 '24 17:05 etree2024

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'; } `

etree2024 avatar May 13 '24 18:05 etree2024