blog.lib.php 123 KB

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