Skip to content

Commit

Permalink
Fixed CodeSniffer
Browse files Browse the repository at this point in the history
  • Loading branch information
Skjodt committed Nov 15, 2024
1 parent d15d835 commit 0b14abf
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 346 deletions.
174 changes: 98 additions & 76 deletions server/moodle/mod/homework/classes/external/delete_file.php
Original file line number Diff line number Diff line change
@@ -1,76 +1,98 @@
<?php
namespace mod_homework\external;

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

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

use core\exception\moodle_exception;
use external_api;
use external_function_parameters;
use external_value;
use context_system;

/**
* Class to handle file deletion for mod_homework.
*/
class delete_file extends external_api {

/**
* Define the parameters for delete_file.
*
* @return external_function_parameters
*/
public static function execute_parameters() {
return new external_function_parameters(
array(
'id' => new external_value(PARAM_INT, 'The ID of the homework to update'),
'file_id' => new external_value(PARAM_INT, 'The ID of the file to delete'),
)
);
}

/**
* Deletes the specified file.
*
* @param int $id The ID of the homework to update.
* @param int $file_id The ID of the file to delete.
* @return bool True if the file was successfully deleted, false otherwise.
*/
public static function execute($id, $file_id) {
global $DB;

// Validate parameters.
$params = self::validate_parameters(self::execute_parameters(), array(
'id' => $id,
'file_id' => $file_id,
));

// Ensure the user is logged in and validate context.
require_login();
$context = context_system::instance();
self::validate_context($context);

// Include the library function to delete the file.
require_once(__DIR__ . '/../../lib.php');
$success = mod_homework_delete_file(
$params['id'],
$params['file_id']
);

if (!$success) {
throw new moodle_exception('deleteerror', 'mod_homework', '', null, 'File deletion failed.');
}

return true;
}

/**
* Define the return type for delete_file.
*
* @return external_value
*/
public static function execute_returns() {
return new external_value(PARAM_BOOL, 'True if file was deleted successfully');
}
}
<?php
// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.

