From d54e2145ba094b18d99ee7868c8f7eecf5b989ac Mon Sep 17 00:00:00 2001
From: danmarsden <danmarsden>
Date: Fri, 28 Nov 2008 09:20:59 +0000
Subject: [PATCH] add time restriction MDL-12588 - add ability to restrict the
 viewing of SCORM objects using a time/date restriction like most other
 modules already allow.

---
 lang/en_utf8/help/scorm/timerestrict.html |  8 ++++++++
 lang/en_utf8/scorm.php                    |  5 +++++
 mod/scorm/db/install.xml                  |  6 ++++--
 mod/scorm/db/upgrade.php                  | 15 +++++++++++++-
 mod/scorm/lib.php                         | 10 +++++++++
 mod/scorm/loadSCO.php                     | 12 ++++++++++-
 mod/scorm/mod_form.php                    | 18 ++++++++++++++++
 mod/scorm/player.php                      | 25 +++++++++++++++++++++++
 mod/scorm/version.php                     |  2 +-
 mod/scorm/view.php                        | 18 ++++++++++++++--
 10 files changed, 112 insertions(+), 7 deletions(-)
 create mode 100644 lang/en_utf8/help/scorm/timerestrict.html

diff --git a/lang/en_utf8/help/scorm/timerestrict.html b/lang/en_utf8/help/scorm/timerestrict.html
new file mode 100644
index 0000000000..ca870685d8
--- /dev/null
+++ b/lang/en_utf8/help/scorm/timerestrict.html
@@ -0,0 +1,8 @@
+<h1>Restrict times</h1>
+
+<p>This setting allows you to define a time window within which 
+   participants are allowed to attempt a SCORM.</p>
+
+<p>To set an open-ended close date just set the time very far in the future.</p>
+
+<p>If you don't need this at all then uncheck the box.</p>
\ No newline at end of file
diff --git a/lang/en_utf8/scorm.php b/lang/en_utf8/scorm.php
index 748b88aa11..4b01d1e458 100644
--- a/lang/en_utf8/scorm.php
+++ b/lang/en_utf8/scorm.php
@@ -55,6 +55,7 @@ $string['everytime']= 'Every time it\'s used';
 $string['exit'] = 'Exit course';
 $string['exitactivity'] = 'Exit activity';
 $string['expcoll'] = 'Expand/Collide';
+$string['expired'] = 'Sorry, this activity closed on $a and is no longer available';
 $string['external']= 'Update external packages timing';
 $string['failed'] = 'Failed';
 $string['firstaccess'] = 'First access';
@@ -122,6 +123,7 @@ $string['normal'] = 'Normal';
 $string['noscriptnoscorm'] = 'Your browser does not support javascript or it has javascript support disabled. This SCORM package may not play or save data correctly.';
 $string['not_corr_type'] = 'Type mismatch for tag $a->tag';
 $string['notattempted'] = 'Not attempted';
+$string['notopenyet'] = 'Sorry, this activity is not available until $a';
 $string['objectives'] = 'Objectives';
 $string['onchanges']= 'Whenever it changes';
 $string['organization'] = 'Organization';
@@ -152,7 +154,9 @@ $string['scorm:savetrack'] = 'Save tracks';
 $string['scorm:skipview'] = 'Skip overview';
 $string['scorm:viewreport'] = 'View reports';
 $string['scorm:viewscores'] = 'View scores';
+$string['scormclose'] = 'Until';
 $string['scormcourse'] = 'Learning Course';
+$string['scormopen'] = 'Open';
 $string['scormtype'] = 'Type';
 $string['scrollbars'] = 'Allow the window to be scrolled';
 $string['sided'] = 'On the left side';
@@ -168,6 +172,7 @@ $string['suspended'] = 'Suspended';
 $string['syntax'] = 'Syntax error';
 $string['tag_error'] = 'Unknown tag ($a->tag) with this content: $a->value';
 $string['time'] = 'Time';
+$string['timerestrict'] = 'Restrict answering to this time period';
 $string['title'] = 'Title';
 $string['too_many_attributes'] = 'Tag $a->tag has too many attributes';
 $string['too_many_children'] = 'Tag $a->tag has too many children';
diff --git a/mod/scorm/db/install.xml b/mod/scorm/db/install.xml
index a0d047d5f6..de8118e076 100644
--- a/mod/scorm/db/install.xml
+++ b/mod/scorm/db/install.xml
@@ -35,8 +35,10 @@
         <FIELD NAME="popup" TYPE="int" LENGTH="1" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="auto" NEXT="options"/>
         <FIELD NAME="options" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="popup" NEXT="width"/>
         <FIELD NAME="width" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="100" SEQUENCE="false" ENUM="false" PREVIOUS="options" NEXT="height"/>
-        <FIELD NAME="height" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="600" SEQUENCE="false" ENUM="false" PREVIOUS="width" NEXT="timemodified"/>
-        <FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="height"/>
+        <FIELD NAME="height" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="600" SEQUENCE="false" ENUM="false" PREVIOUS="width" NEXT="timeopen"/>
+        <FIELD NAME="timeopen" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="height" NEXT="timeclose"/>
+        <FIELD NAME="timeclose" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="timeopen" NEXT="timemodified"/>
+        <FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="timeclose"/>
       </FIELDS>
       <KEYS>
         <KEY NAME="primary" TYPE="primary" FIELDS="id"/>
diff --git a/mod/scorm/db/upgrade.php b/mod/scorm/db/upgrade.php
index 57a6a55777..57462ac547 100644
--- a/mod/scorm/db/upgrade.php
+++ b/mod/scorm/db/upgrade.php
@@ -328,7 +328,20 @@ function xmldb_scorm_upgrade($oldversion) {
         upgrade_mod_savepoint($result, 2008090307, 'scorm');
     }
     
-    
+    if ($result && $oldversion < 2008090308) {
+        $table = new xmldb_table('scorm');
+        $field = new xmldb_field('timeopen', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'height');
+        if (!$dbman->field_exists($table,$field)) {
+            $dbman->add_field($table, $field);
+        }
+        $field = new xmldb_field('timeclose', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'timeopen');
+        if (!$dbman->field_exists($table,$field)) {
+            $dbman->add_field($table, $field);
+        }
+        
+        /// scorm savepoint reached
+        upgrade_mod_savepoint($result, 2008090308, 'scorm');
+    }
     return $result;
 }
 
diff --git a/mod/scorm/lib.php b/mod/scorm/lib.php
index 00a7dbbc5f..8247b1bd3b 100755
--- a/mod/scorm/lib.php
+++ b/mod/scorm/lib.php
@@ -22,6 +22,11 @@ function scorm_add_instance($scorm, $mform=null) {
 
     require_once('locallib.php');
 
+    if (empty($scorm->timerestrict)) {
+        $scorm->timeopen = 0;
+        $scorm->timeclose = 0;
+    }
+
     $cmid       = $scorm->coursemodule;
     $cmidnumber = $scorm->cmidnumber;
     $courseid   = $scorm->course;
@@ -104,6 +109,11 @@ function scorm_update_instance($scorm, $mform=null) {
 
     require_once('locallib.php');
 
+    if (empty($scorm->timerestrict)) {
+        $scorm->timeopen = 0;
+        $scorm->timeclose = 0;
+    }
+
     $cmid       = $scorm->coursemodule;
     $cmidnumber = $scorm->cmidnumber;
     $courseid   = $scorm->course;
diff --git a/mod/scorm/loadSCO.php b/mod/scorm/loadSCO.php
index db062e206c..c954105b4a 100755
--- a/mod/scorm/loadSCO.php
+++ b/mod/scorm/loadSCO.php
@@ -33,7 +33,17 @@
     }
 
     require_login($course->id, false, $cm);
-
+    
+    //check if scorm closed
+    $timenow = time();
+    if ($scorm->timeclose !=0) {
+        if ($scorm->timeopen > $timenow) {
+            error(get_string("notopenyet", "scorm", userdate($scorm->timeopen)));
+        } else if ($timenow > $scorm->timeclose) {
+            error(get_string("expired", "scorm", userdate($scorm->timeclose)));
+        }
+    }
+   
     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
 
     if (!empty($scoid)) {
diff --git a/mod/scorm/mod_form.php b/mod/scorm/mod_form.php
index 9c41f1eaef..5dcb8c5342 100644
--- a/mod/scorm/mod_form.php
+++ b/mod/scorm/mod_form.php
@@ -66,6 +66,19 @@ class mod_scorm_mod_form extends moodleform_mod {
         $mform->addElement('file', 'packagefile', get_string('package','scorm'));
         $mform->disabledIf('packagefile', 'scormtype', 'noteq', SCORM_TYPE_LOCAL);
 
+//-------------------------------------------------------------------------------
+// Time restrictions
+        $mform->addElement('header', 'timerestricthdr', get_string('timerestrict', 'scorm'));
+        $mform->addElement('checkbox', 'timerestrict', get_string('timerestrict', 'scorm'));
+        $mform->setHelpButton('timerestrict', array("timerestrict", get_string("timerestrict","scorm"), "scorm"));
+
+
+        $mform->addElement('date_time_selector', 'timeopen', get_string("scormopen", "scorm"));
+        $mform->disabledIf('timeopen', 'timerestrict');
+
+        $mform->addElement('date_time_selector', 'timeclose', get_string("scormclose", "scorm"));
+        $mform->disabledIf('timeclose', 'timerestrict');
+
 //-------------------------------------------------------------------------------
 // Other Settings
         $mform->addElement('header', 'advanced', get_string('othersettings', 'form'));
@@ -267,6 +280,11 @@ class mod_scorm_mod_form extends moodleform_mod {
         if (isset($default_values['instance'])) {
             $default_values['datadir'] = $default_values['instance'];
         }
+        if (empty($default_values['timeopen'])) {
+            $default_values['timerestrict'] = 0;
+        } else {
+            $default_values['timerestrict'] = 1;
+        }
     }
 
     function validation($data, $files) {
diff --git a/mod/scorm/player.php b/mod/scorm/player.php
index 2029122faf..cc9b85890f 100755
--- a/mod/scorm/player.php
+++ b/mod/scorm/player.php
@@ -65,7 +65,32 @@
         print_header($pagetitle, $course->fullname, $navigation,
                  '', '', true, update_module_button($cm->id, $course->id, $strscorm), '', false);
         notice(get_string("activityiscurrentlyhidden"));
+        print_footer($course);
+        die;
     }
+
+    //check if scorm closed
+    $timenow = time();
+    if ($scorm->timeclose !=0) {
+        if ($scorm->timeopen > $timenow) {
+            $navlinks[] = array('name' => format_string($scorm->name,true), 'link' => "view.php?id=$cm->id", 'type' => 'activityinstance');
+            $navigation = build_navigation($navlinks);
+            print_header($pagetitle, $course->fullname, $navigation,
+                     '', '', true, update_module_button($cm->id, $course->id, $strscorm), '', false);
+            print_simple_box(get_string("notopenyet", "scorm", userdate($scorm->timeopen)), "center");
+            print_footer($course);
+            die;
+        } elseif ($timenow > $scorm->timeclose) {
+            $navlinks[] = array('name' => format_string($scorm->name,true), 'link' => "view.php?id=$cm->id", 'type' => 'activityinstance');
+            $navigation = build_navigation($navlinks);
+            print_header($pagetitle, $course->fullname, $navigation,
+                     '', '', true, update_module_button($cm->id, $course->id, $strscorm), '', false);
+            print_simple_box(get_string("expired", "scorm", userdate($scorm->timeclose)), "center");
+            print_footer($course);
+            die;
+        }
+    }
+
     //
     // TOC processing
     //
diff --git a/mod/scorm/version.php b/mod/scorm/version.php
index 8ac61d8a4c..163d51b9c8 100755
--- a/mod/scorm/version.php
+++ b/mod/scorm/version.php
@@ -10,7 +10,7 @@
 //       catch up now, so until 27th October please only increment in very tiny steps
 //       in HEAD, until we get past that date..
 
-$module->version  = 2008090307;   // The (date) version of this module
+$module->version  = 2008090308;   // The (date) version of this module
 $module->requires = 2008090108;   // The version of Moodle that is required
 $module->cron     = 300;            // How often should cron check this module (seconds)?
 
diff --git a/mod/scorm/view.php b/mod/scorm/view.php
index 4ccdc3c579..a61d54a2ed 100755
--- a/mod/scorm/view.php
+++ b/mod/scorm/view.php
@@ -87,6 +87,20 @@
         $attemptstatus = scorm_get_attempt_status($USER,$scorm);
     }
     print_simple_box(format_text($scorm->summary).$attemptstatus, 'center', '70%', '', 5, 'generalbox', 'intro');
-    scorm_view_display($USER, $scorm, 'view.php?id='.$cm->id, $cm);
+    
+    $scormopen = true;
+    $timenow = time();
+    if ($scorm->timeclose !=0) {
+        if ($scorm->timeopen > $timenow) {
+            print_simple_box(get_string("notopenyet", "scorm", userdate($scorm->timeopen)), "center");
+            $scormopen = false;
+        } else if ($timenow > $scorm->timeclose) {
+            print_simple_box(get_string("expired", "scorm", userdate($scorm->timeclose)), "center");
+            $scormopen = false;
+        }
+    }
+    if ($scormopen) {
+        scorm_view_display($USER, $scorm, 'view.php?id='.$cm->id, $cm);
+    }
     print_footer($course);
-?>
+?>
\ No newline at end of file
-- 
2.39.5