-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
...vices/UIComponent/UserInterfaceHook/REST/RESTController/extensions/umr_v1/models/News.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
/** | ||
* ILIAS REST Plugin for the ILIAS LMS | ||
* | ||
* Authors: D.Schaefer, T.Hufschmidt <(schaefer|hufschmidt)@hrz.uni-marburg.de> | ||
* Since 2014 | ||
*/ | ||
namespace RESTController\extensions\umr_v1; | ||
|
||
|
||
// This allows us to use shortcuts instead of full quantifier | ||
use \RESTController\libs as Libs; | ||
|
||
|
||
/** | ||
* News-stream model | ||
*/ | ||
class News extends Libs\RESTModel { | ||
/** | ||
* Retrieves the ilias personal desktop news for a user. | ||
* Note: code heavily inspired by Services/News/classes/ilPDNewsGUI.php | ||
* @param $user_id | ||
* @return array | ||
*/ | ||
public static function getPDNewsForUser($user_id) | ||
{ | ||
Libs\RESTLib::loadIlUser(); | ||
global $ilUser; | ||
$ilUser->setId($user_id); | ||
$ilUser->read(); | ||
Libs\RESTLib::initAccessHandling(); | ||
|
||
$ref_ids = array(); | ||
$obj_ids = array(); | ||
$pd_items = $ilUser->getDesktopItems(); | ||
foreach($pd_items as $item) | ||
{ | ||
$ref_ids[] = $item["ref_id"]; | ||
$obj_ids[] = $item["obj_id"]; | ||
} | ||
|
||
$sel_ref_id = ($_GET["news_ref_id"] > 0) | ||
? $_GET["news_ref_id"] | ||
: $ilUser->getPref("news_sel_ref_id"); | ||
|
||
include_once("./Services/News/classes/class.ilNewsItem.php"); | ||
$per = ($_SESSION["news_pd_news_per"] != "") | ||
? $_SESSION["news_pd_news_per"] | ||
: \ilNewsItem::_lookupUserPDPeriod($ilUser->getId()); | ||
$news_obj_ids = \ilNewsItem::filterObjIdsPerNews($obj_ids, $per); | ||
|
||
// related objects (contexts) of news | ||
//$contexts[0] = $lng->txt("news_all_items"); | ||
$contexts[0] = "news_all_items"; | ||
|
||
$conts = array(); | ||
$sel_has_news = false; | ||
foreach ($ref_ids as $ref_id) | ||
{ | ||
$obj_id = \ilObject::_lookupObjId($ref_id); | ||
$title = \ilObject::_lookupTitle($obj_id); | ||
|
||
$conts[$ref_id] = $title; | ||
if ($sel_ref_id == $ref_id) | ||
{ | ||
$sel_has_news = true; | ||
} | ||
} | ||
|
||
$cnt = array(); | ||
$nitem = new \ilNewsItem(); | ||
$news_items = $nitem->_getNewsItemsOfUser($ilUser->getId(), false, | ||
true, $per, $cnt); | ||
|
||
// reset selected news ref id, if no news are given for id | ||
if (!$sel_has_news) | ||
{ | ||
$sel_ref_id = ""; | ||
} | ||
asort($conts); | ||
foreach($conts as $ref_id => $title) | ||
{ | ||
$contexts[$ref_id] = $title." (".(int) $cnt[$ref_id].")"; | ||
} | ||
|
||
|
||
if ($sel_ref_id > 0) | ||
{ | ||
$obj_id = \ilObject::_lookupObjId($sel_ref_id); | ||
$obj_type = \ilObject::_lookupType($obj_id); | ||
$nitem->setContextObjId($obj_id); | ||
$nitem->setContextObjType($obj_type); | ||
$news_items = $nitem->getNewsForRefId($sel_ref_id, false, | ||
false, $per, true); | ||
} | ||
|
||
return $news_items; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...vices/UIComponent/UserInterfaceHook/REST/RESTController/extensions/umr_v1/routes/News.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
/** | ||
* ILIAS REST Plugin for the ILIAS LMS | ||
* | ||
* Authors: D.Schaefer and T.Hufschmidt <(schaefer|hufschmidt)@hrz.uni-marburg.de> | ||
* Since 2014 | ||
*/ | ||
namespace RESTController\extensions\umr_v1; | ||
|
||
|
||
// This allows us to use shortcuts instead of full quantifier | ||
// Requires: $app to be \RESTController\RESTController::getInstance() | ||
use \RESTController\libs\RESTAuth as RESTAuth; | ||
use \RESTController\libs as Libs; | ||
use \RESTController\core\auth as Auth; | ||
|
||
$app->group('/v1/umr', function () use ($app) { | ||
/** | ||
* Route: GET /v1/umr/news | ||
* [Without HTTP-GET Parameters] Gets all news for the user encoded by the access-token. | ||
* @See docs/api.pdf | ||
*/ | ||
$app->get('/news', RESTAuth::checkAccess(RESTAuth::PERMISSION), function () use ($app) { | ||
// Fetch userId & userName | ||
$accessToken = Auth\Util::getAccessToken(); | ||
try { | ||
$user_id = $accessToken->getUserId(); | ||
$news = News::getPDNewsForUser($user_id); | ||
|
||
// Output result | ||
$app->success($news); | ||
} | ||
catch (Libs\Exceptions\IdParseProblem $e) { | ||
$app->halt(422, $e->getMessage(), $e->getRESTCode()); | ||
} | ||
catch (Exceptions\Events $e) { | ||
$responseObject = Libs\RESTLib::responseObject($e->getMessage(), $e->getRestCode()); | ||
$responseObject['data'] = $e->getData(); | ||
$app->halt(500, $responseObject); | ||
} | ||
}); | ||
}); |