/**
* homework/classes/external/delete_file.php
*
* @package mod_homework
* @copyright 2024, cs-24-sw-5-01 <cs-24-sw-5-01@student.aau.dk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace mod_homework\external;

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

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

use core\exception\moodle_exception;
use external_api;
use external_function_parameters;
use external_value;
use context_system;

/**
* Class to handle file deletion for mod_homework.
*/
class delete_file extends external_api {
/**
* Define the parameters for delete_file.
*
* @return external_function_parameters
*/
public static function execute_parameters() {
return new external_function_parameters(
[
'id' => new external_value(PARAM_INT, 'The ID of the homework to update'),
'fileid' => new external_value(PARAM_INT, 'The ID of the file to delete'),
]
);
}

/**
* Deletes the specified file.
*
* @param int $id The ID of the homework to update.
* @param int $fileid The ID of the file to delete.
* @return bool True if the file was successfully deleted, false otherwise.
*/
public static function execute($id, $fileid) {
global $DB;

// Validate parameters.
$params = self::validate_parameters(self::execute_parameters(), [
'id' => $id,
'file_id' => $fileid,
]);

// Ensure the user is logged in and validate context.
require_login();
$context = context_system::instance();
self::validate_context($context);

// Include the library function to delete the file.
require_once(__DIR__ . '/../../lib.php');
$success = mod_homework_delete_file(
$params['id'],
$params['file_id']
);

if (!$success) {
throw new moodle_exception('deleteerror', 'mod_homework', '', null, 'File deletion failed.');
}

return true;
}

/**
* Define the return type for delete_file.
*
* @return external_value
*/
public static function execute_returns() {
return new external_value(PARAM_BOOL, 'True if file was deleted successfully');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,17 @@ public static function execute_parameters() {
* @return string[]
* @throws \dml_exception
*/
public static function execute($id, $inputfield, $homeworkid, $link = null, $startpage = null, $endpage = null, $starttime = null, $endtime = null, $fileid = null) {
public static function execute(
$id,
$inputfield,
$homeworkid,
$link = null,
$startpage = null,
$endpage = null,
$starttime = null,
$endtime = null,
$fileid = null
) {
global $DB, $USER;

$record = new \stdClass();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,16 @@ public static function execute_parameters() {
* @return string[]
* @throws \dml_exception
*/
public static function execute($inputfield, $homeworkid, $link = null, $startpage = null, $endpage = null, $starttime = null, $endtime = null, $fileid = null) {
public static function execute(
$inputfield,
$homeworkid,
$link = null,
$startpage = null,
$endpage = null,
$starttime = null,
$endtime = null,
$fileid = null
) {
global $DB, $USER;

$record = new \stdClass();
Expand Down
4 changes: 0 additions & 4 deletions server/moodle/mod/homework/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,3 @@
'enabled' => 1,
],
];




53 changes: 42 additions & 11 deletions server/moodle/mod/homework/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
foreach ($homeworkmaterials as $materials) :
// Generate the preview URL for the file if it exists
// Generate the preview URL for the file if it exists.
if ($materials->file_id !== null) {
// Retrieve additional metadata for generating the URL
// Retrieve additional metadata for generating the URL.
$file = $DB->get_record('files', ['id' => $materials->file_id]);
if ($file) {
// Generate the preview URL using Moodle's pluginfile.php
// Generate the preview URL using Moodle's pluginfile.php.
$previewurl = moodle_url::make_pluginfile_url(
$file->contextid,
$file->component,
Expand All @@ -103,16 +103,35 @@
}
?>

<div class="material" style="border: 1px solid #ccc;padding: 16px;border-radius: 8px;box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);background-color: #f9f9f9;">
<div
class="material"
style="
border: 1px solid #ccc;
padding: 16px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
background-color: #f9f9f9;
"
>
<p><?= htmlspecialchars($materials->description) ?></p>
<?php if ($materials->startpage !== null && $materials->endpage !== null) : ?>
<p><?= "Page: " . htmlspecialchars($materials->startpage) . " - " . htmlspecialchars($materials->endpage) ?></p>
<p><?= "Page: " .
htmlspecialchars($materials->startpage) . " - " .
htmlspecialchars($materials->endpage) ?>
</p>
<?php endif; ?>
<?php if ($materials->link !== null) : ?>
<p><?= "Link: " ?><a href="<?= htmlspecialchars($materials->link) ?>"><?= htmlspecialchars($materials->link) ?></a></p>
<p><?= "Link: " ?><a href="<?=
htmlspecialchars($materials->link) ?>">
<?= htmlspecialchars($materials->link) ?>
</a>
</p>
<?php endif; ?>
<?php if ($materials->starttime !== null && $materials->endtime !== null) : ?>
<p><?= "Time (seconds): " . htmlspecialchars($materials->starttime) . " - " . htmlspecialchars($materials->endtime) ?></p>
<p><?= "Time (seconds): " .
htmlspecialchars($materials->starttime) . " - " .
htmlspecialchars($materials->endtime) ?>
</p>
<?php endif; ?>

<?php if ($materials->file_id !== null && isset($previewurl)) : ?>
Expand All @@ -124,7 +143,13 @@
</video>
<?php else : ?>
<!-- Provide a link for non-video files -->
<p><?= "File: " ?><a href="<?= $previewurl ?>" target="_blank"><?= htmlspecialchars($file->filename) ?></a> (Preview)</p>
<p><?= "File: " ?>
<a
href="<?= $previewurl ?>"
target="_blank">
<?= htmlspecialchars($file->filename) ?>
</a> (Preview)
</p>
<?php endif; ?>
<?php endif; ?>

Expand Down Expand Up @@ -168,10 +193,16 @@
];
}, $homeworkmaterials);

$PAGE->requires->js_call_amd('mod_homework/homeworkchooseredit', 'init', [$cm->id,
get_string('homeworkchooser', 'mod_homework'), $instance->id, $homeworkmaterialids]);
$PAGE->requires->js_call_amd('mod_homework/homeworkchooseredit', 'init', [
$cm->id,
get_string('homeworkchooser', 'mod_homework'),
$instance->id, $homeworkmaterialids,
]);

$PAGE->requires->js_call_amd('mod_homework/homeworkchooserdelete', 'init', [$cm->id, $homeworkmaterialids]);
$PAGE->requires->js_call_amd('mod_homework/homeworkchooserdelete', 'init', [
$cm->id,
$homeworkmaterialids,
]);

// Output the footer - REQUIRED.
echo $OUTPUT->footer();
20 changes: 9 additions & 11 deletions server/moodle/mod/homework/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,18 @@ function mod_homework_pluginfile(
* @param int $file_id The file ID to delete.
* @return bool True if deletion was successful, false otherwise.
*/
function mod_homework_delete_file($id, $file_id) {
function mod_homework_delete_file($id, $fileid) {
global $DB;
$fs = get_file_storage();

// Get the file record
$file = $DB->get_record('files', ['id' => $file_id]);
// Get the file record.
$file = $DB->get_record('files', ['id' => $fileid]);
if (!$file) {
return false;
}

// Load the file from file storage and delete it
$stored_file = $fs->get_file(
// Load the file from file storage and delete it.
$storedfile = $fs->get_file(
$file->contextid,
$file->component,
$file->filearea,
Expand All @@ -173,20 +173,18 @@ function mod_homework_delete_file($id, $file_id) {
$file->filename
);

if ($stored_file) {
// Delete the file record from the database
$stored_file->delete();
if ($storedfile) {
// Delete the file record from the database.
$storedfile->delete();

$record = new \stdClass();

$record->id = $id;
$record->file_id = null;

// Update the record from the database
// Update the record from the database.
$DB->update_record('homework_materials', $record);
}

return true;
}


Loading

0 comments on commit 0b14abf

Please sign in to comment.