Allow serve session tools in Api REST - refs #8366

pull/2487/head
Angel Fernando Quiroz Campos 9 years ago
parent 04c993f77e
commit d4b374eef7
  1. 18
      main/inc/lib/agenda.lib.php
  2. 6
      main/inc/lib/document.lib.php
  3. 234
      main/inc/lib/webservices/Rest.php
  4. 49
      main/webservices/api/v2.php

@ -21,8 +21,11 @@ class Agenda
/**
* Constructor
* @param int $senderId Optional The user sender ID
* @param int $courseId Opitonal. The course ID
* @param int $sessionId Optional The session ID
*/
public function __construct()
public function __construct($senderId = 0, $courseId = 0, $sessionId = 0)
{
//Table definitions
$this->tbl_global_agenda = Database::get_main_table(TABLE_MAIN_SYSTEM_CALENDAR);
@ -32,12 +35,12 @@ class Agenda
//Setting the course object if we are in a course
unset($this->course);
$courseInfo = api_get_course_info();
$courseInfo = api_get_course_info_by_id($courseId);
if (!empty($courseInfo)) {
$this->course = $courseInfo;
}
$this->setSessionId(api_get_session_id());
$this->setSenderId(api_get_user_id());
$this->setSessionId($sessionId ?: api_get_session_id());
$this->setSenderId($senderId ?: api_get_user_id());
$this->setIsAllowedToEdit(api_is_allowed_to_edit(null, true));
$this->events = array();
@ -928,7 +931,6 @@ class Agenda
$this->getPlatformEvents($start, $end);
break;
case 'course':
$session_id = $this->sessionId;
$courseInfo = api_get_course_info_by_id($course_id);
// Session coach can see all events inside a session.
@ -940,7 +942,7 @@ class Agenda
$end,
$courseInfo,
$groupId,
$session_id,
$this->sessionId,
$user_id
);
@ -948,7 +950,7 @@ class Agenda
$this->getSessionEvents(
$start,
$end,
api_get_session_id(),
$this->sessionId,
$user_id,
$this->eventOtherSessionColor
);
@ -958,7 +960,7 @@ class Agenda
$end,
$courseInfo,
$groupId,
$session_id,
$this->sessionId,
$user_id
);
}

