Example #1
0
 /**
  * Adds the component to a course
  */
 public function addCourse($pre = '')
 {
     $this->loadConfig($pre);
     $pre = ($pre === '' ? '' : '_') . $pre;
     Logger::Log('starts POST AddCourse', LogLevel::DEBUG);
     // decode the received course data, as an object
     $insert = Course::decodeCourse($this->_app->request->getBody());
     // always been an array
     $arr = true;
     if (!is_array($insert)) {
         $insert = array($insert);
         $arr = false;
     }
     // this array contains the indices of the inserted objects
     $res = array();
     foreach ($insert as $in) {
         // starts a query, by using a given file
         $result = DBRequest::getRoutedSqlFile($this->query, dirname(__FILE__) . '/Sql/AddCourse.sql', array('object' => $in, 'pre' => $pre));
         // checks the correctness of the query
         if ($result['status'] >= 200 && $result['status'] <= 299) {
             $queryResult = Query::decodeQuery($result['content']);
             $res[] = $in;
             $this->_app->response->setStatus(201);
             if (isset($result['headers']['Content-Type'])) {
                 $this->_app->response->headers->set('Content-Type', $result['headers']['Content-Type']);
             }
         } else {
             Logger::Log('POST AddCourse failed', LogLevel::ERROR);
             $this->_app->response->setStatus(isset($result['status']) ? $result['status'] : 409);
             $this->_app->response->setBody(Course::encodeCourse($res));
             $this->_app->stop();
         }
     }
     if (!$arr && count($res) == 1) {
         $this->_app->response->setBody(Course::encodeCourse($res[0]));
     } else {
         $this->_app->response->setBody(Course::encodeCourse($res));
     }
 }
Example #2
0
 /**
  * Sendet $sql an $linkName und behandelt die Antwort
  *
  * @param string $linkName Der Name des Ausgangs
  * @param string $sql Der zu verwendende SQL Inhalt
  * @param int $positiveStatus Der Status, welcher als erfolgreiche Antwort gesehen wird (Bsp.: 200)
  * @param callable $positiveMethod Im positiven Fall wird diese Methode aufgerufen
  * @param mixed[] $positiveParams Die Werte, welche an die positive Funktion übergeben werden
  * @param callable $negativeMethod Im negativen Fall wird diese Methode aufgerufen
  * @param mixed[] $negativeParams Die Werte, welche an die negative Funktion übergeben werden
  * @param bool $checkSession Ob die Sessiondaten in der Datenbank geprüft werden sollen
  * @return mixed Das Ergebnis der aufgerufenen Resultatfunktion
  */
 public function callSql($linkName, $sql, $positiveStatus, callable $positiveMethod, $positiveParams, callable $negativeMethod, $negativeParams, $checkSession = true)
 {
     $link = CConfig::getLink($this->_conf->getLinks(), $linkName);
     // starts a query, by using given sql statements/statement
     $result = DBRequest::getRoutedSql($link, $sql, $checkSession);
     // checks the correctness of the query
     if ($result['status'] == $positiveStatus) {
         // die Antwort war so, wie wir sie erwartet haben
         $queryResult = Query::decodeQuery($result['content']);
         if (!is_array($queryResult)) {
             $queryResult = array($queryResult);
         }
         // rufe nun die positive Methode auf
         return call_user_func_array($positiveMethod, array_merge(array("input" => $queryResult), $positiveParams));
     }
     // ansonsten rufen wir die negative Methode auf
     return call_user_func_array($negativeMethod, $negativeParams);
 }