blog.lib.php 110 KB

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