Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions addcategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@
$queryparams['categoryid'] = $id;
$isadding = false;
// Editing an existing category.
$category = $DB->get_record('report_customsql_categories',
['id' => $id], '*', MUST_EXIST);
$category = $DB->get_record(
'report_customsql_categories',
['id' => $id],
'*',
MUST_EXIST
);
} else {
$queryparams['categoryid'] = null;
$isadding = true;
Expand Down
10 changes: 7 additions & 3 deletions categoryadd_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class report_customsql_addcategory_form extends moodleform {

#[\Override]
public function definition() {
global $CFG, $DB;
Expand Down Expand Up @@ -74,8 +73,13 @@ public function validation($data, $files) {
if (!isset($data['id'])) {
$data['id'] = 0;// Ensure id to check against.
}
if ($DB->get_record_select('report_customsql_categories',
'name = ? AND id != ?', [$data['name'], $data['id']])) {
if (
$DB->get_record_select(
'report_customsql_categories',
'name = ? AND id != ?',
[$data['name'], $data['id']]
)
) {
$errors['name'] = get_string('categoryexists', 'report_customsql');
}
}
Expand Down
22 changes: 15 additions & 7 deletions categorydelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@
$id = required_param('id', PARAM_INT);

// Start the page.
admin_externalpage_setup('report_customsql', '', ['id' => $id],
'/report/customsql/categorydelete.php');
admin_externalpage_setup(
'report_customsql',
'',
['id' => $id],
'/report/customsql/categorydelete.php'
);
$context = context_system::instance();
require_capability('report/customsql:managecategories', $context);

Expand All @@ -57,9 +61,13 @@

echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('deletecategoryareyousure', 'report_customsql'));
echo html_writer::tag('p', get_string('categorynamex', 'report_customsql', $category->name ));
echo $OUTPUT->confirm(get_string('deletecategoryyesno', 'report_customsql'),
new single_button(report_customsql_url('categorydelete.php',
['id' => $id, 'confirm' => 1, 'sesskey' => sesskey()]), get_string('yes')),
new single_button(report_customsql_url('index.php'), get_string('no')));
echo html_writer::tag('p', get_string('categorynamex', 'report_customsql', $category->name));
echo $OUTPUT->confirm(
get_string('deletecategoryyesno', 'report_customsql'),
new single_button(report_customsql_url(
'categorydelete.php',
['id' => $id, 'confirm' => 1, 'sesskey' => sesskey()]
), get_string('yes')),
new single_button(report_customsql_url('index.php'), get_string('no'))
);
echo $OUTPUT->footer();
1 change: 0 additions & 1 deletion classes/event/query_deleted.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class query_deleted extends \core\event\base {

#[\Override]
protected function init() {
$this->data['crud'] = 'd';
Expand Down
1 change: 0 additions & 1 deletion classes/event/query_edited.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class query_edited extends \core\event\base {

#[\Override]
protected function init() {
$this->data['crud'] = 'u';
Expand Down
1 change: 0 additions & 1 deletion classes/event/query_viewed.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class query_viewed extends \core\event\base {

#[\Override]
protected function init() {
$this->data['crud'] = 'r';
Expand Down
105 changes: 105 additions & 0 deletions classes/external.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?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/>.

/**
* Class of plugins external functions.
*
* @package report_customsql
* @copyright 2018 Andre Scherl, ISB Bayern
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();

global $CFG;

require_once("$CFG->libdir/externallib.php");
require_once("$CFG->dirroot/report/customsql/locallib.php");

/**
* External API for report_customsql webservices.
*
* @package report_customsql
* @copyright 2018 Andre Scherl, ISB Bayern
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class report_customsql_external extends core_external\external_api {
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since Moodle 2.9
*/
public static function get_simple_value_parameters() {

return new external_function_parameters([
'queryname' => new external_value(PARAM_TEXT, 'Name of the query.'),
]);
}

/**
* Execute the (in admin panel) predefined query that returns one simple value.
*
* @param string $queryname
* @return string
*/
public static function get_simple_value($queryname) {
global $DB, $CFG;

// Validate parameters.
$params = self::validate_parameters(self::get_simple_value_parameters(), ['queryname' => $queryname]);
$queryname = $params['queryname'];

// Validate context.
$context = \context_system::instance();
self::validate_context($context);
require_capability('report/customsql:view', $context);

// Get the report and its settings.
$report = $DB->get_record('report_customsql_queries', ['displayname' => $queryname]);
if (!$report) {
throw new \moodle_exception('invalidreportid', 'report_customsql');
}

// Check report-specific capability.
if (!empty($report->capability)) {
require_capability($report->capability, $context);
}

// Prepare and execute the query.
$sql = report_customsql_prepare_sql($report, time());
$sql = preg_replace('/\bprefix_(?=\w+)/i', $CFG->prefix, $sql);
$queryparams = !empty($report->queryparams) ? json_decode($report->queryparams, true) : [];
if (!is_array($queryparams)) {
// Fallback for legacy serialized data.
$queryparams = !empty($report->queryparams) ? unserialize($report->queryparams) : [];
}
$querylimit = !empty($report->querylimit) ? $report->querylimit : REPORT_CUSTOMSQL_MAX_RECORDS;
$result = $DB->get_field_sql($sql, $queryparams);

return $result;
}

/**
* Returns description of method result value
*
* @return external_description
* @since Moodle 2.9
*/
public static function get_simple_value_returns() {

return new external_value(PARAM_TEXT, 'Value parsed as text.');
}
}
15 changes: 10 additions & 5 deletions classes/external/get_users.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,21 @@ public static function execute_parameters(): external_function_parameters {
public static function execute(string $query, string $capability): array {
global $CFG, $DB;

[$query, $capability] = array_values(self::validate_parameters(self::execute_parameters(),
['query' => $query, 'capability' => $capability]));
[$query, $capability] = array_values(self::validate_parameters(
self::execute_parameters(),
['query' => $query, 'capability' => $capability]
));

$context = \context_system::instance();
self::validate_context($context);
require_capability('report/customsql:definequeries', $context);

if (class_exists('\core_user\fields')) {
$extrafields = \core_user\fields::for_identity($context, false)->get_required_fields();
$fields = \core_user\fields::for_identity($context,
false)->with_userpic()->get_sql('u', false, '', '', false)->selects;
$fields = \core_user\fields::for_identity(
$context,
false
)->with_userpic()->get_sql('u', false, '', '', false)->selects;
} else {
$extrafields = get_extra_user_fields($context);
$fields = \user_picture::fields('u', $extrafields);
Expand Down Expand Up @@ -148,6 +152,7 @@ public static function execute_returns(): external_description {
'identity' => new external_value(PARAM_RAW, 'Additional user identifying info.'),
'hasidentity' => new external_value(PARAM_BOOL, 'Whether identity is non-blank.'),
'profileimageurlsmall' => new external_value(PARAM_RAW, 'URL of the user profile image.'),
]));
])
);
}
}
4 changes: 2 additions & 2 deletions classes/local/category.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function load_queries_data(array $queries): void {
* @return \stdClass[] All queries of type.
*/
public static function get_reports_of_a_particular_runtype(array $queries, string $type) {
return array_filter($queries, function($query) use ($type) {
return array_filter($queries, function ($query) use ($type) {
return $query->runable == $type;
}, ARRAY_FILTER_USE_BOTH);
}
Expand All @@ -91,7 +91,7 @@ public static function get_reports_of_a_particular_runtype(array $queries, strin
* @return \stdClass[] queries the current user is allowed to see.
*/
public static function filter_reports_by_capability(array $queries) {
return array_filter($queries, function($query) {
return array_filter($queries, function ($query) {
return has_capability($query->capability ?? 'moodle/site:config', \context_system::instance());
}, ARRAY_FILTER_USE_BOTH);
}
Expand Down
Loading