Skip to content
This repository has been archived by the owner on Jul 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #1 from nihaalshaikh/86byt93eq-plugin-setup
Browse files Browse the repository at this point in the history
86byt93eq plugin setup
  • Loading branch information
nihaalshaikh authored Jul 3, 2024
2 parents 5725104 + 21467ee commit 48fcd8c
Show file tree
Hide file tree
Showing 15 changed files with 804 additions and 1 deletion.
17 changes: 17 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# .github/workflows/ci.yml
name: ci

on: [push, pull_request]

jobs:
test:
uses: catalyst/catalyst-moodle-workflows/.github/workflows/ci.yml@main
secrets:
# Required if you plan to publish (uncomment the below)
moodle_org_token: ${{ secrets.MOODLE_ORG_TOKEN }}
with:
#Grunt fails due to CSS styling needing an !important.
disable_phpunit: true
disable_release : true
release_branches: main
min_php : 8.1
39 changes: 38 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,38 @@
# block_coursenotes
# Moodle - block_coursenotes

The block_coursenotes block stores notes by the user for a specific course.

##### Features:

- Storing course notes

##### Tested in:

![moodle41](https://img.shields.io/badge/moodle-4.1-green.svg)
![PHP8.1](https://img.shields.io/badge/PHP-8.1-brightgreen.svg?logo=php)

### Installation

1. Copy this plugin to the `block\coursenotes` folder on the server
2. Login as administrator
3. Go to Site Administrator > Notification
4. Install the plugin

##### Requirements:

* Recommended Moodle version: **4.1 Moodle**
* Recommended PHP version: **8.1**
* Required Moodle version: **4.1 Moodle**
* Required PHP version: **8.1**

---
### Changelog

##### 4.1.0 (28.05.2024)
* Original release


##### Author:
* Nihaal Shaikh :: [Ldesign Media](https://ldesignmedia.nl/) - [luuk@ldesignmedia.nl](nihaal.shaikh@ldesignmedia.nl)

<img src="https://ldesignmedia.nl/themes/ldesignmedia/assets/images/logo/logo.svg" alt="ldesignmedia" height="70px">
3 changes: 3 additions & 0 deletions amd/build/coursenotes.min.js

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

1 change: 1 addition & 0 deletions amd/build/coursenotes.min.js.map

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

101 changes: 101 additions & 0 deletions amd/src/coursenotes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import Ajax from 'core/ajax';
import Log from 'core/log';

export const init = () => {
let noteHistory = [];
const courseNoteExists = document.getElementById('coursenote-display').textContent.trim() !== '';
const textarea = document.getElementById('coursenote-textarea');

// Hide form if course note exists.
if (courseNoteExists) {
document.getElementById('coursenote-form').style.display = 'none';
}

document.getElementById('edit-icon').addEventListener('click', function () {
document.getElementById('coursenote-display').style.display = 'none';
this.style.display = 'none'; // Hide the edit icon itself.
document.getElementById('coursenote-form').style.display = 'block';

fetchNoteHistory();
});

document.getElementById('cancel-button').addEventListener('click', function () {
window.location.reload();
});

document.getElementById('undo-button').addEventListener('click', function () {
if (noteHistory.length > 1) {
noteHistory.pop(); // Remove current note.
const lastNote = noteHistory[noteHistory.length - 1];
document.getElementById('coursenote-textarea').value = lastNote;
if (noteHistory.length <= 1) {
this.style.display = 'none'; // Hide undo button if only one note left.
}
}
});

let timeoutId;
const courseId = textarea.dataset.courseid;

textarea.addEventListener('input', () => {
clearTimeout(timeoutId);

if (textarea.value.length > 10) {
timeoutId = setTimeout(() => {
saveNoteAJAX(textarea.value);
}, 10000);
}
});
const saveNoteAJAX = (note) => {
let formattedNote;
formattedNote = stripHtmlTags(note);
Ajax.call([{
methodname: 'block_coursenotes_save_note',
args: {
coursenote: formattedNote,
courseid: courseId
},
done: (response) => {
if (response.status) {
noteHistory.push(note);
noteHistory.shift();
Log.log('Note saved successfully');
} else {
Log.log('Error saving note:', response.message);
}
},
fail: (error) => {
Log.error('AJAX error: ' + JSON.stringify(error));
}
}]);
};

const stripHtmlTags = (str) => {
return str.replace(/<\/?[^>]+(>|$)/g, "");
};

const fetchNoteHistory = () => {
Ajax.call([{
methodname: 'block_coursenotes_fetch_notes',
args: {
courseid: courseId
},
done: (response) => {
if (response.status) {
noteHistory = response.notes;
if (response.note_count > 1) {
document.getElementById('undo-button').style.display = 'block';
} else {
document.getElementById('undo-button').style.display = 'none';
}
Log.log('Note history fetched successfully');
} else {
Log.log('Error fetching note history:', response.message);
}
},
fail: (error) => {
Log.error('AJAX error: ' + JSON.stringify(error));
}
}]);
};
};
92 changes: 92 additions & 0 deletions block_coursenotes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?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/>.

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

require_once(__DIR__ . '/externallib.php');

/**
* The coursenotes block
*
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
* @package block_coursenotes
* @copyright 21/05/2024 Mfreak.nl | LdesignMedia.nl - Luuk Verhoeven
* @author Nihaal Shaikh
*/
class block_coursenotes extends block_base {

/**
* Initializes the block
*/
public function init(): void {
$this->title = get_string('coursenotes', 'block_coursenotes');
}

/**
* Returns the block's content
*
*/
public function get_content(): object {
global $COURSE, $OUTPUT;

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

$this->page->requires->js_call_amd('block_coursenotes/coursenotes', 'init');
$this->content = new stdClass();

// Prepare data for the template.
$data = [
'coursenote' => '',
'savebutton' => get_string('savenote', 'block_coursenotes'),
'courseid' => $COURSE->id,
];

// Fetch the latest coursenote.
$latestnote = helper::fetch_latest_coursenote_for_user();

if ($latestnote) {
$data['coursenote'] = $latestnote->coursenote;
}

// Render the Mustache template.
$templatecontext = (object) array_merge($data, ['output' => $OUTPUT]);
$this->content->text = $OUTPUT->render_from_template('block_coursenotes/block', $templatecontext);

// Handle form submission.
if ($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($_POST['coursenote'])) {

$coursenote = required_param('coursenote', PARAM_TEXT);
// Save the note.
block_coursenotes_external::save_note($coursenote, $COURSE->id);
// Refresh the page to see the changes.
redirect(new moodle_url('/course/view.php', ['id' => $COURSE->id]));
}

return $this->content;
}

/**
* Which page types this block may appear on.
*
* @return array page-type prefix => true/false.
*/
public function applicable_formats(): array {
return ['course-view' => true];
}
}
Loading

0 comments on commit 48fcd8c

Please sign in to comment.