<?php
/*************************************************************************/
/* DOCEBO LMS - Learning Managment System                                */
/* ============================================                          */
/*                                                                       */
/* Copyright (c) 2006 by Giovanni Derks <virtualdarkness[AT]gmail-com>   */
/* http://www.docebo.org                                                 */
/*                                                                       */
/* This program is free software. You can redistribute it and/or modify  */
/* it under the terms of the GNU General Public License as published by  */
/* the Free Software Foundation; either version 2 of the License.        */
/*************************************************************************/

if(!defined('IN_DOCEBO')) die('You cannot access this file directly');

if($GLOBALS['current_user']->isAnonymous()) die('You can\'t access!');

addCss("style_wiki", "fw");
addCss("style_wiki_lms", "lms");
require_once($GLOBALS["where_lms"]."/lib/lib.wiki.php");


function &cwaSetup() {
	$res =new LmsWikiAdmin();
	$res->urlManagerSetup("modname=wiki&op=main");
	$res->setCourseId($_SESSION["idCourse"]);
	return $res;
}


function &cwpSetup($wiki_id) {
	$res =new LmsWikiPublic($wiki_id);
	$res->setInternalPerm("edit", checkPerm('edit', true));
	return $res;
}


function wikiMain() {
	checkPerm('view');
	$can_mod =checkPerm('edit', true);
	$can_admin =checkPerm('admin', true);

	$out=& $GLOBALS["page"];
	$out->setWorkingZone("content");
	$res ="";

	$cwa =& cwaSetup();
	$wiki_list =$cwa->getCourseWikiList();

	if ((count($wiki_list["list"]) == 1) && (!$can_admin)) {
		$um =& UrlManager::getInstance();
		$where = implode(",", $wiki_list["list"]);
		$url =$um->getUrl("op=show&wiki_id=".$where);
		jumpTo($url);
		return TRUE;
	}

	$title=$cwa->lang->def("_WIKI");
	$res.=$cwa->titleArea($title);
	$res.=$cwa->getHead(TRUE, TRUE, TRUE, TRUE);


	if ((count($wiki_list["list"]) == 0) && (!$can_admin)) {
		$res.=getInfoUi($cwa->lang->def("_NO_WIKI_AVAILABLE"));
	}
	else {
		//$res.=$cwa->getWikiListTable(0);
		$res.=$cwa->getCourseWikiTable($can_admin, $wiki_list);
	}


	$res.=$cwa->getFooter(TRUE);
	$out->add($res);
}


function &getLmsWikiUrlManager($wiki_id) {
	require_once($GLOBALS['where_framework']."/lib/lib.urlmanager.php");

	$um =& UrlManager::getInstance();
	$um->setStdQuery("modname=wiki&op=show&wiki_id=".$wiki_id);

	return $um;
}


function addeditWiki($id=0) {
	checkPerm('view');
	$can_mod =checkPerm('edit', true);
	$can_admin =checkPerm('admin', true);

	$out=& $GLOBALS["page"];
	$out->setWorkingZone("content");
	$res ="";

	$cwa =& cwaSetup();

	if ($id > 0) {
		$info=$cwa->wikiManager->getWikiInfo($id);
		$title_label=$cwa->lang->def("_EDIT_WIKI").": ".$info["title"];
	}
	else {
		$title_label=$cwa->lang->def("_ADD_WIKI");
	}

	$um=& UrlManager::getInstance();
	$back_url=$um->getUrl();
	$title[$back_url]=$cwa->lang->def("_WIKI");
	$title[]=$title_label;
	$res.=$cwa->titleArea($title);
	$res.=$cwa->getHead();
	$res.=$cwa->backUi();

	$res.=$cwa->addeditWiki($id);

	$res.=$cwa->backUi();
	$res.=$cwa->getFooter();
	$out->add($res);
}


function saveWiki() {
	checkPerm('edit');
	$cwa =& cwaSetup();
	$cwa->saveWiki();
}



