Modulo:Mooc

Da Wikiversità, l'apprendimento libero.
 Questo modulo è parte dell'Interfaccia MOOC.

Questo è lo script centrale dei MOOC per eseguire il rendering di un elemento MOOC in una pagina wiki.

Invokable functions[modifica]

Le seguenti funzioni sono statiche e possono essere richiamate dalle pagine wiki.

overview[modifica]

Esegue la panoramica alla pagina di invocazione. La pagina di riepilogo elenca tutte le lezioni che compongono il MOOC.


Parametri:

  1. Frame: frame - WikiMedia frame object holding information about the invoking page

Returns: string representation of the overview page rendered

render[modifica]

Mostra un elemento MOOC nella pagina di invocazione. Il titolo della pagina di invocazione viene utilizzato per identificare l'elemento da mostrare. Non è quindi possibile richiamare il modulo su pagine non MOOC: Il titolo della pagina deve essere una sottopagina della pagina MOOC

Parametri:

  1. Frame: frame - WikiMedia frame object holding information about the invoking page

Returns:

  • string representation of the item rendered
  • throws an error if the invoking page is not a child of the MOOC base
  • throws an error if the invoking page does not represent any MOOC item (see explanation above)

Instance functions[modifica]

The following functions are accessible by other Lua modules if they have access to a MOOC instance. Therefore they can not be invoked by wiki pages directly.

init[modifica]

Initializes the MOOC module. This method can be overridden and is supposed to register type handlers for all item types known. If the method is overridden you should always call the super method.

Parameters:

  1. String: baseUrl - page title of the MOOC base (the page where the MOOC is located)


addTypeHandler[modifica]

Registers a type handler for a MOOC item type.

Parameters:

  1. Mooc/TypeHandler: typeHandler - instance of the type handler to be registered


getBaseUrl[modifica]

Determines the page title of the MOOC base. This default implementation considers the root page of the invoking wiki page as MOOC base.

Returns: page title of the MOOC base


getIndexUrl[modifica]

Determines the page title of the default MOOC index. The default MOOC index is located `/MoocIndex` relative to the MOOC base.

Returns: page title of the MOOC index


getCurrentPath[modifica]

Gets the MOOC item path of the invoking wiki page. The MOOC item path is always relative to the MOOC base.

Returns:

  • MOOC item path of the invoking wiki page
  • nil if the invoking page is not a child of the MOOC base


getIndex[modifica]

Retrieves the plain wiki text content of the MOOC index page.

Returns:

  • wiki text of the MOOC index page
  • throws an error if the content retrieval failed


renderOverview[modifica]

Renders the overview at the invoking page. The overview page lists all lessons the MOOC consists of.

Parameters:

  1. Frame: frame - WikiMedia frame object holding information about the invoking page

Returns: string representation of the overview page rendered


renderItem[modifica]

Renders a MOOC item at the invoking page. At the moment a page can only render the item it represents. (item with the path equal to the page title relative to the MOOC base)

Parameters:

  1. Frame: frame - WikiMedia frame object holding information about the invoking page

Returns:

  • string representation of the item rendered
  • throws an error if the invoking page is not a child of the MOOC base
  • throws an error if the invoking page does not represent any MOOC item (see explanation above)


Local functions[modifica]

The following functions are local and therefore can not be invoked by wiki pages or other Lua modules.

getHandler[modifica]

Gets the type handler for a certain MOOC item type. The handler must be registered before e.g. by using `addTypeHandler`.

Parameters:

  1. String: typeIdentifier - identifier of the MOOC item type the handler is needed for

Returns:

  • template handler for the MOOC item type specified
  • throws an error if no registered type handler handles the item type specified

require("Modulo:Exception");
local inheritance = require("Modulo:Inheritance");
local Item = require("Modulo:Mooc/Data/Item");
local TypeHandler = require("Modulo:Mooc/TypeHandler");
local IndexParser = require("Modulo:Mooc/IndexParser");
local Unit = require("Modulo:Mooc/Data/Unit");
local Lesson = require("Modulo:Mooc/Data/Lesson");

