# This patch file was generated by NetBeans IDE
# This patch can be applied using context Tools: Apply Diff Patch action on respective folder.
# It uses platform neutral UTF-8 encoding.
# Above lines and this line are ignored by the patching process.
Index: moodle/mod/lesson/backup/moodle2/backup_lesson_activity_task.class.php
--- moodle/mod/lesson/backup/moodle2/backup_lesson_activity_task.class.php No Base Revision
+++ moodle/mod/lesson/backup/moodle2/backup_lesson_activity_task.class.php Locally New
@@ -0,0 +1,139 @@
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * This file contains the backup task for the lesson module
+ *
+ * @package   moodlecore
+ * @copyright 2010 Sam Hemelryk
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+/**
+ * Require the backup lesson steps lib
+ */
+require_once($CFG->dirroot . '/mod/lesson/backup/moodle2/backup_lesson_stepslib.php');
+
+/**
+ * The backup task class for the lesson module
+ * @copyright 2010 Sam Hemelryk
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class backup_lesson_activity_task extends backup_activity_task {
+
+    const NUMERIC = '([0-9]+)';
+    const ALPHA = '([a-zA-Z]+)';
+    const ALPHANUMERIC = '([a-zA-Z0-9]+)';
+
+    protected function define_my_settings() {
+        // There are no settings
+    }
+
+    protected function define_my_steps() {
+        $this->add_step(new backup_lesson_activity_structure_step('lesson structure', 'lesson.xml'));
+    }
+
+    /**
+     * Replaces links within the content with links that can be corrected during
+     * restore
+     *
+     * @global <type> $CFG
+     * @param <type> $content
+     * @return <type>
+     */
+    static public function encode_content_links($content) {
+        global $CFG;
+
+        // Define the pages links can exist for and the params they can have
+        $pages = array(
+            /**
+             * Provides the interface for overall authoring of lessons
+             */
+            'edit' => array(
+                'required'=>array('id' => self::NUMERIC)),
+            /**
+             * Action for adding a question page.  Prints an HTML form.
+             */
+            'editpage' => array(
+                'required'=>array('id' => self::NUMERIC, 'pageid' => self::NUMERIC)),
+            /**
+             * Provides the interface for grading essay questions
+             */
+            'essay' => array(
+                'required' => array('id' => self::NUMERIC)),
+            /**
+             * Provides the interface for viewing and adding high scores
+             */
+            'highscores' => array(
+                'required' => array('id' => self::NUMERIC)),
+            /**
+             * This page lists all the instances of lesson in a particular course
+             */
+            'index' => array(
+                'required'=>array('id' => self::NUMERIC)),
+            /**
+             * This file plays the mediafile set in lesson settings.
+             */
+            'mediafile' => array(
+                'required'=>array('id' => self::NUMERIC)),
+            /**
+             * Displays the lesson statistics.
+             */
+            'report' => array(
+                'required'=>array('id' => self::NUMERIC),
+                'optional'=>array('pageid' => self::NUMERIC)),
+            /**
+             * This page prints a particular instance of lesson
+             */
+            'view' => array(
+                'required'=>array('id' => self::NUMERIC),
+                'optional'=>array('pageid' => self::NUMERIC))
+        );
+        // Foreach page we want to replace the relevant links
+        foreach ($pages as $page=>$params) {
+            $replacement = 'LESSON'.strtoupper($page);
+            $paramregex = array();
+            $count = 1;
+            if (array_key_exists('required', $params)) {
+                foreach ($params['required'] as $param=>$match) {
+                    $paramregex[] = preg_quote($param).'='.$match;
+                    $replacement .= '*$'.$count;
+                    $count++;
+                }
+            }
+            $paramregex = join('&', $paramregex);
+            if (array_key_exists('optional', $params)) {
+                foreach ($params['optional'] as $param=>$match) {
+                    if ($paramregex !== '') {
+                        $paramregex .= '(&'.preg_quote($param).'='.$match.')?';
+                    } else {
+                        $paramregex .= '('.preg_quote($param).'='.$match.')?';
+                    }
+                    $count++;
+                    $replacement .= '*$'.$count;
+                    $count++;
+                }
+            }
+            $pattern = '#'.preg_quote($CFG->wwwroot.'/mod/lesson/'.$page.'.php?', '#').$paramregex.'#';
+            $replacement = '$@'.$replacement.'@$';
+            $content = preg_replace($pattern, $replacement, $content);
+        }
+        // Return the content
+        return $content;
+    }
+
+}
Index: moodle/mod/lesson/backup/moodle2/backup_lesson_stepslib.php
--- moodle/mod/lesson/backup/moodle2/backup_lesson_stepslib.php No Base Revision
+++ moodle/mod/lesson/backup/moodle2/backup_lesson_stepslib.php Locally New
@@ -0,0 +1,133 @@
+<?php
+
+// This file is part of Moodle - http://moodle.org/
+//
+// Moodle is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// Moodle is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
+
+/**
+ * This file contains the backup structure for the lesson module
+ *
+ * @package   moodlecore
+ * @copyright 2010 Sam Hemelryk
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+
+/**
+ * Structure step class that informs a backup task how to backup the lesson module.
+ *
+ * @copyright 2010 Sam Hemelryk
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class backup_lesson_activity_structure_step extends backup_activity_structure_step {
+
+    protected function define_structure() {
+
+        $userinfo = $this->get_setting_value('userinfo');
+
+        $lesson = new backup_nested_element('lesson', array('id'), array(
+            'course','name','practice','modattempts','usepassword','password',
+            'dependency','conditions','grade','custom','ongoing','usemaxgrade',
+            'maxanswers','maxattempts','review','nextpagedefault','feedback',
+            'minquestions','maxpages','timed','maxtime','retake','activitylink',
+            'mediafile','mediaheight','mediawidth','mediaclose','slideshow',
+            'width','height','bgcolor','displayleft','displayleftif','progressbar',
+            'highscores','maxhighscores','available','deadline','timemodified'
+        ));
+
+        $pages = new backup_nested_element('pages');
+        $page = new backup_nested_element('page', array('id'), array(
+            'prevpageid','nextpageid','qtype','qoption','layout',
+            'display','timecreated','timemodified','title','contents'
+        ));
+
+        $answers = new backup_nested_element('answers');
+        $answer = new backup_nested_element('answer', array('id'), array(
+            'jumpto','grade','score','flags','timecreated','timemodified','answer',
+            'response'
+        ));
+
+        $attempts = new backup_nested_element('attempts');
+        $attempt = new backup_nested_element('attempt', array('id'), array(
+            'userid','retry','correct','useranswer','timeseen'
+        ));
+
+        $branches = new backup_nested_element('branches');
+        $branch = new backup_nested_element('branch', array('id'), array(
+            'userid','retry','flag','timeseen'
+        ));
+
+        $grades = new backup_nested_element('grades');
+        $grade = new backup_nested_element('grade', array('id'), array(
+            'userid','grade','late','completed'
+        ));
+
+        $highscores = new backup_nested_element('highscores');
+        $highscore = new backup_nested_element('highscore', array('id'), array(
+            'gradeid','userid','nickname'
+        ));
+
+        $timers = new backup_nested_element('timers');
+        $timer = new backup_nested_element('timer', array('id'), array(
+            'userid','starttime','lessontime'
+        ));
+
+        $lesson->add_child($pages);
+            $pages->add_child($page);
+                $page->add_child($answers);
+                    $answers->add_child($answer);
+                        $answer->add_child($attempts);
+                            $attempts->add_child($attempt);
+                $page->add_child($branches);
+                    $branches->add_child($branch);
+        $lesson->add_child($grades);
+            $grades->add_child($grade);
+        $lesson->add_child($highscores);
+            $highscores->add_child($highscore);
+        $lesson->add_child($timers);
+            $timers->add_child($timer);
+
+        $lesson->set_source_table('lesson', array('id' => backup::VAR_ACTIVITYID));
+        $page->set_source_table('lesson_pages', array('lessonid' => backup::VAR_PARENTID));
+
+        if ($userinfo) {
+            $answer->set_source_table('lesson_answers', array('pageid' => backup::VAR_PARENTID));
+            $attempt->set_source_table('lesson_attempts', array(
+                'answerid' => backup::VAR_PARENTID,
+                'pageid' => '../../../../id', #attempt/attempts/answer/answers/page
+                'lessonid' => '../../../../../../id', #attempt/attempts/answer/answers/page/pages/lesson
+            ));
+            $branch->set_source_table('lesson_branch', array(
+                'pageid' => backup::VAR_PARENTID,
+                'lessonid' => '../../id'
+            ));
+            $grade->set_source_table('lesson_grades', array('lessonid'=>backup::VAR_PARENTID));
+            $highscore->set_source_table('lesson_high_scores', array(
+                'lessonid' => backup::VAR_PARENTID
+            ));
+            $timer->set_source_table('lesson_timer', array('lessonid' => backup::VAR_PARENTID));
+        }
+
+        $attempt->annotate_ids('user', 'userid');
+        $branch->annotate_ids('user', 'userid');
+        $grade->annotate_ids('user', 'userid');
+        $highscore->annotate_ids('user', 'userid');
+        $timer->annotate_ids('user', 'userid');
+
+        $lesson->annotate_files(array('lesson_media_file'), 'id');
+        $page->annotate_files(array('lesson_page_contents'), 'id');
+
+        return $this->prepare_activity_structure($lesson);
+    }
+
+}
Index: moodle/mod/lesson/lib.php
--- moodle/mod/lesson/lib.php Base (1.90)
+++ moodle/mod/lesson/lib.php Locally Modified (Based On 1.90)
@@ -764,7 +764,7 @@
         case FEATURE_COMPLETION_TRACKS_VIEWS: return true;
         case FEATURE_GRADE_HAS_GRADE:         return true;
         case FEATURE_GRADE_OUTCOMES:          return true;
-
+        case FEATURE_BACKUP_MOODLE2:          return true;
         default: return null;
     }
 }
