, Ghent University * @copyright Ghent University * @copyright Patrick Cool * @author Julio Montoya , Dokeos Several fixes * @package dokeos.forum * * @todo several functions have to be moved to the itemmanager library * @todo displaying icons => display library * @todo complete the missing phpdoc the correct order should be * * some explanation of the function * @param * @return * @todo * @author firstname lastname , organisation * @version (day) month year * @deprecated */ /** ************************************************************************** * IMPORTANT NOTICE * Please do not change anything is this code yet because there are still * some significant code that need to happen and I do not have the time to * merge files and test it all over again. So for the moment, please do not * touch the code * -- Patrick Cool ************************************************************************** */ require_once(api_get_path(LIBRARY_PATH).'mail.lib.inc.php'); require_once(api_get_path(LIBRARY_PATH).'text.lib.php'); require_once(api_get_path(LIBRARY_PATH).'usermanager.lib.php'); require_once(api_get_path(LIBRARY_PATH).'text.lib.php'); get_notifications_of_user(); /* ----------------------------------------------------------- Javascript ----------------------------------------------------------- */ $htmlHeadXtra[] = ''; //jQuery $htmlHeadXtra[] = ''; /** * This function handles all the forum and forumcategories actions. This is a wrapper for the * forum and forum categories. All this code code could go into the section where this function is * called but this make the code there cleaner. * @param * @return * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 */ function handle_forum_and_forumcategories() { $action_forum_cat = isset($_GET['action']) ? $_GET['action'] : ''; $post_submit_cat= isset($_POST['SubmitForumCategory']) ? true : false; $post_submit_forum= isset($_POST['SubmitForum']) ? true : false; $get_id=isset($_GET['id']) ? $_GET['id'] : ''; // Adding a forum category if (($action_forum_cat=='add' && $_GET['content']=='forumcategory') || $post_submit_cat ) { show_add_forumcategory_form(); } // Adding a forum if ((($action_forum_cat=='add' || $action_forum_cat=='edit') && $_GET['content']=='forum') || $post_submit_forum ) { if ($action_forum_cat=='edit' && $get_id || $post_submit_forum ) { $inputvalues=get_forums(strval(intval($get_id))); // note: this has to be cleaned first } else { $inputvalues=''; } show_add_forum_form($inputvalues); } // Edit a forum category if (($action_forum_cat=='edit' && $_GET['content']=='forumcategory' && isset($_GET['id'])) || (isset($_POST['SubmitEditForumCategory'])) ? true : false ) { $forum_category=get_forum_categories(strval(intval($_GET['id']))); // note: this has to be cleaned first show_edit_forumcategory_form($forum_category); } // Delete a forum category if (( isset($_GET['action']) && $_GET['action']=='delete') && isset($_GET['content']) && $get_id) { $id_forum=Security::remove_XSS($get_id); $list_threads=get_threads($id_forum); for ( $i=0; $i < count($list_threads); $i++ ) { $messaje=delete_forum_forumcategory_thread('thread',$list_threads[$i]['thread_id']); $table_link = Database :: get_main_table(TABLE_MAIN_GRADEBOOK_LINK); $sql_link='DELETE FROM '.$table_link.' WHERE ref_id='.$list_threads[$i]['thread_id'].' and type=5 and course_code="'.api_get_course_id().'";'; Database::query($sql_link,__FILE__,__LINE__); } $return_message=delete_forum_forumcategory_thread($_GET['content'],$_GET['id']); Display :: display_confirmation_message($return_message,false); } // Change visibility of a forum or a forum category if (($action_forum_cat=='invisible' || $action_forum_cat=='visible') && isset($_GET['content']) && isset($_GET['id'])) { $return_message=change_visibility($_GET['content'], $_GET['id'],$_GET['action']);// note: this has to be cleaned first Display :: display_confirmation_message($return_message,false); } // Change lock status of a forum or a forum category if (($action_forum_cat=='lock' || $action_forum_cat=='unlock') && isset($_GET['content']) && isset($_GET['id'])) { $return_message=change_lock_status($_GET['content'], $_GET['id'],$_GET['action']);// note: this has to be cleaned first Display :: display_confirmation_message($return_message,false); } // Move a forum or a forum category if ($action_forum_cat=='move' && isset($_GET['content']) && isset($_GET['id']) && isset($_GET['direction'])) { $return_message=move_up_down($_GET['content'], $_GET['direction'], $_GET['id']);// note: this has to be cleaned first Display :: display_confirmation_message($return_message,false); } } /** * This function displays the form that is used to add a forum category. * * @param * @return * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 */ function show_add_forumcategory_form($inputvalues=array()) { $gradebook=Security::remove_XSS($_GET['gradebook']); // initiate the object $form = new FormValidator('forumcategory','post','index.php?&gradebook='.$gradebook.''); // settting the form elements $form->addElement('header', '', get_lang('AddForumCategory')); $form->addElement('text', 'forum_category_title', get_lang('Title'),'class="input_titles" id="category_title"'); //$form->applyFilter('forum_category_title', 'html_filter'); $form->addElement('html_editor', 'forum_category_comment', get_lang('Comment'), null, array('ToolbarSet' => 'Forum', 'Width' => '98%', 'Height' => '200')); //$form->applyFilter('forum_category_comment', 'html_filter'); $form->addElement('style_submit_button', 'SubmitForumCategory', get_lang('CreateCategory'), 'class="add"'); // setting the rules $form->addRule('forum_category_title', '
'.get_lang('ThisFieldIsRequired'), 'required'); // The validation or display if ( $form->validate() ) { $check = Security::check_token('post'); if ($check) { $values = $form->exportValues(); store_forumcategory($values); } Security::clear_token(); } else { $token = Security::get_token(); $form->addElement('hidden','sec_token'); $form->setConstants(array('sec_token' => $token)); $form->display(); } } /** * This function displays the form that is used to add a forum category. * * @param * @return * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 */ function show_add_forum_form($inputvalues=array()) { global $_course; $gradebook=Security::remove_XSS($_GET['gradebook']); // initiate the object $form = new FormValidator('forumcategory', 'post', 'index.php?gradebook='.$gradebook.''); // the header for the form if (!empty($inputvalues)) { $form_title = get_lang('EditForum'); } else { $form_title = get_lang('AddForum'); } $session_header = isset($_SESSION['session_name']) ? ' ('.$_SESSION['session_name'].') ' : ''; $form->addElement('header', '', $form_title.$session_header); // we have a hidden field if we are editing if (is_array($inputvalues)) { $my_forum_id=isset($inputvalues['forum_id']) ? $inputvalues['forum_id'] : null; $form->addElement('hidden', 'forum_id', $my_forum_id); } // The title of the forum $form->addElement('text', 'forum_title', get_lang('Title'),'class="input_titles" id="forum_title"'); //$form->applyFilter('forum_title', 'html_filter'); // The comment of the forum $form->addElement('html_editor', 'forum_comment', get_lang('Comment'), null, array('ToolbarSet' => 'Forum', 'Width' => '98%', 'Height' => '200')); //$form->applyFilter('forum_comment', 'html_filter'); // dropdown list: Forum Categories $forum_categories=get_forum_categories(); foreach ($forum_categories as $key=>$value) { $forum_categories_titles[$value['cat_id']]=$value['cat_title']; } $form->addElement('select', 'forum_category', get_lang('InForumCategory'), $forum_categories_titles); $form->applyFilter('forum_category', 'html_filter'); if ($_course['visibility']==COURSE_VISIBILITY_OPEN_WORLD) { // This is for vertical //$form->addElement('radio', 'allow_anonymous', get_lang('AllowAnonymousPosts'), get_lang('Yes'), 1); //$form->addElement('radio', 'allow_anonymous', '', get_lang('No'), 0); // This is for horizontal $group=''; $group[] =& HTML_QuickForm::createElement('radio', 'allow_anonymous',null,get_lang('Yes'),1); $group[] =& HTML_QuickForm::createElement('radio', 'allow_anonymous',null,get_lang('No'),0); $form->addGroup($group, 'allow_anonymous_group', get_lang('AllowAnonymousPosts'), ' '); } // This is for vertical //$form->addElement('radio', 'students_can_edit', get_lang('StudentsCanEdit'), get_lang('Yes'), 1); //$form->addElement('radio', 'students_can_edit', '', get_lang('No'), 0); // This is for horizontal /* if (document.getElementById('id_qualify').style.display == 'none') { document.getElementById('id_qualify').style.display = 'block'; document.getElementById('plus').innerHTML=' '.Display::return_icon('div_hide.gif').' ".get_lang('AddAnAttachment')."'; } else { document.getElementById('options').style.display = 'none'; document.getElementById('plus').innerHTML=' '.Display::return_icon('div_show.gif').' ".get_lang('AddAnAttachment')."'; }*/ $form->addElement('html', ''); $form->addElement('html',''); // The OK button if (isset($_GET['id']) && $_GET['action']=='edit'){ $class='save'; $text=get_lang('ModifyForum'); }else{ $class='add'; $text=get_lang('CreateForum'); } $form->addElement('style_submit_button', 'SubmitForum', $text, 'class="'.$class.'"'); // setting the rules $form->addRule('forum_title', get_lang('ThisFieldIsRequired'), 'required'); $form->addRule('forum_category', get_lang('ThisFieldIsRequired'), 'required'); global $charset; // settings the defaults if (!is_array($inputvalues)) { $defaults['allow_anonymous_group']['allow_anonymous']=0; $defaults['students_can_edit_group']['students_can_edit']=0; $defaults['approval_direct_group']['approval_direct']=0; $defaults['allow_attachments_group']['allow_attachments']=1; $defaults['allow_new_threads_group']['allow_new_threads']=0; $defaults['default_view_type_group']['default_view_type']=api_get_setting('default_forum_view'); $defaults['public_private_group_forum_group']['public_private_group_forum']='public'; if (isset($_GET['forumcategory'])) { $defaults['forum_category']=Security::remove_XSS($_GET['forumcategory']); } } else { // the default values when editing = the data in the table $defaults['forum_id']=isset($inputvalues['forum_id']) ? $inputvalues['forum_id'] : null; $defaults['forum_title']=prepare4display(api_html_entity_decode(isset($inputvalues['forum_title']) ? $inputvalues['forum_title'] : null,ENT_QUOTES,$charset)); $defaults['forum_comment']=prepare4display(isset($inputvalues['forum_comment'])?$inputvalues['forum_comment']:null); $defaults['forum_category']=isset($inputvalues['forum_category']) ? $inputvalues['forum_category'] : null; $defaults['allow_anonymous_group']['allow_anonymous']=isset($inputvalues['allow_anonymous']) ? $inputvalues['allow_anonymous'] :null; $defaults['students_can_edit_group']['students_can_edit']=isset($inputvalues['allow_edit'])?$inputvalues['allow_edit']:null; $defaults['approval_direct_group']['approval_direct']=isset($inputvalues['approval_direct_post'])?$inputvalues['approval_direct_post']:null; $defaults['allow_attachments_group']['allow_attachments']=isset($inputvalues['allow_attachments'])?$inputvalues['allow_attachments']:null; $defaults['allow_new_threads_group']['allow_new_threads']=isset($inputvalues['allow_new_threads'])?$inputvalues['allow_new_threads']:null; $defaults['default_view_type_group']['default_view_type']=isset($inputvalues['default_view'])?$inputvalues['default_view']:null; $defaults['public_private_group_forum_group']['public_private_group_forum']=isset($inputvalues['forum_group_public_private'])?$inputvalues['forum_group_public_private']:null; $defaults['group_forum']=isset($inputvalues['forum_of_group'])?$inputvalues['forum_of_group']:null; } $form->setDefaults($defaults); // The validation or display if( $form->validate() ) { $check = Security::check_token('post'); if ($check) { $values = $form->exportValues(); $return_message = store_forum($values); Display :: display_confirmation_message($return_message); } Security::clear_token(); } else { $token = Security::get_token(); $form->addElement('hidden','sec_token'); $form->setConstants(array('sec_token' => $token)); $form->display(); } } /** * This function deletes the forum image if exists * * @param int forum id * @return boolean true if success * @author Julio Montoya , Dokeos * @version february 2006, dokeos 1.8 */ function delete_forum_image($forum_id) { $table_forums = Database::get_course_table(TABLE_FORUM); $forum_id = Database::escape_string($forum_id); $sql="SELECT forum_image FROM $table_forums WHERE forum_id = '".$forum_id."' "; $result=Database::query($sql,__FILE__,__LINE__); $row=Database::fetch_array($result); if ($row['forum_image']!='') { $del_file = api_get_path(SYS_COURSE_PATH).api_get_course_path().'/upload/forum/images/'.$row['forum_image']; return @unlink($del_file); } else { return false; } } /** * This function displays the form that is used to edit a forum category. * This is more or less a copy from the show_add_forumcategory_form function with the only difference that is uses * some default values. I tried to have both in one function but this gave problems with the handle_forum_and_forumcategories function * (storing was done twice) * * @param * @return * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 */ function show_edit_forumcategory_form($inputvalues=array()) { // initiate the object $gradebook=Security::remove_XSS($_GET['gradebook']); $form = new FormValidator('forumcategory','post','index.php?&gradebook='.$gradebook.''); // settting the form elements $form->addElement('header', '', get_lang('EditForumCategory')); $form->addElement('hidden', 'forum_category_id'); $form->addElement('text', 'forum_category_title', get_lang('Title'),'class="input_titles"'); //$form->applyFilter('forum_category_title', 'html_filter'); $form->addElement('html_editor', 'forum_category_comment', get_lang('Comment'), null, array('ToolbarSet' => 'Forum', 'Width' => '98%', 'Height' => '200')); //$form->applyFilter('forum_category_comment', 'html_filter'); $form->addElement('style_submit_button', 'SubmitEditForumCategory',get_lang('ModifyCategory'), 'class="save"'); global $charset; // setting the default values $defaultvalues['forum_category_id']=$inputvalues['cat_id']; $defaultvalues['forum_category_title']=prepare4display(api_html_entity_decode($inputvalues['cat_title'],ENT_QUOTES,$charset)); $defaultvalues['forum_category_comment']=prepare4display($inputvalues['cat_comment']); $form->setDefaults($defaultvalues); // setting the rules $form->addRule('forum_category_title', get_lang('ThisFieldIsRequired'), 'required'); // The validation or display if ( $form->validate() ) { $check = Security::check_token('post'); if ($check) { $values = $form->exportValues(); store_forumcategory($values); } Security::clear_token(); } else { $token = Security::get_token(); $form->addElement('hidden','sec_token'); $form->setConstants(array('sec_token' => $token)); $form->display(); } } /** * This function stores the forum category in the database. The new category is added to the end. * * @param * @return * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 */ function store_forumcategory($values) { global $table_categories; global $_course; global $_user; // find the max cat_order. The new forum category is added at the end => max cat_order + & $sql="SELECT MAX(cat_order) as sort_max FROM ".Database::escape_string($table_categories); $result=Database::query($sql,__FILE__,__LINE__); $row=Database::fetch_array($result); $new_max=$row['sort_max']+1; $session_id = api_get_session_id(); $clean_cat_title=Database::escape_string(Security::remove_XSS($values['forum_category_title'])); if (isset($values['forum_category_id'])) { // storing an edit $sql="UPDATE ".$table_categories." SET cat_title='".$clean_cat_title."', cat_comment='".Database::escape_string(Security::remove_XSS(stripslashes(api_html_entity_decode($values['forum_category_comment'])),COURSEMANAGERLOWSECURITY))."' WHERE cat_id='".Database::escape_string($values['forum_category_id'])."'"; Database::query($sql,__FILE__,__LINE__); $last_id=Database::insert_id(); api_item_property_update(api_get_course_info(), TOOL_FORUM_CATEGORY, $values['forum_category_id'], 'ForumCategoryUpdated', api_get_user_id()); $return_message=get_lang('ForumCategoryEdited'); } else { $sql = "INSERT INTO ".$table_categories." (cat_title, cat_comment, cat_order, session_id) VALUES ('".$clean_cat_title."','".Database::escape_string(Security::remove_XSS(stripslashes(api_html_entity_decode($values['forum_category_comment'])),COURSEMANAGERLOWSECURITY))."','".Database::escape_string($new_max)."','".Database::escape_string($session_id)."')"; Database::query($sql,__FILE__,__LINE__); $last_id = Database::insert_id(); if ($last_id > 0) { api_item_property_update(api_get_course_info(), TOOL_FORUM_CATEGORY, $last_id, 'ForumCategoryAdded', api_get_user_id()); } $return_message=get_lang('ForumCategoryAdded'); } Display :: display_confirmation_message($return_message); } /** * This function stores the forum in the database. The new forum is added to the end. * * @param * @return * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 */ function store_forum($values) { global $_course; global $_user; $table_forums = Database::get_course_table(TABLE_FORUM); // find the max forum_order for the given category. The new forum is added at the end => max cat_order + & if (is_null($values['forum_category'])) { $new_max=null; } else { $sql="SELECT MAX(forum_order) as sort_max FROM ".$table_forums." WHERE forum_category='".Database::escape_string($values['forum_category'])."'"; $result=Database::query($sql,__FILE__,__LINE__); $row=Database::fetch_array($result); $new_max=$row['sort_max']+1; } $session_id = api_get_session_id(); $clean_title = Database::escape_string(Security::remove_XSS($values['forum_title'])); // forum images $image_moved=false; if (!empty($_FILES['picture']['name'])) { $upload_ok = process_uploaded_file($_FILES['picture']); $has_attachment=true; } else { $image_moved=true; } // remove existing picture if asked if (!empty($_POST['remove_picture'])) { delete_forum_image($values['forum_id']); } if (isset($upload_ok)) { if ($has_attachment) { $courseDir = $_course['path'].'/upload/forum/images'; $sys_course_path = api_get_path(SYS_COURSE_PATH); $updir = $sys_course_path.$courseDir; // Try to add an extension to the file if it hasn't one $new_file_name = add_ext_on_mime(Database::escape_string($_FILES['picture']['name']), $_FILES['picture']['type']); // user's file name $file_name =$_FILES['picture']['name']; if (!filter_extension($new_file_name)) { //Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension')); $image_moved=false; } else { $file_extension = explode('.', $_FILES['picture']['name']); $file_extension = strtolower($file_extension[sizeof($file_extension) - 1]); $new_file_name = uniqid('').'.'.$file_extension; $new_path=$updir.'/'.$new_file_name; $result= @move_uploaded_file($_FILES['picture']['tmp_name'], $new_path); // Storing the attachments if any if ($result) { $image_moved=true; } } } } if (isset($values['forum_id'])) { $sql_image=isset($sql_image)?$sql_image:''; $new_file_name=isset($new_file_name) ? $new_file_name:''; if ($image_moved) { if(empty($_FILES['picture']['name'])){ $sql_image=" "; } else { $sql_image=" forum_image='".Database::escape_string($new_file_name)."', "; delete_forum_image($values['forum_id']); } } // storing an edit $sql="UPDATE ".$table_forums." SET forum_title='".$clean_title."', ".$sql_image." forum_comment='".Database::escape_string(Security::remove_XSS(stripslashes(api_html_entity_decode($values['forum_comment'])),COURSEMANAGERLOWSECURITY))."', forum_category='".Database::escape_string($values['forum_category'])."', allow_anonymous='".Database::escape_string(isset($values['allow_anonymous_group']['allow_anonymous'])?$values['allow_anonymous_group']['allow_anonymous']:null)."', allow_edit='".Database::escape_string($values['students_can_edit_group']['students_can_edit'])."', approval_direct_post='".Database::escape_string(isset($values['approval_direct_group']['approval_direct'])?$values['approval_direct_group']['approval_direct']:null)."', allow_attachments='".Database::escape_string(isset($values['allow_attachments_group']['allow_attachments'])?$values['allow_attachments_group']['allow_attachments']:null)."', allow_new_threads='".Database::escape_string($values['allow_new_threads_group']['allow_new_threads'])."', forum_group_public_private='".Database::escape_string($values['public_private_group_forum_group']['public_private_group_forum'])."', default_view='".Database::escape_string($values['default_view_type_group']['default_view_type'])."', forum_of_group='".Database::escape_string($values['group_forum'])."' WHERE forum_id='".Database::escape_string($values['forum_id'])."'"; Database::query($sql,__FILE__,__LINE__); api_item_property_update($_course, TOOL_FORUM, Database::escape_string($values['forum_id']), 'ForumUpdated', api_get_user_id()); $return_message=get_lang('ForumEdited'); } else { $sql_image=''; if ($image_moved) { $new_file_name=isset($new_file_name)?$new_file_name:''; $sql_image="'".$new_file_name."', "; } $b=$values['forum_comment']; $sql="INSERT INTO ".$table_forums." (forum_title, forum_image, forum_comment, forum_category, allow_anonymous, allow_edit, approval_direct_post, allow_attachments, allow_new_threads, default_view, forum_of_group, forum_group_public_private, forum_order, session_id) VALUES ('".Security::remove_XSS($clean_title)."', ".$sql_image." '".Database::escape_string(isset($values['forum_comment'])?Security::remove_XSS(stripslashes(api_html_entity_decode($values['forum_comment'])),COURSEMANAGERLOWSECURITY):null)."', '".Database::escape_string(isset($values['forum_category'])?$values['forum_category']:null)."', '".Database::escape_string(isset($values['allow_anonymous_group']['allow_anonymous'])?$values['allow_anonymous_group']['allow_anonymous']:null)."', '".Database::escape_string(isset($values['students_can_edit_group']['students_can_edit'])?$values['students_can_edit_group']['students_can_edit']:null)."', '".Database::escape_string(isset($values['approval_direct_group']['approval_direct'])?$values['approval_direct_group']['approval_direct']:null)."', '".Database::escape_string(isset($values['allow_attachments_group']['allow_attachments'])?$values['allow_attachments_group']['allow_attachments']:null)."', '".Database::escape_string(isset($values['allow_new_threads_group']['allow_new_threads'])?$values['allow_new_threads_group']['allow_new_threads']:null)."', '".Database::escape_string(isset($values['default_view_type_group']['default_view_type'])?$values['default_view_type_group']['default_view_type']:null)."', '".Database::escape_string(isset($values['group_forum'])?$values['group_forum']:null)."', '".Database::escape_string(isset($values['public_private_group_forum_group']['public_private_group_forum'])?$values['public_private_group_forum_group']['public_private_group_forum']:null)."', '".Database::escape_string(isset($new_max)?$new_max:null)."', ".intval($session_id).")"; Database::query($sql,__FILE__,__LINE__); $last_id = Database::insert_id(); if ($last_id > 0) { api_item_property_update($_course, TOOL_FORUM, $last_id, 'ForumAdded', api_get_user_id()); } $return_message = get_lang('ForumAdded'); } return $return_message; } /** * This function deletes a forum or a forum category * This function currently does not delete the forums inside the category, nor the threads and replies inside these forums. * For the moment this is the easiest method and it has the advantage that it allows to recover fora that were acidently deleted * when the forum category got deleted. * * @param $content = what we are deleting (a forum or a forum category) * @param $id The id of the forum category that has to be deleted. * * @todo write the code for the cascading deletion of the forums inside a forum category and also the threads and replies inside these forums * @todo config setting for recovery or not (see also the documents tool: real delete or not). * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 */ function delete_forum_forumcategory_thread($content, $id) { global $_course; $table_forums = Database::get_course_table(TABLE_FORUM); $table_forums_post = Database::get_course_table(TABLE_FORUM_POST); $table_forum_thread = Database::get_course_table(TABLE_FORUM_THREAD); // delete all attachment file about this tread id $sql = "SELECT post_id FROM $table_forums_post WHERE thread_id = '".(int)$id."' "; $res = Database::query($sql,__FILE__,__LINE__); while ($poster_id = Database::fetch_row($res)) { delete_attachment($poster_id[0]); } if ($content=='forumcategory') { $tool_constant=TOOL_FORUM_CATEGORY; $return_message=get_lang('ForumCategoryDeleted'); if (!empty($forum_list)){ $sql="SELECT forum_id FROM ". $table_forums . "WHERE forum_category='".$id."'"; $result = Database::query($sql, __FILE__, __LINE__); $row = Database::fetch_array($result); foreach ($row as $arr_forum) { $forum_id = $arr_forum['forum_id']; api_item_property_update($_course,'forum',$forum_id,'delete',api_get_user_id()); } } } if ($content=='forum') { $tool_constant=TOOL_FORUM; $return_message=get_lang('ForumDeleted'); if (!empty($number_threads)){ $sql="SELECT thread_id FROM". $table_forum_thread . "WHERE forum_id='".$id."'"; $result = Database::query($sql, __FILE__, __LINE__); $row = Database::fetch_array($result); foreach ($row as $arr_forum) { $forum_id = $arr_forum['thread_id']; api_item_property_update($_course,'forum_thread',$forum_id,'delete',api_get_user_id()); } } } if ($content=='thread') { $tool_constant=TOOL_FORUM_THREAD; $return_message=get_lang('ThreadDeleted'); } api_item_property_update($_course,$tool_constant,$id,'delete',api_get_user_id()); // note: check if this returns a true and if so => return $return_message, if not => return false; return $return_message; } /** * This function deletes a forum post. This separate function is needed because forum posts do not appear in the item_property table (yet) * and because deleting a post also has consequence on the posts that have this post as parent_id (they are also deleted). * an alternative would be to store the posts also in item_property and mark this post as deleted (visibility = 2). * We also have to decrease the number of replies in the thread table * * @param $post_id the id of the post that will be deleted * * @todo write recursive function that deletes all the posts that have this message as parent * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 */ function delete_post($post_id) { global $table_posts; global $table_threads; $sql="DELETE FROM $table_posts WHERE post_id='".Database::escape_string($post_id)."'"; // note: this has to be a recursive function that deletes all of the posts in this block. Database::query($sql,__FILE__,__LINE__); //delete attachment file about this post id delete_attachment($post_id); $last_post_of_thread=check_if_last_post_of_thread(strval(intval($_GET['thread']))); if (is_array($last_post_of_thread)) { // Decreasing the number of replies for this thread and also changing the last post information $sql="UPDATE $table_threads SET thread_replies=thread_replies-1, thread_last_post='".Database::escape_string($last_post_of_thread['post_id'])."', thread_date='".Database::escape_string($last_post_of_thread['post_date'])."' WHERE thread_id='".Database::escape_string($_GET['thread'])."'"; Database::query($sql,__FILE__,__LINE__); return 'PostDeleted'; } if ($last_post_of_thread==false) { // we deleted the very single post of the thread so we need to delete the entry in the thread table also. $sql="DELETE FROM $table_threads WHERE thread_id='".Database::escape_string($_GET['thread'])."'"; Database::query($sql,__FILE__,__LINE__); return 'PostDeletedSpecial'; } } /** * This function gets the all information of the last (=most recent) post of the thread * This can be done by sorting the posts that have the field thread_id=$thread_id and sort them by post_date * * @param $thread_id the id of the thread we want to know the last post of. * @return an array if there is a last post found, false if there is no post entry linked to that thread => thread will be deleted * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 */ function check_if_last_post_of_thread($thread_id) { global $table_posts; $sql="SELECT * FROM $table_posts WHERE thread_id='".Database::escape_string($thread_id)."' ORDER BY post_date DESC"; $result=Database::query($sql,__FILE__,__LINE__); if ( Database::num_rows($result)>0 ) { $row=Database::fetch_array($result); return $row; } else { return false; } } /** * This function takes care of the display of the visibility icon * * @param $content what is it that we want to make (in)visible: forum category, forum, thread, post * @param $id the id of the content we want to make invisible * @param $current_visibility_status what is the current status of the visibility (0 = invisible, 1 = visible) * @return * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 */ function display_visible_invisible_icon($content, $id, $current_visibility_status, $additional_url_parameters='') { global $origin; $gradebook=Security::remove_XSS($_GET['gradebook']); $id = Security::remove_XSS($id); if ($current_visibility_status=='1') { echo ''.icon('../img/visible.gif',get_lang('MakeInvisible')).''; } if ($current_visibility_status=='0') { echo ''.icon('../img/invisible.gif',get_lang('MakeVisible')).''; } } /** * This function takes care of the display of the lock icon * * @param $content what is it that we want to (un)lock: forum category, forum, thread, post * @param $id the id of the content we want to (un)lock * @param $current_visibility_status what is the current status of the visibility (0 = invisible, 1 = visible) * @return * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 */ function display_lock_unlock_icon($content, $id, $current_lock_status, $additional_url_parameters='') { $id = Security::remove_XSS($id); if ($current_lock_status=='1') { echo ''.icon('../img/lock.gif',get_lang('Unlock')).''; } if ($current_lock_status=='0') { echo ''.icon('../img/unlock.gif',get_lang('Lock')).''; } } /** * This function takes care of the display of the up and down icon * * @param $content what is it that we want to make (in)visible: forum category, forum, thread, post * @param $id is the id of the item we want to display the icons for * @param $list is an array of all the items. All items in this list should have an up and down icon except for the first (no up icon) and the last (no down icon) * The key of this $list array is the id of the item. * * @return * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 */ function display_up_down_icon($content, $id, $list) { $id = strval(intval($id)); $total_items=count($list); $position = 0; $internal_counter=0; if(is_array($list)) { foreach ($list as $key=>$listitem) { $internal_counter++; if ($id==$key) { $position=$internal_counter; } } } if ($position>1) { $return_value=''.Display::return_icon('up.gif',get_lang('MoveUp')).''; } else { $return_value=Display::return_icon('up_na.gif','-'); } if ($position<$total_items) { $return_value.=''.Display::return_icon('down.gif',get_lang('MoveDown')).''; } else { $return_value.=Display::return_icon('down_na.gif','-'); } echo $return_value; } /** * This function changes the visibility in the database (item_property) * * @param $content what is it that we want to make (in)visible: forum category, forum, thread, post * @param $id the id of the content we want to make invisible * @param $target_visibility what is the current status of the visibility (0 = invisible, 1 = visible) * * @todo change the get parameter so that it matches the tool constants. * @todo check if api_item_property_update returns true or false => returnmessage depends on it. * @todo move to itemmanager * * @return * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 */ function change_visibility($content, $id, $target_visibility) { global $_course; $constants=array('forumcategory'=>TOOL_FORUM_CATEGORY, 'forum'=>TOOL_FORUM, 'thread'=>TOOL_FORUM_THREAD); api_item_property_update($_course,$constants[$content],$id,$target_visibility,api_get_user_id()); // note: check if this returns true or false => returnmessage depends on it. if ($target_visibility=='visible') { handle_mail_cue($content, $id); } return get_lang('VisibilityChanged'); } /** * This function changes the lock status in the database * * @param $content what is it that we want to (un)lock: forum category, forum, thread, post * @param $id the id of the content we want to (un)lock * @param $action do we lock (=>locked value in db = 1) or unlock (=> locked value in db = 0) * @return string, language variable * * @todo move to itemmanager * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 */ function change_lock_status($content, $id, $action) { global $table_categories; global $table_forums; global $table_threads; global $table_posts; // Determine the relevant table if ($content=='forumcategory') { $table=$table_categories; $id_field='cat_id'; } elseif ($content=='forum') { $table=$table_forums; $id_field='forum_id'; } elseif ($content=='thread') { $table=$table_threads; $id_field='thread_id'; } else { return get_lang('Error'); } // Determine what we are doing => defines the value for the database and the return message if ($action=='lock') { $db_locked=1; $return_message=get_lang('Locked'); } elseif ($action=='unlock') { $db_locked=0; $return_message=get_lang('Unlocked'); } else { return get_lang('Error'); } // Doing the change in the database $sql="UPDATE $table SET locked='".Database::escape_string($db_locked)."' WHERE $id_field='".Database::escape_string($id)."'"; if (Database::query($sql,__FILE__,__LINE__)) { return $return_message; } else { return get_lang('Error'); } } /** * This function moves a forum or a forum category up or down * * @param $content what is it that we want to make (in)visible: forum category, forum, thread, post * @param $direction do we want to move it up or down. * @param $id the id of the content we want to make invisible * @todo consider removing the table_item_property calls here but this can prevent unwanted side effects when a forum does not have an entry in * the item_property table but does have one in the forum table. * @return * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 */ function move_up_down($content, $direction, $id) { global $table_categories; global $table_forums; global $table_item_property; // Determine which field holds the sort order if ($content=='forumcategory') { $table=$table_categories; $sort_column='cat_order'; $id_column='cat_id'; $sort_column='cat_order'; } elseif ($content=='forum') { $table=$table_forums; $sort_column='forum_order'; $id_column='forum_id'; $sort_column='forum_order'; // we also need the forum_category of this forum $sql="SELECT forum_category FROM $table_forums WHERE forum_id=".Database::escape_string($id); $result=Database::query($sql,__FILE__,__LINE__); $row=Database::fetch_array($result); $forum_category=$row['forum_category']; } else { return get_lang('Error'); } // determine if need to sort ascending or descending if ($direction=='down') { $sort_direction='ASC'; } elseif ($direction=='up') { $sort_direction='DESC'; } else { return get_lang('Error'); } // The SQL statement if ($content=='forumcategory') { $sql="SELECT * FROM".$table_categories." forum_categories, ".$table_item_property." item_properties WHERE forum_categories.cat_id=item_properties.ref AND item_properties.tool='".TOOL_FORUM_CATEGORY."' ORDER BY forum_categories.cat_order $sort_direction"; } if ($content=='forum') { $sql="SELECT * FROM".$table." WHERE forum_category='".Database::escape_string($forum_category)."' ORDER BY forum_order $sort_direction"; } // echo $sql.'
'; // finding the items that need to be switched $result=Database::query($sql,__FILE__,__LINE__); $found=false; while ($row=Database::fetch_array($result)) { //echo $row[$id_column].'-'; if ($found==true) { $next_id=$row[$id_column]; $next_sort=$row[$sort_column]; $found=false; } if($id==$row[$id_column]) { $this_id=$id; $this_sort=$row[$sort_column]; $found=true; } } // Committing the switch // we do an extra check if we do not have illegal values. If your remove this if statment you will // be able to mess with the sorting by refreshing the page over and over again. if ($this_sort<>'' && $next_sort<>'' && $next_id<>'' && $this_id<>'') { $sql_update1="UPDATE $table SET $sort_column='".Database::escape_string($this_sort)."' WHERE $id_column='".Database::escape_string($next_id)."'"; $sql_update2="UPDATE $table SET $sort_column='".Database::escape_string($next_sort)."' WHERE $id_column='".Database::escape_string($this_id)."'"; Database::query($sql_update1,__FILE__,__LINE__); Database::query($sql_update2,__FILE__,__LINE__); } return get_lang(ucfirst($content).'Moved'); } /** * This function returns a piece of html code that make the links grey (=invisible for the student) * * @param boolean 0/1: 0 = invisible, 1 = visible * @return string * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 */ function class_visible_invisible($current_visibility_status) { if ($current_visibility_status=='0') { return "class='invisible'"; } } /** * Retrieve all the information off the forum categories (or one specific) for the current course. * The categories are sorted according to their sorting order (cat_order * * @param $id default ''. When an id is passed we only find the information about that specific forum category. If no id is passed we get all the forum categories. * @return an array containing all the information about all the forum categories * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 */ function get_forum_categories($id='') { $table_categories = Database :: get_course_table(TABLE_FORUM_CATEGORY); $table_item_property = Database :: get_course_table(TABLE_ITEM_PROPERTY); $forum_categories_list = array(); //condition for the session $session_id = api_get_session_id(); $condition_session = api_get_session_condition($session_id); if ($id == '') { $sql="SELECT * FROM".$table_categories." forum_categories, ".$table_item_property." item_properties WHERE forum_categories.cat_id=item_properties.ref AND item_properties.visibility=1 AND item_properties.tool='".TOOL_FORUM_CATEGORY."' $condition_session ORDER BY forum_categories.cat_order ASC"; if (is_allowed_to_edit()) { $sql="SELECT * FROM".$table_categories." forum_categories, ".$table_item_property." item_properties WHERE forum_categories.cat_id=item_properties.ref AND item_properties.visibility<>2 AND item_properties.tool='".TOOL_FORUM_CATEGORY."' $condition_session ORDER BY forum_categories.cat_order ASC"; } } else { $sql="SELECT * FROM".$table_categories." forum_categories, ".$table_item_property." item_properties WHERE forum_categories.cat_id=item_properties.ref AND item_properties.tool='".TOOL_FORUM_CATEGORY."' AND forum_categories.cat_id='".Database::escape_string($id)."' $condition_session ORDER BY forum_categories.cat_order ASC"; } $result=Database::query($sql,__FILE__,__LINE__); while ($row=Database::fetch_array($result)) { if ($id=='') { $forum_categories_list[$row['cat_id']]=$row; } else { $forum_categories_list=$row; } } return $forum_categories_list; } /** * This function retrieves all the fora in a given forum category * * @param integer $cat_id the id of the forum category * @return an array containing all the information about the forums (regardless of their category) * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 */ function get_forums_in_category($cat_id) { global $table_forums; global $table_item_property; $forum_list=array(); $sql="SELECT * FROM ".$table_forums." forum , ".$table_item_property." item_properties WHERE forum.forum_category='".Database::escape_string($cat_id)."' AND forum.forum_id=item_properties.ref AND item_properties.visibility=1 AND item_properties.tool='".TOOL_FORUM."' ORDER BY forum.forum_order ASC"; if (is_allowed_to_edit()) { $sql="SELECT * FROM ".$table_forums." forum , ".$table_item_property." item_properties WHERE forum.forum_category='".Database::escape_string($cat_id)."' AND forum.forum_id=item_properties.ref AND item_properties.visibility<>2 AND item_properties.tool='".TOOL_FORUM."' ORDER BY forum_order ASC"; } $result=Database::query($sql,__FILE__,__LINE__); while ($row=Database::fetch_array($result)) { $forum_list[$row['forum_id']]=$row; } return $forum_list; } /** * Retrieve all the forums (regardless of their category) or of only one. The forums are sorted according to the forum_order. * Since it does not take the forum category into account there probably will be two or more forums that have forum_order=1, ... * * @return an array containing all the information about the forums (regardless of their category) * @todo check $sql4 because this one really looks fishy. * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 */ function get_forums($id='') { global $table_forums; global $table_threads; global $table_posts; global $table_item_property; global $table_users; // **************** GETTING ALL THE FORUMS ************************* // //condition for the session $session_id = api_get_session_id(); $condition_session = api_get_session_condition($session_id); $forum_list = array(); if ($id=='') { //-------------- Student -----------------// // select all the forum information of all forums (that are visible to students) $sql="SELECT * FROM ".$table_forums." forum , ".$table_item_property." item_properties WHERE forum.forum_id=item_properties.ref AND item_properties.visibility=1 AND item_properties.tool='".TOOL_FORUM."' $condition_session ORDER BY forum.forum_order ASC"; // select the number of threads of the forums (only the threads that are visible) $sql2="SELECT count(*) AS number_of_threads, threads.forum_id FROM $table_threads threads, ".$table_item_property." item_properties WHERE threads.thread_id=item_properties.ref AND item_properties.visibility=1 AND item_properties.tool='".TOOL_FORUM_THREAD."' GROUP BY threads.forum_id"; // select the number of posts of the forum (post that are visible and that are in a thread that is visible) $sql3="SELECT count(*) AS number_of_posts, posts.forum_id FROM $table_posts posts, $table_threads threads, ".$table_item_property." item_properties WHERE posts.visible=1 AND posts.thread_id=threads.thread_id AND threads.thread_id=item_properties.ref AND item_properties.visibility=1 AND item_properties.tool='".TOOL_FORUM_THREAD."' GROUP BY threads.forum_id"; //-------------- Course Admin -----------------// if (is_allowed_to_edit()) { // select all the forum information of all forums (that are not deleted) $sql="SELECT * FROM ".$table_forums." forum , ".$table_item_property." item_properties WHERE forum.forum_id=item_properties.ref AND item_properties.visibility<>2 AND item_properties.tool='".TOOL_FORUM."' $condition_session ORDER BY forum_order ASC"; //echo $sql.'
'; // select the number of threads of the forums (only the threads that are not deleted) $sql2="SELECT count(*) AS number_of_threads, threads.forum_id FROM $table_threads threads, ".$table_item_property." item_properties WHERE threads.thread_id=item_properties.ref AND item_properties.visibility<>2 AND item_properties.tool='".TOOL_FORUM_THREAD."' GROUP BY threads.forum_id"; //echo $sql2.'
'; // select the number of posts of the forum $sql3="SELECT count(*) AS number_of_posts, posts.forum_id FROM $table_posts posts, $table_threads threads, ".$table_item_property." item_properties WHERE posts.thread_id=threads.thread_id AND threads.thread_id=item_properties.ref AND item_properties.visibility=1 AND item_properties.tool='".TOOL_FORUM_THREAD."' GROUP BY threads.forum_id"; //echo $sql3.'
'; } } // **************** GETTING ONE SPECIFIC FORUM ************************* // // We could do the splitup into student and course admin also but we want to have as much as information about a certain forum as possible // so we do not take too much information into account. This function (or this section of the function) is namely used to fill the forms // when editing a forum (and for the moment it is the only place where we use this part of the function) else { // select all the forum information of the given forum (that is not deleted) $sql="SELECT * FROM ".$table_forums." forum , ".$table_item_property." item_properties WHERE forum.forum_id=item_properties.ref AND forum_id='".Database::escape_string($id)."' AND item_properties.visibility<>2 AND item_properties.tool='".TOOL_FORUM."' $condition_session ORDER BY forum_order ASC"; // select the number of threads of the forum $sql2="SELECT count(*) AS number_of_threads, forum_id FROM $table_threads WHERE forum_id=".Database::escape_string($id)." GROUP BY forum_id"; // select the number of posts of the forum $sql3="SELECT count(*) AS number_of_posts, forum_id FROM $table_posts WHERE forum_id=".Database::escape_string($id)." GROUP BY forum_id"; // select the last post and the poster (note: this is probably no longer needed) $sql4="SELECT post.post_id, post.forum_id, post.poster_id, post.poster_name, post.post_date, users.lastname, users.firstname FROM $table_posts post, $table_users users WHERE forum_id=".Database::escape_string($id)." AND post.poster_id=users.user_id GROUP BY post.forum_id ORDER BY post.post_id ASC"; } // handling all the forum information $result=Database::query($sql,__FILE__,__LINE__); while ($row=Database::fetch_array($result)) { if ($id=='') { $forum_list[$row['forum_id']]=$row; } else { $forum_list=$row; } } // handling the threadcount information $result2=Database::query($sql2,__FILE__,__LINE__); while ($row2=Database::fetch_array($result2)) { if ($id=='') { $forum_list[$row2['forum_id']]['number_of_threads']=$row2['number_of_threads']; } else { $forum_list['number_of_threads']=$row2['number_of_threads'];; } } // handling the postcount information $result3=Database::query($sql3,__FILE__,__LINE__); while ($row3=Database::fetch_array($result3)) { if ($id=='') { if (array_key_exists($row3['forum_id'],$forum_list)) {// this is needed because sql3 takes also the deleted forums into account $forum_list[$row3['forum_id']]['number_of_posts']=$row3['number_of_posts']; } } else { $forum_list['number_of_posts']=$row3['number_of_posts']; } } // finding the last post information (last_post_id, last_poster_id, last_post_date, last_poster_name, last_poster_lastname, last_poster_firstname) if ($id=='') { if(is_array($forum_list)) { foreach ($forum_list as $key=>$value) { $last_post_info_of_forum=get_last_post_information($key,is_allowed_to_edit()); $forum_list[$key]['last_post_id']=$last_post_info_of_forum['last_post_id']; $forum_list[$key]['last_poster_id']=$last_post_info_of_forum['last_poster_id']; $forum_list[$key]['last_post_date']=$last_post_info_of_forum['last_post_date']; $forum_list[$key]['last_poster_name']=$last_post_info_of_forum['last_poster_name']; $forum_list[$key]['last_poster_lastname']=$last_post_info_of_forum['last_poster_lastname']; $forum_list[$key]['last_poster_firstname']=$last_post_info_of_forum['last_poster_firstname']; } } else { $forum_list = array(); } } else { $last_post_info_of_forum=get_last_post_information($id,is_allowed_to_edit()); $forum_list['last_post_id']=$last_post_info_of_forum['last_post_id']; $forum_list['last_poster_id']=$last_post_info_of_forum['last_poster_id']; $forum_list['last_post_date']=$last_post_info_of_forum['last_post_date']; $forum_list['last_poster_name']=$last_post_info_of_forum['last_poster_name']; $forum_list['last_poster_lastname']=$last_post_info_of_forum['last_poster_lastname']; $forum_list['last_poster_firstname']=$last_post_info_of_forum['last_poster_firstname']; } return $forum_list; } /** * This functions gets all the last post information of a certain forum * * @param $forum_id the id of the forum we want to know the last post information of. * @param $show_invisibles * @return array containing all the information about the last post (last_post_id, last_poster_id, last_post_date, last_poster_name, last_poster_lastname, last_poster_firstname) * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 */ function get_last_post_information($forum_id, $show_invisibles=false) { global $table_forums; global $table_threads; global $table_posts; global $table_item_property; global $table_users; $sql="SELECT post.post_id, post.forum_id, post.poster_id, post.poster_name, post.post_date, users.lastname, users.firstname, post.visible, thread_properties.visibility AS thread_visibility, forum_properties.visibility AS forum_visibility FROM $table_posts post, $table_users users, $table_item_property thread_properties, $table_item_property forum_properties WHERE post.forum_id=".Database::escape_string($forum_id)." AND post.poster_id=users.user_id AND post.thread_id=thread_properties.ref AND thread_properties.tool='".TOOL_FORUM_THREAD."' AND post.forum_id=forum_properties.ref AND forum_properties.tool='".TOOL_FORUM."' ORDER BY post.post_id DESC"; $result=Database::query($sql,__FILE__,__LINE__); if ($show_invisibles==true) { $row=Database::fetch_array($result); $return_array['last_post_id']=$row['post_id']; $return_array['last_poster_id']=$row['poster_id']; $return_array['last_post_date']=$row['post_date']; $return_array['last_poster_name']=$row['poster_name']; $return_array['last_poster_lastname']=$row['lastname']; $return_array['last_poster_firstname']=$row['firstname']; return $return_array; } else { // we have to loop through the results to find the first one that is actually visible to students (forum_category, forum, thread AND post are visible) while ($row=Database::fetch_array($result)) { if ($row['visible']=='1' AND $row['thread_visibility']=='1' AND $row['forum_visibility']=='1') { $return_array['last_post_id']=$row['post_id']; $return_array['last_poster_id']=$row['poster_id']; $return_array['last_post_date']=$row['post_date']; $return_array['last_poster_name']=$row['poster_name']; $return_array['last_poster_lastname']=$row['lastname']; $return_array['last_poster_firstname']=$row['firstname']; return $return_array; } } } } /** * Retrieve all the threads of a given forum * * @param * @return an array containing all the information about the threads * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 */ function get_threads($forum_id) { global $table_item_property; global $table_threads; global $table_posts; global $table_users; $thread_list=array(); // important note: it might seem a little bit awkward that we have 'thread.locked as locked' in the sql statement // because we also have thread.* in it. This is because thread has a field locked and post also has the same field // since we are merging these we would have the post.locked value but in fact we want the thread.locked value // This is why it is added to the end of the field selection $sql = "SELECT thread.*, item_properties.*, post.*, users.firstname, users.lastname, users.user_id, last_poster_users.firstname as last_poster_firstname , last_poster_users.lastname as last_poster_lastname, last_poster_users.user_id as last_poster_user_id, thread.locked as locked FROM $table_threads thread INNER JOIN $table_item_property item_properties ON thread.thread_id=item_properties.ref AND item_properties.visibility='1' AND item_properties.tool='".TABLE_FORUM_THREAD."' LEFT JOIN $table_users users ON thread.thread_poster_id=users.user_id LEFT JOIN $table_posts post ON thread.thread_last_post = post.post_id LEFT JOIN $table_users last_poster_users ON post.poster_id= last_poster_users.user_id WHERE thread.forum_id='".Database::escape_string($forum_id)."' ORDER BY thread.thread_sticky DESC, thread.thread_date DESC"; if (is_allowed_to_edit()) { // important note: it might seem a little bit awkward that we have 'thread.locked as locked' in the sql statement // because we also have thread.* in it. This is because thread has a field locked and post also has the same field // since we are merging these we would have the post.locked value but in fact we want the thread.locked value // This is why it is added to the end of the field selection $sql = "SELECT thread.*, item_properties.*, post.*, users.firstname, users.lastname, users.user_id, last_poster_users.firstname as last_poster_firstname , last_poster_users.lastname as last_poster_lastname, last_poster_users.user_id as last_poster_user_id, thread.locked as locked FROM $table_threads thread INNER JOIN $table_item_property item_properties ON thread.thread_id=item_properties.ref AND item_properties.visibility<>2 AND item_properties.tool='".TABLE_FORUM_THREAD."' LEFT JOIN $table_users users ON thread.thread_poster_id=users.user_id LEFT JOIN $table_posts post ON thread.thread_last_post = post.post_id LEFT JOIN $table_users last_poster_users ON post.poster_id= last_poster_users.user_id WHERE thread.forum_id='".Database::escape_string($forum_id)."' ORDER BY thread.thread_sticky DESC, thread.thread_date DESC"; } $result=Database::query($sql, __FILE__, __LINE__); while ( $row=Database::fetch_array($result,'ASSOC') ) { $thread_list[]=$row; } return $thread_list; } /** * Retrieve all posts of a given thread * * @return an array containing all the information about the posts of a given thread * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 */ function get_posts($thread_id) { global $table_posts; global $table_users; // note: change these SQL so that only the relevant fields of the user table are used if (api_is_allowed_to_edit(null,true)) { $sql = "SELECT * FROM $table_posts posts LEFT JOIN $table_users users ON posts.poster_id=users.user_id WHERE posts.thread_id='".Database::escape_string($thread_id)."' ORDER BY posts.post_id ASC"; } else { // students can only se the posts that are approved (posts.visible='1') $sql = "SELECT * FROM $table_posts posts LEFT JOIN $table_users users ON posts.poster_id=users.user_id WHERE posts.thread_id='".Database::escape_string($thread_id)."' AND posts.visible='1' ORDER BY posts.post_id ASC"; } $result=Database::query($sql, __FILE__, __LINE__); while ($row=Database::fetch_array($result)) { $post_list[]=$row; } return $post_list; } /** * This function return the html syntax for the image * * @param $image_url The url of the image (absolute or relative) * @param $alt The alt text (when the images cannot be displayed). http://www.w3.org/TR/html4/struct/objects.html#adef-alt * @param $title The title of the image. Most browsers display this as 'tool tip'. http://www.w3.org/TR/html4/struct/global.html#adef-title * * @todo this is the same as the Display::xxx function, so it can be removed => all calls have to be changed also * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 */ function icon($image_url,$alt='',$title='') { if ($title=='') { $title=$alt; } return ''.$alt.''; } /************************************************************************** NEW TOPIC FUNCTIONS **************************************************************************/ /** * This function retrieves all the information of a post * * @param $forum_id integer that indicates the forum * @return array returns * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 */ function get_post_information($post_id) { global $table_posts; global $table_users; $sql="SELECT * FROM ".$table_posts."posts, ".$table_users." users WHERE posts.poster_id=users.user_id AND posts.post_id='".Database::escape_string($post_id)."'"; $result=Database::query($sql, __FILE__, __LINE__); $row=Database::fetch_array($result); return $row; } /** * This function retrieves all the information of a thread * * @param $forum_id integer that indicates the forum * @return array returns * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 */ function get_thread_information($thread_id) { global $table_threads; global $table_item_property; $sql="SELECT * FROM ".$table_threads." threads, ".$table_item_property." item_properties WHERE item_properties.tool='".TOOL_FORUM_THREAD."' AND item_properties.ref='".Database::escape_string($thread_id)."' AND threads.thread_id='".Database::escape_string($thread_id)."'"; $result=Database::query($sql, __FILE__, __LINE__); $row=Database::fetch_array($result); return $row; } /** * This function retrieves forum thread users details * @param int Thread ID * @param string Course DB name (optional) * @return array Array of type ([user_id=>w,lastname=>x,firstname=>y,thread_id=>z],[]) * @author Christian Fasanando , * @version octubre 2008, dokeos 1.8 */ function get_thread_users_details($thread_id, $db_name = null) { $t_posts = Database :: get_course_table(TABLE_FORUM_POST, (empty($db_name)?null:$db_name)); $t_users = Database :: get_main_table(TABLE_MAIN_USER); $t_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER); $sql = "SELECT DISTINCT user.user_id, user.lastname, user.firstname, thread_id FROM $t_posts , $t_users user, $t_course_user course_user WHERE poster_id = user.user_id AND user.user_id = course_user.user_id AND thread_id = '".Database::escape_string($thread_id)."' AND course_user.status NOT IN('1') AND course_code = '".api_get_course_id()."'"; $result = Database::query($sql, __FILE__, __LINE__); return $result; } /** * This function retrieves forum thread users qualify * @param int Thread ID * @param string Course DB name (optional) * @return array Array of type ([user_id=>w,lastname=>x,firstname=>y,thread_id=>z],[]) * @author Jhon Hinojosa, * @version octubre 2008, dokeos 1.8 */ function get_thread_users_qualify($thread_id, $db_name = null) { $t_posts = Database :: get_course_table(TABLE_FORUM_POST, (empty($db_name)?null:$db_name)); $t_qualify = Database :: get_course_table(TABLE_FORUM_THREAD_QUALIFY, (empty($db_name)?null:$db_name)); $t_users = Database :: get_main_table(TABLE_MAIN_USER); $t_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER); $sql = "SELECT post.poster_id, user.lastname, user.firstname, post.thread_id,user.user_id,qualify.qualify FROM $t_posts post, $t_qualify qualify, $t_users user, $t_course_user course_user WHERE post.poster_id = user.user_id AND post.poster_id = qualify.user_id AND user.user_id = course_user.user_id AND qualify.thread_id = '".Database::escape_string($thread_id)."' AND course_user.status not in('1') AND course_code = '".api_get_course_id()."' GROUP BY post.poster_id "; $result = Database::query($sql, __FILE__, __LINE__); return $result; } /** * This function retrieves forum thread users not qualify * @param int Thread ID * @param string Course DB name (optional) * @return array Array of type ([user_id=>w,lastname=>x,firstname=>y,thread_id=>z],[]) * @author Jhon Hinojosa, * @version octubre 2008, dokeos 1.8 */ function get_thread_users_not_qualify($thread_id, $db_name = null) { $t_posts = Database :: get_course_table(TABLE_FORUM_POST, (empty($db_name)?null:$db_name)); $t_qualify = Database :: get_course_table(TABLE_FORUM_THREAD_QUALIFY, (empty($db_name)?null:$db_name)); $t_users = Database :: get_main_table(TABLE_MAIN_USER); $t_course_user = Database :: get_main_table(TABLE_MAIN_COURSE_USER); $sql1 = "select user_id FROM $t_qualify WHERE thread_id = '".$thread_id."'"; $result1 = Database::query($sql1,__FILE__,__LINE__); $cad=''; while ($row=Database::fetch_array($result1)) { $cad .= $row['user_id'].','; } if($cad=='') { $cad='0'; } else { $cad=substr($cad,0,strlen($cad)-1); } $sql = "SELECT DISTINCT user.user_id, user.lastname, user.firstname, post.thread_id FROM $t_posts post, $t_users user,$t_course_user course_user WHERE post.poster_id = user.user_id AND user.user_id NOT IN (".$cad.") AND user.user_id = course_user.user_id AND post.thread_id = '".Database::escape_string($thread_id)."' AND course_user.status not in('1') AND course_code = '".api_get_course_id()."'"; $result = Database::query($sql, __FILE__, __LINE__); return $result; } /** * This function retrieves all the information of a given forum_id * * @param $forum_id integer that indicates the forum * @return array returns * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 * * @deprecated this functionality is now moved to get_forums($forum_id) */ function get_forum_information($forum_id) { global $table_forums; global $table_item_property; $sql="SELECT * FROM ".$table_forums." forums, ".$table_item_property." item_properties WHERE item_properties.tool='".TOOL_FORUM."' AND item_properties.ref='".Database::escape_string($forum_id)."' AND forums.forum_id='".Database::escape_string($forum_id)."'"; $result=Database::query($sql, __FILE__, __LINE__); $row=Database::fetch_array($result); $row['approval_direct_post'] = 0; // we can't anymore change this option, so it should always be activated return $row; } /** * This function retrieves all the information of a given forumcategory id * * @param $forum_id integer that indicates the forum * @return array returns * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 */ function get_forumcategory_information($cat_id) { global $table_categories; global $table_item_property; $sql="SELECT * FROM ".$table_categories." forumcategories, ".$table_item_property." item_properties WHERE item_properties.tool='".TOOL_FORUM_CATEGORY."' AND item_properties.ref='".Database::escape_string($cat_id)."' AND forumcategories.cat_id='".Database::escape_string($cat_id)."'"; $result=Database::query($sql, __FILE__, __LINE__); $row=Database::fetch_array($result); return $row; } /** * This function counts the number of forums inside a given category * * @param $cat_id the id of the forum category * @todo an additional parameter that takes the visibility into account. For instance $countinvisible=0 would return the number * of visible forums, $countinvisible=1 would return the number of visible and invisible forums * @return int the number of forums inside the given category * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 */ function count_number_of_forums_in_category($cat_id) { global $table_forums; $sql="SELECT count(*) AS number_of_forums FROM ".$table_forums." WHERE forum_category='".Database::escape_string($cat_id)."'"; $result=Database::query($sql, __FILE__, __LINE__); $row=Database::fetch_array($result); return $row['number_of_forums']; } /** * This function stores a new thread. This is done through an entry in the forum_thread table AND * in the forum_post table because. The threads are also stored in the item_property table. (forum posts are not (yet)) * * @param * @return * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 */ function store_thread($values) { global $table_threads; global $table_posts; global $_user; global $_course; global $current_forum; global $origin; global $forum_table_attachment; $gradebook=Security::remove_XSS($_GET['gradebook']); $upload_ok=1; $has_attachment=false; if(!empty($_FILES['user_upload']['name'])) { $upload_ok = process_uploaded_file($_FILES['user_upload']); $has_attachment=true; } if($upload_ok) { $post_date=date('Y-m-d H:i:s'); if ($current_forum['approval_direct_post']=='1' AND !api_is_allowed_to_edit(null,true)) { $visible=0; // the post is not approved yet. } else { $visible=1; } $clean_post_title=Database::escape_string(Security::remove_XSS($values['post_title'])); // We first store an entry in the forum_thread table because the thread_id is used in the forum_post table $sql="INSERT INTO $table_threads (thread_title, forum_id, thread_poster_id, thread_poster_name, thread_date, thread_sticky,thread_title_qualify,thread_qualify_max,thread_weight,session_id) VALUES ('".$clean_post_title."', '".Database::escape_string($values['forum_id'])."', '".Database::escape_string($_user['user_id'])."', '".Database::escape_string(isset($values['poster_name'])?$values['poster_name']:null)."', '".Database::escape_string($post_date)."', '".Database::escape_string(isset($values['thread_sticky'])?$values['thread_sticky']:null)."'," . "'".Database::escape_string($values['calification_notebook_title'])."'," . "'".Database::escape_string($values['numeric_calification'])."'," . "'".Database::escape_string($values['weight_calification'])."'," . "'".api_get_session_id()."')"; $result=Database::query($sql, __LINE__, __FILE__); $last_thread_id=Database::insert_id(); //add option gradebook qualify if(isset($values['thread_qualify_gradebook']) && 1==$values['thread_qualify_gradebook']) { //add function gradebook $coursecode=api_get_course_id(); $resourcetype=5; $resourceid=$last_thread_id; $resourcename=$values['calification_notebook_title']; $maxqualify=$values['numeric_calification']; $weigthqualify=$values['weight_calification']; $resourcedescription=''; $date=time(); //is_resource_in_course_gradebook($course_code, $resource_type, $resource_id); add_resource_to_course_gradebook($coursecode,$resourcetype,$resourceid,$resourcename,$weigthqualify,$maxqualify,$resourcedescription,$date,0,api_get_session_id()); } api_item_property_update($_course, TOOL_FORUM_THREAD, $last_thread_id,"ForumThreadAdded", api_get_user_id()); // if the forum properties tell that the posts have to be approved we have to put the whole thread invisible // because otherwise the students will see the thread and not the post in the thread. // we also have to change $visible because the post itself has to be visible in this case (otherwise the teacher would have // to make the thread visible AND the post if ($visible==0) { api_item_property_update($_course, TOOL_FORUM_THREAD, $last_thread_id,"invisible", api_get_user_id()); $visible=1; } // We now store the content in the table_post table $sql="INSERT INTO $table_posts (post_title, post_text, thread_id, forum_id, poster_id, poster_name, post_date, post_notification, post_parent_id, visible) VALUES ('".$clean_post_title."', '".Database::escape_string(Security::remove_XSS(stripslashes(api_html_entity_decode($values['post_text'])),COURSEMANAGERLOWSECURITY))."', '".Database::escape_string($last_thread_id)."', '".Database::escape_string($values['forum_id'])."', '".Database::escape_string($_user['user_id'])."', '".Database::escape_string(isset($values['poster_name'])?$values['poster_name']:null)."', '".Database::escape_string($post_date)."', '".Database::escape_string(isset($values['post_notification'])?$values['post_notification']:null)."','0', '".Database::escape_string($visible)."')"; Database::query($sql, __FILE__,__LINE__); $last_post_id=Database::insert_id(); // now have to update the thread table to fill the thread_last_post field (so that we know when the thread has been updated for the last time) $sql="UPDATE $table_threads SET thread_last_post='".Database::escape_string($last_post_id)."' WHERE thread_id='".Database::escape_string($last_thread_id)."'"; $result=Database::query($sql, __LINE__, __FILE__); $message=get_lang('NewThreadStored'); // Storing the attachments if any if ($has_attachment) { $courseDir = $_course['path'].'/upload/forum'; $sys_course_path = api_get_path(SYS_COURSE_PATH); $updir = $sys_course_path.$courseDir; // Try to add an extension to the file if it hasn't one $new_file_name = add_ext_on_mime(stripslashes($_FILES['user_upload']['name']), $_FILES['user_upload']['type']); // user's file name $file_name =$_FILES['user_upload']['name']; if (!filter_extension($new_file_name)) { Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension')); } else { if ($result) { $comment = Database::escape_string($comment); add_forum_attachment_file($comment,$last_post_id); } } } else { $message.='
'; } if ($current_forum['approval_direct_post']=='1' AND !api_is_allowed_to_edit(null,true)) { $message.=get_lang('MessageHasToBeApproved').'
'; $message.=get_lang('ReturnTo').' '.get_lang('Forum').'
'; } else { $message.=get_lang('ReturnTo').' '.get_lang('Forum').'
'; $message.=get_lang('ReturnTo').' '.get_lang('Message').''; } $reply_info['new_post_id'] = $last_post_id; $my_post_notification=isset($values['post_notification']) ? $values['post_notification'] : null; if ($my_post_notification == 1) { set_notification('thread',$last_thread_id, true); } send_notification_mails($last_thread_id,$reply_info); session_unregister('formelements'); session_unregister('origin'); session_unregister('breadcrumbs'); session_unregister('addedresource'); session_unregister('addedresourceid'); Display :: display_confirmation_message($message,false); } else { Display::display_error_message(get_lang('UplNoFileUploaded')); } } /** * This function displays the form that is used to add a post. This can be a new thread or a reply. * @param $action is the parameter that determines if we are * 1. newthread: adding a new thread (both empty) => No I-frame * 2. replythread: Replying to a thread ($action = replythread) => I-frame with the complete thread (if enabled) * 3. replymessage: Replying to a message ($action =replymessage) => I-frame with the complete thread (if enabled) (I first thought to put and I-frame with the message only) * 4. quote: Quoting a message ($action= quotemessage) => I-frame with the complete thread (if enabled). The message will be in the reply. (I first thought not to put an I-frame here) * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 */ function show_add_post_form($action='', $id='', $form_values='') { global $forum_setting; global $current_forum; global $_user; global $origin; global $charset; $gradebook=Security::remove_XSS($_GET['gradebook']); // setting the class and text of the form title and submit button if ($_GET['action']=='quote'){ $class='save'; $text=get_lang('QuoteMessage'); }elseif ($_GET['action'] == 'replythread'){ $class='save'; $text=get_lang('ReplyToThread'); }elseif ($_GET['action']=='replymessage'){ $class='save'; $text=get_lang('ReplyToMessage'); }else { $class='add'; $text=get_lang('CreateThread'); } // initiate the object $my_thread = isset($_GET['thread']) ? $_GET['thread']:''; $my_forum = isset($_GET['forum']) ? $_GET['forum']:''; $my_action = isset($_GET['action']) ? $_GET['action']:''; $my_post = isset($_GET['post']) ? $_GET['post']:''; $my_gradebook = isset($_GET['gradebook']) ? Security::remove_XSS($_GET['gradebook']):''; $form = new FormValidator('thread', 'post', api_get_self().'?forum='.Security::remove_XSS($my_forum).'&gradebook='.$gradebook.'&thread='.Security::remove_XSS($my_thread).'&post='.Security::remove_XSS($my_post).'&action='.Security::remove_XSS($my_action).'&origin='.$origin); $form->setConstants(array('forum' => '5')); $form->addElement('header', '', $text); // settting the form elements $form->addElement('hidden', 'forum_id', strval(intval($my_forum))); $form->addElement('hidden', 'thread_id', strval(intval($my_thread))); $form->addElement('hidden', 'gradebook', $my_gradebook); // if anonymous posts are allowed we also display a form to allow the user to put his name or username in if ($current_forum['allow_anonymous']==1 AND !isset($_user['user_id'])) { $form->addElement('text', 'poster_name', get_lang('Name')); $form->applyFilter('poster_name', 'html_filter'); } $form->addElement('text', 'post_title', get_lang('Title'),'class="input_titles"'); //$form->applyFilter('post_title', 'html_filter'); $form->addElement('html_editor', 'post_text', get_lang('Text'), null, api_is_allowed_to_edit(null,true) ? array('ToolbarSet' => 'Forum', 'Width' => '100%', 'Height' => '400') : array('ToolbarSet' => 'ForumStudent', 'Width' => '100%', 'Height' => '400', 'UserStatus' => 'student') ); //$form->applyFilter('post_text', 'html_filter'); $form->addElement('html', ''); $form->addElement('html',''); $userid =api_get_user_id(); $info =api_get_user_info($userid); $courseid=api_get_course_id(); $form->addElement('style_submit_button', 'SubmitPost', $text, 'class="'.$class.'"'); $form->add_real_progress_bar('DocumentUpload','user_upload'); if ( !empty($form_values) ) { $defaults['post_title']=prepare4display(Security::remove_XSS($form_values['post_title'])); $defaults['post_text']=prepare4display(Security::remove_XSS($form_values['post_text'])); $defaults['post_notification']=Security::remove_XSS($form_values['post_notification']); $defaults['thread_sticky']=Security::remove_XSS($form_values['thread_sticky']); } // if we are quoting a message we have to retrieve the information of the post we are quoting so that // we can add this as default to the textarea if (($action=='quote' || $action=='replymessage') && isset($my_post)) { // we also need to put the parent_id of the post in a hidden form when we are quoting or replying to a message (<> reply to a thread !!!) $form->addElement('hidden', 'post_parent_id', strval(intval($my_post))); // note this has to be cleaned first // if we are replying or are quoting then we display a default title. $values=get_post_information($my_post); // note: this has to be cleaned first $defaults['post_title']=get_lang('ReplyShort').api_html_entity_decode($values['post_title'],ENT_QUOTES,$charset); // When we are quoting a message then we have to put that message into the wysiwyg editor. // note: the style has to be hardcoded here because using class="quote" didn't work if($action=='quote') { $defaults['post_text']='
 
'.get_lang('Quoting').' '.api_get_person_name($values['firstname'], $values['lastname']).':
'.prepare4display($values['post_text']).'
 
 
'; } } $form->setDefaults(isset($defaults)?$defaults:null); // the course admin can make a thread sticky (=appears with special icon and always on top) $form->addRule('post_title', '
'.get_lang('ThisFieldIsRequired'), 'required'); if ($current_forum['allow_anonymous']==1 AND !isset($_user['user_id'])) { $form->addRule('poster_name', '
'.get_lang('ThisFieldIsRequired'), 'required'); } // The validation or display if( $form->validate() ) { $check = Security::check_token('post'); if ($check) { $values = $form->exportValues(); if($values['thread_qualify_gradebook']=='1' && empty($values['weight_calification'])){ Display::display_error_message(get_lang('YouMustAssignWeightOfQualification').' '.get_lang('Back').'',false); return false; } Security::clear_token(); return $values; } } else { $token = Security::get_token(); $form->addElement('hidden','sec_token'); $form->setConstants(array('sec_token' => $token)); $form->display(); echo '
'; if ($forum_setting['show_thread_iframe_on_reply'] and $action<>'newthread') { echo '
'.get_lang('Thread').'
'; echo ""; echo '
'; } } } /** * @param integer contains the information of user id * @param integer contains the information of thread id * @param integer contains the information of thread qualify * @param integer contains the information of user id of qualifier * @param integer contains the information of time * @param integer contains the information of session id * @return Array() optional * @author Isaac Flores , U.N.A.S University * @version October 2008, dokeos 1.8.6 **/ function store_theme_qualify($user_id,$thread_id,$thread_qualify=0,$qualify_user_id=0,$qualify_time,$session_id=null) { $table_threads_qualify = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY,''); $table_threads =Database::get_course_table(TABLE_FORUM_THREAD,''); if ($user_id==strval(intval($user_id)) && $thread_id==strval(intval($thread_id)) && $thread_qualify==strval(floatval($thread_qualify))) { //testing $sql_string="SELECT thread_qualify_max FROM ". $table_threads ." WHERE thread_id=".$thread_id.";"; $res_string=Database::query($sql_string,__FILE__,__LINE__); $row_string=Database::fetch_array($res_string); if ($thread_qualify<=$row_string[0]) { $sql1="SELECT COUNT(*) FROM ".$table_threads_qualify." WHERE user_id=".$user_id." and thread_id=".$thread_id.";"; $res1=Database::query($sql1); $row=Database::fetch_array($res1); if ($row[0]==0) { $sql="INSERT INTO $table_threads_qualify (user_id," . "thread_id,qualify,qualify_user_id,qualify_time,session_id)" . "VALUES('".$user_id."','".$thread_id."',".(float)$thread_qualify."," . "'".$qualify_user_id."','".$qualify_time."','".$session_id."')"; $res=Database::query($sql,__FILE__,__LINE__); return $res; } else { $sql1="SELECT qualify FROM ".$table_threads_qualify." WHERE user_id=".$user_id." and thread_id=".$thread_id.";"; $rs=Database::query($sql1,__FILE__,__LINE__); $row=Database::fetch_array($rs); $row[1]="update"; return $row; } }else{ return null; } } } /** * This function show qualify. * @param string contains the information of option to run * @param string contains the information the current course id * @param integer contains the information the current forum id * @param integer contains the information the current user id * @param integer contains the information the current thread id * @return integer qualify * @example $option=1 obtained the qualification of the current thread * @author Isaac Flores , U.N.A.S University * @version October 2008, dokeos 1.8.6 */ function show_qualify($option,$couser_id,$forum_id,$user_id,$thread_id){ $table_threads_qualify = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY,''); $table_threads =Database::get_course_table(TABLE_FORUM_THREAD,''); if ($user_id==strval(intval($user_id)) && $thread_id==strval(intval($thread_id)) && $option==1) { $sql="SELECT qualify FROM ".$table_threads_qualify." WHERE user_id=".$user_id." and thread_id=".$thread_id.";"; $rs=Database::query($sql,__FILE__,__LINE__); $row=Database::fetch_array($rs); return $row[0]; } if ($user_id==strval(intval($user_id)) && $option==2) { $sql="SELECT thread_qualify_max FROM ".$table_threads." WHERE thread_id=".$thread_id.";"; $rs=Database::query($sql,__FILE__,__LINE__); $row=Database::fetch_array($rs); return $row[0]; } } /** * * This function get qualify historical. * @param integer contains the information the current user id * @param integer contains the information the current thread id * @param boolean contains the information of option to run * @return array() * @author Christian Fasanando , * @author Isaac Flores , * @version October 2008, dokeos 1.8.6 */ function get_historical_qualify($user_id,$thread_id,$opt) { $my_qualify_log=array(); $table_threads_qualify_log = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY_LOG,''); $opt = Database::escape_string($opt); if ($opt=='false') { $sql="SELECT * FROM ".$table_threads_qualify_log." WHERE thread_id='".Database::escape_string($thread_id)."' and user_id='".Database::escape_string($user_id)."' ORDER BY qualify_time"; } else { $sql="SELECT * FROM ".$table_threads_qualify_log." WHERE thread_id='".Database::escape_string($thread_id)."' and user_id='".Database::escape_string($user_id)."' ORDER BY qualify_time DESC"; } $rs=Database::query($sql,__FILE__,__LINE__); while ($row=Database::fetch_array($rs,'ASSOC')) { $my_qualify_log[]=$row; } return $my_qualify_log; } /** * * This function store qualify historical. * @param boolean contains the information of option to run * @param string contains the information the current course id * @param integer contains the information the current forum id * @param integer contains the information the current user id * @param integer contains the information the current thread id * @param integer contains the information the current qualify * @return void * @example $option=1 obtained the qualification of the current thread * @author Isaac Flores , U.N.A.S University * @version October 2008, dokeos 1.8.6 */ function store_qualify_historical($option,$couser_id,$forum_id,$user_id,$thread_id,$current_qualify,$qualify_user_id) { $table_threads_qualify = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY,''); $table_threads =Database::get_course_table(TABLE_FORUM_THREAD,''); $table_threads_qualify_log=Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY_LOG,''); $current_date=date('Y-m-d H:i:s'); if ($user_id==strval(intval($user_id)) && $thread_id==strval(intval($thread_id)) && $option==1) { //extract information of thread_qualify $sql="SELECT qualify,qualify_time FROM ".$table_threads_qualify." WHERE user_id=".$user_id." and thread_id=".$thread_id.";"; $rs=Database::query($sql,__FILE__,__LINE__); $row=Database::fetch_array($rs); //insert thread_historical $sql1="INSERT INTO $table_threads_qualify_log (user_id," . "thread_id,qualify,qualify_user_id,qualify_time,session_id)" . "VALUES('".$user_id."','".$thread_id."',".(float)$row[0]."," . "'".$qualify_user_id."','".$row[1]."','')"; Database::query($sql1,__FILE__,__LINE__); //update $sql2="UPDATE ".$table_threads_qualify." SET qualify=".$current_qualify.",qualify_time='".$current_date."' WHERE user_id=".$user_id." and thread_id=".$thread_id.";"; Database::query($sql2,__FILE__,__LINE__); } } /** * * This function show current thread qualify . * @param integer contains the information the current thread id * @param integer contains the information the current session id * @return integer * @author Isaac Flores , U.N.A.S University * @version December 2008, dokeos 1.8.6 */ function current_qualify_of_thread($thread_id,$session_id) { $table_threads_qualify = Database::get_course_table(TABLE_FORUM_THREAD_QUALIFY,''); $res=Database::query('SELECT qualify FROM '.$table_threads_qualify.' WHERE thread_id='.$thread_id.' AND session_id='.$session_id); $row=Database::fetch_array($res,'ASSOC'); return $row['qualify']; } /** * This function stores a reply in the forum_post table. * It also updates the forum_threads table (thread_replies +1 , thread_last_post, thread_date) * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 */ function store_reply($values) { global $table_threads; global $table_posts; global $forum_table_attachment; global $_user; global $_course; global $current_forum; global $origin; $gradebook=Security::remove_XSS($_GET['gradebook']); $post_date=date('Y-m-d H:i:s'); if ($current_forum['approval_direct_post']=='1' AND !api_is_allowed_to_edit(null,true)) { $visible=0; // the post is not approved yet. } else { $visible=1; } $upload_ok=1; $has_attachment=false; if (!empty($_FILES['user_upload']['name'])) { $upload_ok = process_uploaded_file($_FILES['user_upload']); $has_attachment=true; } if ($upload_ok) { // We first store an entry in the forum_post table $sql="INSERT INTO $table_posts (post_title, post_text, thread_id, forum_id, poster_id, post_date, post_notification, post_parent_id, visible) VALUES ('".Database::escape_string(Security::remove_XSS($values['post_title']))."', '".Database::escape_string(isset($values['post_text']) ? Security::remove_XSS(stripslashes(api_html_entity_decode($values['post_text'])),COURSEMANAGERLOWSECURITY) : null)."', '".Database::escape_string($values['thread_id'])."', '".Database::escape_string($values['forum_id'])."', '".Database::escape_string($_user['user_id'])."', '".Database::escape_string($post_date)."', '".Database::escape_string(isset($values['post_notification'])?$values['post_notification']:null)."', '".Database::escape_string(isset($values['post_parent_id'])?$values['post_parent_id']:null)."', '".Database::escape_string($visible)."')"; $result=Database::query($sql, __LINE__, __FILE__); $new_post_id=Database::insert_id(); $values['new_post_id']=$new_post_id; $message=get_lang('ReplyAdded'); if ($has_attachment) { $courseDir = $_course['path'].'/upload/forum'; $sys_course_path = api_get_path(SYS_COURSE_PATH); $updir = $sys_course_path.$courseDir; // Try to add an extension to the file if it hasn't one $new_file_name = add_ext_on_mime(stripslashes($_FILES['user_upload']['name']), $_FILES['user_upload']['type']); // user's file name $file_name =$_FILES['user_upload']['name']; if (!filter_extension($new_file_name)) { Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension')); } else { $new_file_name = uniqid(''); $new_path=$updir.'/'.$new_file_name; $result= @move_uploaded_file($_FILES['user_upload']['tmp_name'], $new_path); $comment=$values['file_comment']; // Storing the attachments if any if ($result) { $sql='INSERT INTO '.$forum_table_attachment.'(filename,comment, path, post_id,size) '. "VALUES ( '".Database::escape_string($file_name)."', '".Database::escape_string($comment)."', '".Database::escape_string($new_file_name)."' , '".$new_post_id."', '".$_FILES['user_upload']['size']."' )"; $result=Database::query($sql, __LINE__, __FILE__); $message.=' / '.get_lang('FileUploadSucces'); $last_id=Database::insert_id(); api_item_property_update($_course, TOOL_FORUM_ATTACH, $last_id ,'ForumAttachmentAdded', api_get_user_id()); } } } // update the thread update_thread($values['thread_id'], $new_post_id,$post_date); // update the forum api_item_property_update($_course, TOOL_FORUM, $values['forum_id'],"NewMessageInForum", api_get_user_id()); if ($current_forum['approval_direct_post']=='1' AND !api_is_allowed_to_edit(null,true)) { $message.='
'.get_lang('MessageHasToBeApproved').'
'; } $message.='
'.get_lang('ReturnTo').' '.get_lang('Forum').'
'; $message.=get_lang('ReturnTo').' '.get_lang('Message').''; // setting the notification correctly $my_post_notification=isset($values['post_notification']) ? $values['post_notification'] :null; if ($my_post_notification == 1) { set_notification('thread',$values['thread_id'], true); } send_notification_mails($values['thread_id'], $values); session_unregister('formelements'); session_unregister('origin'); session_unregister('breadcrumbs'); session_unregister('addedresource'); session_unregister('addedresourceid'); Display :: display_confirmation_message($message,false); } else { Display::display_error_message(get_lang('UplNoFileUploaded')." ". get_lang('UplSelectFileFirst')); } } /** * This function displays the form that is used to edit a post. This can be a new thread or a reply. * @param array contains all the information about the current post * @param array contains all the information about the current thread * @param array contains all info about the current forum (to check if attachments are allowed) * @param array contains the default values to fill the form * @return void * * @author Patrick Cool , Ghent University * @version february 2006, dokeos 1.8 */ function show_edit_post_form($current_post, $current_thread, $current_forum, $form_values='',$id_attach=0) { global $forum_setting; global $_user; global $origin; $gradebook=Security::remove_XSS($_GET['gradebook']); // initiate the object $form = new FormValidator('edit_post', 'post', api_get_self().'?forum='.Security::remove_XSS($_GET['forum']).'&gradebook='.$gradebook.'&origin='.$origin.'&thread='.Security::remove_XSS($_GET['thread']).'&post='.Security::remove_XSS($_GET['post'])); $form->addElement('header', '', get_lang('EditPost')); // settting the form elements $form->addElement('hidden', 'post_id', $current_post['post_id']); $form->addElement('hidden', 'thread_id', $current_thread['thread_id']); $form->addElement('hidden', 'id_attach', $id_attach); if ($current_post['post_parent_id']==0) { $form->addElement('hidden', 'is_first_post_of_thread', '1'); } $form->addElement('text', 'post_title', get_lang('Title'),'class="input_titles"'); $form->applyFilter('post_title', 'html_filter'); $form->addElement('html_editor', 'post_text', get_lang('Text'), null, api_is_allowed_to_edit(null,true) ? array('ToolbarSet' => 'Forum', 'Width' => '100%', 'Height' => '400') : array('ToolbarSet' => 'ForumStudent', 'Width' => '100%', 'Height' => '400', 'UserStatus' => 'student') ); //$form->applyFilter('post_text', 'html_filter'); $form->addElement('html', ''); $form->addElement('html','