From 644d506ac141f205672098b987308adf56279b75 Mon Sep 17 00:00:00 2001 From: skodak Date: Tue, 17 Feb 2009 16:18:05 +0000 Subject: [PATCH] MDL-16698 towards new general editor element + converted course section summaries --- course/editsection.html | 24 ----------------- course/editsection.php | 47 ++++++++++++++------------------- course/editsection_form.php | 22 +++++++++++++++ course/format/topics/format.php | 9 +++++-- course/format/weeks/format.php | 7 ++++- index.php | 12 ++++++--- lang/en_utf8/error.php | 1 - lib/filelib.php | 24 ++++++++++++----- lib/form/editor.php | 7 ++--- 9 files changed, 85 insertions(+), 68 deletions(-) delete mode 100644 course/editsection.html create mode 100644 course/editsection_form.php diff --git a/course/editsection.html b/course/editsection.html deleted file mode 100644 index 986b0321e3..0000000000 --- a/course/editsection.html +++ /dev/null @@ -1,24 +0,0 @@ -
- - - - - -

:

- "; - if ($usehtmleditor) { - helpbutton("richtext2", get_string("helprichtext"), "moodle", true, true); - } else { - helpbutton("text2", get_string("helptext"), "moodle", true, true); - } - ?> -
- summary, 0, false, 'summary'); ?> -
-
- - -" /> -
-
diff --git a/course/editsection.php b/course/editsection.php index d1a42faac6..86c9574dab 100644 --- a/course/editsection.php +++ b/course/editsection.php @@ -3,8 +3,10 @@ require_once("../config.php"); require_once("lib.php"); + require_once($CFG->libdir.'/filelib.php'); + require_once('editsection_form.php'); - $id = required_param('id',PARAM_INT); // Week ID + $id = required_param('id',PARAM_INT); // Week/topic ID if (! $section = $DB->get_record("course_sections", array("id"=>$id))) { print_error("sectionnotexist"); @@ -14,36 +16,29 @@ print_error("invalidcourseid"); } - require_login($course->id); + require_login($course); + $context = get_context_instance(CONTEXT_COURSE, $course->id); + require_capability('moodle/course:update', $context); - require_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $course->id)); + $draftitemid = file_get_submitted_draftitemid('summary'); + $currenttext = file_prepare_draftarea($draftitemid, $context->id, 'course_section', $section->id, true, $section->summary); + + $mform = new editsection_form(null, $course); + $data = array('id'=>$section->id, 'summary'=>array('text'=>$currenttext, 'format'=>FORMAT_HTML, 'itemid'=>$draftitemid)); + $mform->set_data($data); // set defaults /// If data submitted, then process and store. + if ($mform->is_cancelled()){ + redirect($CFG->wwwroot.'/course/view.php?id='.$course->id); - if ($form = data_submitted() and confirm_sesskey()) { - - $timenow = time(); - - if (!$DB->set_field("course_sections", "summary", $form->summary, array("id"=>$section->id))) { - print_error("cannotupdatesummary"); - } + } else if ($data = $mform->get_data()) { + $text = file_convert_draftarea($data->summary['itemid'], $context->id, 'course_section', $section->id, true, $data->summary['text']); + $DB->set_field("course_sections", "summary", $text, array("id"=>$section->id)); add_to_log($course->id, "course", "editsection", "editsection.php?id=$section->id", "$section->section"); - redirect("view.php?id=$course->id"); - exit; - } - -/// Otherwise fill and print the form. - - if (empty($form)) { - $form = $section; } - // !! no db access using data from $form beyond this point !! - - $usehtmleditor = can_use_html_editor(); - /// Inelegant hack for bug 3408 if ($course->format == 'site') { $sectionname = get_string('site'); @@ -52,15 +47,13 @@ } else { $sectionname = get_section_name($course->format); $stredit = get_string('edit', '', " $sectionname $section->section"); - $strsummaryof = get_string('summaryof', '', " $sectionname $form->section"); + $strsummaryof = get_string('summaryof', '', " $sectionname $section->section"); } print_header_simple($stredit, '', build_navigation(array(array('name' => $stredit, 'link' => null, 'type' => 'misc'))), 'theform.summary' ); - print_heading($strsummaryof); - print_simple_box_start('center'); - include('editsection.html'); - print_simple_box_end(); + print_heading_with_help($strsummaryof, 'summaries'); + $mform->display(); print_footer($course); ?> diff --git a/course/editsection_form.php b/course/editsection_form.php new file mode 100644 index 0000000000..99ce5d0325 --- /dev/null +++ b/course/editsection_form.php @@ -0,0 +1,22 @@ +libdir.'/formslib.php'); + +class editsection_form extends moodleform { + + function definition() { + global $CFG, $DB; + + $mform = $this->_form; + $course = $this->_customdata; + + $mform->addElement('editor', 'summary', get_string('summary'), array('changeformat'=>false, 'maxfiles'=>-1)); + + $mform->addElement('hidden', 'id'); + $mform->setType('id', PARAM_INT); + +//-------------------------------------------------------------------------------- + $this->add_action_buttons(); + + } +} diff --git a/course/format/topics/format.php b/course/format/topics/format.php index 6e84af8e9c..0356a611ae 100644 --- a/course/format/topics/format.php +++ b/course/format/topics/format.php @@ -26,6 +26,7 @@ //TODO (nfreear): Accessibility: evaluation, lang/en_utf8/moodle.php: $string['formattopicscss'] require_once($CFG->libdir.'/ajax/ajaxlib.php'); + require_once($CFG->libdir.'/filelib.php'); $topic = optional_param('topic', -1, PARAM_INT); @@ -133,10 +134,14 @@ echo '
 
