Ver código fonte

create the class notebook.lib.php in main/inc/lib and move all the functions of main/notebook/index.php to this class see CT#191

Carlos Vargas 15 anos atrás
pai
commit
85f5ac38f6
2 arquivos alterados com 227 adições e 208 exclusões
  1. 212 0
      main/inc/lib/notebook.lib.php
  2. 15 208
      main/notebook/index.php

+ 212 - 0
main/inc/lib/notebook.lib.php

@@ -0,0 +1,212 @@
+<?php
+/* For licensing terms, see /chamilo_license.txt */
+
+/**
+==============================================================================
+*	This class provides methods for the notebook management.
+*	Include/require it in your code to use its features.
+*	@author Carlos Vargas <litox84@gmail.com>, move code of main/notebook up here
+*	@package chamilo.library
+==============================================================================
+*/
+
+class NotebookManager
+{
+	private function __construct() {
+
+	}
+	/**
+	 * a little bit of javascript to display a prettier warning when deleting a note
+	 *
+	 * @return unknown
+	 *
+	 * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
+	 * @version januari 2009, dokeos 1.8.6
+	 */
+	function javascript_notebook()
+	{
+		return "<script type=\"text/javascript\">
+				function confirmation (name)
+				{
+					if (confirm(\" ". get_lang("NoteConfirmDelete") ." \"+ name + \" ?\"))
+						{return true;}
+					else
+						{return false;}
+				}
+				</script>";
+	}
+	
+	/**
+	 * This functions stores the note in the database
+	 *
+	 * @param array $values
+	 *
+	 * @author Christian Fasanando <christian.fasanando@dokeos.com>
+	 * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
+	 * @version januari 2009, dokeos 1.8.6
+	 */
+	function save_note($values) {
+		// Database table definition
+		$t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
+	
+		$sql = "INSERT INTO $t_notebook (user_id, course, session_id, title, description, creation_date,update_date,status)
+				VALUES(
+					'".Database::escape_string(api_get_user_id())."',
+					'".Database::escape_string(api_get_course_id())."',
+					'".Database::escape_string($_SESSION['id_session'])."',
+					'".Database::escape_string(Security::remove_XSS($values['note_title']))."',
+					'".Database::escape_string(Security::remove_XSS(stripslashes(api_html_entity_decode($values['note_comment'])),COURSEMANAGERLOWSECURITY))."',
+					'".Database::escape_string(date('Y-m-d H:i:s'))."',
+					'".Database::escape_string(date('Y-m-d H:i:s'))."',
+					'0')";
+		$result = Database::query($sql, __FILE__, __LINE__);
+		$id = Database::insert_id();
+		if ($id > 0) {
+			//insert into item_property
+			api_item_property_update(api_get_course_info(), TOOL_NOTEBOOK, $id, 'NotebookAdded', api_get_user_id());
+		}
+	
+		// display the feedback message
+		Display::display_confirmation_message(get_lang('NoteAdded'));
+	}
+	
+	function get_note_information($notebook_id) {
+		// Database table definition
+		$t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
+	
+		$sql = "SELECT 	notebook_id 		AS notebook_id,
+						title				AS note_title,
+						description 		AS note_comment,
+				   		session_id			AS session_id
+				   FROM $t_notebook
+				   WHERE notebook_id = '".Database::escape_string($notebook_id)."' ";
+		$result = Database::query($sql, __FILE__, __LINE__);
+		return Database::fetch_array($result);
+	}
+	
+	/**
+	 * This functions updates the note in the database
+	 *
+	 * @param array $values
+	 *
+	 * @author Christian Fasanando <christian.fasanando@dokeos.com>
+	 * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
+	 * @version januari 2009, dokeos 1.8.6
+	 */
+	function update_note($values) {
+		// Database table definition
+		$t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
+	
+		$sql = "UPDATE $t_notebook SET
+					user_id = '".Database::escape_string(api_get_user_id())."',
+					course = '".Database::escape_string(api_get_course_id())."',
+					session_id = '".Database::escape_string($_SESSION['id_session'])."',
+					title = '".Database::escape_string(Security::remove_XSS($values['note_title']))."',
+					description = '".Database::escape_string(Security::remove_XSS(stripslashes(api_html_entity_decode($values['note_comment'])),COURSEMANAGERLOWSECURITY))."',
+					update_date = '".Database::escape_string(date('Y-m-d H:i:s'))."'
+				WHERE notebook_id = '".Database::escape_string($values['notebook_id'])."'";
+		$result = Database::query($sql, __FILE__, __LINE__);
+	
+		//update item_property (update)
+		api_item_property_update(api_get_course_info(), TOOL_NOTEBOOK, Database::escape_string($values['notebook_id']), 'NotebookUpdated', api_get_user_id());
+	
+		// display the feedback message
+		Display::display_confirmation_message(get_lang('NoteUpdated'));
+	}
+	
+	function delete_note($notebook_id) {
+		// Database table definition
+		$t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
+	
+		$sql = "DELETE FROM $t_notebook WHERE notebook_id='".Database::escape_string($notebook_id)."' AND user_id = '".Database::escape_string(api_get_user_id())."'";
+		$result = Database::query($sql, __FILE__, __LINE__);
+	
+		//update item_property (delete)
+		api_item_property_update(api_get_course_info(), TOOL_NOTEBOOK, Database::escape_string($notebook_id), 'delete', api_get_user_id());
+	
+		Display::display_confirmation_message(get_lang('NoteDeleted'));
+	}
+	
+	function display_notes() {
+	
+		global $_user;
+	
+		if (!$_GET['direction'])
+		{
+			$sort_direction = 'ASC';
+			$link_sort_direction = 'DESC';
+		}
+		elseif ($_GET['direction'] == 'ASC')
+		{
+			$sort_direction = 'ASC';
+			$link_sort_direction = 'DESC';
+		}
+		else
+		{
+			$sort_direction = 'DESC';
+			$link_sort_direction = 'ASC';
+		}
+	
+	
+		// action links
+		echo '<div class="actions" style="margin-bottom:20px">';
+		//if (api_is_allowed_to_edit())
+		//{
+			if (!api_is_anonymous()) {
+				if (api_get_session_id()==0)
+					echo '<a href="index.php?'.api_get_cidreq().'&amp;action=addnote">'.Display::return_icon('filenew.gif',get_lang('NoteAddNew')).get_lang('NoteAddNew').'</a>';
+				elseif(api_is_allowed_to_session_edit(false,true)){
+					echo '<a href="index.php?'.api_get_cidreq().'&amp;action=addnote">'.Display::return_icon('filenew.gif',get_lang('NoteAddNew')).get_lang('NoteAddNew').'</a>';
+				}
+	
+			} else {
+				echo '<a href="javascript:void(0)">'.Display::return_icon('filenew.gif',get_lang('NoteAddNew')).get_lang('NoteAddNew').'</a>';
+			}
+		//}
+		echo '<a href="index.php?'.api_get_cidreq().'&amp;action=changeview&amp;view=creation_date&amp;direction='.$link_sort_direction.'">'.Display::return_icon('calendar_select.gif',get_lang('OrderByCreationDate')).get_lang('OrderByCreationDate').'</a>';
+		echo '<a href="index.php?'.api_get_cidreq().'&amp;action=changeview&amp;view=update_date&amp;direction='.$link_sort_direction.'">'.Display::return_icon('calendar_select.gif',get_lang('OrderByModificationDate')).get_lang('OrderByModificationDate').'</a>';
+		echo '<a href="index.php?'.api_get_cidreq().'&amp;action=changeview&amp;view=title&amp;direction='.$link_sort_direction.'">'.Display::return_icon('comment.gif',get_lang('OrderByTitle')).get_lang('OrderByTitle').'</a>';
+		echo '</div>';
+	
+		if (!in_array($_SESSION['notebook_view'],array('creation_date','update_date', 'title'))) {
+			$_SESSION['notebook_view'] = 'creation_date';
+		}
+	
+		// Database table definition
+		$t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
+		$order_by = "";
+		if ($_SESSION['notebook_view'] == 'creation_date' || $_SESSION['notebook_view'] == 'update_date') {
+			$order_by = " ORDER BY ".$_SESSION['notebook_view']." $sort_direction ";
+		} else {
+			$order_by = " ORDER BY ".$_SESSION['notebook_view']." $sort_direction ";
+		}
+	
+		//condition for the session
+		$session_id = api_get_session_id();
+		$condition_session = api_get_session_condition($session_id);
+	
+		$cond_extra = ($_SESSION['notebook_view']== 'update_date')?" AND update_date <> '0000-00-00 00:00:00'":" ";
+	
+		$sql = "SELECT * FROM $t_notebook WHERE user_id = '".Database::escape_string(api_get_user_id())."' $condition_session $cond_extra $order_by";
+		$result = Database::query($sql, __FILE__, __LINE__);
+		while ($row = Database::fetch_array($result)) {
+			//validacion when belongs to a session
+			$session_img = api_get_session_image($row['session_id'], $_user['status']);
+			echo '<div class="sectiontitle">';
+			echo '<span style="float: right;"> ('.get_lang('CreationDate').': '.date_to_str_ago($row['creation_date']).'&nbsp;&nbsp;<span class="dropbox_date">'.$row['creation_date'].'</span>';
+			if ($row['update_date'] <> $row['creation_date']) {
+				echo ', '.get_lang('UpdateDate').': '.date_to_str_ago($row['update_date']).'&nbsp;&nbsp;<span class="dropbox_date">'.$row['update_date'].'</span>';
+			}
+			echo ')</span>';
+			echo $row['title'] . $session_img;
+			echo '</div>';
+			echo '<div class="sectioncomment">'.$row['description'].'</div>';
+			echo '<div>';
+			echo '<a href="'.api_get_self().'?action=editnote&amp;notebook_id='.$row['notebook_id'].'">'.Display::return_icon('edit.gif', get_lang('Edit')).'</a>';
+			echo '<a href="'.api_get_self().'?action=deletenote&amp;notebook_id='.$row['notebook_id'].'" onclick="return confirmation(\''.$row['title'].'\');">'.Display::return_icon('delete.gif', get_lang('Delete')).'</a>';
+			echo '</div>';
+		}
+		//return $return;
+	}
+}
+?>

+ 15 - 208
main/notebook/index.php

@@ -11,8 +11,9 @@
 // name of the language file that needs to be included
 $language_file = array('notebook');
 
-// including the global dokeos file
+// including the global dokeos fileç
 require_once '../inc/global.inc.php';
+require_once api_get_path(LIBRARY_PATH).'notebook.lib.php';
 
 // the section (tabs)
 $this_section=SECTION_COURSES;
@@ -25,7 +26,7 @@ api_protect_course_script(true);
 require_once api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php';
 
 // additional javascript
-$htmlHeadXtra[] = javascript_notebook();
+$htmlHeadXtra[] = NotebookManager::javascript_notebook();
 $htmlHeadXtra[] = '<script src="'.api_get_path(WEB_LIBRARY_PATH).'javascript/jquery.js" type="text/javascript" language="javascript"></script>'; //jQuery
 $htmlHeadXtra[] = '<script type="text/javascript">
 function setFocus(){
@@ -70,7 +71,7 @@ if (isset($_GET['action']) && $_GET['action'] == 'addnote')
 	}
 
 	if (!empty($_GET['isStudentView'])) {
-		display_notes();
+		NotebookManager::display_notes();
 		exit;
 	}
 
@@ -98,11 +99,11 @@ if (isset($_GET['action']) && $_GET['action'] == 'addnote')
 		if ($check)
 		{
 	   		$values = $form->exportValues();
-	   		save_note($values);
+	   		NotebookManager::save_note($values);
 
 		}
 		Security::clear_token();
-		display_notes();
+		NotebookManager::display_notes();
 	}
 	else
 	{
@@ -121,7 +122,7 @@ else if (isset($_GET['action']) && $_GET['action'] == 'editnote' && is_numeric($
 {
 
 	if (!empty($_GET['isStudentView'])) {
-		display_notes();
+		NotebookManager::display_notes();
 		exit;
 	}
 
@@ -139,7 +140,7 @@ else if (isset($_GET['action']) && $_GET['action'] == 'editnote' && is_numeric($
 	$form->addElement('style_submit_button', 'SubmitNote', get_lang('ModifyNote'), 'class="save"');
 
 	// setting the defaults
-	$defaults = get_note_information(Security::remove_XSS($_GET['notebook_id']));
+	$defaults = NotebookManager::get_note_information(Security::remove_XSS($_GET['notebook_id']));
 	$form->setDefaults($defaults);
 
 	// setting the rules
@@ -152,10 +153,10 @@ else if (isset($_GET['action']) && $_GET['action'] == 'editnote' && is_numeric($
 		if ($check)
 		{
 	   		$values = $form->exportValues();
-	   		update_note($values);
+	   		NotebookManager::update_note($values);
 		}
 		Security::clear_token();
-		display_notes();
+		NotebookManager::display_notes();
 	}
 	else
 	{
@@ -172,8 +173,8 @@ else if (isset($_GET['action']) && $_GET['action'] == 'editnote' && is_numeric($
 // Action handling: deleting a note
 else if (isset($_GET['action']) && $_GET['action'] == 'deletenote' && is_numeric($_GET['notebook_id']))
 {
-	delete_note(Security::remove_XSS($_GET['notebook_id']));
-	display_notes();
+	NotebookManager::delete_note(Security::remove_XSS($_GET['notebook_id']));
+	NotebookManager::display_notes();
 }
 
 // Action handling: changing the view (sorting order)
@@ -213,206 +214,12 @@ else if ($_GET['action'] == 'changeview' AND in_array($_GET['view'],array('creat
 			break;
 	}
 	$_SESSION['notebook_view'] = $_GET['view'];
-	display_notes();
+	NotebookManager::display_notes();
 } else {
-	display_notes();
+	NotebookManager::display_notes();
 }
 
 
 // footer
 Display::display_footer();
-
-/**
- * a little bit of javascript to display a prettier warning when deleting a note
- *
- * @return unknown
- *
- * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
- * @version januari 2009, dokeos 1.8.6
- */
-function javascript_notebook()
-{
-	return "<script type=\"text/javascript\">
-			function confirmation (name)
-			{
-				if (confirm(\" ". get_lang("NoteConfirmDelete") ." \"+ name + \" ?\"))
-					{return true;}
-				else
-					{return false;}
-			}
-			</script>";
-}
-
-/**
- * This functions stores the note in the database
- *
- * @param array $values
- *
- * @author Christian Fasanando <christian.fasanando@dokeos.com>
- * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
- * @version januari 2009, dokeos 1.8.6
- */
-function save_note($values) {
-	// Database table definition
-	$t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
-
-	$sql = "INSERT INTO $t_notebook (user_id, course, session_id, title, description, creation_date,update_date,status)
-			VALUES(
-				'".Database::escape_string(api_get_user_id())."',
-				'".Database::escape_string(api_get_course_id())."',
-				'".Database::escape_string($_SESSION['id_session'])."',
-				'".Database::escape_string(Security::remove_XSS($values['note_title']))."',
-				'".Database::escape_string(Security::remove_XSS(stripslashes(api_html_entity_decode($values['note_comment'])),COURSEMANAGERLOWSECURITY))."',
-				'".Database::escape_string(date('Y-m-d H:i:s'))."',
-				'".Database::escape_string(date('Y-m-d H:i:s'))."',
-				'0')";
-	$result = Database::query($sql, __FILE__, __LINE__);
-	$id = Database::insert_id();
-	if ($id > 0) {
-		//insert into item_property
-		api_item_property_update(api_get_course_info(), TOOL_NOTEBOOK, $id, 'NotebookAdded', api_get_user_id());
-	}
-
-	// display the feedback message
-	Display::display_confirmation_message(get_lang('NoteAdded'));
-}
-
-function get_note_information($notebook_id) {
-	// Database table definition
-	$t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
-
-	$sql = "SELECT 	notebook_id 		AS notebook_id,
-					title				AS note_title,
-					description 		AS note_comment,
-			   		session_id			AS session_id
-			   FROM $t_notebook
-			   WHERE notebook_id = '".Database::escape_string($notebook_id)."' ";
-	$result = Database::query($sql, __FILE__, __LINE__);
-	return Database::fetch_array($result);
-}
-
-/**
- * This functions updates the note in the database
- *
- * @param array $values
- *
- * @author Christian Fasanando <christian.fasanando@dokeos.com>
- * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
- * @version januari 2009, dokeos 1.8.6
- */
-function update_note($values) {
-	// Database table definition
-	$t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
-
-	$sql = "UPDATE $t_notebook SET
-				user_id = '".Database::escape_string(api_get_user_id())."',
-				course = '".Database::escape_string(api_get_course_id())."',
-				session_id = '".Database::escape_string($_SESSION['id_session'])."',
-				title = '".Database::escape_string(Security::remove_XSS($values['note_title']))."',
-				description = '".Database::escape_string(Security::remove_XSS(stripslashes(api_html_entity_decode($values['note_comment'])),COURSEMANAGERLOWSECURITY))."',
-				update_date = '".Database::escape_string(date('Y-m-d H:i:s'))."'
-			WHERE notebook_id = '".Database::escape_string($values['notebook_id'])."'";
-	$result = Database::query($sql, __FILE__, __LINE__);
-
-	//update item_property (update)
-	api_item_property_update(api_get_course_info(), TOOL_NOTEBOOK, Database::escape_string($values['notebook_id']), 'NotebookUpdated', api_get_user_id());
-
-	// display the feedback message
-	Display::display_confirmation_message(get_lang('NoteUpdated'));
-}
-
-function delete_note($notebook_id) {
-	// Database table definition
-	$t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
-
-	$sql = "DELETE FROM $t_notebook WHERE notebook_id='".Database::escape_string($notebook_id)."' AND user_id = '".Database::escape_string(api_get_user_id())."'";
-	$result = Database::query($sql, __FILE__, __LINE__);
-
-	//update item_property (delete)
-	api_item_property_update(api_get_course_info(), TOOL_NOTEBOOK, Database::escape_string($notebook_id), 'delete', api_get_user_id());
-
-	Display::display_confirmation_message(get_lang('NoteDeleted'));
-}
-
-function display_notes() {
-
-	global $_user;
-
-	if (!$_GET['direction'])
-	{
-		$sort_direction = 'ASC';
-		$link_sort_direction = 'DESC';
-	}
-	elseif ($_GET['direction'] == 'ASC')
-	{
-		$sort_direction = 'ASC';
-		$link_sort_direction = 'DESC';
-	}
-	else
-	{
-		$sort_direction = 'DESC';
-		$link_sort_direction = 'ASC';
-	}
-
-
-	// action links
-	echo '<div class="actions" style="margin-bottom:20px">';
-	//if (api_is_allowed_to_edit())
-	//{
-		if (!api_is_anonymous()) {
-			if (api_get_session_id()==0)
-				echo '<a href="index.php?'.api_get_cidreq().'&amp;action=addnote">'.Display::return_icon('filenew.gif',get_lang('NoteAddNew')).get_lang('NoteAddNew').'</a>';
-			elseif(api_is_allowed_to_session_edit(false,true)){
-				echo '<a href="index.php?'.api_get_cidreq().'&amp;action=addnote">'.Display::return_icon('filenew.gif',get_lang('NoteAddNew')).get_lang('NoteAddNew').'</a>';
-			}
-
-		} else {
-			echo '<a href="javascript:void(0)">'.Display::return_icon('filenew.gif',get_lang('NoteAddNew')).get_lang('NoteAddNew').'</a>';
-		}
-	//}
-	echo '<a href="index.php?'.api_get_cidreq().'&amp;action=changeview&amp;view=creation_date&amp;direction='.$link_sort_direction.'">'.Display::return_icon('calendar_select.gif',get_lang('OrderByCreationDate')).get_lang('OrderByCreationDate').'</a>';
-	echo '<a href="index.php?'.api_get_cidreq().'&amp;action=changeview&amp;view=update_date&amp;direction='.$link_sort_direction.'">'.Display::return_icon('calendar_select.gif',get_lang('OrderByModificationDate')).get_lang('OrderByModificationDate').'</a>';
-	echo '<a href="index.php?'.api_get_cidreq().'&amp;action=changeview&amp;view=title&amp;direction='.$link_sort_direction.'">'.Display::return_icon('comment.gif',get_lang('OrderByTitle')).get_lang('OrderByTitle').'</a>';
-	echo '</div>';
-
-	if (!in_array($_SESSION['notebook_view'],array('creation_date','update_date', 'title'))) {
-		$_SESSION['notebook_view'] = 'creation_date';
-	}
-
-	// Database table definition
-	$t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
-	$order_by = "";
-	if ($_SESSION['notebook_view'] == 'creation_date' || $_SESSION['notebook_view'] == 'update_date') {
-		$order_by = " ORDER BY ".$_SESSION['notebook_view']." $sort_direction ";
-	} else {
-		$order_by = " ORDER BY ".$_SESSION['notebook_view']." $sort_direction ";
-	}
-
-	//condition for the session
-	$session_id = api_get_session_id();
-	$condition_session = api_get_session_condition($session_id);
-
-	$cond_extra = ($_SESSION['notebook_view']== 'update_date')?" AND update_date <> '0000-00-00 00:00:00'":" ";
-
-	$sql = "SELECT * FROM $t_notebook WHERE user_id = '".Database::escape_string(api_get_user_id())."' $condition_session $cond_extra $order_by";
-	$result = Database::query($sql, __FILE__, __LINE__);
-	while ($row = Database::fetch_array($result)) {
-		//validacion when belongs to a session
-		$session_img = api_get_session_image($row['session_id'], $_user['status']);
-		echo '<div class="sectiontitle">';
-		echo '<span style="float: right;"> ('.get_lang('CreationDate').': '.date_to_str_ago($row['creation_date']).'&nbsp;&nbsp;<span class="dropbox_date">'.$row['creation_date'].'</span>';
-		if ($row['update_date'] <> $row['creation_date']) {
-			echo ', '.get_lang('UpdateDate').': '.date_to_str_ago($row['update_date']).'&nbsp;&nbsp;<span class="dropbox_date">'.$row['update_date'].'</span>';
-		}
-		echo ')</span>';
-		echo $row['title'] . $session_img;
-		echo '</div>';
-		echo '<div class="sectioncomment">'.$row['description'].'</div>';
-		echo '<div>';
-		echo '<a href="'.api_get_self().'?action=editnote&amp;notebook_id='.$row['notebook_id'].'">'.Display::return_icon('edit.gif', get_lang('Edit')).'</a>';
-		echo '<a href="'.api_get_self().'?action=deletenote&amp;notebook_id='.$row['notebook_id'].'" onclick="return confirmation(\''.$row['title'].'\');">'.Display::return_icon('delete.gif', get_lang('Delete')).'</a>';
-		echo '</div>';
-	}
-	//return $return;
-}
-?>
+?>