Selaa lähdekoodia

tx-io: psr0, Converts transaction code to PSR-0.

- Removes the idea of configurable classes for transactions and its
  controllers. In PSR-0 the type of dynamic loading used before is hard
  to reimplement because of php processing the 'use' clauses on compile
  time. Now there is only one controller class, which needed the
  following changes:
  - Added a createTransaction() method, so it now behaves like a factory
    too.
  - Added a getTransactionClass() method, which contains a hard-coded
    mapping between transaction action keywords and transaction class
    names.
  - Added a new loadOne() method, to generalize the custom exercise
    transaction controller load_exercise_attempt() method.
- Changes all related code to follow new transaction changes.
Marco Villegas 11 vuotta sitten
vanhempi
commit
52a2285cc4

+ 9 - 8
main/exercice/exercise.class.php

@@ -11,6 +11,8 @@
  * Code
  */
 use \ChamiloSession as Session;
+use \ChamiloLMS\Transaction\TransactionLog;
+use \ChamiloLMS\Transaction\TransactionLogController;
 
 define('ALL_ON_ONE_PAGE', 1);
 define('ONE_PER_PAGE', 2);
@@ -6243,20 +6245,19 @@ class Exercise
                     '',
                     array()
                   );
-                // @todo: Is this really needed? see 33cd962f077ed8c2e4b696bdd18b54db00890ef2
-                require_once api_get_path(LIBRARY_PATH).'transaction.lib.php';
                 $log_transactions_settings = TransactionLog::getTransactionSettings();
                 if (isset($log_transactions_settings['exercise_attempt'])) {
-                  $transaction_actions_map = TransactionLog::getTransactionMappingSettings('exercise_attempt');
-                  $controller_class = $transaction_actions_map['controller'];
-                  $transaction_class = $transaction_actions_map['class'];
-                  $transaction_controller = new $controller_class();
-                  $transaction = $transaction_controller->load_exercise_attempt($exercise_stat_info['exe_id']);
+                  $transaction_controller = new TransactionLogController();
+                  $transaction = $transaction_controller->loadOne(array(
+                    'action' => 'exercise_attempt',
+                    'branch_id' => TransactionLog::BRANCH_LOCAL,
+                    'item_id' => $exercise_stat_info['exe_id'],
+                  ));
                   if (!$transaction) {
                     $transaction_data = array(
                       'item_id' => $exercise_stat_info['exe_id'],
                     );
-                    $transaction = new $transaction_class($transaction_data);
+                    $transaction = $transaction_controller->createTransaction('exercise_attempt', $transaction_data);
                   }
                   $transaction->save();
                 }

+ 9 - 9
main/inc/ajax/exercise.ajax.php

@@ -8,10 +8,9 @@ require_once api_get_path(SYS_CODE_PATH).'exercice/exercise.class.php';
 require_once api_get_path(SYS_CODE_PATH).'exercice/question.class.php';
 require_once api_get_path(SYS_CODE_PATH).'exercice/answer.class.php';
 
-// @todo: Is this really needed? see 33cd962f077ed8c2e4b696bdd18b54db00890ef2
-require_once api_get_path(LIBRARY_PATH).'transaction.lib.php';
-
 use \ChamiloSession as Session;
+use \ChamiloLMS\Transaction\TransactionLog;
+use \ChamiloLMS\Transaction\TransactionLogController;
 
 api_protect_course_script(true);
 