'; echo '
'; echo '
'; + + $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); + $summarytext = file_convert_relative_pluginfiles($thissection->summary, 'pluginfile.php', "$coursecontext->id/course_section/$thissection->id/"); + $summaryformatoptions = new object(); $summaryformatoptions->noclean = true; - echo format_text($thissection->summary, FORMAT_HTML, $summaryformatoptions); + echo format_text($summarytext, FORMAT_HTML, $summaryformatoptions); - if (isediting($course->id) && has_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $course->id))) { + if (isediting($course->id) && has_capability('moodle/course:update', $coursecontext)) { echo ''.$streditsummary.''; diff --git a/course/format/weeks/format.php b/course/format/weeks/format.php index 1f5621344f..937542cf04 100644 --- a/course/format/weeks/format.php +++ b/course/format/weeks/format.php @@ -26,6 +26,7 @@ //TODO (nfreear): Accessibility: evaluation, lang/en_utf8/moodle.php: $string['formatweekscss'] require_once($CFG->libdir.'/ajax/ajaxlib.php'); + require_once($CFG->libdir.'/filelib.php'); $week = optional_param('week', -1, PARAM_INT); @@ -123,8 +124,12 @@ echo '
'; echo '
'; + + $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id); + $summarytext = file_convert_relative_pluginfiles($thissection->summary, 'pluginfile.php', "$coursecontext->id/course_section/$thissection->id/"); + $summaryformatoptions = new object(); $summaryformatoptions->noclean = true; - echo format_text($thissection->summary, FORMAT_HTML, $summaryformatoptions); + echo format_text($summarytext, FORMAT_HTML, $summaryformatoptions); if (isediting($course->id) && has_capability('moodle/course:update', get_context_instance(CONTEXT_COURSE, $course->id))) { echo '

dirroot .'/course/lib.php'); - require_once($CFG->dirroot .'/lib/blocklib.php'); + require_once($CFG->libdir .'/blocklib.php'); + require_once($CFG->libdir .'/filelib.php'); // Bounds for block widths // more flexible for theme designers taken from theme config.php @@ -144,9 +145,12 @@ echo '

