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

29 homework student feature algorithmic sorting of homework #30

Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ phpcs.xml
jsconfig.json
UPGRADING-CURRENT.md
.idea
.idea/*

# Swap files (vim)
[._]*.s[a-v][a-z]
Expand Down
1 change: 1 addition & 0 deletions .idea/moodle-2.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion server/moodle/blocks/homework/amd/build/scroll.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions server/moodle/blocks/homework/amd/src/sort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import $ from 'jquery';
import Ajax from 'core/ajax';

/**
* homework/amd/src/sort.js
*
* @package
* @copyright 2024, cs-24-sw-5-13 <cs-24-sw-5-13@student.aau.dk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
*/

export const init = async() => {
$('#sort').on('change', () => {
Ajax.call([{
methodname: 'block_homework_get_homework',
args: {sort: $('#sort').val()},
done: async function(response) {
let homework = JSON.parse(response.homework);
document.getElementById("outer-box").innerHTML = "";
homework.forEach((homework) => {
let box = document.createElement("div");
box.classList.add("infobox");

let h2 = document.createElement("h2");
h2.innerHTML = `name: ${homework.name}`;
box.appendChild(h2);

let p = document.createElement("p");
p.innerHTML = "Intro: ";
box.appendChild(p);

document.getElementById("outer-box").appendChild(box);
});
},
fail: (error) => {
console.log(error);
throw new Error(`Failed to sort homework: ${error}`);
}
}]);
});
};
1 change: 1 addition & 0 deletions server/moodle/blocks/homework/block_homework.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public function get_content() {

// Include JavaScript functionality for scrolling behavior in the block
$PAGE->requires->js_call_amd('block_homework/scroll', 'init');
$PAGE->requires->js_call_amd('block_homework/sort', 'init');

return $this->content;
}
Expand Down
119 changes: 119 additions & 0 deletions server/moodle/blocks/homework/classes/external/get_homework.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?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/get_homework_chooser.php
* A class defining an external API function
*
* @package block_homework
* @copyright 2024, cs-24-sw-5-13 <cs-24-sw-5-13@student.aau.dk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
*/

namespace block_homework\external;
defined('MOODLE_INTERNAL') || die();

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

use coding_exception;
use dml_exception;
use external_function_parameters;
use external_value;
use external_single_structure;
use JsonException;

/**
*
*/
class get_homework extends \external_api {
/**
*
* @return external_function_parameters Is a definition of the functions parameter type and a description of it.
*/
public static function execute_parameters() {
return new external_function_parameters([
'sort' => new external_value(PARAM_TEXT, 'Sorting parameter'),
]);
}

/**
* The logic making the custom html for modal client-side
* @param $sort - The current modules id
* @return array - The html to be shown client-side
* @throws JsonException|dml_exception
*/
public static function execute($sort){
global $DB, $USER;
$userCourses = enrol_get_users_courses($USER->id, true);
$homeworkArray = [];
foreach($userCourses as $course){
$homeworkRecords = $DB->get_records('homework', ['course' => $course->id]);
foreach($homeworkRecords as $homework){
$homeworkArray[] = [
'id' => $homework->id,
'name' => $homework->name,
'intro' => $homework->intro,
'duedate' => $homework->duedate,
'course' => $course->fullname
];
}
}
if($sort === 'due'){
$homeworkArray = self::sortDueDate($homeworkArray);
}
/*else if($sort === 'time'){
$homeworkArray = self::sortTime($homeworkArray);
}
Implement when time task is done
*/

return ["homework" => json_encode($homeworkArray, JSON_THROW_ON_ERROR), JSON_THROW_ON_ERROR];
}

public static function sortDueDate(array $homeworkArray): array{
usort($homeworkArray, function($a, $b){
return $a['duedate'] - $b['duedate'];
});
return $homeworkArray;
}

public static function sortTime(array $homeworkArray): array{
usort($homeworkArray, function($a, $b){
return $a['time'] - $b['time'];
});
return $homeworkArray;
}

/**
*
* @return string - The name of the function
*/
public static function get_homework_returns() {
return 'homework';
}

/**
*
* @return external_single_structure - Is a definition of the functions return type and a description of it
*/
public static function execute_returns() {
return new external_single_structure([
'homework' => new external_value(PARAM_TEXT, 'Data array of courses'),
]);
}
}
46 changes: 46 additions & 0 deletions server/moodle/blocks/homework/db/services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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/db/services.php
*
* @package blocks_homework
* @copyright 2024, cs-24-sw-5-13 <cs-24-sw-5-13@student.aau.dk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
*/
defined('MOODLE_INTERNAL') || die();

