commit c68bb2c2caaa1d5f437edc3c4d7a0e822347184a
Author: Luke Tucker <ltucker@netspot.com.au>
Date:   Thu Dec 10 10:42:20 2009 +1030

    Initial section patch adds a new option to courses to define a section to be shown when a user first loads the course.

diff --git a/backup/backuplib.php b/backup/backuplib.php
index 0636b03..ab6b0c5 100644
--- a/backup/backuplib.php
+++ b/backup/backuplib.php
@@ -706,6 +706,7 @@
             fwrite ($bf,full_tag("GROUPMODE",3,false,$course->groupmode));
             fwrite ($bf,full_tag("GROUPMODEFORCE",3,false,$course->groupmodeforce));
             fwrite ($bf,full_tag("DEFAULTGROUPINGID",3,false,$course->defaultgroupingid));
+            fwrite ($bf,full_tag("INITIALSECTION",3,false,$course->initialsection));
             fwrite ($bf,full_tag("LANG",3,false,$course->lang));
             fwrite ($bf,full_tag("THEME",3,false,$course->theme));
             fwrite ($bf,full_tag("COST",3,false,$course->cost));
diff --git a/backup/restorelib.php b/backup/restorelib.php
index f437adf..d03d790 100644
--- a/backup/restorelib.php
+++ b/backup/restorelib.php
@@ -797,6 +797,7 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                 //keep the original now - convert after groupings restored
                 $course->defaultgroupingid = addslashes($course_header->course_defaultgroupingid);
             }
+            $course->initialsection = addslashes($course_header->course_initialsection);
             $course->lang = addslashes($course_header->course_lang);
             $course->theme = addslashes($course_header->course_theme);
             $course->cost = addslashes($course_header->course_cost);
@@ -5497,6 +5498,9 @@ define('RESTORE_GROUPS_GROUPINGS', 3);
                         case "DEFAULTGROUPINGID":
                             $this->info->course_defaultgroupingid = $this->getContents();
                             break;
+                        case "INITIALSECTION":
+                            $this->info->course_initialsection = $this->getContents();
+                            break;
                         case "LANG":
                             $this->info->course_lang = $this->getContents();
                             break;
diff --git a/course/edit_form.php b/course/edit_form.php
index 158ce2c..3bf72be 100644
--- a/course/edit_form.php
+++ b/course/edit_form.php
@@ -120,6 +120,15 @@ class course_edit_form extends moodleform {
         $mform->addElement('select', 'numsections', get_string('numberweeks'), $sectionmenu);
         $mform->setDefault('numsections', $courseconfig->numsections);
 
+        $initialsectionmenu[0] = 'All';
+        for ($i=1; $i<=52; $i++) {
+            $initialsectionmenu[$i] = "$i";
+        }
+        $mform->addElement('select', 'initialsection', get_string('initialsection'), $initialsectionmenu);
+        $mform->setHelpButton('initialsection', array('courseinitialsection', get_string('initialsection')), true);
+        $mform->addRule('initialsection', get_string('initialgreaterthantotal'), 'callback', 'validateInitialSection');
+        $mform->setDefault('initialsection', 0);
+
         $mform->addElement('date_selector', 'startdate', get_string('startdate'));
         $mform->setHelpButton('startdate', array('coursestartdate', get_string('startdate')), true);
         $mform->setDefault('startdate', time() + 3600 * 24);
@@ -474,4 +483,13 @@ class course_edit_form extends moodleform {
         return $errors;
     }
 }
+
+function validateInitialSection() {
+    if($_REQUEST['initialsection'] > $_REQUEST['numsections']) {
+        return false;
+    }
+    else {
+        return true;
+   }
+}
 ?>
diff --git a/course/format/topics/format.php b/course/format/topics/format.php
index 62ca069..c92d97a 100644
--- a/course/format/topics/format.php
+++ b/course/format/topics/format.php
@@ -34,7 +34,12 @@
         if (isset($USER->display[$course->id])) {       // for admins, mostly
             $displaysection = $USER->display[$course->id];
         } else {
-            $displaysection = course_set_display($course->id, 0);
+            $initialsection = get_field('course', 'initialsection', 'id', $course->id);
+            if ($initialsection >= 0) {
+                $displaysection = course_set_display($course->id, $initialsection);
+            } else {
+                $displaysection = course_set_display($course->id, 0);
+            }
         }
     }
 
