Browse Source

Adding CRUD for the new LP dates BT#1618

Julio Montoya 14 years ago
parent
commit
cb4a7655bb
4 changed files with 252 additions and 75 deletions
  1. 136 30
      main/newscorm/learnpath.class.php
  2. 46 31
      main/newscorm/lp_add.php
  3. 34 11
      main/newscorm/lp_controller.php
  4. 36 3
      main/newscorm/lp_edit.php

+ 136 - 30
main/newscorm/learnpath.class.php

@@ -73,6 +73,11 @@ class learnpath {
 
     public $prerequisite = 0;
     public $use_max_score = 100; //Should work as usual
+    
+    public $created_on      = '';
+    public $modified_on     = '';
+    public $publicated_on   = '';
+    public $expired_on      = '';
 
     /**
      * Class constructor. Needs a database handler, a course code and a learnpath id from the database.
@@ -118,24 +123,35 @@ class learnpath {
             if ($this->debug > 2) { error_log('New LP - learnpath::__construct() '.__LINE__.' - Querying lp: '.$sql, 0); }
             $res = Database::query($sql);
             if (Database::num_rows($res) > 0) {
-                $this->lp_id = $lp_id;
-                $row = Database::fetch_array($res);
-                $this->type = $row['lp_type'];
-                $this->name = stripslashes($row['name']);
-                //$this->encoding = $row['default_encoding']; // Chamilo 1.8.8: We intend not to use 'default_encoding' field anymore.
-                $this->proximity = $row['content_local'];
-                $this->theme = $row['theme'];
-                $this->maker = $row['content_maker'];
-                $this->prevent_reinit = $row['prevent_reinit'];
-                $this->license = $row['content_license'];
-                $this->scorm_debug = $row['debug'];
-                   $this->js_lib = $row['js_lib'];
-                   $this->path = $row['path'];
-                   $this->preview_image= $row['preview_image'];
-                   $this->author= $row['author'];
-                   $this->lp_session_id = $row['session_id'];
-
-                   if ($this->type == 2) {
+                $this->lp_id            = $lp_id;
+                $row                    = Database::fetch_array($res);
+                $this->type             = $row['lp_type'];
+                $this->name             = stripslashes($row['name']);
+                //$this->encoding       = $row['default_encoding']; // Chamilo 1.8.8: We intend not to use 'default_encoding' field anymore.
+                $this->proximity        = $row['content_local'];
+                $this->theme            = $row['theme'];
+                $this->maker            = $row['content_maker'];
+                $this->prevent_reinit   = $row['prevent_reinit'];
+                $this->license          = $row['content_license'];
+                $this->scorm_debug      = $row['debug'];
+                $this->js_lib           = $row['js_lib'];
+                $this->path             = $row['path'];
+                $this->preview_image    = $row['preview_image'];
+                $this->author           = $row['author'];
+                $this->lp_session_id    = $row['session_id'];
+                
+                $this->created_on       = $row['created_on'];
+                $this->modified_on      = $row['modified_on'];
+                
+                if ($row['publicated_on'] != '0000-00-00 00:00:00') { 
+                    $this->publicated_on    = api_get_local_time($row['publicated_on']);
+                }
+                
+                if ($row['expired_on'] != '0000-00-00 00:00:00') {
+                    $this->expired_on     = api_get_local_time($row['expired_on']);
+                }
+
+                if ($this->type == 2) {
                     if ($row['force_commit'] == 1) {
                         $this->force_commit = true;
                     }
@@ -167,9 +183,9 @@ class learnpath {
         }
         // End of variables checking.
 
-    $session_id = api_get_session_id();
-    // Get the session condition for learning paths of the base + session.
-    $session = api_get_session_condition($session_id);
+        $session_id = api_get_session_id();
+        //  Get the session condition for learning paths of the base + session.
+        $session = api_get_session_condition($session_id);
         // Now get the latest attempt from this user on this LP, if available, otherwise create a new one.
         $lp_table = Database::get_course_table(TABLE_LP_VIEW);
         // Selecting by view_count descending allows to get the highest view_count first.
@@ -572,7 +588,7 @@ class learnpath {
      * @param	string	Zip file containing the learnpath or directory containing the learnpath
      * @return	integer	The new learnpath ID on success, 0 on failure
      */
-    public function add_lp($course, $name, $description = '', $learnpath = 'guess', $origin = 'zip', $zipname = '') {
+    public function add_lp($course, $name, $description = '', $learnpath = 'guess', $origin = 'zip', $zipname = '', $publicated_on = '', $expired_on = '') {
         global $charset;
 
         //if ($this->debug > 0) { error_log('New LP - In learnpath::add_lp()', 0); }
@@ -588,6 +604,20 @@ class learnpath {
         $check_name = "SELECT * FROM $tbl_lp WHERE name = '$name'";
         //if ($this->debug > 2) { error_log('New LP - Checking the name for new LP: '.$check_name, 0); }
         $res_name = Database::query($check_name);
+        
+        if ($publicated_on == '0000-00-00 00:00:00' || empty($publicated_on)) {
+            //by default the publication date is the same that the creation date    
+            $publicated_on = api_get_utc_datetime();        
+        } else {
+        	$publicated_on   = Database::escape_string(api_get_utc_datetime($publicated_on));  
+        }
+        
+        if ($expired_on == '0000-00-00 00:00:00' || empty($expired_on)) {    
+            $expired_on = '';        
+        } else {
+            $expired_on   = Database::escape_string(api_get_utc_datetime($expired_on));  
+        }   
+        
         while (Database :: num_rows($res_name)) {
             // There is already one such name, update the current one a bit.
             $i++;
@@ -603,7 +633,7 @@ class learnpath {
         switch ($learnpath) {
             case 'guess':
                 break;
-            case 'dokeos':
+            case 'chamilo':
                 $type = 1;
                 break;
             case 'aicc':
@@ -623,17 +653,14 @@ class learnpath {
                     $row = Database :: fetch_array($res_max);
                     $dsp = $row[0] + 1;
                 }
-                $sql_insert = "INSERT INTO $tbl_lp " .
-                    "(lp_type,name,description,path,default_view_mod," .
-                    "default_encoding,display_order,content_maker," .
-                    "content_local,js_lib,session_id) " .
-                    "VALUES ($type,'$name','$description','','embedded'," .
-                    "'UTF-8','$dsp','Chamilo'," .
-                    "'local','','".Database::escape_string($session_id)."')";
+                $sql_insert = "INSERT INTO $tbl_lp (lp_type,name,description,path,default_view_mod, default_encoding,display_order,content_maker,content_local,js_lib,session_id, created_on, publicated_on, expired_on) " .
+                              "VALUES ($type,'$name','$description','','embedded','UTF-8','$dsp','Chamilo','local','','".$session_id."', '".api_get_utc_datetime()."' , '".$publicated_on."' , '".$expired_on."')";                    
+                
                 //if ($this->debug > 2) { error_log('New LP - Inserting new lp '.$sql_insert, 0); }
                 $res_insert = Database::query($sql_insert);
                 $id = Database :: insert_id();
                 if ($id > 0) {
+                    
                     // Insert into item_property.
                     api_item_property_update(api_get_course_info(), TOOL_LEARNPATH, $id, 'LearnpathAdded', api_get_user_id());
                     return $id;
@@ -4019,6 +4046,83 @@ class learnpath {
         return true;
     }
     
+     /**
+     * Sets and saves the expired_on date
+     * @param   string  Optional string giving the new author of this learnpath
+     * @return   bool    Returns true if author's name is not empty
+     */
+    public function set_expired_on($expired_on) {
+        if ($this->debug > 0) {
+            error_log('New LP - In learnpath::set_expired_on()', 0);
+        }
+        
+        if (!empty($expired_on)) {
+            $this->expired_on = $this->escape_string(api_get_utc_datetime($expired_on));
+        } else {
+            $this->expired_on = '';
+        }
+        $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
+        $lp_id = $this->get_id();
+        $sql = "UPDATE $lp_table SET expired_on = '" . $this->expired_on . "' WHERE id = '$lp_id'";
+        if ($this->debug > 2) {
+            error_log('New LP - lp updated with new expired_on : ' . $this->expired_on, 0);
+        }
+        $res = Database::query($sql);
+        return true;
+    }
+    
+    
+    /**
+     * Sets and saves the publicated_on date
+     * @param   string  Optional string giving the new author of this learnpath
+     * @return   bool    Returns true if author's name is not empty
+     */
+    public function set_publicated_on($publicated_on) {
+        if ($this->debug > 0) {
+            error_log('New LP - In learnpath::set_expired_on()', 0);
+        }
+        if (!empty($publicated_on)) {
+            $this->publicated_on = $this->escape_string(api_get_utc_datetime($publicated_on));
+        } else {
+        	$this->publicated_on = '';
+        }
+        $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
+        $lp_id = $this->get_id();
+        $sql = "UPDATE $lp_table SET publicated_on = '" . $this->publicated_on . "' WHERE id = '$lp_id'";
+        if ($this->debug > 2) {
+            error_log('New LP - lp updated with new publicated_on : ' . $this->publicated_on, 0);
+        }
+        $res = Database::query($sql);
+        return true;
+    }
+    
+    
+    
+    /**
+     * Sets and saves the expired_on date
+     * @param   string  Optional string giving the new author of this learnpath
+     * @return   bool    Returns true if author's name is not empty
+     */
+    public function set_modified_on() {
+        if ($this->debug > 0) {
+            error_log('New LP - In learnpath::set_expired_on()', 0);
+        }
+        $this->modified_on = api_get_utc_datetime();
+        $lp_table = Database :: get_course_table(TABLE_LP_MAIN);
+        $lp_id = $this->get_id();
+        $sql = "UPDATE $lp_table SET modified_on = '" . $this->modified_on . "' WHERE id = '$lp_id'";
+        if ($this->debug > 2) {
+            error_log('New LP - lp updated with new expired_on : ' . $this->modified_on, 0);
+        }
+        $res = Database::query($sql);
+        return true;
+    }
+    
+    
+    
+    
+    
+    
     
 
     /**
@@ -8538,6 +8642,8 @@ EOD;
             Database::update_query($lp_table, $attributes, $where );
         }
     }
+    
+    
 }
 
 if (!function_exists('trim_value')) {

+ 46 - 31
main/newscorm/lp_add.php

@@ -8,13 +8,16 @@
  * @author Denes Nagy
  * @author Roan Embrechts, refactoring and code cleaning
  * @author Yannick Warnier <ywarnier@beeznest.org> - cleaning and update for new SCORM tool
+ * @author Julio Montoya <gugli100@gmail.com> Adding formvalidator support
+ * 
  * @package chamilo.learnpath
  */
 
 /* INIT SECTION */
 
-$this_section = SECTION_COURSES;
+require_once api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php';
 
+$this_section = SECTION_COURSES;
 api_protect_course_script();
 
 /* Libraries */
@@ -22,9 +25,9 @@ api_protect_course_script();
 // The main_api.lib.php, database.lib.php and display.lib.php
 // libraries are included by default.
 
-include 'learnpath_functions.inc.php';
+require 'learnpath_functions.inc.php';
 //include '../resourcelinker/resourcelinker.inc.php';
-include 'resourcelinker.inc.php';
+require 'resourcelinker.inc.php';
 // Rewrite the language file, sadly overwritten by resourcelinker.inc.php.
 // Name of the language file that needs to be included.
 $language_file = 'learnpath';
@@ -43,13 +46,23 @@ $("#learnpath_title").focus();
 $(document).ready(function () {
   setFocus();
 });
+        
+function timelimit() {
+    if(document.getElementById(\'options2\').style.display == \'none\')
+    {
+        document.getElementById(\'options2\').style.display = \'block\';
+    } else {
+        document.getElementById(\'options2\').style.display = \'none\';
+    }
+}
+     
 </script>';
 
 /* Constants and variables */
 
 $is_allowed_to_edit = api_is_allowed_to_edit(null, true);
 
-$tbl_lp = Database::get_course_table(TABLE_LP_MAIN);
+$tbl_lp      = Database::get_course_table(TABLE_LP_MAIN);
 $tbl_lp_item = Database::get_course_table(TABLE_LP_ITEM);
 $tbl_lp_view = Database::get_course_table(TABLE_LP_VIEW);
 
@@ -63,6 +76,7 @@ $submit			= $_POST['submit_button'];
 if ($action == 'add' && $type == 'learnpathitem') {
      $htmlHeadXtra[] = "<script language='JavaScript' type='text/javascript'> window.location=\"../resourcelinker/resourcelinker.php?source_id=5&action=$action&learnpath_id=$learnpath_id&chapter_id=$chapter_id&originalresource=no\"; </script>";
 }
+
 if ((!$is_allowed_to_edit) || ($isStudentView)) {
     error_log('New LP - User not authorized in lp_add.php');
     header('location:lp_controller.php?action=view&lp_id='.$learnpath_id);
@@ -99,40 +113,41 @@ echo '</div>';
 
 Display::display_normal_message(get_lang('AddLpIntro'), false);
 
-if ($_POST AND empty($_REQUEST['learnpath_name'])) {
+if ($_POST AND empty($_REQUEST['lp_name'])) {
     Display::display_error_message(get_lang('FormHasErrorsPleaseComplete'), false);
 }
 
-echo '<form method="post">';
+
+$form = new FormValidator('lp_add', 'post', 'lp_controller.php');
 
 // Form title
-echo '<div class="row"><div class="form_header">'.get_lang('AddLpToStart').'</div></div>';
+$form->addElement('header', null, get_lang('AddLpToStart'));
 
-// Title field
-echo '<div class="row">';
-echo '<div class="label">';
-echo '<label for="idTitle"><span class="form_required">*</span> '.get_lang('LPName').'</label>';
-echo '</div>';
-echo '<div class="formw">';
-echo '<input id="learnpath_title" name="learnpath_name" type="text" size="50" />';
-echo '</div>';
-echo '</div>';
+// Title
+$form->addElement('text', 'lp_name', api_ucfirst(get_lang('LPName')), array('size' => 43));
+$form->applyFilter('lp_name', 'html_filter');
+$form->addRule('lp_name', get_lang('ThisFieldIsRequired'), 'required');
 
-// Submit button
-echo '<div class="row">';
-echo '<div class="label">';
-echo '</div>';
-echo '<div class="formw">';
-echo '<button  class="save" style="width:150px;" type="submit"/>'.get_lang('CreateLearningPath').'</button>';
-echo '</div>';
-echo '</div>';
-echo '<input name="post_time" type="hidden" value="' . time() . '" />';
-echo '</form>';
+$form->addElement('hidden', 'post_time', time());
+$form->addElement('hidden', 'action', 'add_lp');
+
+$form->addElement('checkbox', 'enabletimelimit',get_lang('EnableTimeLimits'),null,'onclick = "  return timelimit() "');
+    
+$form->addElement('html','<div id="options2" style="display:none;">');
+
+$form->addElement('datepicker', 'publicated_on', get_lang('PublicationDate'), array('form_name'=>'exercise_admin'), 5);
+$form->addElement('datepicker', 'expired_on', get_lang('ExpirationDate'), array('form_name'=>'exercise_admin'), 5);
+
+$form->addElement('html','</div>');
+            
+            
+$defaults['publicated_on']  = date('Y-m-d 12:00:00');
+$defaults['expired_on']     = date('Y-m-d 12:00:00',time()+84600);
+
+$form->setDefaults($defaults);                  
+$form->addElement('style_submit_button', 'Submit',get_lang('CreateLearningPath'),'class="save"');
 
-echo '<div class="row">';
-echo '<div class="label"></div>';
-echo '<div class="formw"><span class="form_required">*</span> <small>'.get_lang('ThisFieldIsRequired').'</small></div>';
-echo '</div>';
 
+$form->display();
 // Footer
-Display::display_footer();
+Display::display_footer();

+ 34 - 11
main/newscorm/lp_controller.php

@@ -246,34 +246,38 @@ switch ($action) {
         break;
 
     case 'add_lp':
-
         if (!$is_allowed_to_edit) {
             api_not_allowed(true);
         }
-
         if ($debug > 0) error_log('New LP - add_lp action triggered', 0);
-
-        $_REQUEST['learnpath_name'] = trim($_REQUEST['learnpath_name']);
-
-        if (!empty($_REQUEST['learnpath_name'])) {
+        $_REQUEST['lp_name'] = trim($_REQUEST['lp_name']);
+        if (!empty($_REQUEST['lp_name'])) {
             $_SESSION['refresh'] = 1;
 
             if (isset($_SESSION['post_time']) && $_SESSION['post_time'] == $_REQUEST['post_time']) {
                 require 'lp_add.php';
             } else {
                 $_SESSION['post_time'] = $_REQUEST['post_time'];
-
+                
+                if (!$_REQUEST['enabletimelimit']) {                	
+                	$_REQUEST['publicated_on'] = null;
+                    $_REQUEST['expired_on'] = null;
+                } else {
+                    $publicated_on  = $_REQUEST['publicated_on'];
+                    $publicated_on  = $publicated_on['Y'].'-'.$publicated_on['F'].'-'.$publicated_on['d'].' '.$publicated_on['H'].':'.$publicated_on['i'].':00';
+                    
+                    $expired_on   = $_REQUEST['expired_on'];
+                    $expired_on   = $expired_on['Y'].'-'.$expired_on['F'].'-'.$expired_on['d'].' '.$expired_on['H'].':'.$expired_on['i'].':00';
+                }
                 // Kevin Van Den Haute: Changed $_REQUEST['learnpath_description'] by '' because it's not used.
                 //old->$new_lp_id = learnpath::add_lp(api_get_course_id(), $_REQUEST['learnpath_name'], $_REQUEST['learnpath_description'], 'dokeos', 'manual', '');
-                $new_lp_id = learnpath::add_lp(api_get_course_id(), Security::remove_XSS($_REQUEST['learnpath_name']), '', 'dokeos', 'manual', '');
+                $new_lp_id = learnpath::add_lp(api_get_course_id(), Security::remove_XSS($_REQUEST['lp_name']), '', 'chamilo', 'manual', '', $publicated_on, $expired_on);
                 //learnpath::toggle_visibility($new_lp_id,'v');
                 // Kevin Van Den Haute: Only go further if learnpath::add_lp has returned an id.
                 if (is_numeric($new_lp_id)) {
                     // TODO: Maybe create a first module directly to avoid bugging the user with useless queries
                     $_SESSION['oLP'] = new learnpath(api_get_course_id(),$new_lp_id,api_get_user_id());
-
                     //$_SESSION['oLP']->add_item(0, -1, 'dokeos_chapter', $_REQUEST['path'], 'Default');
-
                     require 'lp_build.php';
                 }
             }
@@ -577,7 +581,26 @@ switch ($action) {
             $_SESSION['oLP']->set_proximity($_REQUEST['lp_proximity']);
             $_SESSION['oLP']->set_theme($_REQUEST['lp_theme']);
             $_SESSION['oLP']->set_prerequisite($_REQUEST['prerequisites']);
-            $_SESSION['oLP']->set_use_max_score($_REQUEST['use_max_score']);
+            $_SESSION['oLP']->set_use_max_score($_REQUEST['use_max_score']);            
+                   
+            if (!$_REQUEST['enabletimelimit']) {
+                //there will be always a publication date                   
+                $publicated_on  = $_REQUEST['publicated_on'];
+                $publicated_on  = $publicated_on['Y'].'-'.$publicated_on['F'].'-'.$publicated_on['d'].' '.$publicated_on['H'].':'.$publicated_on['i'].':00';
+                
+                $expired_on = null;
+            } else {
+                $publicated_on  = $_REQUEST['publicated_on'];
+                $publicated_on  = $publicated_on['Y'].'-'.$publicated_on['F'].'-'.$publicated_on['d'].' '.$publicated_on['H'].':'.$publicated_on['i'].':00';
+                
+                $expired_on   = $_REQUEST['expired_on'];
+                $expired_on   = $expired_on['Y'].'-'.$expired_on['F'].'-'.$expired_on['d'].' '.$expired_on['H'].':'.$expired_on['i'].':00';
+            }            
+            
+            $_SESSION['oLP']->set_modified_on();
+            $_SESSION['oLP']->set_publicated_on($publicated_on);                        
+            $_SESSION['oLP']->set_expired_on($expired_on);
+            
 
             if ($_REQUEST['remove_picture']) {
                 $_SESSION['oLP']->delete_lp_image();

+ 36 - 3
main/newscorm/lp_edit.php

@@ -31,6 +31,20 @@ if (!empty($gradebook) && $gradebook == 'view') {
 $interbreadcrumb[] = array('url' => 'lp_controller.php?action=list', 'name' => get_lang('_learning_path'));
 $interbreadcrumb[] = array('url' => api_get_self()."?action=admin_view&lp_id=$learnpath_id", 'name' => $_SESSION['oLP']->get_name());
 
+$htmlHeadXtra[] = '<script type="text/javascript">
+        
+    function timelimit() {
+        if(document.getElementById(\'options2\').style.display == \'none\')
+        {
+            document.getElementById(\'options2\').style.display = \'block\';
+        } else {
+            document.getElementById(\'options2\').style.display = \'none\';
+        }
+    }
+     
+</script>';
+
+
 Display::display_header(null, 'Path');
 
 // Action links
@@ -129,9 +143,7 @@ if (strlen($_SESSION['oLP']->get_preview_image()) > 0) {
 }
 
 $form->addElement('file', 'lp_preview_image', ($_SESSION['oLP']->get_preview_image() != '' ? get_lang('UpdateImage') : get_lang('AddImage')));
-
 $form->addElement('static', null, null, get_lang('ImageWillResizeMsg'));
-
 $form->addRule('lp_preview_image', get_lang('OnlyImagesAllowed'), 'filetype', array ('jpg', 'jpeg', 'png', 'gif'));
 
 // Search terms (only if search is activated).
@@ -162,10 +174,30 @@ $defaults['lp_encoding']    = Security::remove_XSS($_SESSION['oLP']->encoding);
 $defaults['lp_name']        = Security::remove_XSS($_SESSION['oLP']->get_name());
 $defaults['lp_author']      = Security::remove_XSS($_SESSION['oLP']->get_author());
 
+$expired_on     = $_SESSION['oLP'] ->expired_on;
+$publicated_on  = $_SESSION['oLP'] ->publicated_on;
+
 // Prerequisites
 $form->addElement('html', '<div class="row"><div class="label">'.get_lang('Prerequisites').'</div><div class="formw">'.$_SESSION['oLP']->display_lp_prerequisites_list().'</div></div>');
 $form->addElement('static', null, null, get_lang('LpPrerequisiteDescription'));
 
+$form->addElement('checkbox', 'enabletimelimit',get_lang('EnableTimeLimits'),null,'onclick = "  return timelimit() "');
+
+if( ($publicated_on!='0000-00-00 00:00:00' && !empty($publicated_on)) && ( $expired_on!='0000-00-00 00:00:00' && !empty($expired_on))  )
+    $defaults['enabletimelimit'] = 1;
+    
+$display_date = 'none';   
+if ($defaults['enabletimelimit'] ) {
+    $display_date = 'block';
+}
+
+
+$form->addElement('html','<div id="options2" style="display:'.$display_date.';">');
+
+$form->addElement('datepicker', 'publicated_on', get_lang('PublicationDate'), array('form_name'=>'exercise_admin'), 5);
+$form->addElement('datepicker', 'expired_on', get_lang('ExpirationDate'), array('form_name'=>'exercise_admin'), 5);
+
+$form->addElement('html','</div>');            
 
 if (api_is_platform_admin()) {
     $form->addElement('checkbox', 'use_max_score', get_lang('UseMaxScore100'));
@@ -181,7 +213,8 @@ $form->addElement('hidden', 'action', 'update_lp');
 $form->addElement('hidden', 'lp_id', $_SESSION['oLP']->get_id());
 
 
-
+$defaults['publicated_on']  = ($publicated_on!='0000-00-00 00:00:00' && !empty($publicated_on))? $publicated_on : date('Y-m-d 12:00:00');
+$defaults['expired_on']     = ($expired_on   !='0000-00-00 00:00:00' && !empty($expired_on) )? $expired_on : date('Y-m-d 12:00:00',time()+84600);
 
 $form->setDefaults($defaults);
 echo '<table><tr><td width="550px">';