def update_hierarchy_with_profile_data()

in common/src/common/utils/parent_child_nodes_handler.py [0:0]


  def update_hierarchy_with_profile_data(cls, learner_profile, node_dict,
                                         collection_type, doc_id,
                                         parent_id=None):
    """This function will find the correct data for a Node Item in LOS in
    LearnerProfile and update it in the hierarchy"""
    # Logic to check if the item is locked/unlocked
    # Update the is_locked flag and progress flag from LearnerProfile
    if learner_profile is not None and learner_profile.progress is not None\
        and collection_type in LOS_COLLECTIONS:

      is_hidden = learner_profile.progress.get(
            collection_type, {}).get(doc_id,
                                    {}).get("is_hidden",
                                            node_dict.get("is_hidden"))
      is_locked = learner_profile.progress.get(
          collection_type, {}).get(doc_id,
                                   {}).get("is_locked",
                                           node_dict.get("is_locked"))
      is_optional = learner_profile.progress.get(
          collection_type, {}).get(doc_id,
                                   {}).get("is_optional",
                                           node_dict.get("is_optional"))
      instruction_completed = learner_profile.progress.get(
          collection_type, {}).get(doc_id,
                                   {}).get("instruction_completed",
                                           node_dict.get(
                                          "instruction_completed"))
      progress_parent = learner_profile.progress.get(
        collection_type, {}).get(doc_id, {}).get("parent_node", "")
      if not progress_parent:
        progress_parent = ""

      # FIXME: this is done to add ungate flag for first LR in project
      # this flags specifies whether cognitive wrapper is unlocked
      # Ticket 4928
      if collection_type == "learning_resources" and node_dict["order"] == 1:
        node = collection_references["learning_resources"].find_by_uuid(doc_id)
        module = collection_references["learning_objects"]\
          .find_by_uuid(node.parent_nodes["learning_objects"][0])
        if module.type == "project":
          cw_is_locked = True
          unit = collection_references["learning_experiences"].find_by_uuid(
            module.parent_nodes["learning_experiences"][0])
          modules = unit.child_nodes
          for level in modules:
            for module_id in modules[level][::-1]:
              neighbor = collection_references["learning_objects"].find_by_uuid(
                module_id)
              if neighbor.type == "cognitive_wrapper":
                cw_is_locked = learner_profile.progress.get("learning_objects",
                        {}).get(neighbor.id, {}).get("is_locked", True)
                break
          node_dict["ungate"] = not cw_is_locked

      if not (progress_parent != parent_id and node_dict["type"] == "srl"
              and node_dict["alias"] == "module" and parent_id):
        node_dict["is_hidden"] = is_hidden
        node_dict["is_locked"] = is_locked
        node_dict["is_optional"] = is_optional
        node_dict["instruction_completed"] = instruction_completed
        if collection_type == "assessments":
          node_dict["num_attempts"] = learner_profile.progress.get(
        collection_type, {}).get(doc_id, {}).get("num_attempts", 0)
      node_dict["progress"] = learner_profile.progress.get(
          collection_type, {}).get(doc_id, {}).get("progress", 0)
      node_dict["status"] = learner_profile.progress.get(
          collection_type, {}).get(doc_id, {}).get("status", "not_attempted")
      node_dict["last_attempted"] = learner_profile.progress.get(
        collection_type, {}).get(doc_id, {}).get("last_attempted", "")
      node_dict["parent_node"] = learner_profile.progress.get(
        collection_type, {}).get(doc_id, {}).get("parent_node", "")
      child_count =learner_profile.progress.get(
        collection_type, {}).get(doc_id, {}).get("child_count", 0)
      if child_count == 0 and node_dict.get("child_nodes"):
        child_count = sum(len(node_dict.get(
          "child_nodes",{}).get(child_type,[]))
         for child_type in node_dict.get("child_nodes",{}))
      node_dict["child_count"] = child_count
      node_dict["completed_child_count"] = learner_profile.progress.get(
        collection_type, {}).get(doc_id, {}).get("completed_child_count", 0)

    if learner_profile is not None and learner_profile.achievements is not None\
         and collection_type == "curriculum_pathways":
      achievement_intersection = list(
          set(learner_profile.achievements)
          & set(node_dict.get("achievements", [])))
      node_dict["earned_achievements"] = []
      for achievement in achievement_intersection:
        node_dict["earned_achievements"].append(
            cls.get_document_from_collection("achievements", achievement))
    # Logic block to ungate assessments of type project
    # This is used to show the Mark as Complete button on the Intro LR of
    # the project module
    if learner_profile is not None and \
      node_dict["type"] == "project" and collection_type == "assessments":
      prereqs = node_dict.get("prerequisites", {})
      if prereqs:
        for k, v in prereqs.items():
          if k != "learning_resources":
            for id_ in v:
              if learner_profile.progress and \
                isinstance(learner_profile.progress, dict) and \
                learner_profile.progress.get(k, {}).get(id_, {}).get("status") \
                  == "completed" or \
                learner_profile.progress.get(k, {}).get(id_, {}).get("ungate") \
                  or (not learner_profile.progress.get(k, {}).get(id_, {}).get(
                "is_locked") and learner_profile.progress.get(k, {}).get(
                id_, {}).get("is_hidden")):
                node_dict["ungate"] = True
              else:
                node_dict["ungate"] = False
                break

    return node_dict