MDL-61674 usertours: Allow rich text (atto) editing of step content
This commit is contained in:
committed by
Amaia Anabitarte
parent
9145d80b0b
commit
de978747a8
@@ -56,6 +56,8 @@ class editstep extends \moodleform {
|
||||
* Form definition.
|
||||
*/
|
||||
public function definition() {
|
||||
global $CFG;
|
||||
|
||||
$mform = $this->_form;
|
||||
|
||||
$mform->addElement('header', 'heading_target', get_string('target_heading', 'tool_usertours'));
|
||||
@@ -79,9 +81,16 @@ class editstep extends \moodleform {
|
||||
$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.
|
||||
|
||||
@@ -96,7 +96,12 @@ class step_list extends \flexible_table {
|
||||
* @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());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -26,6 +26,8 @@ namespace tool_usertours\output;
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
require_once($CFG->libdir . "/filelib.php");
|
||||
|
||||
use tool_usertours\step as stepsource;
|
||||
|
||||
/**
|
||||
@@ -60,6 +62,11 @@ class step implements \renderable {
|
||||
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(
|
||||
@@ -69,8 +76,8 @@ class step implements \renderable {
|
||||
'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],
|
||||
|
||||
@@ -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.
|
||||
*/
|
||||
@@ -147,6 +152,7 @@ class step {
|
||||
$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;
|
||||
@@ -222,6 +228,15 @@ class step {
|
||||
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.
|
||||
*
|
||||
@@ -235,10 +250,12 @@ class step {
|
||||
* 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;
|
||||
@@ -443,6 +460,7 @@ class step {
|
||||
'tourid' => $this->tourid,
|
||||
'title' => $this->title,
|
||||
'content' => $this->content,
|
||||
'contentformat' => $this->contentformat,
|
||||
'targettype' => $this->targettype,
|
||||
'targetvalue' => $this->targetvalue,
|
||||
'sortorder' => $this->sortorder,
|
||||
@@ -486,6 +504,23 @@ class step {
|
||||
$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.
|
||||
@@ -613,6 +648,23 @@ class step {
|
||||
$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;
|
||||
}
|
||||
|
||||
@@ -625,7 +677,7 @@ class step {
|
||||
*/
|
||||
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));
|
||||
|
||||
Reference in New Issue
Block a user