wiki.inc.php 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520
  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. global $_course;
  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. global $_course, $page;
  513. ?>
  514. <script type="text/javascript">
  515. function CheckSend() {
  516. if (document.form1.title.value == "") {
  517. alert("<?php echo get_lang('NoWikiPageTitle');?>");
  518. document.form1.title.focus();
  519. return false;
  520. }
  521. return true;
  522. }
  523. function setFocus() {
  524. $("#wiki_title").focus();
  525. }
  526. $(document).ready(function () {
  527. setFocus();
  528. });
  529. </script>
  530. <?php
  531. //form
  532. echo '<form name="form1" method="post" onsubmit="return CheckSend()" action="'.api_get_self().'?'.api_get_cidreq(
  533. ).'&action=showpage&amp;title='.api_htmlentities(
  534. urlencode(strtolower(str_replace(' ', '_', $page)))
  535. ).'&group_id='.api_htmlentities($_GET['group_id']).'">';
  536. echo '<div id="wikititle" style="min-height:30px;">';
  537. echo '<div style="width:70%;float:left;"><span class="form_required">*</span> '.get_lang(
  538. Title
  539. ).': <input type="text" id="wiki_title" name="title" value="'.api_htmlentities($_GET['title']).'" size="40"></div>';
  540. if (api_is_allowed_to_edit(false, true) || api_is_platform_admin()) {
  541. $_clean['group_id'] = (int)$_SESSION['_gid']; // TODO: check if delete ?
  542. echo'<a href="javascript://" onclick="advanced_parameters()" ><span id="plus_minus" style="float:right">&nbsp;'.Display::return_icon(
  543. 'div_show.gif',
  544. get_lang('Show'),
  545. array('style' => 'vertical-align:middle')
  546. ).'&nbsp;'.get_lang('AdvancedParameters').'</span></a>';
  547. echo '<div id="options" style="display:none; margin: 20px;" >';
  548. //task
  549. echo '<div>&nbsp;</div>';
  550. echo '<div style= "border : 1px dotted; padding:4px; margin-top:20px;">';
  551. 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;
  552. '.Display::return_icon('wiki_task.png', get_lang('DefineTask'), '', ICON_SIZE_SMALL).' '.get_lang(
  553. 'DescriptionOfTheTask'
  554. ).'';
  555. echo '&nbsp;&nbsp;&nbsp;<span id="msg_error4" style="display:none;color:red"></span>';
  556. echo '<div id="option4" style="padding:4px; margin:5px; border:1px dotted; display:none;">';
  557. echo '<table border="0" style="font-weight:normal">';
  558. echo '<tr>';
  559. echo '<td>'.get_lang('DescriptionOfTheTask').'</td>';
  560. echo '</tr>';
  561. echo '<tr>';
  562. //echo '<td><textarea name="task" cols="60" rows="4" >'.stripslashes($row['task']).'</textarea></td>'; // TODO: ¿delete?
  563. echo '<td>'.api_disp_html_area(
  564. 'task',
  565. '',
  566. '',
  567. '',
  568. null,
  569. array('ToolbarSet' => 'wiki_task', 'Width' => '580', 'Height' => '200')
  570. ).'</td>';
  571. echo '</tr>';
  572. echo '</table>';
  573. echo '</div>';
  574. //feedback
  575. 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(
  576. 'AddFeedback'
  577. ).'';
  578. echo '&nbsp;&nbsp;&nbsp;<span id="msg_error2" style="display:none;color:red"></span>';
  579. echo '<div id="option2" style="padding:4px; margin:5px; border:1px dotted; display:none;">';
  580. echo '<table border="0" style="font-weight:normal" align="center">';
  581. echo '<tr>';
  582. echo '<td colspan="2">'.get_lang('Feedback1').'</td>';
  583. echo '<td colspan="2">'.get_lang('Feedback2').'</td>';
  584. echo '<td colspan="2">'.get_lang('Feedback3').'</td>';
  585. echo '</tr>';
  586. echo '<tr>';
  587. echo '<td colspan="2"><textarea name="feedback1" cols="21" rows="4"></textarea></td>';
  588. echo '<td colspan="2"><textarea name="feedback2" cols="21" rows="4"></textarea></td>';
  589. echo '<td colspan="2"><textarea name="feedback3" cols="21" rows="4"></textarea></td>';
  590. echo '</tr>';
  591. echo '<tr>';
  592. echo '<td>'.get_lang('FProgress').':</td>';
  593. echo '<td><select name="fprogress1">
  594. <option value="0" selected>0</option>
  595. <option value="10">10</option>
  596. <option value="20">20</option>
  597. <option value="30">30</option>
  598. <option value="40">40</option>
  599. <option value="50">50</option>
  600. <option value="60">60</option>
  601. <option value="70">70</option>
  602. <option value="80">80</option>
  603. <option value="90">90</option>
  604. <option value="100">100</option>
  605. </select> %</td>';
  606. echo '<td>'.get_lang('FProgress').':</td>';
  607. echo '<td><select name="fprogress2">
  608. <option value="0" selected>0</option>
  609. <option value="10">10</option>
  610. <option value="20">20</option>
  611. <option value="30">30</option>
  612. <option value="40">40</option>
  613. <option value="50">50</option>
  614. <option value="60">60</option>
  615. <option value="70">70</option>
  616. <option value="80">80</option>
  617. <option value="90">90</option>
  618. <option value="100">100</option>
  619. </select> %</td>';
  620. echo '<td>'.get_lang('FProgress').':</td>';
  621. echo '<td><select name="fprogress3">
  622. <option value="0" selected>0</option>
  623. <option value="10">10</option>
  624. <option value="20">20</option>
  625. <option value="30">30</option>
  626. <option value="40">40</option>
  627. <option value="50">50</option>
  628. <option value="60">60</option>
  629. <option value="70">70</option>
  630. <option value="80">80</option>
  631. <option value="90">90</option>
  632. <option value="100">100</option>
  633. </select> %</td>';
  634. echo '</tr>';
  635. echo '</table>';
  636. echo '</div>';
  637. //time limit
  638. 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(
  639. 'PutATimeLimit'
  640. ).'';
  641. echo '&nbsp;&nbsp;&nbsp;<span id="msg_error1" style="display:none;color:red"></span>';
  642. echo '<div id="option1" style="padding:4px; margin:5px; border:1px dotted; display:none;">';
  643. echo '<table width="100%" border="0" style="font-weight:normal">';
  644. echo '<tr>';
  645. echo '<td align="right">'.get_lang("StartDate").':</td>';
  646. echo '<td>';
  647. echo draw_date_picker('startdate_assig').' <input type="checkbox" name="initstartdate" value="1"> '.get_lang(
  648. 'Yes'
  649. ).'/'.get_lang('No').'';
  650. echo '</td>';
  651. echo '</tr>';
  652. echo '<tr>';
  653. echo '<td align="right">'.get_lang("EndDate").':</td>';
  654. echo '<td>';
  655. echo draw_date_picker('enddate_assig').' <input type="checkbox" name="initenddate" value="1"> '.get_lang(
  656. 'Yes'
  657. ).'/'.get_lang('No').'';
  658. echo '</td>';
  659. echo '</tr>';
  660. echo '<tr>';
  661. echo '<td align="right">'.get_lang('AllowLaterSends').':</td>';
  662. echo '<td><input type="checkbox" name="delayedsubmit" value="1"></td>';
  663. echo '</tr>';
  664. echo'</table>';
  665. echo '</div>';
  666. //other limit
  667. 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(
  668. 'OtherSettings'
  669. ).'';
  670. echo '&nbsp;&nbsp;&nbsp;<span id="msg_error3" style="display:none;color:red"></span>';
  671. echo '<div id="option3" style="padding:4px; margin:5px; border:1px dotted; display:none;">';
  672. echo '<div style="font-weight:normal"; align="center">'.get_lang(
  673. 'NMaxWords'
  674. ).':&nbsp;<input type="text" name="max_text" size="3">&nbsp;&nbsp;'.get_lang(
  675. 'NMaxVersion'
  676. ).':&nbsp;<input type="text" name="max_version" size="3"></div>';
  677. echo '</div>';
  678. echo '</div>';
  679. //to define as an individual assignment
  680. echo '<div style= "border : 1px dotted; padding:4px; margin-top:20px;"><img src="../img/icons/22/wiki_assignment.png" title="'.get_lang(
  681. 'CreateAssignmentPage'
  682. ).'" alt="'.get_lang('CreateAssignmentPage').'"/>&nbsp;'.get_lang(
  683. 'DefineAssignmentPage'
  684. ).': <input type="checkbox" name="assignment" value="1"></div>'; // 1= teacher 2 =student
  685. //
  686. echo'</div>';
  687. }
  688. echo '</div>';
  689. echo '<div id="wikicontent">';
  690. api_disp_html_area(
  691. 'content',
  692. '',
  693. '',
  694. '',
  695. null,
  696. api_is_allowed_to_edit(null, true)
  697. ? array('ToolbarSet' => 'Wiki', 'Width' => '100%', 'Height' => '400')
  698. : array('ToolbarSet' => 'WikiStudent', 'Width' => '100%', 'Height' => '400', 'UserStatus' => 'student')
  699. );
  700. echo '<br/>';
  701. echo '<br/>';
  702. echo get_lang('Comments').':&nbsp;&nbsp;<input type="text" name="comment" size="40"><br /><br />';
  703. echo get_lang('Progress').':&nbsp;&nbsp;<select name="progress" id="progress">
  704. <option value="0" selected>0</option>
  705. <option value="10">10</option>
  706. <option value="20">20</option>
  707. <option value="30">30</option>
  708. <option value="40">40</option>
  709. <option value="50">50</option>
  710. <option value="60">60</option>
  711. <option value="70">70</option>
  712. <option value="80">80</option>
  713. <option value="90">90</option>
  714. <option value="100">100</option>
  715. </select> %';
  716. echo '<br/><br/>';
  717. echo '<input type="hidden" name="wpost_id" value="'.md5(uniqid(rand(), true)).'">'; //prevent double post
  718. echo '<button class="save" type="submit" name="SaveWikiNew">'.get_lang(
  719. 'Save'
  720. ).'</button>'; //for button icon. Don't change name (see fckeditor/editor/plugins/customizations/fckplugin_compressed.js and fckplugin.js
  721. echo '</div>';
  722. echo '</form>';
  723. }
  724. /**
  725. * This function displays a wiki entry
  726. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  727. * @author Juan Carlos Raña Trabado
  728. * @return html code
  729. **/
  730. function display_wiki_entry($newtitle)
  731. {
  732. global $charset, $tbl_wiki, $tbl_wiki_conf, $groupfilter, $condition_session, $page;
  733. $course_id = api_get_course_int_id();
  734. if ($newtitle) {
  735. $pageMIX = $newtitle; //display the page after it is created
  736. } else {
  737. $pageMIX = $page; //display current page
  738. }
  739. $_clean['group_id'] = (int)$_SESSION['_gid'];
  740. $session_id = api_get_session_id();
  741. if ($_GET['view']) {
  742. $_clean['view'] = (int)Database::escape_string($_GET['view']);
  743. $filter = ' AND w.id="'.$_clean['view'].'"';
  744. }
  745. //first, check page visibility in the first page version
  746. $sql = 'SELECT * FROM '.$tbl_wiki.' WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string(
  747. $pageMIX
  748. ).'" AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  749. $result = Database::query($sql);
  750. $row = Database::fetch_array($result);
  751. $KeyVisibility = $row['visibility'];
  752. // second, show the last version
  753. $sql = 'SELECT * FROM '.$tbl_wiki.' w , '.$tbl_wiki_conf.' wc
  754. WHERE wc.c_id = '.$course_id.' AND
  755. w.c_id = '.$course_id.' AND
  756. wc.page_id = w.page_id AND
  757. w.reflink = "'.Database::escape_string($pageMIX).'" AND
  758. w.session_id = '.$session_id.' AND
  759. w.'.$groupfilter.' '.$filter.'
  760. ORDER BY id DESC';
  761. $result = Database::query($sql);
  762. $row = Database::fetch_array(
  763. $result
  764. ); // we do not need a while loop since we are always displaying the last version
  765. //update visits
  766. if ($row['id']) {
  767. $sql = 'UPDATE '.$tbl_wiki.' SET hits=(hits+1) WHERE c_id = '.$course_id.' AND id='.$row['id'].'';
  768. Database::query($sql);
  769. }
  770. // if both are empty and we are displaying the index page then we display the default text.
  771. if ($row['content'] == '' AND $row['title'] == '' AND $page == 'index') {
  772. if (api_is_allowed_to_edit(false, true) || api_is_platform_admin() || GroupManager :: is_user_in_group(
  773. api_get_user_id(),
  774. $_SESSION['_gid']
  775. )
  776. ) {
  777. //Table structure for better export to pdf
  778. $default_table_for_content_Start = '<table align="center" border="0"><tr><td align="center">';
  779. $default_table_for_content_End = '</td></tr></table>';
  780. $content = $default_table_for_content_Start.sprintf(
  781. get_lang('DefaultContent'),
  782. api_get_path(WEB_IMG_PATH)
  783. ).$default_table_for_content_End;
  784. $title = get_lang('DefaultTitle');
  785. } else {
  786. return Display::display_normal_message(get_lang('WikiStandBy'));
  787. }
  788. } else {
  789. $content = Security::remove_XSS($row['content'], COURSEMANAGERLOWSECURITY);
  790. $title = $row['title'];
  791. }
  792. //assignment mode: identify page type
  793. if ($row['assignment'] == 1) {
  794. $icon_assignment = Display::return_icon(
  795. 'wiki_assignment.png',
  796. get_lang('AssignmentDescExtra'),
  797. '',
  798. ICON_SIZE_SMALL
  799. );
  800. } elseif ($row['assignment'] == 2) {
  801. $icon_assignment = Display::return_icon('wiki_work.png', get_lang('AssignmentWork'), '', ICON_SIZE_SMALL);
  802. }
  803. //task mode
  804. if (!empty($row['task'])) {
  805. $icon_task = Display::return_icon('wiki_task.png', get_lang('StandardTask'), '', ICON_SIZE_SMALL);
  806. }
  807. //Show page. Show page to all users if isn't hide page. Mode assignments: if student is the author, can view
  808. if ($KeyVisibility == "1" || api_is_allowed_to_edit(false, true) || api_is_platform_admin(
  809. ) || ($row['assignment'] == 2 && $KeyVisibility == "0" && (api_get_user_id() == $row['user_id']))
  810. ) {
  811. echo '<div id="wikititle">';
  812. // page action: protecting (locking) the page
  813. if (api_is_allowed_to_edit(false, true) || api_is_platform_admin()) {
  814. if (check_protect_page() == 1) {
  815. $protect_page = Display::return_icon('lock.png', get_lang('PageLockedExtra'), '', ICON_SIZE_SMALL);
  816. $lock_unlock_protect = 'unlock';
  817. } else {
  818. $protect_page = Display::return_icon('unlock.png', get_lang('PageUnlockedExtra'), '', ICON_SIZE_SMALL);
  819. $lock_unlock_protect = 'lock';
  820. }
  821. }
  822. if ($row['id']) {
  823. echo '<span style="float:right;">';
  824. echo '<a href="index.php?action=showpage&amp;actionpage='.$lock_unlock_protect.'&amp;title='.api_htmlentities(
  825. urlencode($page)
  826. ).'">'.$protect_page.'</a>';
  827. echo '</span>';
  828. }
  829. //page action: visibility
  830. if (api_is_allowed_to_edit(false, true) || api_is_platform_admin()) {
  831. if (check_visibility_page() == 1) {
  832. // TODO: FIX This hides the icon eye closed to users of work they can see yours
  833. //if(($row['assignment']==2 && $KeyVisibility=="0" && (api_get_user_id()==$row['user_id']))==false)
  834. //{
  835. //
  836. // }
  837. $visibility_page = Display::return_icon('visible.png', get_lang('ShowPageExtra'), '', ICON_SIZE_SMALL);
  838. $lock_unlock_visibility = 'invisible';
  839. } else {
  840. $visibility_page = Display::return_icon(
  841. 'invisible.png',
  842. get_lang('HidePageExtra'),
  843. '',
  844. ICON_SIZE_SMALL
  845. );
  846. $lock_unlock_visibility = 'visible';
  847. }
  848. }
  849. if ($row['id']) {
  850. echo '<span style="float:right;">';
  851. echo '<a href="index.php?action=showpage&amp;actionpage='.$lock_unlock_visibility.'&amp;title='.api_htmlentities(
  852. urlencode($page)
  853. ).'">'.$visibility_page.'</a>';
  854. echo '</span>';
  855. }
  856. //page action: notification
  857. if (api_is_allowed_to_session_edit()) {
  858. if (check_notify_page($page) == 1) {
  859. $notify_page = Display::return_icon(
  860. 'messagebox_info.png',
  861. get_lang('NotifyByEmail'),
  862. '',
  863. ICON_SIZE_SMALL
  864. );
  865. $lock_unlock_notify_page = 'unlocknotify';
  866. } else {
  867. $notify_page = Display::return_icon('mail.png', get_lang('CancelNotifyByEmail'), '', ICON_SIZE_SMALL);
  868. $lock_unlock_notify_page = 'locknotify';
  869. }
  870. }
  871. echo '<span style="float:right;">';
  872. echo '<a href="index.php?action=showpage&amp;actionpage='.$lock_unlock_notify_page.'&amp;title='.api_htmlentities(
  873. urlencode($page)
  874. ).'">'.$notify_page.'</a>';
  875. echo '</span>';
  876. //ONly available if row['id'] is set
  877. if ($row['id']) {
  878. //page action: export to pdf
  879. echo '<span style="float:right;padding-top:5px;">';
  880. echo '<form name="form_export2PDF" method="post" action="index.php">';
  881. echo '<input type="hidden" name="action" value="export_to_pdf">';
  882. echo '<input type="hidden" name="wiki_id" value="'.$row['id'].'">';
  883. echo '<input type="image" src="../img/icons/22/pdf.png" border ="0" title="'.get_lang(
  884. 'ExportToPDF'
  885. ).'" alt="'.get_lang('ExportToPDF').'" style=" width:22px; border:none; margin-top: -9px">';
  886. echo '</form>';
  887. echo '</span>';
  888. //page action: copy last version to doc area
  889. if (api_is_allowed_to_edit(false, true) || api_is_platform_admin()) {
  890. echo '<span style="float:right;">';
  891. echo '<form name="form_export2DOC" method="post" action="index.php" >';
  892. echo '<input type=hidden name="export2DOC" value="export2doc">';
  893. echo '<input type=hidden name="doc_id" value="'.$row['id'].'">';
  894. echo '<input type="image" src="../img/icons/22/export_to_documents.png" border ="0" title="'.get_lang(
  895. 'ExportToDocArea'
  896. ).'" alt="'.get_lang('ExportToDocArea').'" style=" width:22px; border:none; margin-top: -6px">';
  897. echo '</form>';
  898. echo '</span>';
  899. }
  900. }
  901. //export to print
  902. ?>
  903. <script>
  904. function goprint() {
  905. var a = window.open('', '', 'width=800,height=600');
  906. a.document.open("text/html");
  907. a.document.write(document.getElementById('wikicontent').innerHTML);
  908. a.document.close();
  909. a.print();
  910. }
  911. </script>
  912. <?php
  913. echo '<span style="float:right; cursor: pointer;">';
  914. echo Display::return_icon(
  915. 'printer.png',
  916. get_lang('Print'),
  917. array('onclick' => "javascript: goprint();"),
  918. ICON_SIZE_SMALL
  919. );
  920. echo '</span>';
  921. if (empty($title)) {
  922. $title = get_lang('DefaultTitle');
  923. }
  924. if (wiki_exist($title)) {
  925. echo $icon_assignment.'&nbsp;'.$icon_task.'&nbsp;'.api_htmlentities($title);
  926. } else {
  927. echo api_htmlentities($title);
  928. }
  929. echo '</div>';
  930. echo '<div id="wikicontent">'.make_wiki_link_clickable(
  931. detect_external_link(
  932. detect_anchor_link(detect_mail_link(detect_ftp_link(detect_irc_link(detect_news_link($content)))))
  933. )
  934. ).'</div>';
  935. echo '<div id="wikifooter">'.get_lang('Progress').': '.$row['progress'].'%&nbsp;&nbsp;&nbsp;'.get_lang(
  936. 'Rating'
  937. ).': '.$row['score'].'&nbsp;&nbsp;&nbsp;'.get_lang('Words').': '.word_count($content).'</div>';
  938. }
  939. //end filter visibility
  940. } // end function display_wiki_entry
  941. /**
  942. * This function counted the words in a document. Thanks Adeel Khan
  943. * @param string Document's text
  944. * @return int Number of words
  945. */
  946. function word_count($document)
  947. {
  948. $search = array(
  949. '@<script[^>]*?>.*?</script>@si',
  950. '@<style[^>]*?>.*?</style>@siU',
  951. '@<div id="player.[^>]*?>.*?</div>@',
  952. '@<![\s\S]*?--[ \t\n\r]*>@'
  953. );
  954. $document = preg_replace($search, '', $document);
  955. # strip all html tags
  956. $wc = strip_tags($document);
  957. $wc = html_entity_decode($wc, ENT_NOQUOTES, 'UTF-8'); // TODO:test also old html_entity_decode(utf8_encode($wc))
  958. # remove 'words' that don't consist of alphanumerical characters or punctuation. And fix accents and some letters
  959. $pattern = "#[^(\w|\d|\'|\"|\.|\!|\?|;|,|\\|\/|\-|:|\&|@|á|é|í|ó|ú|à|è|ì|ò|ù|ä|ë|ï|ö|ü|Á|É|Í|Ó|Ú|À|È|Ò|Ù|Ä|Ë|Ï|Ö|Ü|â|ê|î|ô|û|Â|Ê|Î|Ô|Û|ñ|Ñ|ç|Ç)]+#";
  960. $wc = trim(preg_replace($pattern, " ", $wc));
  961. # remove one-letter 'words' that consist only of punctuation
  962. $wc = trim(preg_replace("#\s*[(\'|\"|\.|\!|\?|;|,|\\|\/|\-|:|\&|@)]\s*#", " ", $wc));
  963. # remove superfluous whitespace
  964. $wc = preg_replace("/\s\s+/", " ", $wc);
  965. # split string into an array of words
  966. $wc = explode(" ", $wc);
  967. # remove empty elements
  968. $wc = array_filter($wc);
  969. # return the number of words
  970. return count($wc);
  971. }
  972. /**
  973. * This function checks if wiki title exist
  974. */
  975. function wiki_exist($title)
  976. {
  977. global $tbl_wiki;
  978. global $groupfilter;
  979. global $condition_session;
  980. $course_id = api_get_course_int_id();
  981. $sql = 'SELECT id FROM '.$tbl_wiki.'WHERE c_id = '.$course_id.' AND title="'.Database::escape_string(
  982. $title
  983. ).'" AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  984. $result = Database::query($sql);
  985. $cant = Database::num_rows($result);
  986. if ($cant > 0) {
  987. return true;
  988. } else {
  989. return false;
  990. }
  991. }
  992. /**
  993. * This function a wiki warning
  994. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  995. * @return html code
  996. **/
  997. function display_wiki_warning($variable)
  998. {
  999. echo '<div class="wiki_warning">'.$variable.'</div>';
  1000. }
  1001. /**
  1002. * Checks if this navigation tab has to be set to active
  1003. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University
  1004. * @return html code
  1005. */
  1006. function is_active_navigation_tab($paramwk)
  1007. {
  1008. if ($_GET['action'] == $paramwk) {
  1009. return ' class="active"';
  1010. }
  1011. }
  1012. /**
  1013. * Lock add pages
  1014. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1015. * return current database status of protect page and change it if get action
  1016. */
  1017. function check_addnewpagelock()
  1018. {
  1019. global $tbl_wiki;
  1020. global $groupfilter;
  1021. global $condition_session;
  1022. $_clean['group_id'] = (int)$_SESSION['_gid'];
  1023. $course_id = api_get_course_int_id();
  1024. $sql = 'SELECT * FROM '.$tbl_wiki.' WHERE c_id = '.$course_id.' AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  1025. $result = Database::query($sql);
  1026. $row = Database::fetch_array($result);
  1027. $status_addlock = $row['addlock'];
  1028. //change status
  1029. if (api_is_allowed_to_edit(false, true) || api_is_platform_admin()) {
  1030. if ($_GET['actionpage'] == 'lockaddnew' && $status_addlock == 1) {
  1031. $status_addlock = 0;
  1032. }
  1033. if ($_GET['actionpage'] == 'unlockaddnew' && $status_addlock == 0) {
  1034. $status_addlock = 1;
  1035. }
  1036. Database::query(
  1037. 'UPDATE '.$tbl_wiki.' SET addlock="'.Database::escape_string(
  1038. $status_addlock
  1039. ).'" WHERE c_id = '.$course_id.' AND '.$groupfilter.$condition_session.''
  1040. );
  1041. $sql = 'SELECT * FROM '.$tbl_wiki.' WHERE c_id = '.$course_id.' AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  1042. $result = Database::query($sql);
  1043. $row = Database::fetch_array($result);
  1044. }
  1045. //show status
  1046. return $row['addlock'];
  1047. }
  1048. /**
  1049. * Protect page
  1050. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1051. * return current database status of protect page and change it if get action
  1052. */
  1053. function check_protect_page()
  1054. {
  1055. global $tbl_wiki;
  1056. global $page;
  1057. global $groupfilter;
  1058. global $condition_session;
  1059. $course_id = api_get_course_int_id();
  1060. $_clean['group_id'] = (int)$_SESSION['_gid'];
  1061. $sql = 'SELECT * FROM '.$tbl_wiki.'WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string(
  1062. $page
  1063. ).'" AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  1064. $result = Database::query($sql);
  1065. $row = Database::fetch_array($result);
  1066. $status_editlock = $row['editlock'];
  1067. $id = $row['id'];
  1068. ///change status
  1069. if (api_is_allowed_to_edit(false, true) || api_is_platform_admin()) {
  1070. if ($_GET['actionpage'] == 'lock' && $status_editlock == 0) {
  1071. $status_editlock = 1;
  1072. }
  1073. if ($_GET['actionpage'] == 'unlock' && $status_editlock == 1) {
  1074. $status_editlock = 0;
  1075. }
  1076. $sql = 'UPDATE '.$tbl_wiki.' SET editlock="'.Database::escape_string(
  1077. $status_editlock
  1078. ).'" WHERE c_id = '.$course_id.' AND id="'.$id.'"';
  1079. Database::query($sql);
  1080. $sql = 'SELECT * FROM '.$tbl_wiki.' WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string(
  1081. $page
  1082. ).'" AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  1083. $result = Database::query($sql);
  1084. $row = Database::fetch_array($result);
  1085. }
  1086. //show status
  1087. return $row['editlock'];
  1088. }
  1089. /**
  1090. * Visibility page
  1091. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1092. * return current database status of visibility and change it if get action
  1093. */
  1094. function check_visibility_page()
  1095. {
  1096. global $tbl_wiki;
  1097. global $page;
  1098. global $groupfilter;
  1099. global $condition_session;
  1100. $_clean['group_id'] = (int)$_SESSION['_gid'];
  1101. $course_id = api_get_course_int_id();
  1102. $sql = 'SELECT * FROM '.$tbl_wiki.' WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string(
  1103. $page
  1104. ).'" AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  1105. $result = Database::query($sql);
  1106. $row = Database::fetch_array($result);
  1107. $status_visibility = $row['visibility'];
  1108. //change status
  1109. if (api_is_allowed_to_edit(false, true) || api_is_platform_admin()) {
  1110. if ($_GET['actionpage'] == 'visible' && $status_visibility == 0) {
  1111. $status_visibility = 1;
  1112. }
  1113. if ($_GET['actionpage'] == 'invisible' && $status_visibility == 1) {
  1114. $status_visibility = 0;
  1115. }
  1116. $sql = 'UPDATE '.$tbl_wiki.' SET visibility="'.Database::escape_string(
  1117. $status_visibility
  1118. ).'" WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string(
  1119. $page
  1120. ).'" AND '.$groupfilter.$condition_session;
  1121. Database::query($sql);
  1122. //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
  1123. $sql = 'SELECT * FROM '.$tbl_wiki.' WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string(
  1124. $page
  1125. ).'" AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  1126. $result = Database::query($sql);
  1127. $row = Database::fetch_array($result);
  1128. }
  1129. if (empty($row['id'])) {
  1130. $row['visibility'] = 1;
  1131. }
  1132. //show status
  1133. return $row['visibility'];
  1134. }
  1135. /**
  1136. * Visibility discussion
  1137. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1138. * @return int current database status of discuss visibility and change it if get action page
  1139. */
  1140. function check_visibility_discuss()
  1141. {
  1142. global $tbl_wiki;
  1143. global $page;
  1144. global $groupfilter;
  1145. global $condition_session;
  1146. $_clean['group_id'] = (int)$_SESSION['_gid'];
  1147. $course_id = api_get_course_int_id();
  1148. $sql = 'SELECT * FROM '.$tbl_wiki.' WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string(
  1149. $page
  1150. ).'" AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  1151. $result = Database::query($sql);
  1152. $row = Database::fetch_array($result);
  1153. $status_visibility_disc = $row['visibility_disc'];
  1154. //change status
  1155. if (api_is_allowed_to_edit(false, true) || api_is_platform_admin()) {
  1156. if ($_GET['actionpage'] == 'showdisc' && $status_visibility_disc == 0) {
  1157. $status_visibility_disc = 1;
  1158. }
  1159. if ($_GET['actionpage'] == 'hidedisc' && $status_visibility_disc == 1) {
  1160. $status_visibility_disc = 0;
  1161. }
  1162. $sql = 'UPDATE '.$tbl_wiki.' SET visibility_disc="'.Database::escape_string(
  1163. $status_visibility_disc
  1164. ).'" WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string(
  1165. $page
  1166. ).'" AND '.$groupfilter.$condition_session;
  1167. Database::query($sql);
  1168. //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
  1169. $sql = 'SELECT * FROM '.$tbl_wiki.' WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string(
  1170. $page
  1171. ).'" AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  1172. $result = Database::query($sql);
  1173. $row = Database::fetch_array($result);
  1174. }
  1175. //show status
  1176. return $row['visibility_disc'];
  1177. }
  1178. /**
  1179. * Lock add discussion
  1180. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1181. * @return int current database status of lock dicuss and change if get action
  1182. */
  1183. function check_addlock_discuss()
  1184. {
  1185. global $tbl_wiki;
  1186. global $page;
  1187. global $groupfilter;
  1188. global $condition_session;
  1189. $course_id = api_get_course_int_id();
  1190. $_clean['group_id'] = (int)$_SESSION['_gid'];
  1191. $sql = 'SELECT * FROM '.$tbl_wiki.' WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string(
  1192. $page
  1193. ).'" AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  1194. $result = Database::query($sql);
  1195. $row = Database::fetch_array($result);
  1196. $status_addlock_disc = $row['addlock_disc'];
  1197. //change status
  1198. if (api_is_allowed_to_edit() || api_is_platform_admin()) {
  1199. if ($_GET['actionpage'] == 'lockdisc' && $status_addlock_disc == 0) {
  1200. $status_addlock_disc = 1;
  1201. }
  1202. if ($_GET['actionpage'] == 'unlockdisc' && $status_addlock_disc == 1) {
  1203. $status_addlock_disc = 0;
  1204. }
  1205. $sql = 'UPDATE '.$tbl_wiki.' SET addlock_disc="'.Database::escape_string($status_addlock_disc).'"
  1206. WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string(
  1207. $page
  1208. ).'" AND '.$groupfilter.$condition_session;
  1209. Database::query($sql);
  1210. //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
  1211. $sql = 'SELECT * FROM '.$tbl_wiki.'WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string(
  1212. $page
  1213. ).'" AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  1214. $result = Database::query($sql);
  1215. $row = Database::fetch_array($result);
  1216. }
  1217. //show status
  1218. return $row['addlock_disc'];
  1219. }
  1220. /**
  1221. * Lock rating discussion
  1222. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1223. * @return int current database status of rating discuss and change it if get action
  1224. */
  1225. function check_ratinglock_discuss()
  1226. {
  1227. global $tbl_wiki;
  1228. global $page;
  1229. global $groupfilter;
  1230. global $condition_session;
  1231. $_clean['group_id'] = (int)$_SESSION['_gid'];
  1232. $course_id = api_get_course_int_id();
  1233. $sql = 'SELECT * FROM '.$tbl_wiki.'
  1234. WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string(
  1235. $page
  1236. ).'" AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  1237. $result = Database::query($sql);
  1238. $row = Database::fetch_array($result);
  1239. $status_ratinglock_disc = $row['ratinglock_disc'];
  1240. //change status
  1241. if (api_is_allowed_to_edit(false, true) || api_is_platform_admin()) {
  1242. if ($_GET['actionpage'] == 'lockrating' && $status_ratinglock_disc == 0) {
  1243. $status_ratinglock_disc = 1;
  1244. }
  1245. if ($_GET['actionpage'] == 'unlockrating' && $status_ratinglock_disc == 1) {
  1246. $status_ratinglock_disc = 0;
  1247. }
  1248. $sql = 'UPDATE '.$tbl_wiki.' SET ratinglock_disc="'.Database::escape_string($status_ratinglock_disc).'"
  1249. WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string(
  1250. $page
  1251. ).'" AND '.$groupfilter.$condition_session; //Visibility. Value to all,not only for the first
  1252. Database::query($sql);
  1253. //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
  1254. $sql = 'SELECT * FROM '.$tbl_wiki.' WHERE c_id = '.$course_id.' AND reflink="'.Database::escape_string(
  1255. $page
  1256. ).'" AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  1257. $result = Database::query($sql);
  1258. $row = Database::fetch_array($result);
  1259. }
  1260. //show status
  1261. return $row['ratinglock_disc'];
  1262. }
  1263. /**
  1264. * Notify page changes
  1265. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1266. * @return int the current notification status
  1267. */
  1268. function check_notify_page($reflink)
  1269. {
  1270. global $tbl_wiki;
  1271. global $groupfilter;
  1272. global $tbl_wiki_mailcue;
  1273. global $condition_session;
  1274. $_clean['group_id'] = (int)$_SESSION['_gid'];
  1275. $session_id = api_get_session_id();
  1276. $course_id = api_get_course_int_id();
  1277. $sql = 'SELECT * FROM '.$tbl_wiki.' WHERE c_id = '.$course_id.' AND reflink="'.$reflink.'" AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  1278. $result = Database::query($sql);
  1279. $row = Database::fetch_array($result);
  1280. $id = $row['id'];
  1281. $sql = 'SELECT * FROM '.$tbl_wiki_mailcue.' WHERE c_id = '.$course_id.' AND id="'.$id.'" AND user_id="'.api_get_user_id(
  1282. ).'" AND type="P"';
  1283. $result = Database::query($sql);
  1284. $row = Database::fetch_array($result);
  1285. $idm = $row['id'];
  1286. if (empty($idm)) {
  1287. $status_notify = 0;
  1288. } else {
  1289. $status_notify = 1;
  1290. }
  1291. //change status
  1292. if ($_GET['actionpage'] == 'locknotify' && $status_notify == 0) {
  1293. $sql = "INSERT INTO ".$tbl_wiki_mailcue." (c_id, id, user_id, type, group_id, session_id) VALUES
  1294. ($course_id, '".$id."','".api_get_user_id()."','P','".$_clean['group_id']."','".$session_id."')";
  1295. Database::query($sql);
  1296. $status_notify = 1;
  1297. }
  1298. if ($_GET['actionpage'] == 'unlocknotify' && $status_notify == 1) {
  1299. $sql = 'DELETE FROM '.$tbl_wiki_mailcue.'
  1300. WHERE id="'.$id.'" AND user_id="'.api_get_user_id(
  1301. ).'" AND type="P" AND c_id = '.$course_id; //$_clean['group_id'] not necessary. CHECK FOR SESSIONS
  1302. Database::query($sql);
  1303. $status_notify = 0;
  1304. }
  1305. //show status
  1306. return $status_notify;
  1307. }
  1308. /**
  1309. * Notify discussion changes
  1310. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1311. * @return int current database status of rating discuss and change it if get action
  1312. */
  1313. function check_notify_discuss($reflink)
  1314. {
  1315. global $tbl_wiki;
  1316. global $groupfilter;
  1317. global $tbl_wiki_mailcue;
  1318. global $condition_session;
  1319. $course_id = api_get_course_int_id();
  1320. $_clean['group_id'] = (int)$_SESSION['_gid'];
  1321. $session_id = api_get_session_id();
  1322. $sql = 'SELECT * FROM '.$tbl_wiki.'WHERE c_id = '.$course_id.' AND reflink="'.$reflink.'" AND '.$groupfilter.$condition_session.' ORDER BY id ASC';
  1323. $result = Database::query($sql);
  1324. $row = Database::fetch_array($result);
  1325. $id = $row['id'];
  1326. $sql = 'SELECT * FROM '.$tbl_wiki_mailcue.' WHERE c_id = '.$course_id.' AND id="'.$id.'" AND user_id="'.api_get_user_id(
  1327. ).'" AND type="D"';
  1328. $result = Database::query($sql);
  1329. $row = Database::fetch_array($result);
  1330. $idm = $row['id'];
  1331. if (empty($idm)) {
  1332. $status_notify_disc = 0;
  1333. } else {
  1334. $status_notify_disc = 1;
  1335. }
  1336. //change status
  1337. if ($_GET['actionpage'] == 'locknotifydisc' && $status_notify_disc == 0) {
  1338. $sql = "INSERT INTO ".$tbl_wiki_mailcue." (c_id, id, user_id, type, group_id, session_id) VALUES
  1339. ($course_id, '".$id."','".api_get_user_id()."','D','".$_clean['group_id']."','".$session_id."')";
  1340. Database::query($sql);
  1341. $status_notify_disc = 1;
  1342. }
  1343. if ($_GET['actionpage'] == 'unlocknotifydisc' && $status_notify_disc == 1) {
  1344. $sql = 'DELETE FROM '.$tbl_wiki_mailcue.' WHERE c_id = '.$course_id.' AND id="'.$id.'" AND user_id="'.api_get_user_id(
  1345. ).'" AND type="D" AND c_id = '.$course_id; //$_clean['group_id'] not necessary TODO:CHECK FOR SESSIONS
  1346. Database::query($sql);
  1347. $status_notify_disc = 0;
  1348. }
  1349. //show status
  1350. return $status_notify_disc;
  1351. }
  1352. /**
  1353. * Notify all changes
  1354. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1355. */
  1356. function check_notify_all()
  1357. {
  1358. global $tbl_wiki_mailcue;
  1359. $course_id = api_get_course_int_id();
  1360. $_clean['group_id'] = (int)$_SESSION['_gid'];
  1361. $session_id = api_get_session_id();
  1362. $sql = 'SELECT * FROM '.$tbl_wiki_mailcue.' WHERE c_id = '.$course_id.' AND user_id="'.api_get_user_id(
  1363. ).'" AND type="F" AND group_id="'.$_clean['group_id'].'" AND session_id="'.$session_id.'"';
  1364. $result = Database::query($sql);
  1365. $row = Database::fetch_array($result);
  1366. $idm = $row['user_id'];
  1367. if (empty($idm)) {
  1368. $status_notify_all = 0;
  1369. } else {
  1370. $status_notify_all = 1;
  1371. }
  1372. //change status
  1373. if ($_GET['actionpage'] == 'locknotifyall' && $status_notify_all == 0) {
  1374. $sql = "INSERT INTO ".$tbl_wiki_mailcue." (c_id, user_id, type, group_id, session_id) VALUES
  1375. ($course_id, '".api_get_user_id()."','F','".$_clean['group_id']."','".$session_id."')";
  1376. Database::query($sql);
  1377. $status_notify_all = 1;
  1378. }
  1379. if ($_GET['actionpage'] == 'unlocknotifyall' && $status_notify_all == 1) {
  1380. $sql = 'DELETE FROM '.$tbl_wiki_mailcue.'
  1381. WHERE c_id = '.$course_id.' AND user_id="'.api_get_user_id(
  1382. ).'" AND type="F" AND group_id="'.$_clean['group_id'].'" AND session_id="'.$session_id.'" AND c_id = '.$course_id;
  1383. Database::query($sql);
  1384. $status_notify_all = 0;
  1385. }
  1386. //show status
  1387. return $status_notify_all;
  1388. }
  1389. /**
  1390. * Sends pending e-mails
  1391. */
  1392. function check_emailcue($id_or_ref, $type, $lastime = '', $lastuser = '')
  1393. {
  1394. global $tbl_wiki;
  1395. global $groupfilter;
  1396. global $tbl_wiki_mailcue;
  1397. global $_course;
  1398. global $condition_session;
  1399. $_clean['group_id'] = (int)$_SESSION['_gid'];
  1400. $session_id = api_get_session_id();
  1401. $course_id = api_get_course_int_id();
  1402. $group_properties = GroupManager :: get_group_properties($_clean['group_id']);
  1403. $group_name = $group_properties['name'];
  1404. $allow_send_mail = false; //define the variable to below
  1405. if ($type == 'P') {
  1406. //if modifying a wiki page
  1407. //first, current author and time
  1408. //Who is the author?
  1409. $userinfo = api_get_user_info($lastuser);
  1410. $email_user_author = get_lang('EditedBy').': '.$userinfo['complete_name'];
  1411. //When ?
  1412. $year = substr($lastime, 0, 4);
  1413. $month = substr($lastime, 5, 2);
  1414. $day = substr($lastime, 8, 2);
  1415. $hours = substr($lastime, 11, 2);
  1416. $minutes = substr($lastime, 14, 2);
  1417. $seconds = substr($lastime, 17, 2);
  1418. $email_date_changes = $day.' '.$month.' '.$year.' '.$hours.":".$minutes.":".$seconds;
  1419. //second, extract data from first reg
  1420. $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
  1421. $result = Database::query($sql);
  1422. $row = Database::fetch_array($result);
  1423. $id = $row['id'];
  1424. $email_page_name = $row['title'];
  1425. if ($row['visibility'] == 1) {
  1426. $allow_send_mail = true; //if visibility off - notify off
  1427. $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.
  1428. $result = Database::query($sql);
  1429. $emailtext = get_lang('EmailWikipageModified').' <strong>'.$email_page_name.'</strong> '.get_lang('Wiki');
  1430. }
  1431. } elseif ($type == 'D') {
  1432. //if added a post to discuss
  1433. //first, current author and time
  1434. //Who is the author of last message?
  1435. $userinfo = api_get_user_info($lastuser);
  1436. $email_user_author = get_lang('AddedBy').': '.$userinfo['complete_name'];
  1437. //When ?
  1438. $year = substr($lastime, 0, 4);
  1439. $month = substr($lastime, 5, 2);
  1440. $day = substr($lastime, 8, 2);
  1441. $hours = substr($lastime, 11, 2);
  1442. $minutes = substr($lastime, 14, 2);
  1443. $seconds = substr($lastime, 17, 2);
  1444. $email_date_changes = $day.' '.$month.' '.$year.' '.$hours.":".$minutes.":".$seconds;
  1445. //second, extract data from first reg
  1446. $id = $id_or_ref; //$id_or_ref is id from tblwiki
  1447. $sql = 'SELECT * FROM '.$tbl_wiki.' WHERE c_id = '.$course_id.' AND id="'.$id.'" ORDER BY id ASC';
  1448. $result = Database::query($sql);
  1449. $row = Database::fetch_array($result);
  1450. $email_page_name = $row['title'];
  1451. if ($row['visibility_disc'] == 1) {
  1452. $allow_send_mail = true; //if visibility off - notify off
  1453. $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
  1454. $result = Database::query($sql);
  1455. $emailtext = get_lang('EmailWikiPageDiscAdded').' <strong>'.$email_page_name.'</strong> '.get_lang('Wiki');
  1456. }
  1457. } elseif ($type == 'A') {
  1458. //for added pages
  1459. $id = 0; //for tbl_wiki_mailcue
  1460. $sql = 'SELECT * FROM '.$tbl_wiki.' WHERE c_id = '.$course_id.' ORDER BY id DESC'; //the added is always the last
  1461. $result = Database::query($sql);
  1462. $row = Database::fetch_array($result);
  1463. $email_page_name = $row['title'];
  1464. //Who is the author?
  1465. $userinfo = api_get_person_name($row['user_id']);
  1466. $email_user_author = get_lang('AddedBy').': '.$userinfo['complete_name'];
  1467. //When ?
  1468. $year = substr($row['dtime'], 0, 4);
  1469. $month = substr($row['dtime'], 5, 2);
  1470. $day = substr($row['dtime'], 8, 2);
  1471. $hours = substr($row['dtime'], 11, 2);
  1472. $minutes = substr($row['dtime'], 14, 2);
  1473. $seconds = substr($row['dtime'], 17, 2);
  1474. $email_date_changes = $day.' '.$month.' '.$year.' '.$hours.":".$minutes.":".$seconds;
  1475. if ($row['assignment'] == 0) {
  1476. $allow_send_mail = true;
  1477. } elseif ($row['assignment'] == 1) {
  1478. $email_assignment = get_lang('AssignmentDescExtra').' ('.get_lang('AssignmentMode').')';
  1479. $allow_send_mail = true;
  1480. } elseif ($row['assignment'] == 2) {
  1481. $allow_send_mail = false; //Mode tasks: avoids notifications to all users about all users
  1482. }
  1483. $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
  1484. $result = Database::query($sql);
  1485. $emailtext = get_lang('EmailWikiPageAdded').' <strong>'.$email_page_name.'</strong> '.get_lang(
  1486. 'In'
  1487. ).' '.get_lang('Wiki');
  1488. } elseif ($type == 'E') {
  1489. $id = 0;
  1490. $allow_send_mail = true;
  1491. //Who is the author?
  1492. $userinfo = api_get_user_info(api_get_user_id()); //current user
  1493. $email_user_author = get_lang('DeletedBy').': '.$userinfo['complete_name'];
  1494. //When ?
  1495. $today = date('r'); //current time
  1496. $email_date_changes = $today;
  1497. $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
  1498. $result = Database::query($sql);
  1499. $emailtext = get_lang('EmailWikipageDedeleted');
  1500. }
  1501. ///make and send email
  1502. if ($allow_send_mail) {
  1503. while ($row = Database::fetch_array($result)) {
  1504. $userinfo = api_get_user_info($row['user_id']);
  1505. //$row['user_id'] obtained from tbl_wiki_mailcue
  1506. $name_to = api_get_person_name(
  1507. $userinfo['firstname'],
  1508. $userinfo['lastname'],
  1509. null,
  1510. PERSON_NAME_EMAIL_ADDRESS
  1511. );
  1512. $email_to = $userinfo['email'];
  1513. $sender_name = api_get_setting('emailAdministrator');
  1514. $sender_email = api_get_setting('emailAdministrator');
  1515. $email_subject = get_lang('EmailWikiChanges').' - '.$_course['official_code'];
  1516. $email_body = get_lang('DearUser').' '.api_get_person_name(
  1517. $userinfo['firstname'],
  1518. $userinfo['lastname']
  1519. ).',<br /><br />';
  1520. if ($session_id == 0) {
  1521. $email_body .= $emailtext.' <strong>'.$_course['name'].' - '.$group_name.'</strong><br /><br /><br />';
  1522. } else {
  1523. $email_body .= $emailtext.' <strong>'.$_course['name'].' ('.api_get_session_name(
  1524. api_get_session_id()
  1525. ).') - '.$group_name.'</strong><br /><br /><br />';
  1526. }
  1527. $email_body .= $email_user_author.' ('.$email_date_changes.')<br /><br /><br />';
  1528. $email_body .= $email_assignment.'<br /><br /><br />';
  1529. $email_body .= '<font size="-2">'.get_lang('EmailWikiChangesExt_1').': <strong>'.get_lang(
  1530. 'NotifyChanges'
  1531. ).'</strong><br />';
  1532. $email_body .= get_lang('EmailWikiChangesExt_2').': <strong>'.get_lang(
  1533. 'NotNotifyChanges'
  1534. ).'</strong></font><br />';
  1535. @api_mail_html($name_to, $email_to, $email_subject, $email_body, $sender_name, $sender_email);
  1536. }
  1537. }
  1538. }
  1539. /**
  1540. * Function export last wiki page version to document area
  1541. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1542. */
  1543. function export2doc($doc_id)
  1544. {
  1545. global $_course;
  1546. $groupId = api_get_group_id();
  1547. $session_id = api_get_session_id();
  1548. $data = get_wiki_data($doc_id);
  1549. if (empty($data)) {
  1550. return false;
  1551. }
  1552. $wikiTitle = $data['title'];
  1553. $wikiContents = $data['content'];
  1554. $template =
  1555. '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  1556. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="{LANGUAGE}" lang="{LANGUAGE}">
  1557. <head>
  1558. <title>{TITLE}</title>
  1559. <meta http-equiv="Content-Type" content="text/html; charset={ENCODING}" />
  1560. <style type="text/css" media="screen, projection">
  1561. /*<![CDATA[*/
  1562. {CSS}
  1563. /*]]>*/
  1564. </style>
  1565. {ASCIIMATHML_SCRIPT}</head>
  1566. <body dir="{TEXT_DIRECTION}">
  1567. {CONTENT}
  1568. </body>
  1569. </html>';
  1570. $css_file = api_get_path(TO_SYS, WEB_CSS_PATH).api_get_setting('stylesheets').'/default.css';
  1571. if (file_exists($css_file)) {
  1572. $css = @file_get_contents($css_file);
  1573. } else {
  1574. $css = '';
  1575. }
  1576. // Fixing some bugs in css files.
  1577. $root_rel = api_get_path(REL_PATH);
  1578. $css_path = 'main/css/';
  1579. $theme = api_get_setting('stylesheets').'/';
  1580. $css = str_replace('behavior:url("/main/css/csshover3.htc");', '', $css);
  1581. $css = str_replace('main/', $root_rel.'main/', $css);
  1582. $css = str_replace('images/', $root_rel.$css_path.$theme.'images/', $css);
  1583. $css = str_replace('../../img/', $root_rel.'main/img/', $css);
  1584. $asciimathmal_script = (Text::api_contains_asciimathml($wikiContents) || Text::api_contains_asciisvg($wikiContents))
  1585. ? '<script src="'.api_get_path(TO_REL, SCRIPT_ASCIIMATHML).'" type="text/javascript"></script>'."\n" : '';
  1586. $template = str_replace(
  1587. array(
  1588. '{LANGUAGE}',
  1589. '{ENCODING}',
  1590. '{TEXT_DIRECTION}',
  1591. '{TITLE}',
  1592. '{CSS}',
  1593. '{ASCIIMATHML_SCRIPT}'
  1594. ),
  1595. array(
  1596. api_get_language_isocode(),
  1597. api_get_system_encoding(),
  1598. api_get_text_direction(),
  1599. $wikiTitle,
  1600. $css,
  1601. $asciimathmal_script
  1602. ),
  1603. $template
  1604. );
  1605. if (0 != $groupId) {
  1606. $groupPart = '_group'.$groupId; // and add groupId to put the same document title in different groups
  1607. $group_properties = GroupManager :: get_group_properties($groupId);
  1608. $groupPath = $group_properties['directory'];
  1609. } else {
  1610. $groupPart = '';
  1611. $groupPath = '';
  1612. }
  1613. $exportDir = api_get_path(SYS_COURSE_PATH).api_get_course_path().'/document'.$groupPath;
  1614. $exportFile = replace_dangerous_char($wikiTitle, 'strict').$groupPart;
  1615. //$clean_wikiContents = trim(preg_replace("/\[\[|\]\]/", " ", $wikiContents));
  1616. //$array_clean_wikiContents= explode('|', $clean_wikiContents);
  1617. $wikiContents = trim(preg_replace("/\[[\[]?([^\]|]*)[|]?([^|\]]*)\][\]]?/", "$1", $wikiContents));
  1618. //TODO: put link instead of title
  1619. $wikiContents = str_replace('{CONTENT}', $wikiContents, $template);
  1620. // replace relative path by absolute path for courses, so you can see items into this page wiki (images, mp3, etc..) exported in documents
  1621. if (api_strpos($wikiContents, '../../courses/') !== false) {
  1622. $web_course_path = api_get_path(WEB_COURSE_PATH);
  1623. $wikiContents = str_replace('../../courses/', $web_course_path, $wikiContents);
  1624. }
  1625. $doc_id = 0;
  1626. $i = 1;
  1627. while (file_exists(
  1628. $exportDir.'/'.$exportFile.'_'.$i.'.html'
  1629. )) {
  1630. $i++;
  1631. } //only export last version, but in new export new version in document area
  1632. $wikiFileName = $exportFile.'_'.$i.'.html';
  1633. $exportPath = $exportDir.'/'.$wikiFileName;
  1634. file_put_contents($exportPath, $wikiContents);
  1635. $doc_id = FileManager::add_document(
  1636. $_course,
  1637. $groupPath.'/'.$wikiFileName,
  1638. 'file',
  1639. filesize($exportPath),
  1640. $wikiTitle
  1641. );
  1642. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'DocumentAdded', api_get_user_id(), $groupId);
  1643. return $doc_id;
  1644. // TODO: link to go document area
  1645. }
  1646. /**
  1647. * Exports the wiki page to PDF
  1648. */
  1649. function export_to_pdf($id, $course_code)
  1650. {
  1651. $data = get_wiki_data($id);
  1652. $content_pdf = api_html_entity_decode($data['content'], ENT_QUOTES, api_get_system_encoding());
  1653. //clean wiki links
  1654. $content_pdf = trim(preg_replace("/\[[\[]?([^\]|]*)[|]?([^|\]]*)\][\]]?/", "$1", $content_pdf));
  1655. //TODO: It should be better to display the link insted of the tile but it is hard for [[title]] links
  1656. $title_pdf = api_html_entity_decode($data['title'], ENT_QUOTES, api_get_system_encoding());
  1657. $title_pdf = api_utf8_encode($title_pdf, api_get_system_encoding());
  1658. $content_pdf = api_utf8_encode($content_pdf, api_get_system_encoding());
  1659. $html = '
  1660. <!-- defines the headers/footers - this must occur before the headers/footers are set -->
  1661. <!--mpdf
  1662. <pageheader name="odds" content-left="'.$title_pdf.'" header-style-left="color: #880000; font-style: italic;" line="1" />
  1663. <pagefooter name="odds" content-right="{PAGENO}/{nb}" line="1" />
  1664. <!-- set the headers/footers - they will occur from here on in the document -->
  1665. <!--mpdf
  1666. <setpageheader name="odds" page="odd" value="on" show-this-page="1" />
  1667. <setpagefooter name="odds" page="O" value="on" />
  1668. mpdf-->'.$content_pdf;
  1669. $css_file = api_get_path(TO_SYS, WEB_CSS_PATH).api_get_setting('stylesheets').'/print.css';
  1670. if (file_exists($css_file)) {
  1671. $css = @file_get_contents($css_file);
  1672. } else {
  1673. $css = '';
  1674. }
  1675. $pdf = new PDF();
  1676. $pdf->content_to_pdf($html, $css, $title_pdf, $course_code);
  1677. exit;
  1678. }
  1679. /**
  1680. * Function prevent double post (reload or F5)
  1681. *
  1682. */
  1683. function double_post($wpost_id)
  1684. {
  1685. if (isset($_SESSION['wpost_id'])) {
  1686. if ($wpost_id == $_SESSION['wpost_id']) {
  1687. return false;
  1688. } else {
  1689. $_SESSION['wpost_id'] = $wpost_id;
  1690. return true;
  1691. }
  1692. } else {
  1693. $_SESSION['wpost_id'] = $wpost_id;
  1694. return true;
  1695. }
  1696. }
  1697. /**
  1698. * Function wizard individual assignment
  1699. * @author Juan Carlos Raña <herodoto@telefonica.net>
  1700. */
  1701. function auto_add_page_users($assignment_type)
  1702. {
  1703. global $assig_user_id, $session_id; //$assig_user_id is need to identify end reflinks
  1704. $_clean['group_id'] = (int)$_SESSION['_gid'];
  1705. if ($_clean['group_id'] == 0) {
  1706. //extract course members
  1707. if (!empty($session_id)) {
  1708. $a_users_to_add = CourseManager :: get_user_list_from_course_code($_SESSION['_course']['id'], $session_id);
  1709. } else {
  1710. $a_users_to_add = CourseManager :: get_user_list_from_course_code($_SESSION['_course']['id'], 0);
  1711. }
  1712. } else {
  1713. //extract group members
  1714. $subscribed_users = GroupManager :: get_subscribed_users($_clean['group_id']);
  1715. $subscribed_tutors = GroupManager :: get_subscribed_tutors($_clean['group_id']);
  1716. $a_users_to_add_with_duplicates = array_merge($subscribed_users, $subscribed_tutors);
  1717. //remove duplicates
  1718. $a_users_to_add = $a_users_to_add_with_duplicates;
  1719. //array_walk($a_users_to_add, create_function('&$value,$key', '$value = json_encode($value);'));
  1720. $a_users_to_add = array_unique($a_users_to_add);
  1721. //array_walk($a_users_to_add, create_function('&$value,$key', '$value = json_decode($value, true);'));
  1722. }
  1723. //echo print_r($a_users_to_add);
  1724. $all_students_pages = array();
  1725. //data about teacher
  1726. $userinfo = api_get_user_info(api_get_user_id());
  1727. $name = $userinfo['complete_name_login_as'];
  1728. if (api_get_user_id() <> 0) {
  1729. $image_path = UserManager::get_user_picture_path_by_id(api_get_user_id(), 'web', false, true);
  1730. $image_repository = $image_path['dir'];
  1731. $existing_image = $image_path['file'];
  1732. $photo = '<img src="'.$image_repository.$existing_image.'" alt="'.$name.'" width="40" height="50" align="top" title="'.$name.'" />';
  1733. } else {
  1734. $photo = '<img src="'.api_get_path(
  1735. WEB_CODE_PATH
  1736. )."img/unknown.jpg".'" alt="'.$name.'" width="40" height="50" align="top" title="'.$name.'" />';
  1737. }
  1738. //teacher assignment title
  1739. $title_orig = $_POST['title'];
  1740. //teacher assignment reflink
  1741. $link2teacher = $_POST['title'] = $title_orig."_uass".api_get_user_id();
  1742. //first: teacher name, photo, and assignment description (original content)
  1743. // $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>';
  1744. $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(
  1745. 'AssignmentDesc'
  1746. ).'</td></tr><tr><td>'.$photo.'<br />'.Display::tag('span', $name).'</td></tr></table></div>';
  1747. $content_orig_B = '<br/><div align="center" style="font-size:24px">'.get_lang(
  1748. 'AssignmentDescription'
  1749. ).': '.$title_orig.'</div><br/>'.$_POST['content'];
  1750. //Second: student list (names, photo and links to their works).
  1751. //Third: Create Students work pages.
  1752. foreach ($a_users_to_add as $user_id => $o_user_to_add) {
  1753. if ($o_user_to_add['user_id'] != api_get_user_id()) //except that puts the task
  1754. {
  1755. $assig_user_id = $o_user_to_add['user_id']; //identifies each page as created by the student, not by teacher
  1756. $image_path = UserManager::get_user_picture_path_by_id($assig_user_id, 'web', false, true);
  1757. $image_repository = $image_path['dir'];
  1758. $existing_image = $image_path['file'];
  1759. $username = api_htmlentities(sprintf(get_lang('LoginX'), $o_user_to_add['username'], ENT_QUOTES));
  1760. $name = api_get_person_name($o_user_to_add['firstname'], $o_user_to_add['lastname'])." . ".$username;
  1761. $photo = '<img src="'.$image_repository.$existing_image.'" alt="'.$name.'" width="40" height="50" align="bottom" title="'.$name.'" />';
  1762. $is_tutor_of_group = GroupManager :: is_tutor_of_group(
  1763. $assig_user_id,
  1764. $_clean['group_id']
  1765. ); //student is tutor
  1766. $is_tutor_and_member = (GroupManager :: is_tutor_of_group(
  1767. $assig_user_id,
  1768. $_clean['group_id']
  1769. ) && GroupManager :: is_subscribed($assig_user_id, $_clean['group_id'])); //student is tutor and member
  1770. if ($is_tutor_and_member) {
  1771. $status_in_group = get_lang('GroupTutorAndMember');
  1772. } else {
  1773. if ($is_tutor_of_group) {
  1774. $status_in_group = get_lang('GroupTutor');
  1775. } else {
  1776. $status_in_group = " "; //get_lang('GroupStandardMember')
  1777. }
  1778. }
  1779. if ($assignment_type == 1) {
  1780. $_POST['title'] = $title_orig;
  1781. $_POST['comment'] = get_lang('AssignmentFirstComToStudent');
  1782. $_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(
  1783. 'AssignmentWork'
  1784. ).'</td></tr><tr><td>'.$photo.'<br />'.$name.'</td></tr></table></div>[['.$link2teacher.' | '.get_lang(
  1785. 'AssignmentLinktoTeacherPage'
  1786. ).']] '; //If $content_orig_B is added here, the task written by the professor was copied to the page of each student. TODO: config options
  1787. //AssignmentLinktoTeacherPage
  1788. $all_students_pages[] = '<li>'.
  1789. Display::tag(
  1790. 'span',
  1791. strtoupper($o_user_to_add['lastname']).', '.$o_user_to_add['firstname'],
  1792. array('title' => $username)
  1793. ).
  1794. ' [['.$_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)
  1795. //$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>';
  1796. $_POST['assignment'] = 2;
  1797. }
  1798. save_new_wiki();
  1799. }
  1800. }
  1801. //end foreach for each user
  1802. foreach ($a_users_to_add as $user_id => $o_user_to_add) {
  1803. if ($o_user_to_add['user_id'] == api_get_user_id()) {
  1804. $assig_user_id = $o_user_to_add['user_id'];
  1805. if ($assignment_type == 1) {
  1806. $_POST['title'] = $title_orig;
  1807. $_POST['comment'] = get_lang('AssignmentDesc');
  1808. sort($all_students_pages);
  1809. $_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(
  1810. 'AssignmentLinkstoStudentsPage'
  1811. ).'</div><br/><div style="background-color: #F5F8FB; border:solid; border-color:#E6E6E6"><ol>'.implode(
  1812. $all_students_pages
  1813. ).'</ol></div><br/>';
  1814. $_POST['assignment'] = 1;
  1815. }
  1816. save_new_wiki();
  1817. }
  1818. } //end foreach to teacher
  1819. }
  1820. /**
  1821. * Displays the results of a wiki search
  1822. * @param string Search term
  1823. * @param int Whether to search the contents (1) or just the titles (0)
  1824. */
  1825. function display_wiki_search_results($search_term, $search_content = 0, $all_vers = 0)
  1826. {
  1827. global $tbl_wiki, $groupfilter, $MonthsLong, $condition_session;
  1828. echo '<legend>'.get_lang('WikiSearchResults').'</legend>';
  1829. $_clean['group_id'] = (int)$_SESSION['_gid'];
  1830. $session_id = api_get_session_id();
  1831. $course_id = api_get_course_int_id();
  1832. //only by professors when page is hidden
  1833. if (api_is_allowed_to_edit(false, true) || api_is_platform_admin()) {
  1834. if ($all_vers == '1') {
  1835. if ($search_content == '1') {
  1836. $sql = "SELECT * FROM ".$tbl_wiki."
  1837. WHERE c_id = $course_id AND title LIKE '%".Database::escape_string(
  1838. $search_term
  1839. )."%' OR content LIKE '%".Database::escape_string(
  1840. $search_term
  1841. )."%' AND ".$groupfilter.$condition_session."";
  1842. //search all pages and all versions
  1843. } else {
  1844. $sql = "SELECT * FROM ".$tbl_wiki."
  1845. WHERE c_id = $course_id AND title LIKE '%".Database::escape_string(
  1846. $search_term
  1847. )."%' AND ".$groupfilter.$condition_session."";
  1848. //search all pages and all versions
  1849. }
  1850. } else {
  1851. if ($search_content == '1') {
  1852. $sql = "SELECT * FROM ".$tbl_wiki." s1
  1853. WHERE s1.c_id = $course_id AND title LIKE '%".Database::escape_string(
  1854. $search_term
  1855. )."%' OR content LIKE '%".Database::escape_string($search_term)."%' AND
  1856. id=(SELECT MAX(s2.id) FROM ".$tbl_wiki." s2 WHERE s2.c_id = $course_id AND s1.reflink = s2.reflink AND ".$groupfilter.$condition_session.")";
  1857. // warning don't use group by reflink because don't return the last version
  1858. } else {
  1859. $sql = " SELECT * FROM ".$tbl_wiki." s1
  1860. WHERE s1.c_id = $course_id AND title LIKE '%".Database::escape_string($search_term)."%' AND
  1861. id=(SELECT MAX(s2.id) FROM ".$tbl_wiki." s2 WHERE s2.c_id = $course_id AND s1.reflink = s2.reflink AND ".$groupfilter.$condition_session.")";
  1862. // warning don't use group by reflink because don't return the last version
  1863. }
  1864. }
  1865. } else {
  1866. if ($all_vers == '1') {
  1867. if ($search_content == '1') {
  1868. $sql = "SELECT * FROM ".$tbl_wiki." WHERE c_id = $course_id AND visibility=1 AND title LIKE '%".Database::escape_string(
  1869. $search_term
  1870. )."%' OR content LIKE '%".Database::escape_string(
  1871. $search_term
  1872. )."%' AND ".$groupfilter.$condition_session.""; //search all pages and all versions
  1873. } else {
  1874. $sql = "SELECT * FROM ".$tbl_wiki." WHERE c_id = $course_id AND visibility=1 AND title LIKE '%".Database::escape_string(
  1875. $search_term
  1876. )."%' AND ".$groupfilter.$condition_session.""; //search all pages and all versions
  1877. }
  1878. } else {
  1879. if ($search_content == '1') {
  1880. $sql = " SELECT * FROM ".$tbl_wiki." s1
  1881. WHERE s1.c_id = $course_id AND visibility=1 AND title LIKE '%".Database::escape_string(
  1882. $search_term
  1883. )."%' OR content LIKE '%".Database::escape_string($search_term)."%' AND
  1884. id=(SELECT MAX(s2.id) FROM ".$tbl_wiki." s2 WHERE s2.c_id = $course_id AND s1.reflink = s2.reflink AND ".$groupfilter.$condition_session.")";
  1885. // warning don't use group by reflink because don't return the last version
  1886. } else {
  1887. $sql = " SELECT * FROM ".$tbl_wiki." s1
  1888. WHERE s1.c_id = $course_id AND visibility=1 AND title LIKE '%".Database::escape_string(
  1889. $search_term
  1890. )."%' AND
  1891. id=(SELECT MAX(s2.id) FROM ".$tbl_wiki." s2 WHERE s2.c_id = $course_id AND s1.reflink = s2.reflink AND ".$groupfilter.$condition_session.")";
  1892. // warning don't use group by reflink because don't return the last version
  1893. }
  1894. }
  1895. }
  1896. $result = Database::query($sql);
  1897. //show table
  1898. if (Database::num_rows($result) > 0) {
  1899. $row = array();
  1900. while ($obj = Database::fetch_object($result)) {
  1901. //get author
  1902. $userinfo = api_get_user_info($obj->user_id);
  1903. //get time
  1904. $year = substr($obj->dtime, 0, 4);
  1905. $month = substr($obj->dtime, 5, 2);
  1906. $day = substr($obj->dtime, 8, 2);
  1907. $hours = substr($obj->dtime, 11, 2);
  1908. $minutes = substr($obj->dtime, 14, 2);
  1909. $seconds = substr($obj->dtime, 17, 2);
  1910. //get type assignment icon
  1911. if ($obj->assignment == 1) {
  1912. $ShowAssignment = Display::return_icon(
  1913. 'wiki_assignment.png',
  1914. get_lang('AssignmentDesc'),
  1915. '',
  1916. ICON_SIZE_SMALL
  1917. );
  1918. } elseif ($obj->assignment == 2) {
  1919. $ShowAssignment = Display::return_icon(
  1920. 'wiki_work.png',
  1921. get_lang('AssignmentWork'),
  1922. '',
  1923. ICON_SIZE_SMALL
  1924. );
  1925. } elseif ($obj->assignment == 0) {
  1926. $ShowAssignment = '<img src="../img/px_transparent.gif" />';
  1927. }
  1928. $row = array();
  1929. $row[] = $ShowAssignment;
  1930. if ($all_vers == '1') {
  1931. $row[] = '<a href="'.api_get_self(
  1932. ).'?cidReq='.$_course['id'].'&action=showpage&title='.api_htmlentities(
  1933. urlencode($obj->reflink)
  1934. ).'&view='.$obj->id.'&session_id='.api_htmlentities(
  1935. urlencode($_GET['$session_id'])
  1936. ).'&group_id='.api_htmlentities(urlencode($_GET['group_id'])).'">'.api_htmlentities($obj->title).'</a>';
  1937. } else {
  1938. $row[] = '<a href="'.api_get_self().'?cidReq='.$_course[id].'&action=showpage&title='.api_htmlentities(
  1939. urlencode($obj->reflink)
  1940. ).'&session_id='.api_htmlentities($_GET['session_id']).'&group_id='.api_htmlentities(
  1941. $_GET['group_id']
  1942. ).'">'.$obj->title.'</a>';
  1943. }
  1944. $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.')';
  1945. $row[] = $year.'-'.$month.'-'.$day.' '.$hours.":".$minutes.":".$seconds;
  1946. if ($all_vers == '1') {
  1947. $row[] = $obj->version;
  1948. } else {
  1949. if (api_is_allowed_to_edit(false, true) || api_is_platform_admin()) {
  1950. $showdelete = ' <a href="'.api_get_self(
  1951. ).'?cidReq='.$_course[id].'&action=delete&title='.api_htmlentities(
  1952. urlencode($obj->reflink)
  1953. ).'&group_id='.api_htmlentities($_GET['group_id']).'">'.Display::return_icon(
  1954. 'delete.png',
  1955. get_lang('Delete'),
  1956. '',
  1957. ICON_SIZE_SMALL
  1958. );
  1959. }
  1960. $row[] = '<a href="'.api_get_self().'?cidReq='.$_course[id].'&action=edit&title='.api_htmlentities(
  1961. urlencode($obj->reflink)
  1962. ).'&group_id='.api_htmlentities($_GET['group_id']).'">'.Display::return_icon(
  1963. 'edit.png',
  1964. get_lang('EditPage'),
  1965. '',
  1966. ICON_SIZE_SMALL
  1967. ).'</a> <a href="'.api_get_self().'?cidReq='.$_course[id].'&action=discuss&title='.api_htmlentities(
  1968. urlencode($obj->reflink)
  1969. ).'&session_id='.api_htmlentities($_GET['session_id']).'&group_id='.api_htmlentities(
  1970. $_GET['group_id']
  1971. ).'">'.Display::return_icon(
  1972. 'discuss.png',
  1973. get_lang('Discuss'),
  1974. '',
  1975. ICON_SIZE_SMALL
  1976. ).'</a> <a href="'.api_get_self().'?cidReq='.$_course[id].'&action=history&title='.api_htmlentities(
  1977. urlencode($obj->reflink)
  1978. ).'&session_id='.api_htmlentities($_GET['session_id']).'&group_id='.api_htmlentities(
  1979. $_GET['group_id']
  1980. ).'">'.Display::return_icon(
  1981. 'history.png',
  1982. get_lang('History'),
  1983. '',
  1984. ICON_SIZE_SMALL
  1985. ).'</a> <a href="'.api_get_self().'?cidReq='.$_course[id].'&action=links&title='.api_htmlentities(
  1986. urlencode($obj->reflink)
  1987. ).'&group_id='.api_htmlentities($_GET['group_id']).'">'.Display::return_icon(
  1988. 'what_link_here.png',
  1989. get_lang('LinksPages'),
  1990. '',
  1991. ICON_SIZE_SMALL
  1992. ).'</a>'.$showdelete;
  1993. }
  1994. $rows[] = $row;
  1995. }
  1996. $table = new SortableTableFromArrayConfig($rows, 1, 10, 'SearchPages_table', '', '', 'ASC');
  1997. $table->set_additional_parameters(
  1998. array(
  1999. 'cidReq' => $_GET['cidReq'],
  2000. 'action' => $_GET['action'],
  2001. 'group_id' => Security::remove_XSS($_GET['group_id']),
  2002. 'mode_table' => 'yes2',
  2003. 'search_term' => $search_term,
  2004. 'search_content' => $search_content,
  2005. 'all_vers' => $all_vers
  2006. )
  2007. );
  2008. $table->set_header(0, get_lang('Type'), true, array('style' => 'width:30px;'));
  2009. $table->set_header(1, get_lang('Title'), true);
  2010. if ($all_vers == '1') {
  2011. $table->set_header(2, get_lang('Author'), true);
  2012. $table->set_header(3, get_lang('Date'), true);
  2013. $table->set_header(4, get_lang('Version'), true);
  2014. } else {
  2015. $table->set_header(2, get_lang('Author').' ('.get_lang('LastVersion').')', true);
  2016. $table->set_header(3, get_lang('Date').' ('.get_lang('LastVersion').')', true);
  2017. $table->set_header(4, get_lang('Actions'), false, array('style' => 'width:130px;'));
  2018. }
  2019. $table->display();
  2020. } else {
  2021. echo get_lang('NoSearchResults');
  2022. }
  2023. }
  2024. /**
  2025. * Returns a date picker
  2026. * @todo replace this function with the formvalidator datepicker
  2027. *
  2028. */
  2029. function draw_date_picker($prefix, $default = '')
  2030. {
  2031. if (empty($default)) {
  2032. $default = date('Y-m-d H:i:s');
  2033. }
  2034. $parts = split(' ', $default);
  2035. list($d_year, $d_month, $d_day) = split('-', $parts[0]);
  2036. list($d_hour, $d_minute) = split(':', $parts[1]);
  2037. $month_list = array(
  2038. 1 => get_lang('JanuaryLong'),
  2039. 2 => get_lang('FebruaryLong'),
  2040. 3 => get_lang('MarchLong'),
  2041. 4 => get_lang('AprilLong'),
  2042. 5 => get_lang('MayLong'),
  2043. 6 => get_lang('JuneLong'),
  2044. 7 => get_lang('JulyLong'),
  2045. 8 => get_lang('AugustLong'),
  2046. 9 => get_lang('SeptemberLong'),
  2047. 10 => get_lang('OctoberLong'),
  2048. 11 => get_lang('NovemberLong'),
  2049. 12 => get_lang('DecemberLong')
  2050. );
  2051. $minute = range(10, 59);
  2052. array_unshift($minute, '00', '01', '02', '03', '04', '05', '06', '07', '08', '09');
  2053. $date_form = make_select($prefix.'_day', array_combine(range(1, 31), range(1, 31)), $d_day);
  2054. $date_form .= make_select($prefix.'_month', $month_list, $d_month);
  2055. $date_form .= make_select(
  2056. $prefix.'_year',
  2057. array(
  2058. $d_year - 2 => $d_year - 2,
  2059. $d_year - 1 => $d_year - 1,
  2060. $d_year => $d_year,
  2061. $d_year + 1 => $d_year + 1,
  2062. $d_year + 2 => $d_year + 2
  2063. ),
  2064. $d_year
  2065. ).'&nbsp;&nbsp;&nbsp;&nbsp;';
  2066. $date_form .= make_select($prefix.'_hour', array_combine(range(0, 23), range(0, 23)), $d_hour).' : ';
  2067. $date_form .= make_select($prefix.'_minute', $minute, $d_minute);
  2068. return $date_form;
  2069. }
  2070. /**
  2071. * Draws an HTML form select with the given options
  2072. *
  2073. */
  2074. function make_select($name, $values, $checked = '')
  2075. {
  2076. $output = '<select name="'.$name.'" id="'.$name.'">';
  2077. foreach ($values as $key => $value) {
  2078. $output .= '<option value="'.$key.'" '.(($checked == $key) ? 'selected="selected"' : '').'>'.$value.'</option>';
  2079. }
  2080. $output .= '</select>';
  2081. return $output;
  2082. }
  2083. /**
  2084. * Translates a form date into a more usable format
  2085. *
  2086. */
  2087. function get_date_from_select($prefix)
  2088. {
  2089. return $_POST[$prefix.'_year'].'-'.two_digits($_POST[$prefix.'_month']).'-'.two_digits(
  2090. $_POST[$prefix.'_day']
  2091. ).' '.two_digits($_POST[$prefix.'_hour']).':'.two_digits($_POST[$prefix.'_minute']).':00';
  2092. }
  2093. /**
  2094. * Converts 1-9 to 01-09
  2095. */
  2096. function two_digits($number)
  2097. {
  2098. $number = (int)$number;
  2099. return ($number < 10) ? '0'.$number : $number;
  2100. }
  2101. /**
  2102. * Get wiki information
  2103. * @param int wiki id
  2104. * @return array wiki data
  2105. */
  2106. function get_wiki_data($id)
  2107. {
  2108. global $tbl_wiki;
  2109. $course_id = api_get_course_int_id();
  2110. $id = intval($id);
  2111. $sql = 'SELECT * FROM '.$tbl_wiki.' WHERE c_id = '.$course_id.' AND id = '.$id.' ';
  2112. $result = Database::query($sql);
  2113. $data = array();
  2114. while ($row = Database::fetch_array($result, 'ASSOC')) {
  2115. $data = $row;
  2116. }
  2117. return $data;
  2118. }