commit
1f7eba21cd
@ -0,0 +1,54 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Application\Migrations\Schema\V111; |
||||
|
||||
use Application\Migrations\AbstractMigrationChamilo; |
||||
use Doctrine\DBAL\Schema\Schema; |
||||
use Doctrine\DBAL\Types\Type; |
||||
|
||||
/** |
||||
* Class Version20160825155200 |
||||
* Add option to allow download documents with the api key |
||||
* @package Application\Migrations\Schema\V111 |
||||
*/ |
||||
class Version20160825155200 extends AbstractMigrationChamilo |
||||
{ |
||||
/** |
||||
* @param Schema $schema |
||||
* @throws \Doctrine\DBAL\DBALException |
||||
* @throws \Doctrine\DBAL\Schema\SchemaException |
||||
*/ |
||||
public function up(Schema $schema) |
||||
{ |
||||
$this->addSettingCurrent( |
||||
'allow_download_documents_by_api_key', |
||||
null, |
||||
'radio', |
||||
'WebServices', |
||||
'false', |
||||
'AllowDownloadDocumentsByApiKeyTitle', |
||||
'AllowDownloadDocumentsByApiKeyComment', |
||||
null, |
||||
null, |
||||
1, |
||||
true, |
||||
true, |
||||
[ |
||||
['value' => 'false', 'text' => 'No'], |
||||
['value' => 'true', 'text' => 'Yes'] |
||||
] |
||||
); |
||||
} |
||||
|
||||
/** |
||||
* @param Schema $schema |
||||
* @throws \Doctrine\DBAL\DBALException |
||||
* @throws \Doctrine\DBAL\Schema\SchemaException |
||||
*/ |
||||
public function down(Schema $schema) |
||||
{ |
||||
$this->addSql("DELETE FROM settings_current WHERE variable = 'allow_download_documents_by_api_key'"); |
||||
$this->addSql("DELETE FROM settings_options WHERE variable = 'allow_download_documents_by_api_key'"); |
||||
} |
||||
} |
@ -1,237 +0,0 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
use Chamilo\UserBundle\Entity\User; |
||||
|
||||
/** |
||||
* Class for manage the messages web service |
||||
* @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com> |
||||
* @package chamilo.webservices.messages |
||||
*/ |
||||
class MessagesWebService extends WebService |
||||
{ |
||||
const SERVICE_NAME = 'MsgREST'; |
||||
/** |
||||
* @var string EXTRA_FIELD_GCM_REGISTRATION Variable name of the user extra field. |
||||
* Necessary for register the registration token from the Google Cloud Messaging |
||||
*/ |
||||
const EXTRA_FIELD_GCM_REGISTRATION = 'gcm_registration_id'; |
||||
|
||||
/** |
||||
* Generate the api key for a user |
||||
* @param int $userId The user id |
||||
* @return string The api key |
||||
*/ |
||||
public function generateApiKey($userId) |
||||
{ |
||||
$apiKey = UserManager::get_api_keys($userId, self::SERVICE_NAME); |
||||
|
||||
if (empty($apiKey)) { |
||||
UserManager::add_api_key($userId, self::SERVICE_NAME); |
||||
|
||||
$apiKey = UserManager::get_api_keys($userId, self::SERVICE_NAME); |
||||
} |
||||
|
||||
return current($apiKey); |
||||
} |
||||
|
||||
/** |
||||
* Get the user api key |
||||
* @param string $username The user name |
||||
* @return string The api key |
||||
*/ |
||||
public function getApiKey($username) |
||||
{ |
||||
$userInfo = api_get_user_info_from_username($username); |
||||
$userId = $userInfo['user_id']; |
||||
|
||||
if ($this->apiKey !== null) { |
||||
return $this->apiKey; |
||||
} else { |
||||
$this->apiKey = $this->generateApiKey($userId); |
||||
|
||||
return $this->apiKey; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Check if the api is valid for a user |
||||
* @param string $username The username |
||||
* @param string $apiKeyToValidate The api key |
||||
* @return boolean Whether the api belongs to the user return true. Otherwise return false |
||||
*/ |
||||
public static function isValidApiKey($username, $apiKeyToValidate) |
||||
{ |
||||
$userInfo = api_get_user_info_from_username($username); |
||||
$userId = $userInfo['user_id']; |
||||
|
||||
$apiKeys = UserManager::get_api_keys($userId, self::SERVICE_NAME); |
||||
|
||||
if (!empty($apiKeys)) { |
||||
$apiKey = current($apiKeys); |
||||
|
||||
if ($apiKey == $apiKeyToValidate) { |
||||
return true; |
||||
} |
||||
} |
||||
|
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* Get the count of new messages for a user |
||||
* @param string $username The username |
||||
* @param int $lastId The id of the last received message |
||||
* @return int The count fo new messages |
||||
*/ |
||||
public function countNewMessages($username, $lastId = 0) |
||||
{ |
||||
$userInfo = api_get_user_info_from_username($username); |
||||
$userId = $userInfo['user_id']; |
||||
|
||||
return MessageManager::countMessagesFromLastReceivedMessage($userId, $lastId); |
||||
} |
||||
|
||||
/** |
||||
* Get the list of new messages for a user |
||||
* @param string $username The username |
||||
* @param int $lastId The id of the last received message |
||||
* @return array the new message list |
||||
*/ |
||||
public function getNewMessages($username, $lastId = 0) |
||||
{ |
||||
$messages = array(); |
||||
|
||||
$userInfo = api_get_user_info_from_username($username); |
||||
$userId = $userInfo['user_id']; |
||||
|
||||
$lastMessages = MessageManager::getMessagesFromLastReceivedMessage($userId, $lastId); |
||||
|
||||
foreach ($lastMessages as $message) { |
||||
$hasAttachments = MessageManager::hasAttachments($message['id']); |
||||
|
||||
$messages[] = array( |
||||
'id' => $message['id'], |
||||
'title' => $message['title'], |
||||
'sender' => array( |
||||
'id' => $message['user_id'], |
||||
'lastname' => $message['lastname'], |
||||
'firstname' => $message['firstname'], |
||||
'completeName' => api_get_person_name($message['firstname'], $message['lastname']), |
||||
), |
||||
'sendDate' => $message['send_date'], |
||||
'content' => $message['content'], |
||||
'hasAttachments' => $hasAttachments, |
||||
'platform' => array( |
||||
'website' => api_get_path(WEB_PATH), |
||||
'messagingTool' => api_get_path(WEB_PATH) . 'main/messages/inbox.php' |
||||
) |
||||
); |
||||
} |
||||
|
||||
return $messages; |
||||
} |
||||
|
||||
/** |
||||
* Create the user extra field |
||||
*/ |
||||
public static function init() |
||||
{ |
||||
$extraField = new ExtraField('user'); |
||||
$fieldInfo = $extraField->get_handler_field_info_by_field_variable(self::EXTRA_FIELD_GCM_REGISTRATION); |
||||
|
||||
if (empty($fieldInfo)) { |
||||
$extraField->save([ |
||||
'variable' => self::EXTRA_FIELD_GCM_REGISTRATION, |
||||
'field_type' => ExtraField::FIELD_TYPE_TEXT, |
||||
'display_text' => self::EXTRA_FIELD_GCM_REGISTRATION |
||||
]); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Register the Registration ID (token) obtained from Google Cloud Messaging for a user |
||||
* @param User $user The user |
||||
* @param string $registrationId The token registration id from Google Cloud Messaging |
||||
* |
||||
* @return int The id after insert or the number of affected rows after update. Otherwhise return false |
||||
*/ |
||||
public static function setGcmRegistrationId(User $user, $registrationId) |
||||
{ |
||||
$registrationId = Security::remove_XSS($registrationId); |
||||
$extraFieldValue = new ExtraFieldValue('user'); |
||||
|
||||
return $extraFieldValue->save([ |
||||
'variable' => self::EXTRA_FIELD_GCM_REGISTRATION, |
||||
'value' => $registrationId, |
||||
'item_id' => $user->getId() |
||||
]); |
||||
} |
||||
|
||||
/** |
||||
* Send the push notifications to MobileMessaging app |
||||
* @param array $userIds The IDs of users who will be notified |
||||
* @param string $title The notification title |
||||
* @param string $content The notification content |
||||
* @return int The number of success notifications. Otherwise returns false |
||||
*/ |
||||
public static function sendPushNotification(array $userIds, $title, $content) |
||||
{ |
||||
if (api_get_configuration_value('messaging_allow_send_push_notification') !== 'true') { |
||||
return false; |
||||
} |
||||
|
||||
$gdcApiKey = api_get_configuration_value('messaging_gdc_api_key'); |
||||
|
||||
if ($gdcApiKey === false) { |
||||
return false; |
||||
} |
||||
|
||||
$content = str_replace(['<br>', '<br/>', '<br />'], "\n", $content); |
||||
$content = strip_tags($content); |
||||
$content = html_entity_decode($content, ENT_QUOTES); |
||||
|
||||
$gcmRegistrationIds = []; |
||||
|
||||
foreach ($userIds as $userId) { |
||||
$extraFieldValue = new ExtraFieldValue('user'); |
||||
$valueInfo = $extraFieldValue->get_values_by_handler_and_field_variable( |
||||
$userId, |
||||
self::EXTRA_FIELD_GCM_REGISTRATION |
||||
); |
||||
|
||||
if (empty($valueInfo)) { |
||||
continue; |
||||
} |
||||
|
||||
$gcmRegistrationIds[] = $valueInfo['value']; |
||||
} |
||||
|
||||
$headers = [ |
||||
'Authorization: key=' . $gdcApiKey, |
||||
'Content-Type: application/json' |
||||
]; |
||||
|
||||
$fields = json_encode([ |
||||
'registration_ids' => $gcmRegistrationIds, |
||||
'data' => [ |
||||
'title' => $title, |
||||
'message' => $content |
||||
] |
||||
]); |
||||
|
||||
$ch = curl_init(); |
||||
curl_setopt($ch, CURLOPT_URL, 'https://gcm-http.googleapis.com/gcm/send'); |
||||
curl_setopt($ch, CURLOPT_POST, true); |
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); |
||||
$result = curl_exec($ch); |
||||
curl_close($ch); |
||||
|
||||
$decodedResult = json_decode($result); |
||||
|
||||
return $decodedResult->success; |
||||
} |
||||
} |
@ -0,0 +1,646 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
use Chamilo\UserBundle\Entity\User; |
||||
use Chamilo\CoreBundle\Entity\Course; |
||||
use Chamilo\CoreBundle\Entity\ExtraFieldValues; |
||||
use Chamilo\CourseBundle\Entity\Repository\CAnnouncementRepository; |
||||
use Chamilo\CourseBundle\Entity\CAnnouncement; |
||||
use Chamilo\CourseBundle\Entity\Repository\CNotebookRepository; |
||||
|
||||
/** |
||||
* Class RestApi |
||||
*/ |
||||
class Rest extends WebService |
||||
{ |
||||
const SERVIVE_NAME = 'MsgREST'; |
||||
const EXTRA_FIELD_GCM_REGISTRATION = 'gcm_registration_id'; |
||||
|
||||
const ACTION_AUTH = 'authenticate'; |
||||
const ACTION_USER_MESSAGES = 'user_messages'; |
||||
const ACTION_GCM_ID = 'gcm_id'; |
||||
const ACTION_USER_COURSES = 'user_courses'; |
||||
const ACTION_PROFILE = 'user_profile'; |
||||
const ACTION_COURSE_INFO = 'course_info'; |
||||
const ACTION_COURSE_DESCRIPTIONS = 'course_descriptions'; |
||||
const ACTION_COURSE_DOCUMENTS = 'course_documents'; |
||||
const ACTION_COURSE_ANNOUNCEMENTS = 'course_announcements'; |
||||
const ACTION_COURSE_ANNOUNCEMENT = 'course_announcement'; |
||||
const ACTION_COURSE_AGENDA = 'course_agenda'; |
||||
const ACTION_COURSE_NOTEBOOKS = 'course_notebooks'; |
||||
const ACTION_COURSE_FORUM_CATEGORIES = 'course_forumcategories'; |
||||
const ACTION_COURSE_FORUM = 'course_forum'; |
||||
const ACTION_COURSE_FORUM_THREAD = 'course_forumthread'; |
||||
|
||||
const EXTRAFIELD_GCM_ID = 'gcm_registration_id'; |
||||
|
||||
/** |
||||
* Rest constructor. |
||||
* @param string $username |
||||
* @param string $apiKey |
||||
*/ |
||||
public function __construct($username, $apiKey) |
||||
{ |
||||
parent::__construct($username, $apiKey); |
||||
} |
||||
|
||||
/** |
||||
* @param string $username |
||||
* @param string $apiKeyToValidate |
||||
* @return Rest |
||||
* @throws Exception |
||||
*/ |
||||
public static function validate($username, $apiKeyToValidate) |
||||
{ |
||||
$apiKey = self::findUserApiKey($username, self::SERVIVE_NAME); |
||||
|
||||
if ($apiKey != $apiKeyToValidate) { |
||||
throw new Exception(get_lang('InvalidApiKey')); |
||||
} |
||||
|
||||
return new self($username, $apiKey); |
||||
} |
||||
|
||||
/** |
||||
* Create the gcm_registration_id extra field for users |
||||
*/ |
||||
public static function init() |
||||
{ |
||||
$extraField = new ExtraField('user'); |
||||
$fieldInfo = $extraField->get_handler_field_info_by_field_variable(self::EXTRA_FIELD_GCM_REGISTRATION); |
||||
|
||||
if (empty($fieldInfo)) { |
||||
$extraField->save([ |
||||
'variable' => self::EXTRA_FIELD_GCM_REGISTRATION, |
||||
'field_type' => ExtraField::FIELD_TYPE_TEXT, |
||||
'display_text' => self::EXTRA_FIELD_GCM_REGISTRATION |
||||
]); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* @param string $registrationId |
||||
* @return bool |
||||
*/ |
||||
public function setGcmId($registrationId) |
||||
{ |
||||
$registrationId = Security::remove_XSS($registrationId); |
||||
$extraFieldValue = new ExtraFieldValue('user'); |
||||
|
||||
return $extraFieldValue->save([ |
||||
'variable' => self::EXTRA_FIELD_GCM_REGISTRATION, |
||||
'value' => $registrationId, |
||||
'item_id' => $this->user->getId() |
||||
]); |
||||
} |
||||
|
||||
/** |
||||
* @param int $lastMessageId |
||||
* @return array |
||||
*/ |
||||
public function getUserMessages($lastMessageId = 0) |
||||
{ |
||||
$lastMessages = MessageManager::getMessagesFromLastReceivedMessage($this->user->getId(), $lastMessageId); |
||||
$messages = []; |
||||
|
||||
foreach ($lastMessages as $message) { |
||||
$hasAttachments = MessageManager::hasAttachments($message['id']); |
||||
|
||||
$messages[] = array( |
||||
'id' => $message['id'], |
||||
'title' => $message['title'], |
||||
'sender' => array( |
||||
'id' => $message['user_id'], |
||||
'lastname' => $message['lastname'], |
||||
'firstname' => $message['firstname'], |
||||
'completeName' => api_get_person_name($message['firstname'], $message['lastname']), |
||||
), |
||||
'sendDate' => $message['send_date'], |
||||
'content' => $message['content'], |
||||
'hasAttachments' => $hasAttachments, |
||||
'url' => '' |
||||
); |
||||
} |
||||
|
||||
return $messages; |
||||
} |
||||
|
||||
/** |
||||
* Get the user courses |
||||
* @return array |
||||
*/ |
||||
public function getUserCourses() |
||||
{ |
||||
$courses = CourseManager::get_courses_list_by_user_id($this->user->getId()); |
||||
$data = []; |
||||
|
||||
foreach ($courses as $courseId) { |
||||
/** @var Course $course */ |
||||
$course = Database::getManager()->find('ChamiloCoreBundle:Course', $courseId['real_id']); |
||||
|
||||
$teachers = CourseManager::get_teacher_list_from_course_code_to_string($course->getCode()); |
||||
|
||||
$data[] = [ |
||||
'id' => $course->getId(), |
||||
'title' => $course->getTitle(), |
||||
'code' => $course->getCode(), |
||||
'directory' => $course->getDirectory(), |
||||
'urlPicture' => $course->getPicturePath(true), |
||||
'teachers' => $teachers |
||||
]; |
||||
} |
||||
|
||||
return $data; |
||||
} |
||||
|
||||
/** |
||||
* @param int $courseId |
||||
* @return array |
||||
* @throws Exception |
||||
*/ |
||||
public function getCourseInfo($courseId) |
||||
{ |
||||
/** @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()); |
||||
|
||||
return [ |
||||
'id' => $course->getId(), |
||||
'title' => $course->getTitle(), |
||||
'code' => $course->getCode(), |
||||
'directory' => $course->getDirectory(), |
||||
'urlPicture' => $course->getPicturePath(true), |
||||
'teachers' => $teachers |
||||
]; |
||||
} |
||||
|
||||
/** |
||||
* Get the course descriptions |
||||
* @param int $courseId |
||||
* @return array |
||||
* @throws Exception |
||||
*/ |
||||
public function getCourseDescriptions($courseId) |
||||
{ |
||||
/** @var Course $course */ |
||||
$course = Database::getManager()->find('ChamiloCoreBundle:Course', $courseId); |
||||
|
||||
if (!$course) { |
||||
throw new Exception(get_lang('NoCourse')); |
||||
} |
||||
|
||||
$descriptions = CourseDescription::get_descriptions($course->getId()); |
||||
$results = []; |
||||
|
||||
/** @var CourseDescription $description */ |
||||
foreach($descriptions as $description) { |
||||
$results[] = [ |
||||
'id' => $description->get_description_type(), |
||||
'title' => $description->get_title(), |
||||
'content' => str_replace('src="/', 'src="' . api_get_path(WEB_PATH), $description->get_content()) |
||||
]; |
||||
} |
||||
|
||||
return $results; |
||||
} |
||||
|
||||
/** |
||||
* @param int $courseId |
||||
* @param int $directoryId |
||||
* @return array |
||||
* @throws Exception |
||||
*/ |
||||
public function getCourseDocuments($courseId, $directoryId = 0) |
||||
{ |
||||
/** @var Course $course */ |
||||
$course = Database::getManager()->find('ChamiloCoreBundle:Course', $courseId); |
||||
|
||||
if (!$course) { |
||||
throw new Exception(get_lang('NoCourse')); |
||||
} |
||||
|
||||
/** @var string $path */ |
||||
$path = '/'; |
||||
|
||||
if ($directoryId) { |
||||
$directory = DocumentManager::get_document_data_by_id($directoryId, $course->getCode(), false, 0); |
||||
|
||||
if (!$directory) { |
||||
throw new Exception('NoDataAvailable'); |
||||
} |
||||
|
||||
$path = $directory['path']; |
||||
} |
||||
|
||||
require_once api_get_path(LIBRARY_PATH) . 'fileDisplay.lib.php'; |
||||
|
||||
$courseInfo = api_get_course_info_by_id($course->getId()); |
||||
|
||||
$documents = DocumentManager::get_all_document_data($courseInfo, $path); |
||||
$results = []; |
||||
|
||||
if (is_array($documents)) { |
||||
$webPath = api_get_path(WEB_CODE_PATH) . 'document/document.php?'; |
||||
|
||||
/** @var array $document */ |
||||
foreach($documents as $document) { |
||||
if ($document['visibility'] != '1') { |
||||
continue; |
||||
} |
||||
|
||||
$icon = $document['filetype'] == 'file' |
||||
? choose_image($document['path']) |
||||
: chooseFolderIcon($document['path']); |
||||
|
||||
$results[] = [ |
||||
'id' => $document['id'], |
||||
'type' => $document['filetype'], |
||||
'title' => $document['title'], |
||||
'path' => $document['path'], |
||||
'url' => $webPath . http_build_query([ |
||||
'username' => $this->user->getUsername(), |
||||
'api_key' => $this->apiKey, |
||||
'cidReq' => $course->getCode(), |
||||
'id_session' => 0, |
||||
'gidReq' => 0, |
||||
'gradebook' => 0, |
||||
'origin' => '', |
||||
'action' => 'download', |
||||
'id' => $document['id'] |
||||
]), |
||||
'icon' => $icon, |
||||
'size' => format_file_size($document['size']) |
||||
]; |
||||
} |
||||
} |
||||
|
||||
return $results; |
||||
} |
||||
|
||||
/** |
||||
* @param int $courseId |
||||
* @return array |
||||
* @throws Exception |
||||
*/ |
||||
public function getCourseAnnouncements($courseId) |
||||
{ |
||||
/** @var Course $course */ |
||||
$course = Database::getManager()->find('ChamiloCoreBundle:Course', $courseId); |
||||
|
||||
if (!$course) { |
||||
throw new Exception(get_lang('NoCourse')); |
||||
} |
||||
|
||||
$announcements = AnnouncementManager::getAnnouncements( |
||||
null, |
||||
null, |
||||
false, |
||||
null, |
||||
null, |
||||
null, |
||||
null, |
||||
null, |
||||
0, |
||||
$this->user->getId(), |
||||
$courseId |
||||
); |
||||
|
||||
$announcements = array_map(function ($announcement) { |
||||
return [ |
||||
'id' => intval($announcement['id']), |
||||
'title' => strip_tags($announcement['title']), |
||||
'creatorName' => strip_tags($announcement['username']), |
||||
'date' => strip_tags($announcement['insert_date']) |
||||
]; |
||||
}, $announcements); |
||||
|
||||
return $announcements; |
||||
} |
||||
|
||||
/** |
||||
* @param int $announcementId |
||||
* @param int $courseId |
||||
* @return array |
||||
* @throws Exception |
||||
*/ |
||||
public function getCourseAnnouncement($announcementId, $courseId) |
||||
{ |
||||
/** @var Course $course */ |
||||
$course = Database::getManager()->find('ChamiloCoreBundle:Course', $courseId); |
||||
|
||||
if (!$course) { |
||||
throw new Exception(get_lang('NoCourse')); |
||||
} |
||||
|
||||
$announcement = AnnouncementManager::getAnnouncementInfoById( |
||||
$announcementId, |
||||
$course->getId(), |
||||
$this->user->getId() |
||||
); |
||||
|
||||
if (!$announcement) { |
||||
throw new Exception(get_lang('NoAnnouncement')); |
||||
} |
||||
|
||||
return [ |
||||
'id' => intval($announcement['announcement']->getIid()), |
||||
'title' => $announcement['announcement']->getTitle(), |
||||
'creatorName' => $announcement['item_property']->getInsertUser()->getCompleteName(), |
||||
'date' => api_convert_and_format_date($announcement['item_property']->getInsertDate(), DATE_TIME_FORMAT_LONG_24H), |
||||
'content' => AnnouncementManager::parse_content( |
||||
$this->user->getId(), |
||||
$announcement['announcement']->getContent(), |
||||
$course->getCode() |
||||
) |
||||
]; |
||||
} |
||||
|
||||
/** |
||||
* @param int $courseId |
||||
* @return array |
||||
* @throws Exception |
||||
*/ |
||||
public function getCourseAgenda($courseId) |
||||
{ |
||||
/** @var Course $course */ |
||||
$course = Database::getManager()->find('ChamiloCoreBundle:Course', $courseId); |
||||
|
||||
if (!$course) { |
||||
throw new Exception(get_lang('NoCourse')); |
||||
} |
||||
|
||||
$agenda = new Agenda(); |
||||
$agenda->setType('course'); |
||||
$result = $agenda->parseAgendaFilter(null); |
||||
|
||||
$start = new DateTime('now'); |
||||
$start->modify('first day of month'); |
||||
$end = new DateTime('now'); |
||||
$end->modify('first day of month'); |
||||
|
||||
$groupId = current($result['groups']); |
||||
$userId = current($result['users']); |
||||
|
||||
$events = $agenda->getEvents( |
||||
$start->getTimestamp(), |
||||
$end->getTimestamp(), |
||||
$course->getId(), |
||||
$groupId, |
||||
$userId, |
||||
'array' |
||||
); |
||||
|
||||
if (!is_array($events)) { |
||||
return []; |
||||
} |
||||
|
||||
$webPath = api_get_path(WEB_PATH); |
||||
|
||||
return array_map( |
||||
function ($event) use ($webPath) { |
||||
return [ |
||||
'id' => intval($event['unique_id']), |
||||
'title' => $event['title'], |
||||
'content' => str_replace('src="/', 'src="' . $webPath, $event['description']), |
||||
'startDate' => $event['start_date_localtime'], |
||||
'endDate' => $event['end_date_localtime'], |
||||
'isAllDay' => $event['allDay'] ? true : false |
||||
]; |
||||
}, |
||||
$events |
||||
); |
||||
} |
||||
|
||||
/** |
||||
* @param int $courseId |
||||
* @return array |
||||
* @throws Exception |
||||
*/ |
||||
public function getCourseNotebooks($courseId) |
||||
{ |
||||
$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); |
||||
|
||||
return array_map( |
||||
function (\Chamilo\CourseBundle\Entity\CNotebook $notebook) { |
||||
return [ |
||||
'id' => $notebook->getIid(), |
||||
'title' => $notebook->getTitle(), |
||||
'description' => $notebook->getDescription(), |
||||
'creationDate' => api_format_date( |
||||
$notebook->getCreationDate()->getTimestamp() |
||||
), |
||||
'updateDate' => api_format_date( |
||||
$notebook->getUpdateDate()->getTimestamp() |
||||
) |
||||
]; |
||||
}, |
||||
$notebooks |
||||
); |
||||
} |
||||
|
||||
/** |
||||
* @param int $courseId |
||||
* @return array |
||||
* @throws Exception |
||||
*/ |
||||
public function getCourseForumCategories($courseId) |
||||
{ |
||||
$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/'; |
||||
|
||||
require_once api_get_path(SYS_CODE_PATH) . 'forum/forumfunction.inc.php'; |
||||
|
||||
$categoriesFullData = get_forum_categories('', $course->getId()); |
||||
$categories = []; |
||||
$includeGroupsForums = api_get_setting('display_groups_forum_in_general_tool') === 'true'; |
||||
$forumsFullData = get_forums('', $course->getCode(), $includeGroupsForums); |
||||
$forums = []; |
||||
|
||||
foreach ($forumsFullData as $forumId => $forumInfo) { |
||||
$forum = [ |
||||
'id' => intval($forumInfo['iid']), |
||||
'catId' => intval($forumInfo['forum_category']), |
||||
'title' => $forumInfo['forum_title'], |
||||
'description' => $forumInfo['forum_comment'], |
||||
'image' => $forumInfo['forum_image'] ? ($webCoursePath . $forumInfo['forum_image']) : '', |
||||
'numberOfThreads' => intval($forumInfo['number_of_threads']), |
||||
'lastPost' => null |
||||
]; |
||||
|
||||
$lastPostInfo = get_last_post_information($forumId, false, $course->getId()); |
||||
|
||||
if ($lastPostInfo) { |
||||
$forum['lastPost'] = [ |
||||
'date' => api_convert_and_format_date($lastPostInfo['last_post_date']), |
||||
'user' => api_get_person_name( |
||||
$lastPostInfo['last_poster_firstname'], |
||||
$lastPostInfo['last_poster_lastname'] |
||||
) |
||||
]; |
||||
} |
||||
|
||||
$forums[] = $forum; |
||||
} |
||||
|
||||
foreach ($categoriesFullData as $category) { |
||||
$categoryForums = array_filter( |
||||
$forums, |
||||
function (array $forum) use ($category) { |
||||
if ($forum['catId'] != $category['cat_id']) { |
||||
return false; |
||||
} |
||||
|
||||
return true; |
||||
} |
||||
); |
||||
|
||||
$categories[] = [ |
||||
'id' => intval($category['iid']), |
||||
'title' => $category['cat_title'], |
||||
'catId' => intval($category['cat_id']), |
||||
'description' => $category['cat_comment'], |
||||
'forums' => $categoryForums, |
||||
'courseId' => $course->getId() |
||||
]; |
||||
} |
||||
|
||||
return $categories; |
||||
} |
||||
|
||||
/** |
||||
* @param int $forumId |
||||
* @return array |
||||
* @throws Exception |
||||
*/ |
||||
public function getCourseForum($forumId) |
||||
{ |
||||
require_once api_get_path(SYS_CODE_PATH) . 'forum/forumfunction.inc.php'; |
||||
|
||||
$forumInfo = get_forums($forumId); |
||||
|
||||
if (!isset($forumInfo['iid'])) { |
||||
throw new Exception(get_lang('NoForum')); |
||||
} |
||||
|
||||
/** @var Course $course */ |
||||
$course = Database::getManager()->find('ChamiloCoreBundle:Course', $forumInfo['c_id']); |
||||
|
||||
if (!$course) { |
||||
throw new Exception(get_lang('NoCourse')); |
||||
} |
||||
|
||||
$webCoursePath = api_get_path(WEB_COURSE_PATH) . $course->getDirectory() . '/upload/forum/images/'; |
||||
$forum = [ |
||||
'id' => $forumInfo['iid'], |
||||
'title' => $forumInfo['forum_title'], |
||||
'description' => $forumInfo['forum_comment'], |
||||
'image' => $forumInfo['forum_image'] ? ($webCoursePath . $forumInfo['forum_image']) : '', |
||||
'threads' => [] |
||||
]; |
||||
|
||||
$threads = get_threads($forumInfo['iid']); |
||||
|
||||
foreach ($threads as $thread) { |
||||
$forum['threads'][] = [ |
||||
'id' => $thread['iid'], |
||||
'title' => $thread['thread_title'], |
||||
'lastEditDate' => api_convert_and_format_date($thread['lastedit_date'], DATE_TIME_FORMAT_LONG_24H), |
||||
'numberOfReplies' => $thread['thread_replies'], |
||||
'numberOfViews' => $thread['thread_views'], |
||||
'author' => api_get_person_name($thread['firstname'], $thread['lastname']) |
||||
]; |
||||
} |
||||
|
||||
return $forum; |
||||
} |
||||
|
||||
/** |
||||
* @param int $threadId |
||||
* @return array |
||||
*/ |
||||
public function getCourseForumThread($threadId) |
||||
{ |
||||
require_once api_get_path(SYS_CODE_PATH) . 'forum/forumfunction.inc.php'; |
||||
|
||||
$threadInfo = get_thread_information($threadId); |
||||
|
||||
$thread = [ |
||||
'id' => intval($threadInfo['iid']), |
||||
'cId' => intval($threadInfo['c_id']), |
||||
'title' => $threadInfo['thread_title'], |
||||
'forumId' => intval($threadInfo['forum_id']), |
||||
'posts' => [] |
||||
]; |
||||
|
||||
$forumInfo = get_forums($threadInfo['forum_id']); |
||||
|
||||
$postsInfo = getPosts($forumInfo, $threadInfo['iid'], 'ASC'); |
||||
|
||||
foreach ($postsInfo as $postInfo) { |
||||
$thread['posts'][] = [ |
||||
'id' => $postInfo['iid'], |
||||
'title' => $postInfo['post_title'], |
||||
'text' => $postInfo['post_text'], |
||||
'author' => api_get_person_name($postInfo['firstname'], $postInfo['lastname']), |
||||
'date' => api_convert_and_format_date($postInfo['post_date'], DATE_TIME_FORMAT_LONG_24H), |
||||
'parentId' => $postInfo['post_parent_id'] |
||||
]; |
||||
} |
||||
|
||||
return $thread; |
||||
} |
||||
|
||||
/** |
||||
* @return array |
||||
*/ |
||||
public function getUserProfile() |
||||
{ |
||||
$pictureInfo = UserManager::get_user_picture_path_by_id($this->user->getId(), 'web'); |
||||
|
||||
$result = [ |
||||
'pictureUri' => $pictureInfo['dir'] . $pictureInfo['file'], |
||||
'fullName' => $this->user->getCompleteName(), |
||||
'username' => $this->user->getUsername(), |
||||
'officialCode' => $this->user->getOfficialCode(), |
||||
'phone' => $this->user->getPhone(), |
||||
'extra' => [] |
||||
]; |
||||
|
||||
$fieldValue = new ExtraFieldValue('user'); |
||||
$extraInfo = $fieldValue->getAllValuesForAnItem($this->user->getId(), true); |
||||
|
||||
foreach ($extraInfo as $extra) { |
||||
/** @var ExtraFieldValues $extraValue */ |
||||
$extraValue = $extra['value']; |
||||
|
||||
$result['extra'][] = [ |
||||
'title' => $extraValue->getField()->getDisplayText(true), |
||||
'value' => $extraValue->getValue() |
||||
]; |
||||
} |
||||
|
||||
return $result; |
||||
} |
||||
} |
@ -0,0 +1,65 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
/** |
||||
* Class RestApiResponse |
||||
*/ |
||||
class RestResponse |
||||
{ |
||||
/** |
||||
* @var bool |
||||
*/ |
||||
private $error; |
||||
/** |
||||
* @var string |
||||
*/ |
||||
private $errorMessage; |
||||
/** |
||||
* @var array |
||||
*/ |
||||
private $data; |
||||
|
||||
/** |
||||
* RestApiResponse constructor. |
||||
*/ |
||||
public function __construct() |
||||
{ |
||||
$this->error = true; |
||||
$this->errorMessage = null; |
||||
$this->data = []; |
||||
} |
||||
|
||||
/** |
||||
* @param array $data |
||||
*/ |
||||
public function setData(array $data) |
||||
{ |
||||
$this->error = false; |
||||
$this->data = $data; |
||||
} |
||||
|
||||
/** |
||||
* @param string $message |
||||
*/ |
||||
public function setErrorMessage($message) |
||||
{ |
||||
$this->error = true; |
||||
$this->errorMessage = $message; |
||||
} |
||||
|
||||
/** |
||||
* @return string |
||||
*/ |
||||
public function format() |
||||
{ |
||||
$json = ['error' => $this->error]; |
||||
|
||||
if ($this->error) { |
||||
$json['message'] = $this->errorMessage; |
||||
} else { |
||||
$json['data'] = $this->data; |
||||
} |
||||
|
||||
return json_encode($json, JSON_PRETTY_PRINT); |
||||
} |
||||
} |
@ -0,0 +1,157 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
require_once '../../inc/global.inc.php'; |
||||
|
||||
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : null; |
||||
$username = isset($_POST['username']) ? Security::remove_XSS($_POST['username']) : null; |
||||
$apiKey = isset($_POST['api_key']) ? Security::remove_XSS($_POST['api_key']) : null; |
||||
|
||||
$restResponse = new RestResponse(); |
||||
|
||||
try { |
||||
/** @var Rest $restApi */ |
||||
$restApi = $apiKey ? Rest::validate($username, $apiKey) : null; |
||||
|
||||
switch ($action) { |
||||
case Rest::ACTION_AUTH: |
||||
Rest::init(); |
||||
|
||||
$password = isset($_POST['password']) ? $_POST['password'] : null; |
||||
|
||||
$isValid = Rest::isValidUser($username, $password); |
||||
|
||||
if (!$isValid) { |
||||
throw new Exception(get_lang('InvalideUserDetected')); |
||||
} |
||||
|
||||
$restResponse->setData([ |
||||
'url' => api_get_path(WEB_PATH), |
||||
'apiKey' => Rest::findUserApiKey($username, Rest::SERVIVE_NAME), |
||||
'gcmSenderId' => api_get_setting('messaging_gdc_project_number') |
||||
]); |
||||
break; |
||||
|
||||
case Rest::ACTION_GCM_ID: |
||||
$gcmId = isset($_POST['registration_id']) ? Security::remove_XSS($_POST['registration_id']) : null; |
||||
|
||||
$restApi->setGcmId($gcmId); |
||||
|
||||
$restResponse->setData(['status' => true]); |
||||
break; |
||||
|
||||
case Rest::ACTION_USER_MESSAGES: |
||||
$lastMessageId = isset($_POST['last']) ? intval($_POST['last']) : 0; |
||||
|
||||
$messages = $restApi->getUserMessages($lastMessageId); |
||||
|
||||
$restResponse->setData($messages); |
||||
break; |
||||
|
||||
case Rest::ACTION_USER_COURSES: |
||||
$courses = $restApi->getUserCourses(); |
||||
|
||||
$restResponse->setData($courses); |
||||
break; |
||||
|
||||
case Rest::ACTION_COURSE_INFO: |
||||
$courseId = isset($_POST['c_id']) ? Security::remove_XSS($_POST['c_id']) : 0; |
||||
$courseInfo = $restApi->getCourseInfo($courseId); |
||||
|
||||
$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); |
||||
|
||||
$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); |
||||
|
||||
$restResponse->setData($documents); |
||||
break; |
||||
|
||||
case Rest::ACTION_COURSE_ANNOUNCEMENTS: |
||||
$courseId = isset($_POST['c_id']) ? Security::remove_XSS($_POST['c_id']) : 0; |
||||
|
||||
$announcements = $restApi->getCourseAnnouncements($courseId); |
||||
|
||||
$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; |
||||
|
||||
$announcement = $restApi->getCourseAnnouncement($announcementId, $courseId); |
||||
|
||||
$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); |
||||
|
||||
$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); |
||||
|
||||
$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); |
||||
|
||||
$restResponse->setData($forums); |
||||
break; |
||||
|
||||
case Rest::ACTION_COURSE_FORUM: |
||||
$forumId = isset($_POST['forum']) ? Security::remove_XSS($_POST['forum']) : 0; |
||||
|
||||
$forum = $restApi->getCourseForum($forumId); |
||||
|
||||
$restResponse->setData($forum); |
||||
break; |
||||
|
||||
case Rest::ACTION_COURSE_FORUM_THREAD: |
||||
$threadId = isset($_POST['thread']) ? Security::remove_XSS($_POST['thread']) : 0; |
||||
|
||||
$thread = $restApi->getCourseForumThread($threadId); |
||||
|
||||
$restResponse->setData($thread); |
||||
break; |
||||
|
||||
case Rest::ACTION_PROFILE: |
||||
$userInfo = $restApi->getUserProfile(); |
||||
|
||||
$restResponse->setData($userInfo); |
||||
break; |
||||
|
||||
default: |
||||
throw new Exception(get_lang('InvalidAction')); |
||||
} |
||||
} catch (Exception $exeption) { |
||||
$restResponse->setErrorMessage( |
||||
$exeption->getMessage() |
||||
); |
||||
|
||||
} |
||||
|
||||
//header('Content-Type: application/json'); |
||||
header('Access-Control-Allow-Origin: *'); |
||||
|
||||
echo $restResponse->format(); |
@ -1,97 +0,0 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
/** |
||||
* Controller for REST request |
||||
* @author Angel Fernando Quiroz Campos <angel.quiroz@beeznest.com> |
||||
* @package chamilo.webservices |
||||
*/ |
||||
/* Require libs and classes */ |
||||
require_once '../inc/global.inc.php'; |
||||
|
||||
/* Manage actions */ |
||||
$json = array(); |
||||
|
||||
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'nothing'; |
||||
$username = isset($_POST['username']) ? Security::remove_XSS($_POST['username']) : null; |
||||
$apiKey = isset($_POST['api_key']) ? Security::remove_XSS($_POST['api_key']) : null; |
||||
|
||||
$em = Database::getManager(); |
||||
|
||||
switch ($action) { |
||||
case 'loginNewMessages': |
||||
$password = isset($_POST['password']) ? Security::remove_XSS($_POST['password']) : null; |
||||
|
||||
if (MessagesWebService::isValidUser($username, $password)) { |
||||
MessagesWebService::init(); |
||||
|
||||
$webService = new MessagesWebService(); |
||||
$apiKey = $webService->getApiKey($username); |
||||
|
||||
$json = array( |
||||
'status' => true, |
||||
'apiKey' => $apiKey, |
||||
'gcmSenderId' => api_get_setting('messaging_gdc_project_number'), |
||||
); |
||||
} else { |
||||
$json = array( |
||||
'status' => false |
||||
); |
||||
} |
||||
break; |
||||
case 'countNewMessages': |
||||
if (MessagesWebService::isValidApiKey($username, $apiKey)) { |
||||
$webService = new MessagesWebService(); |
||||
$webService->setApiKey($apiKey); |
||||
|
||||
$lastId = isset($_POST['last']) ? $_POST['last'] : 0; |
||||
|
||||
$count = $webService->countNewMessages($username, $lastId); |
||||
|
||||
$json = array( |
||||
'status' => true, |
||||
'count' => $count |
||||
); |
||||
} else { |
||||
$json = array( |
||||
'status' => false |
||||
); |
||||
} |
||||
break; |
||||
case 'getNewMessages': |
||||
if (MessagesWebService::isValidApiKey($username, $apiKey)) { |
||||
$webService = new MessagesWebService(); |
||||
$webService->setApiKey($apiKey); |
||||
|
||||
$lastId = isset($_POST['last']) ? $_POST['last'] : 0; |
||||
|
||||
$messages = $webService->getNewMessages($username, $lastId); |
||||
|
||||
$json = array( |
||||
'status' => true, |
||||
'messages' => $messages |
||||
); |
||||
} else { |
||||
$json = array( |
||||
'status' => false |
||||
); |
||||
} |
||||
break; |
||||
case 'setGcmRegistrationId': |
||||
if (!MessagesWebService::isValidApiKey($username, $apiKey)) { |
||||
$json = ['status' => false]; |
||||
break; |
||||
} |
||||
|
||||
$user = $em->getRepository('ChamiloUserBundle:User')->findOneBy(['username' => $username]); |
||||
|
||||
MessagesWebService::setGcmRegistrationId($user, $_POST['registration_id']); |
||||
|
||||
$json = ['status' => true]; |
||||
break; |
||||
default: |
||||
} |
||||
|
||||
/* View */ |
||||
header('Content-Type: application/json'); |
||||
header('Access-Control-Allow-Origin: *'); |
||||
echo json_encode($json); |
@ -0,0 +1,83 @@ |
||||
<?php |
||||
/* For licensing terms, see /license.txt */ |
||||
|
||||
namespace Chamilo\CourseBundle\Entity\Repository; |
||||
|
||||
use Chamilo\CoreBundle\Entity\Course; |
||||
use Chamilo\CoreBundle\Entity\Session; |
||||
use Chamilo\UserBundle\Entity\User; |
||||
use Doctrine\ORM\EntityRepository; |
||||
|
||||
/** |
||||
* Class CNotebookRepository |
||||
* @package Chamilo\CourseBundle\Entity\Repository |
||||
*/ |
||||
class CNotebookRepository extends EntityRepository |
||||
{ |
||||
|
||||
/** |
||||
* Get the user notebooks in a course |
||||
* @param User $user |
||||
* @param Course $course |
||||
* @param Session|null $session |
||||
* @param string $orderField |
||||
* @param string $orderDirection |
||||
* @return array |
||||
*/ |
||||
public function findByUser( |
||||
User $user, |
||||
Course $course, |
||||
Session $session = null, |
||||
$orderField = 'creation_date', |
||||
$orderDirection = 'DESC') |
||||
{ |
||||
switch ($orderField) { |
||||
case 'creation_date': |
||||
$orderField = 'N.creationDate'; |
||||
break; |
||||
case 'update_date': |
||||
$orderField = 'N.updateDate'; |
||||
break; |
||||
case 'title': |
||||
$orderField = 'N.title'; |
||||
break; |
||||
} |
||||
|
||||
$qb = $this->createQueryBuilder('N'); |
||||
$qb |
||||
->where( |
||||
$qb->expr()->andX( |
||||
$qb->expr()->eq('N.userId', $user->getId()), |
||||
$qb->expr()->eq('N.cId', $course->getId()) |
||||
) |
||||
); |
||||
|
||||
if ($session) { |
||||
$qb->andWhere( |
||||
$qb->expr()->eq('N.sessionId', $session->getId()) |
||||
); |
||||
} else { |
||||
$qb->andWhere( |
||||
$qb->expr()->orX( |
||||
$qb->expr()->eq('N.sessionId', 0), |
||||
$qb->expr()->isNull('N.sessionId') |
||||
) |
||||
); |
||||
} |
||||
|
||||
if ($orderField === 'N.updateDate') { |
||||
$qb->andWhere( |
||||
$qb->expr()->orX( |
||||
$qb->expr()->neq('N.updateDate', ''), |
||||
$qb->expr()->isNotNull('N.updateDate') |
||||
) |
||||
); |
||||
} |
||||
|
||||
$qb->orderBy($orderField, $orderDirection); |
||||
|
||||
error_log($qb->getQuery()->getSQL()); |
||||
|
||||
return $qb->getQuery()->getResult(); |
||||
} |
||||
} |
Loading…
Reference in new issue