blog.lib.php 102 KB

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