<?php
/*
  topic.php -- Generate the Topics view

  Part of Worldview Manager
  Copyright (C) 2009 Scott Aaronson, Leonid Grinberg, Louis Wasserman

  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License.  You may obtain a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0                

  Unless  required  by  applicable law or  agreed  to  in  writing,  software
  distributed under the License is distributed on an  "AS IS" BASIS,  WITHOUT
  WARRANTIES OR  CONDITIONS OF ANY KIND,  either express or implied.  See the
  License  for the  specific language  governing permissions  and limitations
  under the License.
*/


require_once("lib/includes.php");

fix_url();
global $TOPIC_ID;

if (list($TOPIC_ID, $topic_name, $topic_text) = get_topic()) {
    set_topic_id_cookie($TOPIC_ID);
    topic_page_template($TOPIC_ID, $topic_name, $topic_text);
} else {
    set_topic_id_cookie(0);
    topic_template();
}


function fix_url()
{
    if (preg_match("/topic\.php\??$/", $_SERVER["REQUEST_URI"]))
        redirect("/worldview/topics/");
    else if (preg_match("/topic\.php\?topic_id=(\d+)$/", $_SERVER["REQUEST_URI"], $topic_id))
        redirect("/worldview/topics/" . $topic_id[1]);
}


function topic_template()
{
    $path = make_path(
        array(
            new PathElement("Topics", false, "/worldview/topics/"),
            new PathElement("Topic Selection")));
    $title = make_title($path);
    $loc = make_loc($path);
    list($grey_link_explanation, $topic_list) = explanation_and_topic_list();
    $page = new Page("topic/topic.html", array(
                         "header" => header_template($title, $loc, "topic.php"),
                         "footer" => footer_template(),
                         "topic_list" => $topic_list,
                         "grey_link_explanation" => $grey_link_explanation));
    print $page->output;
}


function explanation_and_topic_list()
{
    global $USER_ID;
    $topics = get_all_topics();
    $done_topics = array();
    $query = topics_done($USER_ID);
    while (list($tid) = mysql_fetch_row($query))
        $done_topics[$tid] = true;

    if (count($done_topics))
        $grey_link_explanation = "Grey, crossed-out topic links are for those topics that you have already completed. If you click on those links, your old responses will be discarded (though please remember that for statistical and other research purposes, we keep a record of all old responses, even discarded ones).";
    else 
        $grey_link_explanation = "";

    $topic_list = "<ul id=\"topic_list\">\n";
    foreach ($topics as $topic) {
	$tid = $topic['topic_id'];
        $extra = (isset($done_topics[$tid]) ? " class = \"done\"" : "");
        $link = send_topic($tid);
	$topic_list .= <<<EOT
<li>
	<a href = "$link" $extra> $topic[topic_name] </a>
</li>
EOT;
    }
    $topic_list .= "</ul>\n";
    return array($grey_link_explanation, $topic_list);
}


function topic_page_template($topic_id, $topic_name, $topic_text)
{
    global $USER_ID;
    $USER_ID = intval($USER_ID);
    $path = make_path(
        array(
            new PathElement("Topics", false, "/worldview/topics/"),
            new PathElement($topic_name)));
    $title = make_title($path);
    $loc = make_loc($path);

    $query = topics_done($USER_ID);
    $flag = false;
    while (list($tid) = mysql_fetch_row($query)) {
        if ($tid == $topic_id) {
            $flag = true;
            break;
        }
    }

    if ($flag) {
        $status = "You have already completed this topic";
        $action = "clear your answers and start again";
        $done_topic = "?done_topic=$topic_id";
        $special_msg = "<p>You can also <a href=\"/worldview/done/$topic_id\">see a summary</a> of the responses you gave to this topic.</p>";
    } else if (mysql_fetch_row(get_users_responses($USER_ID, $topic_id))) {
        $status = "You have already responded to some statements in this topic";
        $action = "continue";
        $done_topic = "";
        $special_msg = "<p>You can also <a href=\"/worldview/statements/start?done_topic=$topic_id\">clear your responses</a> and start over.</p>";
    } else {
        $status = "You have not yet responded to any of the statements in this topic";
        $action = "begin";
        $done_topic = "";
        $special_msg = "";
    }

    $page = new Page("topic/topic_page.html", array(
                     "header" => header_template($title, $loc),
                     "footer" => footer_template(),
                     "topic_name" => $topic_name,
                     "topic_text" => $topic_text,
                     "status" => $status,
                     "action" => $action,
                     "done_topic" => $done_topic,
                     "special_msg" => $special_msg,
                     "comments" => make_comment_area('topic', $topic_id, get_name($USER_ID))));
    print $page->output;
}

?>