blog.lib.php 108 KB

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