commit
d5e4eb909d
@ -0,0 +1,35 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace ChamiloLMS\Command\Transaction; |
||||||
|
|
||||||
|
use Symfony\Component\Console\Command\Command; |
||||||
|
use Symfony\Component\Console\Input\InputInterface; |
||||||
|
use Symfony\Component\Console\Input\InputOption; |
||||||
|
use Symfony\Component\Console\Output\OutputInterface; |
||||||
|
use ChamiloLMS\Transaction\TransactionLogController; |
||||||
|
|
||||||
|
class ImportToSystemCommand extends Command |
||||||
|
{ |
||||||
|
protected function configure() |
||||||
|
{ |
||||||
|
$this |
||||||
|
->setName('tx:import-to-system') |
||||||
|
->setDescription('Imports transactions on the transaction table to the local system.') |
||||||
|
->addOption('limit', null, InputOption::VALUE_OPTIONAL, 'The maximum number of transactions to import into the system in this operation.', 10); |
||||||
|
} |
||||||
|
|
||||||
|
protected function execute(InputInterface $input, OutputInterface $output) |
||||||
|
{ |
||||||
|
$tc = new TransactionLogController(); |
||||||
|
$limit = (int) $input->getOption('limit'); |
||||||
|
if ($limit <= 0) { |
||||||
|
$limit = 10; |
||||||
|
} |
||||||
|
$imported_ids = $tc->importPendingToSystem($limit); |
||||||
|
$output->writeln(sprintf('Imported correctly (%d) transactions to the system.', count($imported_ids['success']))); |
||||||
|
if (!empty($imported_ids['fail'])) { |
||||||
|
$output->writeln(sprintf('The transactions identified by the following ids failed to be imported: (%s)', implode(', ', $imported_ids['fail']))); |
||||||
|
return 1; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,56 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace ChamiloLMS\Command\Transaction; |
||||||
|
|
||||||
|
use Database; |
||||||
|
use Symfony\Component\Console\Command\Command; |
||||||
|
use Symfony\Component\Console\Input\ArrayInput; |
||||||
|
use Symfony\Component\Console\Input\InputInterface; |
||||||
|
use Symfony\Component\Console\Input\InputArgument; |
||||||
|
use Symfony\Component\Console\Output\OutputInterface; |
||||||
|
|
||||||
|
/** |
||||||
|
* A customization of tx:send for specific case. |
||||||
|
* |
||||||
|
* Assumtions: |
||||||
|
* - branch_rel_session table only contains the local branch entries. |
||||||
|
* - branch_rel_session.display_order represents a "turn". |
||||||
|
*/ |
||||||
|
class MineduSendCommand extends Command |
||||||
|
{ |
||||||
|
protected function configure() |
||||||
|
{ |
||||||
|
$this |
||||||
|
->setName('minedu:send') |
||||||
|
->setDescription('Sends data using tx:send, using custom minedu logic to convert turn numbers into a course/session pair.') |
||||||
|
->addArgument('turn', InputArgument::REQUIRED, 'The turn to be used.'); |
||||||
|
} |
||||||
|
|
||||||
|
protected function execute(InputInterface $input, OutputInterface $output) |
||||||
|
{ |
||||||
|
// This is just wrong, but was requested. Used to pass a session_id to |
||||||
|
// be used on the MineduAuthHttpsPostSend plugin. |
||||||
|
global $session_id; |
||||||
|
|
||||||
|
$turn = $input->getArgument('turn'); |
||||||
|
$branch_rel_session_table = Database::get_main_table(TABLE_BRANCH_REL_SESSION); |
||||||
|
$results = Database::select('session_id', $branch_rel_session_table, array('where'=> array('display_order = ?' => array($turn)))); |
||||||
|
if (empty($results)) { |
||||||
|
$output->writeln(sprintf('Failed to retrive a session id for the given turn "%s".', $turn)); |
||||||
|
return 100; |
||||||
|
} |
||||||
|
$row = array_shift($results); |
||||||
|
$session_id = $row['session_id']; |
||||||
|
$command = $this->getApplication()->find('tx:send'); |
||||||
|
$arguments = array( |
||||||
|
'command' => 'tx:send', |
||||||
|
'--session' => $session_id, |
||||||
|
); |
||||||
|
$input = new ArrayInput($arguments); |
||||||
|
$return_code = $command->run($input, $output); |
||||||
|
if ($return_code !== 0) { |
||||||
|
$output->writeln('Failed trying to send the turn information.'); |
||||||
|
return $return_code; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,35 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace ChamiloLMS\Command\Transaction; |
||||||
|
|
||||||
|
use Symfony\Component\Console\Command\Command; |
||||||
|
use Symfony\Component\Console\Input\InputInterface; |
||||||
|
use Symfony\Component\Console\Input\InputOption; |
||||||
|
use Symfony\Component\Console\Output\OutputInterface; |
||||||
|
use ChamiloLMS\Transaction\TransactionLogController; |
||||||
|
|
||||||
|
class ProcessReceivedEnvelopesCommand extends Command |
||||||
|
{ |
||||||
|
protected function configure() |
||||||
|
{ |
||||||
|
$this |
||||||
|
->setName('tx:process-received-envelopes') |
||||||
|
->setDescription('Process already received envelopes to extract its transactions to the transactions table.') |
||||||
|
->addOption('limit', null, InputOption::VALUE_OPTIONAL, 'The maximum number of received envelopes to process in this operation.'); |
||||||
|
} |
||||||
|
|
||||||
|
protected function execute(InputInterface $input, OutputInterface $output) |
||||||
|
{ |
||||||
|
$tc = new TransactionLogController(); |
||||||
|
$limit = (int) $input->getOption('limit'); |
||||||
|
if ($limit <= 0) { |
||||||
|
$limit = 10; |
||||||
|
} |
||||||
|
$imported_transaction_ids_by_envelope = $tc->importPendingEnvelopes($limit); |
||||||
|
$total_transactions = 0; |
||||||
|
foreach ($imported_transaction_ids_by_envelope as $envelope_id => $imported_transaction_ids) { |
||||||
|
$total_transactions += count($imported_transaction_ids); |
||||||
|
} |
||||||
|
$output->writeln(sprintf('Imported correctly (%d) transactions from (%d) envelopes to the transactions table.', $total_transactions, count($imported_transaction_ids_by_envelope))); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,31 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace ChamiloLMS\Command\Transaction; |
||||||
|
|
||||||
|
use Symfony\Component\Console\Command\Command; |
||||||
|
use Symfony\Component\Console\Input\InputInterface; |
||||||
|
use Symfony\Component\Console\Input\InputOption; |
||||||
|
use Symfony\Component\Console\Output\OutputInterface; |
||||||
|
use ChamiloLMS\Transaction\TransactionLogController; |
||||||
|
|
||||||
|
class ReceiveCommand extends Command |
||||||
|
{ |
||||||
|
protected function configure() |
||||||
|
{ |
||||||
|
$this |
||||||
|
->setName('tx:receive') |
||||||
|
->setDescription('Runs local branch associated receive plugin processing envelope reception.') |
||||||
|
->addOption('limit', null, InputOption::VALUE_OPTIONAL, 'The maximum number of envelopes to receive in this operation.', 1); |
||||||
|
} |
||||||
|
|
||||||
|
protected function execute(InputInterface $input, OutputInterface $output) |
||||||
|
{ |
||||||
|
$tc = new TransactionLogController(); |
||||||
|
$limit = (int) $input->getOption('limit'); |
||||||
|
if ($limit <= 0) { |
||||||
|
$limit = 1; |
||||||
|
} |
||||||
|
$envelopes = $tc->receiveEnvelopeData($limit); |
||||||
|
$output->writeln(sprintf('Correctly received (%d) envelopes now added to the queue.', count($envelopes))); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,105 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace ChamiloLMS\Command\Transaction; |
||||||
|
|
||||||
|
use Symfony\Component\Console\Command\Command; |
||||||
|
use Symfony\Component\Console\Input\InputInterface; |
||||||
|
use Symfony\Component\Console\Input\InputOption; |
||||||
|
use Symfony\Component\Console\Output\OutputInterface; |
||||||
|
use ChamiloLMS\Transaction\Envelope; |
||||||
|
use ChamiloLMS\Transaction\TransactionLogController; |
||||||
|
use ChamiloLMS\Transaction\TransactionLog; |
||||||
|
|
||||||
|
/** |
||||||
|
* Selects a subset of transactions, wraps and send them. |
||||||
|
* |
||||||
|
* It will use local branch settings. |
||||||
|
*/ |
||||||
|
class SendCommand extends Command |
||||||
|
{ |
||||||
|
protected $local_branch = null; |
||||||
|
|
||||||
|
public function __construct(string $name = null) |
||||||
|
{ |
||||||
|
parent::__construct($name); |
||||||
|
// @todo Is there a clean way/no-global to access app from here? |
||||||
|
global $app; |
||||||
|
$this->local_branch = $app['orm.em']->getRepository('Entity\BranchSync')->getLocalBranch(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @todo Let accept multiple values on options. Notice Database::select() |
||||||
|
* does not allow multiple values. |
||||||
|
*/ |
||||||
|
protected function configure() |
||||||
|
{ |
||||||
|
$this |
||||||
|
->setName('tx:send') |
||||||
|
->setDescription('Selects a subset of transactions, wraps and send them using local branch configuration.') |
||||||
|
->addOption('course', null, InputOption::VALUE_OPTIONAL, 'A course ID to select transactions.', '') |
||||||
|
->addOption('session', null, InputOption::VALUE_OPTIONAL, 'A session ID to select transactions.', ''); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Convert input options into TransactionLogController::load() conditions. |
||||||
|
* |
||||||
|
* @todo Validate arguments? |
||||||
|
*/ |
||||||
|
protected function getSelectConditions(InputInterface $input) |
||||||
|
{ |
||||||
|
$conditions = array( |
||||||
|
'branch_id' => $this->local_branch->getId(), |
||||||
|
'status_id' => TransactionLog::STATUS_LOCAL, |
||||||
|
); |
||||||
|
// Course. |
||||||
|
$course_string = $input->getOption('course'); |
||||||
|
if (strlen($course_string) > 0) { |
||||||
|
$conditions['c_id'] = $course_string; |
||||||
|
} |
||||||
|
// Session. |
||||||
|
$session_string = $input->getOption('session'); |
||||||
|
if (strlen($session_string) > 0) { |
||||||
|
$conditions['session_id'] = $session_string; |
||||||
|
} |
||||||
|
return $conditions; |
||||||
|
} |
||||||
|
|
||||||
|
protected function execute(InputInterface $input, OutputInterface $output) |
||||||
|
{ |
||||||
|
$tc = new TransactionLogController(); |
||||||
|
$transactions = $tc->load($this->getSelectConditions($input)); |
||||||
|
if (empty($transactions)) { |
||||||
|
$output->writeln('No transactions to be exported'); |
||||||
|
return 1; |
||||||
|
} |
||||||
|
|
||||||
|
// Export. |
||||||
|
$export_result = $tc->exportTransactions($transactions); |
||||||
|
if (!empty($export_result['fail'])) { |
||||||
|
$fail_data = print_r($export_result['fail'], 1); |
||||||
|
$output->writeln('Failed exporting some transactions:'); |
||||||
|
$output->writeln("$fail_data"); |
||||||
|
return 2; |
||||||
|
} |
||||||
|
|
||||||
|
// Create an evelope and wrap it. |
||||||
|
$wrapper = TransactionLogController::createPlugin('wrapper', $this->local_branch->getPluginEnvelope(), $this->local_branch->getPluginData('wrapper')); |
||||||
|
$envelope_data = array('transactions' => $transactions, 'origin_branch_id' => $this->local_branch->getId()); |
||||||
|
$envelope = new Envelope($wrapper, $envelope_data); |
||||||
|
try { |
||||||
|
$envelope->wrap(); |
||||||
|
} |
||||||
|
catch (Exception $e) { |
||||||
|
$output->writeln(sprintf('Failed wrapping the envelope: %s.', $e->getMessage())); |
||||||
|
return 3; |
||||||
|
} |
||||||
|
|
||||||
|
// Finally send it. |
||||||
|
$success = $tc->sendEnvelope($envelope); |
||||||
|
if ($success !== TRUE) { |
||||||
|
$output->writeln('There was a problem while sending the envelope.'); |
||||||
|
return 4; |
||||||
|
} |
||||||
|
$output->writeln('Envelope sent!'); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,45 @@ |
|||||||
|
<?php |
||||||
|
/* For licensing terms, see /license.txt */ |
||||||
|
|
||||||
|
namespace ChamiloLMS\Transaction\Plugin; |
||||||
|
|
||||||
|
use ChamiloLMS\Transaction\Envelope; |
||||||
|
|
||||||
|
/** |
||||||
|
* Customization to have an specific file name. |
||||||
|
*/ |
||||||
|
class MineduAuthHttpsPostSend extends AuthHttpsPostSend |
||||||
|
{ |
||||||
|
|
||||||
|
/** |
||||||
|
* Base name for files to send for minedu. |
||||||
|
*/ |
||||||
|
const BASE_NAME='PE_0265'; |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public static function getMachineName() |
||||||
|
{ |
||||||
|
return 'minedu_auth_https_post'; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* A custom name for the teporary filename to send via curl. |
||||||
|
* Retrieves a temporary filename. |
||||||
|
*/ |
||||||
|
protected function getTemporaryFileToSend($name, Envelope $envelope) { |
||||||
|
// @fixme This is just wrong but it was requested. An Envelope nor any |
||||||
|
// place from here really knows which session_id to use because an |
||||||
|
// Envelope just contains a list of transactions, it does not matter if |
||||||
|
// they are part of one session or multiple ones. |
||||||
|
global $session_id; |
||||||
|
if ($name != 'blob_file') { |
||||||
|
return parent::getTemporaryFileToSend($name, $envelope); |
||||||
|
} |
||||||
|
// At blob_file request. |
||||||
|
$stamp = str_replace(array(' ', ':', '-'), '', api_get_datetime()); |
||||||
|
$filename = sprintf('%s_%05d_%05d_%s_', self::BASE_NAME, $envelope->getOriginBranchId(), $session_id, $stamp); |
||||||
|
return parent::getTemporaryFileToSend($filename, $envelope); |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue