blog.lib.php 110 KB

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