wiki.inc.php 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * Functions library for the wiki tool
  5. * @author Juan Carlos Raña <herodoto@telefonica.net>
  6. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium
  7. * @author Julio Montoya <gugli100@gmail.com> using the pdf.lib.php library
  8. * @package chamilo.wiki
  9. */
  10. /* FUNCTIONS FOR WIKI */
  11. /**
  12. * Create a reference link for a given new page title (adds incremental number
  13. * to the title). It is a recursive function because every newly created
  14. * reflink suggestion has to be checked also
  15. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  16. * @param
  17. */
  18. function createreflink($testvalue)
  19. {
  20. $counter = '';
  21. while (!checktitle($testvalue.$counter)) {
  22. $counter++;
  23. echo $counter."-".$testvalue.$counter."<br />";
  24. }
  25. // the reflink has not been found yet, so it is OK
  26. return $testvalue.$counter;
  27. }
  28. /**
  29. * Check whether this title is already used
  30. * @param string title
  31. * @return bool False if title is already taken
  32. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  33. **/
  34. function checktitle($paramwk)
  35. {
  36. global $tbl_wiki;
  37. global $groupfilter;
  38. global $condition_session;
  39. $course_id = api_get_course_int_id();
  40. $sql = 'SELECT * FROM '.$tbl_wiki.' WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string(
  41. $paramwk
  42. ).'" AND '.$groupfilter.$condition_session.'';
  43. $result = Database::query($sql);
  44. $numberofresults = Database::num_rows($result);
  45. if ($numberofresults == 0) // the value has not been found and is this available
  46. {
  47. return true;
  48. } else // the value has been found
  49. {
  50. return false;
  51. }
  52. }
  53. /**
  54. * check wikilinks that has a page
  55. * @author Juan Carlos Raña <herodoto@telefonica.net>
  56. **/
  57. function links_to($input)
  58. {
  59. $input_array = preg_split("/(\[\[|\]\])/", $input, -1, PREG_SPLIT_DELIM_CAPTURE);
  60. $all_links = array();
  61. foreach ($input_array as $key => $value) {
  62. if ($input_array[$key - 1] == '[[' AND $input_array[$key + 1] == ']]') {
  63. if (api_strpos($value, "|") !== false) {
  64. $full_link_array = explode("|", $value);
  65. $link = trim($full_link_array[0]);
  66. $title = trim($full_link_array[1]);
  67. } else {
  68. $link = trim($value);
  69. $title = trim($value);
  70. }
  71. unset($input_array[$key - 1]);
  72. unset($input_array[$key + 1]);
  73. $all_links[] = Database::escape_string(
  74. str_replace(' ', '_', $link)
  75. ).' '; //replace blank spaces by _ within the links. But to remove links at the end add a blank space
  76. }
  77. }
  78. $output = implode($all_links);
  79. return $output;
  80. }
  81. /**
  82. * detect and add style to external links
  83. * @author Juan Carlos Raña Trabado
  84. **/
  85. function detect_external_link($input)
  86. {
  87. $exlink = 'href=';
  88. $exlinkStyle = 'class="wiki_link_ext" href=';
  89. $output = str_replace($exlink, $exlinkStyle, $input);
  90. return $output;
  91. }
  92. /**
  93. * detect and add style to anchor links
  94. * @author Juan Carlos Raña Trabado
  95. **/
  96. function detect_anchor_link($input)
  97. {
  98. $anchorlink = 'href="#';
  99. $anchorlinkStyle = 'class="wiki_anchor_link" href="#';
  100. $output = str_replace($anchorlink, $anchorlinkStyle, $input);
  101. return $output;
  102. }
  103. /**
  104. * detect and add style to mail links
  105. * author Juan Carlos Raña Trabado
  106. **/
  107. function detect_mail_link($input)
  108. {
  109. $maillink = 'href="mailto';
  110. $maillinkStyle = 'class="wiki_mail_link" href="mailto';
  111. $output = str_replace($maillink, $maillinkStyle, $input);
  112. return $output;
  113. }
  114. /**
  115. * detect and add style to ftp links
  116. * @author Juan Carlos Raña Trabado
  117. **/
  118. function detect_ftp_link($input)
  119. {
  120. $ftplink = 'href="ftp';
  121. $ftplinkStyle = 'class="wiki_ftp_link" href="ftp';
  122. $output = str_replace($ftplink, $ftplinkStyle, $input);
  123. return $output;
  124. }
  125. /**
  126. * detect and add style to news links
  127. * @author Juan Carlos Raña Trabado
  128. **/
  129. function detect_news_link($input)
  130. {
  131. $newslink = 'href="news';
  132. $newslinkStyle = 'class="wiki_news_link" href="news';
  133. $output = str_replace($newslink, $newslinkStyle, $input);
  134. return $output;
  135. }
  136. /**
  137. * detect and add style to irc links
  138. * @author Juan Carlos Raña Trabado
  139. **/
  140. function detect_irc_link($input)
  141. {
  142. $irclink = 'href="irc';
  143. $irclinkStyle = 'class="wiki_irc_link" href="irc';
  144. $output = str_replace($irclink, $irclinkStyle, $input);
  145. return $output;
  146. }
  147. /**
  148. * This function allows users to have [link to a title]-style links like in most regular wikis.
  149. * It is true that the adding of links is probably the most anoying part of Wiki for the people
  150. * who know something about the wiki syntax.
  151. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  152. * Improvements [[]] and [[ | ]]by Juan Carlos Raña
  153. * Improvements internal wiki style and mark group by Juan Carlos Raña
  154. **/
  155. function make_wiki_link_clickable($input)
  156. {
  157. $_course = api_get_course_info();
  158. if (isset($_SESSION['_gid'])) {
  159. $_clean['group_id'] = intval($_SESSION['_gid']);
  160. }
  161. if (isset($_GET['group_id'])) {
  162. $_clean['group_id'] = intval($_GET['group_id']);
  163. }
  164. $session_id = api_get_session_id(); // TODO: check
  165. $input_array = preg_split("/(\[\[|\]\])/", $input, -1, PREG_SPLIT_DELIM_CAPTURE); //now doubles brackets
  166. foreach ($input_array as $key => $value) {
  167. if ($input_array[$key - 1] == '[[' AND $input_array[$key + 1] == ']]') //now doubles brackets
  168. {
  169. /////////// TODO: metawiki
  170. /*
  171. if ($session_id==0)
  172. {
  173. if ($_clean['group_id']==0)
  174. {
  175. $titleg_ex='';
  176. }
  177. else
  178. {
  179. $group_properties = GroupManager :: get_group_properties($_clean['group_id']);
  180. $group_name= $group_properties['name'];
  181. $titleg_ex='<sup><img src="css/wgroup.gif" alt="('.$group_name.')" title="Link to Wikigroup:'.$group_name.'"/></sup>';
  182. }
  183. }
  184. else
  185. {
  186. TODO:logic for sessions
  187. }
  188. */
  189. /////////
  190. //now full wikilink
  191. if (api_strpos($value, "|") !== false) {
  192. $full_link_array = explode("|", $value);
  193. $link = trim(strip_tags($full_link_array[0]));
  194. $title = trim($full_link_array[1]);
  195. } else {
  196. $link = trim(strip_tags($value));
  197. $title = trim($value);
  198. }
  199. //if wikilink is homepage
  200. if ($link == 'index') {
  201. $title = get_lang('DefaultTitle');
  202. }
  203. if ($link == get_lang('DefaultTitle')) {
  204. $link = 'index';
  205. }
  206. // note: checkreflink checks if the link is still free. If it is not used then it returns true, if it is used, then it returns false. Now the title may be different
  207. if (checktitle(strtolower(str_replace(' ', '_', $link)))) {
  208. $link = api_html_entity_decode($link);
  209. $input_array[$key] = '<a href="'.api_get_path(WEB_PATH).'main/wiki/index.php?'.api_get_cidreq(
  210. ).'&action=addnew&amp;title='.api_htmlentities(
  211. urlencode($link)
  212. ).'&group_id='.$_clean['group_id'].'" class="new_wiki_link">'.$title.'</a>';
  213. } else {
  214. $input_array[$key] = '<a href="'.api_get_path(WEB_PATH).'main/wiki/index.php?'.api_get_cidreq(
  215. ).'&action=showpage&amp;title='.urlencode(
  216. strtolower(str_replace(' ', '_', $link))
  217. ).'&group_id='.$_clean['group_id'].'" class="wiki_link">'.$title.'</a>';
  218. }
  219. unset($input_array[$key - 1]);
  220. unset($input_array[$key + 1]);
  221. }
  222. }
  223. $output = implode('', $input_array);
  224. return $output;
  225. }
  226. /**
  227. * This function saves a change in a wiki page
  228. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  229. * @return language string saying that the changes are stored
  230. **/
  231. function save_wiki()
  232. {
  233. global $charset, $tbl_wiki, $_course, $tbl_wiki_conf;
  234. // NOTE: visibility, visibility_disc and ratinglock_disc changes are not made here, but through the interce buttons
  235. // cleaning the variables
  236. $_clean['page_id'] = Database::escape_string($_POST['page_id']);
  237. $_clean['reflink'] = Database::escape_string(trim($_POST['reflink']));
  238. $_clean['title'] = Database::escape_string(trim($_POST['title']));
  239. $_clean['content'] = Database::escape_string($_POST['content']);
  240. if (api_get_setting('htmlpurifier_wiki') == 'true') {
  241. $purifier = new HTMLPurifier();
  242. $_clean['content'] = $purifier->purify($_clean['content']);
  243. }
  244. $_clean['user_id'] = api_get_user_id();
  245. $_clean['assignment'] = Database::escape_string($_POST['assignment']);
  246. $_clean['comment'] = Database::escape_string($_POST['comment']);
  247. $_clean['progress'] = Database::escape_string($_POST['progress']);
  248. $_clean['version'] = intval($_POST['version']) + 1;
  249. $_clean['linksto'] = links_to($_clean['content']); //and check links content
  250. $dtime = date("Y-m-d H:i:s");
  251. $session_id = api_get_session_id();
  252. if (isset($_SESSION['_gid'])) {
  253. $_clean['group_id'] = Database::escape_string($_SESSION['_gid']);
  254. }
  255. if (isset($_GET['group_id'])) {
  256. $_clean['group_id'] = Database::escape_string($_GET['group_id']);
  257. }
  258. //cleaning config variables
  259. if (!empty($_POST['task'])) {
  260. $_clean['task'] = Database::escape_string($_POST['task']);
  261. }
  262. if (!empty($_POST['feedback1']) || !empty($_POST['feedback2']) || !empty($_POST['feedback3'])) {
  263. $_clean['feedback1'] = Database::escape_string($_POST['feedback1']);
  264. $_clean['feedback2'] = Database::escape_string($_POST['feedback2']);
  265. $_clean['feedback3'] = Database::escape_string($_POST['feedback3']);
  266. $_clean['fprogress1'] = Database::escape_string($_POST['fprogress1']);
  267. $_clean['fprogress2'] = Database::escape_string($_POST['fprogress2']);
  268. $_clean['fprogress3'] = Database::escape_string($_POST['fprogress3']);
  269. }
  270. if (Security::remove_XSS($_POST['initstartdate'] == 1)) {
  271. $_clean['startdate_assig'] = Database::escape_string(get_date_from_select('startdate_assig'));
  272. } else {
  273. $_clean['startdate_assig'] = Database::escape_string($_POST['startdate_assig']);
  274. }
  275. if (Security::remove_XSS($_POST['initenddate'] == 1)) {
  276. $_clean['enddate_assig'] = Database::escape_string(get_date_from_select('enddate_assig'));
  277. } else {
  278. $_clean['enddate_assig'] = Database::escape_string($_POST['enddate_assig']);
  279. }
  280. $_clean['delayedsubmit'] = Database::escape_string($_POST['delayedsubmit']);
  281. if (!empty($_POST['max_text']) || !empty($_POST['max_version'])) {
  282. $_clean['max_text'] = Database::escape_string($_POST['max_text']);
  283. $_clean['max_version'] = Database::escape_string($_POST['max_version']);
  284. }
  285. $course_id = api_get_course_int_id();
  286. $sql = "INSERT INTO ".$tbl_wiki." (c_id, page_id, reflink, title, content, user_id, group_id, dtime, assignment, comment, progress, version, linksto, user_ip, session_id)
  287. VALUES ($course_id, '".$_clean['page_id']."','".$_clean['reflink']."','".$_clean['title']."','".$_clean['content']."','".$_clean['user_id']."','".$_clean['group_id']."','".$dtime."','".$_clean['assignment']."','".$_clean['comment']."','".$_clean['progress']."','".$_clean['version']."','".$_clean['linksto']."','".Database::escape_string(
  288. $_SERVER['REMOTE_ADDR']
  289. )."', '".Database::escape_string($session_id)."')";
  290. $result = Database::query($sql);
  291. $Id = Database::insert_id();
  292. if ($Id > 0) {
  293. //insert into item_property
  294. api_item_property_update(
  295. api_get_course_info(),
  296. TOOL_WIKI,
  297. $Id,
  298. 'WikiAdded',
  299. api_get_user_id(),
  300. $_clean['group_id']
  301. );
  302. }
  303. if ($_clean['page_id'] == 0) {
  304. $sql = 'UPDATE '.$tbl_wiki.' SET page_id="'.$Id.'" WHERE c_id = '.$course_id.' AND id="'.$Id.'"';
  305. Database::query($sql);
  306. }
  307. //update wiki config
  308. if ($_clean['reflink'] == 'index' && $_clean['version'] == 1) {
  309. $sql = "INSERT INTO ".$tbl_wiki_conf." (c_id, page_id, task, feedback1, feedback2, feedback3, fprogress1, fprogress2, fprogress3, max_text, max_version, startdate_assig, enddate_assig, delayedsubmit)
  310. VALUES ($course_id, '".$Id."','".$_clean['task']."','".$_clean['feedback1']."','".$_clean['feedback2']."','".$_clean['feedback3']."','".$_clean['fprogress1']."','".$_clean['fprogress2']."','".$_clean['fprogress3']."','".$_clean['max_text']."','".$_clean['max_version']."','".$_clean['startdate_assig']."','".$_clean['enddate_assig']."','".$_clean['delayedsubmit']."')";
  311. } else {
  312. $sql = 'UPDATE'.$tbl_wiki_conf.' SET task="'.$_clean['task'].'", feedback1="'.$_clean['feedback1'].'", feedback2="'.$_clean['feedback2'].'", feedback3="'.$_clean['feedback3'].'", fprogress1="'.$_clean['fprogress1'].'", fprogress2="'.$_clean['fprogress2'].'", fprogress3="'.$_clean['fprogress3'].'", max_text="'.$_clean['max_text'].'", max_version="'.$_clean['max_version'].'", startdate_assig="'.$_clean['startdate_assig'].'", enddate_assig="'.$_clean['enddate_assig'].'", delayedsubmit="'.$_clean['delayedsubmit'].'"
  313. WHERE page_id="'.$_clean['page_id'].'" AND c_id = '.$course_id;
  314. }
  315. Database::query($sql);
  316. api_item_property_update($_course, 'wiki', $Id, 'WikiAdded', api_get_user_id(), $_clean['group_id']);
  317. check_emailcue($_clean['reflink'], 'P', $dtime, $_clean['user_id']);
  318. return get_lang('ChangesStored');
  319. }
  320. /**
  321. * This function restore a wikipage
  322. * @author Juan Carlos Raña <herodoto@telefonica.net>
  323. * @return string Message of success (to be printed on screen)
  324. **/
  325. function restore_wikipage(
  326. $r_page_id,
  327. $r_reflink,
  328. $r_title,
  329. $r_content,
  330. $r_group_id,
  331. $r_assignment,
  332. $r_progress,
  333. $c_version,
  334. $r_version,
  335. $r_linksto
  336. ) {
  337. global $tbl_wiki, $_course;
  338. $r_user_id = api_get_user_id();
  339. $r_dtime = date("Y-m-d H:i:s");
  340. $r_version = $r_version + 1;
  341. $r_comment = get_lang('RestoredFromVersion').': '.$c_version;
  342. $session_id = api_get_session_id();
  343. $course_id = api_get_course_int_id();
  344. $sql = "INSERT INTO ".$tbl_wiki." (c_id, page_id, reflink, title, content, user_id, group_id, dtime, assignment, comment, progress, version, linksto, user_ip, session_id) VALUES
  345. ($course_id, '".$r_page_id."','".$r_reflink."','".$r_title."','".$r_content."','".$r_user_id."','".$r_group_id."','".$r_dtime."','".$r_assignment."','".$r_comment."','".$r_progress."','".$r_version."','".$r_linksto."','".Database::escape_string(
  346. $_SERVER['REMOTE_ADDR']
  347. )."','".Database::escape_string($session_id)."')";
  348. $result = Database::query($sql);
  349. $Id = Database::insert_id();
  350. api_item_property_update($_course, 'wiki', $Id, 'WikiAdded', api_get_user_id(), $r_group_id);
  351. check_emailcue($r_reflink, 'P', $r_dtime, $r_user_id);
  352. return get_lang('PageRestored');
  353. }
  354. /**
  355. * This function delete a wiki
  356. * @author Juan Carlos Raña <herodoto@telefonica.net>
  357. * @return string Message of success (to be printed)
  358. **/
  359. function delete_wiki()
  360. {
  361. global $tbl_wiki, $tbl_wiki_conf, $tbl_wiki_discuss, $tbl_wiki_mailcue, $groupfilter, $condition_session;
  362. $course_id = api_get_course_int_id();
  363. //identify the first id by group = identify wiki
  364. $sql = 'SELECT * FROM '.$tbl_wiki.' WHERE c_id = '.$course_id.' AND '.$groupfilter.$condition_session.' ORDER BY id DESC';
  365. $allpages = Database::query($sql);
  366. while ($row = Database::fetch_array($allpages)) {
  367. $id = $row['id'];
  368. $group_id = $row['group_id'];
  369. $session_id = $row['session_id'];
  370. $page_id = $row['page_id'];
  371. Database::query('DELETE FROM '.$tbl_wiki_conf.' WHERE page_id="'.$id.'" AND c_id = '.$course_id);
  372. Database::query('DELETE FROM '.$tbl_wiki_discuss.' WHERE publication_id="'.$id.'" AND c_id = '.$course_id);
  373. }
  374. Database::query(
  375. 'DELETE FROM '.$tbl_wiki_mailcue.' WHERE session_id="'.$session_id.'" AND group_id="'.$group_id.'" AND c_id = '.$course_id
  376. );
  377. Database::query(
  378. 'DELETE FROM '.$tbl_wiki.' WHERE session_id="'.$session_id.'" AND group_id="'.$group_id.'" AND c_id = '.$course_id
  379. );
  380. return get_lang('WikiDeleted');
  381. }
  382. /**
  383. * This function saves a new wiki page.
  384. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  385. * @todo consider merging this with the function save_wiki into one single function.
  386. * @return string Message of success
  387. **/
  388. function save_new_wiki()
  389. {
  390. global $charset;
  391. global $tbl_wiki;
  392. global $assig_user_id; //need for assignments mode
  393. global $tbl_wiki_conf;
  394. global $page;
  395. // cleaning the variables
  396. $_clean['assignment'] = Database::escape_string($_POST['assignment']);
  397. // session_id
  398. $session_id = api_get_session_id();
  399. if ($_clean['assignment'] == 2 || $_clean['assignment'] == 1) { // Unlike ordinary pages of pages of assignments. Allow create a ordinary page although there is a assignment with the same name
  400. $page = str_replace(' ', '_', $_POST['title']."_uass".$assig_user_id);
  401. } else {
  402. $page = str_replace(' ', '_', $_POST['title']);
  403. }
  404. $_clean['reflink'] = Database::escape_string(strip_tags(api_htmlentities($page)));
  405. $_clean['title'] = Database::escape_string(strip_tags(trim($_POST['title'])));
  406. $_clean['content'] = Database::escape_string($_POST['content']);
  407. if (api_get_setting('htmlpurifier_wiki') == 'true') {
  408. $purifier = new HTMLPurifier();
  409. $_clean['content'] = $purifier->purify($_clean['content']);
  410. }
  411. //re-check after strip_tags if the title is empty
  412. if (empty($_clean['title']) || empty($_clean['reflink'])) {
  413. return false;
  414. }
  415. if ($_clean['assignment'] == 2) { //config by default for individual assignment (students)
  416. $_clean['user_id'] = (int)Database::escape_string(
  417. $assig_user_id
  418. ); //Identifies the user as a creator, not the teacher who created
  419. $_clean['visibility'] = 0;
  420. $_clean['visibility_disc'] = 0;
  421. $_clean['ratinglock_disc'] = 0;
  422. } else {
  423. $_clean['user_id'] = api_get_user_id();
  424. $_clean['visibility'] = 1;
  425. $_clean['visibility_disc'] = 1;
  426. $_clean['ratinglock_disc'] = 1;
  427. }
  428. $_clean['comment'] = Database::escape_string($_POST['comment']);
  429. $_clean['progress'] = Database::escape_string($_POST['progress']);
  430. $_clean['version'] = 1;
  431. if (isset($_SESSION['_gid'])) {
  432. $_clean['group_id'] = (int)$_SESSION['_gid'];
  433. }
  434. if (isset($_GET['group_id'])) {
  435. $_clean['group_id'] = (int)Database::escape_string($_GET['group_id']);
  436. }
  437. $_clean['linksto'] = links_to($_clean['content']); //check wikilinks
  438. //cleaning config variables
  439. $_clean['task'] = Database::escape_string($_POST['task']);
  440. $_clean['feedback1'] = Database::escape_string($_POST['feedback1']);
  441. $_clean['feedback2'] = Database::escape_string($_POST['feedback2']);
  442. $_clean['feedback3'] = Database::escape_string($_POST['feedback3']);
  443. $_clean['fprogress1'] = Database::escape_string($_POST['fprogress1']);
  444. $_clean['fprogress2'] = Database::escape_string($_POST['fprogress2']);
  445. $_clean['fprogress3'] = Database::escape_string($_POST['fprogress3']);
  446. if ($_POST['initstartdate'] == 1) {
  447. $_clean['startdate_assig'] = Database::escape_string(get_date_from_select('startdate_assig'));
  448. } else {
  449. $_clean['startdate_assig'] = Database::escape_string($_POST['startdate_assig']);
  450. }
  451. if ($_POST['initenddate'] == 1) {
  452. $_clean['enddate_assig'] = Database::escape_string(get_date_from_select('enddate_assig'));
  453. } else {
  454. $_clean['enddate_assig'] = Database::escape_string($_POST['enddate_assig']);
  455. }
  456. $_clean['delayedsubmit'] = Database::escape_string($_POST['delayedsubmit']);
  457. $_clean['max_text'] = Database::escape_string($_POST['max_text']);
  458. $_clean['max_version'] = Database::escape_string($_POST['max_version']);
  459. $course_id = api_get_course_int_id();
  460. //filter no _uass
  461. if (api_eregi('_uass', $_POST['title']) || (api_strtoupper(trim($_POST['title'])) == 'INDEX' || api_strtoupper(
  462. trim(api_htmlentities($_POST['title'], ENT_QUOTES, $charset))
  463. ) == api_strtoupper(api_htmlentities(get_lang('DefaultTitle'), ENT_QUOTES, $charset)))
  464. ) {
  465. $message = get_lang('GoAndEditMainPage');
  466. Display::display_warning_message($message, false);
  467. } else {
  468. $var = $_clean['reflink'];
  469. $group_id = Security::remove_XSS($_GET['group_id']);
  470. if (!checktitle($var)) {
  471. return get_lang(
  472. 'WikiPageTitleExist'
  473. ).'<a href="index.php?action=edit&amp;title='.$var.'&group_id='.$group_id.'">'.$_POST['title'].'</a>';
  474. } else {
  475. $dtime = date("Y-m-d H:i:s");
  476. $sql = "INSERT INTO ".$tbl_wiki." (c_id, reflink, title, content, user_id, group_id, dtime, visibility, visibility_disc, ratinglock_disc, assignment, comment, progress, version, linksto, user_ip, session_id) VALUES
  477. ($course_id, '".$_clean['reflink']."','".$_clean['title']."','".$_clean['content']."','".$_clean['user_id']."','".$_clean['group_id']."','".$dtime."','".$_clean['visibility']."','".$_clean['visibility_disc']."','".$_clean['ratinglock_disc']."','".$_clean['assignment']."','".$_clean['comment']."','".$_clean['progress']."','".$_clean['version']."','".$_clean['linksto']."','".Database::escape_string(
  478. $_SERVER['REMOTE_ADDR']
  479. )."', '".Database::escape_string($session_id)."')";
  480. $result = Database::query($sql);
  481. $Id = Database::insert_id();
  482. if ($Id > 0) {
  483. //insert into item_property
  484. api_item_property_update(
  485. api_get_course_info(),
  486. TOOL_WIKI,
  487. $Id,
  488. 'WikiAdded',
  489. api_get_user_id(),
  490. $_clean['group_id']
  491. );
  492. }
  493. $sql = 'UPDATE '.$tbl_wiki.' SET page_id="'.$Id.'" WHERE c_id = '.$course_id.' AND id="'.$Id.'"';
  494. Database::query($sql);
  495. //insert wiki config
  496. $sql = "INSERT INTO ".$tbl_wiki_conf." (c_id, page_id, task, feedback1, feedback2, feedback3, fprogress1, fprogress2, fprogress3, max_text, max_version, startdate_assig, enddate_assig, delayedsubmit) VALUES
  497. ($course_id, '".$Id."','".$_clean['task']."','".$_clean['feedback1']."','".$_clean['feedback2']."','".$_clean['feedback3']."','".$_clean['fprogress1']."','".$_clean['fprogress2']."','".$_clean['fprogress3']."','".$_clean['max_text']."','".$_clean['max_version']."','".$_clean['startdate_assig']."','".$_clean['enddate_assig']."','".$_clean['delayedsubmit']."')";
  498. Database::query($sql);
  499. check_emailcue(0, 'A');
  500. return get_lang('NewWikiSaved');
  501. }
  502. }
  503. //end filter no _uass
  504. }
  505. /**
  506. * This function displays the form for adding a new wiki page.
  507. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  508. * @return html code
  509. **/
  510. function display_new_wiki_form()
  511. {
  512. $_course = api_get_course_info();
  513. global $page;
  514. ?>
  515. <script type="text/javascript">
  516. function CheckSend() {
  517. if (document.form1.title.value == "") {
  518. alert("<?php echo get_lang('NoWikiPageTitle');?>");
  519. document.form1.title.focus();
  520. return false;
  521. }
  522. return true;
  523. }
  524. function setFocus() {
  525. $("#wiki_title").focus();
  526. }
  527. $(document).ready(function () {
  528. setFocus();
  529. });
  530. </script>
  531. <?php
  532. //form
  533. echo '<form name="form1" method="post" onsubmit="return CheckSend()" action="'.api_get_self().'?'.api_get_cidreq(
  534. ).'&action=showpage&amp;title='.api_htmlentities(
  535. urlencode(strtolower(str_replace(' ', '_', $page)))
  536. ).'&group_id='.api_htmlentities($_GET['group_id']).'">';
  537. echo '<div id="wikititle" style="min-height:30px;">';
  538. echo '<div style="width:70%;float:left;"><span class="form_required">*</span> '.get_lang(
  539. Title
  540. ).': <input type="text" id="wiki_title" name="title" value="'.api_htmlentities($_GET['title']).'" size="40"></div>';
  541. if (api_is_allowed_to_edit(false, true) || api_is_platform_admin()) {
  542. $_clean['group_id'] = (int)$_SESSION['_gid']; // TODO: check if delete ?
  543. echo'<a href="javascript://" onclick="advanced_parameters()" ><span id="plus_minus" style="float:right">&nbsp;'.Display::return_icon(
  544. 'div_show.gif',
  545. get_lang('Show'),
  546. array('style' => 'vertical-align:middle')
  547. ).'&nbsp;'.get_lang('AdvancedParameters').'</span></a>';
  548. echo '<div id="options" style="display:none; margin: 20px;" >';
  549. //task
  550. echo '<div>&nbsp;</div>';
  551. echo '<div style= "border : 1px dotted; padding:4px; margin-top:20px;">';
  552. echo '<input type="checkbox" value="1" name="checktask" onclick="javascript: if(this.checked){document.getElementById(\'option4\').style.display=\'block\';}else{document.getElementById(\'option4\').style.display=\'none\';}"/>&nbsp;
  553. '.Display::return_icon('wiki_task.png', get_lang('DefineTask'), '', ICON_SIZE_SMALL).' '.get_lang(
  554. 'DescriptionOfTheTask'
  555. ).'';
  556. echo '&nbsp;&nbsp;&nbsp;<span id="msg_error4" style="display:none;color:red"></span>';
  557. echo '<div id="option4" style="padding:4px; margin:5px; border:1px dotted; display:none;">';
  558. echo '<table border="0" style="font-weight:normal">';
  559. echo '<tr>';
  560. echo '<td>'.get_lang('DescriptionOfTheTask').'</td>';
  561. echo '</tr>';
  562. echo '<tr>';
  563. //echo '<td><textarea name="task" cols="60" rows="4" >'.stripslashes($row['task']).'</textarea></td>'; // TODO: ¿delete?
  564. echo '<td>'.api_disp_html_area(
  565. 'task',
  566. '',
  567. '',
  568. '',
  569. null,
  570. array('ToolbarSet' => 'wiki_task', 'Width' => '580', 'Height' => '200')
  571. ).'</td>';
  572. echo '</tr>';
  573. echo '</table>';
  574. echo '</div>';
  575. //feedback
  576. echo '<div>&nbsp;</div><input type="checkbox" value="1" name="checkfeedback" onclick="javascript: if(this.checked){document.getElementById(\'option2\').style.display=\'block\';}else{document.getElementById(\'option2\').style.display=\'none\';}"/>&nbsp;'.get_lang(
  577. 'AddFeedback'
  578. ).'';
  579. echo '&nbsp;&nbsp;&nbsp;<span id="msg_error2" style="display:none;color:red"></span>';
  580. echo '<div id="option2" style="padding:4px; margin:5px; border:1px dotted; display:none;">';
  581. echo '<table border="0" style="font-weight:normal" align="center">';
  582. echo '<tr>';
  583. echo '<td colspan="2">'.get_lang('Feedback1').'</td>';
  584. echo '<td colspan="2">'.get_lang('Feedback2').'</td>';
  585. echo '<td colspan="2">'.get_lang('Feedback3').'</td>';
  586. echo '</tr>';
  587. echo '<tr>';
  588. echo '<td colspan="2"><textarea name="feedback1" cols="21" rows="4"></textarea></td>';
  589. echo '<td colspan="2"><textarea name="feedback2" cols="21" rows="4"></textarea></td>';
  590. echo '<td colspan="2"><textarea name="feedback3" cols="21" rows="4"></textarea></td>';
  591. echo '</tr>';
  592. echo '<tr>';
  593. echo '<td>'.get_lang('FProgress').':</td>';
  594. echo '<td><select name="fprogress1">
  595. <option value="0" selected>0</option>
  596. <option value="10">10</option>
  597. <option value="20">20</option>
  598. <option value="30">30</option>
  599. <option value="40">40</option>
  600. <option value="50">50</option>
  601. <option value="60">60</option>
  602. <option value="70">70</option>
  603. <option value="80">80</option>
  604. <option value="90">90</option>
  605. <option value="100">100</option>
  606. </select> %</td>';
  607. echo '<td>'.get_lang('FProgress').':</td>';
  608. echo '<td><select name="fprogress2">
  609. <option value="0" selected>0</option>
  610. <option value="10">10</option>
  611. <option value="20">20</option>
  612. <option value="30">30</option>
  613. <option value="40">40</option>
  614. <option value="50">50</option>
  615. <option value="60">60</option>
  616. <option value="70">70</option>
  617. <option value="80">80</option>
  618. <option value="90">90</option>
  619. <option value="100">100</option>
  620. </select> %</td>';
  621. echo '<td>'.get_lang('FProgress').':</td>';
  622. echo '<td><select name="fprogress3">
  623. <option value="0" selected>0</option>
  624. <option value="10">10</option>
  625. <option value="20">20</option>
  626. <option value="30">30</option>
  627. <option value="40">40</option>
  628. <option value="50">50</option>
  629. <option value="60">60</option>
  630. <option value="70">70</option>
  631. <option value="80">80</option>
  632. <option value="90">90</option>
  633. <option value="100">100</option>
  634. </select> %</td>';
  635. echo '</tr>';
  636. echo '</table>';
  637. echo '</div>';
  638. //time limit
  639. echo '<div>&nbsp;</div><input type="checkbox" value="1" name="checktimelimit" onclick="javascript: if(this.checked){document.getElementById(\'option1\').style.display=\'block\';}else{document.getElementById(\'option1\').style.display=\'none\';}"/>&nbsp;'.get_lang(
  640. 'PutATimeLimit'
  641. ).'';
  642. echo '&nbsp;&nbsp;&nbsp;<span id="msg_error1" style="display:none;color:red"></span>';
  643. echo '<div id="option1" style="padding:4px; margin:5px; border:1px dotted; display:none;">';
  644. echo '<table width="100%" border="0" style="font-weight:normal">';
  645. echo '<tr>';
  646. echo '<td align="right">'.get_lang("StartDate").':</td>';
  647. echo '<td>';
  648. echo draw_date_picker('startdate_assig').' <input type="checkbox" name="initstartdate" value="1"> '.get_lang(
  649. 'Yes'
  650. ).'/'.get_lang('No').'';
  651. echo '</td>';
  652. echo '</tr>';
  653. echo '<tr>';
  654. echo '<td align="right">'.get_lang("EndDate").':</td>';
  655. echo '<td>';
  656. echo draw_date_picker('enddate_assig').' <input type="checkbox" name="initenddate" value="1"> '.get_lang(
  657. 'Yes'
  658. ).'/'.get_lang('No').'';
  659. echo '</td>';
  660. echo '</tr>';
  661. echo '<tr>';
  662. echo '<td align="right">'.get_lang('AllowLaterSends').':</td>';
  663. echo '<td><input type="checkbox" name="delayedsubmit" value="1"></td>';
  664. echo '</tr>';
  665. echo'</table>';
  666. echo '</div>';
  667. //other limit
  668. echo '<div>&nbsp;</div><input type="checkbox" value="1" name="checkotherlimit" onclick="javascript: if(this.checked){document.getElementById(\'option3\').style.display=\'block\';}else{document.getElementById(\'option3\').style.display=\'none\';}"/>&nbsp;'.get_lang(
  669. 'OtherSettings'
  670. ).'';
  671. echo '&nbsp;&nbsp;&nbsp;<span id="msg_error3" style="display:none;color:red"></span>';
  672. echo '<div id="option3" style="padding:4px; margin:5px; border:1px dotted; display:none;">';
  673. echo '<div style="font-weight:normal"; align="center">'.get_lang(
  674. 'NMaxWords'
  675. ).':&nbsp;<input type="text" name="max_text" size="3">&nbsp;&nbsp;'.get_lang(
  676. 'NMaxVersion'
  677. ).':&nbsp;<input type="text" name="max_version" size="3"></div>';
  678. echo '</div>';
  679. echo '</div>';
  680. //to define as an individual assignment
  681. echo '<div style= "border : 1px dotted; padding:4px; margin-top:20px;"><img src="../img/icons/22/wiki_assignment.png" title="'.get_lang(
  682. 'CreateAssignmentPage'
  683. ).'" alt="'.get_lang('CreateAssignmentPage').'"/>&nbsp;'.get_lang(
  684. 'DefineAssignmentPage'
  685. ).': <input type="checkbox" name="assignment" value="1"></div>'; // 1= teacher 2 =student
  686. //
  687. echo'</div>';
  688. }
  689. echo '</div>';
  690. echo '<div id="wikicontent">';
  691. api_disp_html_area(
  692. 'content',
  693. '',
  694. '',
  695. '',
  696. null,
  697. api_is_allowed_to_edit(null, true)
  698. ? array('ToolbarSet' => 'Wiki', 'Width' => '100%', 'Height' => '400')
  699. : array('ToolbarSet' => 'WikiStudent', 'Width' => '100%', 'Height' => '400', 'UserStatus' => 'student')
  700. );
  701. echo '<br/>';
  702. echo '<br/>';
  703. echo get_lang('Comments').':&nbsp;&nbsp;<input type="text" name="comment" size="40"><br /><br />';
  704. echo get_lang('Progress').':&nbsp;&nbsp;<select name="progress" id="progress">
  705. <option value="0" selected>0</option>
  706. <option value="10">10</option>
  707. <option value="20">20</option>
  708. <option value="30">30</option>
  709. <option value="40">40</option>
  710. <option value="50">50</option>
  711. <option value="60">60</option>
  712. <option value="70">70</option>
  713. <option value="80">80</option>
  714. <option value="90">90</option>
  715. <option value="100">100</option>
  716. </select> %';
  717. echo '<br/><br/>';
  718. echo '<input type="hidden" name="wpost_id" value="'.md5(uniqid(rand(), true)).'">'; //prevent double post
  719. echo '<button class="save" type="submit" name="SaveWikiNew">'.get_lang(
  720. 'Save'
  721. ).'</button>'; //for button icon. Don't change name (see fckeditor/editor/plugins/customizations/fckplugin_compressed.js and fckplugin.js
  722. echo '</div>';
  723. echo '</form>';
  724. }
  725. /**
  726. * This function displays a wiki entry
  727. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  728. * @author Juan Carlos Raña Trabado
  729. * @return html code
  730. **/
  731. function display_wiki_entry($newtitle)
  732. {
  733. global $charset, $tbl_wiki, $tbl_wiki_conf, $groupfilter, $condition_session, $page;
  734. $course_id = api_get_course_int_id();
  735. if ($newtitle) {
  736. $pageMIX = $newtitle; //display the page after it is created
  737. } else {
  738. $pageMIX = $page; //display current page
  739. }
  740. $_clean['group_id'] = (int)$_SESSION['_gid'];
  741. $session_id = api_get_session_id();
  742. if ($_GET['view']) {
  743. $_clean['view'] = (int)Database::escape_string($_GET['view']);
  744. $filter = ' AND w.id="'.$_clean['view'].'"';
  745. }
  746. //first, check page visibility in the first page version
  747. $sql = 'SELECT * FROM '.$tbl_wiki.' WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string(
  748. $pageMIX
  749. ).'" AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  750. $result = Database::query($sql);
  751. $row = Database::fetch_array($result);
  752. $KeyVisibility = $row['visibility'];
  753. // second, show the last version
  754. $sql = 'SELECT * FROM '.$tbl_wiki.' w , '.$tbl_wiki_conf.' wc
  755. WHERE wc.c_id = '.$course_id.' AND
  756. w.c_id = '.$course_id.' AND
  757. wc.page_id = w.page_id AND
  758. w.reflink = "'.Database::escape_string($pageMIX).'" AND
  759. w.session_id = '.$session_id.' AND
  760. w.'.$groupfilter.' '.$filter.'
  761. ORDER BY id DESC';
  762. $result = Database::query($sql);
  763. $row = Database::fetch_array(
  764. $result
  765. ); // we do not need a while loop since we are always displaying the last version
  766. //update visits
  767. if ($row['id']) {
  768. $sql = 'UPDATE '.$tbl_wiki.' SET hits=(hits+1) WHERE c_id = '.$course_id.' AND id='.$row['id'].'';
  769. Database::query($sql);
  770. }
  771. // if both are empty and we are displaying the index page then we display the default text.
  772. if ($row['content'] == '' AND $row['title'] == '' AND $page == 'index') {
  773. if (api_is_allowed_to_edit(false, true) || api_is_platform_admin() || GroupManager :: is_user_in_group(
  774. api_get_user_id(),
  775. $_SESSION['_gid']
  776. )
  777. ) {
  778. //Table structure for better export to pdf
  779. $default_table_for_content_Start = '<table align="center" border="0"><tr><td align="center">';
  780. $default_table_for_content_End = '</td></tr></table>';
  781. $content = $default_table_for_content_Start.sprintf(
  782. get_lang('DefaultContent'),
  783. api_get_path(WEB_IMG_PATH)
  784. ).$default_table_for_content_End;
  785. $title = get_lang('DefaultTitle');
  786. } else {
  787. return Display::display_normal_message(get_lang('WikiStandBy'));
  788. }
  789. } else {
  790. $content = Security::remove_XSS($row['content'], COURSEMANAGERLOWSECURITY);
  791. $title = $row['title'];
  792. }
  793. //assignment mode: identify page type
  794. if ($row['assignment'] == 1) {
  795. $icon_assignment = Display::return_icon(
  796. 'wiki_assignment.png',
  797. get_lang('AssignmentDescExtra'),
  798. '',
  799. ICON_SIZE_SMALL
  800. );
  801. } elseif ($row['assignment'] == 2) {
  802. $icon_assignment = Display::return_icon('wiki_work.png', get_lang('AssignmentWork'), '', ICON_SIZE_SMALL);
  803. }
  804. //task mode
  805. if (!empty($row['task'])) {
  806. $icon_task = Display::return_icon('wiki_task.png', get_lang('StandardTask'), '', ICON_SIZE_SMALL);
  807. }
  808. //Show page. Show page to all users if isn't hide page. Mode assignments: if student is the author, can view
  809. if ($KeyVisibility == "1" || api_is_allowed_to_edit(false, true) || api_is_platform_admin(
  810. ) || ($row['assignment'] == 2 && $KeyVisibility == "0" && (api_get_user_id() == $row['user_id']))
  811. ) {
  812. echo '<div id="wikititle">';
  813. // page action: protecting (locking) the page
  814. if (api_is_allowed_to_edit(false, true) || api_is_platform_admin()) {
  815. if (check_protect_page() == 1) {
  816. $protect_page = Display::return_icon('lock.png', get_lang('PageLockedExtra'), '', ICON_SIZE_SMALL);
  817. $lock_unlock_protect = 'unlock';
  818. } else {
  819. $protect_page = Display::return_icon('unlock.png', get_lang('PageUnlockedExtra'), '', ICON_SIZE_SMALL);
  820. $lock_unlock_protect = 'lock';
  821. }
  822. }
  823. if ($row['id']) {
  824. echo '<span style="float:right;">';
  825. echo '<a href="index.php?action=showpage&amp;actionpage='.$lock_unlock_protect.'&amp;title='.api_htmlentities(
  826. urlencode($page)
  827. ).'">'.$protect_page.'</a>';
  828. echo '</span>';
  829. }
  830. //page action: visibility
  831. if (api_is_allowed_to_edit(false, true) || api_is_platform_admin()) {
  832. if (check_visibility_page() == 1) {
  833. // TODO: FIX This hides the icon eye closed to users of work they can see yours
  834. //if(($row['assignment']==2 && $KeyVisibility=="0" && (api_get_user_id()==$row['user_id']))==false)
  835. //{
  836. //
  837. // }
  838. $visibility_page = Display::return_icon('visible.png', get_lang('ShowPageExtra'), '', ICON_SIZE_SMALL);
  839. $lock_unlock_visibility = 'invisible';
  840. } else {
  841. $visibility_page = Display::return_icon(
  842. 'invisible.png',
  843. get_lang('HidePageExtra'),
  844. '',
  845. ICON_SIZE_SMALL
  846. );
  847. $lock_unlock_visibility = 'visible';
  848. }
  849. }
  850. if ($row['id']) {
  851. echo '<span style="float:right;">';
  852. echo '<a href="index.php?action=showpage&amp;actionpage='.$lock_unlock_visibility.'&amp;title='.api_htmlentities(
  853. urlencode($page)
  854. ).'">'.$visibility_page.'</a>';
  855. echo '</span>';
  856. }
  857. //page action: notification
  858. if (api_is_allowed_to_session_edit()) {
  859. if (check_notify_page($page) == 1) {
  860. $notify_page = Display::return_icon(
  861. 'messagebox_info.png',
  862. get_lang('NotifyByEmail'),
  863. '',
  864. ICON_SIZE_SMALL
  865. );
  866. $lock_unlock_notify_page = 'unlocknotify';
  867. } else {
  868. $notify_page = Display::return_icon('mail.png', get_lang('CancelNotifyByEmail'), '', ICON_SIZE_SMALL);
  869. $lock_unlock_notify_page = 'locknotify';
  870. }
  871. }
  872. echo '<span style="float:right;">';
  873. echo '<a href="index.php?action=showpage&amp;actionpage='.$lock_unlock_notify_page.'&amp;title='.api_htmlentities(
  874. urlencode($page)
  875. ).'">'.$notify_page.'</a>';
  876. echo '</span>';
  877. //ONly available if row['id'] is set
  878. if ($row['id']) {
  879. //page action: export to pdf
  880. echo '<span style="float:right;padding-top:5px;">';
  881. echo '<form name="form_export2PDF" method="post" action="index.php">';
  882. echo '<input type="hidden" name="action" value="export_to_pdf">';
  883. echo '<input type="hidden" name="wiki_id" value="'.$row['id'].'">';
  884. echo '<input type="image" src="../img/icons/22/pdf.png" border ="0" title="'.get_lang(
  885. 'ExportToPDF'
  886. ).'" alt="'.get_lang('ExportToPDF').'" style=" width:22px; border:none; margin-top: -9px">';
  887. echo '</form>';
  888. echo '</span>';
  889. //page action: copy last version to doc area
  890. if (api_is_allowed_to_edit(false, true) || api_is_platform_admin()) {
  891. echo '<span style="float:right;">';
  892. echo '<form name="form_export2DOC" method="post" action="index.php" >';
  893. echo '<input type=hidden name="export2DOC" value="export2doc">';
  894. echo '<input type=hidden name="doc_id" value="'.$row['id'].'">';
  895. echo '<input type="image" src="../img/icons/22/export_to_documents.png" border ="0" title="'.get_lang(
  896. 'ExportToDocArea'
  897. ).'" alt="'.get_lang('ExportToDocArea').'" style=" width:22px; border:none; margin-top: -6px">';
  898. echo '</form>';
  899. echo '</span>';
  900. }
  901. }
  902. //export to print
  903. ?>
  904. <script>
  905. function goprint() {
  906. var a = window.open('', '', 'width=800,height=600');
  907. a.document.open("text/html");
  908. a.document.write(document.getElementById('wikicontent').innerHTML);
  909. a.document.close();
  910. a.print();
  911. }
  912. </script>
  913. <?php
  914. echo '<span style="float:right; cursor: pointer;">';
  915. echo Display::return_icon(
  916. 'printer.png',
  917. get_lang('Print'),
  918. array('onclick' => "javascript: goprint();"),
  919. ICON_SIZE_SMALL
  920. );
  921. echo '</span>';
  922. if (empty($title)) {
  923. $title = get_lang('DefaultTitle');
  924. }
  925. if (wiki_exist($title)) {
  926. echo $icon_assignment.'&nbsp;'.$icon_task.'&nbsp;'.api_htmlentities($title);
  927. } else {
  928. echo api_htmlentities($title);
  929. }
  930. echo '</div>';
  931. echo '<div id="wikicontent">'.make_wiki_link_clickable(
  932. detect_external_link(
  933. detect_anchor_link(detect_mail_link(detect_ftp_link(detect_irc_link(detect_news_link($content)))))
  934. )
  935. ).'</div>';
  936. echo '<div id="wikifooter">'.get_lang('Progress').': '.$row['progress'].'%&nbsp;&nbsp;&nbsp;'.get_lang(
  937. 'Rating'
  938. ).': '.$row['score'].'&nbsp;&nbsp;&nbsp;'.get_lang('Words').': '.word_count($content).'</div>';
  939. }
  940. //end filter visibility
  941. } // end function display_wiki_entry
  942. /**
  943. * This function counted the words in a document. Thanks Adeel Khan
  944. * @param string Document's text
  945. * @return int Number of words
  946. */
  947. function word_count($document)
  948. {
  949. $search = array(
  950. '@<script[^>]*?>.*?</script>@si',
  951. '@<style[^>]*?>.*?</style>@siU',
  952. '@<div id="player.[^>]*?>.*?</div>@',
  953. '@<![\s\S]*?--[ \t\n\r]*>@'
  954. );
  955. $document = preg_replace($search, '', $document);
  956. # strip all html tags
  957. $wc = strip_tags($document);
  958. $wc = html_entity_decode($wc, ENT_NOQUOTES, 'UTF-8'); // TODO:test also old html_entity_decode(utf8_encode($wc))
  959. # remove 'words' that don't consist of alphanumerical characters or punctuation. And fix accents and some letters
  960. $pattern = "#[^(\w|\d|\'|\"|\.|\!|\?|;|,|\\|\/|\-|:|\&|@|á|é|í|ó|ú|à|è|ì|ò|ù|ä|ë|ï|ö|ü|Á|É|Í|Ó|Ú|À|È|Ò|Ù|Ä|Ë|Ï|Ö|Ü|â|ê|î|ô|û|Â|Ê|Î|Ô|Û|ñ|Ñ|ç|Ç)]+#";
  961. $wc = trim(preg_replace($pattern, " ", $wc));
  962. # remove one-letter 'words' that consist only of punctuation
  963. $wc = trim(preg_replace("#\s*[(\'|\"|\.|\!|\?|;|,|\\|\/|\-|:|\&|@)]\s*#", " ", $wc));
  964. # remove superfluous whitespace
  965. $wc = preg_replace("/\s\s+/", " ", $wc);
  966. # split string into an array of words
  967. $wc = explode(" ", $wc);
  968. # remove empty elements
  969. $wc = array_filter($wc);
  970. # return the number of words
  971. return count($wc);
  972. }
  973. /**
  974. * This function checks if wiki title exist
  975. */
  976. function wiki_exist($title)
  977. {
  978. global $tbl_wiki;
  979. global $groupfilter;
  980. global $condition_session;
  981. $course_id = api_get_course_int_id();
  982. $sql = 'SELECT id FROM '.$tbl_wiki.'WHERE c_id = '.$course_id.' AND title="'.Database::escape_string(
  983. $title
  984. ).'" AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  985. $result = Database::query($sql);
  986. $cant = Database::num_rows($result);
  987. if ($cant > 0) {
  988. return true;
  989. } else {
  990. return false;
  991. }
  992. }
  993. /**
  994. * This function a wiki warning
  995. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  996. * @return html code
  997. **/
  998. function display_wiki_warning($variable)
  999. {
  1000. echo '<div class="wiki_warning">'.$variable.'</div>';
  1001. }
  1002. /**
  1003. * Checks if this navigation tab has to be set to active
  1004. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  1005. * @return html code
  1006. */
  1007. function is_active_navigation_tab($paramwk)
  1008. {
  1009. if ($_GET['action'] == $paramwk) {
  1010. return ' class="active"';
  1011. }
  1012. }
  1013. /**
  1014. * Lock add pages
  1015. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1016. * return current database status of protect page and change it if get action
  1017. */
  1018. function check_addnewpagelock()
  1019. {
  1020. global $tbl_wiki;
  1021. global $groupfilter;
  1022. global $condition_session;
  1023. $_clean['group_id'] = (int)$_SESSION['_gid'];
  1024. $course_id = api_get_course_int_id();
  1025. $sql = 'SELECT * FROM '.$tbl_wiki.' WHERE c_id = '.$course_id.' AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  1026. $result = Database::query($sql);
  1027. $row = Database::fetch_array($result);
  1028. $status_addlock = $row['addlock'];
  1029. //change status
  1030. if (api_is_allowed_to_edit(false, true) || api_is_platform_admin()) {
  1031. if ($_GET['actionpage'] == 'lockaddnew' && $status_addlock == 1) {
  1032. $status_addlock = 0;
  1033. }
  1034. if ($_GET['actionpage'] == 'unlockaddnew' && $status_addlock == 0) {
  1035. $status_addlock = 1;
  1036. }
  1037. Database::query(
  1038. 'UPDATE '.$tbl_wiki.' SET addlock="'.Database::escape_string(
  1039. $status_addlock
  1040. ).'" WHERE c_id = '.$course_id.' AND '.$groupfilter.$condition_session.''
  1041. );
  1042. $sql = 'SELECT * FROM '.$tbl_wiki.' WHERE c_id = '.$course_id.' AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  1043. $result = Database::query($sql);
  1044. $row = Database::fetch_array($result);
  1045. }
  1046. //show status
  1047. return $row['addlock'];
  1048. }
  1049. /**
  1050. * Protect page
  1051. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1052. * return current database status of protect page and change it if get action
  1053. */
  1054. function check_protect_page()
  1055. {
  1056. global $tbl_wiki;
  1057. global $page;
  1058. global $groupfilter;
  1059. global $condition_session;
  1060. $course_id = api_get_course_int_id();
  1061. $_clean['group_id'] = (int)$_SESSION['_gid'];
  1062. $sql = 'SELECT * FROM '.$tbl_wiki.'WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string(
  1063. $page
  1064. ).'" AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  1065. $result = Database::query($sql);
  1066. $row = Database::fetch_array($result);
  1067. $status_editlock = $row['editlock'];
  1068. $id = $row['id'];
  1069. ///change status
  1070. if (api_is_allowed_to_edit(false, true) || api_is_platform_admin()) {
  1071. if ($_GET['actionpage'] == 'lock' && $status_editlock == 0) {
  1072. $status_editlock = 1;
  1073. }
  1074. if ($_GET['actionpage'] == 'unlock' && $status_editlock == 1) {
  1075. $status_editlock = 0;
  1076. }
  1077. $sql = 'UPDATE '.$tbl_wiki.' SET editlock="'.Database::escape_string(
  1078. $status_editlock
  1079. ).'" WHERE c_id = '.$course_id.' AND id="'.$id.'"';
  1080. Database::query($sql);
  1081. $sql = 'SELECT * FROM '.$tbl_wiki.' WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string(
  1082. $page
  1083. ).'" AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  1084. $result = Database::query($sql);
  1085. $row = Database::fetch_array($result);
  1086. }
  1087. //show status
  1088. return $row['editlock'];
  1089. }
  1090. /**
  1091. * Visibility page
  1092. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1093. * return current database status of visibility and change it if get action
  1094. */
  1095. function check_visibility_page()
  1096. {
  1097. global $tbl_wiki;
  1098. global $page;
  1099. global $groupfilter;
  1100. global $condition_session;
  1101. $_clean['group_id'] = (int)$_SESSION['_gid'];
  1102. $course_id = api_get_course_int_id();
  1103. $sql = 'SELECT * FROM '.$tbl_wiki.' WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string(
  1104. $page
  1105. ).'" AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  1106. $result = Database::query($sql);
  1107. $row = Database::fetch_array($result);
  1108. $status_visibility = $row['visibility'];
  1109. //change status
  1110. if (api_is_allowed_to_edit(false, true) || api_is_platform_admin()) {
  1111. if ($_GET['actionpage'] == 'visible' && $status_visibility == 0) {
  1112. $status_visibility = 1;
  1113. }
  1114. if ($_GET['actionpage'] == 'invisible' && $status_visibility == 1) {
  1115. $status_visibility = 0;
  1116. }
  1117. $sql = 'UPDATE '.$tbl_wiki.' SET visibility="'.Database::escape_string(
  1118. $status_visibility
  1119. ).'" WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string(
  1120. $page
  1121. ).'" AND '.$groupfilter.$condition_session;
  1122. Database::query($sql);
  1123. //Although the value now is assigned to all (not only the first), these three lines remain necessary. They do that by changing the page state is made when you press the button and not have to wait to change his page
  1124. $sql = 'SELECT * FROM '.$tbl_wiki.' WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string(
  1125. $page
  1126. ).'" AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  1127. $result = Database::query($sql);
  1128. $row = Database::fetch_array($result);
  1129. }
  1130. if (empty($row['id'])) {
  1131. $row['visibility'] = 1;
  1132. }
  1133. //show status
  1134. return $row['visibility'];
  1135. }
  1136. /**
  1137. * Visibility discussion
  1138. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1139. * @return int current database status of discuss visibility and change it if get action page
  1140. */
  1141. function check_visibility_discuss()
  1142. {
  1143. global $tbl_wiki;
  1144. global $page;
  1145. global $groupfilter;
  1146. global $condition_session;
  1147. $_clean['group_id'] = (int)$_SESSION['_gid'];
  1148. $course_id = api_get_course_int_id();
  1149. $sql = 'SELECT * FROM '.$tbl_wiki.' WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string(
  1150. $page
  1151. ).'" AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  1152. $result = Database::query($sql);
  1153. $row = Database::fetch_array($result);
  1154. $status_visibility_disc = $row['visibility_disc'];
  1155. //change status
  1156. if (api_is_allowed_to_edit(false, true) || api_is_platform_admin()) {
  1157. if ($_GET['actionpage'] == 'showdisc' && $status_visibility_disc == 0) {
  1158. $status_visibility_disc = 1;
  1159. }
  1160. if ($_GET['actionpage'] == 'hidedisc' && $status_visibility_disc == 1) {
  1161. $status_visibility_disc = 0;
  1162. }
  1163. $sql = 'UPDATE '.$tbl_wiki.' SET visibility_disc="'.Database::escape_string(
  1164. $status_visibility_disc
  1165. ).'" WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string(
  1166. $page
  1167. ).'" AND '.$groupfilter.$condition_session;
  1168. Database::query($sql);
  1169. //Although the value now is assigned to all (not only the first), these three lines remain necessary. They do that by changing the page state is made when you press the button and not have to wait to change his page
  1170. $sql = 'SELECT * FROM '.$tbl_wiki.' WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string(
  1171. $page
  1172. ).'" AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  1173. $result = Database::query($sql);
  1174. $row = Database::fetch_array($result);
  1175. }
  1176. //show status
  1177. return $row['visibility_disc'];
  1178. }
  1179. /**
  1180. * Lock add discussion
  1181. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1182. * @return int current database status of lock dicuss and change if get action
  1183. */
  1184. function check_addlock_discuss()
  1185. {
  1186. global $tbl_wiki;
  1187. global $page;
  1188. global $groupfilter;
  1189. global $condition_session;
  1190. $course_id = api_get_course_int_id();
  1191. $_clean['group_id'] = (int)$_SESSION['_gid'];
  1192. $sql = 'SELECT * FROM '.$tbl_wiki.' WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string(
  1193. $page
  1194. ).'" AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  1195. $result = Database::query($sql);
  1196. $row = Database::fetch_array($result);
  1197. $status_addlock_disc = $row['addlock_disc'];
  1198. //change status
  1199. if (api_is_allowed_to_edit() || api_is_platform_admin()) {
  1200. if ($_GET['actionpage'] == 'lockdisc' && $status_addlock_disc == 0) {
  1201. $status_addlock_disc = 1;
  1202. }
  1203. if ($_GET['actionpage'] == 'unlockdisc' && $status_addlock_disc == 1) {
  1204. $status_addlock_disc = 0;
  1205. }
  1206. $sql = 'UPDATE '.$tbl_wiki.' SET addlock_disc="'.Database::escape_string($status_addlock_disc).'"
  1207. WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string(
  1208. $page
  1209. ).'" AND '.$groupfilter.$condition_session;
  1210. Database::query($sql);
  1211. //Although the value now is assigned to all (not only the first), these three lines remain necessary. They do that by changing the page state is made when you press the button and not have to wait to change his page
  1212. $sql = 'SELECT * FROM '.$tbl_wiki.'WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string(
  1213. $page
  1214. ).'" AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  1215. $result = Database::query($sql);
  1216. $row = Database::fetch_array($result);
  1217. }
  1218. //show status
  1219. return $row['addlock_disc'];
  1220. }
  1221. /**
  1222. * Lock rating discussion
  1223. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1224. * @return int current database status of rating discuss and change it if get action
  1225. */
  1226. function check_ratinglock_discuss()
  1227. {
  1228. global $tbl_wiki;
  1229. global $page;
  1230. global $groupfilter;
  1231. global $condition_session;
  1232. $_clean['group_id'] = (int)$_SESSION['_gid'];
  1233. $course_id = api_get_course_int_id();
  1234. $sql = 'SELECT * FROM '.$tbl_wiki.'
  1235. WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string(
  1236. $page
  1237. ).'" AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  1238. $result = Database::query($sql);
  1239. $row = Database::fetch_array($result);
  1240. $status_ratinglock_disc = $row['ratinglock_disc'];
  1241. //change status
  1242. if (api_is_allowed_to_edit(false, true) || api_is_platform_admin()) {
  1243. if ($_GET['actionpage'] == 'lockrating' && $status_ratinglock_disc == 0) {
  1244. $status_ratinglock_disc = 1;
  1245. }
  1246. if ($_GET['actionpage'] == 'unlockrating' && $status_ratinglock_disc == 1) {
  1247. $status_ratinglock_disc = 0;
  1248. }
  1249. $sql = 'UPDATE '.$tbl_wiki.' SET ratinglock_disc="'.Database::escape_string($status_ratinglock_disc).'"
  1250. WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string(
  1251. $page
  1252. ).'" AND '.$groupfilter.$condition_session; //Visibility. Value to all,not only for the first
  1253. Database::query($sql);
  1254. //Although the value now is assigned to all (not only the first), these three lines remain necessary. They do that by changing the page state is made when you press the button and not have to wait to change his page
  1255. $sql = 'SELECT * FROM '.$tbl_wiki.' WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string(
  1256. $page
  1257. ).'" AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  1258. $result = Database::query($sql);
  1259. $row = Database::fetch_array($result);
  1260. }
  1261. //show status
  1262. return $row['ratinglock_disc'];
  1263. }
  1264. /**
  1265. * Notify page changes
  1266. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1267. * @return int the current notification status
  1268. */
  1269. function check_notify_page($reflink)
  1270. {
  1271. global $tbl_wiki;
  1272. global $groupfilter;
  1273. global $tbl_wiki_mailcue;
  1274. global $condition_session;
  1275. $_clean['group_id'] = (int)$_SESSION['_gid'];
  1276. $session_id = api_get_session_id();
  1277. $course_id = api_get_course_int_id();
  1278. $sql = 'SELECT * FROM '.$tbl_wiki.' WHERE c_id = '.$course_id.' AND reflink="'.$reflink.'" AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  1279. $result = Database::query($sql);
  1280. $row = Database::fetch_array($result);
  1281. $id = $row['id'];
  1282. $sql = 'SELECT * FROM '.$tbl_wiki_mailcue.' WHERE c_id = '.$course_id.' AND id="'.$id.'" AND user_id="'.api_get_user_id(
  1283. ).'" AND type="P"';
  1284. $result = Database::query($sql);
  1285. $row = Database::fetch_array($result);
  1286. $idm = $row['id'];
  1287. if (empty($idm)) {
  1288. $status_notify = 0;
  1289. } else {
  1290. $status_notify = 1;
  1291. }
  1292. //change status
  1293. if ($_GET['actionpage'] == 'locknotify' && $status_notify == 0) {
  1294. $sql = "INSERT INTO ".$tbl_wiki_mailcue." (c_id, id, user_id, type, group_id, session_id) VALUES
  1295. ($course_id, '".$id."','".api_get_user_id()."','P','".$_clean['group_id']."','".$session_id."')";
  1296. Database::query($sql);
  1297. $status_notify = 1;
  1298. }
  1299. if ($_GET['actionpage'] == 'unlocknotify' && $status_notify == 1) {
  1300. $sql = 'DELETE FROM '.$tbl_wiki_mailcue.'
  1301. WHERE id="'.$id.'" AND user_id="'.api_get_user_id(
  1302. ).'" AND type="P" AND c_id = '.$course_id; //$_clean['group_id'] not necessary. CHECK FOR SESSIONS
  1303. Database::query($sql);
  1304. $status_notify = 0;
  1305. }
  1306. //show status
  1307. return $status_notify;
  1308. }
  1309. /**
  1310. * Notify discussion changes
  1311. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1312. * @return int current database status of rating discuss and change it if get action
  1313. */
  1314. function check_notify_discuss($reflink)
  1315. {
  1316. global $tbl_wiki;
  1317. global $groupfilter;
  1318. global $tbl_wiki_mailcue;
  1319. global $condition_session;
  1320. $course_id = api_get_course_int_id();
  1321. $_clean['group_id'] = (int)$_SESSION['_gid'];
  1322. $session_id = api_get_session_id();
  1323. $sql = 'SELECT * FROM '.$tbl_wiki.'WHERE c_id = '.$course_id.' AND reflink="'.$reflink.'" AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  1324. $result = Database::query($sql);
  1325. $row = Database::fetch_array($result);
  1326. $id = $row['id'];
  1327. $sql = 'SELECT * FROM '.$tbl_wiki_mailcue.' WHERE c_id = '.$course_id.' AND id="'.$id.'" AND user_id="'.api_get_user_id(
  1328. ).'" AND type="D"';
  1329. $result = Database::query($sql);
  1330. $row = Database::fetch_array($result);
  1331. $idm = $row['id'];
  1332. if (empty($idm)) {
  1333. $status_notify_disc = 0;
  1334. } else {
  1335. $status_notify_disc = 1;
  1336. }
  1337. //change status
  1338. if ($_GET['actionpage'] == 'locknotifydisc' && $status_notify_disc == 0) {
  1339. $sql = "INSERT INTO ".$tbl_wiki_mailcue." (c_id, id, user_id, type, group_id, session_id) VALUES
  1340. ($course_id, '".$id."','".api_get_user_id()."','D','".$_clean['group_id']."','".$session_id."')";
  1341. Database::query($sql);
  1342. $status_notify_disc = 1;
  1343. }
  1344. if ($_GET['actionpage'] == 'unlocknotifydisc' && $status_notify_disc == 1) {
  1345. $sql = 'DELETE FROM '.$tbl_wiki_mailcue.' WHERE c_id = '.$course_id.' AND id="'.$id.'" AND user_id="'.api_get_user_id(
  1346. ).'" AND type="D" AND c_id = '.$course_id; //$_clean['group_id'] not necessary TODO:CHECK FOR SESSIONS
  1347. Database::query($sql);
  1348. $status_notify_disc = 0;
  1349. }
  1350. //show status
  1351. return $status_notify_disc;
  1352. }
  1353. /**
  1354. * Notify all changes
  1355. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1356. */
  1357. function check_notify_all()
  1358. {
  1359. global $tbl_wiki_mailcue;
  1360. $course_id = api_get_course_int_id();
  1361. $_clean['group_id'] = (int)$_SESSION['_gid'];
  1362. $session_id = api_get_session_id();
  1363. $sql = 'SELECT * FROM '.$tbl_wiki_mailcue.' WHERE c_id = '.$course_id.' AND user_id="'.api_get_user_id(
  1364. ).'" AND type="F" AND group_id="'.$_clean['group_id'].'" AND session_id="'.$session_id.'"';
  1365. $result = Database::query($sql);
  1366. $row = Database::fetch_array($result);
  1367. $idm = $row['user_id'];
  1368. if (empty($idm)) {
  1369. $status_notify_all = 0;
  1370. } else {
  1371. $status_notify_all = 1;
  1372. }
  1373. //change status
  1374. if ($_GET['actionpage'] == 'locknotifyall' && $status_notify_all == 0) {
  1375. $sql = "INSERT INTO ".$tbl_wiki_mailcue." (c_id, user_id, type, group_id, session_id) VALUES
  1376. ($course_id, '".api_get_user_id()."','F','".$_clean['group_id']."','".$session_id."')";
  1377. Database::query($sql);
  1378. $status_notify_all = 1;
  1379. }
  1380. if ($_GET['actionpage'] == 'unlocknotifyall' && $status_notify_all == 1) {
  1381. $sql = 'DELETE FROM '.$tbl_wiki_mailcue.'
  1382. WHERE c_id = '.$course_id.' AND user_id="'.api_get_user_id(
  1383. ).'" AND type="F" AND group_id="'.$_clean['group_id'].'" AND session_id="'.$session_id.'" AND c_id = '.$course_id;
  1384. Database::query($sql);
  1385. $status_notify_all = 0;
  1386. }
  1387. //show status
  1388. return $status_notify_all;
  1389. }
  1390. /**
  1391. * Sends pending e-mails
  1392. */
  1393. function check_emailcue($id_or_ref, $type, $lastime = '', $lastuser = '')
  1394. {
  1395. global $tbl_wiki;
  1396. global $groupfilter;
  1397. global $tbl_wiki_mailcue;
  1398. $_course = api_get_course_info();
  1399. global $condition_session;
  1400. $_clean['group_id'] = (int)$_SESSION['_gid'];
  1401. $session_id = api_get_session_id();
  1402. $course_id = api_get_course_int_id();
  1403. $group_properties = GroupManager :: get_group_properties($_clean['group_id']);
  1404. $group_name = $group_properties['name'];
  1405. $allow_send_mail = false; //define the variable to below
  1406. if ($type == 'P') {
  1407. //if modifying a wiki page
  1408. //first, current author and time
  1409. //Who is the author?
  1410. $userinfo = api_get_user_info($lastuser);
  1411. $email_user_author = get_lang('EditedBy').': '.$userinfo['complete_name'];
  1412. //When ?
  1413. $year = substr($lastime, 0, 4);
  1414. $month = substr($lastime, 5, 2);
  1415. $day = substr($lastime, 8, 2);
  1416. $hours = substr($lastime, 11, 2);
  1417. $minutes = substr($lastime, 14, 2);
  1418. $seconds = substr($lastime, 17, 2);
  1419. $email_date_changes = $day.' '.$month.' '.$year.' '.$hours.":".$minutes.":".$seconds;
  1420. //second, extract data from first reg
  1421. $sql = 'SELECT * FROM '.$tbl_wiki.' WHERE c_id = '.$course_id.' AND reflink="'.$id_or_ref.'" AND '.$groupfilter.$condition_session.' ORDER BY id ASC'; //id_or_ref is reflink from tblwiki
  1422. $result = Database::query($sql);
  1423. $row = Database::fetch_array($result);
  1424. $id = $row['id'];
  1425. $email_page_name = $row['title'];
  1426. if ($row['visibility'] == 1) {
  1427. $allow_send_mail = true; //if visibility off - notify off
  1428. $sql = 'SELECT * FROM '.$tbl_wiki_mailcue.' WHERE c_id = '.$course_id.' AND id="'.$id.'" AND type="'.$type.'" OR type="F" AND group_id="'.$_clean['group_id'].'" AND session_id="'.$session_id.'"'; //type: P=page, D=discuss, F=full.
  1429. $result = Database::query($sql);
  1430. $emailtext = get_lang('EmailWikipageModified').' <strong>'.$email_page_name.'</strong> '.get_lang('Wiki');
  1431. }
  1432. } elseif ($type == 'D') {
  1433. //if added a post to discuss
  1434. //first, current author and time
  1435. //Who is the author of last message?
  1436. $userinfo = api_get_user_info($lastuser);
  1437. $email_user_author = get_lang('AddedBy').': '.$userinfo['complete_name'];
  1438. //When ?
  1439. $year = substr($lastime, 0, 4);
  1440. $month = substr($lastime, 5, 2);
  1441. $day = substr($lastime, 8, 2);
  1442. $hours = substr($lastime, 11, 2);
  1443. $minutes = substr($lastime, 14, 2);
  1444. $seconds = substr($lastime, 17, 2);
  1445. $email_date_changes = $day.' '.$month.' '.$year.' '.$hours.":".$minutes.":".$seconds;
  1446. //second, extract data from first reg
  1447. $id = $id_or_ref; //$id_or_ref is id from tblwiki
  1448. $sql = 'SELECT * FROM '.$tbl_wiki.' WHERE c_id = '.$course_id.' AND id="'.$id.'" ORDER BY id ASC';
  1449. $result = Database::query($sql);
  1450. $row = Database::fetch_array($result);
  1451. $email_page_name = $row['title'];
  1452. if ($row['visibility_disc'] == 1) {
  1453. $allow_send_mail = true; //if visibility off - notify off
  1454. $sql = 'SELECT * FROM '.$tbl_wiki_mailcue.'WHERE c_id = '.$course_id.' AND id="'.$id.'" AND type="'.$type.'" OR type="F" AND group_id="'.$_clean['group_id'].'" AND session_id="'.$session_id.'"'; //type: P=page, D=discuss, F=full
  1455. $result = Database::query($sql);
  1456. $emailtext = get_lang('EmailWikiPageDiscAdded').' <strong>'.$email_page_name.'</strong> '.get_lang('Wiki');
  1457. }
  1458. } elseif ($type == 'A') {
  1459. //for added pages
  1460. $id = 0; //for tbl_wiki_mailcue
  1461. $sql = 'SELECT * FROM '.$tbl_wiki.' WHERE c_id = '.$course_id.' ORDER BY id DESC'; //the added is always the last
  1462. $result = Database::query($sql);
  1463. $row = Database::fetch_array($result);
  1464. $email_page_name = $row['title'];
  1465. //Who is the author?
  1466. $userinfo = api_get_person_name($row['user_id']);
  1467. $email_user_author = get_lang('AddedBy').': '.$userinfo['complete_name'];
  1468. //When ?
  1469. $year = substr($row['dtime'], 0, 4);
  1470. $month = substr($row['dtime'], 5, 2);
  1471. $day = substr($row['dtime'], 8, 2);
  1472. $hours = substr($row['dtime'], 11, 2);
  1473. $minutes = substr($row['dtime'], 14, 2);
  1474. $seconds = substr($row['dtime'], 17, 2);
  1475. $email_date_changes = $day.' '.$month.' '.$year.' '.$hours.":".$minutes.":".$seconds;
  1476. if ($row['assignment'] == 0) {
  1477. $allow_send_mail = true;
  1478. } elseif ($row['assignment'] == 1) {
  1479. $email_assignment = get_lang('AssignmentDescExtra').' ('.get_lang('AssignmentMode').')';
  1480. $allow_send_mail = true;
  1481. } elseif ($row['assignment'] == 2) {
  1482. $allow_send_mail = false; //Mode tasks: avoids notifications to all users about all users
  1483. }
  1484. $sql = 'SELECT * FROM '.$tbl_wiki_mailcue.' WHERE c_id = '.$course_id.' AND id="'.$id.'" AND type="F" AND group_id="'.$_clean['group_id'].'" AND session_id="'.$session_id.'"'; //type: P=page, D=discuss, F=full
  1485. $result = Database::query($sql);
  1486. $emailtext = get_lang('EmailWikiPageAdded').' <strong>'.$email_page_name.'</strong> '.get_lang(
  1487. 'In'
  1488. ).' '.get_lang('Wiki');
  1489. } elseif ($type == 'E') {
  1490. $id = 0;
  1491. $allow_send_mail = true;
  1492. //Who is the author?
  1493. $userinfo = api_get_user_info(api_get_user_id()); //current user
  1494. $email_user_author = get_lang('DeletedBy').': '.$userinfo['complete_name'];
  1495. //When ?
  1496. $today = date('r'); //current time
  1497. $email_date_changes = $today;
  1498. $sql = 'SELECT * FROM '.$tbl_wiki_mailcue.'WHERE c_id = '.$course_id.' AND id="'.$id.'" AND type="F" AND group_id="'.$_clean['group_id'].'" AND session_id="'.$session_id.'"'; //type: P=page, D=discuss, F=wiki
  1499. $result = Database::query($sql);
  1500. $emailtext = get_lang('EmailWikipageDedeleted');
  1501. }
  1502. ///make and send email
  1503. if ($allow_send_mail) {
  1504. while ($row = Database::fetch_array($result)) {
  1505. $userinfo = api_get_user_info($row['user_id']);
  1506. //$row['user_id'] obtained from tbl_wiki_mailcue
  1507. $name_to = api_get_person_name(
  1508. $userinfo['firstname'],
  1509. $userinfo['lastname'],
  1510. null,
  1511. PERSON_NAME_EMAIL_ADDRESS
  1512. );
  1513. $email_to = $userinfo['email'];
  1514. $sender_name = api_get_setting('emailAdministrator');
  1515. $sender_email = api_get_setting('emailAdministrator');
  1516. $email_subject = get_lang('EmailWikiChanges').' - '.$_course['official_code'];
  1517. $email_body = get_lang('DearUser').' '.api_get_person_name(
  1518. $userinfo['firstname'],
  1519. $userinfo['lastname']
  1520. ).',<br /><br />';
  1521. if ($session_id == 0) {
  1522. $email_body .= $emailtext.' <strong>'.$_course['name'].' - '.$group_name.'</strong><br /><br /><br />';
  1523. } else {
  1524. $email_body .= $emailtext.' <strong>'.$_course['name'].' ('.api_get_session_name(
  1525. api_get_session_id()
  1526. ).') - '.$group_name.'</strong><br /><br /><br />';
  1527. }
  1528. $email_body .= $email_user_author.' ('.$email_date_changes.')<br /><br /><br />';
  1529. $email_body .= $email_assignment.'<br /><br /><br />';
  1530. $email_body .= '<font size="-2">'.get_lang('EmailWikiChangesExt_1').': <strong>'.get_lang(
  1531. 'NotifyChanges'
  1532. ).'</strong><br />';
  1533. $email_body .= get_lang('EmailWikiChangesExt_2').': <strong>'.get_lang(
  1534. 'NotNotifyChanges'
  1535. ).'</strong></font><br />';
  1536. @api_mail_html($name_to, $email_to, $email_subject, $email_body, $sender_name, $sender_email);
  1537. }
  1538. }
  1539. }
  1540. /**
  1541. * Function export last wiki page version to document area
  1542. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1543. */
  1544. function export2doc($doc_id)
  1545. {
  1546. $_course = api_get_course_info();
  1547. $groupId = api_get_group_id();
  1548. $session_id = api_get_session_id();
  1549. $data = get_wiki_data($doc_id);
  1550. if (empty($data)) {
  1551. return false;
  1552. }
  1553. $wikiTitle = $data['title'];
  1554. $wikiContents = $data['content'];
  1555. $template =
  1556. '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  1557. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="{LANGUAGE}" lang="{LANGUAGE}">
  1558. <head>
  1559. <title>{TITLE}</title>
  1560. <meta http-equiv="Content-Type" content="text/html; charset={ENCODING}" />
  1561. <style type="text/css" media="screen, projection">
  1562. /*<![CDATA[*/
  1563. {CSS}
  1564. /*]]>*/
  1565. </style>
  1566. {ASCIIMATHML_SCRIPT}</head>
  1567. <body dir="{TEXT_DIRECTION}">
  1568. {CONTENT}
  1569. </body>
  1570. </html>';
  1571. $css_file = api_get_path(TO_SYS, WEB_CSS_PATH).api_get_setting('stylesheets').'/default.css';
  1572. if (file_exists($css_file)) {
  1573. $css = @file_get_contents($css_file);
  1574. } else {
  1575. $css = '';
  1576. }
  1577. // Fixing some bugs in css files.
  1578. $root_rel = api_get_path(REL_PATH);
  1579. $css_path = 'main/css/';
  1580. $theme = api_get_setting('stylesheets').'/';
  1581. $css = str_replace('behavior:url("/main/css/csshover3.htc");', '', $css);
  1582. $css = str_replace('main/', $root_rel.'main/', $css);
  1583. $css = str_replace('images/', $root_rel.$css_path.$theme.'images/', $css);
  1584. $css = str_replace('../../img/', $root_rel.'main/img/', $css);
  1585. $asciimathmal_script = (Text::api_contains_asciimathml($wikiContents) || Text::api_contains_asciisvg($wikiContents))
  1586. ? '<script src="'.api_get_path(TO_REL, SCRIPT_ASCIIMATHML).'" type="text/javascript"></script>'."\n" : '';
  1587. $template = str_replace(
  1588. array(
  1589. '{LANGUAGE}',
  1590. '{ENCODING}',
  1591. '{TEXT_DIRECTION}',
  1592. '{TITLE}',
  1593. '{CSS}',
  1594. '{ASCIIMATHML_SCRIPT}'
  1595. ),
  1596. array(
  1597. api_get_language_isocode(),
  1598. api_get_system_encoding(),
  1599. api_get_text_direction(),
  1600. $wikiTitle,
  1601. $css,
  1602. $asciimathmal_script
  1603. ),
  1604. $template
  1605. );
  1606. if (0 != $groupId) {
  1607. $groupPart = '_group'.$groupId; // and add groupId to put the same document title in different groups
  1608. $group_properties = GroupManager :: get_group_properties($groupId);
  1609. $groupPath = $group_properties['directory'];
  1610. } else {
  1611. $groupPart = '';
  1612. $groupPath = '';
  1613. }
  1614. $exportDir = api_get_path(SYS_COURSE_PATH).api_get_course_path().'/document'.$groupPath;
  1615. $exportFile = api_replace_dangerous_char($wikiTitle, 'strict').$groupPart;
  1616. //$clean_wikiContents = trim(preg_replace("/\[\[|\]\]/", " ", $wikiContents));
  1617. //$array_clean_wikiContents= explode('|', $clean_wikiContents);
  1618. $wikiContents = trim(preg_replace("/\[[\[]?([^\]|]*)[|]?([^|\]]*)\][\]]?/", "$1", $wikiContents));
  1619. //TODO: put link instead of title
  1620. $wikiContents = str_replace('{CONTENT}', $wikiContents, $template);
  1621. // replace relative path by absolute path for courses, so you can see items into this page wiki (images, mp3, etc..) exported in documents
  1622. if (api_strpos($wikiContents, '../../courses/') !== false) {
  1623. $web_course_path = api_get_path(WEB_COURSE_PATH);
  1624. $wikiContents = str_replace('../../courses/', $web_course_path, $wikiContents);
  1625. }
  1626. $doc_id = 0;
  1627. $i = 1;
  1628. while (file_exists(
  1629. $exportDir.'/'.$exportFile.'_'.$i.'.html'
  1630. )) {
  1631. $i++;
  1632. } //only export last version, but in new export new version in document area
  1633. $wikiFileName = $exportFile.'_'.$i.'.html';
  1634. $exportPath = $exportDir.'/'.$wikiFileName;
  1635. file_put_contents($exportPath, $wikiContents);
  1636. $doc_id = FileManager::add_document(
  1637. $_course,
  1638. $groupPath.'/'.$wikiFileName,
  1639. 'file',
  1640. filesize($exportPath),
  1641. $wikiTitle
  1642. );
  1643. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'DocumentAdded', api_get_user_id(), $groupId);
  1644. return $doc_id;
  1645. // TODO: link to go document area
  1646. }
  1647. /**
  1648. * Exports the wiki page to PDF
  1649. */
  1650. function export_to_pdf($id, $course_code)
  1651. {
  1652. $data = get_wiki_data($id);
  1653. $content_pdf = api_html_entity_decode($data['content'], ENT_QUOTES, api_get_system_encoding());
  1654. //clean wiki links
  1655. $content_pdf = trim(preg_replace("/\[[\[]?([^\]|]*)[|]?([^|\]]*)\][\]]?/", "$1", $content_pdf));
  1656. //TODO: It should be better to display the link insted of the tile but it is hard for [[title]] links
  1657. $title_pdf = api_html_entity_decode($data['title'], ENT_QUOTES, api_get_system_encoding());
  1658. $title_pdf = api_utf8_encode($title_pdf, api_get_system_encoding());
  1659. $content_pdf = api_utf8_encode($content_pdf, api_get_system_encoding());
  1660. $html = '
  1661. <!-- defines the headers/footers - this must occur before the headers/footers are set -->
  1662. <!--mpdf
  1663. <pageheader name="odds" content-left="'.$title_pdf.'" header-style-left="color: #880000; font-style: italic;" line="1" />
  1664. <pagefooter name="odds" content-right="{PAGENO}/{nb}" line="1" />
  1665. <!-- set the headers/footers - they will occur from here on in the document -->
  1666. <!--mpdf
  1667. <setpageheader name="odds" page="odd" value="on" show-this-page="1" />
  1668. <setpagefooter name="odds" page="O" value="on" />
  1669. mpdf-->'.$content_pdf;
  1670. $css_file = api_get_path(TO_SYS, WEB_CSS_PATH).api_get_setting('stylesheets').'/print.css';
  1671. if (file_exists($css_file)) {
  1672. $css = @file_get_contents($css_file);
  1673. } else {
  1674. $css = '';
  1675. }
  1676. $pdf = new PDF();
  1677. $pdf->content_to_pdf($html, $css, $title_pdf, $course_code);
  1678. exit;
  1679. }
  1680. /**
  1681. * Function prevent double post (reload or F5)
  1682. *
  1683. */
  1684. function double_post($wpost_id)
  1685. {
  1686. if (isset($_SESSION['wpost_id'])) {
  1687. if ($wpost_id == $_SESSION['wpost_id']) {
  1688. return false;
  1689. } else {
  1690. $_SESSION['wpost_id'] = $wpost_id;
  1691. return true;
  1692. }
  1693. } else {
  1694. $_SESSION['wpost_id'] = $wpost_id;
  1695. return true;
  1696. }
  1697. }
  1698. /**
  1699. * Function wizard individual assignment
  1700. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1701. */
  1702. function auto_add_page_users($assignment_type)
  1703. {
  1704. global $assig_user_id, $session_id; //$assig_user_id is need to identify end reflinks
  1705. $_clean['group_id'] = (int)$_SESSION['_gid'];
  1706. if ($_clean['group_id'] == 0) {
  1707. //extract course members
  1708. if (!empty($session_id)) {
  1709. $a_users_to_add = CourseManager :: get_user_list_from_course_code($_SESSION['_course']['id'], $session_id);
  1710. } else {
  1711. $a_users_to_add = CourseManager :: get_user_list_from_course_code($_SESSION['_course']['id'], 0);
  1712. }
  1713. } else {
  1714. //extract group members
  1715. $subscribed_users = GroupManager :: get_subscribed_users($_clean['group_id']);
  1716. $subscribed_tutors = GroupManager :: get_subscribed_tutors($_clean['group_id']);
  1717. $a_users_to_add_with_duplicates = array_merge($subscribed_users, $subscribed_tutors);
  1718. //remove duplicates
  1719. $a_users_to_add = $a_users_to_add_with_duplicates;
  1720. //array_walk($a_users_to_add, create_function('&$value,$key', '$value = json_encode($value);'));
  1721. $a_users_to_add = array_unique($a_users_to_add);
  1722. //array_walk($a_users_to_add, create_function('&$value,$key', '$value = json_decode($value, true);'));
  1723. }
  1724. //echo print_r($a_users_to_add);
  1725. $all_students_pages = array();
  1726. //data about teacher
  1727. $userinfo = api_get_user_info(api_get_user_id());
  1728. $name = $userinfo['complete_name_login_as'];
  1729. if (api_get_user_id() <> 0) {
  1730. $image_path = UserManager::get_user_picture_path_by_id(api_get_user_id(), 'web', false, true);
  1731. $image_repository = $image_path['dir'];
  1732. $existing_image = $image_path['file'];
  1733. $photo = '<img src="'.$image_repository.$existing_image.'" alt="'.$name.'" width="40" height="50" align="top" title="'.$name.'" />';
  1734. } else {
  1735. $photo = '<img src="'.api_get_path(
  1736. WEB_CODE_PATH
  1737. )."img/unknown.jpg".'" alt="'.$name.'" width="40" height="50" align="top" title="'.$name.'" />';
  1738. }
  1739. //teacher assignment title
  1740. $title_orig = $_POST['title'];
  1741. //teacher assignment reflink
  1742. $link2teacher = $_POST['title'] = $title_orig."_uass".api_get_user_id();
  1743. //first: teacher name, photo, and assignment description (original content)
  1744. // $content_orig_A='<div align="center" style="background-color: #F5F8FB; border:double">'.$photo.'<br />'.api_get_person_name($userinfo['firstname'], $userinfo['lastname']).'<br />('.get_lang('Teacher').')</div><br/><div>';
  1745. $content_orig_A = '<div align="center" style="background-color: #F5F8FB; border:solid; border-color: #E6E6E6"><table border="0"><tr><td style="font-size:24px">'.get_lang(
  1746. 'AssignmentDesc'
  1747. ).'</td></tr><tr><td>'.$photo.'<br />'.Display::tag('span', $name).'</td></tr></table></div>';
  1748. $content_orig_B = '<br/><div align="center" style="font-size:24px">'.get_lang(
  1749. 'AssignmentDescription'
  1750. ).': '.$title_orig.'</div><br/>'.$_POST['content'];
  1751. //Second: student list (names, photo and links to their works).
  1752. //Third: Create Students work pages.
  1753. foreach ($a_users_to_add as $user_id => $o_user_to_add) {
  1754. if ($o_user_to_add['user_id'] != api_get_user_id()) //except that puts the task
  1755. {
  1756. $assig_user_id = $o_user_to_add['user_id']; //identifies each page as created by the student, not by teacher
  1757. $image_path = UserManager::get_user_picture_path_by_id($assig_user_id, 'web', false, true);
  1758. $image_repository = $image_path['dir'];
  1759. $existing_image = $image_path['file'];
  1760. $username = api_htmlentities(sprintf(get_lang('LoginX'), $o_user_to_add['username'], ENT_QUOTES));
  1761. $name = api_get_person_name($o_user_to_add['firstname'], $o_user_to_add['lastname'])." . ".$username;
  1762. $photo = '<img src="'.$image_repository.$existing_image.'" alt="'.$name.'" width="40" height="50" align="bottom" title="'.$name.'" />';
  1763. $is_tutor_of_group = GroupManager :: is_tutor_of_group(
  1764. $assig_user_id,
  1765. $_clean['group_id']
  1766. ); //student is tutor
  1767. $is_tutor_and_member = (GroupManager :: is_tutor_of_group(
  1768. $assig_user_id,
  1769. $_clean['group_id']
  1770. ) && GroupManager :: is_subscribed($assig_user_id, $_clean['group_id'])); //student is tutor and member
  1771. if ($is_tutor_and_member) {
  1772. $status_in_group = get_lang('GroupTutorAndMember');
  1773. } else {
  1774. if ($is_tutor_of_group) {
  1775. $status_in_group = get_lang('GroupTutor');
  1776. } else {
  1777. $status_in_group = " "; //get_lang('GroupStandardMember')
  1778. }
  1779. }
  1780. if ($assignment_type == 1) {
  1781. $_POST['title'] = $title_orig;
  1782. $_POST['comment'] = get_lang('AssignmentFirstComToStudent');
  1783. $_POST['content'] = '<div align="center" style="background-color: #F5F8FB; border:solid; border-color: #E6E6E6"><table border="0"><tr><td style="font-size:24px">'.get_lang(
  1784. 'AssignmentWork'
  1785. ).'</td></tr><tr><td>'.$photo.'<br />'.$name.'</td></tr></table></div>[['.$link2teacher.' | '.get_lang(
  1786. 'AssignmentLinktoTeacherPage'
  1787. ).']] '; //If $content_orig_B is added here, the task written by the professor was copied to the page of each student. TODO: config options
  1788. //AssignmentLinktoTeacherPage
  1789. $all_students_pages[] = '<li>'.
  1790. Display::tag(
  1791. 'span',
  1792. strtoupper($o_user_to_add['lastname']).', '.$o_user_to_add['firstname'],
  1793. array('title' => $username)
  1794. ).
  1795. ' [['.$_POST['title']."_uass".$assig_user_id.' | '.$photo.']] '.$status_in_group.'</li>'; //don't change this line without guaranteeing that users will be ordered by last names in the following format (surname, name)
  1796. //$all_students_pages[] = '<li><table border="0"><tr><td width="200">'.api_get_person_name($o_user_to_add['lastname'], $o_user_to_add['firstname']).'</td><td>[['.$_POST['title']."_uass".$assig_user_id.' | '.$photo.']] '.$status_in_group.'</td></tr></table></li>';
  1797. $_POST['assignment'] = 2;
  1798. }
  1799. save_new_wiki();
  1800. }
  1801. }
  1802. //end foreach for each user
  1803. foreach ($a_users_to_add as $user_id => $o_user_to_add) {
  1804. if ($o_user_to_add['user_id'] == api_get_user_id()) {
  1805. $assig_user_id = $o_user_to_add['user_id'];
  1806. if ($assignment_type == 1) {
  1807. $_POST['title'] = $title_orig;
  1808. $_POST['comment'] = get_lang('AssignmentDesc');
  1809. sort($all_students_pages);
  1810. $_POST['content'] = $content_orig_A.$content_orig_B.'<br/><div align="center" style="font-size:18px; background-color: #F5F8FB; border:solid; border-color:#E6E6E6">'.get_lang(
  1811. 'AssignmentLinkstoStudentsPage'
  1812. ).'</div><br/><div style="background-color: #F5F8FB; border:solid; border-color:#E6E6E6"><ol>'.implode(
  1813. $all_students_pages
  1814. ).'</ol></div><br/>';
  1815. $_POST['assignment'] = 1;
  1816. }
  1817. save_new_wiki();
  1818. }
  1819. } //end foreach to teacher
  1820. }
  1821. /**
  1822. * Displays the results of a wiki search
  1823. * @param string Search term
  1824. * @param int Whether to search the contents (1) or just the titles (0)
  1825. */
  1826. function display_wiki_search_results($search_term, $search_content = 0, $all_vers = 0)
  1827. {
  1828. global $tbl_wiki, $groupfilter, $MonthsLong, $condition_session;
  1829. echo '<legend>'.get_lang('WikiSearchResults').'</legend>';
  1830. $_clean['group_id'] = (int)$_SESSION['_gid'];
  1831. $session_id = api_get_session_id();
  1832. $course_id = api_get_course_int_id();
  1833. //only by professors when page is hidden
  1834. if (api_is_allowed_to_edit(false, true) || api_is_platform_admin()) {
  1835. if ($all_vers == '1') {
  1836. if ($search_content == '1') {
  1837. $sql = "SELECT * FROM ".$tbl_wiki."
  1838. WHERE c_id = $course_id AND title LIKE '%".Database::escape_string(
  1839. $search_term
  1840. )."%' OR content LIKE '%".Database::escape_string(
  1841. $search_term
  1842. )."%' AND ".$groupfilter.$condition_session."";
  1843. //search all pages and all versions
  1844. } else {
  1845. $sql = "SELECT * FROM ".$tbl_wiki."
  1846. WHERE c_id = $course_id AND title LIKE '%".Database::escape_string(
  1847. $search_term
  1848. )."%' AND ".$groupfilter.$condition_session."";
  1849. //search all pages and all versions
  1850. }
  1851. } else {
  1852. if ($search_content == '1') {
  1853. $sql = "SELECT * FROM ".$tbl_wiki." s1
  1854. WHERE s1.c_id = $course_id AND title LIKE '%".Database::escape_string(
  1855. $search_term
  1856. )."%' OR content LIKE '%".Database::escape_string($search_term)."%' AND
  1857. id=(SELECT MAX(s2.id) FROM ".$tbl_wiki." s2 WHERE s2.c_id = $course_id AND s1.reflink = s2.reflink AND ".$groupfilter.$condition_session.")";
  1858. // warning don't use group by reflink because don't return the last version
  1859. } else {
  1860. $sql = " SELECT * FROM ".$tbl_wiki." s1
  1861. WHERE s1.c_id = $course_id AND title LIKE '%".Database::escape_string($search_term)."%' AND
  1862. id=(SELECT MAX(s2.id) FROM ".$tbl_wiki." s2 WHERE s2.c_id = $course_id AND s1.reflink = s2.reflink AND ".$groupfilter.$condition_session.")";
  1863. // warning don't use group by reflink because don't return the last version
  1864. }
  1865. }
  1866. } else {
  1867. if ($all_vers == '1') {
  1868. if ($search_content == '1') {
  1869. $sql = "SELECT * FROM ".$tbl_wiki." WHERE c_id = $course_id AND visibility=1 AND title LIKE '%".Database::escape_string(
  1870. $search_term
  1871. )."%' OR content LIKE '%".Database::escape_string(
  1872. $search_term
  1873. )."%' AND ".$groupfilter.$condition_session.""; //search all pages and all versions
  1874. } else {
  1875. $sql = "SELECT * FROM ".$tbl_wiki." WHERE c_id = $course_id AND visibility=1 AND title LIKE '%".Database::escape_string(
  1876. $search_term
  1877. )."%' AND ".$groupfilter.$condition_session.""; //search all pages and all versions
  1878. }
  1879. } else {
  1880. if ($search_content == '1') {
  1881. $sql = " SELECT * FROM ".$tbl_wiki." s1
  1882. WHERE s1.c_id = $course_id AND visibility=1 AND title LIKE '%".Database::escape_string(
  1883. $search_term
  1884. )."%' OR content LIKE '%".Database::escape_string($search_term)."%' AND
  1885. id=(SELECT MAX(s2.id) FROM ".$tbl_wiki." s2 WHERE s2.c_id = $course_id AND s1.reflink = s2.reflink AND ".$groupfilter.$condition_session.")";
  1886. // warning don't use group by reflink because don't return the last version
  1887. } else {
  1888. $sql = " SELECT * FROM ".$tbl_wiki." s1
  1889. WHERE s1.c_id = $course_id AND visibility=1 AND title LIKE '%".Database::escape_string(
  1890. $search_term
  1891. )."%' AND
  1892. id=(SELECT MAX(s2.id) FROM ".$tbl_wiki." s2 WHERE s2.c_id = $course_id AND s1.reflink = s2.reflink AND ".$groupfilter.$condition_session.")";
  1893. // warning don't use group by reflink because don't return the last version
  1894. }
  1895. }
  1896. }
  1897. $result = Database::query($sql);
  1898. //show table
  1899. if (Database::num_rows($result) > 0) {
  1900. $row = array();
  1901. while ($obj = Database::fetch_object($result)) {
  1902. //get author
  1903. $userinfo = api_get_user_info($obj->user_id);
  1904. //get time
  1905. $year = substr($obj->dtime, 0, 4);
  1906. $month = substr($obj->dtime, 5, 2);
  1907. $day = substr($obj->dtime, 8, 2);
  1908. $hours = substr($obj->dtime, 11, 2);
  1909. $minutes = substr($obj->dtime, 14, 2);
  1910. $seconds = substr($obj->dtime, 17, 2);
  1911. //get type assignment icon
  1912. if ($obj->assignment == 1) {
  1913. $ShowAssignment = Display::return_icon(
  1914. 'wiki_assignment.png',
  1915. get_lang('AssignmentDesc'),
  1916. '',
  1917. ICON_SIZE_SMALL
  1918. );
  1919. } elseif ($obj->assignment == 2) {
  1920. $ShowAssignment = Display::return_icon(
  1921. 'wiki_work.png',
  1922. get_lang('AssignmentWork'),
  1923. '',
  1924. ICON_SIZE_SMALL
  1925. );
  1926. } elseif ($obj->assignment == 0) {
  1927. $ShowAssignment = '<img src="../img/px_transparent.gif" />';
  1928. }
  1929. $row = array();
  1930. $row[] = $ShowAssignment;
  1931. if ($all_vers == '1') {
  1932. $row[] = '<a href="'.api_get_self(
  1933. ).'?cidReq='.$_course['id'].'&action=showpage&title='.api_htmlentities(
  1934. urlencode($obj->reflink)
  1935. ).'&view='.$obj->id.'&session_id='.api_htmlentities(
  1936. urlencode($_GET['$session_id'])
  1937. ).'&group_id='.api_htmlentities(urlencode($_GET['group_id'])).'">'.api_htmlentities($obj->title).'</a>';
  1938. } else {
  1939. $row[] = '<a href="'.api_get_self().'?cidReq='.$_course[id].'&action=showpage&title='.api_htmlentities(
  1940. urlencode($obj->reflink)
  1941. ).'&session_id='.api_htmlentities($_GET['session_id']).'&group_id='.api_htmlentities(
  1942. $_GET['group_id']
  1943. ).'">'.$obj->title.'</a>';
  1944. }
  1945. $row[] = $obj->user_id <> 0 ? '<a href="../user/userInfo.php?uInfo='.$userinfo['user_id'].'">'.api_htmlentities($userinfo['complete_name']).'</a>' : get_lang('Anonymous').' ('.$obj->user_ip.')';
  1946. $row[] = $year.'-'.$month.'-'.$day.' '.$hours.":".$minutes.":".$seconds;
  1947. if ($all_vers == '1') {
  1948. $row[] = $obj->version;
  1949. } else {
  1950. if (api_is_allowed_to_edit(false, true) || api_is_platform_admin()) {
  1951. $showdelete = ' <a href="'.api_get_self(
  1952. ).'?cidReq='.$_course[id].'&action=delete&title='.api_htmlentities(
  1953. urlencode($obj->reflink)
  1954. ).'&group_id='.api_htmlentities($_GET['group_id']).'">'.Display::return_icon(
  1955. 'delete.png',
  1956. get_lang('Delete'),
  1957. '',
  1958. ICON_SIZE_SMALL
  1959. );
  1960. }
  1961. $row[] = '<a href="'.api_get_self().'?cidReq='.$_course[id].'&action=edit&title='.api_htmlentities(
  1962. urlencode($obj->reflink)
  1963. ).'&group_id='.api_htmlentities($_GET['group_id']).'">'.Display::return_icon(
  1964. 'edit.png',
  1965. get_lang('EditPage'),
  1966. '',
  1967. ICON_SIZE_SMALL
  1968. ).'</a> <a href="'.api_get_self().'?cidReq='.$_course[id].'&action=discuss&title='.api_htmlentities(
  1969. urlencode($obj->reflink)
  1970. ).'&session_id='.api_htmlentities($_GET['session_id']).'&group_id='.api_htmlentities(
  1971. $_GET['group_id']
  1972. ).'">'.Display::return_icon(
  1973. 'discuss.png',
  1974. get_lang('Discuss'),
  1975. '',
  1976. ICON_SIZE_SMALL
  1977. ).'</a> <a href="'.api_get_self().'?cidReq='.$_course[id].'&action=history&title='.api_htmlentities(
  1978. urlencode($obj->reflink)
  1979. ).'&session_id='.api_htmlentities($_GET['session_id']).'&group_id='.api_htmlentities(
  1980. $_GET['group_id']
  1981. ).'">'.Display::return_icon(
  1982. 'history.png',
  1983. get_lang('History'),
  1984. '',
  1985. ICON_SIZE_SMALL
  1986. ).'</a> <a href="'.api_get_self().'?cidReq='.$_course[id].'&action=links&title='.api_htmlentities(
  1987. urlencode($obj->reflink)
  1988. ).'&group_id='.api_htmlentities($_GET['group_id']).'">'.Display::return_icon(
  1989. 'what_link_here.png',
  1990. get_lang('LinksPages'),
  1991. '',
  1992. ICON_SIZE_SMALL
  1993. ).'</a>'.$showdelete;
  1994. }
  1995. $rows[] = $row;
  1996. }
  1997. $table = new SortableTableFromArrayConfig($rows, 1, 10, 'SearchPages_table', '', '', 'ASC');
  1998. $table->set_additional_parameters(
  1999. array(
  2000. 'cidReq' => $_GET['cidReq'],
  2001. 'action' => $_GET['action'],
  2002. 'group_id' => Security::remove_XSS($_GET['group_id']),
  2003. 'mode_table' => 'yes2',
  2004. 'search_term' => $search_term,
  2005. 'search_content' => $search_content,
  2006. 'all_vers' => $all_vers
  2007. )
  2008. );
  2009. $table->set_header(0, get_lang('Type'), true, array('style' => 'width:30px;'));
  2010. $table->set_header(1, get_lang('Title'), true);
  2011. if ($all_vers == '1') {
  2012. $table->set_header(2, get_lang('Author'), true);
  2013. $table->set_header(3, get_lang('Date'), true);
  2014. $table->set_header(4, get_lang('Version'), true);
  2015. } else {
  2016. $table->set_header(2, get_lang('Author').' ('.get_lang('LastVersion').')', true);
  2017. $table->set_header(3, get_lang('Date').' ('.get_lang('LastVersion').')', true);
  2018. $table->set_header(4, get_lang('Actions'), false, array('style' => 'width:130px;'));
  2019. }
  2020. $table->display();
  2021. } else {
  2022. echo get_lang('NoSearchResults');
  2023. }
  2024. }
  2025. /**
  2026. * Returns a date picker
  2027. * @todo replace this function with the formvalidator datepicker
  2028. *
  2029. */
  2030. function draw_date_picker($prefix, $default = '')
  2031. {
  2032. if (empty($default)) {
  2033. $default = date('Y-m-d H:i:s');
  2034. }
  2035. $parts = split(' ', $default);
  2036. list($d_year, $d_month, $d_day) = split('-', $parts[0]);
  2037. list($d_hour, $d_minute) = split(':', $parts[1]);
  2038. $month_list = array(
  2039. 1 => get_lang('JanuaryLong'),
  2040. 2 => get_lang('FebruaryLong'),
  2041. 3 => get_lang('MarchLong'),
  2042. 4 => get_lang('AprilLong'),
  2043. 5 => get_lang('MayLong'),
  2044. 6 => get_lang('JuneLong'),
  2045. 7 => get_lang('JulyLong'),
  2046. 8 => get_lang('AugustLong'),
  2047. 9 => get_lang('SeptemberLong'),
  2048. 10 => get_lang('OctoberLong'),
  2049. 11 => get_lang('NovemberLong'),
  2050. 12 => get_lang('DecemberLong')
  2051. );
  2052. $minute = range(10, 59);
  2053. array_unshift($minute, '00', '01', '02', '03', '04', '05', '06', '07', '08', '09');
  2054. $date_form = make_select($prefix.'_day', array_combine(range(1, 31), range(1, 31)), $d_day);
  2055. $date_form .= make_select($prefix.'_month', $month_list, $d_month);
  2056. $date_form .= make_select(
  2057. $prefix.'_year',
  2058. array(
  2059. $d_year - 2 => $d_year - 2,
  2060. $d_year - 1 => $d_year - 1,
  2061. $d_year => $d_year,
  2062. $d_year + 1 => $d_year + 1,
  2063. $d_year + 2 => $d_year + 2
  2064. ),
  2065. $d_year
  2066. ).'&nbsp;&nbsp;&nbsp;&nbsp;';
  2067. $date_form .= make_select($prefix.'_hour', array_combine(range(0, 23), range(0, 23)), $d_hour).' : ';
  2068. $date_form .= make_select($prefix.'_minute', $minute, $d_minute);
  2069. return $date_form;
  2070. }
  2071. /**
  2072. * Draws an HTML form select with the given options
  2073. *
  2074. */
  2075. function make_select($name, $values, $checked = '')
  2076. {
  2077. $output = '<select name="'.$name.'" id="'.$name.'">';
  2078. foreach ($values as $key => $value) {
  2079. $output .= '<option value="'.$key.'" '.(($checked == $key) ? 'selected="selected"' : '').'>'.$value.'</option>';
  2080. }
  2081. $output .= '</select>';
  2082. return $output;
  2083. }
  2084. /**
  2085. * Translates a form date into a more usable format
  2086. *
  2087. */
  2088. function get_date_from_select($prefix)
  2089. {
  2090. return $_POST[$prefix.'_year'].'-'.two_digits($_POST[$prefix.'_month']).'-'.two_digits(
  2091. $_POST[$prefix.'_day']
  2092. ).' '.two_digits($_POST[$prefix.'_hour']).':'.two_digits($_POST[$prefix.'_minute']).':00';
  2093. }
  2094. /**
  2095. * Converts 1-9 to 01-09
  2096. */
  2097. function two_digits($number)
  2098. {
  2099. $number = (int)$number;
  2100. return ($number < 10) ? '0'.$number : $number;
  2101. }
  2102. /**
  2103. * Get wiki information
  2104. * @param int wiki id
  2105. * @return array wiki data
  2106. */
  2107. function get_wiki_data($id)
  2108. {
  2109. global $tbl_wiki;
  2110. $course_id = api_get_course_int_id();
  2111. $id = intval($id);
  2112. $sql = 'SELECT * FROM '.$tbl_wiki.' WHERE c_id = '.$course_id.' AND id = '.$id.' ';
  2113. $result = Database::query($sql);
  2114. $data = array();
  2115. while ($row = Database::fetch_array($result, 'ASSOC')) {
  2116. $data = $row;
  2117. }
  2118. return $data;
  2119. }