Skip to content

Commit

Permalink
Release 1.182.0
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaliikovali committed Mar 26, 2024
1 parent 829eefa commit abeed41
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 16 deletions.
21 changes: 21 additions & 0 deletions classes/helpers/DBHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,18 @@ public static function get_db_client($penetrationtype = self::PENETRATION_TYPE_I

if (in_array($CFG->dbtype, self::$supporteddbclients[$penetrationtype])) {

if (isset(self::$customdbclient[$penetrationtype]) && !self::is_database_connected(self::$customdbclient[$penetrationtype])) {
try {
self::$customdbclient[$penetrationtype]->dispose();
} catch (\Throwable $e) {
// Ignore if connection already disposed.

$e->getMessage();
}

unset(self::$customdbclient[$penetrationtype]);
}

if (!isset(self::$customdbclient[$penetrationtype])) {
$db = self::get_driver_instance($CFG->dbtype, $penetrationtype);
$db->connect($CFG->dbhost, $CFG->dbuser, $CFG->dbpass, $CFG->dbname, $CFG->prefix, $CFG->dboptions);
Expand All @@ -556,4 +568,13 @@ public static function get_db_client($penetrationtype = self::PENETRATION_TYPE_I

return $DB;
}

public static function is_database_connected($db) {
try {
$db->get_records_sql("SELECT 1");
return true;
} catch (\Exception $e) {
return false;
}
}
}
25 changes: 24 additions & 1 deletion classes/services/migration_service.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

namespace local_intellidata\services;

use local_intellidata\helpers\DBHelper;
use local_intellidata\helpers\MigrationHelper;
use local_intellidata\helpers\SettingsHelper;
use local_intellidata\helpers\ParamsHelper;
Expand Down Expand Up @@ -64,14 +65,36 @@ public function __construct($exportformatkey = null, $exportservice = null) {
* @param false $cronprocessing
*/
public function process($params = null, $cronprocessing = false) {
global $DB, $CFG;

$alltables = $this->tables;
$tables = (!empty($params['datatype']) && isset($alltables[$params['datatype']])) ?
[$params['datatype'] => $alltables[$params['datatype']]] : $this->tables;

if (count($tables)) {
foreach ($tables as $table) {
$this->export_table($table, $params, $cronprocessing);
$retries = 1;
do {
try {
$this->export_table($table, $params, $cronprocessing);
$continue = false;
} catch (\dml_exception $e) {
if (!isset($CFG->intellidata_db_reconnects) || $CFG->intellidata_db_reconnects == 0) {
throw $e;
}

sleep($retries);
$DB = DBHelper::get_db_client(DBHelper::PENETRATION_TYPE_EXTERNAL);
$continue = true;
$reconnectsremaining = $CFG->intellidata_db_reconnects - $retries;
mtrace($e->getMessage());
mtrace("Database connection lost: Reconnected: {$reconnectsremaining} reconnection attempts remaining. ");

if (++$retries > $CFG->intellidata_db_reconnects) {
throw $e;
}
}
} while ($continue);

// If it is processing by cron, we need to allow only one table processing.
if ($cronprocessing) {
Expand Down
35 changes: 33 additions & 2 deletions classes/services/new_export_service.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,17 @@ public function insert_records_event($table, $dataobjects) {
$requiredatatype = !isset($entity::$datatype);

foreach ($dataobjects as $dataobject) {
$record = $DB->get_record($table, $this->prepare_data_for_query($table, (array)$dataobject));
if ($requiredatatype && !$this->filter_id($entity::TYPE, (object)$dataobject)) {
$params = $this->prepare_data_for_query($table, (array)$dataobject);
$records = $DB->get_records($table, $params, 'id DESC', '*', 0, 1);
if (!$record = array_shift($records)) {
continue;
}
} else {
$record = (object)$dataobject;
}

if ($requiredatatype && !$this->filter($entity::TYPE, $record)) {
if (!isset($record) || ($requiredatatype && !$this->filter($entity::TYPE, $record))) {
continue;
}

Expand Down Expand Up @@ -400,6 +408,29 @@ private function filter($datatype, $data) {
return $access;
}

/**
* @param string $table
* @param \stdClass $data
* @return bool
*/
private function filter_id($datatype, $data) {
$needid = true;
switch ($datatype) {
case 'participation':
if (!in_array($data->crud, ['c', 'u']) || !$data->userid ||
!in_array($data->contextlevel, [CONTEXT_COURSE, CONTEXT_MODULE])) {
$needid = false;
}
break;
case 'userlogins':
$needid = isset($data->eventname) && ($data->eventname == '\core\event\user_loggedin') &&
$data->contextid == 1;
break;
}

return $needid;
}

/**
* @param string $datatype
* @param \stdClass $data
Expand Down
1 change: 0 additions & 1 deletion lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,3 @@ function local_intellidata_after_config() {
function local_intellidata_before_session_start() {
local_intellidata_after_config();
}

23 changes: 13 additions & 10 deletions settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

use local_intellidata\helpers\SettingsHelper;
use local_intellidata\helpers\StorageHelper;
use local_intellidata\helpers\ParamsHelper;

defined('MOODLE_INTERNAL') || die;

Expand Down Expand Up @@ -387,16 +388,18 @@
);
$settings->add($setting);

$name = 'newtracking';
$setting = new admin_setting_configcheckbox(
$pluginname . '/' . $name,
get_string($name, $pluginname),
'',
SettingsHelper::get_defaut_config_value($name),
true,
false
);
$settings->add($setting);
if (ParamsHelper::get_release() >= 3.8) {
$name = 'newtracking';
$setting = new admin_setting_configcheckbox(
$pluginname . '/' . $name,
get_string($name, $pluginname),
'',
SettingsHelper::get_defaut_config_value($name),
true,
false
);
$settings->add($setting);
}

$name = 'divideexportbydatatype';
$setting = new admin_setting_configcheckbox(
Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2024031900;
$plugin->version = 2024032100;
$plugin->requires = 2017111300;
$plugin->release = '1.176.0';
$plugin->release = '1.182.0';
$plugin->maturity = MATURITY_STABLE;
$plugin->component = 'local_intellidata';

0 comments on commit abeed41

Please sign in to comment.