blog.lib.php 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Blog class
  5. * Contains several functions dealing with displaying,
  6. * editing,... of a blog
  7. *
  8. * @version 1.0
  9. * @package chamilo.blogs
  10. * @author Toon Keppens <toon@vi-host.net>
  11. * @author Julio Montoya - Cleaning code
  12. *
  13. */
  14. /**
  15. * Class
  16. * @package chamilo.blogs
  17. */
  18. class Blog {
  19. /**
  20. * Get the title of a blog
  21. * @author Toon Keppens
  22. *
  23. * @param Integer $blog_id
  24. *
  25. * @return String Blog Title
  26. */
  27. public static function get_blog_title ($blog_id) {
  28. $course_id = api_get_course_int_id();
  29. if(is_numeric($blog_id)) {
  30. // init
  31. $tbl_blogs = Database::get_course_table(TABLE_BLOGS);
  32. $sql = "SELECT blog_name
  33. FROM " . $tbl_blogs . "
  34. WHERE c_id = $course_id AND blog_id = " . intval($blog_id);
  35. $result = Database::query($sql);
  36. $blog = Database::fetch_array($result);
  37. return stripslashes($blog['blog_name']);
  38. }
  39. }
  40. /**
  41. * Get the description of a blog
  42. * @author Toon Keppens
  43. *
  44. * @param Integer $blog_id
  45. *
  46. * @return String Blog description
  47. */
  48. public static function get_blog_subtitle ($blog_id) {
  49. // init
  50. $tbl_blogs = Database::get_course_table(TABLE_BLOGS);
  51. $course_id = api_get_course_int_id();
  52. $sql = "SELECT blog_subtitle FROM $tbl_blogs WHERE c_id = $course_id AND blog_id ='".intval($blog_id)."'";
  53. $result = Database::query($sql);
  54. $blog = Database::fetch_array($result);
  55. return stripslashes($blog['blog_subtitle']);
  56. }
  57. /**
  58. * Get the users of a blog
  59. * @author Toon Keppens
  60. *
  61. * @param Integer $blog_id
  62. *
  63. * @return Array Returns an array with [userid]=>[username]
  64. */
  65. public static function get_blog_users ($blog_id) {
  66. // Database table definitions
  67. $tbl_users = Database::get_main_table(TABLE_MAIN_USER);
  68. $tbl_blogs_rel_user = Database::get_course_table(TABLE_BLOGS_REL_USER);
  69. $course_id = api_get_course_int_id();
  70. // Get blog members
  71. $sql = "SELECT user.user_id, user.firstname, user.lastname
  72. FROM " . $tbl_blogs_rel_user . " blogs_rel_user INNER JOIN " . $tbl_users . " user ON blogs_rel_user.user_id = user.user_id
  73. WHERE blogs_rel_user.c_id = $course_id AND
  74. blogs_rel_user.blog_id = '" . (int)$blog_id."'";
  75. $result = Database::query($sql);
  76. $blog_members = array ();
  77. while($user = Database::fetch_array($result)) {
  78. $blog_members[$user['user_id']] = api_get_person_name($user['firstname'], $user['lastname']);
  79. }
  80. return $blog_members;
  81. }
  82. /**
  83. * Creates a new blog in the given course
  84. * @author Toon Keppens
  85. * @param Integer $course_id Id
  86. * @param String $title
  87. * @param Text $description
  88. */
  89. public static function create_blog ($title, $subtitle) {
  90. global $_user;
  91. $course_id = api_get_course_int_id();
  92. $current_date=date('Y-m-d H:i:s',time());
  93. $session_id = api_get_session_id();
  94. // Tabel definitions
  95. $tbl_blogs = Database::get_course_table(TABLE_BLOGS);
  96. $tbl_tool = Database::get_course_table(TABLE_TOOL_LIST);
  97. $tbl_blogs_posts = Database::get_course_table(TABLE_BLOGS_POSTS);
  98. $tbl_blogs_tasks = Database::get_course_table(TABLE_BLOGS_TASKS);
  99. //verified if exist blog
  100. $sql='SELECT COUNT(*) as count FROM '.$tbl_blogs.' WHERE c_id = '.$course_id.' AND blog_name="'.Database::escape_string($title).'" AND blog_subtitle="'.Database::escape_string($subtitle).'";';
  101. $res=Database::query($sql);
  102. $info_count=Database::result($res,0,0);
  103. if ($info_count==0) {
  104. // Create the blog
  105. $sql = "INSERT INTO $tbl_blogs (c_id, blog_name, blog_subtitle, date_creation, visibility, session_id )
  106. VALUES ($course_id, '".Database::escape_string($title)."', '".Database::escape_string($subtitle)."', '".$current_date."', '1', '$session_id');";
  107. Database::query($sql);
  108. $this_blog_id = Database::insert_id();
  109. if ($this_blog_id > 0) {
  110. //insert into item_property
  111. api_item_property_update(api_get_course_info(), TOOL_BLOGS, $this_blog_id, 'BlogAdded', api_get_user_id());
  112. }
  113. // Make first post. :)
  114. $sql = "INSERT INTO $tbl_blogs_posts (c_id, title, full_text, date_creation, blog_id, author_id )
  115. VALUES ($course_id, '".get_lang("Welcome")."', '" . get_lang('FirstPostText')."','".$current_date."', '".Database::escape_string((int)$this_blog_id)."', '".Database::escape_string((int)$_user['user_id'])."');";
  116. Database::query($sql);
  117. // Put it on course homepage
  118. $sql = "INSERT INTO $tbl_tool (c_id, name, link, image, visibility, admin, address, added_tool, session_id)
  119. VALUES ($course_id, '".Database::escape_string($title)."','blog/blog.php?blog_id=".(int)$this_blog_id."','blog.gif','1','0','pastillegris.gif',0,'$session_id')";
  120. Database::query($sql);
  121. // Subscribe the teacher to this blog
  122. Blog::set_user_subscribed((int)$this_blog_id,(int)$_user['user_id']);
  123. }
  124. }
  125. /**
  126. * Update title and subtitle of a blog in the given course
  127. * @author Toon Keppens
  128. * @param Integer $course_id Id
  129. * @param String $title
  130. * @param Text $description
  131. */
  132. public static function edit_blog ($blog_id, $title, $subtitle) {
  133. global $_user;
  134. // Table definitions
  135. $tbl_blogs = Database::get_course_table(TABLE_BLOGS);
  136. $tbl_tool = Database::get_course_table(TABLE_TOOL_LIST);
  137. $course_id = api_get_course_int_id();
  138. // Update the blog
  139. $sql = "UPDATE $tbl_blogs SET blog_name = '".Database::escape_string($title)."', blog_subtitle = '".Database::escape_string($subtitle)."'
  140. WHERE c_id = $course_id AND blog_id ='".Database::escape_string((int)$blog_id)."' LIMIT 1";
  141. Database::query($sql);
  142. $this_blog_id = Database::insert_id();
  143. //update item_property (update)
  144. api_item_property_update(api_get_course_info(), TOOL_BLOGS, Database::escape_string($blog_id), 'BlogUpdated', api_get_user_id());
  145. // Update course homepage link
  146. $sql = "UPDATE $tbl_tool SET name = '".Database::escape_string($title)."' WHERE c_id = $course_id AND link = 'blog/blog.php?blog_id=".Database::escape_string((int)$blog_id)."' LIMIT 1";
  147. Database::query($sql);
  148. }
  149. /**
  150. * Deletes a blog and it's posts from the course database
  151. * @author Toon Keppens
  152. * @param Integer $blog_id
  153. */
  154. public static function delete_blog ($blog_id) {
  155. // Init
  156. $tbl_blogs = Database::get_course_table(TABLE_BLOGS);
  157. $tbl_blogs_posts = Database::get_course_table(TABLE_BLOGS_POSTS);
  158. $tbl_blogs_comment = Database::get_course_table(TABLE_BLOGS_COMMENTS);
  159. $tbl_blogs_tasks = Database::get_course_table(TABLE_BLOGS_TASKS);
  160. $tbl_tool = Database::get_course_table(TABLE_TOOL_LIST);
  161. $tbl_blogs_rating = Database::get_course_table(TABLE_BLOGS_RATING);
  162. $tbl_blogs_attachment = Database::get_course_table(TABLE_BLOGS_ATTACHMENT);
  163. $course_id = api_get_course_int_id();
  164. // Delete posts from DB and the attachments
  165. delete_all_blog_attachment($blog_id);
  166. //Delete comments
  167. $sql = "DELETE FROM $tbl_blogs_comment WHERE c_id = $course_id AND blog_id ='".(int)$blog_id."'";
  168. Database::query($sql);
  169. // Delete posts
  170. $sql = "DELETE FROM $tbl_blogs_posts WHERE c_id = $course_id AND blog_id ='".(int)$blog_id."'";
  171. Database::query($sql);
  172. // Delete tasks
  173. $sql = "DELETE FROM $tbl_blogs_tasks WHERE c_id = $course_id AND blog_id ='".(int)$blog_id."'";
  174. Database::query($sql);
  175. // Delete ratings
  176. $sql = "DELETE FROM $tbl_blogs_rating WHERE c_id = $course_id AND blog_id ='".(int)$blog_id."'";
  177. Database::query($sql);
  178. // Delete blog
  179. $sql ="DELETE FROM $tbl_blogs WHERE c_id = $course_id AND blog_id ='".(int)$blog_id."'";
  180. Database::query($sql);
  181. // Delete from course homepage
  182. $sql = "DELETE FROM $tbl_tool WHERE c_id = $course_id AND link = 'blog/blog.php?blog_id=".(int)$blog_id."'";
  183. Database::query($sql);
  184. //update item_property (delete)
  185. api_item_property_update(api_get_course_info(), TOOL_BLOGS, Database::escape_string($blog_id), 'delete', api_get_user_id());
  186. }
  187. /**
  188. * Creates a new post in a given blog
  189. * @author Toon Keppens
  190. * @param String $title
  191. * @param String $full_text
  192. * @param Integer $blog_id
  193. */
  194. public static function create_post ($title, $full_text, $file_comment, $blog_id) {
  195. global $_user;
  196. global $_course;
  197. $blog_table_attachment = Database::get_course_table(TABLE_BLOGS_ATTACHMENT);
  198. $upload_ok=true;
  199. $has_attachment=false;
  200. $current_date=date('Y-m-d H:i:s',time());
  201. $course_id = api_get_course_int_id();
  202. if(!empty($_FILES['user_upload']['name'])) {
  203. require_once 'fileUpload.lib.php';
  204. $upload_ok = process_uploaded_file($_FILES['user_upload']);
  205. $has_attachment=true;
  206. }
  207. if ($upload_ok) {
  208. // Table Definitions
  209. $tbl_blogs_posts = Database::get_course_table(TABLE_BLOGS_POSTS);
  210. // Create the post
  211. $sql = "INSERT INTO $tbl_blogs_posts (c_id, title, full_text, date_creation, blog_id, author_id )
  212. VALUES ($course_id, '".Database::escape_string($title)."', '".Database::escape_string($full_text)."','".$current_date."', '".(int)$blog_id."', '".(int)$_user['user_id']."');";
  213. Database::query($sql);
  214. $last_post_id=Database::insert_id();
  215. if ($has_attachment) {
  216. $courseDir = $_course['path'].'/upload/blog';
  217. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  218. $updir = $sys_course_path.$courseDir;
  219. // Try to add an extension to the file if it hasn't one
  220. $new_file_name = add_ext_on_mime(stripslashes($_FILES['user_upload']['name']), $_FILES['user_upload']['type']);
  221. // user's file name
  222. $file_name =$_FILES['user_upload']['name'];
  223. if (!filter_extension($new_file_name)) {
  224. Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  225. } else {
  226. $new_file_name = uniqid('');
  227. $new_path=$updir.'/'.$new_file_name;
  228. $result= @move_uploaded_file($_FILES['user_upload']['tmp_name'], $new_path);
  229. $comment=Database::escape_string($file_comment);
  230. // Storing the attachments if any
  231. if ($result) {
  232. $sql='INSERT INTO '.$blog_table_attachment.'(c_id, filename,comment, path, post_id,size, blog_id,comment_id) '.
  233. "VALUES ($course_id, '".Database::escape_string($file_name)."', '".Database::escape_string($comment)."', '".Database::escape_string($new_file_name)."' , '".$last_post_id."', '".intval($_FILES['user_upload']['size'])."', '".$blog_id."', '0' )";
  234. $result=Database::query($sql);
  235. $message.=' / '.get_lang('AttachmentUpload');
  236. }
  237. }
  238. }
  239. } else {
  240. Display::display_error_message(get_lang('UplNoFileUploaded'));
  241. }
  242. }
  243. /**
  244. * Edits a post in a given blog
  245. * @author Toon Keppens
  246. * @param Integer $blog_id
  247. * @param String $title
  248. * @param String $full_text
  249. * @param Integer $blog_id
  250. */
  251. public static function edit_post ($post_id, $title, $full_text, $blog_id) {
  252. // Init
  253. $tbl_blogs_posts = Database::get_course_table(TABLE_BLOGS_POSTS);
  254. $course_id = api_get_course_int_id();
  255. // Create the post
  256. $sql = "UPDATE $tbl_blogs_posts SET title = '" . Database::escape_string($title)."', full_text = '" . Database::escape_string($full_text)."'
  257. WHERE c_id = $course_id AND post_id ='".(int)$post_id."' AND blog_id ='".(int)$blog_id."' LIMIT 1 ;";
  258. Database::query($sql);
  259. }
  260. /**
  261. * Deletes an article and it's comments
  262. * @author Toon Keppens
  263. * @param Integer $blog_id
  264. * @param Integer $post_id
  265. */
  266. public static function delete_post($blog_id, $post_id) {
  267. // Init
  268. $tbl_blogs_posts = Database::get_course_table(TABLE_BLOGS_POSTS);
  269. $tbl_blogs_comments = Database::get_course_table(TABLE_BLOGS_COMMENTS);
  270. $tbl_blogs_rating = Database::get_course_table(TABLE_BLOGS_RATING);
  271. $course_id = api_get_course_int_id();
  272. // Delete ratings on this comment
  273. $sql = "DELETE FROM $tbl_blogs_rating WHERE c_id = $course_id AND blog_id = '".(int)$blog_id."' AND item_id = '".(int)$post_id."' AND rating_type = 'post'";
  274. Database::query($sql);
  275. // Delete the post
  276. $sql = "DELETE FROM $tbl_blogs_posts WHERE c_id = $course_id AND post_id = '".(int)$post_id."'";
  277. Database::query($sql);
  278. // Delete the comments
  279. $sql = "DELETE FROM $tbl_blogs_comments WHERE c_id = $course_id AND post_id = '".(int)$post_id."' AND blog_id = '".(int)$blog_id."'";
  280. Database::query($sql);
  281. // Delete posts and attachments
  282. delete_all_blog_attachment($blog_id,$post_id);
  283. }
  284. /**
  285. * Creates a comment on a post in a given blog
  286. * @author Toon Keppens
  287. * @param String $title
  288. * @param String $full_text
  289. * @param Integer $blog_id
  290. * @param Integer $post_id
  291. * @param Integer $parent_id
  292. */
  293. public static function create_comment($title, $full_text, $file_comment,$blog_id, $post_id, $parent_id, $task_id = 'NULL') {
  294. global $_user;
  295. global $_course;
  296. global $blog_table_attachment;
  297. $upload_ok = true;
  298. $has_attachment = false;
  299. $current_date = date('Y-m-d H:i:s',time());
  300. $course_id = api_get_course_int_id();
  301. if(!empty($_FILES['user_upload']['name'])) {
  302. require_once('fileUpload.lib.php');
  303. $upload_ok = process_uploaded_file($_FILES['user_upload']);
  304. $has_attachment=true;
  305. }
  306. if ($upload_ok) {
  307. // Table Definition
  308. $tbl_blogs_comments = Database::get_course_table(TABLE_BLOGS_COMMENTS);
  309. // Create the comment
  310. $sql = "INSERT INTO $tbl_blogs_comments (c_id, title, comment, author_id, date_creation, blog_id, post_id, parent_comment_id, task_id )
  311. VALUES ($course_id, '".Database::escape_string($title)."', '".Database::escape_string($full_text)."', '".(int)$_user['user_id']."','".$current_date."', '".(int)$blog_id."', '".(int)$post_id."', '".(int)$parent_id."', '".(int)$task_id."')";
  312. Database::query($sql);
  313. // Empty post values, or they are shown on the page again
  314. $_POST['comment_title'] = "";
  315. $_POST['comment_text'] = "";
  316. $last_id=Database::insert_id();
  317. if ($has_attachment) {
  318. $courseDir = $_course['path'].'/upload/blog';
  319. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  320. $updir = $sys_course_path.$courseDir;
  321. // Try to add an extension to the file if it hasn't one
  322. $new_file_name = add_ext_on_mime(stripslashes($_FILES['user_upload']['name']), $_FILES['user_upload']['type']);
  323. // user's file name
  324. $file_name =$_FILES['user_upload']['name'];
  325. if (!filter_extension($new_file_name))
  326. {
  327. Display :: display_error_message(get_lang('UplUnableToSaveFileFilteredExtension'));
  328. }
  329. else
  330. {
  331. $new_file_name = uniqid('');
  332. $new_path=$updir.'/'.$new_file_name;
  333. $result= @move_uploaded_file($_FILES['user_upload']['tmp_name'], $new_path);
  334. $comment=Database::escape_string($file_comment);
  335. // Storing the attachments if any
  336. if ($result)
  337. {
  338. $sql='INSERT INTO '.$blog_table_attachment.'(c_id, filename,comment, path, post_id,size,blog_id,comment_id) '.
  339. "VALUES ($course_id, '".Database::escape_string($file_name)."', '".Database::escape_string($comment)."', '".Database::escape_string($new_file_name)."' , '".$post_id."', '".$_FILES['user_upload']['size']."', '".$blog_id."', '".$last_id."' )";
  340. $result=Database::query($sql);
  341. $message.=' / '.get_lang('AttachmentUpload');
  342. }
  343. }
  344. }
  345. }
  346. }
  347. /**
  348. * Deletes a comment from a blogpost
  349. * @author Toon Keppens
  350. * @param Integer $blog_id
  351. * @param Integer $comment_id
  352. */
  353. public static function delete_comment ($blog_id, $post_id, $comment_id) {
  354. // Init
  355. $tbl_blogs_comments = Database::get_course_table(TABLE_BLOGS_COMMENTS);
  356. $tbl_blogs_rating = Database::get_course_table(TABLE_BLOGS_RATING);
  357. $blog_id = Database::escape_string($blog_id);
  358. $post_id = Database::escape_string($post_id);
  359. $comment_id = Database::escape_string($comment_id);
  360. $course_id = api_get_course_int_id();
  361. delete_all_blog_attachment($blog_id,$post_id,$comment_id);
  362. // Delete ratings on this comment
  363. $sql = "DELETE FROM $tbl_blogs_rating WHERE c_id = $course_id AND blog_id = '".(int)$blog_id."' AND item_id = '".(int)$comment_id."' AND rating_type = 'comment'";
  364. Database::query($sql);
  365. // select comments that have the selected comment as their parent
  366. $sql = "SELECT comment_id FROM $tbl_blogs_comments WHERE c_id = $course_id AND parent_comment_id = '".(int)$comment_id."'";
  367. $result = Database::query($sql);
  368. // Delete them recursively
  369. while($comment = Database::fetch_array($result)) {
  370. Blog::delete_comment($blog_id,$post_id,$comment['comment_id']);
  371. }
  372. // Finally, delete the selected comment to
  373. $sql = "DELETE FROM $tbl_blogs_comments WHERE c_id = $course_id AND comment_id = '".(int)$comment_id."'";
  374. Database::query($sql);
  375. }
  376. /**
  377. * Creates a new task in a blog
  378. * @author Toon Keppens
  379. * @param Integer $blog_id
  380. * @param String $title
  381. * @param String $description
  382. * @param String $color
  383. */
  384. public static function create_task ($blog_id, $title, $description, $articleDelete, $articleEdit, $commentsDelete, $color) {
  385. // Init
  386. $tbl_blogs_tasks = Database::get_course_table(TABLE_BLOGS_TASKS);
  387. $tbl_tasks_permissions = Database::get_course_table(TABLE_BLOGS_TASKS_PERMISSIONS);
  388. $course_id = api_get_course_int_id();
  389. // Create the task
  390. $sql = "INSERT INTO $tbl_blogs_tasks (c_id, blog_id, title, description, color, system_task )
  391. VALUES ($course_id , '".(int)$blog_id."', '" . Database::escape_string($title)."', '" . Database::escape_string($description)."', '" . Database::escape_string($color)."', '0');";
  392. Database::query($sql);
  393. $task_id = Database::insert_id();
  394. $tool = 'BLOG_' . $blog_id;
  395. if ($articleDelete == 'on') {
  396. $sql = " INSERT INTO " . $tbl_tasks_permissions . " ( c_id, task_id, tool, action) VALUES (
  397. '" . (int)$course_id . "',
  398. '" . (int)$task_id . "',
  399. '" . Database::escape_string($tool) . "',
  400. 'article_delete'
  401. )";
  402. Database::query($sql);
  403. }
  404. if($articleEdit == 'on') {
  405. $sql = "
  406. INSERT INTO " . $tbl_tasks_permissions . " (c_id, task_id, tool, action ) VALUES (
  407. '" . (int)$course_id . "',
  408. '" . (int)$task_id . "',
  409. '" . Database::escape_string($tool) . "',
  410. 'article_edit'
  411. )";
  412. Database::query($sql);
  413. }
  414. if($commentsDelete == 'on') {
  415. $sql = "
  416. INSERT INTO " . $tbl_tasks_permissions . " (c_id, task_id, tool, action ) VALUES (
  417. '" . (int)$course_id . "',
  418. '" . (int)$task_id . "',
  419. '" . Database::escape_string($tool) . "',
  420. 'article_comments_delete'
  421. )";
  422. Database::query($sql);
  423. }
  424. }
  425. /**
  426. * Edit a task in a blog
  427. * @author Toon Keppens
  428. * @param Integer $task_id
  429. * @param String $title
  430. * @param String $description
  431. * @param String $color
  432. */
  433. public static function edit_task($blog_id, $task_id, $title, $description, $articleDelete, $articleEdit, $commentsDelete, $color) {
  434. // Init
  435. $tbl_blogs_tasks = Database::get_course_table(TABLE_BLOGS_TASKS);
  436. $tbl_tasks_permissions = Database::get_course_table(TABLE_BLOGS_TASKS_PERMISSIONS);
  437. $course_id = api_get_course_int_id();
  438. // Create the task
  439. $sql = "UPDATE $tbl_blogs_tasks SET
  440. title = '".Database::escape_string($title)."',
  441. description = '".Database::escape_string($description)."',
  442. color = '".Database::escape_string($color)."'
  443. WHERE c_id = $course_id AND task_id ='".(int)$task_id."' LIMIT 1";
  444. Database::query($sql);
  445. $tool = 'BLOG_' . $blog_id;
  446. $sql = "DELETE FROM " . $tbl_tasks_permissions . " WHERE c_id = $course_id AND task_id = '" . (int)$task_id."'";
  447. Database::query($sql);
  448. if ($articleDelete == 'on') {
  449. $sql = "INSERT INTO " . $tbl_tasks_permissions . " ( c_id, task_id, tool, action) VALUES (
  450. '" . (int)$course_id . "',
  451. '" . (int)$task_id . "',
  452. '" . Database::escape_string($tool) . "',
  453. 'article_delete'
  454. )";
  455. Database::query($sql);
  456. }
  457. if($articleEdit == 'on') {
  458. $sql = "INSERT INTO " . $tbl_tasks_permissions . " (c_id, task_id, tool, action) VALUES (
  459. '" . (int)$course_id . "',
  460. '" . (int)$task_id . "',
  461. '" . Database::escape_string($tool) . "',
  462. 'article_edit'
  463. )";
  464. Database::query($sql);
  465. }
  466. if($commentsDelete == 'on') {
  467. $sql = " INSERT INTO " . $tbl_tasks_permissions . " (c_id, task_id, tool, action) VALUES (
  468. '" . (int)$course_id . "',
  469. '" . (int)$task_id . "',
  470. '" . Database::escape_string($tool) . "',
  471. 'article_comments_delete'
  472. )";
  473. Database::query($sql);
  474. }
  475. }
  476. /**
  477. * Deletes a task from a blog
  478. * @param Integer $blog_id
  479. * @param Integer $task_id
  480. */
  481. public static function delete_task ($blog_id, $task_id) {
  482. $tbl_blogs_tasks = Database::get_course_table(TABLE_BLOGS_TASKS);
  483. $course_id = api_get_course_int_id();
  484. // Delete posts
  485. $sql = "DELETE FROM $tbl_blogs_tasks WHERE c_id = $course_id AND blog_id = '".(int)$blog_id."' AND task_id = '".(int)$task_id."'";
  486. Database::query($sql);
  487. }
  488. /**
  489. * Deletes an assigned task from a blog
  490. * @param Integer $blog_id
  491. * @param Integer $assignment_id
  492. */
  493. public static function delete_assigned_task ($blog_id, $task_id,$user_id) {
  494. $tbl_blogs_tasks_rel_user = Database::get_course_table(TABLE_BLOGS_TASKS_REL_USER);
  495. $course_id = api_get_course_int_id();
  496. // Delete posts
  497. $sql = "DELETE FROM $tbl_blogs_tasks_rel_user WHERE c_id = $course_id AND blog_id = '".(int)$blog_id."' AND task_id = '".(int)$task_id."' AND user_id = '".(int)$user_id."'";
  498. Database::query($sql);
  499. }
  500. /**
  501. * Get personal task list
  502. * @author Toon Keppens
  503. * @return Returns an unsorted list (<ul></ul>) with the users' tasks
  504. */
  505. public static function get_personal_task_list () {
  506. global $_user;
  507. // Init
  508. $tbl_blogs = Database::get_course_table(TABLE_BLOGS);
  509. $tbl_blogs_tasks_rel_user = Database::get_course_table(TABLE_BLOGS_TASKS_REL_USER);
  510. $tbl_blogs_tasks = Database::get_course_table(TABLE_BLOGS_TASKS);
  511. $course_id = api_get_course_int_id();
  512. if($_user['user_id']) {
  513. $sql = "SELECT task_rel_user.*, task.title, blog.blog_name FROM $tbl_blogs_tasks_rel_user task_rel_user
  514. INNER JOIN $tbl_blogs_tasks task ON task_rel_user.task_id = task.task_id
  515. INNER JOIN $tbl_blogs blog ON task_rel_user.blog_id = blog.blog_id
  516. AND blog.blog_id = ".intval($_GET['blog_id'])."
  517. WHERE
  518. task.c_id = $course_id AND
  519. blog.c_id = $course_id AND
  520. task_rel_user.c_id = $course_id AND
  521. task_rel_user.user_id = ".(int)$_user['user_id']."
  522. ORDER BY target_date ASC";
  523. $result = Database::query($sql);
  524. if (Database::num_rows($result) > 0) {
  525. echo '<ul>';
  526. while($mytask = Database::fetch_array($result)) {
  527. echo '<li><a href="blog.php?action=execute_task&amp;blog_id=' . $mytask['blog_id'] . '&amp;task_id='.stripslashes($mytask['task_id']) . '" title="[Blog: '.stripslashes($mytask['blog_name']) . '] ' . get_lang('ExecuteThisTask') . '">'.stripslashes($mytask['title']) . '</a></li>';
  528. }
  529. echo '<ul>';
  530. }
  531. else
  532. {
  533. echo get_lang('NoTasks');
  534. }
  535. }
  536. else
  537. {
  538. echo get_lang('NoTasks');
  539. }
  540. }
  541. /**
  542. * Changes the visibility of a blog
  543. * @author Toon Keppens
  544. * @param Integer $blog_id
  545. */
  546. public static function change_blog_visibility ($blog_id) {
  547. // Init
  548. $tbl_blogs = Database::get_course_table(TABLE_BLOGS);
  549. $tbl_tool = Database::get_course_table(TABLE_TOOL_LIST);
  550. $course_id = api_get_course_int_id();
  551. // Get blog properties
  552. $sql = "SELECT blog_name, visibility FROM $tbl_blogs WHERE c_id = $course_id AND blog_id='".(int)$blog_id."'";
  553. $result = Database::query($sql);
  554. $blog = Database::fetch_array($result);
  555. $visibility = $blog['visibility'];
  556. $title = $blog['blog_name'];
  557. if($visibility == 1)
  558. {
  559. // Change visibility state, remove from course home.
  560. $sql = "UPDATE $tbl_blogs SET visibility = '0' WHERE c_id = $course_id AND blog_id ='".(int)$blog_id."' LIMIT 1";
  561. $result = Database::query($sql);
  562. $sql = "DELETE FROM $tbl_tool WHERE c_id = $course_id AND name = '".Database::escape_string($title)."' LIMIT 1";
  563. $result = Database::query($sql);
  564. }
  565. else
  566. {
  567. // Change visibility state, add to course home.
  568. $sql = "UPDATE $tbl_blogs SET visibility = '1' WHERE c_id = $course_id AND blog_id ='".(int)$blog_id."' LIMIT 1";
  569. $result = Database::query($sql);
  570. $sql = "INSERT INTO $tbl_tool (c_id, name, link, image, visibility, admin, address, added_tool, target )
  571. VALUES ($course_id, '".Database::escape_string($title)."', 'blog/blog.php?blog_id=".(int)$blog_id."', 'blog.gif', '1', '0', 'pastillegris.gif', '0', '_self')";
  572. $result = Database::query($sql);
  573. }
  574. }
  575. /**
  576. * Shows the posts of a blog
  577. * @author Toon Keppens
  578. *
  579. * @param Integer $blog_id
  580. */
  581. public static function display_blog_posts ($blog_id, $filter = '1=1', $max_number_of_posts = 20) {
  582. // Init
  583. $tbl_blogs_posts = Database::get_course_table(TABLE_BLOGS_POSTS);
  584. $tbl_blogs_comments = Database::get_course_table(TABLE_BLOGS_COMMENTS);
  585. $tbl_users = Database::get_main_table(TABLE_MAIN_USER);
  586. global $dateFormatLong;
  587. $course_id = api_get_course_int_id();
  588. // Get posts and authors
  589. $sql = "SELECT post.*, user.lastname, user.firstname FROM $tbl_blogs_posts post
  590. INNER JOIN $tbl_users user ON post.author_id = user.user_id
  591. WHERE post.blog_id = '".(int)$blog_id."' AND
  592. post.c_id = $course_id AND
  593. $filter
  594. ORDER BY post_id DESC LIMIT 0,".(int)$max_number_of_posts;
  595. $result = Database::query($sql);
  596. // Display
  597. if(Database::num_rows($result) > 0) {
  598. $limit = api_get_setting('read_more_limit'); //nmbr of words in introduction text
  599. if (empty($limit)) {
  600. $limit = 200;
  601. }
  602. while($blog_post = Database::fetch_array($result)) {
  603. // Get number of comments
  604. $sql = "SELECT COUNT(1) as number_of_comments FROM $tbl_blogs_comments WHERE c_id = $course_id AND blog_id = '".(int)$blog_id."' AND post_id = '" . (int)$blog_post['post_id']."'";
  605. $tmp = Database::query($sql);
  606. $blog_post_comments = Database::fetch_array($tmp);
  607. // Prepare data
  608. $blog_post_id = $blog_post['post_id'];
  609. $blog_post_text = make_clickable(stripslashes($blog_post['full_text']));
  610. $blog_post_date = api_convert_and_format_date($blog_post['date_creation'], null, date_default_timezone_get());
  611. // Create an introduction text (but keep FULL sentences)
  612. $introduction_text = "";
  613. $words = 0;
  614. $blog_post_text_cut = cut($blog_post_text, $limit) ;
  615. $words = strlen($blog_post_text);
  616. if ($words >= $limit) {
  617. $readMoreLink = ' <div class="link" onclick="document.getElementById(\'blogpost_text_' . $blog_post_id . '\').style.display=\'block\'; document.getElementById(\'blogpost_introduction_' . $blog_post_id . '\').style.display=\'none\'">' . get_lang('ReadMore') . '</div>';
  618. $introduction_text = $blog_post_text_cut;
  619. } else {
  620. $introduction_text = $blog_post_text;
  621. $readMoreLink = '';
  622. }
  623. $introduction_text = stripslashes($introduction_text);
  624. echo '<div class="blogpost">';
  625. echo '<span class="blogpost_title"><a href="blog.php?action=view_post&amp;blog_id=' . $blog_id . '&amp;post_id=' . $blog_post['post_id'] . '#add_comment" title="' . get_lang('ReadPost') . '" >'.stripslashes($blog_post['title']) . '</a></span>';
  626. echo '<span class="blogpost_date"><a href="blog.php?action=view_post&amp;blog_id=' . $blog_id . '&amp;post_id=' . $blog_post['post_id'] . '#add_comment" title="' . get_lang('ReadPost') . '" >' . $blog_post_date . '</a></span>';
  627. echo '<div class="blogpost_introduction" id="blogpost_introduction_'.$blog_post_id.'">' . $introduction_text.$readMoreLink.'</div>';
  628. echo '<div class="blogpost_text" id="blogpost_text_' . $blog_post_id . '" style="display: none">' . $blog_post_text . '</div>';
  629. $file_name_array=get_blog_attachment($blog_id,$blog_post_id,0);
  630. if (!empty($file_name_array)) {
  631. echo '<br /><br />';
  632. echo Display::return_icon('attachment.gif',get_lang('Attachment'));
  633. echo '<a href="download.php?file=';
  634. echo $file_name_array['path'];
  635. echo ' "> '.$file_name_array['filename'].' </a><br />';
  636. echo '</span>';
  637. }
  638. echo '<span class="blogpost_info">' . get_lang('Author') . ': ' . api_get_person_name($blog_post['firstname'], $blog_post['lastname']) . ' - <a href="blog.php?action=view_post&amp;blog_id=' . $blog_id . '&amp;post_id=' . $blog_post['post_id'] . '#add_comment" title="' . get_lang('ReadPost') . '" >' . get_lang('Comments') . ': ' . $blog_post_comments['number_of_comments'] . '</a></span>'."\n";
  639. echo '</div>';
  640. }
  641. } else {
  642. if($filter == '1=1') {
  643. echo get_lang('NoArticles');
  644. } else {
  645. echo get_lang('NoArticleMatches');
  646. }
  647. }
  648. }
  649. /**
  650. * Display the search results
  651. *
  652. * @param Integer $blog_id
  653. * @param String $query_string
  654. */
  655. public static function display_search_results ($blog_id, $query_string) {
  656. // Init
  657. $query_string = Database::escape_string($query_string);
  658. $query_string_parts = explode(' ',$query_string);
  659. $query_string = array();
  660. foreach ($query_string_parts as $query_part) {
  661. $query_string[] = " full_text LIKE '%" . $query_part."%' OR title LIKE '%" . $query_part."%' ";
  662. }
  663. $query_string = '('.implode('OR',$query_string) . ')';
  664. // Display the posts
  665. echo '<span class="blogpost_title">' . get_lang('SearchResults') . '</span>';
  666. Blog::display_blog_posts($blog_id, $query_string);
  667. }
  668. /**
  669. * Display posts from a certain date
  670. *
  671. * @param Integer $blog_id
  672. * @param String $query_string
  673. */
  674. public static function display_day_results ($blog_id, $query_string) {
  675. // Init
  676. $date_output = $query_string;
  677. $date = explode('-',$query_string);
  678. $query_string = ' DAYOFMONTH(date_creation) =' . intval($date[2]) . ' AND MONTH(date_creation) =' . intval($date[1]) . ' AND YEAR(date_creation) =' . intval($date[0]);
  679. // Put date in correct output format
  680. $date_output = api_format_date($date_output, DATE_FORMAT_LONG);
  681. // Display the posts
  682. echo '<span class="blogpost_title">' . get_lang('PostsOf') . ': ' . $date_output . '</span>';
  683. Blog::display_blog_posts($blog_id, $query_string);
  684. }
  685. /**
  686. * Displays a post and his comments
  687. *
  688. * @param Integer $blog_id
  689. * @param Integer $post_id
  690. */
  691. public static function display_post ($blog_id, $post_id) {
  692. // Init
  693. $tbl_blogs_posts = Database::get_course_table(TABLE_BLOGS_POSTS);
  694. $tbl_blogs_comments = Database::get_course_table(TABLE_BLOGS_COMMENTS);
  695. $tbl_users = Database::get_main_table(TABLE_MAIN_USER);
  696. global $charset,$dateFormatLong;
  697. $course_id = api_get_course_int_id();
  698. // Get posts and author
  699. $sql = "SELECT post.*, user.lastname, user.firstname FROM $tbl_blogs_posts post
  700. INNER JOIN $tbl_users user ON post.author_id = user.user_id
  701. WHERE
  702. post.c_id = $course_id AND
  703. post.blog_id = '".(int)$blog_id."' AND
  704. post.post_id = '".(int)$post_id."'
  705. ORDER BY post_id DESC";
  706. $result = Database::query($sql);
  707. $blog_post = Database::fetch_array($result);
  708. // Get number of comments
  709. $sql = "SELECT COUNT(1) as number_of_comments FROM $tbl_blogs_comments WHERE c_id = $course_id AND blog_id = '".(int)$blog_id."' AND post_id = '".(int)$post_id."'";
  710. $result = Database::query($sql);
  711. $blog_post_comments = Database::fetch_array($result);
  712. // Prepare data
  713. $blog_post_text = make_clickable(stripslashes($blog_post['full_text']));
  714. $blog_post_date = api_convert_and_format_date($blog_post['date_creation'], null, date_default_timezone_get());
  715. $blog_post_actions = "";
  716. $task_id = (isset($_GET['task_id']) && is_numeric($_GET['task_id'])) ? intval($_GET['task_id']) : 0;
  717. if(api_is_allowed('BLOG_' . $blog_id, 'article_edit', $task_id))
  718. $blog_post_actions .= '<a href="blog.php?action=edit_post&amp;blog_id=' . $blog_id . '&amp;post_id=' . $post_id . '&amp;article_id=' . $blog_post['post_id'] . '&amp;task_id=' . $task_id . '" title="' . get_lang('EditThisPost') . '"><img src="../img/edit.gif" /></a>';
  719. if(api_is_allowed('BLOG_' . $blog_id, 'article_delete', $task_id))
  720. $blog_post_actions .= '<a href="blog.php?action=view_post&amp;blog_id=' . $blog_id . '&amp;post_id=' . $post_id . '&amp;do=delete_article&amp;article_id=' . $blog_post['post_id'] . '&amp;task_id=' . $task_id . '" title="' . get_lang('DeleteThisArticle') . '" onclick="javascript:if(!confirm(\''.addslashes(api_htmlentities(get_lang("ConfirmYourChoice"),ENT_QUOTES,$charset)). '\')) return false;"><img src="../img/delete.gif" border="0" /></a>';
  721. if(api_is_allowed('BLOG_' . $blog_id, 'article_rate'))
  722. $rating_select = Blog::display_rating_form('post',$blog_id,$post_id);
  723. $blog_post_text=stripslashes($blog_post_text);
  724. // Display post
  725. echo '<div class="blogpost">';
  726. echo '<span class="blogpost_title"><a href="blog.php?action=view_post&amp;blog_id=' . $blog_id . '&amp;post_id=' . $blog_post['post_id'] . '" title="' . get_lang('ReadPost') . '" >'.stripslashes($blog_post['title']) . '</a></span>';
  727. echo '<span class="blogpost_date">' . $blog_post_date . '</span>';
  728. echo '<span class="blogpost_text">' . $blog_post_text . '</span><br />';
  729. $file_name_array=get_blog_attachment($blog_id,$post_id);
  730. if (!empty($file_name_array))
  731. {
  732. echo ' <br />';
  733. echo Display::return_icon('attachment.gif',get_lang('Attachment'));
  734. echo '<a href="download.php?file=';
  735. echo $file_name_array['path'];
  736. echo ' "> '.$file_name_array['filename'].' </a>';
  737. echo '</span>';
  738. echo '<span class="attachment_comment">';
  739. echo $file_name_array['comment'];
  740. echo '</span>';
  741. echo '<br />';
  742. }
  743. echo '<span class="blogpost_info">' . get_lang('Author') . ': ' . api_get_person_name($blog_post['firstname'], $blog_post['lastname']) . ' - ' . get_lang('Comments') . ': ' . $blog_post_comments['number_of_comments'] . ' - ' . get_lang('Rating') . ': '.Blog::display_rating('post',$blog_id,$post_id) . $rating_select . '</span>';
  744. echo '<span class="blogpost_actions">' . $blog_post_actions . '</span>';
  745. echo '</div>';
  746. // Display comments if there are any
  747. if($blog_post_comments['number_of_comments'] > 0)
  748. {
  749. echo '<div class="comments">';
  750. echo '<span class="blogpost_title">' . get_lang('Comments') . '</span><br />';
  751. Blog::get_threaded_comments(0, 0, $blog_id, $post_id, $task_id);
  752. echo '</div>';
  753. }
  754. // Display comment form
  755. if(api_is_allowed('BLOG_' . $blog_id, 'article_comments_add'))
  756. {
  757. Blog::display_new_comment_form($blog_id, $post_id, $blog_post['title']);
  758. }
  759. }
  760. /**
  761. * Adds rating to a certain post or comment
  762. * @author Toon Keppens
  763. *
  764. * @param String $type
  765. * @param Integer $blog_id
  766. * @param Integer $item_id
  767. * @param Integer $rating
  768. *
  769. * @return Boolean success
  770. */
  771. public static function add_rating ($type, $blog_id, $item_id, $rating) {
  772. global $_user;
  773. // Init
  774. $tbl_blogs_rating = Database::get_course_table(TABLE_BLOGS_RATING);
  775. $course_id = api_get_course_int_id();
  776. // Check if the user has already rated this post/comment
  777. $sql = "SELECT rating_id FROM $tbl_blogs_rating
  778. WHERE c_id = $course_id AND
  779. blog_id = '".(int)$blog_id."'
  780. AND item_id = '".(int)$item_id."'
  781. AND rating_type = '".Database::escape_string($type)."'
  782. AND user_id = '".(int)$_user['user_id']."'";
  783. $result = Database::query($sql);
  784. if(Database::num_rows($result) == 0) // Add rating
  785. {
  786. $sql = "INSERT INTO $tbl_blogs_rating (c_id, blog_id, rating_type, item_id, user_id, rating )
  787. VALUES ($course_id, '".(int)$blog_id."', '".Database::escape_string($type)."', '".(int)$item_id."', '".(int)$_user['user_id']."', '".Database::escape_string($rating)."')";
  788. $result = Database::query($sql);
  789. return true;
  790. }
  791. else // Return
  792. {
  793. return false;
  794. }
  795. }
  796. /**
  797. * Shows the rating of user
  798. *
  799. * @param String $type
  800. * @param Integer $blog_id
  801. * @param Integer $item_id
  802. *
  803. * @return array()
  804. */
  805. public static function display_rating ($type, $blog_id, $item_id) {
  806. $tbl_blogs_rating = Database::get_course_table(TABLE_BLOGS_RATING);
  807. $course_id = api_get_course_int_id();
  808. // Calculate rating
  809. $sql = "SELECT AVG(rating) as rating FROM $tbl_blogs_rating WHERE c_id = $course_id AND blog_id = '".(int)$blog_id."' AND item_id = '".(int)$item_id."' AND rating_type = '".Database::escape_string($type)."' ";
  810. $result = Database::query($sql);
  811. $result = Database::fetch_array($result);
  812. return round($result['rating'], 2);
  813. }
  814. /**
  815. * Shows the rating form if not already rated by that user
  816. * @author Toon Keppens
  817. *
  818. * @param String $type
  819. * @param Integer $blog_id
  820. * @param Integer $item_id
  821. *
  822. *@return String
  823. */
  824. public static function display_rating_form ($type, $blog_id, $post_id, $comment_id = NULL) {
  825. global $_user;
  826. $tbl_blogs_rating = Database::get_course_table(TABLE_BLOGS_RATING);
  827. $course_id = api_get_course_int_id();
  828. if($type == 'post')
  829. {
  830. // Check if the user has already rated this post
  831. $sql = "SELECT rating_id FROM $tbl_blogs_rating
  832. WHERE c_id = $course_id AND
  833. blog_id = '".(int)$blog_id."'
  834. AND item_id = '".(int)$post_id."'
  835. AND rating_type = '".Database::escape_string($type)."'
  836. AND user_id = '".(int)$_user['user_id']."'";
  837. $result = Database::query($sql);
  838. if(Database::num_rows($result) == 0) // Add rating
  839. {
  840. return ' - ' . get_lang('RateThis') . ': <form method="get" action="blog.php" style="display: inline" id="frm_rating_' . $type . '_' . $post_id . '" name="frm_rating_' . $type . '_' . $post_id . '"><select name="rating" onchange="document.forms[\'frm_rating_' . $type . '_' . $post_id . '\'].submit()"><option value="">-</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option></select><input type="hidden" name="action" value="view_post" /><input type="hidden" name="type" value="' . $type . '" /><input type="hidden" name="do" value="rate" /><input type="hidden" name="blog_id" value="' . $blog_id . '" /><input type="hidden" name="post_id" value="' . $post_id . '" /></form>';
  841. }
  842. else // Return
  843. {
  844. return '';
  845. }
  846. }
  847. if($type = 'comment')
  848. {
  849. // Check if the user has already rated this comment
  850. $sql = "SELECT rating_id FROM $tbl_blogs_rating
  851. WHERE c_id = $course_id AND blog_id = '".(int)$blog_id ."'
  852. AND item_id = '".(int)$comment_id."'
  853. AND rating_type = '".Database::escape_string($type)."'
  854. AND user_id = '".(int)$_user['user_id']."'";
  855. $result = Database::query($sql);
  856. if(Database::num_rows($result) == 0) // Add rating
  857. {
  858. return ' - ' . get_lang('RateThis') . ': <form method="get" action="blog.php" style="display: inline" id="frm_rating_' . $type . '_' . $comment_id . '" name="frm_rating_' . $type . '_' . $comment_id . '"><select name="rating" onchange="document.forms[\'frm_rating_' . $type . '_' . $comment_id . '\'].submit()"><option value="">-</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option></select><input type="hidden" name="action" value="view_post" /><input type="hidden" name="type" value="' . $type . '" /><input type="hidden" name="do" value="rate" /><input type="hidden" name="blog_id" value="' . $blog_id . '" /><input type="hidden" name="post_id" value="' . $post_id . '" /><input type="hidden" name="comment_id" value="' . $comment_id . '" /></form>';
  859. }
  860. else // Return
  861. {
  862. return '';
  863. }
  864. }
  865. }
  866. /**
  867. * This functions gets all replys to a post, threaded.
  868. *
  869. * @param Integer $current
  870. * @param Integer $current_level
  871. * @param Integer $blog_id
  872. * @param Integer $post_id
  873. */
  874. public static function get_threaded_comments ($current = 0, $current_level = 0, $blog_id, $post_id, $task_id = 0) {
  875. // Init
  876. $tbl_blogs_comments = Database::get_course_table(TABLE_BLOGS_COMMENTS);
  877. $tbl_users = Database::get_main_table(TABLE_MAIN_USER);
  878. $tbl_blogs_tasks = Database::get_course_table(TABLE_BLOGS_TASKS);
  879. global $charset,$dateFormatLong;
  880. $course_id = api_get_course_int_id();
  881. // Select top level comments
  882. $next_level = $current_level + 1;
  883. $sql = "SELECT comments.*, user.lastname, user.firstname, task.color
  884. FROM $tbl_blogs_comments comments
  885. INNER JOIN $tbl_users user ON comments.author_id = user.user_id
  886. LEFT JOIN $tbl_blogs_tasks task ON comments.task_id = task.task_id AND task.c_id = $course_id
  887. WHERE comments.c_id = $course_id AND
  888. parent_comment_id = $current AND
  889. comments.blog_id = '".(int)$blog_id."' AND
  890. comments.post_id = '".(int)$post_id."'";
  891. $result = Database::query($sql);
  892. while($comment = Database::fetch_array($result)) {
  893. // Select the children recursivly
  894. $tmp = "SELECT comments.*, user.lastname, user.firstname FROM $tbl_blogs_comments comments
  895. INNER JOIN $tbl_users user ON comments.author_id = user.user_id
  896. WHERE
  897. comments.c_id = $course_id AND
  898. comment_id = $current
  899. AND blog_id = '".(int)$blog_id."'
  900. AND post_id = '".(int)$post_id."'";
  901. $tmp = Database::query($tmp);
  902. $tmp = Database::fetch_array($tmp);
  903. $parent_cat = $tmp['parent_comment_id'];
  904. $border_color = '';
  905. // Prepare data
  906. $comment_text = make_clickable(stripslashes($comment['comment']));
  907. $blog_comment_date = api_convert_and_format_date($comment['date_creation'], null, date_default_timezone_get());
  908. $blog_comment_actions = "";
  909. if(api_is_allowed('BLOG_' . $blog_id, 'article_comments_delete', $task_id)) { $blog_comment_actions .= '<a href="blog.php?action=view_post&amp;blog_id=' . $blog_id . '&amp;post_id=' . $post_id . '&amp;do=delete_comment&amp;comment_id=' . $comment['comment_id'] . '&amp;task_id=' . $task_id . '" title="' . get_lang('DeleteThisComment') . '" onclick="javascript:if(!confirm(\''.addslashes(api_htmlentities(get_lang("ConfirmYourChoice"),ENT_QUOTES,$charset)). '\')) return false;"><img src="../img/delete.gif" border="0" /></a>'; }
  910. if(api_is_allowed('BLOG_' . $blog_id, 'article_comments_rate')) { $rating_select = Blog::display_rating_form('comment', $blog_id, $post_id, $comment['comment_id']); }
  911. if(!is_null($comment['task_id'])) {
  912. $border_color = ' border-left: 3px solid #' . $comment['color'];
  913. }
  914. $comment_text=stripslashes($comment_text);
  915. // Output...
  916. $margin = $current_level * 30;
  917. echo '<div class="blogpost_comment" style="margin-left: ' . $margin . 'px;' . $border_color . '">';
  918. echo '<span class="blogpost_comment_title"><a href="#add_comment" onclick="document.getElementById(\'comment_parent_id\').value=\'' . $comment['comment_id'] . '\'; document.getElementById(\'comment_title\').value=\'Re: '.addslashes($comment['title']) . '\'" title="' . get_lang('ReplyToThisComment') . '" >'.stripslashes($comment['title']) . '</a></span>';
  919. echo '<span class="blogpost_comment_date">' . $blog_comment_date . '</span>';
  920. echo '<span class="blogpost_text">' . $comment_text . '</span>';
  921. $file_name_array=get_blog_attachment($blog_id,$post_id, $comment['comment_id']);
  922. if (!empty($file_name_array))
  923. {
  924. echo '<br /><br />';
  925. echo Display::return_icon('attachment.gif',get_lang('Attachment'));
  926. echo '<a href="download.php?file=';
  927. echo $file_name_array['path'];
  928. echo ' "> '.$file_name_array['filename'].' </a>';
  929. echo '<span class="attachment_comment">';
  930. echo $file_name_array['comment'];
  931. echo '</span><br />';
  932. }
  933. echo '<span class="blogpost_comment_info">' . get_lang('Author') . ': ' . api_get_person_name($comment['firstname'], $comment['lastname']) . ' - ' . get_lang('Rating') . ': '.Blog::display_rating('comment', $blog_id, $comment['comment_id']) . $rating_select . '</span>';
  934. echo '<span class="blogpost_actions">' . $blog_comment_actions . '</span>';
  935. echo '</div>';
  936. // Go further down the tree.
  937. Blog::get_threaded_comments( $comment['comment_id'], $next_level, $blog_id, $post_id);
  938. }
  939. }
  940. /**
  941. * Displays the form to create a new post
  942. * @author Toon Keppens
  943. *
  944. * @param Integer $blog_id
  945. */
  946. public static function display_form_new_post ($blog_id) {
  947. if(api_is_allowed('BLOG_' . $blog_id, 'article_add')) {
  948. echo '<script type="text/javascript">
  949. function FCKeditor_OnComplete( editorInstance )
  950. {
  951. editorInstance.Events.AttachEvent( \'OnSelectionChange\', check_for_title ) ;
  952. }
  953. function check_for_title()
  954. {
  955. // This functions shows that you can interact directly with the editor area
  956. // DOM. In this way you have the freedom to do anything you want with it.
  957. // Get the editor instance that we want to interact with.
  958. var oEditor = FCKeditorAPI.GetInstance(\'post_full_text\') ;
  959. // Get the Editor Area DOM (Document object).
  960. var oDOM = oEditor.EditorDocument ;
  961. var iLength ;
  962. var contentText ;
  963. var contentTextArray;
  964. var bestandsnaamNieuw = "";
  965. var bestandsnaamOud = "";
  966. // The are two diffent ways to get the text (without HTML markups).
  967. // It is browser specific.
  968. if( document.all ) // If Internet Explorer.
  969. {
  970. contentText = oDOM.body.innerText ;
  971. }
  972. else // If Gecko.
  973. {
  974. var r = oDOM.createRange() ;
  975. r.selectNodeContents( oDOM.body ) ;
  976. contentText = r.toString() ;
  977. }
  978. // Compose title if there is none
  979. contentTextArray = contentText.split(\' \') ;
  980. var x=0;
  981. for(x=0; (x<5 && x<contentTextArray.length); x++)
  982. {
  983. if(x < 4)
  984. {
  985. bestandsnaamNieuw += contentTextArray[x] + \' \';
  986. }
  987. else
  988. {
  989. bestandsnaamNieuw += contentTextArray[x] + \'...\';
  990. }
  991. }
  992. if(document.getElementById(\'post_title_edited\').value == "false")
  993. {
  994. document.getElementById(\'post_title\').value = bestandsnaamNieuw;
  995. }
  996. }
  997. function trim(s) {
  998. while(s.substring(0,1) == \' \') {
  999. s = s.substring(1,s.length);
  1000. }
  1001. while(s.substring(s.length-1,s.length) == \' \') {
  1002. s = s.substring(0,s.length-1);
  1003. }
  1004. return s;
  1005. }
  1006. function check_if_still_empty()
  1007. {
  1008. if(trim(document.getElementById(\'post_title\').value) != "")
  1009. {
  1010. document.getElementById(\'post_title_edited\').value = "true";
  1011. }
  1012. }
  1013. </script>';
  1014. echo '<form name="add_post" enctype="multipart/form-data" method="post" action="blog.php?action=new_post&blog_id=' . $blog_id . '">';
  1015. echo '<input type="hidden" name="post_title_edited" id="post_title_edited" value="false" />';
  1016. // form title
  1017. echo '<div class="row"><div class="form_header">' . get_lang('NewPost') . '</div></div>';
  1018. // article title
  1019. echo '<div><span class="form_required">*</span>'.get_lang('Title') . ': <input name="post_title" id="post_title" type="text" size="60" onblur="check_if_still_empty()" /></div><br />';
  1020. // article text
  1021. $oFCKeditor = new FCKeditor('post_full_text') ;
  1022. $oFCKeditor->Width = '100%';
  1023. $oFCKeditor->Height = '200';
  1024. if(!api_is_allowed_to_edit()) {
  1025. $oFCKeditor->ToolbarSet = 'ProjectStudent';
  1026. } else {
  1027. $oFCKeditor->ToolbarSet = 'Project';
  1028. }
  1029. $oFCKeditor->Value = isset($_POST['post_full_text'])?stripslashes($_POST['post_full_text']):'';
  1030. echo '<div class="formw">';
  1031. $oFCKeditor->Create();
  1032. echo '</div>';
  1033. // attachment
  1034. echo '<div class="row">
  1035. <div class="label">
  1036. ' . get_lang('AddAnAttachment') . '
  1037. </div>
  1038. <div class="formw">
  1039. <input type="file" name="user_upload"/>
  1040. </div>
  1041. </div>';
  1042. // comment
  1043. echo '<div class="row">
  1044. <div class="label">
  1045. ' . get_lang('FileComment') . '
  1046. </div>
  1047. <div class="formw">
  1048. <textarea name="post_file_comment" cols="34" /></textarea>
  1049. </div>
  1050. </div>';
  1051. // submit
  1052. echo '<div class="row">
  1053. <div class="label">
  1054. </div>
  1055. <div class="formw">
  1056. <input type="hidden" name="action" value="" />
  1057. <input type="hidden" name="new_post_submit" value="true" />
  1058. <button class="save" type="submit" name="Submit">' . get_lang('Save') . '</button>
  1059. </div>
  1060. </div>';
  1061. echo '</form>';
  1062. }
  1063. else
  1064. {
  1065. api_not_allowed();
  1066. }
  1067. }
  1068. /**
  1069. * Displays the form to edit a post
  1070. * @author Toon Keppens
  1071. *
  1072. * @param Integer $blog_id
  1073. */
  1074. public static function display_form_edit_post ($blog_id, $post_id) {
  1075. // Init
  1076. $tbl_blogs_posts = Database::get_course_table(TABLE_BLOGS_POSTS);
  1077. $tbl_users = Database::get_main_table(TABLE_MAIN_USER);
  1078. $course_id = api_get_course_int_id();
  1079. // Get posts and author
  1080. $sql = "SELECT post.*, user.lastname, user.firstname FROM $tbl_blogs_posts post
  1081. INNER JOIN $tbl_users user ON post.author_id = user.user_id
  1082. WHERE
  1083. post.c_id = $course_id AND
  1084. post.blog_id = '".(int)$blog_id ."'
  1085. AND post.post_id = '".(int)$post_id."'
  1086. ORDER BY post_id DESC";
  1087. $result = Database::query($sql);
  1088. $blog_post = Database::fetch_array($result);
  1089. // Prepare data
  1090. $blog_post_text = stripslashes($blog_post['full_text']);
  1091. echo '<form name="edit_post" method="post" action="blog.php?action=edit_post&post_id=' . Security::remove_XSS($_GET['post_id']) . '&blog_id=' . Security::remove_XSS($blog_id) . '&article_id='.Security::remove_XSS($_GET['article_id']).'&task_id='.Security::remove_XSS($_GET['task_id']).'">';
  1092. // form title
  1093. echo '<div class="row"><div class="form_header">' . get_lang('EditPost') . '</div></div>';
  1094. // article title
  1095. echo '<div><span class="form_required">*</span>' . get_lang('Title') . ': <input name="post_title" id="post_title" type="text" size="60" value="'.stripslashes($blog_post['title']) . '" /><br /></div>';
  1096. // article text
  1097. $oFCKeditor = new FCKeditor('post_full_text') ;
  1098. $oFCKeditor->Width = '100%';
  1099. $oFCKeditor->Height = '200';
  1100. if(!api_is_allowed_to_edit()) {
  1101. $oFCKeditor->ToolbarSet = 'ProjectStudent';
  1102. } else {
  1103. $oFCKeditor->ToolbarSet = 'Project';
  1104. }
  1105. $oFCKeditor->Value = isset($_POST['post_full_text'])?stripslashes($_POST['post_full_text']):$blog_post_text;
  1106. echo '<div class="formw">';
  1107. echo $oFCKeditor->Create();
  1108. echo '</div>';
  1109. // submit
  1110. echo ' <div class="row">
  1111. <div class="label">
  1112. </div>
  1113. <div class="formw">
  1114. <input type="hidden" name="action" value="" />
  1115. <input type="hidden" name="edit_post_submit" value="true" />
  1116. <input type="hidden" name="post_id" value="' . (int)$_GET['post_id'] . '" />
  1117. <button class="save" type="submit" name="Submit">' . get_lang('Save') . '</button>
  1118. </div>
  1119. </div>';
  1120. echo '</form>';
  1121. }
  1122. /**
  1123. * Displays a list of tasks in this blog
  1124. * @author Toon Keppens
  1125. *
  1126. * @param Integer $blog_id
  1127. */
  1128. public static function display_task_list ($blog_id) {
  1129. global $charset;
  1130. $course_id = api_get_course_int_id();
  1131. if(api_is_allowed('BLOG_' . $blog_id, 'article_add')) {
  1132. // Init
  1133. $tbl_blogs_tasks = Database::get_course_table(TABLE_BLOGS_TASKS);
  1134. $counter = 0;
  1135. global $color2;
  1136. echo '<div class="actions">';
  1137. echo '<a href="' .api_get_self(). '?action=manage_tasks&amp;blog_id=' . $blog_id . '&amp;do=add"><img src="../img/blog_newtasks.gif" border="0" align="middle" alt="'.get_lang('AddTasks').'" />' . get_lang('AddTasks') . '</a> ';
  1138. echo '<a href="' .api_get_self(). '?action=manage_tasks&amp;blog_id=' . $blog_id . '&amp;do=assign"><img src="../img/blog_task.gif" border="0" align="middle" alt="'.get_lang('AssignTasks').'" />' . get_lang('AssignTasks') . '</a>';
  1139. ?>
  1140. <a href="<?php echo api_get_self(); ?>?action=manage_rights&amp;blog_id=<?php echo $blog_id ?>" title="<?php echo get_lang('ManageRights') ?>"><?php echo Display::return_icon('blog_admin_users.png', get_lang('RightsManager'),'','22'). get_lang('RightsManager') ?></a>
  1141. <?php
  1142. echo '</div>';
  1143. echo '<span class="blogpost_title">' . get_lang('TaskList') . '</span><br />';
  1144. echo "<table class=\"data_table\">";
  1145. echo "<tr bgcolor=\"$color2\" align=\"center\" valign=\"top\">",
  1146. "<th width='240'><b>",get_lang('Title'),"</b></th>\n",
  1147. "<th><b>",get_lang('Description'),"</b></th>\n",
  1148. "<th><b>",get_lang('Color'),"</b></th>\n",
  1149. "<th width='50'><b>",get_lang('Modify'),"</b></th>\n",
  1150. "</tr>\n";
  1151. $sql = " SELECT
  1152. blog_id,
  1153. task_id,
  1154. blog_id,
  1155. title,
  1156. description,
  1157. color,
  1158. system_task
  1159. FROM " . $tbl_blogs_tasks . "
  1160. WHERE c_id = $course_id AND blog_id = " . (int)$blog_id . "
  1161. ORDER BY system_task, title";
  1162. $result = Database::query($sql);
  1163. while($task = Database::fetch_array($result))
  1164. {
  1165. $counter++;
  1166. $css_class = (($counter % 2) == 0) ? "row_odd" : "row_even";
  1167. $delete_icon = ($task['system_task'] == '1') ? "delete_na.gif" : "delete.gif";
  1168. $delete_title = ($task['system_task'] == '1') ? get_lang('DeleteSystemTask') : get_lang('DeleteTask');
  1169. $delete_link = ($task['system_task'] == '1') ? '#' : api_get_self() . '?action=manage_tasks&amp;blog_id=' . $task['blog_id'] . '&amp;do=delete&amp;task_id=' . $task['task_id'];
  1170. $delete_confirm = ($task['system_task'] == '1') ? '' : 'onclick="javascript:if(!confirm(\''.addslashes(api_htmlentities(get_lang("ConfirmYourChoice"),ENT_QUOTES,$charset)). '\')) return false;"';
  1171. echo '<tr class="' . $css_class . '" valign="top">',
  1172. '<td width="240">' . Security::remove_XSS($task['title']) . '</td>',
  1173. '<td>' . Security::remove_XSS($task['description']) . '</td>',
  1174. '<td><span style="background-color: #' . $task['color'] . '">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></td>',
  1175. '<td width="50">',
  1176. '<a href="' .api_get_self(). '?action=manage_tasks&amp;blog_id=' . $task['blog_id'] . '&amp;do=edit&amp;task_id=' . $task['task_id'] . '">',
  1177. '<img src="../img/edit.gif" border="0" title="' . get_lang('EditTask') . '" />',
  1178. "</a>\n",
  1179. '<a href="' . $delete_link . '"',
  1180. $delete_confirm,
  1181. '><img src="../img/' . $delete_icon . '" border="0" title="' . $delete_title . '" />',
  1182. "</a>\n",
  1183. '</td>',
  1184. '</tr>';
  1185. }
  1186. echo "</table>";
  1187. }
  1188. }
  1189. /**
  1190. * Displays a list of tasks assigned to a user in this blog
  1191. * @author Toon Keppens
  1192. *
  1193. * @param Integer $blog_id
  1194. */
  1195. public static function display_assigned_task_list ($blog_id) {
  1196. // Init
  1197. $tbl_users = Database::get_main_table(TABLE_MAIN_USER);
  1198. $tbl_blogs_tasks = Database::get_course_table(TABLE_BLOGS_TASKS);
  1199. $tbl_blogs_tasks_rel_user = Database::get_course_table(TABLE_BLOGS_TASKS_REL_USER);
  1200. $counter = 0;
  1201. global $charset,$color2;
  1202. echo '<span class="blogpost_title">' . get_lang('AssignedTasks') . '</span><br />';
  1203. echo "<table class=\"data_table\">";
  1204. echo "<tr bgcolor=\"$color2\" align=\"center\" valign=\"top\">",
  1205. "<th width='240'><b>",get_lang('Member'),"</b></th>\n",
  1206. "<th><b>",get_lang('Task'),"</b></th>\n",
  1207. "<th><b>",get_lang('Description'),"</b></th>\n",
  1208. "<th><b>",get_lang('TargetDate'),"</b></th>\n",
  1209. "<th width='50'><b>",get_lang('Modify'),"</b></th>\n",
  1210. "</tr>";
  1211. $course_id = api_get_course_int_id();
  1212. $sql = "SELECT task_rel_user.*, task.title, user.firstname, user.lastname, task.description, task.system_task, task.blog_id, task.task_id
  1213. FROM $tbl_blogs_tasks_rel_user task_rel_user
  1214. INNER JOIN $tbl_blogs_tasks task ON task_rel_user.task_id = task.task_id
  1215. INNER JOIN $tbl_users user ON task_rel_user.user_id = user.user_id
  1216. WHERE task_rel_user.c_id = $course_id AND
  1217. task.c_id = $course_id AND
  1218. task_rel_user.blog_id = '".(int)$blog_id."'
  1219. ORDER BY target_date ASC";
  1220. $result = Database::query($sql);
  1221. while($assignment = Database::fetch_array($result)) {
  1222. $counter++;
  1223. $css_class = (($counter % 2)==0) ? "row_odd" : "row_even";
  1224. $delete_icon = ($assignment['system_task'] == '1') ? "delete_na.gif" : "delete.gif";
  1225. $delete_title = ($assignment['system_task'] == '1') ? get_lang('DeleteSystemTask') : get_lang('DeleteTask');
  1226. $delete_link = ($assignment['system_task'] == '1') ? '#' : api_get_self() . '?action=manage_tasks&amp;blog_id=' . $assignment['blog_id'] . '&amp;do=delete&amp;task_id=' . $assignment['task_id'];
  1227. $delete_confirm = ($assignment['system_task'] == '1') ? '' : 'onclick="javascript:if(!confirm(\''.addslashes(api_htmlentities(get_lang("ConfirmYourChoice"),ENT_QUOTES,$charset)). '\')) return false;"';
  1228. echo '<tr class="' . $css_class . '" valign="top">',
  1229. '<td width="240">' . api_get_person_name($assignment['firstname'], $assignment['lastname']) . '</td>',
  1230. '<td>'.stripslashes($assignment['title']) . '</td>',
  1231. '<td>'.stripslashes($assignment['description']) . '</td>',
  1232. '<td>' . $assignment['target_date'] . '</td>',
  1233. '<td width="50">',
  1234. '<a href="' .api_get_self(). '?action=manage_tasks&amp;blog_id=' . $assignment['blog_id'] . '&amp;do=edit_assignment&amp;task_id=' . $assignment['task_id'] . '&amp;user_id=' . $assignment['user_id'] . '">',
  1235. '<img src="../img/edit.gif" border="0" title="' . get_lang('EditTask') . '" />',
  1236. "</a>\n",
  1237. '<a href="' .api_get_self(). '?action=manage_tasks&amp;blog_id=' . $assignment['blog_id'] . '&amp;do=delete_assignment&amp;task_id=' . $assignment['task_id'] . '&amp;user_id=' . $assignment['user_id'] . '" ',
  1238. 'onclick="javascript:if(!confirm(\''.addslashes(api_htmlentities(get_lang("ConfirmYourChoice"),ENT_QUOTES,$charset)). '\')) return false;"',
  1239. '<img src="../img/' . $delete_icon . '" border="0" title="' . $delete_title . '" />',
  1240. "</a>\n",
  1241. '</td>',
  1242. '</tr>';
  1243. }
  1244. echo "</table>";
  1245. }
  1246. /**
  1247. * Displays new task form
  1248. * @author Toon Keppens
  1249. *
  1250. */
  1251. public static function display_new_task_form ($blog_id) {
  1252. // Init
  1253. $colors = array('FFFFFF','FFFF99','FFCC99','FF9933','FF6699','CCFF99','CC9966','66FF00', '9966FF', 'CF3F3F', '990033','669933','0033FF','003366','000000');
  1254. // form
  1255. echo '<form name="add_task" method="post" action="blog.php?action=manage_tasks&amp;blog_id=' . $blog_id . '">';
  1256. // form title
  1257. echo '<div class="row"><div class="form_header">'.get_lang('AddTask').'</div></div>';
  1258. // task title
  1259. echo ' <div class="row">
  1260. <div class="label">
  1261. <span class="form_required">*</span>' . get_lang('Title') . '
  1262. </div>
  1263. <div class="formw">
  1264. <input name="task_name" type="text" size="70" />
  1265. </div>
  1266. </div>';
  1267. // task comment
  1268. echo ' <div class="row">
  1269. <div class="label">
  1270. ' . get_lang('Description') . '
  1271. </div>
  1272. <div class="formw">
  1273. <textarea name="task_description" cols="45"></textarea>
  1274. </div>
  1275. </div>';
  1276. // task management
  1277. echo ' <div class="row">
  1278. <div class="label">
  1279. ' . get_lang('TaskManager') . '
  1280. </div>
  1281. <div class="formw">';
  1282. echo "\t\t\t" . '<table class="data_table" cellspacing="0" style="border-collapse:collapse; width:446px;">';
  1283. echo "\t\t\t\t" . '<tr>' . "\n";
  1284. echo "\t\t\t\t\t" . '<th colspan="2" style="width:223px;">' . get_lang('ArticleManager') . '</th>' . "\n";
  1285. echo "\t\t\t\t\t" . '<th width:223px;>' . get_lang('CommentManager') . '</th>' . "\n";
  1286. echo "\t\t\t\t" . '</tr>' . "\n";
  1287. echo "\t\t\t\t" . '<tr>' . "\n";
  1288. echo "\t\t\t\t\t" . '<th style="width:111px;"><label for="articleDelete">' . get_lang('Delete') . '</label></th>' . "\n";
  1289. echo "\t\t\t\t\t" . '<th style="width:112px;"><label for="articleEdit">' . get_lang('Edit') . '</label></th>' . "\n";
  1290. echo "\t\t\t\t\t" . '<th style="width:223px;"><label for="commentsDelete">' . get_lang('Delete') . '</label></th>' . "\n";
  1291. echo "\t\t\t\t" . '</tr>' . "\n";
  1292. echo "\t\t\t\t" . '<tr>' . "\n";
  1293. echo "\t\t\t\t\t" . '<td style="text-align:center;"><input id="articleDelete" name="chkArticleDelete" type="checkbox" /></td>' . "\n";
  1294. echo "\t\t\t\t\t" . '<td style="text-align:center;"><input id="articleEdit" name="chkArticleEdit" type="checkbox" /></td>' . "\n";
  1295. echo "\t\t\t\t\t" . '<td style="border:1px dotted #808080; text-align:center;"><input id="commentsDelete" name="chkCommentsDelete" type="checkbox" /></td>' . "\n";
  1296. echo "\t\t\t\t" . '</tr>' . "\n";
  1297. echo "\t\t\t" . '</table>' . "\n";
  1298. echo ' </div>
  1299. </div>';
  1300. // task color
  1301. echo ' <div class="row">
  1302. <div class="label">
  1303. ' . get_lang('Color') . '
  1304. </div>
  1305. <div class="formw">';
  1306. echo ' <select name="task_color" id="color" style="width: 150px; background-color: #eeeeee" onchange="document.getElementById(\'color\').style.backgroundColor=\'#\'+document.getElementById(\'color\').value" onkeypress="document.getElementById(\'color\').style.backgroundColor=\'#\'+document.getElementById(\'color\').value">';
  1307. foreach ($colors as $color)
  1308. {
  1309. $style = 'style="background-color: #' . $color . '"';
  1310. echo '<option value="' . $color . '" ' . $style . '>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>';
  1311. }
  1312. echo ' </select>';
  1313. echo ' </div>
  1314. </div>';
  1315. // submit
  1316. echo ' <div class="row">
  1317. <div class="label">
  1318. </div>
  1319. <div class="formw">
  1320. <input type="hidden" name="action" value="" />
  1321. <input type="hidden" name="new_task_submit" value="true" />
  1322. <button class="save" type="submit" name="Submit">' . get_lang('Save') . '</button>
  1323. </div>
  1324. </div>';
  1325. echo '</form>';
  1326. echo '<div style="clear:both; margin-bottom: 10px;"></div>';
  1327. }
  1328. /**
  1329. * Displays edit task form
  1330. * @author Toon Keppens
  1331. *
  1332. */
  1333. public static function display_edit_task_form ($blog_id, $task_id) {
  1334. $tbl_blogs_tasks = Database::get_course_table(TABLE_BLOGS_TASKS);
  1335. $course_id = api_get_course_int_id();
  1336. $colors = array('FFFFFF','FFFF99','FFCC99','FF9933','FF6699','CCFF99','CC9966','66FF00', '9966FF', 'CF3F3F', '990033','669933','0033FF','003366','000000');
  1337. $sql = "SELECT blog_id, task_id, title, description, color FROM $tbl_blogs_tasks WHERE c_id = $course_id AND task_id = '".(int)$task_id."'";
  1338. $result = Database::query($sql);
  1339. $task = Database::fetch_array($result);
  1340. // Display
  1341. echo '<form name="edit_task" method="post" action="blog.php?action=manage_tasks&amp;blog_id=' . $blog_id . '">
  1342. <div class="form_header">' . get_lang('EditTask') . '</div>
  1343. <table width="100%" border="0" cellspacing="2">
  1344. <tr>
  1345. <td align="right">' . get_lang('Title') . ':&nbsp;&nbsp;</td>
  1346. <td><input name="task_name" type="text" size="70" value="'.Security::remove_XSS($task['title']) . '" /></td>
  1347. </tr>
  1348. <tr>
  1349. <td align="right">' . get_lang('Description') . ':&nbsp;&nbsp;</td>
  1350. <td><textarea name="task_description" cols="45">'.Security::remove_XSS($task['description']).'</textarea></td>
  1351. </tr>';
  1352. /* edit by Kevin Van Den Haute (kevin@develop-it.be) */
  1353. $tbl_tasks_permissions = Database::get_course_table(TABLE_BLOGS_TASKS_PERMISSIONS);
  1354. $sql = " SELECT id, action FROM " . $tbl_tasks_permissions . "
  1355. WHERE c_id = $course_id AND task_id = '" . (int)$task_id."'";
  1356. $result = Database::query($sql);
  1357. $arrPermissions = array();
  1358. while ($row = Database::fetch_array($result))
  1359. $arrPermissions[] = $row['action'];
  1360. echo "\t" . '<tr>' . "\n";
  1361. echo "\t\t" . '<td style="text-align:right; vertical-align:top;">' . get_lang('TaskManager') . ':&nbsp;&nbsp;</td>' . "\n";
  1362. echo "\t\t" . '<td>' . "\n";
  1363. echo "\t\t\t" . '<table class="data_table" cellspacing="0" style="border-collapse:collapse; width:446px;">';
  1364. echo "\t\t\t\t" . '<tr>' . "\n";
  1365. echo "\t\t\t\t\t" . '<th colspan="2" style="width:223px;">' . get_lang('ArticleManager') . '</th>' . "\n";
  1366. echo "\t\t\t\t\t" . '<th width:223px;>' . get_lang('CommentManager') . '</th>' . "\n";
  1367. echo "\t\t\t\t" . '</tr>' . "\n";
  1368. echo "\t\t\t\t" . '<tr>' . "\n";
  1369. echo "\t\t\t\t\t" . '<th style="width:111px;"><label for="articleDelete">' . get_lang('Delete') . '</label></th>' . "\n";
  1370. echo "\t\t\t\t\t" . '<th style="width:112px;"><label for="articleEdit">' . get_lang('Edit') . '</label></th>' . "\n";
  1371. echo "\t\t\t\t\t" . '<th style="width:223px;"><label for="commentsDelete">' . get_lang('Delete') . '</label></th>' . "\n";
  1372. echo "\t\t\t\t" . '</tr>' . "\n";
  1373. echo "\t\t\t\t" . '<tr>' . "\n";
  1374. echo "\t\t\t\t\t" . '<td style="text-align:center;"><input ' . ((in_array('article_delete', $arrPermissions)) ? 'checked ' : '') . 'id="articleDelete" name="chkArticleDelete" type="checkbox" /></td>' . "\n";
  1375. echo "\t\t\t\t\t" . '<td style="text-align:center;"><input ' . ((in_array('article_edit', $arrPermissions)) ? 'checked ' : '') . 'id="articleEdit" name="chkArticleEdit" type="checkbox" /></td>' . "\n";
  1376. echo "\t\t\t\t\t" . '<td style="text-align:center;"><input ' . ((in_array('article_comments_delete', $arrPermissions)) ? 'checked ' : '') . 'id="commentsDelete" name="chkCommentsDelete" type="checkbox" /></td>' . "\n";
  1377. echo "\t\t\t\t" . '</tr>' . "\n";
  1378. echo "\t\t\t" . '</table>' . "\n";
  1379. echo "\t\t" . '</td>' . "\n";
  1380. echo "\t" . '</tr>' . "\n";
  1381. /* end of edit */
  1382. echo '<tr>
  1383. <td align="right">' . get_lang('Color') . ':&nbsp;&nbsp;</td>
  1384. <td>
  1385. <select name="task_color" id="color" style="width: 150px; background-color: #' . $task['color'] . '" onchange="document.getElementById(\'color\').style.backgroundColor=\'#\'+document.getElementById(\'color\').value" onkeypress="document.getElementById(\'color\').style.backgroundColor=\'#\'+document.getElementById(\'color\').value">';
  1386. foreach ($colors as $color)
  1387. {
  1388. $selected = ($color == $task['color']) ? ' selected' : '';
  1389. $style = 'style="background-color: #' . $color . '"';
  1390. echo '<option value="' . $color . '" ' . $style . ' ' . $selected . ' >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>';
  1391. }
  1392. echo ' </select>
  1393. </td>
  1394. </tr>
  1395. <tr>
  1396. <td align="right">&nbsp;</td>
  1397. <td><br /><input type="hidden" name="action" value="" />
  1398. <input type="hidden" name="edit_task_submit" value="true" />
  1399. <input type="hidden" name="task_id" value="' . $task['task_id'] . '" />
  1400. <input type="hidden" name="blog_id" value="' . $task['blog_id'] . '" />
  1401. <button class="save" type="submit" name="Submit">' . get_lang('Save') . '</button></td>
  1402. </tr>
  1403. </table>
  1404. </form>';
  1405. }
  1406. /**
  1407. * Displays assign task form
  1408. * @author Toon Keppens
  1409. *
  1410. */
  1411. public static function display_assign_task_form ($blog_id) {
  1412. // Init
  1413. $tbl_users = Database::get_main_table(TABLE_MAIN_USER);
  1414. $tbl_blogs_rel_user = Database::get_course_table(TABLE_BLOGS_REL_USER);
  1415. $tbl_blogs_tasks = Database::get_course_table(TABLE_BLOGS_TASKS);
  1416. $day = date("d");
  1417. $month = date("m");
  1418. $year = date("Y");
  1419. global $MonthsLong;
  1420. $course_id = api_get_course_int_id();
  1421. // Get users in this blog / make select list of it
  1422. $sql = "SELECT user.user_id, user.firstname, user.lastname FROM $tbl_users user
  1423. INNER JOIN $tbl_blogs_rel_user blogs_rel_user
  1424. ON user.user_id = blogs_rel_user.user_id
  1425. WHERE blogs_rel_user.c_id = $course_id AND blogs_rel_user.blog_id = '".(int)$blog_id."'";
  1426. $result = Database::query($sql);
  1427. $select_user_list = '<select name="task_user_id">';
  1428. while($user = Database::fetch_array($result))
  1429. {
  1430. $select_user_list .= '<option value="' . $user['user_id'] . '">' . api_get_person_name($user['firstname'], $user['lastname']) . '</option>';
  1431. }
  1432. $select_user_list .= '</select>';
  1433. // Get tasks in this blog / make select list of it
  1434. $sql = "
  1435. SELECT
  1436. blog_id,
  1437. task_id,
  1438. blog_id,
  1439. title,
  1440. description,
  1441. color,
  1442. system_task
  1443. FROM " . $tbl_blogs_tasks . "
  1444. WHERE c_id = $course_id AND blog_id = " . (int)$blog_id . "
  1445. ORDER BY system_task, title";
  1446. $result = Database::query($sql);
  1447. $select_task_list = '<select name="task_task_id">';
  1448. while($task = Database::fetch_array($result))
  1449. {
  1450. $select_task_list .= '<option value="' . $task['task_id'] . '">'.stripslashes($task['title']) . '</option>';
  1451. }
  1452. $select_task_list .= '</select>';
  1453. // form
  1454. echo '<form name="assign_task" method="post" action="blog.php?action=manage_tasks&amp;blog_id=' . $blog_id . '">';
  1455. // form title
  1456. echo '<div class="row"><div class="form_header">'.get_lang('AssignTask').'</div></div>';
  1457. // user
  1458. echo ' <div class="row">
  1459. <div class="label">
  1460. <span class="form_required">*</span>' . get_lang('SelectUser') . '
  1461. </div>
  1462. <div class="formw">
  1463. '.$select_user_list.'
  1464. </div>
  1465. </div>';
  1466. // task
  1467. echo ' <div class="row">
  1468. <div class="label">
  1469. <span class="form_required">*</span>' . get_lang('SelectTask') . '
  1470. </div>
  1471. <div class="formw">
  1472. '.$select_task_list.'
  1473. </div>
  1474. </div>';
  1475. // date
  1476. echo ' <div class="row">
  1477. <div class="label">
  1478. <span class="form_required">*</span>' . get_lang('SelectTargetDate') . '
  1479. </div>
  1480. <div class="formw">';
  1481. echo ' <select name="task_day">';
  1482. for($i=1; $i<=31; $i++)
  1483. {
  1484. // values need to have double digits
  1485. $value = ($i <= 9 ? "0" . $i : $i);
  1486. // the current day is indicated with [] around the date
  1487. if($value==$day)
  1488. { echo "\t\t\t\t <option value=\"" . $value."\" selected> " . $i." </option>\n";}
  1489. else
  1490. { echo "\t\t\t\t <option value=\"" . $value."\">" . $i."</option>\n"; }
  1491. }
  1492. echo '</select>
  1493. <select name="task_month">';
  1494. for($i=1; $i<=12; $i++)
  1495. {
  1496. // values need to have double digits
  1497. $value = ($i <= 9 ? "0" . $i : $i);
  1498. if($value==$month)
  1499. { echo "\t\t\t\t <option value=\"" . $value."\" selected>" . $MonthsLong[$i-1]."</option>\n"; }
  1500. else
  1501. { echo "\t\t\t\t <option value=\"" . $value."\">" . $MonthsLong[$i-1]."</option>\n"; }
  1502. }
  1503. echo '</select>
  1504. <select name="task_year">
  1505. <option value="'.($year-1) . '">'.($year-1) . '</option>
  1506. <option value="' . $year . '" selected> ' . $year . ' </option>';
  1507. for($i=1; $i<=5; $i++)
  1508. {
  1509. $value=$year+$i;
  1510. echo "\t\t\t\t<option value=\"" . $value."\">" . $value."</option>\n";
  1511. }
  1512. echo '</select>
  1513. <a title="Kalender" href="javascript:openCalendar(\'assign_task\', \'task_\')"><img src="../img/calendar_select.gif" border="0" align="absmiddle"/></a>';
  1514. echo ' </div>
  1515. </div>';
  1516. // submit
  1517. echo ' <div class="row">
  1518. <div class="label">
  1519. </div>
  1520. <div class="formw">
  1521. <input type="hidden" name="action" value="" />
  1522. <input type="hidden" name="assign_task_submit" value="true" />
  1523. <button class="save" type="submit" name="Submit">' . get_lang('Ok') . '</button>
  1524. </div>
  1525. </div>';
  1526. echo '</form>';
  1527. echo '<div style="clear: both; margin-bottom:10px;"></div>';
  1528. }
  1529. /**
  1530. * Displays assign task form
  1531. * @author Toon Keppens
  1532. *
  1533. */
  1534. public static function display_edit_assigned_task_form ($blog_id, $task_id, $user_id) {
  1535. $tbl_users = Database::get_main_table(TABLE_MAIN_USER);
  1536. $tbl_blogs_rel_user = Database::get_course_table(TABLE_BLOGS_REL_USER);
  1537. $tbl_blogs_tasks = Database::get_course_table(TABLE_BLOGS_TASKS);
  1538. $tbl_blogs_tasks_rel_user = Database::get_course_table(TABLE_BLOGS_TASKS_REL_USER);
  1539. $course_id = api_get_course_int_id();
  1540. $year = date("Y");
  1541. global $MonthsLong;
  1542. // Get assignd date;
  1543. $sql = "
  1544. SELECT target_date
  1545. FROM $tbl_blogs_tasks_rel_user
  1546. WHERE c_id = $course_id AND
  1547. blog_id = '".(int)$blog_id."' AND
  1548. user_id = '".(int)$user_id."' AND
  1549. task_id = '".(int)$task_id."'";
  1550. $result = Database::query($sql);
  1551. $row = Database::fetch_assoc($result);
  1552. $old_date = $row['target_date'];
  1553. $date = explode('-', $row['target_date']);
  1554. // Get users in this blog / make select list of it
  1555. $sql = "
  1556. SELECT user.user_id, user.firstname, user.lastname
  1557. FROM $tbl_users user
  1558. INNER JOIN $tbl_blogs_rel_user blogs_rel_user ON user.user_id = blogs_rel_user.user_id
  1559. WHERE blogs_rel_user.c_id = $course_id AND blogs_rel_user.blog_id = '".(int)$blog_id."'";
  1560. $result = Database::query($sql);
  1561. $select_user_list = '<select name="task_user_id">';
  1562. while($user = Database::fetch_array($result)) {
  1563. $select_user_list .= '<option ' . (($user_id == $user['user_id']) ? 'selected="selected "' : ' ') . 'value="' . $user['user_id'] . '">' . api_get_person_name($user['firstname'], $user['lastname']) . '</option>';
  1564. }
  1565. $select_user_list .= '</select>';
  1566. // Get tasks in this blog / make select list of it
  1567. $sql = "
  1568. SELECT
  1569. blog_id,
  1570. task_id,
  1571. title,
  1572. description,
  1573. color,
  1574. system_task
  1575. FROM " . $tbl_blogs_tasks . "
  1576. WHERE c_id = $course_id AND blog_id = " . (int)$blog_id . "
  1577. ORDER BY system_task, title";
  1578. $result = Database::query($sql);
  1579. $select_task_list = '<select name="task_task_id">';
  1580. while($task = Database::fetch_array($result)) {
  1581. //if(!in_array($task['task_id'], $arrUserTasks) || $task_id == $task['task_id'])
  1582. $select_task_list .= '<option ' . (($task_id == $task['task_id']) ? 'selected="selected "' : ' ') . 'value="' . $task['task_id'] . '">'.stripslashes($task['title']) . '</option>';
  1583. }
  1584. $select_task_list .= '</select>';
  1585. // Display
  1586. echo '<form name="assign_task" method="post" action="blog.php?action=manage_tasks&amp;blog_id=' . $blog_id . '">
  1587. <table width="100%" border="0" cellspacing="2" cellpadding="0" style="background-color: #f6f6f6; border: 1px solid #dddddd">
  1588. <tr>
  1589. <td width="200"></td>
  1590. <td><b>' . get_lang('AssignTask') . '</b><br /><br /></td>
  1591. </tr>
  1592. <tr>
  1593. <td align="right">' . get_lang('SelectUser') . ':&nbsp;&nbsp;</td>
  1594. <td>' . $select_user_list . '</td>
  1595. </tr>
  1596. <tr>
  1597. <td align="right">' . get_lang('SelectTask') . ':&nbsp;&nbsp;</td>
  1598. <td>' . $select_task_list . '</td>
  1599. </tr>
  1600. <tr>
  1601. <td align="right">' . get_lang('SelectTargetDate') . ':&nbsp;&nbsp;</td>
  1602. <td>
  1603. <select name="task_day">';
  1604. for($i=1; $i<=31; $i++)
  1605. {
  1606. // values need to have double digits
  1607. $value = ($i <= 9 ? "0" . $i : $i);
  1608. echo "\t\t\t\t<option " . (($date[2] == $value) ? 'selected="selected "' : ' ') . "value=\"" . $value . "\">" . $i . "</option>\n";
  1609. }
  1610. echo '</select>
  1611. <select name="task_month">';
  1612. for($i=1; $i<=12; $i++)
  1613. {
  1614. // values need to have double digits
  1615. $value = ($i <= 9 ? "0" . $i : $i);
  1616. echo "\t\t\t\t<option " . (($date[1] == $value) ? 'selected="selected "' : ' ') . "value=\"" . $value . "\">" . $MonthsLong[$i-1]."</option>\n";
  1617. }
  1618. echo '</select>
  1619. <select name="task_year">
  1620. <option value="' . ($year - 1) . '">' . ($year - 1) . '</option>
  1621. <option value="' . $year . '" selected> ' . $year . ' </option>';
  1622. for($i=1; $i<=5; $i++)
  1623. {
  1624. $value = $year + $i;
  1625. echo "\t\t\t\t<option " . (($date[0] == $value) ? 'selected="selected "' : ' ') . "value=\"" . $value . "\">" . $value . "</option>\n";
  1626. }
  1627. echo '</select>
  1628. <a title="Kalender" href="javascript:openCalendar(\'assign_task\', \'task_\')"><img src="../img/calendar_select.gif" border="0" align="absmiddle"/></a>
  1629. </td>
  1630. </tr>
  1631. <tr>
  1632. <td align="right">&nbsp;</td>
  1633. <input type="hidden" name="action" value="" />
  1634. <input type="hidden" name="old_task_id" value="' . $task_id . '" />
  1635. <input type="hidden" name="old_user_id" value="' . $user_id . '" />
  1636. <input type="hidden" name="old_target_date" value="' . $old_date . '" />
  1637. <input type="hidden" name="assign_task_edit_submit" value="true" />
  1638. <td><br /><button class="save type="submit" name="Submit">' . get_lang('Ok') . '</button></td>
  1639. </tr>
  1640. </table>
  1641. </form>';
  1642. }
  1643. /**
  1644. * Assigns a task to a user in a blog
  1645. *
  1646. * @param Integer $blog_id
  1647. * @param Integer $user_id
  1648. * @param Integer $task_id
  1649. * @param Date $target_date
  1650. */
  1651. public static function assign_task ($blog_id, $user_id, $task_id, $target_date) {
  1652. // Init
  1653. $tbl_blogs_tasks_rel_user = Database::get_course_table(TABLE_BLOGS_TASKS_REL_USER);
  1654. $course_id = api_get_course_int_id();
  1655. $sql = "
  1656. SELECT COUNT(*) as 'number'
  1657. FROM " . $tbl_blogs_tasks_rel_user . "
  1658. WHERE c_id = $course_id AND
  1659. blog_id = " . (int)$blog_id . "
  1660. AND user_id = " . (int)$user_id . "
  1661. AND task_id = " . (int)$task_id . "
  1662. ";
  1663. $result = @Database::query($sql);
  1664. $row = Database::fetch_assoc($result);
  1665. if($row['number'] == 0) {
  1666. $sql = "
  1667. INSERT INTO " . $tbl_blogs_tasks_rel_user . " (
  1668. c_id,
  1669. blog_id,
  1670. user_id,
  1671. task_id,
  1672. target_date
  1673. ) VALUES (
  1674. '" . (int)$course_id . "',
  1675. '" . (int)$blog_id . "',
  1676. '" . (int)$user_id . "',
  1677. '" . (int)$task_id . "',
  1678. '" . Database::escape_string($target_date) . "'
  1679. )";
  1680. $result = @Database::query($sql);
  1681. }
  1682. }
  1683. public static function edit_assigned_task ($blog_id, $user_id, $task_id, $target_date, $old_user_id, $old_task_id, $old_target_date) {
  1684. // Init
  1685. $tbl_blogs_tasks_rel_user = Database::get_course_table(TABLE_BLOGS_TASKS_REL_USER);
  1686. $course_id = api_get_course_int_id();
  1687. $sql = "SELECT COUNT(*) as 'number'
  1688. FROM " . $tbl_blogs_tasks_rel_user . "
  1689. WHERE
  1690. c_id = $course_id AND
  1691. blog_id = " . (int)$blog_id . " AND
  1692. user_id = " . (int)$user_id . " AND
  1693. task_id = " . (int)$task_id . "
  1694. ";
  1695. $result = @Database::query($sql);
  1696. $row = Database::fetch_assoc($result);
  1697. if($row['number'] == 0 || ($row['number'] != 0 && $task_id == $old_task_id && $user_id == $old_user_id)) {
  1698. $sql = "
  1699. UPDATE " . $tbl_blogs_tasks_rel_user . "
  1700. SET
  1701. user_id = " . (int)$user_id . ",
  1702. task_id = " . (int)$task_id . ",
  1703. target_date = '" . Database::escape_string($target_date) . "'
  1704. WHERE
  1705. c_id = $course_id AND
  1706. blog_id = " . (int)$blog_id . " AND
  1707. user_id = " . (int)$old_user_id . " AND
  1708. task_id = " . (int)$old_task_id . " AND
  1709. target_date = '" . Database::escape_string($old_target_date) . "'
  1710. ";
  1711. $result = @Database::query($sql);
  1712. }
  1713. }
  1714. /**
  1715. * Displays a list with posts a user can select to execute his task.
  1716. *
  1717. * @param Integer $blog_id
  1718. * @param unknown_type $task_id
  1719. */
  1720. public static function display_select_task_post ($blog_id, $task_id) {
  1721. // Init
  1722. $tbl_blogs_tasks = Database::get_course_table(TABLE_BLOGS_TASKS);
  1723. $tbl_blogs_posts = Database::get_course_table(TABLE_BLOGS_POSTS);
  1724. $tbl_users = Database::get_main_table(TABLE_MAIN_USER);
  1725. $course_id = api_get_course_int_id();
  1726. $sql = "SELECT title, description FROM $tbl_blogs_tasks
  1727. WHERE task_id = '".(int)$task_id."'";
  1728. $result = Database::query($sql);
  1729. $row = Database::fetch_assoc($result);
  1730. // Get posts and authors
  1731. $sql = "SELECT post.*, user.lastname, user.firstname
  1732. FROM $tbl_blogs_posts post
  1733. INNER JOIN $tbl_users user ON post.author_id = user.user_id
  1734. WHERE post.blog_id = '".(int)$blog_id."' AND post.c_id = $course_id
  1735. ORDER BY post_id DESC
  1736. LIMIT 0, 100";
  1737. $result = Database::query($sql);
  1738. // Display
  1739. echo '<span class="blogpost_title">' . get_lang('SelectTaskArticle') . ' "' . stripslashes($row['title']) . '"</span>';
  1740. echo '<span style="font-style: italic;"">'.stripslashes($row['description']) . '</span><br><br>';
  1741. if(Database::num_rows($result) > 0) {
  1742. while($blog_post = Database::fetch_array($result)) {
  1743. echo '<a href="blog.php?action=execute_task&amp;blog_id=' . $blog_id . '&amp;task_id=' . $task_id . '&amp;post_id=' . $blog_post['post_id'] . '#add_comment">'.stripslashes($blog_post['title']) . '</a>, ' . get_lang('WrittenBy') . ' ' . stripslashes(api_get_person_name($blog_post['firstname'], $blog_post['lastname'])) . '<br />';
  1744. }
  1745. } else
  1746. echo get_lang('NoArticles');
  1747. }
  1748. /**
  1749. * Subscribes a user to a given blog
  1750. * @author Toon Keppens
  1751. *
  1752. * @param Integer $blog_id
  1753. * @param Integer $user_id
  1754. */
  1755. public static function set_user_subscribed ($blog_id,$user_id) {
  1756. // Init
  1757. $tbl_blogs_rel_user = Database::get_course_table(TABLE_BLOGS_REL_USER);
  1758. $tbl_user_permissions = Database::get_course_table(TABLE_PERMISSION_USER);
  1759. $course_id = api_get_course_int_id();
  1760. // Subscribe the user
  1761. $sql = "INSERT INTO $tbl_blogs_rel_user (c_id, blog_id, user_id ) VALUES ($course_id, '".(int)$blog_id."', '".(int)$user_id."');";
  1762. $result = Database::query($sql);
  1763. // Give this user basic rights
  1764. $sql="INSERT INTO $tbl_user_permissions (c_id, user_id,tool,action) VALUES ($course_id, '".(int)$user_id."','BLOG_" . (int)$blog_id."','article_add')";
  1765. $result = Database::query($sql);
  1766. $sql="INSERT INTO $tbl_user_permissions (c_id, user_id,tool,action) VALUES ($course_id, '".(int)$user_id."','BLOG_" . (int)$blog_id."','article_comments_add')";
  1767. $result = Database::query($sql);
  1768. }
  1769. /**
  1770. * Unsubscribe a user from a given blog
  1771. * @author Toon Keppens
  1772. *
  1773. * @param Integer $blog_id
  1774. * @param Integer $user_id
  1775. */
  1776. public static function set_user_unsubscribed ($blog_id, $user_id) {
  1777. // Init
  1778. $tbl_blogs_rel_user = Database::get_course_table(TABLE_BLOGS_REL_USER);
  1779. $tbl_user_permissions = Database::get_course_table(TABLE_PERMISSION_USER);
  1780. // Unsubscribe the user
  1781. $sql = "DELETE FROM $tbl_blogs_rel_user WHERE blog_id = '".(int)$blog_id."' AND user_id = '".(int)$user_id."'";
  1782. $result = @Database::query($sql);
  1783. // Remove this user's permissions.
  1784. $sql = "DELETE FROM $tbl_user_permissions WHERE user_id = '".(int)$user_id."'";
  1785. $result = Database::query($sql);
  1786. }
  1787. /**
  1788. * Displays the form to register users in a blog (in a course)
  1789. * The listed users are users subcribed in the course.
  1790. * @author Toon Keppens
  1791. *
  1792. * @param Integer $blog_id
  1793. *
  1794. * @return Html Form with sortable table with users to subcribe in a blog, in a course.
  1795. */
  1796. public static function display_form_user_subscribe ($blog_id) {
  1797. // Init
  1798. global $_course;
  1799. $is_western_name_order = api_is_western_name_order();
  1800. $currentCourse = $_course['sysCode'];
  1801. $tbl_users = Database::get_main_table(TABLE_MAIN_USER);
  1802. $tbl_blogs_rel_user = Database::get_course_table(TABLE_BLOGS_REL_USER);
  1803. $table_course_user = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  1804. echo '<div class="row"><div class="form_header">'.get_lang('SubscribeMembers').'</div></div>';
  1805. $course_id = api_get_course_int_id();
  1806. $properties["width"] = "100%";
  1807. // Get blog members' id.
  1808. $sql = "SELECT user.user_id FROM $tbl_users user
  1809. INNER JOIN $tbl_blogs_rel_user blogs_rel_user
  1810. ON user.user_id = blogs_rel_user.user_id
  1811. WHERE blogs_rel_user.c_id = $course_id AND blogs_rel_user.blog_id = '".intval($blog_id)."'";
  1812. $result = Database::query($sql);
  1813. $blog_member_ids = array();
  1814. while($user = Database::fetch_array($result))
  1815. {
  1816. $blog_member_ids[] = $user['user_id'];
  1817. }
  1818. // Set table headers
  1819. $column_header[] = array ('', false, '');
  1820. if ($is_western_name_order) {
  1821. $column_header[] = array (get_lang('FirstName'), true, '');
  1822. $column_header[] = array (get_lang('LastName'), true, '');
  1823. } else {
  1824. $column_header[] = array (get_lang('LastName'), true, '');
  1825. $column_header[] = array (get_lang('FirstName'), true, '');
  1826. }
  1827. $column_header[] = array (get_lang('Email'), true, '');
  1828. $column_header[] = array (get_lang('Register'), false, '');
  1829. if(isset($_SESSION['session_id'])){
  1830. $session_id = intval($_SESSION['session_id']);
  1831. }
  1832. else{
  1833. $session_id = 0;
  1834. }
  1835. $student_list = CourseManager :: get_student_list_from_course_code($currentCourse, false, $session_id);
  1836. $user_data = array ();
  1837. // Add users that are not in this blog to the list.
  1838. foreach($student_list as $key=>$user) {
  1839. if(isset($user['id_user'])) {
  1840. $user['user_id'] = $user['id_user'];
  1841. }
  1842. if(!in_array($user['user_id'],$blog_member_ids)) {
  1843. $a_infosUser = UserManager :: get_user_info_by_id($user['user_id']);
  1844. $row = array ();
  1845. $row[] = '<input type="checkbox" name="user[]" value="' . $a_infosUser['user_id'] . '" '.(($_GET['selectall'] == "subscribe") ? ' checked="checked" ' : '') . '/>';
  1846. if ($is_western_name_order) {
  1847. $row[] = $a_infosUser["firstname"];
  1848. $row[] = $a_infosUser["lastname"];
  1849. } else {
  1850. $row[] = $a_infosUser["lastname"];
  1851. $row[] = $a_infosUser["firstname"];
  1852. }
  1853. $row[] = Display::encrypted_mailto_link($a_infosUser["email"]);
  1854. //Link to register users
  1855. if($a_infosUser["user_id"] != $_SESSION['_user']['user_id'])
  1856. {
  1857. $row[] = "<a class=\"a_button orange small \" href=\"" .api_get_self()."?action=manage_members&amp;blog_id=$blog_id&amp;register=yes&amp;user_id=" . $a_infosUser["user_id"]."\">" . get_lang('Register')."</a>";
  1858. }
  1859. else
  1860. {
  1861. $row[] = '';
  1862. }
  1863. $user_data[] = $row;
  1864. }
  1865. }
  1866. // Display
  1867. $query_vars['action'] = 'manage_members';
  1868. $query_vars['blog_id'] = $blog_id;
  1869. echo '<form method="post" action="blog.php?action=manage_members&amp;blog_id=' . $blog_id . '">';
  1870. Display::display_sortable_table($column_header, $user_data,null,null,$query_vars);
  1871. $link = '';
  1872. $link .= isset ($_GET['action']) ? 'action=' . Security::remove_XSS($_GET['action']) . '&amp;' : '';
  1873. $link .= "blog_id=$blog_id&amp;";
  1874. echo '<a href="blog.php?' . $link . 'selectall=subscribe">' . get_lang('SelectAll') . '</a> - ';
  1875. echo '<a href="blog.php?' . $link . '">' . get_lang('UnSelectAll') . '</a> ';
  1876. echo get_lang('WithSelected') . ' : ';
  1877. echo '<select name="action">';
  1878. echo '<option value="select_subscribe">' . get_lang('Register') . '</option>';
  1879. echo '</select>';
  1880. echo '<input type="hidden" name="register" value="true" />';
  1881. echo '<button class="save" type="submit">' . get_lang('Ok') . '</button>';
  1882. echo '</form>';
  1883. }
  1884. /**
  1885. * Displays the form to register users in a blog (in a course)
  1886. * The listed users are users subcribed in the course.
  1887. * @author Toon Keppens
  1888. *
  1889. * @param Integer $blog_id
  1890. *
  1891. * @return Html Form with sortable table with users to unsubcribe from a blog.
  1892. */
  1893. public static function display_form_user_unsubscribe ($blog_id) {
  1894. global $_user;
  1895. $is_western_name_order = api_is_western_name_order();
  1896. // Init
  1897. $tbl_users = Database::get_main_table(TABLE_MAIN_USER);
  1898. $tbl_blogs_rel_user = Database::get_course_table(TABLE_BLOGS_REL_USER);
  1899. echo '<div class="row"><div class="form_header">'.get_lang('UnsubscribeMembers').'</div></div>';
  1900. $properties["width"] = "100%";
  1901. //table column titles
  1902. $column_header[] = array ('', false, '');
  1903. if ($is_western_name_order) {
  1904. $column_header[] = array (get_lang('FirstName'), true, '');
  1905. $column_header[] = array (get_lang('LastName'), true, '');
  1906. } else {
  1907. $column_header[] = array (get_lang('LastName'), true, '');
  1908. $column_header[] = array (get_lang('FirstName'), true, '');
  1909. }
  1910. $column_header[] = array (get_lang('Email'), true, '');
  1911. $column_header[] = array (get_lang('TaskManager'), true, '');
  1912. $column_header[] = array (get_lang('UnRegister'), false, '');
  1913. $course_id = api_get_course_int_id();
  1914. $sql_query = "SELECT user.user_id, user.lastname, user.firstname, user.email
  1915. FROM $tbl_users user INNER JOIN $tbl_blogs_rel_user blogs_rel_user
  1916. ON user.user_id = blogs_rel_user.user_id
  1917. WHERE blogs_rel_user.c_id = $course_id AND blogs_rel_user.blog_id = '".(int)$blog_id."'";
  1918. if (!($sql_result = Database::query($sql_query))) {
  1919. return false;
  1920. }
  1921. $user_data = array ();
  1922. while($myrow = Database::fetch_array($sql_result)) {
  1923. $row = array ();
  1924. $row[] = '<input type="checkbox" name="user[]" value="' . $myrow['user_id'] . '" '.(($_GET['selectall'] == "unsubscribe") ? ' checked="checked" ' : '') . '/>';
  1925. if ($is_western_name_order) {
  1926. $row[] = $myrow["firstname"];
  1927. $row[] = $myrow["lastname"];
  1928. } else {
  1929. $row[] = $myrow["lastname"];
  1930. $row[] = $myrow["firstname"];
  1931. }
  1932. $row[] = Display::encrypted_mailto_link($myrow["email"]);
  1933. $sql = "SELECT bt.title task
  1934. FROM " . Database::get_course_table(TABLE_BLOGS_TASKS_REL_USER) . " btu
  1935. INNER JOIN " . Database::get_course_table(TABLE_BLOGS_TASKS) . " bt ON btu.task_id = bt.task_id
  1936. WHERE btu.c_id = $course_id AND
  1937. bt.c_id = $course_id AND
  1938. btu.blog_id = $blog_id AND
  1939. btu.user_id = " . $myrow['user_id'];
  1940. if (!($sql_res = Database::query($sql))) {
  1941. die(Database::error());
  1942. }
  1943. $task = '';
  1944. while($r = Database::fetch_array($sql_res)) {
  1945. $task .= stripslashes($r['task']) . ', ';
  1946. }
  1947. //echo $task;
  1948. $task = (api_strlen(trim($task)) != 0) ? api_substr($task, 0, api_strlen($task) - 2) : get_lang('Reader');
  1949. $row[] = $task;
  1950. //Link to register users
  1951. if($myrow["user_id"] != $_user['user_id']) {
  1952. $row[] = "<a class=\"a_button gray small \" href=\"" .api_get_self()."?action=manage_members&amp;blog_id=$blog_id&amp;unregister=yes&amp;user_id=" . $myrow[user_id]."\">" . get_lang('UnRegister')."</a>";
  1953. } else {
  1954. $row[] = '';
  1955. }
  1956. $user_data[] = $row;
  1957. }
  1958. $query_vars['action'] = 'manage_members';
  1959. $query_vars['blog_id'] = $blog_id;
  1960. echo '<form method="post" action="blog.php?action=manage_members&amp;blog_id=' . $blog_id . '">';
  1961. Display::display_sortable_table($column_header, $user_data,null,null,$query_vars);
  1962. $link = '';
  1963. $link .= isset ($_GET['action']) ? 'action=' . Security::remove_XSS($_GET['action']). '&amp;' : '';
  1964. $link .= "blog_id=$blog_id&amp;";
  1965. echo '<a href="blog.php?' . $link . 'selectall=unsubscribe">' . get_lang('SelectAll') . '</a> - ';
  1966. echo '<a href="blog.php?' . $link . '">' . get_lang('UnSelectAll') . '</a> ';
  1967. echo get_lang('WithSelected') . ' : ';
  1968. echo '<select name="action">';
  1969. echo '<option value="select_unsubscribe">' . get_lang('UnRegister') . '</option>';
  1970. echo '</select>';
  1971. echo '<input type="hidden" name="unregister" value="true" />';
  1972. echo '<button class="save" type="submit">' . get_lang('Ok') . '</button>';
  1973. echo '</form>';
  1974. }
  1975. /**
  1976. * Displays a matrix with selectboxes. On the left: users, on top: possible rights.
  1977. * The blog admin can thus select what a certain user can do in the current blog
  1978. *
  1979. * @param Integer $blog_id
  1980. */
  1981. public static function display_form_user_rights ($blog_id) {
  1982. // Init
  1983. $tbl_users = Database::get_main_table(TABLE_MAIN_USER);
  1984. $tbl_blogs_rel_user = Database::get_course_table(TABLE_BLOGS_REL_USER);
  1985. echo '<div class="row"><div class="form_header">'.get_lang('RightsManager').'</div></div>';
  1986. echo '<br />';
  1987. // Integration of patricks permissions system.
  1988. require_once api_get_path(SYS_CODE_PATH).'permissions/blog_permissions.inc.php';
  1989. }
  1990. /**
  1991. * Displays the form to create a new post
  1992. * @author Toon Keppens
  1993. *
  1994. * @param Integer $blog_id
  1995. */
  1996. public static function display_new_comment_form ($blog_id, $post_id, $title) {
  1997. echo '<form name="add_post" enctype="multipart/form-data" method="post" action="blog.php?action=view_post&amp;blog_id=' . intval($blog_id) . '&amp;post_id=' . intval($post_id) . '">';
  1998. // form title
  1999. echo '<div class="row"><div class="form_header">';
  2000. echo (isset($_GET['task_id']) ? get_lang('ExecuteThisTask') : get_lang('AddNewComment'));
  2001. echo '</div></div>';
  2002. // comment title
  2003. echo '<div><span class="form_required">*</span>' . get_lang('Title') . ': <input name="comment_title" id="comment_title" type="text" size="60" value="Re: '.stripslashes($title) . '" /></div><br />';
  2004. // comment text
  2005. $oFCKeditor = new FCKeditor('comment_text') ;
  2006. $oFCKeditor->Width = '100%';
  2007. $oFCKeditor->Height = '200';
  2008. if(!api_is_allowed_to_edit())
  2009. {
  2010. $oFCKeditor->ToolbarSet = 'ProjectCommentStudent';
  2011. }
  2012. else
  2013. {
  2014. $oFCKeditor->ToolbarSet = 'ProjectComment';
  2015. }
  2016. $oFCKeditor->Value = isset($_POST['comment_text'])?stripslashes($_POST['comment_text']):'';
  2017. echo '<div class="formw">';
  2018. echo $oFCKeditor->Create() ;
  2019. echo '
  2020. </div>';
  2021. // attachment
  2022. echo ' <div class="row">
  2023. <div class="label">
  2024. ' . get_lang('AddAnAttachment') . '
  2025. </div>
  2026. <div class="formw">
  2027. <input type="file" name="user_upload"/>
  2028. </div>
  2029. </div>';
  2030. // attachment comment
  2031. echo ' <div class="row">
  2032. <div class="label">
  2033. ' . get_lang('FileComment') . '
  2034. </div>
  2035. <div class="formw">
  2036. <textarea name="post_file_comment" cols="34" /></textarea>
  2037. </div>
  2038. </div>';
  2039. // attachment comment
  2040. echo ' <div class="row">
  2041. <div class="label">
  2042. </div>
  2043. <div class="formw">
  2044. <input type="hidden" name="action" value="" />
  2045. <input type="hidden" name="comment_parent_id" id="comment_parent_id" value="0" />';
  2046. if(isset($_GET['task_id']))
  2047. {
  2048. echo ' <input type="hidden" name="new_task_execution_submit" value="true" />';
  2049. echo ' <input type="hidden" name="task_id" value="' . intval($_GET['task_id']) . '" />';
  2050. }
  2051. else
  2052. {
  2053. echo ' <input type="hidden" name="new_comment_submit" value="true" />';
  2054. }
  2055. echo ' <button class="save" type="submit" name="Submit">' . get_lang('Save') . '</button>
  2056. </div>
  2057. </div>';
  2058. echo '</form>';
  2059. }
  2060. /**
  2061. * show the calender of the given month
  2062. * @author Patrick Cool
  2063. * @author Toon Keppens
  2064. *
  2065. * @param Array $blogitems an array containing all the blog items for the given month
  2066. * @param Integer $month: the integer value of the month we are viewing
  2067. * @param Integer $year: the 4-digit year indication e.g. 2005
  2068. * @param String $monthName: the language variable for the mont name
  2069. *
  2070. * @return html code
  2071. */
  2072. public static function display_minimonthcalendar ($month, $year, $blog_id) {
  2073. // Init
  2074. global $_user;
  2075. global $DaysShort;
  2076. global $MonthsLong;
  2077. $posts = array();
  2078. $tasks = array();
  2079. $tbl_users = Database::get_main_table(TABLE_MAIN_USER);
  2080. $tbl_blogs_posts = Database::get_course_table(TABLE_BLOGS_POSTS);
  2081. $tbl_blogs_tasks = Database::get_course_table(TABLE_BLOGS_TASKS);
  2082. $tbl_blogs_tasks_rel_user = Database::get_course_table(TABLE_BLOGS_TASKS_REL_USER);
  2083. $tbl_blogs = Database::get_course_table(TABLE_BLOGS);
  2084. $course_id = api_get_course_int_id();
  2085. //Handle leap year
  2086. $numberofdays = array (0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
  2087. if(($year % 400 == 0) or ($year % 4 == 0 and $year % 100 <> 0))
  2088. $numberofdays[2] = 29;
  2089. //Get the first day of the month
  2090. $dayone = getdate(mktime(0, 0, 0, $month, 1, $year));
  2091. $monthName = $MonthsLong[$month-1];
  2092. //Start the week on monday
  2093. $startdayofweek = $dayone['wday'] <> 0 ? ($dayone['wday'] - 1) : 6;
  2094. $backwardsURL = api_get_self()."?blog_id=" . (int)$_GET['blog_id']."&amp;filter=" . Security::remove_XSS($_GET['filter'])."&amp;month=". ($month == 1 ? 12 : $month -1)."&amp;year=". ($month == 1 ? $year -1 : $year);
  2095. $forewardsURL = api_get_self()."?blog_id=" . (int)$_GET['blog_id']."&amp;filter=" . Security::remove_XSS($_GET['filter'])."&amp;month=". ($month == 12 ? 1 : $month +1)."&amp;year=". ($month == 12 ? $year +1 : $year);
  2096. // Get posts for this month
  2097. $sql = "SELECT post.*, DAYOFMONTH(date_creation) as post_day, user.lastname, user.firstname FROM $tbl_blogs_posts post
  2098. INNER JOIN $tbl_users user
  2099. ON post.author_id = user.user_id
  2100. WHERE post.c_id = $course_id AND
  2101. post.blog_id = '".(int)$blog_id."' AND
  2102. MONTH(date_creation) = '".(int)$month."' AND
  2103. YEAR(date_creation) = '".(int)$year."'
  2104. ORDER BY date_creation";
  2105. $result = Database::query($sql);
  2106. // We will create an array of days on which there are posts.
  2107. if( Database::num_rows($result) > 0) {
  2108. while($blog_post = Database::fetch_array($result)) {
  2109. // If the day of this post is not yet in the array, add it.
  2110. if(!in_array($blog_post['post_day'], $posts))
  2111. $posts[] = $blog_post['post_day'];
  2112. }
  2113. }
  2114. // Get tasks for this month
  2115. if($_user['user_id']) {
  2116. $sql = " SELECT task_rel_user.*, DAYOFMONTH(target_date) as task_day, task.title, blog.blog_name
  2117. FROM $tbl_blogs_tasks_rel_user task_rel_user
  2118. INNER JOIN $tbl_blogs_tasks task ON task_rel_user.task_id = task.task_id
  2119. INNER JOIN $tbl_blogs blog ON task_rel_user.blog_id = blog.blog_id
  2120. WHERE
  2121. task_rel_user.c_id = $course_id AND
  2122. task.c_id = $course_id AND
  2123. blog.c_id = $course_id AND
  2124. task_rel_user.user_id = '".(int)$_user['user_id']."'
  2125. AND MONTH(target_date) = '".(int)$month."'
  2126. AND YEAR(target_date) = '".(int)$year."'
  2127. ORDER BY target_date ASC";
  2128. $result = Database::query($sql);
  2129. if (Database::num_rows($result) > 0)
  2130. {
  2131. while($mytask = Database::fetch_array($result))
  2132. {
  2133. $tasks[$mytask['task_day']][$mytask['task_id']]['task_id'] = $mytask['task_id'];
  2134. $tasks[$mytask['task_day']][$mytask['task_id']]['title'] = $mytask['title'];
  2135. $tasks[$mytask['task_day']][$mytask['task_id']]['blog_id'] = $mytask['blog_id'];
  2136. $tasks[$mytask['task_day']][$mytask['task_id']]['blog_name'] = $mytask['blog_name'];
  2137. $tasks[$mytask['task_day']][$mytask['task_id']]['day'] = $mytask['task_day'];
  2138. //echo '<li><a href="blog.php?action=execute_task&amp;blog_id=' . $mytask['blog_id'] . '&amp;task_id='.stripslashes($mytask['task_id']) . '" title="[Blog: ' . $mytask['blog_name'] . '] ' . get_lang('ExecuteThisTask') . '">'.stripslashes($mytask['title']) . '</a></li>';
  2139. }
  2140. }
  2141. }
  2142. echo '<table id="smallcalendar" class="data_table">',
  2143. "<tr id=\"title\">\n",
  2144. "<th width=\"10%\"><a href=\"", $backwardsURL, "\">&laquo;</a></th>\n",
  2145. "<th align=\"center\" width=\"80%\" colspan=\"5\">", $monthName, " ", $year, "</th>\n",
  2146. "<th width=\"10%\" align=\"right\"><a href=\"", $forewardsURL, "\">&raquo;</a></th>\n", "</tr>\n";
  2147. echo "<tr>\n";
  2148. for($ii = 1; $ii < 8; $ii ++)
  2149. echo "<td class=\"weekdays\">", $DaysShort[$ii % 7], "</td>\n";
  2150. echo "</tr>\n";
  2151. $curday = -1;
  2152. $today = getdate();
  2153. while($curday <= $numberofdays[$month])
  2154. {
  2155. echo "<tr>\n";
  2156. for($ii = 0; $ii < 7; $ii ++)
  2157. {
  2158. if(($curday == -1) && ($ii == $startdayofweek))
  2159. $curday = 1;
  2160. if(($curday > 0) && ($curday <= $numberofdays[$month]))
  2161. {
  2162. $bgcolor = $ii < 5 ? $class="class=\"days_week\"" : $class="class=\"days_weekend\"";
  2163. $dayheader = "$curday";
  2164. if(($curday == $today[mday]) && ($year == $today[year]) && ($month == $today[mon]))
  2165. {
  2166. $dayheader = "$curday";
  2167. $class = "class=\"days_today\"";
  2168. }
  2169. echo "\t<td " . $class.">";
  2170. // If there are posts on this day, create a filter link.
  2171. if(in_array($curday, $posts))
  2172. echo '<a href="blog.php?blog_id=' . $blog_id . '&amp;filter=' . $year . '-' . $month . '-' . $curday . '&amp;month=' . $month . '&amp;year=' . $year . '" title="' . get_lang('ViewPostsOfThisDay') . '">' . $curday . '</a>';
  2173. else
  2174. echo $dayheader;
  2175. if (count($tasks) > 0)
  2176. {
  2177. if (is_array($tasks[$curday]))
  2178. {
  2179. // Add tasks to calendar
  2180. foreach ($tasks[$curday] as $task)
  2181. {
  2182. echo '<a href="blog.php?action=execute_task&amp;blog_id=' . $task['blog_id'] . '&amp;task_id='.stripslashes($task['task_id']) . '" title="' . $task['title'] . ' : ' . get_lang('InBlog') . ' : ' . $task['blog_name'] . ' - ' . get_lang('ExecuteThisTask') . '"><img src="../img/blog_task.gif" alt="Task" title="' . get_lang('ExecuteThisTask') . '" /></a>';
  2183. }
  2184. }
  2185. }
  2186. echo "</td>\n";
  2187. $curday ++;
  2188. }
  2189. else
  2190. echo "<td>&nbsp;</td>\n";
  2191. }
  2192. echo "</tr>\n";
  2193. }
  2194. echo "</table>\n";
  2195. }
  2196. /**
  2197. * Blog admin | Display the form to add a new blog.
  2198. *
  2199. */
  2200. public static function display_new_blog_form () {
  2201. echo '<form name="add_blog" method="post" action="blog_admin.php?action=add">
  2202. <div class="row">
  2203. <div class="form_header">
  2204. ' . get_lang('AddBlog') . '
  2205. </div>
  2206. </div>
  2207. <div class="row">
  2208. <div class="label">
  2209. <span class="form_required">*</span>' . get_lang('Title') . '
  2210. </div>
  2211. <div class="formw">
  2212. <input name="blog_name" type="text" size="50" value="'.Security::remove_XSS($_POST['blog_name']).'" />
  2213. </div>
  2214. </div>
  2215. <div class="row">
  2216. <div class="label">
  2217. ' . get_lang('Subtitle') . '
  2218. </div>
  2219. <div class="formw">
  2220. <textarea name="blog_subtitle" cols="45">'.Security::remove_XSS($_POST['blog_subtitle']).'</textarea>
  2221. </div>
  2222. </div>
  2223. <div class="row">
  2224. <div class="label">
  2225. </div>
  2226. <div class="formw">
  2227. <input type="hidden" name="action" value="" />
  2228. <input type="hidden" name="new_blog_submit" value="true" />
  2229. <button class="save" type="submit" name="Submit">' . get_lang('SaveProject') . '</button>
  2230. </div>
  2231. </div>
  2232. </form>
  2233. <div style="clear: both; margin-bottom:10px;"></div>';
  2234. }
  2235. /**
  2236. * Blog admin | Display the form to edit a blog.
  2237. *
  2238. */
  2239. public static function display_edit_blog_form ($blog_id) {
  2240. $course_id = api_get_course_int_id();
  2241. $tbl_blogs = Database::get_course_table(TABLE_BLOGS);
  2242. $sql = "SELECT blog_id, blog_name, blog_subtitle FROM $tbl_blogs WHERE c_id = $course_id AND blog_id = '".(int)$blog_id."'";
  2243. $result = Database::query($sql);
  2244. $blog = Database::fetch_array($result);
  2245. // the form contained errors but we do not want to lose the changes the user already did
  2246. if ($_POST)
  2247. {
  2248. $blog['blog_name'] = Security::remove_XSS($_POST['blog_name']);
  2249. $blog['blog_subtitle'] = Security::remove_XSS($_POST['blog_subtitle']);
  2250. }
  2251. echo '<form name="edit_blog" method="post" action="blog_admin.php?action=edit&blog_id='.Security::remove_XSS($_GET['blog_id']).'">
  2252. <div class="row">
  2253. <div class="form_header">
  2254. ' . get_lang('EditBlog') . '
  2255. </div>
  2256. </div>
  2257. <div class="row">
  2258. <div class="label">
  2259. <span class="form_required">*</span>' . get_lang('Title') . '
  2260. </div>
  2261. <div class="formw">
  2262. <input name="blog_name" type="text" size="50" value="' . $blog['blog_name'] . '" />
  2263. </div>
  2264. </div>
  2265. <div class="row">
  2266. <div class="label">
  2267. ' . get_lang('Subtitle') . '
  2268. </div>
  2269. <div class="formw">
  2270. <textarea name="blog_subtitle" type="text" cols="45">' . $blog['blog_subtitle'] . '</textarea>
  2271. </div>
  2272. </div>
  2273. <div class="row">
  2274. <div class="label">
  2275. </div>
  2276. <div class="formw">
  2277. <input type="hidden" name="action" value="" />
  2278. <input type="hidden" name="edit_blog_submit" value="true" />
  2279. <input type="hidden" name="blog_id" value="' . $blog['blog_id'] . '" />
  2280. <button class="save" type="submit" name="Submit">' . get_lang('Save') . '</button>
  2281. </div>
  2282. </div>
  2283. </form>
  2284. <div style="clear: both; margin-bottom:10px;"></div>
  2285. ';
  2286. }
  2287. /**
  2288. * Blog admin | Returns table with blogs in this course
  2289. */
  2290. public static function display_blog_list () {
  2291. global $charset, $_user;
  2292. $course_id = api_get_course_int_id();
  2293. // Init
  2294. $counter = 0;
  2295. $tbl_blogs = Database::get_course_table(TABLE_BLOGS);
  2296. //condition for the session
  2297. $session_id = api_get_session_id();
  2298. $condition_session = api_get_session_condition($session_id, false);
  2299. $sql = "SELECT blog_name,blog_subtitle,visibility,blog_id FROM $tbl_blogs WHERE c_id = $course_id ORDER BY date_creation DESC";
  2300. $result = Database::query($sql);
  2301. if (Database::num_rows($result)) {
  2302. while ($row_project=Database::fetch_row($result)) {
  2303. $list_info[]=$row_project;
  2304. }
  2305. }
  2306. $list_content_blog = array();
  2307. $list_body_blog = array();
  2308. if (is_array($list_info)) {
  2309. foreach($list_info as $key => $info_log) {
  2310. //validacion when belongs to a session
  2311. $session_img = api_get_session_image($info_log[4], $_user['status']);
  2312. $url_start_blog = 'blog.php' ."?". "blog_id=".$info_log[3]. "&amp;".api_get_cidreq();
  2313. $title = $info_log[0];
  2314. $image = '<img src="../img/blog.gif" border="0" align="absmiddle" alt="' . $title . '">'."\n";
  2315. $list_name = '<div style="float: left; width: 35px; height: 22px;"><a href="'.$url_start_blog.'">' . $image . '</a></div><a href="'.$url_start_blog.'">' .$title. '</a>' . $session_img . "\n";
  2316. $list_body_blog[] = $list_name;
  2317. $list_body_blog[] = $info_log[1];
  2318. $visibility_icon=($info_log[2]==0) ? 'invisible' : 'visible';
  2319. $visibility_info=($info_log[2]==0) ? 'Visible' : 'Invisible';
  2320. $my_image.='<a href="' .api_get_self(). '?action=edit&amp;blog_id=' . $info_log[3] . '">';
  2321. $my_image.='<img src="../img/edit.gif" border="0" title="' . get_lang('EditBlog') . '" />';
  2322. $my_image.="</a>\n";
  2323. $my_image.='<a href="' .api_get_self(). '?action=delete&amp;blog_id=' . $info_log[3] . '" ';
  2324. $my_image.='onclick="javascript:if(!confirm(\''.addslashes(api_htmlentities(get_lang("ConfirmYourChoice"),ENT_QUOTES,$charset)). '\')) return false;" >';
  2325. $my_image.='<img src="../img/delete.gif" border="0" title="' . get_lang('DeleteBlog') . '" />';
  2326. $my_image.="</a>\n";
  2327. $my_image.='<a href="' .api_get_self(). '?action=visibility&amp;blog_id=' . $info_log[3] . '">';
  2328. $my_image.='<img src="../img/' . $visibility_icon . '.gif" border="0" title="' . get_lang($visibility_info) . '" />';
  2329. $my_image.="</a>\n";
  2330. $list_body_blog[]=$my_image;
  2331. $my_image='';
  2332. $list_content_blog[]=$list_body_blog;
  2333. $list_body_blog = array();
  2334. }
  2335. $parameters='';
  2336. //$parameters=array('action'=>Security::remove_XSS($_GET['action']));
  2337. $table = new SortableTableFromArrayConfig($list_content_blog, 1,20,'project');
  2338. //$table->set_additional_parameters($parameters);
  2339. $table->set_header(0, get_lang('Title'));
  2340. $table->set_header(1, get_lang('Subtitle'));
  2341. $table->set_header(2, get_lang('Modify'));
  2342. $table->display();
  2343. }
  2344. }
  2345. }
  2346. /**
  2347. *
  2348. * END CLASS BLOG
  2349. *
  2350. */
  2351. /**
  2352. * Show a list with all the attachments according the parameter's
  2353. * @param the blog's id
  2354. * @param the post's id
  2355. * @param the comment's id
  2356. * @return array with the post info according the parameters
  2357. * @author Julio Montoya Dokeos
  2358. * @version avril 2008, dokeos 1.8.5
  2359. */
  2360. function get_blog_attachment($blog_id, $post_id=null,$comment_id=null)
  2361. {
  2362. $blog_table_attachment = Database::get_course_table(TABLE_BLOGS_ATTACHMENT);
  2363. $blog_id = Database::escape_string($blog_id);
  2364. $comment_id = Database::escape_string($comment_id);
  2365. $post_id = Database::escape_string($post_id);
  2366. $row=array();
  2367. $where='';
  2368. if (!empty ($post_id) && is_numeric($post_id)) {
  2369. $where.=' AND post_id ="'.$post_id.'" ';
  2370. }
  2371. if (!empty ($comment_id) && is_numeric($comment_id)) {
  2372. if (!empty ($post_id)) {
  2373. $where.= ' AND ';
  2374. }
  2375. $where.=' comment_id ="'.$comment_id.'" ';
  2376. }
  2377. $course_id = api_get_course_int_id();
  2378. $sql = 'SELECT path, filename, comment FROM '. $blog_table_attachment.' WHERE c_id = '.$course_id.' AND blog_id ="'.intval($blog_id).'" '.$where;
  2379. $result=Database::query($sql);
  2380. if (Database::num_rows($result)!=0) {
  2381. $row=Database::fetch_array($result);
  2382. }
  2383. return $row;
  2384. }
  2385. /**
  2386. * Delete the all the attachments according the parameters.
  2387. * @param the blog's id
  2388. * @param the post's id
  2389. * @param the comment's id
  2390. * @author Julio Montoya Dokeos
  2391. * @version avril 2008, dokeos 1.8.5
  2392. */
  2393. function delete_all_blog_attachment($blog_id,$post_id=null,$comment_id=null)
  2394. {
  2395. global $_course;
  2396. $blog_table_attachment = Database::get_course_table(TABLE_BLOGS_ATTACHMENT);
  2397. $blog_id = Database::escape_string($blog_id);
  2398. $comment_id = Database::escape_string($comment_id);
  2399. $post_id = Database::escape_string($post_id);
  2400. $course_id = api_get_course_int_id();
  2401. // delete files in DB
  2402. if (!empty ($post_id) && is_numeric($post_id) )
  2403. {
  2404. $where.=' AND post_id ="'.$post_id.'" ';
  2405. }
  2406. if (!empty ($comment_id) && is_numeric($comment_id) )
  2407. {
  2408. if (!empty ($post_id) )
  2409. {
  2410. $where.= ' AND ';
  2411. }
  2412. $where.=' comment_id ="'.$comment_id.'" ';
  2413. }
  2414. // delete all files in directory
  2415. $courseDir = $_course['path'].'/upload/blog';
  2416. $sys_course_path = api_get_path(SYS_COURSE_PATH);
  2417. $updir = $sys_course_path.$courseDir;
  2418. $sql= 'SELECT path FROM '.$blog_table_attachment.' WHERE c_id = '.$course_id.' AND blog_id ="'.intval($blog_id).'" '.$where;
  2419. $result=Database::query($sql);
  2420. while ($row=Database::fetch_row($result))
  2421. {
  2422. $file=$updir.'/'.$row[0];
  2423. if (Security::check_abs_path($file,$updir) )
  2424. {
  2425. @ unlink($file);
  2426. }
  2427. }
  2428. $sql = 'DELETE FROM '. $blog_table_attachment.' WHERE c_id = '.$course_id.' AND blog_id ="'.intval($blog_id).'" '.$where;
  2429. Database::query($sql);
  2430. }
  2431. /**
  2432. * Gets all the post from a given user id
  2433. * @param string db course name
  2434. * @param int user id
  2435. */
  2436. function get_blog_post_from_user($course_code, $user_id) {
  2437. $tbl_blogs = Database::get_course_table(TABLE_BLOGS);
  2438. $tbl_blog_post = Database::get_course_table(TABLE_BLOGS_POSTS);
  2439. $course_info = api_get_course_info($course_code);
  2440. $course_id = $course_info['real_id'];
  2441. $sql = "SELECT DISTINCT blog.blog_id, post_id, title, full_text, post.date_creation
  2442. FROM $tbl_blogs blog INNER JOIN $tbl_blog_post post
  2443. ON (blog.blog_id = post.blog_id)
  2444. WHERE
  2445. blog.c_id = $course_id AND
  2446. post.c_id = $course_id AND
  2447. author_id = $user_id AND visibility = 1
  2448. ORDER BY post.date_creation DESC ";
  2449. $result = Database::query($sql);
  2450. $return_data = '';
  2451. if (Database::num_rows($result)!=0) {
  2452. while ($row=Database::fetch_array($result)) {
  2453. $return_data.= '<div class="clear"></div><br />';
  2454. $return_data.= '<div class="actions" style="margin-left:5px;margin-right:5px;">'.Display::return_icon('blog_article.png',get_lang('BlogPosts')).' '.$row['title'].'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<div style="float:right;margin-top:-18px"><a href="../blog/blog.php?blog_id='.$row['blog_id'].'&gidReq=&cidReq='.$my_course_id.' " >'.get_lang('SeeBlog').'</a></div></div>';
  2455. $return_data.= '<br / >';
  2456. $return_data.= $row['full_text'];
  2457. $return_data.= '<br /><br />';
  2458. }
  2459. }
  2460. return $return_data;
  2461. }
  2462. /**
  2463. * Gets all the post comments from a given user id
  2464. * @param string db course name
  2465. * @param int user id
  2466. */
  2467. function get_blog_comment_from_user($course_code, $user_id) {
  2468. $tbl_blogs = Database::get_course_table(TABLE_BLOGS);
  2469. $tbl_blog_comment = Database::get_course_table(TABLE_BLOGS_COMMENTS);
  2470. $user_id = Database::escape_string($user_id);
  2471. $course_info = api_get_course_info($course_code);
  2472. $course_id = $course_info['real_id'];
  2473. $sql = "SELECT DISTINCT blog.blog_id, comment_id, title, comment, comment.date_creation
  2474. FROM $tbl_blogs blog INNER JOIN $tbl_blog_comment comment
  2475. ON (blog.blog_id = comment.blog_id)
  2476. WHERE blog.c_id = $course_id AND
  2477. comment.c_id = $course_id AND
  2478. author_id = $user_id AND
  2479. visibility = 1
  2480. ORDER BY blog_name";
  2481. $result = Database::query($sql);
  2482. $return_data = '';
  2483. if (Database::num_rows($result)!=0) {
  2484. while ($row=Database::fetch_array($result)) {
  2485. $return_data.= '<div class="clear"></div><br />';
  2486. $return_data.= '<div class="actions" style="margin-left:5px;margin-right:5px;">'.$row['title'].'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<div style="float:right;margin-top:-18px"><a href="../blog/blog.php?blog_id='.$row['blog_id'].'&gidReq=&cidReq='.Security::remove_XSS($course_code).' " >'.get_lang('SeeBlog').'</a></div></div>';
  2487. $return_data.= '<br / >';
  2488. //$return_data.= '<strong>'.$row['title'].'</strong>'; echo '<br>';*/
  2489. $return_data.= $row['comment'];
  2490. $return_data.= '<br />';
  2491. }
  2492. }
  2493. return $return_data;
  2494. }
  2495. ?>