function deleteWiki() {
	checkPerm('edit');
	$res="";

	if ((isset($_GET["wiki_id"])) && ($_GET["wiki_id"] > 0))
		$wiki_id=(int)$_GET["wiki_id"];
	else
		return FALSE;

	$cwa =& cwaSetup();
	$cwa->requireLmsWikiOwner($wiki_id);

	$delete_ui_code=$cwa->deleteWiki($wiki_id);

	if (!empty($delete_ui_code)) {

		$info=$cwa->wikiManager->getWikiInfo($wiki_id);
		$title_label=$cwa->lang->def("_DELETE_WIKI").": ".$info["title"];

		$out=& $GLOBALS["page"];
		$out->setWorkingZone("content");

		$um=& UrlManager::getInstance();
		$back_url=$um->getUrl();
		$title[$back_url]=$cwa->lang->def("_WIKI");
		$title[]=$title_label;
		$res.=$cwa->titleArea($title);
		$res.=$cwa->getHead();
		$res.=$cwa->backUi();

		$course_id =$cwa->getCourseId();
		if ($cwa->wikiManager->isWikiUsedByOthers($course_id, $wiki_id)) {
			$res.=getInfoUI($cwa->lang->def("_WIKI_USED_BY_OTHER_COURSES"));
		}

		$res.=$delete_ui_code;

		$res.=$cwa->getFooter();
		$out->add($res);
	}
}


function showWikiPerm() {
	checkPerm('edit');
	$res="";

	if ((isset($_GET["wiki_id"])) && ($_GET["wiki_id"] > 0))
		$wiki_id=(int)$_GET["wiki_id"];
	else
		return FALSE;

	$cwa =& cwaSetup();

	$page_content=$cwa->showWikiPerm($wiki_id);

	if ($page_content !== FALSE) {

		$out=& $GLOBALS["page"];
		$out->setWorkingZone("content");

		$info=$cwa->wikiManager->getWikiInfo($wiki_id);
		$title_label=$cwa->lang->def("_WIKI_PERMISSIONS").": ".$info["title"];

		$um=& UrlManager::getInstance();
		$back_url=$um->getUrl();
		$title[$back_url]=$cwa->lang->def("_WIKI");
		$title[]=$title_label;
		$res.=$cwa->titleArea($title);
		$res.=$cwa->getHead();

		$res.=$page_content;

		$res.=$cwa->getFooter();
		$out->add($res);
	}
}


function doneWikiPerm() {
	checkPerm('edit');

	$GLOBALS['current_user']->loadUserSectionST('/lms/course/private/');
	$GLOBALS['current_user']->SaveInSession();

	$cwa =& cwaSetup();
	$um=& UrlManager::getInstance();
	$url=$um->getUrl();
	jumpTo($url);
}


function selectWiki() {
	checkPerm('edit');
	$res="";

	$cwa =& cwaSetup();

	$page_content =$cwa->selectLmsWiki();

	if ($page_content !== FALSE) {

		$out=& $GLOBALS["page"];
		$out->setWorkingZone("content");

		$title_label=$cwa->lang->def("_SELECT_WIKI");

		$um=& UrlManager::getInstance();
		$back_url=$um->getUrl();
		$title[$back_url]=$cwa->lang->def("_WIKI");
		$title[]=$title_label;
		$res.=$cwa->titleArea($title);
		$res.=$cwa->getHead();

		$res.=$page_content;

		$res.=$cwa->getFooter();
		$out->add($res);
	}
}


// --------------------------------------------------------


function showWiki() {
	checkPerm('view');
	$can_mod =checkPerm('edit', true);
	$can_admin =checkPerm('admin', true);
	$lang =& DoceboLanguage::createInstance('profile', 'framework');

	if ((!isset($_GET["wiki_id"])) || ($_GET["wiki_id"] < 1))
		return FALSE;

	$out=& $GLOBALS["page"];
	$out->setWorkingZone("content");
	$res ="";

	$wiki_id =$_GET["wiki_id"];
	$um =& getLmsWikiUrlManager($wiki_id);
	$cwp =& cwpSetup($wiki_id);

	$title=$cwp->lang->def("_WIKI");
	$res.=$cwp->titleArea($title);
	
	$res .= getBackUi('index.php?modname=wiki&op=main', '<< '.$lang->def('_BACK').'');//'<a href="index.php?modname=wiki&op=main">Indietro</a></p>';
	
	$res.=$cwp->getHead(TRUE, TRUE, TRUE, TRUE);

	$res.=$cwp->getPageContent();

	$res.=$cwp->getFooter(TRUE);
	
	$out->add($res);
}


