blog.lib.php 118 KB

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