blog.lib.php 123 KB

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