Skip to content

Commit

Permalink
Merge pull request #37 from AAU-P5-Moodle/19-homework-student-feature…
Browse files Browse the repository at this point in the history
…-show-all-the-data-from-the-database-in-the-preparation-feed-block

19 homework student feature show all the data from the database in the preparation feed block
  • Loading branch information
mikk771h authored Nov 11, 2024
2 parents ea520b6 + 0020bb0 commit 18394f5
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 14 deletions.
2 changes: 1 addition & 1 deletion MoodleSQL.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- --------------------------------------------------------
mdl_user-- --------------------------------------------------------
-- Host: 127.0.0.1
-- Server version: 11.5.2-MariaDB - mariadb.org binary distribution
-- Server OS: Win64
Expand Down
39 changes: 32 additions & 7 deletions server/moodle/blocks/homework/block_homework.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,46 @@ public function init() {

/**
* Retrieves and prepares the content to be displayed by the block
*
* @return stdClass|null
*/
public function get_content() {

global $OUTPUT, $DB, $value, $USER;
global $OUTPUT, $DB, $USER;

// Get current time.
$currenttime = time();

// Fetch courses user is enrolled in.
$usercourses = enrol_get_users_courses($USER->id);

// Extract course IDs.
$courseids = array_map(function($course) {
return $course->id;
}, $usercourses);


// Create a string of ? placeholders for each found course_id, seperated by commas.
$placeholders = implode(',', array_fill(0, count($courseids), '?'));

// Merge parameters.
$parameters = array_merge([$currenttime], $courseids);

// Construct WHERE condition for select.
$select = "duedate > ? AND course IN ($placeholders)";

// Fetch homeworks using get_records_select.
$homeworks = $DB->get_records_select('homework', $select, $parameters);


$homeworks = $DB->get_records('homework');
$data = [];

if ($this->content !== null) {
return $this->content;
}

$this->content = new stdClass();

// If the current page is a course then remove unrelated homework.
if ($this->page->pagetype == 'course-view-topics') {
$homeworks = $this->filter_homework_content($this->page->url, $homeworks);
Expand All @@ -70,7 +97,7 @@ public function get_content() {
$tmp = [];
$tmp['id'] = $homework->id;
$tmp['name'] = $homework->name;
$tmp['duedate'] = $homework->duedate;
$tmp['duedate'] = date('d-m-Y', $homework->duedate);
$tmp['intro'] = strip_tags($homework->intro);
$tmp['courseTitle'] = $DB->get_field('course', 'fullname', ['id' => $homework->course]);

Expand Down Expand Up @@ -109,7 +136,8 @@ public function get_content() {
$filearea,
$itemid,
$filepath,
$filename
$filename,
false
);

// Get appropriate icon for file type.
Expand All @@ -135,8 +163,6 @@ public function get_content() {

// Render the content using a template and pass the homework data to it.
$this->content->text = $OUTPUT->render_from_template('block_homework/data', ['data' => $data]);


// Include JavaScript functionality for scrolling behavior in the block.
$this->page->requires->js_call_amd('block_homework/scroll', 'init');
$this->page->requires->js_call_amd('block_homework/sort', 'init');
Expand All @@ -147,7 +173,6 @@ public function get_content() {
["homework", $data, $USER->id, $homeworkcompletionrecords]
);


return $this->content;
}

Expand Down
10 changes: 7 additions & 3 deletions server/moodle/blocks/homework/db/access.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
// This file is part of Moodle - https://moodle.org/

// 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
Expand All @@ -8,11 +9,14 @@
//
// 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
// 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/>.
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

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


/**
* Block definition class for the block_homework plugin.
Expand Down
6 changes: 4 additions & 2 deletions server/moodle/blocks/homework/templates/data.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@
<div class="outer-box" id="outer-box">
{{#data}}
<div class="infobox">
<h2>name: {{name}}</h2>
<p>Intro: {{intro}}</p>
<h2>{{courseTitle}}</h2>
<h2>{{name}}</h2>
<h3>{{duedate}}</h3>
<p>{{intro}}</p>
<button class="timebutton" data-homework-id={{id}} id="{{id}}"">Time</button>
{{#files}}
<img src="{{iconurl}}" alt="File icon">
Expand Down
2 changes: 2 additions & 0 deletions server/moodle/blocks/homework/tests/block_homework_test.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

// This file is part of Moodle - https://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
Expand All @@ -21,6 +22,7 @@
use block_homework\external\get_homework;
use stdClass;


/**
* Test for block homework
* @copyright group 11
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public static function execute($inputfield, $link, $homework) {
}

/**
* Returns single structure of status and message.
*
* @return external_single_structure Define the return values.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@
use external_single_structure;

/**
*
* Class for saving homework literature.
*/
class save_homework_literature extends \external_api {
/**
* Returns parameters inputfield, startpage and endpage
*
* @return external_function_parameters Define the parameters expected by this function.
*/
Expand Down Expand Up @@ -102,6 +103,7 @@ public static function execute($inputfield, $startpage, $endpage, $homework, $fi
}

/**
* Returns status and message as single structure
*
* @return external_single_structure Define the return values.
*/
Expand Down
54 changes: 54 additions & 0 deletions server/moodle/mod/homework/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,57 @@ function homework_delete_instance($id) {

return true;
}

/**
* Inspiration taken from https://moodledev.io/docs/4.5/apis/subsystems/files
* Serve the files from the myplugin file areas.
*
* @param stdClass $course the course object
* @param stdClass $cm the course module object
* @param stdClass $context the context
* @param string $filearea the name of the file area
* @param array $args extra arguments (itemid, path)
* @param bool $forcedownload whether or not force download
* @param array $options additional options affecting the file serving
* @return bool false if the file not found, just send the file otherwise and do not return anything
*/
function homework_pluginfile(
$course,
$cm,
$context,
string $filearea,
array $args,
bool $forcedownload,
array $options = []
): bool {
// Make sure the user is logged in and has access to the module.
require_login($course, true);

// Extract the filename / filepath from the $args array.
$filename = array_pop($args); // The last item in the $args array.
if (empty($args)) {
// ... $args is empty => the path is '/'.
$filepath = '/';
} else {
// ... $args contains the remaining elements of the filepath.
$filepath = '/' . implode('/', $args) . '/';
}

$itemid = null;

// Retrieve the file from the Files API.
$fs = get_file_storage();
$file = $fs->get_file($context->id, 'homework', $filearea, $itemid, $filepath, $filename);
if (!$file) {
// The file does not exist.
//error_log() is forbidden, changed to debuggin
debugging("File not found: Context ID - $context->id, File area - $filearea, Item ID - $itemid,
Path - $filepath, Filename - $filename");
return false;
}

// Send file to browser with a cache lifetime of 1 day and no filtering.
send_stored_file($file, 86400, 0, $forcedownload, $options);
return true;

}

0 comments on commit 18394f5

Please sign in to comment.