blog.lib.php 108 KB

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