blog.lib.php 125 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257
  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. }