$functions = [
'block_homework_get_homework' => [
'classname' => 'block_homework\external\get_homework',
'methodname' => 'execute',
'classpath' => 'blocks/homework/classes/external/get_homework.php',
'description' => 'Get the sorted homework',
'type' => 'read',
'ajax' => true,
],
];

$services = [
'block_homework_services' => [
'functions' => [
'block_homework_get_homework',
],
'restrictedusers' => 0,
'enabled' => 1,
],
];
6 changes: 6 additions & 0 deletions server/moodle/blocks/homework/templates/data.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
<button class="nav-btn" id="todaybtn">today</button>
<button class="nav-btn" id="prevbtn"><-</button>
<button class="nav-btn" id="nextbtn">-></button>
<label for="sort">Sort by:</label>
<select name="sort" id="sort">
<option value="all">All</option>
<option value="due">Due Date</option>
<option value="time">Time</option>
</select>
</div>

<div class="outer-box" id="outer-box">
Expand Down
15 changes: 15 additions & 0 deletions server/moodle/blocks/homework/tests/block_homework_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace block_homework\tests;

use block_homework\external\get_homework;
use stdClass;

class block_homework_test extends \basic_testcase {
Expand All @@ -21,6 +22,8 @@ public function test_course_homeworkfilter(){
$testhomework1->introformat = 1;
$testhomework1->description = '';
$testhomework1->eventid = 0;
$testhomework1->duedate = time() + 86400000 * 2; // 2 days from now
$testhomework1->time = 10000;

$testhomework2 = new stdClass();
$testhomework2->id = 2;
Expand All @@ -32,6 +35,8 @@ public function test_course_homeworkfilter(){
$testhomework2->introformat = 1;
$testhomework2->description = '';
$testhomework2->eventid = 0;
$testhomework2->duedate = time() + 86400000; // 1 day from now
$testhomework2->time = 20000;


//Assert that a course belonging to the correct course is returned
Expand All @@ -44,6 +49,16 @@ public function test_course_homeworkfilter(){
$tmpArray = \block_homework::filter_homework_content('http://localhost/course/view.php?id=3',$homeworks);
$this->assertNotContains($testhomework2, $tmpArray);

$this->assertEquals($homeworks[0], $testhomework1);
$this->assertEquals($homeworks[1], $testhomework2);
$homeworks = get_homework::sortDueDate($homeworks);

$this->assertEquals($homeworks[1], $testhomework1);
$this->assertEquals($homeworks[0], $testhomework2);

$homeworks = get_homework::sortTime($homeworks);
$this->assertEquals($homeworks[0], $testhomework1);
$this->assertEquals($homeworks[1], $testhomework2);
}


Expand Down
2 changes: 1 addition & 1 deletion server/moodle/blocks/homework/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@
defined('MOODLE_INTERNAL') || die();

$plugin->component = 'block_homework'; // Plugin name
$plugin->version = 2024101400; // Plugin version
$plugin->version = 2024103003; // Plugin version
$plugin->requires = 2016052300; // Moodle version
2 changes: 1 addition & 1 deletion server/moodle/mod/homework/db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
'ajax' => true,
],
'mod_homework_get_homework_chooser' => [
'classname' => 'mod_homework\external\get_homework_chooser',
'classname' => 'mod_homework\external\get_homework_',
'methodname' => 'execute',
'classpath' => 'mod/homework/classes/external/get_homework_chooser.php',
'description' => 'Get the homework chooser content',
Expand Down
2 changes: 1 addition & 1 deletion server/moodle/mod/homework/tests/modal_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function test_get_homework_chooser(): void {

$homework = $this->getDataGenerator()->get_plugin_generator('mod_homework')->create_instance(['course' => $course->id]);
// Call the external function directly.
$result = \mod_homework\external\get_homework_chooser::execute($homework->id);
$result = \mod_homework\external\get_homework_::execute($homework->id);

// Verify that the result contains the expected HTML structure.
$this->assertNotEmpty($result);
Expand Down
2 changes: 1 addition & 1 deletion server/moodle/mod/homework/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@

$plugin->component = 'mod_homework';
$plugin->release = '0.1.2';
$plugin->version = 2024101400;
$plugin->version = 2024103000;
$plugin->requires = 2022112800;
$plugin->maturity = MATURITY_ALPHA;
1 change: 0 additions & 1 deletion server/moodle/mod/homework/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

require_once('../../config.php');
global $OUTPUT, $PAGE, $DB, $CFG;
use mod_homework\view_page;

$id = required_param('id', PARAM_INT);// Course module ID.
[$course, $cm] = get_course_and_cm_from_cmid($id, 'homework');
Expand Down