blog.lib.php 101 KB

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