public function toSelect($returnClassedHTML = false, $includeSkillGaps = false, $showQuestions = false, $iterator = false, $lessons = false, $courses = false)
 {
     if (!$iterator) {
         $iterator = new EfrontNodeFilterIterator(new RecursiveIteratorIterator(new RecursiveArrayIterator($this->tree), RecursiveIteratorIterator::SELF_FIRST));
     }
     if ($lessons === false) {
         //If a lessons list is not specified, get all active lessons
         if ($showQuestions) {
             $result = eF_getTableData("lessons l JOIN questions q ON q.lessons_ID = l.id", "l.id, l.name, count(q.id) as questions", "q.type <> 'raw_text' AND l.archive = 0 AND l.active=1", "", "l.name");
         } else {
             $result = eF_getTableData("lessons", "*", "archive = 0 && active=1");
             //Get all lessons at once, thus avoiding looping queries
         }
         foreach ($result as $value) {
             $value['name'] = str_replace("'", "&#039;", $value['name']);
             $lessons[$value['id']] = new EfrontLesson($value);
             //Create an array of EfrontLesson objects
         }
     }
     $directionsQuestions = array();
     $directionsLessons = array();
     foreach ($lessons as $id => $lesson) {
         if (!$lesson->lesson['active']) {
             //Remove inactive lessons
             unset($lessons[$id]);
         } elseif (!$lesson->lesson['course_only']) {
             //Lessons in courses will be handled by the course's display method, so remove them from the list
             $directions_ID = $lesson->lesson['directions_ID'];
             $directionsLessons[$directions_ID][] = $id;
             //Create an intermediate array that maps lessons to directions
             if ($showQuestions) {
                 if (isset($directionsQuestions[$directions_ID])) {
                     $directionsQuestions[$directions_ID] += $lesson->lesson['questions'];
                 } else {
                     $directionsQuestions[$directions_ID] = $lesson->lesson['questions'];
                 }
             }
         }
     }
     if ($courses === false) {
         //If a courses list is not specified, get all active courses
         if ($showQuestions) {
             $resultQuestions = eF_getTableData("courses c JOIN lessons_to_courses lc ON lc.courses_ID = c.id JOIN questions q ON q.lessons_ID = lc.lessons_ID", "c.id, count(q.id) as questions", "q.type <> 'raw_text' AND c.archive = 0 AND c.active=1", "", "c.name");
             $coursesQuestions = array();
             foreach ($resultQuestions as $resultQs) {
                 $coursesQuestions[$resultQs['id']] = $resultQs['questions'];
             }
         }
         $result = eF_getTableData("courses", "*", "archive = 0 AND active=1");
         //Get all courses at once, thus avoiding looping queries
         foreach ($result as $value) {
             $value['name'] = str_replace("'", "&#039;", $value['name']);
             $value['questions'] = $coursesQuestions[$value['id']] != "" ? $coursesQuestions[$value['id']] : 0;
             // 0 + to cast empty values to 0
             $courses[$value['id']] = new EfrontCourse($value);
             //Create an array of EfrontCourse objects
         }
     }
     $directionsCourses = array();
     foreach ($courses as $id => $course) {
         if (!$course->course['active']) {
             //Remove inactive courses
             unset($courses[$id]);
         } else {
             $directions_ID = $course->course['directions_ID'];
             $directionsCourses[$directions_ID][] = $id;
             //Create an intermediate array that maps courses to directions
             if ($showQuestions) {
                 if (isset($directionsQuestions[$directions_ID])) {
                     $directionsQuestions[$directions_ID] += $course->course['questions'];
                 } else {
                     $directionsQuestions[$directions_ID] = $course->course['questions'];
                 }
             }
         }
     }
     //We need to calculate which directions will be displayed. We will keep only directions that have lessons or courses and their parents. In order to do so, we traverse the directions tree and set the 'hasNodes' attribute to the nodes that will be kept
     foreach ($iterator as $key => $value) {
         if (isset($directionsLessons[$value['id']]) || isset($directionsCourses[$value['id']])) {
             $count = $iterator->getDepth();
             $value['hasNodes'] = true;
             isset($directionsLessons[$value['id']]) ? $value['lessons'] = $directionsLessons[$value['id']] : null;
             //Assign lessons ids to the direction
             isset($directionsCourses[$value['id']]) ? $value['courses'] = $directionsCourses[$value['id']] : null;
             //Assign courses ids to the direction
             while ($count) {
                 $node = $iterator->getSubIterator($count--);
                 $node['hasNodes'] = true;
                 //Mark "keep" all the parents of the node
             }
         }
     }
     $iterator = new EfrontNodeFilterIterator($iterator, array('hasNodes' => true));
     //Filter in only tree nodes that have the 'hasNodes' attribute
     $iterator->rewind();
     $current = $iterator->current();
     // pr($current);
     $current_level_father = 0;
     $treeArray = array();
     if ($includeSkillGaps) {
         $treeArray['lesson_0'] = _SKILLGAPTESTS;
     }
     $offset = "";
     while ($iterator->valid()) {
         $children = array();
         //The $children array is used so that when collapsing a direction, all its children disappear as well
         foreach (new EfrontNodeFilterIterator(new ArrayIterator($this->getNodeChildren($current), RecursiveIteratorIterator::SELF_FIRST)) as $key => $value) {
             $children[] = $key;
         }
         if ($offset != "") {
             $treeArray['direction_' . $current['id']] = str_replace("'", "&#039;", $offset . " " . $current['name']);
         } else {
             $treeArray['direction_' . $current['id']] = str_replace("'", "&#039;", $current['name']);
         }
         if (sizeof($current['lessons']) > 0) {
             foreach ($current->offsetGet('lessons') as $lessonId) {
                 $treeArray['lesson_' . $current['id'] . '_' . $lessonId] = str_replace("'", "&#039;", $offset . "- " . $lessons[$lessonId]->lesson['name']);
             }
         }
         if (sizeof($current['courses']) > 0) {
             foreach ($current->offsetGet('courses') as $courseId) {
                 $coursesArray = $courses[$courseId]->toSelect();
                 $first = 1;
                 foreach ($coursesArray as $courseId => $courseName) {
                     // The first result is the name of the course - the rest lesson names
                     // We need this distinction to have different keys (starting with course_ or lesson_ correctly
                     if ($first) {
                         $treeArray['course_' . $current['id'] . '_' . $courseId . "_" . $courseId] = str_replace("'", "&#039;", $offset . "-" . $courseName);
                         $first = 0;
                     } else {
                         $treeArray['lesson_' . $current['id'] . '_' . $courseId . "_" . $courseId] = str_replace("'", "&#039;", $offset . "-" . $courseName);
                     }
                 }
             }
         }
         $iterator->next();
         $current = $iterator->current();
         // $current
         if ($current['parent_direction_ID'] != $current_level_father) {
             $offset .= "-";
             $current_level_father = $current['parent_direction_ID'];
         }
     }
     if ($returnClassedHTML) {
         $htmlString = '<select id= "educational_criteria_row" name ="educational_criteria_row" onchange="createQuestionsSelect(this)" mySelectedIndex = "0">';
         if ($showQuestions) {
             $result = eF_getTableData("questions", "lessons_ID, count(lessons_ID) as quests", "type <> 'raw_text'", "", "lessons_ID");
             $lessonQuestions = array();
             foreach ($result as $lesson) {
                 if ($lesson['quests'] > 0) {
                     $lessonQuestions[$lesson['lessons_ID']] = $lesson['quests'];
                 }
             }
         }
         foreach ($treeArray as $key => $value) {
             $extras = " ";
             $htmlString .= '<option';
             if (strpos($key, "direction_") === 0) {
                 $directions_ID = strrchr($key, "_");
                 $htmlString .= ' value = "direction' . $directions_ID . '" style="background-color:maroon; color:white"';
                 $course_ID = substr($directions_ID, 1);
                 if ($showQuestions) {
                     $questions = $directionsQuestions[$directions_ID];
                     if ($questions) {
                         $extras = ' (' . $questions . ')';
                     } else {
                         $extras = ' (0)';
                     }
                 }
             } else {
                 if (strpos($key, "course_") === 0) {
                     $course_ID = strrchr($key, "_");
                     $htmlString .= ' value = "course' . $course_ID . '" style="background-color:green; color:white"';
                     $course_ID = substr($course_ID, 1);
                     if ($showQuestions) {
                         $questions = $courses[$course_ID]->course['questions'];
                         if ($questions) {
                             $extras = ' (' . $questions . ')';
                         } else {
                             $extras = ' (0)';
                         }
                     }
                 } else {
                     $htmlString .= ' value = "lesson' . strrchr($key, "_") . '" ';
                     if ($showQuestions) {
                         $lessonId = substr(strrchr($key, "_"), 1);
                         if ($showQuestions) {
                             if ($lessonQuestions[$lessonId]) {
                                 $extras .= "(" . $lessonQuestions[$lessonId] . ")";
                             } else {
                                 $extras .= "(0)";
                             }
                         }
                     }
                 }
             }
             $htmlString .= ">" . $value . $extras . "</option>";
         }
         $htmlString .= "</select>";
         // If no lessons or anything is found, then an empty select or array should be returned
         return $htmlString;
     }
     return $treeArray;
 }
