blog.lib.php 125 KB

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