function editWikiPage() {
	checkPerm('edit');

	if ((!isset($_GET["wiki_id"])) || ($_GET["wiki_id"] < 1))
		return FALSE;
	
	$wiki_id =$_GET["wiki_id"];
	$um =& getLmsWikiUrlManager($wiki_id);
	$cwp =& cwpSetup($wiki_id);
	//$cwp->checkWikiPerm($wiki_id, "edit");

	$page_code=$cwp->editWikiPage();

	if (!empty($page_code)) {
		$out=& $GLOBALS["page"];
		$out->setWorkingZone("content");
		$res ="";

		$title =$cwp->lang->def("_WIKI");
		$res.=$cwp->titleArea($title);
		$res.=$cwp->getHead(TRUE, FALSE);
		
		$res.=$page_code;
		
		$res.=$cwp->getFooter();
		$out->add($res);
	}
}


function wikiMap() {
	checkPerm('edit');

	if ((!isset($_GET["wiki_id"])) || ($_GET["wiki_id"] < 1))
		return FALSE;

	$wiki_id =$_GET["wiki_id"];
	$um =& getLmsWikiUrlManager($wiki_id);
	$cwp =& cwpSetup($wiki_id);
	//$cwp->checkWikiPerm($wiki_id, "view");

	$out=& $GLOBALS["page"];
	$out->setWorkingZone("content");

	$title=$cwp->lang->def("_WIKI");
	$res ="";
	$res.=$cwp->titleArea($title);
	$res.=$cwp->getHead();

	$res.=$cwp->wikiMap();

	$res.=$cwp->getFooter();

	$out=& $GLOBALS["page"];
	$out->setWorkingZone("content");
	$out->add($res);
}


function wikiPageHistory() {
	checkPerm('edit');

	if ((!isset($_GET["wiki_id"])) || ($_GET["wiki_id"] < 1))
		return FALSE;

	$wiki_id =$_GET["wiki_id"];
	$um =& getLmsWikiUrlManager($wiki_id);
	$cwp =& cwpSetup($wiki_id);
	//$cwp->checkWikiPerm($wiki_id, "edit");

	$out=& $GLOBALS["page"];
	$out->setWorkingZone("content");

	$title=$cwp->lang->def("_WIKI");
	$res ="";
	$res.=$cwp->titleArea($title);
	$res.=$cwp->getHead();

	$res.=$cwp->wikiPageHistory();

	$res.=$cwp->getFooter();

	$out=& $GLOBALS["page"];
	$out->setWorkingZone("content");
	$out->add($res);
}


function wikiSearch() {
	checkPerm('edit');

	if ((!isset($_GET["wiki_id"])) || ($_GET["wiki_id"] < 1))
		return FALSE;

	$wiki_id =$_GET["wiki_id"];
	$um =& getLmsWikiUrlManager($wiki_id);
	$cwp =& cwpSetup($wiki_id);
	//$cwp->checkWikiPerm($wiki_id, "view");

	$out=& $GLOBALS["page"];
	$out->setWorkingZone("content");

	$title=$cwp->lang->def("_WIKI");
	$res ="";
	$res.=$cwp->titleArea($title);
	$res.=$cwp->getHead(TRUE, FALSE, FALSE, TRUE);


	$res.=$cwp->getFoundPages();


	$res.=$cwp->getFooter(FALSE);
	$out->add($res);
}



// --------------------------------------------------------


function wikiDispatch($op) {

	switch($op) {
		case "main": {
			wikiMain();
		} break;
		case "addwiki" : {
			addeditWiki();
		} break;
		case "editwiki" : {
			addeditWiki((int)$_GET["wiki_id"]);
		} break;
		case "savewiki" : {
			if (!isset($_POST["undo"]))
				saveWiki();
			else
				wikiMain();
		} break;
		case "delwiki" : {
			deleteWiki();
		} break;
		case "setperm" : {
			showWikiPerm();
		} break;
		case "doneperm" : {
			doneWikiPerm();
		} break;
		case "selectwiki": {
			selectWiki();
		} break;

		case "show": {
			showWiki();
		} break;
		case "edit": {
			editWikiPage();
		} break;
		case "map": {
			wikiMap();
		} break;
		case "history": {
			wikiPageHistory();
		} break;
		case "search": {
			wikiSearch();
		} break;
	}

}


?>