Skip to content

Commit

Permalink
MDL-61674 usertours: Allow rich text (atto) editing of step content
Browse files Browse the repository at this point in the history
  • Loading branch information
nadavkav authored and Amaia Anabitarte committed Oct 13, 2021
1 parent 9145d80 commit de97874
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 9 deletions.
13 changes: 11 additions & 2 deletions admin/tool/usertours/classes/local/forms/editstep.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public function __construct($target, \tool_usertours\step $step) {
* Form definition.
*/
public function definition() {
global $CFG;

$mform = $this->_form;

$mform->addElement('header', 'heading_target', get_string('target_heading', 'tool_usertours'));
Expand All @@ -79,9 +81,16 @@ public function definition() {
$mform->setType('title', PARAM_TEXT);
$mform->addHelpButton('title', 'title', 'tool_usertours');

$mform->addElement('textarea', 'content', get_string('content', 'tool_usertours'));
$editoroptions = [
'subdirs' => 1,
'maxbytes' => $CFG->maxbytes,
'maxfiles' => EDITOR_UNLIMITED_FILES,
'changeformat' => 1,
'trusttext' => true
];
$mform->addElement('editor', 'content', get_string('content', 'tool_usertours'), null, $editoroptions);
$mform->addRule('content', get_string('required'), 'required', null, 'client');
$mform->setType('content', PARAM_RAW);
$mform->setType('content', PARAM_RAW); // No XSS prevention here, users must be trusted.
$mform->addHelpButton('content', 'content', 'tool_usertours');

// Add the step configuration.
Expand Down
7 changes: 6 additions & 1 deletion admin/tool/usertours/classes/local/table/step_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ protected function col_title(step $step) {
* @return string
*/
protected function col_content(step $step) {
return format_text(step::get_string_from_input($step->get_content()), FORMAT_HTML);
$content = $step->get_content();
$systemcontext = \context_system::instance();
$content = file_rewrite_pluginfile_urls($content, 'pluginfile.php', $systemcontext->id,
'tool_usertours', 'stepcontent', $step->get_id());

return format_text(step::get_string_from_input($content), $step->get_contentformat());
}

/**
Expand Down
11 changes: 9 additions & 2 deletions admin/tool/usertours/classes/output/step.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

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

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

use tool_usertours\step as stepsource;

/**
Expand Down Expand Up @@ -60,6 +62,11 @@ public function export_for_template(\renderer_base $output) {
global $PAGE;
$step = $this->step;

$content = $step->get_content();
$systemcontext = \context_system::instance();
$content = file_rewrite_pluginfile_urls($content, 'pluginfile.php', $systemcontext->id,
'tool_usertours', 'stepcontent', $step->get_id());

$result = (object) [
'stepid' => $step->get_id(),
'title' => external_format_text(
Expand All @@ -69,8 +76,8 @@ public function export_for_template(\renderer_base $output) {
'tool_usertours'
)[0],
'content' => external_format_text(
stepsource::get_string_from_input($step->get_content()),
FORMAT_HTML,
stepsource::get_string_from_input($content),
$step->get_contentformat(),
$PAGE->context->id,
'tool_usertours'
)[0],
Expand Down
56 changes: 54 additions & 2 deletions admin/tool/usertours/classes/step.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ class step {
*/
protected $content;

/**
* @var int $contentformat The content format: FORMAT_MOODLE/FORMAT_HTML/FORMAT_PLAIN/FORMAT_MARKDOWN.
*/
protected $contentformat;

/**
* @var int $targettype The type of target.
*/
Expand Down Expand Up @@ -147,6 +152,7 @@ protected function reload_from_record($record, $clean = false) {
$this->title = $record->title;
$this->content = $record->content;
}
$this->contentformat = isset($record->contentformat) ? $record->contentformat : 1;
$this->targettype = $record->targettype;
$this->targetvalue = $record->targetvalue;
$this->sortorder = $record->sortorder;
Expand Down Expand Up @@ -222,6 +228,15 @@ public function set_title($value) {
return $this;
}

/**
* Get the content format of the step.
*
* @return int
*/
public function get_contentformat(): int {
return $this->contentformat;
}

/**
* Get the body content of the step.
*
Expand All @@ -235,10 +250,12 @@ public function get_content() {
* Set the content value for this step.
*
* @param string $value The new content to use.
* @param int $format The new format to use: FORMAT_MOODLE/FORMAT_HTML/FORMAT_PLAIN/FORMAT_MARKDOWN.
* @return $this
*/
public function set_content($value) {
public function set_content($value, $format = FORMAT_HTML) {
$this->content = clean_text($value);
$this->contentformat = $format;
$this->dirty = true;

return $this;
Expand Down Expand Up @@ -443,6 +460,7 @@ public function to_record() {
'tourid' => $this->tourid,
'title' => $this->title,
'content' => $this->content,
'contentformat' => $this->contentformat,
'targettype' => $this->targettype,
'targetvalue' => $this->targetvalue,
'sortorder' => $this->sortorder,
Expand Down Expand Up @@ -486,6 +504,23 @@ public function persist($force = false) {
$this->get_tour()->reset_step_sortorder();
}

$systemcontext = \context_system::instance();
if ($draftid = file_get_submitted_draft_itemid('content')) {
// Take any files added to the stepcontent draft file area and
// convert them into the proper event description file area. Also
// parse the content text and replace the URLs to the draft files
// with the @@PLUGIN_FILE@@ placeholder to be persisted in the DB.
$this->content = file_save_draft_area_files(
$draftid,
$systemcontext->id,
'tool_usertours',
'stepcontent',
$this->id,
['subdirs' => true],
$this->content
);
$DB->set_field('tool_usertours_steps', 'content', $this->content, ['id' => $this->id]);
}
$this->reload();

// Notify of a change to the step configuration.
Expand Down Expand Up @@ -613,6 +648,23 @@ public function prepare_data_for_form() {
$this->get_target()->prepare_data_for_form($data);
}

// Prepare content for editing in a form 'editor' field type.
$draftitemid = file_get_submitted_draft_itemid('tool_usertours');
$systemcontext = \context_system::instance();
$data->content = [
'format' => $data->contentformat,
'itemid' => $draftitemid,
'text' => file_prepare_draft_area(
$draftitemid,
$systemcontext->id,
'tool_usertours',
'stepcontent',
$this->id,
['subdirs' => true],
$data->content
),
];

return $data;
}

Expand All @@ -625,7 +677,7 @@ public function prepare_data_for_form() {
*/
public function handle_form_submission(local\forms\editstep &$mform, \stdClass $data) {
$this->set_title($data->title);
$this->set_content($data->content);
$this->set_content($data->content['text'], $data->content['format']);
$this->set_targettype($data->targettype);

$this->set_targetvalue($this->get_target()->get_value_from_form($data));
Expand Down
3 changes: 2 additions & 1 deletion admin/tool/usertours/db/install.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="admin/tool/usertours/db" VERSION="20211007" COMMENT="XMLDB file for Moodle tool/usertours"
<XMLDB PATH="admin/tool/usertours/db" VERSION="20211013" COMMENT="XMLDB file for Moodle tool/usertours"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../lib/xmldb/xmldb.xsd"
>
Expand All @@ -26,6 +26,7 @@
<FIELD NAME="tourid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="title" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="Title of the step"/>
<FIELD NAME="content" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="Content of the user tour - allow for multilang tags"/>
<FIELD NAME="contentformat" TYPE="int" LENGTH="4" NOTNULL="true" DEFAULT="1" SEQUENCE="false"/>
<FIELD NAME="targettype" TYPE="int" LENGTH="2" NOTNULL="true" SEQUENCE="false" COMMENT="Type of the target (e.g. block, CSS selector, etc.)"/>
<FIELD NAME="targetvalue" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="The value for the specified target type."/>
<FIELD NAME="sortorder" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
Expand Down
14 changes: 14 additions & 0 deletions admin/tool/usertours/db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,19 @@ function xmldb_tool_usertours_upgrade($oldversion) {
upgrade_plugin_savepoint(true, 2021100700, 'tool', 'usertours');
}

if ($oldversion < 2021101300) {
// Define field contentformat to be added to tool_usertours_steps.
$table = new xmldb_table('tool_usertours_steps');
$field = new xmldb_field('contentformat', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '1', 'content');

// Conditionally launch add field contentformat.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Usertours savepoint reached.
upgrade_plugin_savepoint(true, 2021101300, 'tool', 'usertours');
}

return true;
}
27 changes: 27 additions & 0 deletions admin/tool/usertours/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,30 @@ function tool_usertours_get_fontawesome_icon_map() {
'tool_usertours:t/filler' => 'fa-spacer',
];
}


/**
* Serves any files associated with the user tour content.
*
* @param stdClass $course Course object
* @param stdClass $cm Course module object
* @param context $context Context
* @param string $filearea File area for data privacy
* @param array $args Arguments
* @param bool $forcedownload If we are forcing the download
* @param array $options More options
* @return bool Returns false if we don't find a file.
*/
function tool_usertours_pluginfile($course, $cm, $context, $filearea, $args, $forcedownload, array $options = []): bool {
if ($context->contextlevel != CONTEXT_SYSTEM) {
return false;
}

$fs = get_file_storage();
$file = $fs->get_file($context->id, 'tool_usertours', $filearea, $args[0], '/', $args[1]);
if (!$file) {
return false; // No such file.
}
send_stored_file($file, null, 0, $forcedownload, $options);
return true;
}
2 changes: 1 addition & 1 deletion admin/tool/usertours/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@

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

$plugin->version = 2021100700; // The current module version (Date: YYYYMMDDXX).
$plugin->version = 2021101300; // The current module version (Date: YYYYMMDDXX).
$plugin->requires = 2021052500; // Requires this Moodle version.
$plugin->component = 'tool_usertours'; // Full name of the plugin (used for diagnostics).

0 comments on commit de97874

Please sign in to comment.