blog.lib.php 108 KB

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