MDL-55327 mod_lesson: Added the ability to duplicate pages.
authorAdrian Greeve <adrian@moodle.com>
Tue, 26 Jul 2016 11:26:16 +0000 (19:26 +0800)
committerAdrian Greeve <adrian@moodle.com>
Tue, 20 Sep 2016 02:36:13 +0000 (10:36 +0800)
mod/lesson/lang/en/lesson.php
mod/lesson/lesson.php
mod/lesson/locallib.php
mod/lesson/pix/e/copy.png [new file with mode: 0644]
mod/lesson/pix/e/copy.svg [new file with mode: 0644]
mod/lesson/renderer.php

index b47b954..4b3cea4 100644 (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';
index 390c29c..4256d39 100644 (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;
index 6b84bf4..80e2547 100644 (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.
      *
diff --git a/mod/lesson/pix/e/copy.png b/mod/lesson/pix/e/copy.png
new file mode 100644 (file)
index 0000000..d3f36c3
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 (file)
index 0000000..6950ca9
--- /dev/null
@@ -0,0 +1,3 @@
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" [\r
+       <!ENTITY ns_flows "http://ns.adobe.com/Flows/1.0/">\r
+]><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>
\ No newline at end of file
index 56e071a..2f7fc22 100644 (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'));