MDL-55327 mod_lesson: Added the ability to duplicate pages.

This commit is contained in:
Adrian Greeve
2016-09-20 10:36:13 +08:00
parent 1f2744851f
commit 9f937c3bb3
6 changed files with 103 additions and 0 deletions
+1
View File
@@ -155,6 +155,7 @@ $string['displayscorewithessays'] = '<p>You earned {$a->score} out of {$a->tempm
<p>Your {$a->essayquestions} essay question(s) will be graded and added into your final score at a later date.</p>
<p>Your current grade without the essay question(s) is {$a->score} out of {$a->grade}.</p>';
$string['displayscorewithoutessays'] = 'Your score is {$a->score} (out of {$a->grade}).';
$string['duplicatepagenamed'] = 'Duplicate page: {$a}';
$string['edit'] = 'Edit';
$string['editingquestionpage'] = 'Editing {$a} question page';
$string['editlessonsettings'] = 'Edit lesson settings';
+5
View File
@@ -23,6 +23,7 @@
* delete
* move
* moveit
* duplicate
* @package mod_lesson
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
@@ -129,6 +130,10 @@ switch ($action) {
$lesson->resort_pages($pageid, $after);
redirect("$CFG->wwwroot/mod/lesson/edit.php?id=$cm->id");
break;
case 'duplicate':
$lesson->duplicate_page($pageid);
redirect(new moodle_url('/mod/lesson/edit.php', array('id' => $cm->id)));
break;
default:
print_error('unknowaction');
break;
+87
View File
@@ -1662,6 +1662,93 @@ class lesson extends lesson_base {
return $this->pages;
}
/**
* Duplicate the lesson page.
*
* @param int $pageid Page ID of the page to duplicate.
* @return void.
*/
public function duplicate_page($pageid) {
global $PAGE;
$cm = get_coursemodule_from_instance('lesson', $this->properties->id, $this->properties->course);
$context = context_module::instance($cm->id);
// Load the page.
$page = $this->load_page($pageid);
$properties = $page->properties();
// The create method checks to see if these properties are set and if not sets them to zero, hence the unsetting here.
if (!$properties->qoption) {
unset($properties->qoption);
}
if (!$properties->layout) {
unset($properties->layout);
}
if (!$properties->display) {
unset($properties->display);
}
$properties->pageid = $pageid;
// Add text and format into the format required to create a new page.
$properties->contents_editor = array(
'text' => $properties->contents,
'format' => $properties->contentsformat
);
$answers = $page->get_answers();
// Answers need to be added to $properties.
$i = 0;
$answerids = array();
foreach ($answers as $answer) {
// Needs to be rearranged to work with the create function.
$properties->answer_editor[$i] = array(
'text' => $answer->answer,
'format' => $answer->answerformat
);
$properties->response_editor[$i] = array(
'text' => $answer->response,
'format' => $answer->responseformat
);
$answerids[] = $answer->id;
$properties->jumpto[$i] = $answer->jumpto;
$properties->score[$i] = $answer->score;
$i++;
}
// Create the duplicate page.
$newlessonpage = lesson_page::create($properties, $this, $context, $PAGE->course->maxbytes);
$newanswers = $newlessonpage->get_answers();
// Copy over the file areas as well.
$this->copy_page_files('page_contents', $pageid, $newlessonpage->id, $context->id);
$j = 0;
foreach ($newanswers as $answer) {
if (strpos($answer->answer, '@@PLUGINFILE@@') !== false) {
$this->copy_page_files('page_answers', $answerids[$j], $answer->id, $context->id);
}
if (strpos($answer->response, '@@PLUGINFILE@@') !== false) {
$this->copy_page_files('page_responses', $answerids[$j], $answer->id, $context->id);
}
$j++;
}
}
/**
* Copy the files from one page to another.
*
* @param string $filearea Area that the files are stored.
* @param int $itemid Item ID.
* @param int $newitemid The item ID for the new page.
* @param int $contextid Context ID for this page.
* @return void.
*/
protected function copy_page_files($filearea, $itemid, $newitemid, $contextid) {
$fs = get_file_storage();
$files = $fs->get_area_files($contextid, 'mod_lesson', $filearea, $itemid);
foreach ($files as $file) {
$fieldupdates = array('itemid' => $newitemid);
$fs->create_file_from_storedfile($fieldupdates, $file);
}
}
/**
* Determines if a jumpto value is correct or not.
*
Binary file not shown.

After

Width:  |  Height:  |  Size: 176 B

+3
View File
@@ -0,0 +1,3 @@
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" [
<!ENTITY ns_flows "http://ns.adobe.com/Flows/1.0/">
]><svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" preserveAspectRatio="xMinYMid meet" overflow="visible"><path d="M16 7v9H6v-4H0V0h7l3 3v1h3l3 3zM1 11h8V4H6V1H1v10zm6-8h1.6L7 1.4V3zm8 5h-3V5h-2v7H7v3h8V8zm-2-1h1.6L13 5.4V7z" fill="#999"/></svg>

After

Width:  |  Height:  |  Size: 434 B

+7
View File
@@ -414,6 +414,13 @@ class mod_lesson_renderer extends plugin_renderer_base {
$img = html_writer::img($this->output->pix_url('t/edit'), $label, array('class' => 'iconsmall'));
$actions[] = html_writer::link($url, $img, array('title' => $label));
// Duplicate action.
$url = new moodle_url('/mod/lesson/lesson.php', array('id' => $this->page->cm->id, 'pageid' => $page->id,
'action' => 'duplicate', 'sesskey' => sesskey()));
$label = get_string('duplicatepagenamed', 'lesson', format_string($page->title));
$img = html_writer::img($this->output->pix_url('e/copy', 'mod_lesson'), $label, array('class' => 'iconsmall'));
$actions[] = html_writer::link($url, $img, array('title' => $label));
$url = new moodle_url('/mod/lesson/view.php', array('id' => $this->page->cm->id, 'pageid' => $page->id));
$label = get_string('previewpagenamed', 'lesson', format_string($page->title));
$img = html_writer::img($this->output->pix_url('t/preview'), $label, array('class' => 'iconsmall'));