Skip to content

Commit

Permalink
UMR News Route v.1.0 added
Browse files Browse the repository at this point in the history
  • Loading branch information
disc5 committed Nov 10, 2015
1 parent 709c495 commit 6388f19
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
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;
}
}
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);
}
});
});

0 comments on commit 6388f19

Please sign in to comment.