如何让我的用户学生查看他/她所属的所有教室?

时间:2018-09-27 06:58:08

标签: javascript firebase web firebase-realtime-database

在我的网络应用中,有两种类型的用户:教师学生

老师可以创建虚拟教室并查看加入他/她房间的学生

学生可以加入教室,并且应该能够看到他/她加入的教室。

这些代码允许教师创建教室:

function classcreation(q)
    {
        var checkcn = document.getElementById('classroomName').value;
        if(checkcn == "" && checkcn == null){
            alert("Empty Class Name!!");
            }
            else {
              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);
                }
      }

,这些内容允许教师查看他/她创建的教室以及加入他/她房间的学生

var userRef = firebase.database().ref().child('Classes' + '/' + user.uid);
      userRef.on('child_added', function(data)
      {
            var roomNames = data.val().TheClass;
            var Studentx = data.val().MyStudents;

            var studentRawList = '';
            for (var key in Studentx) {
                studentRawList += ('['+Studentx[key].Studentname + ']');
            }

            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><span onclick="clickDone(this)">Students : 
                             </span><ul style="display:none">
                             <li>'+studentRawList+'</li></ul></li></ul>';
              });
            });

JSON:

{
  "Accounts" : {
    "FykyhzEZjndylFj3BbCnPqoTGDo1" : {
      "displayName" : "Dodong Advices",
      "email" : "advicenidodong@gmail.com",
      "status" : "Teacher"
    },
    "HOgdSlTed9V8g0kSZjizgODMDOe2" : {
      "displayName" : "Sweet Macaroni",
      "email" : "Sweet@gmail.com",
      "status" : "Student"
    },
    "yJif4ReTxCcGmo682xWSG3L5MKE3" : {
      "displayName" : "Purple Salad",
      "email" : "Purple@gmail.com",
      "status" : "Student"
    }
  },
  "Classes" : {
    "FykyhzEZjndylFj3BbCnPqoTGDo1" : {
      "-LMpvlBl3mEazhxaJwqb" : {
        "ClassID" : "6503-3503-6827",
        "Teacher" : "Dodong Advices",
        "TeacherID" : "FykyhzEZjndylFj3BbCnPqoTGDo1",
        "TheClass" : "StackMates"
      },
      "-LMrfIBg8v-hj1k8X2Qf" : {
        "ClassID" : "7583-2402-2757",
        "MyStudents" : {
          "HOgdSlTed9V8g0kSZjizgODMDOe2" : {
            "Studentname" : "Sweet Macaroni"
          }
        },
        "Teacher" : "Dodong Advices",
        "TeacherID" : "FykyhzEZjndylFj3BbCnPqoTGDo1",
        "TheClass" : "asdasd"
      },
      "-LMrfMV1aw3YNA0PfooR" : {
        "ClassID" : "8083-2712-3347",
        "MyStudents" : {
          "HOgdSlTed9V8g0kSZjizgODMDOe2" : {
            "Studentname" : "Sweet Macaroni"
          },
          "yJif4ReTxCcGmo682xWSG3L5MKE3" : {
            "Studentname" : "Purple Salad"
          }
        },
        "Teacher" : "Dodong Advices",
        "TeacherID" : "FykyhzEZjndylFj3BbCnPqoTGDo1",
        "TheClass" : "Trial"
      }
    }
  }
 }

因此,基于JSON,我们有2个 Student 学生,名为:甜通心粉和Purple Salad。 甜通心粉加入了 asdasd 试验班,而紫色沙拉仅加入了课堂试验 >

问题是,如何让每个用户学生看到/查看他们所属的房间?

也尝试了这些代码,但是没有用:

  var studentRef = firebase.database().ref().child('Classes' + '/' + 
                                                   user.uid);
              studentRef.on('child_added', function(data)
              {
                    var roomNamess = data.val().TheClass;
                    var Studentxx = data.val().MyStudents;

                    var studentRawLists = '';
                    for (var keys in Studentxx) {
                      if(studentxx[keys].Studentname  == user.displayName){
                        studentRawLists += 
                                    ('['+Studentxx[keys].Studentname + ']');

                    }
                  }
                    var classDD = data.val().ClassID;
                    var ull = document.createElement('ul');
                    document.getElementById('myLista').appendChild(ull);
                    var lii = document.createElement('li');
                    ull.appendChild(lii);

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

1 个答案:

答案 0 :(得分:0)

{
    "Classes": {
        "FykyhzEZjndylFj3BbCnPqoTGDo1": {

                "students": [
                    "-LMpvlBl3mEazhxaJwqb",
                    "-LMrfIBg8v-hj1k8X2Qf",
                    "-LMrfMV1aw3YNA0PfooR"
                ],

                "staff": [
                    "-LMpvlBsadsadsdadsad"
                ],

                "details": {
                    "title": "Leaning English",
                    "room_number": "L32",
                    "time_start": "14:00",
                    "time_end": "17:00"
                }

        }
    }
}

这取决于您查询数据的方式,但我想学生(无论是学生还是教师)都希望看到他们分配的房间,因此您应该在每个班级中扫描用户uid。

在NoSQL中,您不应该担心在不同位置多次记录数据,以节省其他查询。

更新了Json的解释。

让我为您识字json。

for(i in this.data){

  var uid = user.uid;

  var student = this.data[i].val.students;

  for(i2 in student){

    if(student[i2] == uid){

      //student assigned to room
      console.log(this.data[i].val.details);

    }

  }

}