Browse Source

[svn r18955] * allow defining of platform templates
* refactoring and cleaning up of fckeditor/fcktemplates.xml.php
* improved performance of user templates

Patrick Cool 16 years ago
parent
commit
3620474df3
2 changed files with 431 additions and 46 deletions
  1. 285 2
      main/admin/settings.php
  2. 146 44
      main/inc/lib/fckeditor/fcktemplates.xml.php

+ 285 - 2
main/admin/settings.php

@@ -1,4 +1,4 @@
-<?php // $Id: settings.php 18264 2009-02-05 21:23:18Z juliomontoya $
+<?php // $Id: settings.php 18955 2009-03-11 10:45:48Z pcool $
 /*
 ==============================================================================
 	Dokeos - elearning and course management software
@@ -335,6 +335,9 @@ if (isset ($_GET['category']))
         case 'Search' :
             handle_search();
             break;
+		case 'Templates' :
+			handle_templates();
+			break;            
 		default :
 			$form->display();
 	}
@@ -557,7 +560,7 @@ function handle_stylesheets()
 	$allowed_file_types = array ('css');
 	$form->addRule('new_stylesheet', get_lang('InvalidExtension').' ('.implode(',', $allowed_file_types).')', 'filetype', $allowed_file_types);
 	$form->addRule('new_stylesheet', get_lang('ThisFieldIsRequired'), 'required');
-	$form->addElement('style_submit_button', 'stylesheet_upload', get_lang('Ok'));
+	$form->addElement('style_submit_button', 'stylesheet_upload', get_lang('Ok'), array('class'=>'save'));
 	if( $form->validate() AND is_writable(api_get_path(SYS_CODE_PATH).'css/'))
 	{
 		$values = $form->exportValues();
@@ -840,4 +843,284 @@ function handle_search() {
     }
 }
 
+/**
+ * wrapper for the templates
+ * 
+ * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
+ * @version August 2008
+ * @since Dokeos 1.8.6
+ */
+function handle_templates()
+{
+	if ($_GET['action'] == 'add' OR ( $_GET['action'] == 'edit' AND is_numeric($_GET['id'])))
+	{
+		add_edit_template();
+	}
+	else 
+	{
+		if ($_GET['action'] == 'delete' and is_numeric($_GET['id']))
+		{
+			delete_template($_GET['id']);
+		}
+		echo '<a href="settings.php?category=Templates&amp;action=add">'.get_lang('AddTemplate').'</a>';
+		display_templates();
+	}
+}
+
+/**
+ * Display a sortable table with all the templates that the platform administrator has defined.
+ *
+ * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
+ * @version August 2008
+ * @since Dokeos 1.8.6
+ */
+function display_templates()
+{
+	$table = new SortableTable('templates', 'get_number_of_templates', 'get_template_data',1);
+	$table->set_additional_parameters(array('category'=>$_GET['category']));
+	$table->set_header(0, get_lang('Image'));
+	$table->set_header(1, get_lang('Title'));
+	$table->set_header(2, get_lang('Actions'));	
+	$table->set_column_filter(2,'actions_filter');
+	$table->set_column_filter(0,'image_filter');
+	$table->display();	
+}
+
+/**
+ * Get the number of templates that are defined by the platform admin. 
+ *
+ * @return integer 
+ * 
+ * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
+ * @version August 2008
+ * @since Dokeos 1.8.6
+ */
+function get_number_of_templates()
+{
+	// Database table definition
+	$table_system_template = Database :: get_main_table('system_template');
+	
+	// The sql statement
+	$sql = "SELECT COUNT(id) AS total FROM $table_system_template";
+	$result = api_sql_query($sql, __FILE__, __LINE__);
+	$row = Database::fetch_array($result);
+	
+	// returning the number of templates
+	return $row['total'];
+}
+
+/**
+ * Get all the template data for the sortable table
+ *
+ * @param integer $from the start of the limit statement
+ * @param integer $number_of_items the number of elements that have to be retrieved from the database
+ * @param integer $column the column that is 
+ * @param string $direction the sorting direction (ASC or DESCà
+ * @return array 
+ * 
+ * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
+ * @version August 2008
+ * @since Dokeos 1.8.6
+ */
+function get_template_data($from, $number_of_items, $column, $direction)
+{
+	// Database table definition
+	$table_system_template = Database :: get_main_table('system_template');
+	
+	// the sql statement
+	$sql = "SELECT image as col0, title as col1, id as col2 FROM $table_system_template";
+	$sql .= " ORDER BY col$column $direction ";
+	$sql .= " LIMIT $from,$number_of_items";	
+	$result = api_sql_query($sql, __FILE__, __LINE__);
+	while ($row = Database::fetch_array($result))
+	{
+		$return[]=$row;
+	}
+	
+	// returning all the information for the sortable table
+	return $return;
+}
+
+/**
+ * display the edit and delete icons in the sortable table
+ *
+ * @param integer $id the id of the template
+ * @return html code for the link to edit and delete the template
+ * 
+ * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
+ * @version August 2008
+ * @since Dokeos 1.8.6
+ */
+function actions_filter($id)
+{
+	$return .= '<a href="settings.php?category=Templates&amp;action=edit&amp;id='.Security::remove_XSS($id).'">'.Display::return_icon('edit.gif').'</a>';
+	$return .= '<a href="settings.php?category=Templates&amp;action=delete&amp;id='.Security::remove_XSS($id).'" onclick="javascript:if(!confirm('."'".get_lang("ConfirmYourChoice")."'".')) return false;">'.Display::return_icon('delete.gif').'</a>';
+	return $return;
+}
+
+/**
+ * Display the image of the template in the sortable table
+ *
+ * @param string $image the image
+ * @return html code for the image
+ * 
+ * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
+ * @version August 2008
+ * @since Dokeos 1.8.6
+ */
+function image_filter($image)
+{
+	return '<img src="'.api_get_path(WEB_PATH).'home/'.$image.'" alt="'.$image.'"/>';
+}
+
+/**
+ * Add (or edit) a template. This function displays the form and also takes care of uploading the image and storing the information in the database
+ *
+ * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
+ * @version August 2008
+ * @since Dokeos 1.8.6
+ */
+function add_edit_template()
+{
+	// initiate the object
+	$form = new FormValidator('template', 'post', 'settings.php?category=Templates&action='.$_GET['action'].'&id='.$_GET['id']);
+	
+	// settting the form elements: the header
+	if ($_GET['action'] == 'add')
+	{
+		$title = get_lang('AddTemplate');
+	}
+	else 
+	{
+		$title = get_lang('EditTemplate');
+	}
+	$form->addElement('header', '', $title);
+	
+	// settting the form elements: the title of the template
+	$form->add_textfield('title', get_lang('Title'), false);
+	
+	// settting the form elements: the content of the template (wysiwyg editor)
+	$form->addElement('html_editor', 'template_text', get_lang('Text'));
+	
+	// settting the form elements: the form to upload an image to be used with the template
+	$form->addElement('file','template_image',get_lang('Image'),'');
+	
+	// settting the form elements: a little bit information about the template image
+	$form->addElement('static', 'file_comment', '', get_lang('TemplateImageComment100x70'));
+	
+	// getting all the information of the template when editing a template 
+	if ($_GET['action'] == 'edit')
+	{
+		// Database table definition		
+		$table_system_template = Database :: get_main_table('system_template');
+		$sql = "SELECT * FROM $table_system_template WHERE id = '".Database::escape_string($_GET['id'])."'";
+		$result = api_sql_query($sql, __FILE__, __LINE__);
+		$row = Database::fetch_array($result);
+		
+		$defaults['template_id'] 	= $_GET['id'];
+		$defaults['template_text'] 	= $row['content'];
+		$defaults['title'] 			= $row['title'];
+		
+		// adding an extra field: a hidden field with the id of the template we are editing
+		$form->addElement('hidden','template_id');
+		
+		// adding an extrra field: a preview of the image that is currently used
+		$form->addElement('static','template_image_preview', '', '<img src="'.api_get_path(WEB_PATH).'home/'.$row['image'].'" alt="'.$row['image'].'"/>');
+		
+		// setting the information of the template that we are editing 
+		$form->setDefaults($defaults);
+	}	
+		// settting the form elements: the submit button
+	$form->addElement('submit', 'submit', get_lang('Ok'));
+	
+	// setting the rules: the required fields
+	$form->addRule('title', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');	
+	$form->addRule('template_text', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');	
+	
+	// if the form validates (complies to all rules) we save the information, else we display the form again (with error message if needed)
+	if( $form->validate() )
+	{
+		// exporting the values
+		$values = $form->exportValues();
+		
+		// upload the file
+		if (!empty($_FILES['template_image']['name']))
+		{
+			include_once (api_get_path(LIBRARY_PATH).'fileUpload.lib.php');
+			$upload_ok = process_uploaded_file($_FILES['template_image']);
+			
+			if ($upload_ok)
+			{
+				// Try to add an extension to the file if it hasn't one
+				$new_file_name = add_ext_on_mime(stripslashes($_FILES['template_image']['name']), $_FILES['template_image']['type']);	
+				
+				// upload dir
+				$upload_dir = api_get_path(SYS_PATH).'home/';
+				
+				// move the uploaded file to the home folder
+				$result= @move_uploaded_file($_FILES['template_image']['tmp_name'], $upload_dir.$new_file_name);
+			}
+	   }
+	   
+	   // store the information in the database (as insert or as update)
+	   $table_system_template = Database :: get_main_table('system_template');
+	   if ($_GET['action'] == 'add')
+	   {
+		   	$sql = "INSERT INTO $table_system_template (title, content, image) VALUES ('".Database::escape_string($values['title'])."','".Database::escape_string($values['template_text'])."','".Database::escape_string($new_file_name)."')";
+		   	$result = api_sql_query($sql, __FILE__, __LINE__);
+		   	
+		   	// display a feedback message
+		   	Display::display_confirmation_message('TemplateAdded');
+	   }
+	   else 
+	   {
+		   	$sql = "UPDATE $table_system_template set title = '".Database::escape_string($values['title'])."',
+											   		  content = '".Database::escape_string($values['template_text'])."'";
+		   	if (!empty($new_file_name))
+		   	{
+		   		$sql .= ", image = '".Database::escape_string($new_file_name)."'";
+		   	}
+		   	$sql .= " WHERE id='".Database::escape_string($_GET['id'])."'";
+		   	$result = api_sql_query($sql, __FILE__, __LINE__);
+		   	
+		   	// display a feedback message
+		   	Display::display_confirmation_message('TemplateEdited');		   	
+	   }
+	   display_templates();
+	}
+	else 
+	{
+		// display the form
+		$form->display();		
+	}
+}
+
+/**
+ * Delete a template
+ *
+ * @param integer $id the id of the template that has to be deleted
+ * 
+ * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
+ * @version August 2008
+ * @since Dokeos 1.8.6
+ */
+function delete_template($id)
+{
+	// first we remove the image
+	$table_system_template = Database :: get_main_table('system_template');
+	$sql = "SELECT * FROM $table_system_template WHERE id = '".Database::escape_string($id)."'";
+	$result = api_sql_query($sql, __FILE__, __LINE__);
+	$row = Database::fetch_array($result);	
+	if (!empty($row['image']))
+	{
+		unlink(api_get_path(SYS_PATH).'home/'.$row['image']);
+	}
+	
+	// now we remove it from the database
+	$sql = "DELETE FROM $table_system_template WHERE id = '".Database::escape_string($id)."'";
+	$result = api_sql_query($sql, __FILE__, __LINE__);
+	
+	// display a feedback message
+	Display::display_confirmation_message('TemplateDeleted');
+}
 ?>

+ 146 - 44
main/inc/lib/fckeditor/fcktemplates.xml.php

@@ -1,16 +1,61 @@
-<?php header('Content-Type: text/xml; charset=utf-8');
+<?php 
+// setting the character set to UTF-8
+header('Content-Type: text/xml; charset=utf-8');
+
+// name of the language file that needs to be included
 $language_file = 'document';
+
+// including the global dokeos file
 require_once('../../global.inc.php');
+
+// outputting the opening tag of the xml file
 echo '<?xml version="1.0" encoding="utf-8" ?>';
+
+// is this needed? 
 $IMConfig['base_url'] = $_configuration['root_web'].'main/img/gallery/';
+
+// load a stylesheet
+$css = loadCSS(api_get_setting('stylesheets'));
+
+// setting some paths
+$img_dir = api_get_path(REL_CODE_PATH).'img/';
+$default_course_dir = api_get_path(REL_CODE_PATH).'default_course_document/';
+
+// start the templates node
+echo '<Templates imagesBasePath="">';
+
+// load the templates that were defined by the platform admin
+load_platform_templates();
+
+// load the personal templates
+load_personal_templates(api_get_user_id());
+
+// load the hardcoded templates
+load_hardcoded_templates();
+
+// end the templates node
+echo '</Templates>';
+exit;
+
+
+/**
+ * load a given css style (default.css)
+ *
+ * @param string $css_name the folder name of the style
+ * @return html code for adding a css style <style ...
+ */
 function loadCSS($css_name)
 {
 	$template_css = ' <style type="text/css">'.str_replace('../../img/',api_get_path(REL_CODE_PATH).'img/',file_get_contents(api_get_path(SYS_PATH).'main/css/'.$css_name.'/default.css')).'</style>';
 	$template_css = str_replace('images/',api_get_path(REL_CODE_PATH).'css/'.$css_name.'/images/',$template_css);
 	return $template_css;
 }
+
 /**
  * Transforms a language variable into XML-usable code
+ *
+ * @param unknown_type $var
+ * @return unknown
  */
 function s($var)
 {
@@ -19,8 +64,12 @@ function s($var)
     $replace = array ('&amp;','&amp;lt;','&amp;gt;');
     return str_replace($search,$replace,mb_convert_encoding(get_lang($var),'UTF-8',$charset));
 }
+
 /**
- * Transforms a string into XML-usable code
+ * Transforms a language variable into XML-usable code
+ *
+ * @param unknown_type $var
+ * @return unknown
  */
 function s2($var)
 {
@@ -29,53 +78,104 @@ function s2($var)
     $replace = array ('&amp;','&amp;lt;','&amp;gt;');
     return str_replace($search,$replace,mb_convert_encoding($var,'UTF-8',$charset));
 }
-$css = loadCSS(api_get_setting('stylesheets'));
-$img_dir = api_get_path(REL_CODE_PATH).'img/';
-$default_course_dir = api_get_path(REL_CODE_PATH).'default_course_document/';
-//<Templates imagesBasePath="fck_template/images/">
-?>
-<Templates imagesBasePath="">	
-<?php
-	//Get all personnal templates in the database 	
+
+/**
+ * Load the platform templates as defined by the platform administrator in "Platform administration > Dokeos configuration settings > templates"
+ *
+ * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
+ * @version March 2009
+ * @since Dokeos 1.8.6
+ */
+function load_platform_templates()
+{
+	// Database table definition
+	$table_template = Database::get_main_table('system_template');	
+	
+	$sql = "SELECT * FROM $table_template";
+	$result = api_sql_query($sql, __FILE__, __LINE__);
+	while ($row = Database::fetch_array($result))
+	{
+		echo '	<Template title="'.$row['title'].'" image="'.api_get_path(WEB_PATH).'home/'.$row['image'].'">
+					<Description>'.$row['Description'].'</Description>
+					<Html>
+						<![CDATA[
+							    '.$row['content'].'
+						]]>
+					</Html>
+				</Template>';			
+	}
+}
+
+/**
+ * Load all the personal templates of the user when 
+ *
+ * @param integer $user_id the id of the user
+ * @return xml node
+ * 
+ * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
+ * @version March 2009
+ * @since Dokeos 1.8.6 The code already existed but not in a function and a lot less performant. 
+ */
+function load_personal_templates($user_id=0)
+{
+	global $_course; 
+
+	// the templates that the user has defined are only available inside the course itself
+	if (empty($_course))
+	{
+		return false;
+	}
+	
+	// For which user are we getting the templates? 
+	if ($user_id==0)
+	{
+		$user_id = api_get_user_id();
+	}
+
+	// Database table definition
 	$table_template = Database::get_main_table(TABLE_MAIN_TEMPLATES);	
-	$sql = 'SELECT id, title, description, ref_doc FROM '.$table_template.' WHERE course_code="'.api_get_course_id().'" AND user_id="'.api_get_user_id().'"';	
-	$result_template = api_sql_query($sql,__FILE__,__LINE__);
-	 
-	$course = api_get_course_info();
 	$table_document = Database::get_course_table(TABLE_DOCUMENT, $_course['dbName']);	
-	while($a_template = Database::fetch_array($result_template))
+	
+	
+	// The sql statement for getting all the user defined templates
+	$sql = "SELECT template.id, template.title, template.description, template.image, template.ref_doc, document.path 
+			FROM ".$table_template." template, ".$table_document." document 
+			WHERE user_id='".Database::escape_string($user_id)."'
+			AND course_code='".Database::escape_string(api_get_course_id())."'
+			AND document.id = template.ref_doc"; 
+	$result_template = api_sql_query($sql,__FILE__,__LINE__);
+	while ($row = Database::fetch_array($result_template))
 	{
-		$document_id = $a_template['ref_doc'];				
-		$sql_document_path = 'SELECT path FROM '.$table_document.' WHERE id="'.$document_id.'"';		
-		$result_document_path = api_sql_query($sql_document_path,__FILE__,__LINE__);
-		$document_path = Database::result($result_document_path,0,0);
-		/*		
-		$width = 100;
-		$height = 90;
+		$row['content'] = file_get_contents(api_get_path('SYS_COURSE_PATH').$_course['path'].'/document'.$row['path']);
+		//$row['content'] = api_get_path('SYS_COURSE_PATH').$_course['path'].'/document'.$row['path'];
 		
-		$im = @ImageCreate($width, $height);
-		$bg_color = ImageColorAllocate($im, 255, 255, 255);
-		$ttfont   = api_get_path(LIBRARY_PATH).'fckeditor/FreeSans.ttf';
-		$text_color = ImageColorAllocate($im, 0, 0, 0);
-		
-		$a_text=explode(' ',$a_template['title']);
-		$y=25;
-		foreach ($a_text as $a_part_of_title) 
+		if (!empty($row['image']))
+		{
+			$image = api_get_path(WEB_CODE_PATH).'upload/template_thumbnails/'.$row['image'];
+		}
+		else 
 		{
-			imagettftext($im, 10, 0, 10, $y, $text_color, $ttfont, $a_part_of_title);
-			$y+=20;
-		}		
-		imagejpeg($im, api_get_path(SYS_CODE_PATH).'upload/template_thumbnails/'.$a_template['id'].'.jpg');
-		*/
-		//echo '<Template title="'.htmlentities($a_template['title']).'" image="'.api_get_path(REL_CODE_PATH).'upload/template_thumbnails/'.$a_template['id'].'.jpg">';
-		echo '<Template title="'.s2($a_template['title']).'" >';
-			echo '<Description>'.s2($a_template['description']).'</Description>';
-			echo '<Html>';
-			echo htmlspecialchars(file_get_contents(api_get_path(SYS_COURSE_PATH).$course['path'].'/document'.$document_path),ENT_COMPAT);			
-			echo '</Html>';
-		echo '</Template>';			
+			$image = api_get_path(WEB_CODE_PATH).'inc/lib/fckeditor/editor/dialog/fck_template/images/noimage.gif';
+		}
+		
+		
+		echo '	<Template title="'.s2($row['title']).'" image="'.$image.'">
+					<Description>'.s2($row['Description']).'</Description>
+					<Html>
+						<![CDATA[
+							    '.$row['content'].'
+						]]>
+					</Html>
+				</Template>';		
 	}	
-?>    
+}
+function load_hardcoded_templates()
+{
+	global $css; 
+	global $img_dir; 
+	global $default_course_dir;
+?>
+
 <Template title="<?php echo s('Empty'); ?>" >
     <Description></Description>
     <Html>
@@ -940,4 +1040,6 @@ s1.addVariable("repeat","false");
 		]]>
 	</Html>
 </Template>	
-</Templates>
+<?php
+}
+?>