@@ -560,16 +559,17 @@ switch ($action) {
 
                 $log_transactions_settings = TransactionLog::getTransactionSettings();
                 if (isset($log_transactions_settings['exercise_attempt'])) {
-                  $transaction_actions_map = TransactionLog::getTransactionMappingSettings('exercise_attempt');
-                  $controller_class = $transaction_actions_map['controller'];
-                  $transaction_class = $transaction_actions_map['class'];
-                  $transaction_controller = new $controller_class();
-                  $transaction = $transaction_controller->load_exercise_attempt($exe_id);
+                  $transaction_controller = new TransactionLogController();
+                  $transaction = $transaction_controller->loadOne(array(
+                    'action' => 'exercise_attempt',
+                    'branch_id' => TransactionLog::BRANCH_LOCAL,
+                    'item_id' => $exe_id,
+                  ));
                   if (!$transaction) {
                     $transaction_data = array(
                       'item_id' => $exe_id,
                     );
-                    $transaction = new $transaction_class($transaction_data);
+                    $transaction = $transaction_controller->createTransaction('exercise_attempt', $transaction_data);
                   }
                   $transaction->save();
                 }

+ 0 - 23
src/ChamiloLMS/Transaction/ExerciseAttemptTransactionLogController.php

@@ -1,23 +0,0 @@
-<?php
-/* For licensing terms, see /license.txt */
-
-namespace ChamiloLMS\Transaction;
-
-/**
- * Controller for exercise tool attempt transactions.
- */
-class ExerciseAttemptTransactionLogController extends TransactionLogController {
-  /**
-   * Retrieves an individual exercise attempt transaction.
-   *
-   * @return boolean|ExerciseAttemptTransactionLog
-   *   FALSE if not found, or the corresponding object.
-   */
-  public function load_exercise_attempt($attempt_id, $branch_id = TransactionLog::BRANCH_LOCAL) {
-    $transactions = $this->load(array('action' => 'exercise_attempt', 'branch_id' => $branch_id, 'item_id' => $attempt_id));
-    if (empty($transactions)) {
-      return FALSE;
-    }
-    return array_shift($transactions);
-  }
-}

+ 1 - 1
src/ChamiloLMS/Transaction/TransactionExportException.php

@@ -6,5 +6,5 @@ namespace ChamiloLMS\Transaction;
 /**
  * A custom exception for transaction exports.
  */
-class TransactionExportException extends Exception {
+class TransactionExportException extends \Exception {
 }

+ 1 - 1
src/ChamiloLMS/Transaction/TransactionImportException.php

@@ -6,5 +6,5 @@ namespace ChamiloLMS\Transaction;
 /**
  * A custom exception for transaction imports.
  */
-class TransactionImportException extends Exception {
+class TransactionImportException extends \Exception {
 }

+ 3 - 3
src/ChamiloLMS/Transaction/TransactionLog.php

@@ -3,6 +3,8 @@
 
 namespace ChamiloLMS\Transaction;
 
+use Database;
+
 /**
  * Base transaction log class.
  */
@@ -134,9 +136,7 @@ abstract class TransactionLog {
 
   public function getController() {
     if (empty($this->controller)) {
-      $transaction_actions_map = TransactionLog::getTransactionMappingSettings($this->action);
-      $controller_class = $transaction_actions_map['controller'];
-      $this->controller = new $controller_class();
+      $this->controller = new TransactionLogController();
     }
     return $this->controller;
   }

+ 61 - 14
src/ChamiloLMS/Transaction/TransactionLogController.php

@@ -3,6 +3,9 @@
 
 namespace ChamiloLMS\Transaction;
 
+use Database;
+use ChamiloLMS\Transaction\ExerciseAttemptTransactionLog;
+
 /**
  * Controller class for transactions.
  */
@@ -37,7 +40,6 @@ class TransactionLogController {
    *   A list of TransactionLog object that match passed conditions.
    */
   public function load($db_fields) {
-    $transaction_actions_map = TransactionLog::getTransactionMappingSettings();
     foreach ($db_fields as $db_field => $db_value) {
       $conditions[] = "$db_field = ?";
       $values[] = $db_value;
@@ -45,29 +47,41 @@ class TransactionLogController {
     $results = Database::select('*', $this->table, array('where' => array(implode(' AND ', $conditions) => $values)));
     $objects = array();
     foreach ($results as $result) {
-      $class_name = $transaction_actions_map[$result['action']]['class'];
-      $objects[] = new $class_name($result);
+      $objects[] = self::createTransaction($result['action'], $result);
     }
     return $objects;
   }
 
   /**
-   * Loads by id.
+   * Loads one transaction based on parameters.
    *
-   * @param int
-   *   branch_transaction.id
+   * @param array $db_fields
+   *   See self::load().
    *
    * @return boolean|TransactionLog
    *   FALSE if not found, or the corresponding object.
    */
-  public function load_by_id($id) {
-    $transactions = $this->load(array('id' => $id));
+  public function loadOne($db_fields) {
+    $transactions = $this->load($db_fields);
     if (empty($transactions)) {
       return FALSE;
     }
     return array_shift($transactions);
   }
 
+  /**
+   * Loads by id.
+   *
+   * @param int
+   *   branch_transaction.id
+   *
+   * @return boolean|TransactionLog
+   *   FALSE if not found, or the corresponding object.
+   */
+  public function load_by_id($id) {
+    return $this->loadOne(array('id' => $id));
+  }
+
   /**
    * Load by branch and transaction.
    *
@@ -98,16 +112,14 @@ class TransactionLogController {
    *   A set of transaction ids correctly added.
    */
   public function importToLog($exported_transactions) {
-    $transaction_actions_map = TransactionLog::getTransactionMappingSettings();
     $added_transactions = array();
     foreach ($exported_transactions as $exported_transaction) {
       $transaction_data = json_decode($exported_transaction, TRUE);
-      $class_name = $transaction_actions_map[$transaction_data['action']]['class'];
       // Set the right id in the new system.
       $transaction_data['transaction_id'] = $transaction_data['id'];
       unset($transaction_data['id']);
       $transaction_data['status_id'] = TransactionLog::STATUS_TO_BE_EXECUTED;
-      $transaction = new $class_name($transaction_data);
+      $transaction = self::createTransaction($transaction_data['action'], $transaction_data);
       $transaction->save();
       $added_transactions[] = $transaction->id;
     }
@@ -183,13 +195,11 @@ class TransactionLogController {
   public function importPendingToSystem($limit = 10) {
     // Sadly multiple values are not supported by Database::select(), aka IN
     // operation.
-    $transaction_actions_map = TransactionLog::getTransactionMappingSettings();
     $sql = sprintf('SELECT * FROM %s WHERE branch_id != %d AND status_id IN (%d, %d) LIMIT %d', $this->table, TransactionLog::BRANCH_LOCAL, TransactionLog::STATUS_TO_BE_EXECUTED, TransactionLog::STATUS_FAILED, $limit);
     $result = Database::query($sql);
     $transactions = array();
     while ($row = $result->fetch()) {
-      $class_name = $transaction_actions_map[$row['action']]['class'];
-      $transactions[$row['id']] = new $class_name($row);
+      $transactions[$row['id']] = self::createTransaction($row['action'], $row);
       $transactions[$row['id']]->loadData();
     }
     return $this->importToSystem($transactions);
@@ -216,4 +226,41 @@ class TransactionLogController {
     );
     return Database::insert($this->log_table, $log_entry);
   }
+
+  /**
+   * Creates a new transaction object based on passed information.
+   *
+   * @param string $action
+   *   The action keyword mapped to the transaction class.
+   * @param array $data
+   *   See the action type constructor.
+   *
+   * @return TransactionLog
+   *   A transaction object.
+   */
+  public static function createTransaction($action, $data) {
+    $class_name = self::getTransactionClass($action);
+    return new $class_name($data);
+  }
+
+  /**
+   * Returns the class name related with the action passed.
+   *
+   * @param string $action
+   *   The action keyword mapped to the transaction class.
+   *
+   * @return string
+   *   The related transaction class name.
+   */
+  public static function getTransactionClass($action) {
+    // Do the mapping manually. It seems like it cannot be done dynamically
+    // because on PSR-0 we need to add a 'use' clause per used class, which php
+    // process on compiling, so it cannot be discovered and loaded in runtime.
+    // Instead all possible transaction classes are added at the start of this
+    // file.
+    $map = array(
+      'exercise_attempt' => 'ExerciseAttemptTransactionLog',
+    );
+    return '\ChamiloLMS\Transaction\\' . $map[$action];
+  }
 }