summaryrefslogtreecommitdiffstats
path: root/modules-available/dozmod/api.inc.php
diff options
context:
space:
mode:
authorChristian Klinger2016-06-10 17:19:50 +0200
committerChristian Klinger2016-06-10 17:19:50 +0200
commitcab4355fec9928639e688d096d9ad5e77cb28493 (patch)
tree79aa7dc308c4c877bef87f89cf2b88fbe917ef74 /modules-available/dozmod/api.inc.php
parentuse Module:: instead of require (diff)
downloadslx-admin-cab4355fec9928639e688d096d9ad5e77cb28493.tar.gz
slx-admin-cab4355fec9928639e688d096d9ad5e77cb28493.tar.xz
slx-admin-cab4355fec9928639e688d096d9ad5e77cb28493.zip
first rough version of exams-module.
Diffstat (limited to 'modules-available/dozmod/api.inc.php')
-rw-r--r--modules-available/dozmod/api.inc.php128
1 files changed, 68 insertions, 60 deletions
diff --git a/modules-available/dozmod/api.inc.php b/modules-available/dozmod/api.inc.php
index 52c8dcfb..b37c668c 100644
--- a/modules-available/dozmod/api.inc.php
+++ b/modules-available/dozmod/api.inc.php
@@ -11,7 +11,7 @@
if (!Module::isAvailable('locations')) {
- die('require locations module');
+ die('require locations module');
}
@@ -23,52 +23,52 @@ $availableRessources = ['vmx', 'test', 'netrules'];
function cache_hash($obj)
{
- return md5(serialize($obj));
+ return md5(serialize($obj));
}
function cache_key_to_filename($key)
{
- return "/tmp/bwlp-slxadmin-cache-$key"; // TODO: hash
+ return "/tmp/bwlp-slxadmin-cache-$key"; // TODO: hash
}
function cache_put($key, $value)
{
- $filename = cache_key_to_filename($key);
- file_put_contents($filename, $value);
+ $filename = cache_key_to_filename($key);
+ file_put_contents($filename, $value);
}
function cache_has($key)
{
- $filename = cache_key_to_filename($key);
- $mtime = filemtime($filename);
+ $filename = cache_key_to_filename($key);
+ $mtime = filemtime($filename);
- if (!$mtime) {
- return false; // cache miss
- }
- if (time() - $mtime > CONFIG_DOZMOD_EXPIRE) {
- return false;
- } else {
- return true;
- }
+ if (!$mtime) {
+ return false; // cache miss
+ }
+ if (time() - $mtime > CONFIG_DOZMOD_EXPIRE) {
+ return false;
+ } else {
+ return true;
+ }
}
function cache_get($key)
{
- $filename = cache_key_to_filename($key);
- return file_get_contents($filename);
+ $filename = cache_key_to_filename($key);
+ return file_get_contents($filename);
}
/* good for large binary files */
function cache_get_passthru($key)
{
- $filename = cache_key_to_filename($key);
- $fp = fopen($filename, "r");
- if ($fp) {
- fpassthru($fp);
- } else {
- Util::traceError("cannot open file");
- }
+ $filename = cache_key_to_filename($key);
+ $fp = fopen($filename, "r");
+ if ($fp) {
+ fpassthru($fp);
+ } else {
+ Util::traceError("cannot open file");
+ }
}
/* END: Cache ---------------------------------------------------- */
@@ -88,56 +88,64 @@ function println($str) { echo "$str\n"; }
* */
function _getLecturesForLocations($locationIds)
{
- $ids = implode('%20', $locationIds);
- $url = LIST_URL . "?locations=$ids";
- $responseXML = Download::asString($url, 60, $code);
- $xml = new SimpleXMLElement($responseXML);
- $uuids = [];
- foreach ($xml->eintrag as $e) {
- $uuids[] = strval($e->uuid['param'][0]);
- }
- return $uuids;
+ /* if in any of the locations there is an exam active, consider the client
+ to be in "exam-mode" and only offer him exams (no lectures) */
+ $examMode = false;
+
+ if (Module::isAvailable('exams')) {
+ $examMode = Exams::isInExamMode($locationIds);
+ }
+ $ids = implode('%20', $locationIds);
+ $url = LIST_URL . "?locations=$ids" . ($examMode ? '&exams' : '');
+ $responseXML = Download::asString($url, 60, $code);
+ $xml = new SimpleXMLElement($responseXML);
+
+ $uuids = [];
+ foreach ($xml->eintrag as $e) {
+ $uuids[] = strval($e->uuid['param'][0]);
+ }
+ return $uuids;
}
/** Caching wrapper around _getLecturesForLocations() */
function getLecturesForLocations($locationIds)
{
- $key = 'lectures_' . cache_hash($locationIds);
- if (cache_has($key)) {
- return unserialize(cache_get($key));
- } else {
- $value = _getLecturesForLocations($locationIds);
- cache_put($key, serialize($value));
- return $value;
- }
+ $key = 'lectures_' . cache_hash($locationIds);
+ if (cache_has($key)) {
+ return unserialize(cache_get($key));
+ } else {
+ $value = _getLecturesForLocations($locationIds);
+ cache_put($key, serialize($value));
+ return $value;
+ }
}
function _getVMX($lecture_uuid)
{
- $url = VMX_URL . '/' . $lecture_uuid;
- $response = Download::asString($url, 60, $code);
- return $response;
+ $url = VMX_URL . '/' . $lecture_uuid;
+ $response = Download::asString($url, 60, $code);
+ return $response;
}
/** Caching wrapper around _getVMX() **/
function getVMX($lecture_uuid)
{
- $key = 'vmx_' . $lecture_uuid;
- if (cache_has($key)) {
- cache_get_passthru($key);
- } else {
- $value = _getVMX($lecture_uuid);
- cache_put($key, $value);
- return $value;
- }
+ $key = 'vmx_' . $lecture_uuid;
+ if (cache_has($key)) {
+ cache_get_passthru($key);
+ } else {
+ $value = _getVMX($lecture_uuid);
+ cache_put($key, $value);
+ return $value;
+ }
}
// -----------------------------------------------------------------------------//
$ip = $_SERVER['REMOTE_ADDR'];
if (substr($ip, 0, 7) === '::ffff:') {
- $ip = substr($ip, 7);
+ $ip = substr($ip, 7);
}
/* request data, don't trust */
@@ -145,10 +153,10 @@ $resource = Request::get('resource', false, 'string');
$lecture = Request::get('lecture', false, 'string');
if ($resource === false) {
- Util::traceError("you have to specify the 'resource' parameter");
+ Util::traceError("you have to specify the 'resource' parameter");
}
if ($lecture === false) {
- Util::traceError("you have to specify the 'lecture' parameter");
+ Util::traceError("you have to specify the 'lecture' parameter");
}
/* lookup location id(s) */
@@ -161,18 +169,18 @@ $lectures = getLecturesForLocations($location_ids);
/* validate request -------------------------------------------- */
/* check resources */
if (!in_array($resource, $availableRessources)) {
- Util::traceError("unknown resource: $resource");
+ Util::traceError("unknown resource: $resource");
}
/* check that the user requests a lecture that he is allowed to have */
if (!in_array($lecture, $lectures)) {
- Util::traceError("client is not allowed to access this lecture: $lecture");
+ Util::traceError("client is not allowed to access this lecture: $lecture");
}
if ($resource === 'vmx') {
- echo getVMX($lecture);
+ echo getVMX($lecture);
} else if ($resource === 'test') {
- echo "Here's your special test data!";
+ echo "Here's your special test data!";
} else {
- echo "I don't know how to give you that resource";
+ echo "I don't know how to give you that resource";
}