Quellcode durchsuchen

Server lp with url to show in browser for REST api - refs #8366

Angel Fernando Quiroz Campos vor 8 Jahren
Ursprung
Commit
32110808f9
2 geänderte Dateien mit 95 neuen und 9 gelöschten Zeilen
  1. 76 5
      main/inc/lib/webservices/Rest.php
  2. 19 4
      main/webservices/api/v2.php

+ 76 - 5
main/inc/lib/webservices/Rest.php

@@ -6,6 +6,7 @@ use Chamilo\CoreBundle\Entity\ExtraFieldValues;
 use Chamilo\CourseBundle\Entity\Repository\CAnnouncementRepository;
 use Chamilo\CourseBundle\Entity\Repository\CNotebookRepository;
 use Chamilo\CourseBundle\Entity\CLpCategory;
+use ChamiloSession as Session;
 
 /**
  * Class RestApi
@@ -31,6 +32,7 @@ class Rest extends WebService
     const ACTION_COURSE_FORUM = 'course_forum';
     const ACTION_COURSE_FORUM_THREAD = 'course_forumthread';
     const ACTION_COURSE_LEARNPATHS = 'course_learnpaths';
+    const ACTION_COURSE_LEARNPATH = 'course_learnpath';
 
     const EXTRAFIELD_GCM_ID = 'gcm_registration_id';
 
@@ -668,8 +670,6 @@ class Rest extends WebService
 
         $categories = array_merge([$categoryNone], $categoriesTempList);
 
-        $userId = api_get_user_id();
-
         $categoryData = array();
 
         /** @var CLpCategory $category */
@@ -739,16 +739,28 @@ class Rest extends WebService
                     }
                 }
 
-                $lpStartUrl = api_get_cidreq() . '&action=view&lp_id=' . $lpId;
-                $progress = learnpath::getProgress($lpId, $userId, $courseId);
+                $progress = learnpath::getProgress($lpId, $this->user->getId(), $courseId);
 
                 $listData[] = array(
-                    'urlStart' => rawurlencode($lpStartUrl),
+                    'id' => $lpId,
                     'title' => Security::remove_XSS($lpDetails['lp_name']),
                     'progress' => intval($progress),
+                    'url' => api_get_path(WEB_CODE_PATH) . 'webservices/api/v2.php?' . http_build_query([
+                        'hash' => $this->encodeParams([
+                            'action' => 'course_learnpath',
+                            'lp_id' => $lpId,
+                            'cidReq' => $course->getCode(),
+                            'id_session' => 0,
+                            'gidReq' => 0
+                        ])
+                    ])
                 );
             }
 
+            if (empty($listData)) {
+                continue;
+            }
+
             $categoryData[] = array(
                 'id' => $category->getId(),
                 'name' => $category->getName(),
@@ -758,4 +770,63 @@ class Rest extends WebService
 
         return $categoryData;
     }
+
+    /**
+     * @param array $additionalParams Optional
+     * @return string
+     */
+    private function encodeParams(array $additionalParams = [])
+    {
+        $params = array_merge($additionalParams, [
+            'api_key' => $this->apiKey,
+            'username' => $this->user->getUsername(),
+        ]);
+
+        $strParams = serialize($params);
+
+        $b64Encoded = base64_encode($strParams);
+
+        return str_replace(['+', '/', '='], ['-', '_', '.'], $b64Encoded);
+    }
+
+    /**
+     * @param string $encoded
+     * @return array
+     */
+    public static function decodeParams($encoded){
+        $decoded = str_replace(['-', '_', '.'], ['+', '/', '='], $encoded);
+        $mod4 = strlen($decoded) % 4;
+
+        if ($mod4) {
+            $decoded .= substr('====', $mod4);
+        }
+
+        $b64Decoded = base64_decode($decoded);
+
+        return unserialize($b64Decoded);
+    }
+
+    /**
+     * Start login for a user. Then make a redirect to show the learnpath
+     * @param int $lpId
+     * @param string $courseCode
+     */
+    public function showLearningPath($lpId, $courseCode)
+    {
+        $loggedUser['user_id'] = $this->user->getId();
+        $loggedUser['status'] = $this->user->getStatus();
+        $loggedUser['uidReset'] = true;
+
+        Session::write('_user', $loggedUser);
+        Login::init_user($this->user->getId(), true);
+
+        $url = api_get_path(WEB_CODE_PATH) . 'lp/lp_controller.php?' . http_build_query([
+            'action' => 'view',
+            'lp_id' => intval($lpId),
+            'cidReq' => $courseCode
+        ]);
+
+        header("Location: $url");
+        exit;
+    }
 }

+ 19 - 4
main/webservices/api/v2.php

@@ -3,9 +3,19 @@
 
 require_once '../../inc/global.inc.php';
 
+$hash = isset($_REQUEST['hash']) ? $_REQUEST['hash'] : null;
+
+if ($hash) {
+    $hashParams = Rest::decodeParams($hash);
+
+    foreach ($hashParams as $key => $value) {
+        $_REQUEST[$key] = $value;
+    }
+}
+
 $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;
+$username = isset($_REQUEST['username']) ? Security::remove_XSS($_REQUEST['username']) : null;
+$apiKey = isset($_REQUEST['api_key']) ? Security::remove_XSS($_REQUEST['api_key']) : null;
 
 $restResponse = new RestResponse();
 
@@ -13,8 +23,6 @@ try {
     /** @var Rest $restApi */
     $restApi = $apiKey ? Rest::validate($username, $apiKey) : null;
 
-
-
     switch ($action) {
         case Rest::ACTION_AUTH:
             Rest::init();
@@ -151,6 +159,13 @@ try {
             $restResponse->setData($data);
             break;
 
+        case Rest::ACTION_COURSE_LEARNPATH:
+            $lpId = isset($_REQUEST['lp_id']) ? intval($_REQUEST['lp_id']) : 0;
+            $cidReq = isset($_REQUEST['cidReq']) ? Security::remove_XSS($_REQUEST['cidReq']) : 0;
+
+            $restApi->showLearningPath($lpId, $cidReq);
+            break;
+
         default:
             throw new Exception(get_lang('InvalidAction'));
     }