Skip to content

Commit

Permalink
Merge pull request #31 from catalyst/issue28_29
Browse files Browse the repository at this point in the history
Add settings to control user ID for matomo to fix #28 and #29
  • Loading branch information
dmitriim authored Jun 30, 2020
2 parents 3f776b5 + b4f07ab commit 0c8dfa9
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 5 deletions.
55 changes: 55 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Upgrade script.
*
* @package tool_webanalytics
* @author Dmitrii Metelkin ([email protected])
* @copyright 2020 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

use \tool_webanalytics\records_manager;

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

/**
* Upgrade the plugin.
*
* @param int $oldversion The old version of the plugin
* @return bool
*/
function xmldb_tool_webanalytics_upgrade($oldversion) {
global $CFG, $DB;

if ($oldversion < 2020063001) {
// Apply new matomo settings for existing matomo tools.

$records = $DB->get_records(records_manager::TABLE_NAME, ['type' => 'matomo']);
foreach ($records as $record) {
$settings = unserialize($record->settings);
$settings['userid'] = 1;
$settings['usefield'] = 'id';
$record->settings = serialize($settings);
$DB->update_record(records_manager::TABLE_NAME, $record);
}

upgrade_plugin_savepoint(true, 2020063001, 'tool', 'webanalytics');
}

return true;
}
27 changes: 26 additions & 1 deletion tool/matomo/classes/tool/tool.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ public function get_tracking_code() {
$custompiwikjs = (isset($settings['piwikjsurl']) && !empty($settings['piwikjsurl']));
$template->piwikjsurl = $custompiwikjs ? $settings['piwikjsurl'] : $settings['siteurl'];
$template->imagetrack = $settings['imagetrack'];
$template->userid = $USER->id;

$template->userid = false;

if (!empty($settings['userid']) && !empty($settings['usefield']) && !empty($USER->{$settings['usefield']})) {
$template->userid = $USER->{$settings['usefield']};
}

$template->doctitle = "";

if (!empty($this->record->get_property('cleanurl'))) {
Expand Down Expand Up @@ -88,6 +94,21 @@ public function form_add_settings_elements(\MoodleQuickForm &$mform) {

$mform->addElement('checkbox', 'imagetrack', get_string('imagetrack', 'watool_matomo'));
$mform->addHelpButton('imagetrack', 'imagetrack', 'watool_matomo');

$mform->addElement('checkbox', 'userid', get_string('userid', 'watool_matomo'));
$mform->addHelpButton('userid', 'userid', 'watool_matomo');
$mform->setDefault('userid', 1);

$choices = [
'id' => 'id',
'username' => 'username',
];

$mform->addElement('select', 'usefield', get_string('usefield', 'watool_matomo'), $choices);
$mform->addHelpButton('usefield', 'usefield', 'watool_matomo');
$mform->setType('usefield', PARAM_TEXT);

$mform->disabledIf('usefield', 'userid');
}

/**
Expand Down Expand Up @@ -142,6 +163,8 @@ public function form_build_settings(\stdClass $data) {
$settings['siteurl'] = isset($data->siteurl) ? $data->siteurl : '';
$settings['piwikjsurl'] = isset($data->piwikjsurl) ? $data->piwikjsurl : '';
$settings['imagetrack'] = isset($data->imagetrack) ? $data->imagetrack : 0;
$settings['userid'] = isset($data->userid) ? $data->userid : 0;
$settings['usefield'] = isset($data->usefield) ? $data->usefield : 'id';

return $settings;
}
Expand All @@ -158,5 +181,7 @@ public function form_set_data(\stdClass &$data) {
$data->siteurl = isset($data->settings['siteurl']) ? $data->settings['siteurl'] : '';
$data->piwikjsurl = isset($data->settings['piwikjsurl']) ? $data->settings['piwikjsurl'] : '';
$data->imagetrack = isset($data->settings['imagetrack']) ? $data->settings['imagetrack'] : 0;
$data->userid = isset($data->settings['userid']) ? $data->settings['userid'] : 1;
$data->usefield = isset($data->settings['usefield']) ? $data->settings['usefield'] : 'id';
}
}
4 changes: 4 additions & 0 deletions tool/matomo/lang/en/watool_matomo.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@
$string['error:siteurltrailingslash'] = 'Please provide URL without a trailing slash';
$string['privacy:metadata:watool_matomo'] = 'In order to track user activity, user data needs to be sent with that service.';
$string['privacy:metadata:watool_matomo:userid'] = 'The userid is sent from Moodle to personalise user activity.';
$string['userid'] = 'Track User ID';
$string['userid_help'] = 'If enabled userId parameter will be sent for tracking';
$string['usefield'] = 'User ID field';
$string['usefield_help'] = 'Select a user field to be used as User ID when sending for tracking';
4 changes: 2 additions & 2 deletions tool/matomo/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

defined('MOODLE_INTERNAL') || die;

$plugin->version = 2019080800; // The current plugin version (Date: YYYYMMDDXX).
$plugin->release = 2019080800; // Same as version
$plugin->version = 2020063001; // The current plugin version (Date: YYYYMMDDXX).
$plugin->release = 2020063001; // Same as version
$plugin->requires = 2017051500; // Requires Moodle 3.3 or later.
$plugin->component = "watool_matomo";
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

defined('MOODLE_INTERNAL') || die;

$plugin->version = 2020050800; // The current plugin version (Date: YYYYMMDDXX).
$plugin->release = 2020050800; // Same as version
$plugin->version = 2020063001; // The current plugin version (Date: YYYYMMDDXX).
$plugin->release = 2020063001; // Same as version
$plugin->requires = 2017051500; // Requires Moodle 3.3 or later.
$plugin->component = "tool_webanalytics";
$plugin->maturity = MATURITY_STABLE;

0 comments on commit 0c8dfa9

Please sign in to comment.