blog.lib.php 104 KB

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