]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-16698 towards new general editor element + converted course section summaries
authorskodak <skodak>
Tue, 17 Feb 2009 16:18:05 +0000 (16:18 +0000)
committerskodak <skodak>
Tue, 17 Feb 2009 16:18:05 +0000 (16:18 +0000)
course/editsection.html [deleted file]
course/editsection.php
course/editsection_form.php [new file with mode: 0644]
course/format/topics/format.php
course/format/weeks/format.php
index.php
lang/en_utf8/error.php
lib/filelib.php
lib/form/editor.php

diff --git a/course/editsection.html b/course/editsection.html
deleted file mode 100644 (file)
index 986b032..0000000
+++ /dev/null
@@ -1,24 +0,0 @@
-<form id="theform" method="post" action="editsection.php">
-<table summary="Summary of week" cellpadding="5" class="boxaligncenter">
-<tr valign="top">
-    <td align="right"><p><b><?php print_string("summary") ?>:</b></p>
-     <?php helpbutton("summaries", get_string("helpsummaries"), "moodle", true, true);
-        echo "<br />";
-        if ($usehtmleditor) { 
-           helpbutton("richtext2", get_string("helprichtext"), "moodle", true, true);
-        } else {   
-           helpbutton("text2", get_string("helptext"), "moodle", true, true);   
-        } 
-     ?>
-    </td>
-    <td>
-        <?php print_textarea($usehtmleditor, 25, 60, 0, 0, 'summary', $form->summary, 0, false, 'summary'); ?>
-    </td>
-</tr>
-</table>
-<div class="singlebutton">
-<input type="hidden" name="id" value="<?php echo $form->id ?>" />
-<input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
-<input type="submit" value="<?php print_string("savechanges") ?>" />
-</div>
-</form>
index d1a42faac6dd54c4cd0cf5eebb244fb4ed41b0b0..86c9574dab0946bffe5c46ed837a295507a52f5b 100644 (file)
@@ -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");
         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');
     } 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 (file)
index 0000000..99ce5d0
--- /dev/null
@@ -0,0 +1,22 @@
+<?php  //$Id$
+
+require_once($CFG->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();
+
+    }
+}
index 6e84af8e9c430858acfdf69c8df4ef22451eab4c..0356a611aee9200c9f392f437631499b0aedce9e 100644 (file)
@@ -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);
 
         echo '<div class="right side" >&nbsp;</div>';        
         echo '<div class="content">';
         echo '<div class="summary">';
+
+        $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 '<a title="'.$streditsummary.'" '.
                  ' href="editsection.php?id='.$thissection->id.'"><img src="'.$CFG->pixpath.'/t/edit.gif" '.
                  ' class="icon edit" alt="'.$streditsummary.'" /></a>';
index 1f5621344f4c35cf53f5b8cbcf720b935764845f..937542cf044cb7063a8f206d77af5a5b2a0ce0b7 100644 (file)
@@ -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);
 
         echo '<div class="content">';
         
         echo '<div class="summary">';
+
+        $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 '<p><a title="'.$streditsummary.'" '.
index f06d7d9470e7ab64fd1ab608fd1888f0fe923f62..718bd25efe6b5c30197b7a31dec4d92446a22b21 100644 (file)
--- a/index.php
+++ b/index.php
@@ -32,7 +32,8 @@
 
     require_once('config.php');
     require_once($CFG->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
                 echo '</font></p>';
             }
 
-            $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');
index d587a8bdb75b010f3f1fde3894c8256731856e8f..c2e8dcb261a741147f6c33a550d12cbd34a3dfc5 100644 (file)
@@ -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';
index 5e703c98bbb990390c7764c59857cf79dc3eb6cc..0a9848646ff89f166ff969dd210785e4569a1e66 100644 (file)
@@ -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;
 }
index a07c39bffd30e0c67b088b0cd5d10b9c884d5a80..509b3de298590d01cf42308e6041a77bb5432e4a 100644 (file)
@@ -158,6 +158,7 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element {
             $str .= '</select>';
         } else {
             // no changes of format allowed
+            $str .= '<input type="hidden" name="'.$elname.'[format]" value="'.s($formats[$format]).'" />';
             $str .= $formats[$format];
         }
         $str .= '</div>';
@@ -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 .= '<script type="text/javascript">
 //<![CDATA[
     var fileman = document.getElementById("'.$id.'_filemanager");
@@ -191,7 +192,7 @@ class MoodleQuickForm_editor extends HTML_QuickForm_element {
 //]]>
 </script>';
 
-            }
+            }*/
         }