Skip to content

Commit

Permalink
Webservice CREATE_SESSION_FROM_MODEL BT#16388
Browse files Browse the repository at this point in the history
  • Loading branch information
Sébastien Ducoulombier committed Nov 21, 2019
1 parent c5f0981 commit f8522aa
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 2 deletions.
56 changes: 56 additions & 0 deletions main/inc/lib/webservices/Rest.php
Expand Up @@ -58,6 +58,7 @@ class Rest extends WebService
const GET_COURSES = 'get_courses';
const ADD_COURSES_SESSION = 'add_courses_session';
const ADD_USERS_SESSION = 'add_users_session';
const CREATE_SESSION_FROM_MODEL = 'create_session_from_model';

/**
* @var Session
Expand Down Expand Up @@ -1590,4 +1591,59 @@ private function encodeParams(array $additionalParams = [])

return $encoded;
}

/**
* @param $params
*
* @throws Exception
*
* @return integer
*/
public function createSessionFromModel($modelSessionId, $sessionName, $startDate, $endDate, array $extraFields = [])
{
if (!SessionManager::isValidId($modelSessionId)) {
throw new Exception(get_lang('ModelSessionDoesNotExist'));
}

$modelSession = SessionManager::fetch($modelSessionId);

foreach ($extraFields as $k => $v) {
if (array_key_exists($k, $modelSession)) {
$modelSession[$k] = $v;
}
}

if (api_is_multiple_url_enabled()) {
if (api_get_current_access_url_id() != -1) {
$modelSession['accessUrlId'] = api_get_current_access_url_id();
}
}

$newSessionId = SessionManager::create_session(
$sessionName,
$startDate,
$endDate,
$startDate,
$endDate,
$startDate,
$endDate,
$modelSession['coachId'],
$modelSession['sessionCategoryId'],
$modelSession['visibility'],
false,
$modelSession['duration'],
$modelSession['description'],
$modelSession['showDescription'],
$extraFields,
$modelSession['sessionAdminId'],
$modelSession['sendSubscriptionNotification'],
$modelSession['accessUrlId']
);

if (empty($newSessionId)) {
throw new Exception(get_lang('SessionNotRegistered'));
}

return [$newSessionId];
}
}
57 changes: 55 additions & 2 deletions main/webservices/api/test_api.php
Expand Up @@ -59,8 +59,6 @@ public function testAuthenticate()
/**
* @param $apiKey
*
* @return int
*
* @depends testAuthenticate
* @throws Exception
*
Expand Down Expand Up @@ -111,4 +109,59 @@ public function testCreateUser($apiKey)
UserManager::delete_user($userId);
}

/**
* @param $apiKey
* @depends testAuthenticate
*/
public function testCreateSessionFromModel($apiKey)
{
$modelSessionId = SessionManager::create_session(
'Model session',
'2019-01-01 00:00', '2019-08-31 00:00',
'2019-01-01 00:00', '2019-08-31 00:00',
'2019-01-01 00:00', '2019-08-31 00:00',
null, null
);

$this->assertIsInt($modelSessionId);

$response = $this->client->post(
'v2.php',
[
'form_params' => [
// data for the user who makes the request
'action' => 'create_session_from_model',
'username' => self::WEBSERVICE_USERNAME,
'api_key' => $apiKey,
// data for new user
'modelSessionId' => $modelSessionId,
'sessionName' => 'Name of the new session',
'startDate' => '2019-09-01 00:00',
'endDate' => '2019-12-31 00:00',
'extraFields' => [
[
'field_name' => 'description',
'field_value' => 'Description of the new session',
],
],
],
]
);

SessionManager::delete($modelSessionId);

$this->assertSame(200, $response->getStatusCode(), 'Entry denied with code : ' . $response->getStatusCode());

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

$this->assertFalse($jsonResponse->error, 'Session not created because : ' . $jsonResponse->message);
$this->assertNotNull($jsonResponse->data);
$this->assertIsArray($jsonResponse->data);
$this->assertArrayHasKey(0, $jsonResponse->data);
$this->assertIsInt($jsonResponse->data[0]);

$newSessionId = $jsonResponse->data[0];
SessionManager::delete($newSessionId);
}

}
14 changes: 14 additions & 0 deletions main/webservices/api/v2.php
Expand Up @@ -291,6 +291,20 @@
$restApi->setMessageRead($messageId);
$restResponse->setData(['status' => true]);
break;
case Rest::CREATE_SESSION_FROM_MODEL:
if (
empty($_POST['modelSessionId']) || empty($_POST['sessionName']) || empty($_POST['startDate']) || empty($_POST['endDate'])
) {
throw new Exception(get_lang('NoData'));
}
$modelSessionId = intval($_POST['modelSessionId']);
$sessionName = $_POST['sessionName'];
$startDate = $_POST['startDate'];
$endDate = $_POST['endDate'];
$extraFields = isset($_POST['extraFields']) ? $_POST['extraFields'] : [];
$newSessionId = $restApi->createSessionFromModel($modelSessionId, $sessionName, $startDate, $endDate, $extraFields);
$restResponse->setData($newSessionId);
break;
default:
throw new Exception(get_lang('InvalidAction'));
}
Expand Down

0 comments on commit f8522aa

Please sign in to comment.