diff --git a/course/format/weeks/format.php b/course/format/weeks/format.php
index 64f3f77..b5ff891 100644
--- a/course/format/weeks/format.php
+++ b/course/format/weeks/format.php
@@ -29,7 +29,12 @@
         if (isset($USER->display[$course->id])) {
             $displaysection = $USER->display[$course->id];
         } else {
-            $displaysection = course_set_display($course->id, 0);
+            $initialsection = get_field('course', 'initialsection', 'id', $course->id);
+            if ($initialsection >= 0) {
+                $displaysection = course_set_display($course->id, $initialsection);
+            } else {
+                $displaysection = course_set_display($course->id, 0);
+            }
         }
     }
 
diff --git a/course/format/weekscss/format.php b/course/format/weekscss/format.php
index 07e0d3d..7882496 100644
--- a/course/format/weekscss/format.php
+++ b/course/format/weekscss/format.php
@@ -33,7 +33,12 @@
         if (isset($USER->display[$course->id])) {
             $displaysection = $USER->display[$course->id];
         } else {
-            $displaysection = course_set_display($course->id, 0);
+            $initialsection = get_field('course', 'initialsection', 'id', $course->id);
+            if ($initialsection >= 0) {
+                $displaysection = course_set_display($course->id, $initialsection);
+            } else {
+                $displaysection = course_set_display($course->id, 0);
+            }
         }
     }
 
diff --git a/lang/en_utf8/help/courseinitialsection.html b/lang/en_utf8/help/courseinitialsection.html
new file mode 100644
index 0000000..5f7c6d4
--- /dev/null
+++ b/lang/en_utf8/help/courseinitialsection.html
@@ -0,0 +1,10 @@
+<h1>Initial week/topic</h1>
+
+<p>This setting can be used to force a user to be shown 
+   an individual week or topic when they first access the
+   course.</p>
+
+<p>Only users accessing the course for the first time will
+   be affected by this setting. Returning users will continue
+   to be shown the same week/topic from their last visit.</p>
+
diff --git a/lang/en_utf8/moodle.php b/lang/en_utf8/moodle.php
index b0a75fe..9a208fb 100644
--- a/lang/en_utf8/moodle.php
+++ b/lang/en_utf8/moodle.php
@@ -835,6 +835,8 @@ $string['includeroleassignments'] = 'Include role assignments';
 $string['includesitefiles'] = 'Include Site Files Used in This Course';
 $string['includeuserfiles'] = 'Include User Files';
 $string['info'] = 'Information';
+$string['initialsection'] = 'Initial week/topic to show';
+$string['initialgreaterthantotal'] = 'Initial week/topic value must be less than the Number of weeks/topics setting.';
 $string['institution'] = 'Institution';
 $string['instudentview'] = 'in student view';
 $string['interests'] = 'Interests';
diff --git a/local/db/upgrade.php b/local/db/upgrade.php
new file mode 100644
index 0000000..d785609
--- /dev/null
+++ b/local/db/upgrade.php
@@ -0,0 +1,21 @@
+<?php
+
+function xmldb_local_upgrade($oldversion) {
+    global $CFG, $db;
+
+    $result = true;
+
+    if ($result && $oldversion < 2009082600) {
+
+        $table = new XMLDBTable('course');
+        $field = new XMLDBField('initialsection');
+        $field->setAttributes(XMLDB_TYPE_INTEGER, '5', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0');
+
+        /// Launch add field attachment
+        $result = $result && add_field($table, $field);
+    }
+
+    return $result;
+}
+
+?>
diff --git a/local/version.php b/local/version.php
new file mode 100644
index 0000000..19e5ebb
--- /dev/null
+++ b/local/version.php
@@ -0,0 +1,5 @@
+<?php
+
+    $local_version = 2009082600;
+
+?>