Exemple #2
0
 /**
  * Get node ancestors
  *
  * This function is used to get the node ancestors
  * <br/>Example:
  * <code>
  * </code>
  *
  * @param mixed $node Either the node id or an EfrontNode object
  * @return array An array of array obhects, ancestors of the
  */
 public function getNodeAncestors($node)
 {
     $node instanceof ArrayObject ? $nodeId = $node['id'] : ($nodeId = $node);
     $iterator = new EfrontNodeFilterIterator(new RecursiveIteratorIterator($this->tree, RecursiveIteratorIterator::SELF_FIRST));
     //Get an iterator for the current tree. This iterator returns only whole node arrays and not node members separately (such as id, timestamp etc)
     $iterator->rewind();
     //Initialize iterator
     while ($iterator->valid() && $iterator->key() != $nodeId) {
         //Forward iterator index until you reach the designated element, which has an index equal to the node id that will be removed
         $iterator->next();
     }
     if ($iterator->valid()) {
         $parents[] = $this->filterOutChildren($iterator->current());
         for ($i = $iterator->getDepth(); $i > 0; $i--) {
             //Climb up the iterators and keep ancestor ids
             $parents[] = $this->filterOutChildren($iterator->getSubIterator($i));
             //Get the corresponding nodes
         }
     } else {
         $parents = array();
     }
     return $parents;
 }