Skip to content

Commit

Permalink
MDL-16698 towards new general editor element + converted course secti…
Browse files Browse the repository at this point in the history
…on summaries
  • Loading branch information
skodak committed Feb 17, 2009
1 parent cd010c0 commit 644d506
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 68 deletions.
24 changes: 0 additions & 24 deletions course/editsection.html

This file was deleted.

47 changes: 20 additions & 27 deletions course/editsection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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');
Expand All @@ -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);

?>
22 changes: 22 additions & 0 deletions course/editsection_form.php
Original file line number Diff line number Diff line change
@@ -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();

}
}
9 changes: 7 additions & 2 deletions course/format/topics/format.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -133,10 +134,14 @@
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>';
Expand Down
7 changes: 6 additions & 1 deletion course/format/weeks/format.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -123,8 +124,12 @@
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.'" '.
Expand Down
12 changes: 8 additions & 4 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -144,9 +145,12 @@
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');
Expand Down
1 change: 0 additions & 1 deletion lang/en_utf8/error.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@
$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';
Expand Down
24 changes: 18 additions & 6 deletions lib/filelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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;
}
Expand Down
7 changes: 4 additions & 3 deletions lib/form/editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ function toHtml() {
$str .= '</select>';
} else {
// no changes of format allowed
$str .= '<input type="hidden" name="'.$elname.'[format]" value="'.s($formats[$format]).'" />';
$str .= $formats[$format];
}
$str .= '</div>';
Expand All @@ -179,10 +180,10 @@ function toHtml() {

/// 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");
Expand All @@ -191,7 +192,7 @@ function toHtml() {
//]]>
</script>';
}
}*/
}


Expand Down

0 comments on commit 644d506

Please sign in to comment.