Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ws to the plugin #148

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 10 additions & 1 deletion categoryadd_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
*/
class report_customsql_addcategory_form extends moodleform {

// Form definition.
/**
* Define the form.
*/
public function definition() {
global $CFG, $DB;
$mform = $this->_form;
Expand All @@ -65,6 +67,13 @@ public function definition() {
$this->add_action_buttons(true, $strsubmit);
}

/**
* Validation.
*
* @param array $data Form data.
* @param array $files Form files.
* @return array Array of errors.
*/
public function validation($data, $files) {
global $DB;
$errors = parent::validation($data, $files);
Expand Down
19 changes: 19 additions & 0 deletions classes/event/query_deleted.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,39 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class query_deleted extends \core\event\base {

/**
* Event constructor.
*/
protected function init() {
$this->data['crud'] = 'd';
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'report_customsql_queries';
}

/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('query_deleted', 'report_customsql');
}

/**
* Returns description of the query deleted event.
*
* @return string
*/
public function get_description() {
return "User {$this->userid} has deleted the SQL query with id {$this->objectid}.";
}

/**
* Returns relevant URL.
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/report/customsql/index.php');
}
Expand Down
19 changes: 19 additions & 0 deletions classes/event/query_edited.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,39 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class query_edited extends \core\event\base {

/**
* Event constructor.
*/
protected function init() {
$this->data['crud'] = 'u';
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'report_customsql_queries';
}

/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('query_edited', 'report_customsql');
}

/**
* Returns description of the query edited event.
*
* @return string
*/
public function get_description() {
return "User {$this->userid} has edited the SQL query with id {$this->objectid}.";
}

/**
* Returns url to view the query.
*
* @return string
*/
public function get_url() {
return new \moodle_url('/report/customsql/view.php', ['id' => $this->objectid]);
}
Expand Down
19 changes: 19 additions & 0 deletions classes/event/query_viewed.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,39 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class query_viewed extends \core\event\base {

/**
* Event constructor.
*/
protected function init() {
$this->data['crud'] = 'r';
$this->data['edulevel'] = self::LEVEL_OTHER;
$this->data['objecttable'] = 'report_customsql_queries';
}

/**
* Returns localised general event name.
*
* @return string
*/
public static function get_name() {
return get_string('query_viewed', 'report_customsql');
}

/**
* Returns description of the query viewed event.
*
* @return string
*/
public function get_description() {
return "User {$this->userid} has viewed the SQL query with id {$this->objectid}.";
}

/**
* Returns relevant URL.
*
* @return \moodle_url
*/
public function get_url() {
return new \moodle_url('/report/customsql/view.php', ['id' => $this->objectid]);
}
Expand Down
147 changes: 147 additions & 0 deletions classes/external/create_query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?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/>.

namespace report_customsql\external;

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

global $CFG;
require_once($CFG->libdir . '/externallib.php');
require_once($CFG->dirroot . '/report/customsql/edit_form.php');

/**
* Web service to create new queries.
*
* @package report_customsql
* @author Oscar Nadjar <[email protected]>
* @copyright 2024 Moodle US
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class create_query extends \external_api {
/**
* Parameter declaration.
*
* @return \external_function_parameters Parameters
*/
public static function execute_parameters(): \external_function_parameters {
return new \external_function_parameters([
'displayname' => new \external_value(PARAM_ALPHANUMEXT, 'Short name of the query.', VALUE_REQUIRED),
'description' => new \external_value(PARAM_RAW, 'Description of the query.', VALUE_DEFAULT, ''),
'querysql' => new \external_value(PARAM_RAW, 'SQL query.', VALUE_REQUIRED),
'queryparams' => new \external_value(PARAM_RAW, 'Description of the query.', VALUE_DEFAULT, ''),
'querylimit' => new \external_value(PARAM_INT, 'Limit of the query.', VALUE_DEFAULT, 5000),
'capability' => new \external_value(PARAM_CAPABILITY, 'Capability to view the query.',
VALUE_DEFAULT, 'moodle/site:config'),
'runable' => new \external_value(PARAM_ALPHAEXT, 'manual, weekly, montly.', VALUE_DEFAULT, 'manual'),
'at' => new \external_value(PARAM_TEXT, 'Time of the execution.', VALUE_DEFAULT, ''),
'emailto' => new \external_value(PARAM_EMAIL, 'Email to send the report to.', VALUE_DEFAULT, ''),
'emailwhat' => new \external_value(PARAM_TEXT, 'What to send in the email.', VALUE_DEFAULT, ''),
'categoryid' => new \external_value(PARAM_INT, 'Category of the query.', VALUE_DEFAULT, 1),
'customdir' => new \external_value(PARAM_RAW, 'Custom directory of the query.', VALUE_DEFAULT, ''),
]);
}

/**
* Create a new query.
*
* @param string $displayname Short name of the query.
* @param string $description Description of the query.
* @param string $querysql SQL query.
* @param string $queryparams Description of the query.
* @param int $querylimit Limit of the query.
* @param string $capability Capability to view the query.
* @param string $runable manual, weekly, montly.
* @param string $at Time of the execution.
* @param string $emailto Email to send the report to.
* @param string $emailwhat What to send in the email.
* @param int $categoryid Category of the query.
* @param string $customdir Custom directory of the query.
*
* @return int id of the created query.
*/
public static function execute(
string $displayname,
string $description,
string $querysql,
string $queryparams,
int $querylimit,
string $capability,
string $runable,
string $at,
string $emailto,
string $emailwhat,
int $categoryid,
string $customdir
): array {
global $CFG, $DB, $USER;

// We need an associative array in order to use the validation functions.
$params = [
'displayname' => $displayname,
'description' => $description,
'querysql' => $querysql,
'queryparams' => $queryparams,
'querylimit' => $querylimit,
'capability' => $capability,
'runable' => $runable,
'at' => $at,
'emailto' => $emailto,
'emailwhat' => $emailwhat,
'categoryid' => $categoryid,
'customdir' => $customdir
];

// This will assign the validated values to the variables.
$formdata = self::validate_parameters(self::execute_parameters(), $params);

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

// Validate the data using the form class.
$form = new \report_customsql_edit_form();
$errors = $form->validation($formdata, []);

if (!empty($errors)) {
throw new \moodle_exception('error', 'report_customsql', '', $errors);
}

// We are ready to insert the query in the database.
$query = (object)$formdata;
$query->usermodified = $USER->id;
$query->timecreated = time();
$query->timemodified = time();
$query->id = $DB->insert_record('report_customsql_queries', $query);

if (empty($query->id)) {
throw new \moodle_exception('error', 'report_customsql', '', $errors);
}

return ['queryid' => $query->id];
}

/**
* Returns the id of the created query.
*
* @return \external_description Result type
*/
public static function execute_returns(): \external_description {
return new \external_single_structure([
'queryid' => new \external_value(PARAM_INT, 'id of the created query.'),
]);
}
}
86 changes: 86 additions & 0 deletions classes/external/delete_query.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?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/>.

namespace report_customsql\external;

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

global $CFG;
require_once($CFG->libdir . '/externallib.php');

/**
* Web service to delete a query.
*
* @package report_customsql
* @author Oscar Nadjar <[email protected]>
* @copyright 2024 Moodle US
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class delete_query extends \external_api {
/**
* Parameter declaration.
*
* @return \external_function_parameters Parameters
*/
public static function execute_parameters(): \external_function_parameters {
return new \external_function_parameters([
'queryid' => new \external_value(PARAM_INT, 'The id of the query', VALUE_REQUIRED),
]);
}

/**
* Delete a query.
*
* @param int $queryid The id of the query.
*
* @return array
*/
public static function execute(int $queryid): array {
global $CFG, $DB, $USER;

// This will assign the validated values to the variables.
$params = self::validate_parameters(self::execute_parameters(), ['queryid' => $queryid]);
$queryid = $params['queryid'];

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

// We checkout the queryid.
if (empty($DB->record_exists('report_customsql_queries', ['id' => $queryid]))) {
throw new \moodle_exception('error:invalidqueryid', 'report_customsql');
}

// We delete the query.
if (empty($DB->delete_records('report_customsql_queries', ['id' => $queryid]))) {
throw new \moodle_exception('error:cannotdeletequery', 'report_customsql');
}

return ['success' => true];
}

/**
* Returns true if the query was deleted.
*
* @return \external_description Result type
*/
public static function execute_returns(): \external_description {
return new \external_single_structure([
'success' => new \external_value(PARAM_BOOL, 'True if the query was deleted.'),
]);
}
}
Loading
Loading