<commit_before><?php  // $Id$

/// Library of extra functions for the dialogue module

// SQL FUNCTIONS ///////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////
function dialogue_count_all_needing_replies_self($user = '') {
// count [conversations] needing replies [from] self for all dialogues
// function requested by <NAME> 17 Oct 2003 
    global $USER;
    
    if ($user) {    
        return count_records_select("dialogue_conversations", "(userid = $user->id OR 
            recipientid = $user->id) AND lastid != $user->id AND closed = 0");
    } else {
        return count_records_select("dialogue_conversations", "(userid = $USER->id OR 
            recipientid = $USER->id) AND lastid != $USER->id AND closed = 0");
    }
}


//////////////////////////////////////////////////////////////////////////////////////
function dialogue_count_closed($dialogue, $user) {
    
    return count_records_select("dialogue_conversations", "dialogueid = $dialogue->id AND 
        (userid = $user->id OR recipientid = $user->id) AND closed = 1");
    }


//////////////////////////////////////////////////////////////////////////////////////
function dialogue_count_open($dialogue, $user) {
    
    return count_records_select("dialogue_conversations", "dialogueid = $dialogue->id AND 
        (userid = $user->id OR recipientid = $user->id) AND closed = 0");
}


//////////////////////////////////////////////////////////////////////////////////////
function dialogue_count_needing_replies_other($dialogue, $user) {
// count [conversations] needing replies [from] other [person]  
    return count_records_select("dialogue_conversations", "dialogueid = $dialogue->id AND 
        (userid = $user->id OR recipientid = $user->id) AND lastid = $user->id AND closed = 0");
}


//////////////////////////////////////////////////////////////////////////////////////
function dialogue_count_needing_replies_self($dialogue, $user) {
// count [conversations] needing replies [from] self
    
    return count_records_select("dialogue_conversations", "dialogueid = $dialogue->id AND 
        (userid = $user->id OR recipientid = $user->id) AND lastid != $user->id AND closed = 0");
}


//////////////////////////////////////////////////////////////////////////////////////
function dialogue_get_participants($dialogueid) {
//Returns the users with data in one dialogue
//(users with records in dialogue_conversations, creators and receivers)

    global $CFG;

    //Get conversation's creators
    $creators = get_records_sql("SELECT DISTINCT u.*
                                FROM {$CFG->prefix}user u,
                                     {$CFG->prefix}dialogue_conversations c
                                WHERE c.dialogueid = '$dialogueid' and
                                      u.id = c.userid");

    //Get conversation's receivers
    $receivers = get_records_sql("SELECT DISTINCT u.*
                                FROM {$CFG->prefix}user u,
                                     {$CFG->prefix}dialogue_conversations c
                                WHERE c.dialogueid = '$dialogueid' and
                                      u.id = c.recipientid");

    //Add receivers to creators
    if ($receivers) {
        foreach ($receivers as $receiver) {
            $creators[$receiver->id] = $receiver;
        }
    }

    //Return creators array (it contains an array of unique users, creators and receivers)
    return ($creators);
} 

//////////////////////////////////////////////////////////////////////////////////////
function dialogue_get_available_users($dialogue) {

    if (! $course = get_record("course", "id", $dialogue->course)) {
        error("Course is misconfigured");
    }
    switch ($dialogue->dialoguetype) {
        case 0 : // teacher to student
            if (isteacher($course->id)) {
                return dialogue_get_available_students($dialogue);
            }
            else {
                return dialogue_get_available_teachers($dialogue);
            }
        case 1: // student to student
            if (isstudent($course->id)) {
                return dialogue_get_available_students($dialogue);
            }
            else {
                return;
            }
        case 2: // everyone
            if ($teachers = dialogue_get_available_teachers($dialogue)) {
                foreach ($teachers as $userid=>$name) {
                    $names[$userid] = $name;
                }
                $names[-1] = "-------------";
            }
            if ($students = dialogue_get_available_students($dialogue)) {
                foreach ($students as $userid=>$name) {
                    $names[$userid] = $name;
                }
            }
            if (isset($names)) {
                return $names;
            }
            return;
    }
}

                    
//////////////////////////////////////////////////////////////////////////////////////
function dialogue_get_available_students($dialogue) {
global $USER;
    
    if (! $course = get_record("course", "id", $dialogue->course)) {
        error("Course is misconfigured");
    }
    if (! $cm = get_coursemodule_from_instance("dialogue", $dialogue->id, $course->id)) {
        error("Course Module ID was incorrect");
    }
    
    $groupid = get_current_group($course->id);
    // add current group before list of students if it's the teacher
    if (isteacher($course->id) and groupmode($course)) {
        // show teacher their current group
        if ($groupid) {
            if (!$group = get_record("groups", "id", $groupid)) {
                error("Dialogue get available students: group not found");
            }
            $gnames["g$groupid"] = $group->name;
        } else { // all participants
            $gnames["g0"] = get_string("allparticipants");
        }
        $gnames["spacer"] = "------------";
    }
    // get the students on this course (default sort order)...
    if ($users = get_course_students($course->id)) {
        foreach ($users as $otheruser) {
            // ...exclude self and...
            if ($USER->id != $otheruser->id) {
                // ...if teacher and groups then exclude students not in the current group
                if (isteacher($course->id) and groupmode($course) and $groupid) {
                    if (!ismember($groupid, $otheruser->id)) {
                        continue;
                    }
                }
                // ...if student and groupmode is SEPARATEGROUPS then exclude students not in student's group
                if (isstudent($course->id) and (groupmode($course, $cm) == SEPARATEGROUPS)) {
                    if (!ismember($groupid, $otheruser->id)) {
                        continue;
                    }
                }
                // ... and any already in any open conversations unless multiple conversations allowed
                if ($dialogue->multipleconversations or count_records_select("dialogue_conversations", 
                        "dialogueid = $dialogue->id AND 
                        ((userid = $USER->id AND recipientid = $otheruser->id) OR 
                        (userid = $otheruser->id AND recipientid = $USER->id)) AND closed = 0") == 0) {
                    $names[$otheruser->id] = fullname($otheruser);
                }
            }
        }
    }
    if (isset($gnames)) {
        $list = $gnames;
    }
    if (isset($names)) {<commit_msg>Added second argument ($cm) to groupmode() calls in _get_available_students function.<commit_after><?php  // $Id$

/// Library of extra functions for the dialogue module

// SQL FUNCTIONS ///////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////
function dialogue_count_all_needing_replies_self($user = '') {
// count [conversations] needing replies [from] self for all dialogues
// function requested by <NAME> 17 Oct 2003 
    global $USER;
    
    if ($user) {    
        return count_records_select("dialogue_conversations", "(userid = $user->id OR 
            recipientid = $user->id) AND lastid != $user->id AND closed = 0");
    } else {
        return count_records_select("dialogue_conversations", "(userid = $USER->id OR 
            recipientid = $USER->id) AND lastid != $USER->id AND closed = 0");
    }
}


//////////////////////////////////////////////////////////////////////////////////////
function dialogue_count_closed($dialogue, $user) {
    
    return count_records_select("dialogue_conversations", "dialogueid = $dialogue->id AND 
        (userid = $user->id OR recipientid = $user->id) AND closed = 1");
    }


//////////////////////////////////////////////////////////////////////////////////////
function dialogue_count_open($dialogue, $user) {
    
    return count_records_select("dialogue_conversations", "dialogueid = $dialogue->id AND 
        (userid = $user->id OR recipientid = $user->id) AND closed = 0");
}


//////////////////////////////////////////////////////////////////////////////////////
function dialogue_count_needing_replies_other($dialogue, $user) {
// count [conversations] needing replies [from] other [person]  
    return count_records_select("dialogue_conversations", "dialogueid = $dialogue->id AND 
        (userid = $user->id OR recipientid = $user->id) AND lastid = $user->id AND closed = 0");
}


//////////////////////////////////////////////////////////////////////////////////////
function dialogue_count_needing_replies_self($dialogue, $user) {
// count [conversations] needing replies [from] self
    
    return count_records_select("dialogue_conversations", "dialogueid = $dialogue->id AND 
        (userid = $user->id OR recipientid = $user->id) AND lastid != $user->id AND closed = 0");
}


//////////////////////////////////////////////////////////////////////////////////////
function dialogue_get_participants($dialogueid) {
//Returns the users with data in one dialogue
//(users with records in dialogue_conversations, creators and receivers)

    global $CFG;

    //Get conversation's creators
    $creators = get_records_sql("SELECT DISTINCT u.*
                                FROM {$CFG->prefix}user u,
                                     {$CFG->prefix}dialogue_conversations c
                                WHERE c.dialogueid = '$dialogueid' and
                                      u.id = c.userid");

    //Get conversation's receivers
    $receivers = get_records_sql("SELECT DISTINCT u.*
                                FROM {$CFG->prefix}user u,
                                     {$CFG->prefix}dialogue_conversations c
                                WHERE c.dialogueid = '$dialogueid' and
                                      u.id = c.recipientid");

    //Add receivers to creators
    if ($receivers) {
        foreach ($receivers as $receiver) {
            $creators[$receiver->id] = $receiver;
        }
    }

    //Return creators array (it contains an array of unique users, creators and receivers)
    return ($creators);
} 

//////////////////////////////////////////////////////////////////////////////////////
function dialogue_get_available_users($dialogue) {

    if (! $course = get_record("course", "id", $dialogue->course)) {
        error("Course is misconfigured");
    }
    switch ($dialogue->dialoguetype) {
        case 0 : // teacher to student
            if (isteacher($course->id)) {
                return dialogue_get_available_students($dialogue);
            }
            else {
                return dialogue_get_available_teachers($dialogue);
            }
        case 1: // student to student
            if (isstudent($course->id)) {
                return dialogue_get_available_students($dialogue);
            }
            else {
                return;
            }
        case 2: // everyone
            if ($teachers = dialogue_get_available_teachers($dialogue)) {
                foreach ($teachers as $userid=>$name) {
                    $names[$userid] = $name;
                }
                $names[-1] = "-------------";
            }
            if ($students = dialogue_get_available_students($dialogue)) {
                foreach ($students as $userid=>$name) {
                    $names[$userid] = $name;
                }
            }
            if (isset($names)) {
                return $names;
            }
            return;
    }
}

                    
//////////////////////////////////////////////////////////////////////////////////////
function dialogue_get_available_students($dialogue) {
global $USER;
    
    if (! $course = get_record("course", "id", $dialogue->course)) {
        error("Course is misconfigured");
    }
    if (! $cm = get_coursemodule_from_instance("dialogue", $dialogue->id, $course->id)) {
        error("Course Module ID was incorrect");
    }
    
    $groupid = get_current_group($course->id);
    // add current group before list of students if it's the teacher
    if (isteacher($course->id) and groupmode($course, $cm)) {
        // show teacher their current group
        if ($groupid) {
            if (!$group = get_record("groups", "id", $groupid)) {
                error("Dialogue get available students: group not found");
            }
            $gnames["g$groupid"] = $group->name;
        } else { // all participants
            $gnames["g0"] = get_string("allparticipants");
        }
        $gnames["spacer"] = "------------";
    }
    // get the students on this course (default sort order)...
    if ($users = get_course_students($course->id)) {
        foreach ($users as $otheruser) {
            // ...exclude self and...
            if ($USER->id != $otheruser->id) {
                // ...if teacher and groups then exclude students not in the current group
                if (isteacher($course->id) and groupmode($course, $cm) and $groupid) {
                    if (!ismember($groupid, $otheruser->id)) {
                        continue;
                    }
                }
                // ...if student and groupmode is SEPARATEGROUPS then exclude students not in student's group
                if (isstudent($course->id) and (groupmode($course, $cm) == SEPARATEGROUPS)) {
                    if (!ismember($groupid, $otheruser->id)) {
                        continue;
                    }
                }
                // ... and any already in any open conversations unless multiple conversations allowed
                if ($dialogue->multipleconversations or count_records_select("dialogue_conversations", 
                        "dialogueid = $dialogue->id AND 
                        ((userid = $USER->id AND recipientid = $otheruser->id) OR 
                        (userid = $otheruser->id AND recipientid = $USER->id)) AND closed = 0") == 0) {
                    $names[$otheruser->id] = fullname($otheruser);
                }
            }
        }
    }
    if (isset($gnames)) {
        $list = $gnames;
    }
    if (isset($names)) {
{'fraction_non_alphanumeric': 0.21159190154823343, 'fraction_numerical': 0.0025142252216488023, 'mean_word_length': 1.4257743540362702, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 107, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '740984', 'n_tokens_mistral': 3882, 'n_tokens_neox': 3681, 'n_words': 937}