Skip to content

Commit

Permalink
Squashed 'report/customsql/' changes from 1f02742..48d1592
Browse files Browse the repository at this point in the history
48d1592 Bump version number for the 3.2 release
614cab2 Fix bugs in the last commit.
6b64b24 Bump the version for the 3.1 release.
554205d Merge pull request moodle#21 from brendanheywood/save2disk
5d35b97 Fix event classes get_url
8322bce Added export to filesystem option

git-subtree-dir: report/customsql
git-subtree-split: 48d1592
  • Loading branch information
kwiliarty committed Sep 13, 2016
1 parent e4267ba commit c242550
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 10 deletions.
2 changes: 1 addition & 1 deletion classes/event/query_deleted.php
Expand Up @@ -48,7 +48,7 @@ public function get_description() {
}

public function get_url() {
return new \moodle_url('/report/customsql/index.php', array('id' => $this->context->instanceid));
return new \moodle_url('/report/customsql/index.php');
}

public function get_legacy_logdata() {
Expand Down
2 changes: 1 addition & 1 deletion classes/event/query_edited.php
Expand Up @@ -48,7 +48,7 @@ public function get_description() {
}

public function get_url() {
return new \moodle_url('/report/customsql/view.php', array('id' => $this->context->instanceid));
return new \moodle_url('/report/customsql/view.php', array('id' => $this->objectid));
}

public function get_legacy_logdata() {
Expand Down
2 changes: 1 addition & 1 deletion classes/event/query_viewed.php
Expand Up @@ -48,7 +48,7 @@ public function get_description() {
}

public function get_url() {
return new \moodle_url('/report/customsql/view.php', array('id' => $this->context->instanceid));
return new \moodle_url('/report/customsql/view.php', array('id' => $this->objectid));
}

public function get_legacy_logdata() {
Expand Down
1 change: 1 addition & 0 deletions db/install.xml
Expand Up @@ -22,6 +22,7 @@
<FIELD NAME="emailto" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="A comma separated list of usernames"/>
<FIELD NAME="emailwhat" TYPE="char" LENGTH="64" NOTNULL="false" SEQUENCE="false" COMMENT="A list of email options in a select menu"/>
<FIELD NAME="categoryid" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="The category ID from report_customsql_categories table."/>
<FIELD NAME="customdir" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
Expand Down
13 changes: 13 additions & 0 deletions db/upgrade.php
Expand Up @@ -160,5 +160,18 @@ function xmldb_report_customsql_upgrade($oldversion) {
upgrade_plugin_savepoint(true, 2015062900, 'report', 'customsql');
}

if ($oldversion < 2016011800) {

// Define field customdir to be added to report_customsql_queries.
$table = new xmldb_table('report_customsql_queries');
$field = new xmldb_field('customdir', XMLDB_TYPE_CHAR, '255', null, null, null, null, 'categoryid');

if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

upgrade_plugin_savepoint(true, 2016011800, 'report', 'customsql');
}

return true;
}
1 change: 1 addition & 0 deletions edit.php
Expand Up @@ -74,6 +74,7 @@
$newreport->at = '';
$newreport->emailto = '';
$newreport->emailwhat = '';
$newreport->customdir = '';
}
if ($newreport->runable == 'manual' || empty($newreport->singlerow)) {
$newreport->singlerow = 0;
Expand Down
41 changes: 40 additions & 1 deletion edit_form.php
Expand Up @@ -97,6 +97,11 @@ public function definition() {
$mform->addElement('checkbox', 'singlerow', get_string('typeofresult', 'report_customsql'),
get_string('onerow', 'report_customsql'));

$mform->addElement('text', 'customdir', get_string('customdir', 'report_customsql'), 'size = 70');
$mform->setType('customdir', PARAM_PATH);
$mform->disabledIf('customdir', 'runable', 'eq', 'manual');
$mform->addHelpButton('customdir', 'customdir', 'report_customsql');

$mform->addElement('text', 'emailto', get_string('emailto', 'report_customsql'), 'size = 70');
$mform->addElement('select', 'emailwhat', get_string('emailwhat', 'report_customsql'),
report_customsql_email_options());
Expand Down Expand Up @@ -202,7 +207,41 @@ public function validation($data, $files) {
$errors['querylimit'] = get_string('querylimitrange', 'report_customsql', REPORT_CUSTOMSQL_MAX_RECORDS);
}

if (!empty($data['customdir'])) {
$path = $data['customdir'];

// The path either needs to be a writable directory ...
if (is_dir($path) ) {
if (!is_writable($path)) {
$errors['customdir'] = get_string('customdirnotwritable', 'report_customsql', $path);
}

} else if (substr($path, -1) == DIRECTORY_SEPARATOR) {
// ... and it must exist...
$errors['customdir'] = get_string('customdirmustexist', 'report_customsql', $path);

} else {

// ... or be a path to a writable file, or a new file in a writable directory.
$dir = dirname($path);

if (!is_dir($dir)) {
$errors['customdir'] = get_string('customdirnotadirectory', 'report_customsql', $dir);
} else {

if (file_exists($path)) {
if (!is_writable($path)) {
$errors['customdir'] = get_string('filenotwritable', 'report_customsql', $path);
}
} else {
if (!is_writable($dir)) {
$errors['customdir'] = get_string('customdirmustexist', 'report_customsql', $dir);
}
}
}
}
}

return $errors;
}

}
5 changes: 5 additions & 0 deletions lang/en/report_customsql.php
Expand Up @@ -40,6 +40,11 @@
$string['categorynamex'] = 'Category name: {$a}';
$string['changetheparameters'] = 'Change the parameters';
$string['crontask'] = 'Ad-hoc database queries: run scheduled reports task';
$string['customdir'] = 'Export csv report to path / directory';
$string['customdir_help'] = 'Files are exported in the CSV format to the file path specified. If a directory is specified the filename format will be reportid-timecreated.csv.';
$string['customdirmustexist'] = 'The directory "{$a}" does not exist.';
$string['customdirnotadirectory'] = 'The path "{$a}" is not a directory.';
$string['customdirnotwritable'] = 'The directory "{$a}" is not writable.';
$string['customsql:definequeries'] = 'Define custom queries';
$string['customsql:managecategories'] = 'Define custom categories';
$string['customsql:view'] = 'View custom queries report';
Expand Down
49 changes: 46 additions & 3 deletions locallib.php
Expand Up @@ -120,13 +120,23 @@ function report_customsql_generate_csv($report, $timenow) {
$DB->update_record('report_customsql_queries', $updaterecord);

// Report is runable daily, weekly or monthly.
if (($report->runable != 'manual') && !empty($report->emailto)) {
if ($report->runable != 'manual') {
if ($csvfilenames) {
foreach ($csvfilenames as $csvfilename) {
report_customsql_email_report($report, $csvfilename);
if (!empty($report->emailto)) {
report_customsql_email_report($report, $csvfilename);
}
if (!empty($report->customdir)) {
report_customsql_copy_csv_to_customdir($report, $timenow, $csvfilename);
}
}
} else { // If there is no data.
report_customsql_email_report($report);
if (!empty($report->emailto)) {
report_customsql_email_report($report);
}
if (!empty($report->customdir)) {
report_customsql_copy_csv_to_customdir($report);
}
}
}
return $csvtimestamp;
Expand Down Expand Up @@ -648,3 +658,36 @@ function report_customsql_category_options() {
global $DB;
return $DB->get_records_menu('report_customsql_categories', null, 'name ASC', 'id, name');
}

/**
* Copies a csv file to an optional custom directory or file path.
*
* @param object $report
* @param integer $timenow
* @param string $csvfilename
*/
function report_customsql_copy_csv_to_customdir($report, $timenow, $csvfilename = null) {

// If the filename is empty then there was no data so we can't export a
// new file, but if we are saving over the same file then we should delete
// the existing file or it will have stale data in it.
if (empty($csvfilename)) {
$filepath = $report->customdir;
if (!is_dir($filepath)) {
file_put_contents($filepath, '');
mtrace("No data so resetting $filepath");
}
return;
}

$filename = $report->id . '-' . basename($csvfilename);
if (is_dir($report->customdir)) {
$filepath = realpath($report->customdir) . DIRECTORY_SEPARATOR . $filename;
} else {
$filepath = $report->customdir;
}

copy($csvfilename, $filepath);
mtrace("Exported $csvfilename to $filepath");
}

6 changes: 3 additions & 3 deletions version.php
Expand Up @@ -24,10 +24,10 @@

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

$plugin->version = 2015071000;
$plugin->requires = 2014041100;
$plugin->version = 2016080300;
$plugin->requires = 2015111600;
$plugin->component = 'report_customsql';
$plugin->maturity = MATURITY_STABLE;
$plugin->release = '3.0 for Moodle 2.7+';
$plugin->release = '3.2 for Moodle 3.0+';

$plugin->outestssufficient = true;

0 comments on commit c242550

Please sign in to comment.