//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
/**
 * @package    block_course_overview_lite
 * @author     Josh Stagg
 * @copyright  2014 Josh Stagg <*****@*****.**>
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
require_once dirname(__FILE__) . '/../../config.php';
require_once dirname(__FILE__) . '/locallib.php';
require_sesskey();
require_login();
$togglecourse = required_param('toggle_hidden', PARAM_INT);
$hiddencourses = block_course_overview_lite_get_courses_hidden();
if (array_key_exists($togglecourse, $hiddencourses) && $hiddencourses[$togglecourse] == true) {
    unset($hiddencourses[$togglecourse]);
} else {
    $hiddencourses[$togglecourse] = true;
}
block_course_overview_lite_update_courses_hidden($hiddencourses);
/**
 * Return sorted list of user courses
 *
 * @return array list of sorted courses and count of courses.
 */
function block_course_overview_lite_get_sorted_courses($usenav = true)
{
    global $PAGE;
    $courses = array();
    $highlightprefix = get_config('block_course_overview_lite', 'highlightprefix');
    $highlightdelim = get_config('block_course_overview_lite', 'highlightdelim');
    $hiddencourses = block_course_overview_lite_get_courses_hidden();
    if ($usenav) {
        $PAGE->navigation->initialise();
        $navigation = clone $PAGE->navigation;
        foreach (array($navigation) as $item) {
            if (!$item->display && !$item->contains_active_node() || $item->type != navigation_node::TYPE_SYSTEM || empty($item->action)) {
                continue;
            }
            $my = $item->get('mycourses');
            if (!empty($my) && $my->children) {
                if ($my->forceopen) {
                    $collection = $my->find_all_of_type(navigation_node::TYPE_COURSE);
                    foreach ($collection as $coursenode) {
                        $course = new stdClass();
                        $course->id = $coursenode->key;
                        $course->fullname = $coursenode->title;
                        $course->shortname = $coursenode->shorttext;
                        $course->url = $coursenode->action;
                        $course->hidden = $coursenode->hidden;
                        $course->modinfo = '';
                        $course->sectioncache = '';
                        $course->current = !empty($highlightprefix) && block_course_overview_lite_current($highlightprefix, $highlightdelim, $course->shortname);
                        $course->userhidden = array_key_exists($course->id, $hiddencourses) && $hiddencourses[$course->id] == true;
                        $courses[$course->id] = $course;
                    }
                }
                break;
            }
        }
    }
    if (empty($courses)) {
        if ($usenav) {
            $PAGE->requires->string_for_js('move', 'moodle');
            $PAGE->requires->string_for_js('hide_icon_alt', 'block_course_overview_lite');
        } else {
            $rawcourses = enrol_get_my_courses();
            foreach ($rawcourses as $rawcourse) {
                $course = new stdClass();
                $course->id = $rawcourse->id;
                $course->fullname = $rawcourse->fullname;
                $course->shortname = $rawcourse->shortname;
                $url = new moodle_url('/course/view.php', array('id' => $rawcourse->id));
                $course->url = $url->out();
                $course->hidden = $rawcourse->visible == 0;
                $course->current = !empty($highlightprefix) && block_course_overview_lite_current($highlightprefix, $highlightdelim, $rawcourse->shortname);
                $course->userhidden = array_key_exists($course->id, $hiddencourses) && $hiddencourses[$course->id] == true;
                $courses[$course->id] = $course;
            }
        }
    }
    $courses = block_course_overview_lite_sort_courses($courses);
    return array($courses, count($courses), array_sum($hiddencourses));
}