blog.lib.php 124 KB

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