'; } - $options = NULL; - $options->noclean = true; - echo format_text($section->summary, FORMAT_HTML, $options); + $context = get_context_instance(CONTEXT_COURSE, SITEID); + $summarytext = file_convert_relative_pluginfiles($section->summary, 'pluginfile.php', "$context->id/course_section/$section->id/"); + $summaryformatoptions = new object(); + $summaryformatoptions->noclean = true; + + echo format_text($summarytext, FORMAT_HTML, $summaryformatoptions); if ($editing) { $streditsummary = get_string('editsummary'); diff --git a/lang/en_utf8/error.php b/lang/en_utf8/error.php index d587a8bdb7..c2e8dcb261 100644 --- a/lang/en_utf8/error.php +++ b/lang/en_utf8/error.php @@ -152,7 +152,6 @@ $string['cannotupdateprofile'] = 'Error updating user record'; $string['cannotupdatecustomprofile'] = 'Error updating user custom record'; $string['cannotupdaterss'] = 'Cannot update RSS'; $string['cannotupdatesecret'] = 'Error resetting user secret string'; -$string['cannotupdatesummary'] = 'Could not update the summary!'; $string['cannotupdatesubcate'] = 'Could not update a child category!'; $string['cannotupdatesubcourse'] = 'Could not update a child course!'; $string['cannotuploadfile'] = 'Error processing upload file'; diff --git a/lib/filelib.php b/lib/filelib.php index 5e703c98bb..0a9848646f 100644 --- a/lib/filelib.php +++ b/lib/filelib.php @@ -131,20 +131,32 @@ function file_prepare_draftarea(&$draftitemid, $contextid, $filearea, $itemid, $ } /// relink embedded files - editor can not handle @@PLUGINFILE@@ ! + return file_convert_relative_pluginfiles($text, 'draftfile.php', "$usercontext->id/user_draft/$draftitemid/", $forcehttps); +} + +/** + * Convert relative @@PLUGINFILE@@ into real absolute paths + * @param string $text + * @param string $file pluginfile.php, draftfile.php, etc + * @param string $pluginstub path 'contextid/area/itemid/' + * @param boot $forcehttps + * @return string text with absolute links + * + */ +function file_convert_relative_pluginfiles($text, $file, $pluginstub, $forcehttps=false) { + global $CFG; if ($CFG->slasharguments) { - $draftbase = "$CFG->wwwroot/draftfile.php/$usercontext->id/user_draft/$draftitemid/"; + $draftbase = "$CFG->wwwroot/$file/$pluginstub"; } else { - $draftbase = "$CFG->wwwroot/draftfile.php?file=/$usercontext->id/user_draft/$draftitemid/"; + $draftbase = "$CFG->wwwroot/draftfile.php?file=/$pluginstub"; } if ($forcehttps) { $draftbase = str_replace('http://', 'https://', $draftbase); } - $text = str_replace('@@PLUGINFILE@@/', $draftbase); - - return $text; + return str_replace('@@PLUGINFILE@@/', $draftbase, $text); } /** @@ -261,7 +273,7 @@ function file_convert_draftarea($draftitemid, $contextid, $filearea, $itemid, $s $draftbase = str_replace('http://', 'https://', $draftbase); } - $text = str_ireplace($draftbase, '@@PLUGINFILE@@/'); + $text = str_ireplace($draftbase, '@@PLUGINFILE@@/', $text); return $text; } diff --git a/lib/form/editor.php b/lib/form/editor.php index a07c39bffd..509b3de298 100644 --- a/lib/form/editor.php +++ b/lib/form/editor.php @@ -158,6 +158,7 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element { $str .= ''; } else { // no changes of format allowed + $str .= ''; $str .= $formats[$format]; } $str .= '
'; @@ -179,10 +180,10 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element { /// TODO: somehow pass 'itemid' to tinymce so that image chooser known where to look for and upload files, // also include list of expected file types handled by editor array('image', 'video', 'media') - // JS code by Dongsheng goes here + // JS code by Dongsheng goes here - uncomment following block when finished /// TODO: hide embedded file manager if tinymce used - if ($editorclass === 'form-textarea-advanced') { +/* if ($editorclass === 'form-textarea-advanced') { $str .= ''; - } + }*/ } -- 2.39.5