DROP PROCEDURE IF EXISTS GetPublicProjectsForPresentation;
DELIMITER //
CREATE PROCEDURE GetPublicProjectsForPresentation
(
  IN p_sid TEXT,
  IN p_password TEXT,
  OUT outputCode INT(11)
)
MODIFIES SQL DATA
BEGIN

CALL `CheckIdLogin`(p_sid,p_password, outputCode);
IF outputCode = 42 OR outputCode = 2005 THEN 
	SET outputCode = 42;
	INSERT INTO projects SELECT p.ID AS projectID, p.name AS projectName, v.ID AS versionID , v.name AS versionName FROM projets AS p LEFT JOIN versions AS v ON p.ID = v.projectID WHERE (p.ownerID = 1 OR CheckInGroup(1,p.secondaryOwnersGroupID) = 42 OR CheckInGroup(1,p.authorizedGroupID) = 42) AND (v.ID IS NULL OR v.versionAbs = (SELECT MAX(versionAbs) FROM versions WHERE projectID = p.ID));
END IF;

END//
DELIMITER ;
