Afegit mètode getClasses

Aquest mètode retorna una llista de classes. Ara per ara, només retorna
les classes que s'estan realitzant en l'instant de fer la petició.

Si l'usuari ha iniciat sessió, es retorna també si la classe és d'una
assignatura escollida per l'alumne.

Change-Id: Ia75ee7867003745f2fbc1effa4c444d5edbf41a2
diff --git a/inc/API.php b/inc/API.php
index b8aa454..f1763ea 100644
--- a/inc/API.php
+++ b/inc/API.php
@@ -144,8 +144,12 @@
         break;
 
       case 'getClasses':
-        self::checkSignInStatus();
-        // @TODO: Implement this method
+        $body = self::getJSONBody();
+        $response = Classes::handleAPIGetClasses($body);
+        if ($response === false)
+          self::returnError();
+        else
+          self::returnPayload($response);
         break;
 
       case 'setClassState':
diff --git a/inc/Classes.php b/inc/Classes.php
new file mode 100644
index 0000000..5059040
--- /dev/null
+++ b/inc/Classes.php
@@ -0,0 +1,62 @@
+<?php
+namespace DAFME\Covid;
+
+class Classes {
+  public static function getCurrentClasses() {
+    global $con;
+
+    $isSignedIn = Users::isSignedIn();
+
+    $sentence = 'SELECT c.id, c.calendar_name, c.room, c.begins, c.ends, s.id subject_id, s.friendly_name'.($isSignedIn ? ', u_s.id user_subject_id' : '').'
+        FROM classes c
+        INNER JOIN subjects s
+          ON c.calendar_name = s.calendar_name
+        '.($isSignedIn ? 'LEFT OUTER JOIN user_subjects u_s
+          ON s.id = u_s.subject_id
+        ' : '').
+        'WHERE
+          c.begins < NOW() AND
+          c.ends > NOW()'.($isSignedIn ? ' AND
+          (
+            u_s.user_id = :user_id OR
+            u_s.subject_id IS NULL
+          )': '');
+    $query = $con->prepare($sentence);
+    
+    if (!$query->execute(($isSignedIn ? ['user_id' => Users::getUserId()] : [])))
+      return false;
+
+    $classes = $query->fetchAll(\PDO::FETCH_ASSOC);
+
+    foreach ($classes as &$class) {
+      if ($isSignedIn)
+        $class['user_subject_id'] = null;
+
+      $class['user_selected'] = $class['user_subject_id'] !== null;
+    }
+
+    return $classes;
+  }
+
+  public static function handleAPIGetClasses($body) {
+    if (!isset($body['type']))
+      return false;
+
+    $response = [];
+
+    switch ($body['type']) {
+      case 'current':
+        $classes = self::getCurrentClasses();
+        if ($classes === false)
+          return false;
+
+        $response['classes'] = $classes;
+        break;
+
+      default:
+        return false;
+    }
+
+    return $response;
+  }
+}