diff --git a/mod/lesson/lang/en/lesson.php b/mod/lesson/lang/en/lesson.php index b47b954c9d8..4b3cea46fc4 100644 --- a/mod/lesson/lang/en/lesson.php +++ b/mod/lesson/lang/en/lesson.php @@ -155,6 +155,7 @@ $string['displayscorewithessays'] = '

You earned {$a->score} out of {$a->tempm

Your {$a->essayquestions} essay question(s) will be graded and added into your final score at a later date.

Your current grade without the essay question(s) is {$a->score} out of {$a->grade}.

'; $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'; diff --git a/mod/lesson/lesson.php b/mod/lesson/lesson.php index 390c29c00fb..4256d3994ad 100644 --- a/mod/lesson/lesson.php +++ b/mod/lesson/lesson.php @@ -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; diff --git a/mod/lesson/locallib.php b/mod/lesson/locallib.php index 6b84bf42900..80e2547cb5b 100644 --- a/mod/lesson/locallib.php +++ b/mod/lesson/locallib.php @@ -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. * diff --git a/mod/lesson/pix/e/copy.png b/mod/lesson/pix/e/copy.png new file mode 100644 index 00000000000..d3f36c3dd82 Binary files /dev/null and b/mod/lesson/pix/e/copy.png differ diff --git a/mod/lesson/pix/e/copy.svg b/mod/lesson/pix/e/copy.svg new file mode 100644 index 00000000000..6950ca925e4 --- /dev/null +++ b/mod/lesson/pix/e/copy.svg @@ -0,0 +1,3 @@ + +]> \ No newline at end of file diff --git a/mod/lesson/renderer.php b/mod/lesson/renderer.php index 56e071af212..2f7fc2264c6 100644 --- a/mod/lesson/renderer.php +++ b/mod/lesson/renderer.php @@ -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'));