lundi 24 septembre 2018

Adding students to a virtual classroom

In my web app, there are 2 kinds of users: Teachers and Students

The Teachers:
- can create classrooms with unique classroom ID
- can view his/her students in each classroom he/she created

The Students
- can join a classroom/s created by a Teacher using classroom IDs
- can view classmates from each he/she joined

For the Teacher I used these codes to create classrooms :

function classcreation(q)
{
    var usuid = generateId();
    var myClasses={};
    myClasses.TheClass = document.getElementById('classroomName').value;
    myClasses.Teacher = user.displayName;
    myClasses.TeacherID = user.uid;
    myClasses.ClassID = usuid;
    fbclass.child(user.uid).push().set(myClasses);
}

And these codes to view each user.teachers created classrooms:

var userRef = firebase.database().ref().child('Classes' + '/' + user.uid);
userRef.on('child_added', function(data)
{
    var roomNames = data.val().TheClass;
    var Studentx = data.val().TheStudents;
    var classD = data.val().ClassID;
    var ul = document.createElement('ul');
    document.getElementById('myList').appendChild(ul);
    var li = document.createElement('li');
    ul.appendChild(li);

    Object.keys(roomNames).forEach(function(key)
    {
        li.innerHTML += '<span 
            onclick="clickDone(this)">'+roomNames[key]+'</span> 
            <ul style="display:none"><li>Class Id : 
            '+classD+'</li><li>Students : '+Studentx+'</li> 
            </ul>';
    });
});

I attempted to use these codes for user.students to join a classroom

function addclass() {
    var addclassID = document.getElementById("classroomID").value;
    var checkclassID = firebase.database().ref().child('Classes' + '/' + 
    user.uid);

    userRef.on('child_added', function(data)
    {
        var studentx = data.val().TheStudents;
        var clazzID = data.val().ClassID;
        Object.keys(clazzID).forEach(function(key)
        {
            if(clazzID[key] == addclassID) {
                var myClass{};
                myClass.Studentname= user.displayName;
                checkclassID.child("MyStudents").set(myClass);
            } 
            else
            {
                alert("Class doesnt exist!");
            }
        });
    }}

I believe the codes below is incorrect:

Object.keys(clazzID).forEach(function(key)
{
    if(clazzID[key] == addclassID) {
        var myClass{};
        myClass.Studentname= user.displayName;
        checkclassID.child("MyStudents").set(myClass);
    } 

What did I miss? What should be the alternative way/codes for students to join a particular class?

Class Image

So, under the Classes is the user.uid, under the user.uid is a random id sent by the push(), below it are the information of a classroom such as ClassID, Teacher, TeacherID, and TheClass which is the classname. If a user.student successfully joined a classroom,a MyStudents should appear on the same level of the information: => ClassID,Teacher,TeacherID,TheClass, MyStudent. Under the MyStudents, it is where the data of the user.student should be place after he/she joined a classroom.

For more clarifications just comment it down below. Any help would be deeply appreciated.




Aucun commentaire:

Enregistrer un commentaire