blog.lib.php 109 KB

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