Merge branch '1.11.x' of github.com:chamilo/chamilo-lms into 1.11.x

pull/3035/head
Sébastien Ducoulombier 6 years ago
commit a876e92c1f
  1. 43
      main/exercise/exercise_result.class.php
  2. 25
      main/inc/lib/webservices/Rest.php
  3. 25
      main/webservices/api/tests/CreateSessionFromModelTest.php
  4. 8
      main/webservices/api/tests/GetSessionFromExtraFieldTest.php
  5. 2
      main/webservices/api/tests/README
  6. 8
      main/webservices/api/tests/SaveUserTest.php
  7. 8
      main/webservices/api/tests/SubscribeUserToSessionFromUsernameTest.php
  8. 16
      main/webservices/api/tests/UpdateUserFromUsernameTest.php
  9. 9
      main/webservices/api/tests/V2TestCase.php
  10. 8
      main/webservices/api/v2.php

@ -81,7 +81,8 @@ class ExerciseResult
te.exe_duration as duration, te.exe_duration as duration,
te.orig_lp_id as orig_lp_id, te.orig_lp_id as orig_lp_id,
tlm.name as lp_name, tlm.name as lp_name,
user.username user.username,
te.status as exstatus
FROM $TBL_EXERCISES AS ce FROM $TBL_EXERCISES AS ce
INNER JOIN $TBL_TRACK_EXERCISES AS te INNER JOIN $TBL_TRACK_EXERCISES AS te
ON (te.exe_exo_id = ce.id) ON (te.exe_exo_id = ce.id)
@ -91,7 +92,6 @@ class ExerciseResult
ON (tlm.id = te.orig_lp_id AND tlm.c_id = ce.c_id) ON (tlm.id = te.orig_lp_id AND tlm.c_id = ce.c_id)
WHERE WHERE
ce.c_id = $course_id AND ce.c_id = $course_id AND
te.status != 'incomplete' AND
te.c_id = ce.c_id $user_id_and $session_id_and AND te.c_id = ce.c_id $user_id_and $session_id_and AND
ce.active <>-1"; ce.active <>-1";
} else { } else {
@ -112,7 +112,8 @@ class ExerciseResult
ce.results_disabled as exdisabled, ce.results_disabled as exdisabled,
te.orig_lp_id as orig_lp_id, te.orig_lp_id as orig_lp_id,
tlm.name as lp_name, tlm.name as lp_name,
user.username user.username,
te.status as exstatus
FROM $TBL_EXERCISES AS ce FROM $TBL_EXERCISES AS ce
INNER JOIN $TBL_TRACK_EXERCISES AS te INNER JOIN $TBL_TRACK_EXERCISES AS te
ON (te.exe_exo_id = ce.id) ON (te.exe_exo_id = ce.id)
@ -122,7 +123,6 @@ class ExerciseResult
ON (tlm.id = te.orig_lp_id AND tlm.c_id = ce.c_id) ON (tlm.id = te.orig_lp_id AND tlm.c_id = ce.c_id)
WHERE WHERE
ce.c_id = $course_id AND ce.c_id = $course_id AND
te.status != 'incomplete' AND
te.c_id = ce.c_id $user_id_and $session_id_and AND te.c_id = ce.c_id $user_id_and $session_id_and AND
ce.active <>-1 AND ce.active <>-1 AND
ORDER BY userpart2, te.c_id ASC, ce.title ASC, te.exe_date DESC"; ORDER BY userpart2, te.c_id ASC, ce.title ASC, te.exe_date DESC";
@ -180,25 +180,29 @@ class ExerciseResult
if (is_array($results)) { if (is_array($results)) {
$i = 0; $i = 0;
foreach ($results as $result) { foreach ($results as $result) {
$revised = false; $revised = 0;
//revised or not if ($result['exstatus'] === 'incomplete') {
$sql_exe = "SELECT exe_id $revised = -1;
FROM $TBL_TRACK_ATTEMPT_RECORDING } else {
WHERE //revised or not
author != '' AND $sql_exe = "SELECT exe_id
exe_id = ".Database::escape_string($result['exid'])." FROM $TBL_TRACK_ATTEMPT_RECORDING
LIMIT 1"; WHERE
$query = Database::query($sql_exe); author != '' AND
exe_id = ".intval($result['exid'])."
if (Database:: num_rows($query) > 0) { LIMIT 1";
$revised = true; $query = Database::query($sql_exe);
if (Database:: num_rows($query) > 0) {
$revised = 1;
}
} }
if ($filter_by_not_revised && $revised) { if ($filter_by_not_revised && $revised === 1) {
continue; continue;
} }
if ($filter_by_revised && !$revised) { if ($filter_by_revised && $revised < 1) {
continue; continue;
} }
@ -222,7 +226,8 @@ class ExerciseResult
$return[$i]['duration'] = $result['duration']; $return[$i]['duration'] = $result['duration'];
$return[$i]['result'] = $result['exresult']; $return[$i]['result'] = $result['exresult'];
$return[$i]['max'] = $result['exweight']; $return[$i]['max'] = $result['exweight'];
$return[$i]['status'] = $revised ? get_lang('Validated') : get_lang('NotValidated'); // Revised: 1 = revised, 0 = not revised, -1 = not even finished by user
$return[$i]['status'] = $revised === 1 ? get_lang('Validated') : ($revised === 0 ? get_lang('NotValidated') : get_lang('Unclosed') );
$return[$i]['lp_id'] = $result['orig_lp_id']; $return[$i]['lp_id'] = $result['orig_lp_id'];
$return[$i]['lp_name'] = $result['lp_name']; $return[$i]['lp_name'] = $result['lp_name'];

@ -1604,15 +1604,16 @@ class Rest extends WebService
* @param $startDate * @param $startDate
* @param $endDate * @param $endDate
* @param array $extraFields * @param array $extraFields
* @return array containing an int, the id of the new session * @return int, the id of the new session
* @throws Exception * @throws Exception
*/ */
public function createSessionFromModel($modelSessionId, $sessionName, $startDate, $endDate, array $extraFields = []) public function createSessionFromModel($modelSessionId, $sessionName, $startDate, $endDate, array $extraFields = [])
{ {
if (empty($_POST['modelSessionId']) if (empty($modelSessionId)
|| empty($_POST['sessionName']) || empty($sessionName)
|| empty($_POST['startDate']) || empty($startDate)
|| empty($_POST['endDate'])) { || empty($endDate)
) {
throw new Exception(get_lang('NoData')); throw new Exception(get_lang('NoData'));
} }
@ -1694,7 +1695,7 @@ class Rest extends WebService
UrlManager::add_session_to_url($newSessionId, 1); UrlManager::add_session_to_url($newSessionId, 1);
} }
return [$newSessionId]; return $newSessionId;
} }
/** /**
@ -1702,7 +1703,7 @@ class Rest extends WebService
* *
* @param int $sessionId the session id * @param int $sessionId the session id
* @param string $loginName the user's login name * @param string $loginName the user's login name
* @return array containing a boolean, whether it worked * @return boolean, whether it worked
* @throws Exception * @throws Exception
*/ */
public function subscribeUserToSessionFromUsername($sessionId, $loginName) public function subscribeUserToSessionFromUsername($sessionId, $loginName)
@ -1725,7 +1726,7 @@ class Rest extends WebService
throw new Exception(get_lang('UserNotSubscribed')); throw new Exception(get_lang('UserNotSubscribed'));
} }
return [true]; return true;
} }
/** /**
@ -1733,7 +1734,7 @@ class Rest extends WebService
* *
* @param $fieldName * @param $fieldName
* @param $fieldValue * @param $fieldValue
* @return array containing an int, the matching session id * @return int, the matching session id
* @throws Exception when no session matched or more than one session matched * @throws Exception when no session matched or more than one session matched
*/ */
public function getSessionFromExtraField($fieldName, $fieldValue) public function getSessionFromExtraField($fieldName, $fieldValue)
@ -1754,14 +1755,14 @@ class Rest extends WebService
} }
// return sessionId // return sessionId
return [ intval($sessionIdList[0]['item_id']) ]; return intval($sessionIdList[0]['item_id']);
} }
/** /**
* updates a user identified by its login name * updates a user identified by its login name
* *
* @param array $parameters * @param array $parameters
* @return array containing the boolean true on success * @return boolean, true on success
* @throws Exception on failure * @throws Exception on failure
*/ */
public function updateUserFromUserName($parameters) public function updateUserFromUserName($parameters)
@ -1949,6 +1950,6 @@ class Rest extends WebService
} }
} }
return [ true ]; return true;
} }
} }

