Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: e07635f4d0
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 187 lines (153 sloc) 6.185 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
<?PHP // $Id$


include("$CFG->dirroot/mod/discuss/lib.php");

// These are non-special forum types ie the ones that aren't automatically created
$FORUM_TYPE = array ("general" => "General Forum",
                       "eachuser" => "Each $student posts a topic");

function is_subscribed($user, $forum) {
    global $db;

    return record_exists_sql("SELECT * FROM forum_subscriptions WHERE user='$user' AND forum='$forum'");
}

function forum_subscribe($user, $forum) {
    global $db;

    return $db->Execute("INSERT INTO forum_subscriptions SET user = '$user', forum = '$forum'");
}

function forum_unsubscribe($user, $forum) {
    global $db;

    return $db->Execute("DELETE FROM forum_subscriptions WHERE user = '$user' AND forum = '$forum'");
}


function user_has_posted_discussion($forumid, $userid) {
    if ($topics = get_all_topics($forumid, "DESC", $userid)) {
        return true;
    } else {
        return false;
    }
}

function user_can_post_discussion($forum) {
// $forum is an object
    global $USER;

    if ($forum->type == "eachuser") {
        return (! user_has_posted_discussion($forum->id, $USER->id));
    } else if (isteacher($forum->course)) {
        return true;
    } else {
        return $forum->open;
    }
}


function get_all_topics($forum="0", $forum_sort="DESC", $user=0) {
    if ($user) {
        $userselect = " AND u.id = '$user' ";
    } else {
        $userselect = "";
    }
    return get_records_sql("SELECT p.*, u.firstname, u.lastname, u.email, u.picture, u.id as userid
FROM discuss d, discuss_posts p, user u
WHERE d.forum = '$forum' AND p.discuss = d.id AND
p.parent= 0 AND p.user = u.id $userselect
ORDER BY p.created $forum_sort");
}


function get_course_news_forum($courseid) {
    if ($forum = get_record_sql("SELECT * from forum WHERE course = '$courseid' AND type = 'news'")) {
        return $forum;
    } else {
        // Doesn't exist, so create one now.
        $forum->course = $courseid;
        $forum->type = "news";
        $forum->name = "News";
        $forum->intro= "General news about this course";
        $forum->open = 0;
        $forum->assessed = 0;
        $forum->timemodified = time();
        $forum->id = insert_record("forum", $forum);
        return get_record_sql("SELECT * from forum WHERE id = '$forum->id'");
    }
}

function get_course_social_forum($courseid) {
    if ($forum = get_record_sql("SELECT * from forum WHERE course = '$courseid' AND type = 'social'")) {
        return $forum;
    } else {
        // Doesn't exist, so create one now.
        $forum->course = $courseid;
        $forum->type = "social";
        $forum->name = "Social";
        $forum->intro= "A forum to socialise and talk about anything you like";
        $forum->open = 1;
        $forum->assessed = 0;
        $forum->timemodified = time();
        $forum->id = insert_record("forum", $forum);
        return get_record_sql("SELECT * from forum WHERE id = '$forum->id'");
    }
}

function get_course_discussion_forum($courseid) {
    if ($forum = get_record_sql("SELECT * from forum WHERE course = '$courseid' AND type = 'discussion'")) {
        return $forum;
    } else {
        // Doesn't exist, so create one now.
        $forum->course = $courseid;
        $forum->type = "discussion";
        $forum->name = "Course Discussion";
        $forum->intro= "Discussions about course content";
        $forum->open = 0;
        $forum->assessed = 1;
        $forum->timemodified = time();
        $forum->id = insert_record("forum", $forum);
        return get_record_sql("SELECT * from forum WHERE id = '$forum->id'");
    }
}


function print_forum_latest_topics($forum_id=0, $forum_numtopics=5, $forum_style="plain", $forum_sort="DESC") {
    global $CFG, $USER;
    
    if ($forum_id) {
        if (! $forum = get_record("forum", "id", $forum_id)) {
            error("Forum ID was incorrect");
        }
        if (! $course = get_record("course", "id", $forum->course)) {
            error("Could not find the course this forum belongs to!");
        }

        if ($course->category) {
            require_login($course->id);
        }

    } else {
        if (! $course = get_record("course", "category", 0)) {
            error("Could not find a top-level course!");
        }
        if (! $forum = get_course_news_forum($course->id)) {
            error("Could not find or create a main forum in this course (id $course->id)");
        }
    }

    if (! $topics = get_all_topics($forum->id, $forum_sort) ) {
        echo "<P ALIGN=CENTER><B>There are no discussion topics yet in this forum.</B></P>";

    } else {

        $replies = count_discussion_replies($forum->id);

        $topiccount = 0;

        foreach ($topics as $topic) {
            $topiccount++;

            if ($forum_numtopics && ($topiccount > $forum_numtopics)) {
                echo "<P ALIGN=right><A HREF=\"$CFG->wwwroot/mod/discuss/index.php?forum=$forum->id\">Older topics</A> ...</P>";
                break;
            }
            if ($replies[$topic->discuss]) {
                $topic->replies = $replies[$topic->discuss]->replies;
            } else {
                $topic->replies = 0;
            }
            $ownpost = ($topic->userid == $USER->id);
            switch ($forum_style) {
                case "minimal":
                    echo "<P><FONT COLOR=#555555>".userdate($topic->modified, "j M H:i")."</FONT>";
                    echo "<BR>$topic->subject ";
                    echo "<A HREF=\"$CFG->wwwroot/mod/discuss/view.php?d=$topic->discuss\">more...</A>";
                    echo "</P>\n";
                break;
                default:
                    print_post($topic, $forum->course, $ownpost, $reply=0, $link=1, $assessed=false);
                    echo "<BR>\n";
                break;
            }
        }
    }
    if (user_can_post_discussion($forum)) {
        echo "<P ALIGN=right>";
        echo "<A HREF=\"$CFG->wwwroot/mod/discuss/post.php?forum=$forum->id\">Add a new topic...</A>";
        echo "</P>";
    }

}


?>
Something went wrong with that request. Please try again.