@ -535,6 +535,7 @@ class DocumentManager
* @param int $to_user_id
* @param boolean $can_see_invisible
* @param boolean $search
* @param int $sessionId
* @return array with all document data
*/
public static function get_all_document_data(
@ -543,7 +544,8 @@ class DocumentManager
$to_group_id = 0,
$to_user_id = null,
$can_see_invisible = false,
$search = false
$search = false,
$sessionId = 0
) {
$TABLE_ITEMPROPERTY = Database::get_course_table(TABLE_ITEM_PROPERTY);
$TABLE_DOCUMENT = Database::get_course_table(TABLE_DOCUMENT);
@ -574,7 +576,7 @@ class DocumentManager
$added_slash = $path == '/' ? '' : '/';
// Condition for the session
$sessionId = api_get_session_id();
$sessionId = $sessionId ?: api_get_session_id();
$condition_session = " AND (last.session_id = '$sessionId' OR (last.session_id = '0' OR last.session_id IS NULL) )";
$condition_session .= self::getSessionFolderFilters($originalPath, $sessionId);

@ -6,7 +6,7 @@ use Chamilo\CoreBundle\Entity\ExtraFieldValues;
use Chamilo\CourseBundle\Entity\Repository\CAnnouncementRepository;
use Chamilo\CourseBundle\Entity\Repository\CNotebookRepository;
use Chamilo\CourseBundle\Entity\CLpCategory;
use ChamiloSession as Session;
use Chamilo\CoreBundle\Entity\Session;
/**
* Class RestApi
@ -38,6 +38,15 @@ class Rest extends WebService
const EXTRAFIELD_GCM_ID = 'gcm_registration_id';
/**
* @var Session
*/
private $session;
/**
* @var Course
*/
private $course;
/**
* Rest constructor.
* @param string $username
@ -48,6 +57,45 @@ class Rest extends WebService
parent::__construct($username, $apiKey);
}
public function setCourse($id)
{
if (!$id) {
$this->course = null;
return;
}
$em = Database::getManager();
/** @var Course $course */
$course = $em->find('ChamiloCoreBundle:Course', $id);
if (!$course) {
throw new Exception(get_lang('NoCourse'));
}
$this->course = $course;
}
/** Set the current session */
public function setSession($id)
{
if (!$id) {
$this->session = null;
return;
}
$em = Database::getManager();
/** @var Session $session */
$session = $em->find('ChamiloCoreBundle:Session', $id);
if (!$session) {
throw new Exception(get_lang('NoSession'));
}
$this->session = $session;
}
/**
* @param string $username
* @param string $apiKeyToValidate
@ -158,47 +206,31 @@ class Rest extends WebService
}
/**
* @param int $courseId
* @return array
* @throws Exception
*/
public function getCourseInfo($courseId)
public function getCourseInfo()
{
/** @var Course $course */
$course = Database::getManager()->find('ChamiloCoreBundle:Course', $courseId);
if (!$course) {
throw new Exception(get_lang('NoCourse'));
}
$teachers = CourseManager::get_teacher_list_from_course_code_to_string($course->getCode());
$teachers = CourseManager::get_teacher_list_from_course_code_to_string($this->course->getCode());
return [
'id' => $course->getId(),
'title' => $course->getTitle(),
'code' => $course->getCode(),
'directory' => $course->getDirectory(),
'urlPicture' => $course->getPicturePath(true),
'id' => $this->course->getId(),
'title' => $this->course->getTitle(),
'code' => $this->course->getCode(),
'directory' => $this->course->getDirectory(),
'urlPicture' => $this->course->getPicturePath(true),
'teachers' => $teachers
];
}
/**
* Get the course descriptions
* @param int $courseId
* @return array
* @throws Exception
*/
public function getCourseDescriptions($courseId)
public function getCourseDescriptions()
{
/** @var Course $course */
$course = Database::getManager()->find('ChamiloCoreBundle:Course', $courseId);
if (!$course) {
throw new Exception(get_lang('NoCourse'));
}
$descriptions = CourseDescription::get_descriptions($course->getId());
$descriptions = CourseDescription::get_descriptions($this->course->getId());
$results = [];
/** @var CourseDescription $description */
@ -214,25 +246,24 @@ class Rest extends WebService
}
/**
* @param int $courseId
* @param int $directoryId
* @return array
* @throws Exception
*/
public function getCourseDocuments($courseId, $directoryId = 0)
public function getCourseDocuments($directoryId = 0)
{
/** @var Course $course */
$course = Database::getManager()->find('ChamiloCoreBundle:Course', $courseId);
if (!$course) {
throw new Exception(get_lang('NoCourse'));
}
/** @var string $path */
$path = '/';
$sessionId = $this->session ? $this->session->getId() : 0;
if ($directoryId) {
$directory = DocumentManager::get_document_data_by_id($directoryId, $course->getCode(), false, 0);
$directory = DocumentManager::get_document_data_by_id(
$directoryId,
$this->course->getCode(),
false,
$sessionId
);
if (!$directory) {
throw new Exception('NoDataAvailable');
@ -243,9 +274,9 @@ class Rest extends WebService
require_once api_get_path(LIBRARY_PATH) . 'fileDisplay.lib.php';
$courseInfo = api_get_course_info_by_id($course->getId());
$courseInfo = api_get_course_info_by_id($this->course->getId());
$documents = DocumentManager::get_all_document_data($courseInfo, $path);
$documents = DocumentManager::get_all_document_data($courseInfo, $path, 0, null, false, false, $sessionId);
$results = [];
if (is_array($documents)) {
@ -269,8 +300,8 @@ class Rest extends WebService
'url' => $webPath . http_build_query([
'username' => $this->user->getUsername(),
'api_key' => $this->apiKey,
'cidReq' => $course->getCode(),
'id_session' => 0,
'cidReq' => $this->course->getCode(),
'id_session' => $sessionId,
'gidReq' => 0,
'gradebook' => 0,
'origin' => '',
@ -291,14 +322,9 @@ class Rest extends WebService
* @return array
* @throws Exception
*/
public function getCourseAnnouncements($courseId)
public function getCourseAnnouncements()
{
/** @var Course $course */
$course = Database::getManager()->find('ChamiloCoreBundle:Course', $courseId);
if (!$course) {
throw new Exception(get_lang('NoCourse'));
}
$sessionId = $this->session ? $this->session->getId() : 0;
$announcements = AnnouncementManager::getAnnouncements(
null,
@ -311,7 +337,8 @@ class Rest extends WebService
null,
0,
$this->user->getId(),
$courseId
$this->course->getId(),
$sessionId
);
$announcements = array_map(function ($announcement) {
@ -328,22 +355,15 @@ class Rest extends WebService
/**
* @param int $announcementId
* @param int $courseId
* @return array
* @throws Exception
*/
public function getCourseAnnouncement($announcementId, $courseId)
public function getCourseAnnouncement($announcementId)
{
/** @var Course $course */
$course = Database::getManager()->find('ChamiloCoreBundle:Course', $courseId);
if (!$course) {
throw new Exception(get_lang('NoCourse'));
}
$sessionId = $this->session ? $this->session->getId() : 0;
$announcement = AnnouncementManager::getAnnouncementInfoById(
$announcementId,
$course->getId(),
$this->course->getId(),
$this->user->getId()
);
@ -359,26 +379,21 @@ class Rest extends WebService
'content' => AnnouncementManager::parse_content(
$this->user->getId(),
$announcement['announcement']->getContent(),
$course->getCode()
$this->course->getCode(),
$sessionId
)
];
}
/**
* @param int $courseId
* @return array
* @throws Exception
*/
public function getCourseAgenda($courseId)
public function getCourseAgenda()
{
/** @var Course $course */
$course = Database::getManager()->find('ChamiloCoreBundle:Course', $courseId);
$sessionId = $this->session ? $this->session->getId() : 0;
if (!$course) {
throw new Exception(get_lang('NoCourse'));
}
$agenda = new Agenda();
$agenda = new Agenda($this->user->getId(), $this->course->getId(), $sessionId);
$agenda->setType('course');
$result = $agenda->parseAgendaFilter(null);
@ -393,7 +408,7 @@ class Rest extends WebService
$events = $agenda->getEvents(
$start->getTimestamp(),
$end->getTimestamp(),
$course->getId(),
$this->course->getId(),
$groupId,
$userId,
'array'
@ -421,23 +436,15 @@ class Rest extends WebService
}
/**
* @param int $courseId
* @return array
* @throws Exception
*/
public function getCourseNotebooks($courseId)
public function getCourseNotebooks()
{
$em = Database::getManager();
/** @var Course $course */
$course = $em->find('ChamiloCoreBundle:Course', $courseId);
if (!$course) {
throw new Exception(get_lang('NoCourse'));
}
/** @var CNotebookRepository $notebooksRepo */
$notebooksRepo = $em->getRepository('ChamiloCourseBundle:CNotebook');
$notebooks = $notebooksRepo->findByUser($this->user, $course, null);
$notebooks = $notebooksRepo->findByUser($this->user, $this->course, $this->session);
return array_map(
function (\Chamilo\CourseBundle\Entity\CNotebook $notebook) {
@ -458,28 +465,20 @@ class Rest extends WebService
}
/**
* @param int $courseId
* @return array
* @throws Exception
*/
public function getCourseForumCategories($courseId)
public function getCourseForumCategories()
{
$em = Database::getManager();
/** @var Course $course */
$course = $em->find('ChamiloCoreBundle:Course', $courseId);
if (!$course) {
throw new Exception(get_lang('NoCourse'));
}
$webCoursePath = api_get_path(WEB_COURSE_PATH) . $course->getDirectory() . '/upload/forum/images/';
$sessionId = $this->session ? $this->session->getId() : 0;
$webCoursePath = api_get_path(WEB_COURSE_PATH) . $this->course->getDirectory() . '/upload/forum/images/';
require_once api_get_path(SYS_CODE_PATH) . 'forum/forumfunction.inc.php';
$categoriesFullData = get_forum_categories('', $course->getId());
$categoriesFullData = get_forum_categories('', $this->course->getId(), $sessionId);
$categories = [];
$includeGroupsForums = api_get_setting('display_groups_forum_in_general_tool') === 'true';
$forumsFullData = get_forums('', $course->getCode(), $includeGroupsForums);
$forumsFullData = get_forums('', $this->course->getCode(), $includeGroupsForums, $sessionId);
$forums = [];
foreach ($forumsFullData as $forumId => $forumInfo) {
@ -493,7 +492,7 @@ class Rest extends WebService
'lastPost' => null
];
$lastPostInfo = get_last_post_information($forumId, false, $course->getId());
$lastPostInfo = get_last_post_information($forumId, false, $this->course->getId());
if ($lastPostInfo) {
$forum['lastPost'] = [
@ -526,7 +525,7 @@ class Rest extends WebService
'catId' => intval($category['cat_id']),
'description' => $category['cat_comment'],
'forums' => $categoryForums,
'courseId' => $course->getId()
'courseId' => $this->course->getId()
];
}
@ -649,21 +648,13 @@ class Rest extends WebService
}
/**
* @param int $courseId
* @return array
* @throws Exception
*/
public function getCourseLearnPaths($courseId)
public function getCourseLearnPaths()
{
$em = Database::getManager();
/** @var Course $course */
$course = $em->find('ChamiloCoreBundle:Course', $courseId);
if (!$course) {
throw new Exception(get_lang('NoCourse'));
}
$categoriesTempList = learnpath::getCategories($courseId);
$sessionId = $this->session ? $this->session->getId() : 0;
$categoriesTempList = learnpath::getCategories($this->course->getId());
$categoryNone = new \Chamilo\CourseBundle\Entity\CLpCategory();
$categoryNone->setId(0);
@ -678,8 +669,8 @@ class Rest extends WebService
foreach ($categories as $category) {
$learnPathList = new LearnpathList(
$this->user->getId(),
$course->getCode(),
null,
$this->course->getCode(),
$sessionId,
null,
false,
$category->getId()
@ -701,7 +692,8 @@ class Rest extends WebService
if (!learnpath::is_lp_visible_for_student(
$lpId,
$this->user->getId(),
$course->getCode()
$this->course->getCode(),
$sessionId
)) {
continue;
}
@ -735,7 +727,7 @@ class Rest extends WebService
}
}
$progress = learnpath::getProgress($lpId, $this->user->getId(), $courseId);
$progress = learnpath::getProgress($lpId, $this->user->getId(), $this->course->getId(), $sessionId);
$listData[] = array(
'id' => $lpId,
@ -745,9 +737,8 @@ class Rest extends WebService
'hash' => $this->encodeParams([
'action' => 'course_learnpath',
'lp_id' => $lpId,
'cidReq' => $course->getCode(),
'id_session' => 0,
'gidReq' => 0
'course' => $this->course->getId(),
'session' => $sessionId
])
])
);
@ -805,21 +796,27 @@ class Rest extends WebService
/**
* Start login for a user. Then make a redirect to show the learnpath
* @param int $lpId
* @param string $courseCode
*/
public function showLearningPath($lpId, $courseCode)
public function showLearningPath($lpId)
{
$loggedUser['user_id'] = $this->user->getId();
$loggedUser['status'] = $this->user->getStatus();
$loggedUser['uidReset'] = true;
Session::write('_user', $loggedUser);
$sessionId = $this->session ? $this->session->getId() : 0;
ChamiloSession::write('_user', $loggedUser);
Login::init_user($this->user->getId(), true);
$url = api_get_path(WEB_CODE_PATH) . 'lp/lp_controller.php?' . http_build_query([
'cidReq' => $this->course->getCode(),
'id_session' => $sessionId,
'gidReq' => 0,
'gradebook' => 0,
'origin' => '',
'action' => 'view',
'lp_id' => intval($lpId),
'cidReq' => $courseCode
'isStudentView' => 'true'
]);
header("Location: $url");
@ -829,16 +826,15 @@ class Rest extends WebService
/**
* @param array $postValues
* @param int $forumId
* @param int $courseId
* @return array
*/
public function saveForumPost(array $postValues, $forumId, $courseId)
public function saveForumPost(array $postValues, $forumId)
{
require_once api_get_path(SYS_CODE_PATH) . 'forum/forumfunction.inc.php';
$forum = get_forums($forumId);
store_reply($forum, $postValues, $courseId, $this->user->getId());
store_reply($forum, $postValues, $this->course->getId(), $this->user->getId());
return [
'registered' => true

@ -16,6 +16,8 @@ if ($hash) {
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : null;
$username = isset($_REQUEST['username']) ? Security::remove_XSS($_REQUEST['username']) : null;
$apiKey = isset($_REQUEST['api_key']) ? Security::remove_XSS($_REQUEST['api_key']) : null;
$course = !empty($_REQUEST['course']) ? intval($_REQUEST['course']) : null;
$session = !empty($_REQUEST['session']) ? intval($_REQUEST['session']) : null;
$restResponse = new RestResponse();
@ -23,6 +25,11 @@ try {
/** @var Rest $restApi */
$restApi = $apiKey ? Rest::validate($username, $apiKey) : null;
if ($restApi) {
$restApi->setCourse($course);
$restApi->setSession($session);
}
switch ($action) {
case Rest::ACTION_AUTH:
Rest::init();
@ -65,22 +72,18 @@ try {
break;
case Rest::ACTION_COURSE_INFO:
$courseId = isset($_POST['c_id']) ? Security::remove_XSS($_POST['c_id']) : 0;
$courseInfo = $restApi->getCourseInfo($courseId);
$courseInfo = $restApi->getCourseInfo();
$restResponse->setData($courseInfo);
break;
case Rest::ACTION_COURSE_DESCRIPTIONS:
$courseId = isset($_POST['c_id']) ? Security::remove_XSS($_POST['c_id']) : 0;
$descriptions = $restApi->getCourseDescriptions($courseId);
$descriptions = $restApi->getCourseDescriptions();
$restResponse->setData($descriptions);
break;
case Rest::ACTION_COURSE_DOCUMENTS:
$courseId = isset($_POST['c_id']) ? Security::remove_XSS($_POST['c_id']) : 0;
$directoryId = isset($_POST['dir_id']) ? Security::remove_XSS($_POST['dir_id']) : null;
$documents = $restApi->getCourseDocuments($courseId, $directoryId);
@ -89,42 +92,33 @@ try {
break;
case Rest::ACTION_COURSE_ANNOUNCEMENTS:
$courseId = isset($_POST['c_id']) ? Security::remove_XSS($_POST['c_id']) : 0;
$announcements = $restApi->getCourseAnnouncements($courseId);
$announcements = $restApi->getCourseAnnouncements();
$restResponse->setData($announcements);
break;
case Rest::ACTION_COURSE_ANNOUNCEMENT:
$courseId = isset($_POST['c_id']) ? Security::remove_XSS($_POST['c_id']) : 0;
$announcementId = isset($_POST['a_id']) ? Security::remove_XSS($_POST['a_id']) : 0;
$announcementId = isset($_POST['announcement']) ? Security::remove_XSS($_POST['announcement']) : 0;
$announcement = $restApi->getCourseAnnouncement($announcementId, $courseId);
$announcement = $restApi->getCourseAnnouncement($announcementId);
$restResponse->setData($announcement);
break;
case Rest::ACTION_COURSE_AGENDA:
$courseId = isset($_POST['c_id']) ? Security::remove_XSS($_POST['c_id']) : 0;
$agenda = $restApi->getCourseAgenda($courseId);
$agenda = $restApi->getCourseAgenda();
$restResponse->setData($agenda);
break;
case Rest::ACTION_COURSE_NOTEBOOKS:
$courseId = isset($_POST['c_id']) ? Security::remove_XSS($_POST['c_id']) : 0;
$notebooks = $restApi->getCourseNotebooks($courseId);
$notebooks = $restApi->getCourseNotebooks();
$restResponse->setData($notebooks);
break;
case Rest::ACTION_COURSE_FORUM_CATEGORIES:
$courseId = isset($_POST['c_id']) ? Security::remove_XSS($_POST['c_id']) : 0;
$forums = $restApi->getCourseForumCategories($courseId);
$forums = $restApi->getCourseForumCategories();
$restResponse->setData($forums);
break;
@ -152,29 +146,24 @@ try {
break;
case Rest::ACTION_COURSE_LEARNPATHS:
$courseId = isset($_POST['c_id']) ? Security::remove_XSS($_POST['c_id']) : 0;
$data = $restApi->getCourseLearnPaths($courseId);
$data = $restApi->getCourseLearnPaths();
$restResponse->setData($data);
break;
case Rest::ACTION_COURSE_LEARNPATH:
$lpId = isset($_REQUEST['lp_id']) ? intval($_REQUEST['lp_id']) : 0;
$cidReq = isset($_REQUEST['cidReq']) ? Security::remove_XSS($_REQUEST['cidReq']) : 0;
$restApi->showLearningPath($lpId, $cidReq);
$restApi->showLearningPath($lpId);
break;
case Rest::ACTION_SAVE_FORUM_POST:
if (
empty($_POST['title']) || empty($_POST['text']) || empty($_POST['thread']) || empty($_POST['forum']) ||
empty($_POST['course'])
empty($_POST['title']) || empty($_POST['text']) || empty($_POST['thread']) || empty($_POST['forum'])
) {
throw new Exception(get_lang('NoData'));
}
$courseId = intval($_POST['course']);
$notify = !empty($_POST['notify']);
$parentId = !empty($_POST['parent']) ? intval($_POST['parent']) : null;
@ -187,7 +176,7 @@ try {
'post_parent_id' => $parentId
];
$data = $restApi->saveForumPost($postValues, $forumId, $courseId);
$data = $restApi->saveForumPost($postValues, $forumId);
$restResponse->setData($data);
break;

Loading…
Cancel
Save