local Mooc = inheritance.extend(inheritance.Class);

local handlers = {}
local function getHandler(typeIdentifier)
	for k,v in pairs(handlers) do
		if v:handlesType(typeIdentifier) then
  			return v;
  		end
	end
	
	local registered = {}
    for k,v in pairs(handlers) do
    	table.insert(registered, k);
    end
    throw('there is no type handler for item type "' .. typeIdentifier .. '" registered. Registered: ' .. table.concat(registered, ",") .. ".\n");
end

function Mooc:init(baseUrl)
  if baseUrl then
  	self.baseUrl = baseUrl;
  end
	
  self.typeHandlers = {}

  -- register basic item types
  self:addTypeHandler(TypeHandler(Unit.TYPE, "Modulo:Mooc/Data/Unit", "Module:Mooc/Template/Unit"));
  self:addTypeHandler(TypeHandler(Lesson.TYPE, "Modulo:Mooc/Data/Lesson", "Module:Mooc/Template/Lesson"));
  	
  self.typeHandlers.getHandler = function(typeIdentifier)
    return getHandler(typeIdentifier);
  end
end

function Mooc:addTypeHandler(typeHandler)
  handlers[typeHandler:getType():getIdentifier()] = typeHandler;
end

function Mooc:getBaseUrl()
  if not self.baseUrl then
    local crrTitle = mw.title.getCurrentTitle();
    --TODO substitute with interwiki, nsText and rootText for non-expensive solution
    --expensive
    local rootTitle = crrTitle.rootPageTitle;
    self.baseUrl = rootTitle.fullText;
  end
  return self.baseUrl;
end

function Mooc:getIndexUrl()
  return self:getBaseUrl() .. "/MoocIndex";
end

function Mooc:getCurrentPath()
	local crrUrl = mw.title.getCurrentTitle().fullText;
	local baseUrl = self:getBaseUrl();
	
	if string.sub(crrUrl:lower(), 1, string.len(baseUrl)) == baseUrl:lower() then
		--TODO this disables to use "_" in item path
		local crrPath = string.gsub(string.sub(crrUrl, string.len(baseUrl) + 2), "_", " ");
		return crrPath;
	end
	return nil;
end

function Mooc:getIndex()
  local indexUrl = self:getIndexUrl();
  --expensive
  local indexPage = mw.title.new(indexUrl);
  local indexPlain = indexPage:getContent();
  if indexPlain then
    return indexPlain;
  end
  
  throw('failed to read index from URL "' .. indexUrl .. '"');
end

function Mooc:renderOverview(frame)
	local overviewTemplate = require("Module:Mooc/Template/Overview");
	local index = IndexParser.parseIndexOverview(self:getIndex(), self:getBaseUrl());
	return overviewTemplate:render(frame, index, self);
end

function Mooc:renderItem(frame, itemPath)
	local itemPath = itemPath;
	if itemPath == nil then
		itemPath = self:getCurrentPath();
		if itemPath == nil then
			throw("failed to render item: the page is not a child of the base page");
		end
	end
	local index = IndexParser.parseIndex(self:getIndex(), itemPath, self:getBaseUrl());
	if not index then
		throw('failed to render item @ "' .. itemPath .. '": item not found');
	end
	local typeHandler = self.typeHandlers.getHandler(index["item"]:getTypeIdentifier());
	return typeHandler:getTemplate():render(frame, index, self);
end

function Mooc.overview(frame)
	local baseUrl = frame.args['base'];
	local mooc = Mooc(baseUrl);
	return mooc:renderOverview(frame);
end

function Mooc.render(frame)
	local baseUrl = frame.args['base'];
	local itemPath = frame.args['path'];
	local mooc = Mooc(baseUrl);
	return mooc:renderItem(frame, itemPath);
end

return Mooc;