Skip to content

Commit

Permalink
DEF-2237: Adding WS
Browse files Browse the repository at this point in the history
  • Loading branch information
ojnadjarm committed Oct 2, 2024
1 parent ce26f60 commit b69080b
Show file tree
Hide file tree
Showing 35 changed files with 1,888 additions and 42 deletions.
99 changes: 99 additions & 0 deletions bitbucket-pipelines.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
---
image:
name: '271411534863.dkr.ecr.us-east-2.amazonaws.com/moodleusdev:latest'
aws:
access-key: "$AWS_ACCESS_KEY"
secret-key: "$AWS_SECRET_KEY"
definitions:
installscript: &baseInst
echo "Checking out Moodle\n" ;
moodle-plugin-ci install --branch='MOODLE_401_STABLE' ;
steps:
- step: &base
name: 'Moodle 4.1, PHP 7.4 and MariaDB 10.6'
caches:
- npm
- composer
- docker
services:
- mariadb
- docker
services:
mariadb:
image: mariadb:10.6
variables:
MARIADB_ALLOW_EMPTY_ROOT_PASSWORD: 'yes'
caches:
npm: $HOME/.npm
pipelines:
default:
- parallel:
steps:
- step:
<<: *base
name: PHP Lint
script:
- *baseInst
- moodle-plugin-ci phplint
- step:
<<: *base
name: PHP Copy detector
script:
- *baseInst
- moodle-plugin-ci phpcpd
- step:
<<: *base
name: PHP Mess detector
script:
- *baseInst
- moodle-plugin-ci phpmd
- step:
<<: *base
name: PHP Code checker
script:
- *baseInst
- moodle-plugin-ci codechecker
- step:
<<: *base
name: PHP Validate
script:
- *baseInst
- moodle-plugin-ci validate
- step:
<<: *base
name: PHP Savepoints
script:
- *baseInst
- moodle-plugin-ci savepoints
- step:
<<: *base
name: PHP Mustache
script:
- *baseInst
- moodle-plugin-ci mustache
- step:
<<: *base
name: PHP Grunt
script:
- *baseInst
- moodle-plugin-ci grunt
- step:
<<: *base
name: PHP Doc
script:
- *baseInst
- moodle-plugin-ci phpdoc
- step:
<<: *base
name: PHPUnit
script:
- *baseInst
- moodle-plugin-ci phpunit
# Commented out Behat because there are no Behat tests in this project yet.
# - step:
# <<: *base
# name: Behat
# script:
# - *baseInst
# - preset-start-behat
# - moodle-plugin-ci behat
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.'),
]);
}
}
Loading

0 comments on commit b69080b

Please sign in to comment.