$query = "\tSELECT a.*, COALESCE(b.`objective_details`,a.`objective_description`) AS `objective_description`, COALESCE(b.`objective_type`,c.`objective_type`) AS `objective_type`,\n                                    b.`importance`,c.`objective_details`, COALESCE(c.`eobjective_id`,0) AS `mapped`,\n                                    COALESCE(b.`cobjective_id`,0) AS `mapped_to_course`\n                                    FROM `global_lu_objectives` a\n                                    LEFT JOIN `course_objectives` b\n                                    ON a.`objective_id` = b.`objective_id`\n                                    AND b.`active` = '1'\n                                    AND b.`course_id` = " . $db->qstr($COURSE_ID) . "\n                                    LEFT JOIN `event_objectives` c\n                                    ON c.`objective_id` = a.`objective_id`\n                                    AND c.`event_id` = " . $db->qstr($EVENT_ID) . "\n                                    WHERE a.`objective_active` = '1'\n                                    AND (c.`event_id` = " . $db->qstr($EVENT_ID) . " OR b.`course_id` = " . $db->qstr($COURSE_ID) . ")\n                                    GROUP BY a.`objective_id`\n                                    ORDER BY a.`objective_id` ASC";
 $mapped_objectives = $db->GetAll($query);
 $primary = false;
 $secondary = false;
 $tertiary = false;
 $hierarchical_objectives = array();
 $flat_objectives = array();
 $explicit_event_objectives = false;
 //array();
 $mapped_event_objectives = array();
 if ($mapped_objectives) {
     foreach ($mapped_objectives as $objective) {
         //if its mapped to the event, but not the course, then it belongs in the event objective list
         //echo $objective["objective_name"].' is '.$objective["mapped"].' and '.$objective["mapped_to_course"]."<br/>";
         if ($objective["mapped"] && !$objective["mapped_to_course"]) {
             if (!event_objective_parent_mapped_course($objective["objective_id"], $EVENT_ID)) {
                 $explicit_event_objectives[] = $objective;
             } else {
                 if ($objective["objective_type"] == "course") {
                     //$objective_id = $objective["objective_id"];
                     $hierarchical_objectives[] = $objective;
                 } else {
                     $flat_objectives[] = $objective;
                 }
             }
         } else {
             if ($objective["objective_type"] == "course") {
                 //$objective_id = $objective["objective_id"];
                 $hierarchical_objectives[] = $objective;
             } else {
                 $flat_objectives[] = $objective;
 $qu_arr[1] .= "\t\tJOIN `objective_organisation` AS c ON a.`objective_id` = c.`objective_id` ";
 $qu_arr[2] = "\t\tWHERE a.`objective_parent` = " . $db->qstr($id) . " \n\t\t\t\t\t\tAND a.`objective_active` = '1'\n\t\t\t\t\t\tAND c.`organisation_id` = " . $db->qstr($org_id);
 $qu_arr[4] = "\t\tORDER BY a.`objective_order`";
 $query = implode(" ", $qu_arr);
 $objectives = $db->GetAll($query);
 if ($objectives) {
     $obj_array = array();
     foreach ($objectives as $objective) {
         $fields = array('objective_id' => $objective["objective_id"], 'objective_code' => $objective["objective_code"], 'objective_name' => $objective["objective_name"], 'objective_description' => $objective["objective_description"]);
         if ($course_id || $event_id || $assessment_id || $objective_ids_string) {
             $fields["mapped"] = $objective["mapped"];
             if ($course_id) {
                 $fields["child_mapped"] = course_objective_has_child_mapped($objective["objective_id"], $course_id, true);
             } else {
                 if ($event_id) {
                     $fields["child_mapped"] = event_objective_parent_mapped_course($objective["objective_id"], $event_id, true);
                 } else {
                     if ($assessment_id) {
                         $fields["child_mapped"] = assessment_objective_parent_mapped_course($objective["objective_id"], $assessment_id, true);
                     }
                 }
             }
         }
         $query = "\tSELECT a.* FROM `global_lu_objectives` AS a\n\t\t\t\t\t\tJOIN `objective_organisation` AS b ON a.`objective_id` = b.`objective_id`\n\t\t\t\t\t\tWHERE a.`objective_parent` = " . $db->qstr($objective["objective_id"]) . "\n\t\t\t\t\t\tAND b.`organisation_id` = " . $db->qstr($org_id);
         $fields["has_child"] = $db->GetAll($query) ? true : false;
         $obj_array[] = $fields;
     }
     echo json_encode($obj_array);
 } else {
     echo json_encode(array('error' => 'No child objectives found for the selected objective.'));
 }