moodle-mod_htmlcert/classes/external.php

89 lines
3 KiB
PHP

<?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/>.
/**
* This is the external API for this tool.
*
* @package mod_htmlcert
* @copyright 2016 Mark Nelson <markn@moodle.com>, 2021 Klaus-Uwe Mitterer <kumitterer@kumi.systems>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace mod_htmlcert;
defined('MOODLE_INTERNAL') || die();
require_once("$CFG->libdir/externallib.php");
/**
* This is the external API for this tool.
*
* @copyright 2016 Mark Nelson <markn@moodle.com>, 2021 Klaus-Uwe Mitterer <kumitterer@kumi.systems>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class external extends \external_api {
/**
* Returns the delete_issue() parameters.
*
* @return \external_function_parameters
*/
public static function delete_issue_parameters() {
return new \external_function_parameters(
array(
'certificateid' => new \external_value(PARAM_INT, 'The certificate id'),
'issueid' => new \external_value(PARAM_INT, 'The issue id'),
)
);
}
/**
* Handles deleting a htmlcert issue.
*
* @param int $certificateid The certificate id.
* @param int $issueid The issue id.
* @return bool
*/
public static function delete_issue($certificateid, $issueid) {
global $DB;
$params = [
'certificateid' => $certificateid,
'issueid' => $issueid
];
self::validate_parameters(self::delete_issue_parameters(), $params);
$certificate = $DB->get_record('htmlcert', ['id' => $certificateid], '*', MUST_EXIST);
$issue = $DB->get_record('htmlcert_issues', ['id' => $issueid, 'htmlcertid' => $certificateid], '*', MUST_EXIST);
$cm = get_coursemodule_from_instance('htmlcert', $certificate->id, 0, false, MUST_EXIST);
// Make sure the user has the required capabilities.
$context = \context_module::instance($cm->id);
self::validate_context($context);
require_capability('mod/htmlcert:manage', $context);
// Delete the issue.
return $DB->delete_records('htmlcert_issues', ['id' => $issue->id]);
}
/**
* Returns the delete_issue result value.
*
* @return \external_value
*/
public static function delete_issue_returns() {
return new \external_value(PARAM_BOOL, 'True if successful, false otherwise');
}
}