@ -1,15 +1,17 @@
<?php <?php
require 'V2TestCase.php'; /* For licensing terms, see /license.txt */
require_once __DIR__.'/V2TestCase.php';
require_once __DIR__.'/../../../../vendor/autoload.php'; require_once __DIR__.'/../../../../vendor/autoload.php';
/** /**
* Class TestCreateSessionFromModel * Class CreateSessionFromModelTest
* *
* CREATE_SESSION_FROM_MODEL webservice unit tests * CREATE_SESSION_FROM_MODEL webservice unit tests
*/ */
class TestCreateSessionFromModel extends V2TestCase class CreateSessionFromModelTest extends V2TestCase
{ {
protected function action() protected function action()
{ {
@ -45,14 +47,19 @@ class TestCreateSessionFromModel extends V2TestCase
); );
// assert the session was created and given the returned session id // assert the session was created and given the returned session id
$session = (new SessionManager)->fetch($newSessionId); $entityManager = Database::getManager();
$this->assertIsArray($session); $repository = $entityManager->getRepository('ChamiloCoreBundle:Session');
$newSession = $repository->find($newSessionId);
$this->assertIsObject($newSession);
// assert the new session got the right data // assert the new session got the right data
$this->assertSame($name, $session['name']); $this->assertSame($name, $newSession->getName());
// FIXME time shift (one or two hours back) // FIXME account for UTC / local timezone shift
// $this->assertSame($startDate, $session['display_start_date']); // $this->assertSame($endDate, $newSession->getDisplayEndDate());
// $this->assertSame($endDate, $session['display_end_date']); // $this->assertSame($startDate, $newSession->getAccessStartDate());
// $this->assertSame($endDate, $newSession->getAccessEndDate());
// $this->assertSame($startDate, $newSession->getCoachAccessStartDate());
// $this->assertSame($endDate, $newSession->getCoachAccessEndDate());
// clean up // clean up
SessionManager::delete($modelSessionId); SessionManager::delete($modelSessionId);

@ -1,15 +1,17 @@
<?php <?php
require 'V2TestCase.php'; /* For licensing terms, see /license.txt */
require_once __DIR__.'/V2TestCase.php';
require_once __DIR__.'/../../../../vendor/autoload.php'; require_once __DIR__.'/../../../../vendor/autoload.php';
/** /**
* Class TestGetSessionFromExtraField * Class GetSessionFromExtraFieldTest
* *
* GET_SESSION_FROM_EXTRA_FIELD webservice unit tests * GET_SESSION_FROM_EXTRA_FIELD webservice unit tests
*/ */
class TestGetSessionFromExtraField extends V2TestCase class GetSessionFromExtraFieldTest extends V2TestCase
{ {
protected function action() protected function action()
{ {

@ -0,0 +1,2 @@
Command to run the tests :
vendor/phpunit/phpunit/phpunit main/webservices/api/tests

@ -1,15 +1,17 @@
<?php <?php
require 'V2TestCase.php'; /* For licensing terms, see /license.txt */
require_once __DIR__.'/V2TestCase.php';
require_once __DIR__.'/../../../../vendor/autoload.php'; require_once __DIR__.'/../../../../vendor/autoload.php';
/** /**
* Class TestSaveUser * Class SaveUserTest
* *
* SAVE_USER webservice unit tests * SAVE_USER webservice unit tests
*/ */
class TestSaveUser extends V2TestCase class SaveUserTest extends V2TestCase
{ {
protected function action() protected function action()
{ {

@ -1,15 +1,17 @@
<?php <?php
require 'V2TestCase.php'; /* For licensing terms, see /license.txt */
require_once __DIR__.'/V2TestCase.php';
require_once __DIR__.'/../../../../vendor/autoload.php'; require_once __DIR__.'/../../../../vendor/autoload.php';
/** /**
* Class TestSubscribeUserToSessionFromUsername * Class SubscribeUserToSessionFromUsernameTest
* *
* SUBSCRIBE_USER_TO_SESSION_FROM_USERNAME webservice unit tests * SUBSCRIBE_USER_TO_SESSION_FROM_USERNAME webservice unit tests
*/ */
class TestSubscribeUserToSessionFromUsername extends V2TestCase class SubscribeUserToSessionFromUsernameTest extends V2TestCase
{ {
protected function action() protected function action()
{ {

@ -1,15 +1,17 @@
<?php <?php
require 'V2TestCase.php'; /* For licensing terms, see /license.txt */
require_once __DIR__.'/V2TestCase.php';
require_once __DIR__.'/../../../../vendor/autoload.php'; require_once __DIR__.'/../../../../vendor/autoload.php';
/** /**
* Class TestUpdateUserFromUsername * Class UpdateUserFromUsernameTest
* *
* UPDATE_USER_FROM_USERNAME webservice unit tests * UPDATE_USER_FROM_USERNAME webservice unit tests
*/ */
class TestUpdateUserFromUsername extends V2TestCase class UpdateUserFromUsernameTest extends V2TestCase
{ {
protected function action() protected function action()
{ {
@ -76,10 +78,10 @@ class TestUpdateUserFromUsername extends V2TestCase
$userManager = UserManager::getManager(); $userManager = UserManager::getManager();
$user = $userManager->find($userId); $user = $userManager->find($userId);
$userManager->reloadUser($user); $userManager->reloadUser($user);
/* $this->assertSame($newFirstName, $user->newFirstname()); $this->assertSame($newFirstName, $user->getFirstname());
$this->assertSame($newLastName, $user->newLastName()); $this->assertSame($newLastName, $user->getLastname());
$this->assertSame($newStatus, $user->newStatus()); $this->assertSame($newStatus, $user->getStatus());
$this->assertSame($newEmail, $user->newEmail());*/ $this->assertSame($newEmail, $user->getEmail());
// assert extra field values have been updated // assert extra field values have been updated
$extraFieldValueModel = new ExtraFieldValue('user'); $extraFieldValueModel = new ExtraFieldValue('user');

@ -82,6 +82,7 @@ abstract class V2TestCase extends TestCase
$this->assertSame(200, $response->getStatusCode()); $this->assertSame(200, $response->getStatusCode());
$decodedResponse = json_decode($response->getBody()->getContents()); $decodedResponse = json_decode($response->getBody()->getContents());
$this->assertNotNull($decodedResponse); $this->assertNotNull($decodedResponse);
return $decodedResponse; return $decodedResponse;
} }
@ -102,6 +103,7 @@ abstract class V2TestCase extends TestCase
$this->assertTrue(property_exists($decodedResponse, 'message')); $this->assertTrue(property_exists($decodedResponse, 'message'));
$message = $decodedResponse->message; $message = $decodedResponse->message;
$this->assertIsString($message); $this->assertIsString($message);
return $message; return $message;
} }
@ -117,9 +119,11 @@ abstract class V2TestCase extends TestCase
$this->assertIsObject($decodedResponse); $this->assertIsObject($decodedResponse);
$this->assertTrue( $this->assertTrue(
property_exists($decodedResponse, 'data'), property_exists($decodedResponse, 'data'),
'response data property is missing: '.print_r($decodedResponse, true)); 'response data property is missing: '.print_r($decodedResponse, true)
);
$data = $decodedResponse->data; $data = $decodedResponse->data;
$this->assertIsArray($data); $this->assertIsArray($data);
return $data; return $data;
} }
@ -133,6 +137,7 @@ abstract class V2TestCase extends TestCase
{ {
$data = $this->dataArray($parameters); $data = $this->dataArray($parameters);
$this->assertSame(1, count($data)); $this->assertSame(1, count($data));
return $data[0]; return $data[0];
} }
@ -146,6 +151,7 @@ abstract class V2TestCase extends TestCase
{ {
$value = $this->singleElementValue($parameters); $value = $this->singleElementValue($parameters);
$this->assertIsInt($value); $this->assertIsInt($value);
return $value; return $value;
} }
@ -159,6 +165,7 @@ abstract class V2TestCase extends TestCase
{ {
$value = $this->singleElementValue($parameters); $value = $this->singleElementValue($parameters);
$this->assertIsBool($value); $this->assertIsBool($value);
return $value; return $value;
} }
} }

@ -298,25 +298,25 @@ try {
$_POST['startDate'], $_POST['startDate'],
$_POST['endDate'], $_POST['endDate'],
isset($_POST['extraFields']) ? $_POST['extraFields'] : []); isset($_POST['extraFields']) ? $_POST['extraFields'] : []);
$restResponse->setData($newSessionId); $restResponse->setData([$newSessionId]);
break; break;
case Rest::SUBSCRIBE_USER_TO_SESSION_FROM_USERNAME: case Rest::SUBSCRIBE_USER_TO_SESSION_FROM_USERNAME:
if (empty($_POST['sessionId']) || empty($_POST['loginName'])) { if (empty($_POST['sessionId']) || empty($_POST['loginName'])) {
throw new Exception(get_lang('NoData')); throw new Exception(get_lang('NoData'));
} }
$subscribed = $restApi->subscribeUserToSessionFromUsername($_POST['sessionId'], $_POST['loginName']); $subscribed = $restApi->subscribeUserToSessionFromUsername($_POST['sessionId'], $_POST['loginName']);
$restResponse->setData($subscribed); $restResponse->setData([$subscribed]);
break; break;
case Rest::GET_SESSION_FROM_EXTRA_FIELD; case Rest::GET_SESSION_FROM_EXTRA_FIELD;
if (empty($_POST['field_name']) || empty($_POST['field_value'])) { if (empty($_POST['field_name']) || empty($_POST['field_value'])) {
throw new Exception(get_lang('NoData')); throw new Exception(get_lang('NoData'));
} }
$idSession = $restApi->getSessionFromExtraField($_POST['field_name'], $_POST['field_value']); $idSession = $restApi->getSessionFromExtraField($_POST['field_name'], $_POST['field_value']);
$restResponse->setData($idSession); $restResponse->setData([$idSession]);
break; break;
case Rest::UPDATE_USER_FROM_USERNAME: case Rest::UPDATE_USER_FROM_USERNAME:
$data = $restApi->updateUserFromUserName($_POST); $data = $restApi->updateUserFromUserName($_POST);
$restResponse->setData($data); $restResponse->setData([$data]);
break; break;
default: default:
throw new Exception(get_lang('InvalidAction')); throw new Exception(get_lang('InvalidAction'));

Loading…
Cancel
Save