Skip to content

Commit

Permalink
MDL-78476 notes: use standard text editor for editing note content.
Browse files Browse the repository at this point in the history
Also allows for storage/embedding of files attached to the editor.
  • Loading branch information
paulholden committed Dec 17, 2024
1 parent a97ddeb commit 33aeb23
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 20 deletions.
22 changes: 22 additions & 0 deletions lib/filelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -5194,6 +5194,28 @@ function file_pluginfile($relativepath, $forcedownload, $preview = null, $offlin
\core\session\manager::write_close(); // Unlock session during file serving.
send_stored_file($file, 60*60, 0, $forcedownload, $sendfileoptions);
}
} else if ($component === 'notes') {
require_login($course);

$noteid = (int) array_shift($args);

$note = $DB->get_record('post', ['module' => 'notes', 'id' => $noteid]);
$notecontext = \core\context\course::instance($note->courseid);

if ($context->id !== $notecontext->id || !has_capability('moodle/notes:view', $context)) {
send_file_not_found();
}

$filename = array_pop($args);
if (($file = $fs->get_file($context->id, $component, $filearea, $note->id, '/', $filename)) &&
!$file->is_directory()) {

\core\session\manager::write_close();
send_stored_file($file, HOURSECS, 0, $forcedownload, $sendfileoptions);
}

send_file_not_found();

} else if ($component === 'contentbank') {
if ($filearea != 'public' || isguestuser()) {
send_file_not_found();
Expand Down
12 changes: 10 additions & 2 deletions notes/classes/reportbuilder/local/entities/note.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace core_notes\reportbuilder\local\entities;

use core\context\course;
use lang_string;
use stdClass;
use core_reportbuilder\local\entities\base;
Expand Down Expand Up @@ -104,12 +105,19 @@ protected function get_all_columns(): array {
->add_joins($this->get_joins())
->set_type(column::TYPE_LONGTEXT)
->add_field($contentfieldsql, 'content')
->add_field("{$postalias}.format")
->add_fields("{$postalias}.format, {$postalias}.id, {$postalias}.courseid")
->add_callback(static function(?string $content, stdClass $note): string {
global $CFG;
require_once("{$CFG->libdir}/filelib.php");

if ($content === null) {
return '';
}
return format_text($content, $note->format);

$context = course::instance($note->courseid);
$content = file_rewrite_pluginfile_urls($content, 'pluginfile.php', $context->id, 'notes', 'content', $note->id);

return format_text($content, $note->format, ['context' => $context]);
});

// Publish state.
Expand Down
23 changes: 22 additions & 1 deletion notes/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
$state = optional_param('publishstate', NOTES_STATE_PUBLIC, PARAM_ALPHA);

$note = new stdClass();
$note->id = null;
$note->format = FORMAT_HTML;
$note->courseid = $courseid;
$note->userid = $userid;
$note->publishstate = $state;
Expand Down Expand Up @@ -67,7 +69,16 @@
throw new \moodle_exception('invaliduserid');
}

$noteform = new note_edit_form();
$editoroptions = [
'context' => $context,
'maxfiles' => EDITOR_UNLIMITED_FILES,
'maxbytes' => get_user_max_upload_file_size($context, $CFG->maxbytes, $course->maxbytes),
];

$note->contentformat = $note->format;
$note = file_prepare_standard_editor($note, 'content', $editoroptions, $context, 'notes', 'content', $note->id);

$noteform = new note_edit_form(null, ['editoroptions' => $editoroptions]);
$noteform->set_data($note);

// If form was cancelled then return to the notes list of the note.
Expand All @@ -83,7 +94,17 @@
unset($note->courseid);
unset($note->userid);
}

// Extract editor content so it can be initially saved.
$contenteditor = $note->content_editor;
['text' => $note->content, 'format' => $note->format] = $contenteditor;
note_save($note);

// Post-process editor content.
$note->content_editor = $contenteditor;
file_postupdate_standard_editor($note, 'content', $editoroptions, $context, 'notes', 'content', $note->id);
$DB->update_record('post', $note);

// Redirect to notes list that contains this note.
redirect($CFG->wwwroot . '/notes/index.php?course=' . $note->courseid . '&user=' . $note->userid);
}
Expand Down
10 changes: 6 additions & 4 deletions notes/edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ class note_edit_form extends moodleform {
*/
public function definition() {
$mform =& $this->_form;

$editoroptions = $this->_customdata['editoroptions'];

$mform->addElement('header', 'general', get_string('note', 'notes'));

$mform->addElement('textarea', 'content', get_string('content', 'notes'), array('rows' => 15, 'cols' => 40));
$mform->setType('content', PARAM_RAW);
$mform->addRule('content', get_string('nocontent', 'notes'), 'required', null, 'client');
$mform->setForceLtr('content', false);
$mform->addElement('editor', 'content_editor', get_string('content', 'notes'), null, $editoroptions);
$mform->setType('content_editor', PARAM_RAW);
$mform->addRule('content_editor', get_string('nocontent', 'notes'), 'required', null, 'client');

$mform->addElement('select', 'publishstate', get_string('publishstate', 'notes'), note_get_state_names());
$mform->setDefault('publishstate', NOTES_STATE_PUBLIC);
Expand Down
26 changes: 15 additions & 11 deletions notes/externallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,16 @@ public static function get_notes($notes) {
$context = context_course::instance($note->courseid);
self::validate_context($context);
require_capability('moodle/notes:view', $context);
list($gotnote['text'], $gotnote['format']) = util::format_text($note->content,
$note->format,
$context->id,
'notes',
'',
'');

[$gotnote['text'], $gotnote['format']] = util::format_text(
$note->content,
$note->format,
$context,
'notes',
'content',
$note->id,
);

$gotnote['noteid'] = $note->id;
$gotnote['userid'] = $note->userid;
$gotnote['publishstate'] = $note->publishstate;
Expand Down Expand Up @@ -485,7 +489,7 @@ public static function get_course_notes_parameters() {
* Create a notes list
*
* @param int $courseid ID of the Course
* @param stdClass $context context object
* @param context $context context instance
* @param int $userid ID of the User
* @param int $state
* @param int $author
Expand All @@ -500,10 +504,10 @@ protected static function create_note_list($courseid, $context, $userid, $state,
[$note['content'], $note['format']] = util::format_text(
$note['content'],
$note['format'],
$context->id,
'',
'',
0
$context,
'notes',
'content',
$note['id'],
);
$results[$key] = $note;
}
Expand Down
11 changes: 9 additions & 2 deletions notes/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,16 @@ function note_delete($note) {
$note = $DB->get_record('post', array('id' => $noteid), '*', MUST_EXIST);
$return = $DB->delete_records('post', array('id' => $note->id, 'module' => 'notes'));

$context = \core\context\course::instance($note->courseid);
get_file_storage()->delete_area_files($context->id, 'notes', 'content', $note->id);

// Trigger event.
$event = \core\event\note_deleted::create(array(
'objectid' => $note->id,
'courseid' => $note->courseid,
'relateduserid' => $note->userid,
'userid' => $note->usermodified,
'context' => context_course::instance($note->courseid),
'context' => $context,
'other' => array('publishstate' => $note->publishstate)
));
$event->add_record_snapshot('post', $note);
Expand Down Expand Up @@ -263,7 +266,8 @@ function note_print($note, $detail = NOTES_SHOW_FULL) {
// Print note content.
if ($detail & NOTES_SHOW_BODY) {
echo '<div class="content">';
echo format_text($note->content, $note->format, array('overflowdiv' => true));
$content = file_rewrite_pluginfile_urls($note->content, 'pluginfile.php', $context->id, 'notes', 'content', $note->id);
echo format_text($content, $note->format, ['overflowdiv' => true, 'context' => $context]);
echo '</div>';
}

Expand Down Expand Up @@ -344,6 +348,9 @@ function note_print_notes($header, $addcourseid = 0, $viewnotes = true, $coursei
function note_delete_all($courseid) {
global $DB;

$context = \core\context\course::instance($courseid);
get_file_storage()->delete_area_files($context->id, 'notes', 'content');

return $DB->delete_records('post', array('module' => 'notes', 'courseid' => $courseid));
}

Expand Down

0 comments on commit 33aeb23

Please sign in to comment.