/**
 * Given a course_module object, this function returns any
 * "extra" information that may be needed when printing
 * this activity in a course listing.
 *
 * See {@link get_array_of_activities()} in course/lib.php
 *
 * @param object $coursemodule
 * @return cached_cm_info info
 */
function practice_get_coursemodule_info($coursemodule)
{
    global $CFG, $DB;
    require_once "{$CFG->dirroot}/mod/practice/locallib.php";
    if (!($practice = $DB->get_record('practice', array('id' => $coursemodule->instance), 'id, name, display, displayoptions, externalpractice, parameters, intro, introformat'))) {
        return NULL;
    }
    $info = new cached_cm_info();
    $info->name = $practice->name;
    //note: there should be a way to differentiate links from normal resources
    $info->icon = practice_guess_icon($practice->externalpractice, 24);
    $display = practice_get_final_display_type($practice);
    if ($display == RESOURCELIB_DISPLAY_POPUP) {
        $fullpractice = "{$CFG->wwwroot}/mod/practice/view.php?id={$coursemodule->id}&redirect=1";
        $options = empty($practice->displayoptions) ? array() : unserialize($practice->displayoptions);
        $width = empty($options['popupwidth']) ? 620 : $options['popupwidth'];
        $height = empty($options['popupheight']) ? 450 : $options['popupheight'];
        $wh = "width={$width},height={$height},toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
        $info->onclick = "window.open('{$fullpractice}', '', '{$wh}'); return false;";
    } else {
        if ($display == RESOURCELIB_DISPLAY_NEW) {
            $fullpractice = "{$CFG->wwwroot}/mod/practice/view.php?id={$coursemodule->id}&redirect=1";
            $info->onclick = "window.open('{$fullpractice}'); return false;";
        }
    }
    if ($coursemodule->showdescription) {
        // Convert intro to html. Do not filter cached version, filters run at display time.
        $info->content = format_module_intro('practice', $practice, $coursemodule->id, false);
    }
    return $info;
}
// Update 'viewed' state if required by completion system
$completion = new completion_info($course);
$completion->set_module_viewed($cm);
$PAGE->set_practice('/mod/practice/view.php', array('id' => $cm->id));
// Make sure practice exists before generating output - some older sites may contain empty practices
// Do not use PARAM_practice here, it is too strict and does not support general URIs!
$exturl = trim($practice->externalurl);
if (empty($extpractice) or $extpractice === 'http://') {
    practice_print_header($practice, $cm, $course);
    practice_print_heading($practice, $cm, $course);
    practice_print_intro($practice, $cm, $course);
    notice(get_string('invalidstoredpractice', 'practice'), new moodle_practice('/course/practice.php', array('pid' => $practice->externalurl)));
    die;
}
unset($extpractice);
$displaytype = practice_get_final_display_type($practice);
if ($displaytype == RESOURCELIB_DISPLAY_OPEN) {
    // For 'open' links, we always redirect to the content - except if the user
    // just chose 'save and display' from the form then that would be confusing
    if (!isset($_SERVER['HTTP_REFERER']) || strpos($_SERVER['HTTP_REFERER'], 'modedit.php') === false) {
        $redirect = true;
    }
}
if ($redirect) {
    // coming from course page or practice index page,
    // the redirection is needed for completion tracking and logging
    $fullpractice = str_replace('&', '&', practice_get_full_practice($practice, $cm, $course));
    if (!course_get_format($course)->has_view_page()) {
        // If course format does not have a view page, add redirection delay with a link to the edit page.
        // Otherwise teacher is redirected to the external practice without any possibility to edit activity or course settings.
        $editpractice = null;
/**
 * Print practice info and link.
 * @param object $practice
 * @param object $cm
 * @param object $course
 * @return does not return
 */
function practice_print_workaround($practice, $cm, $course)
{
    global $OUTPUT;
    practice_print_header($practice, $cm, $course);
    practice_print_heading($practice, $cm, $course, true);
    practice_print_intro($practice, $cm, $course, true);
    $fullpractice = practice_get_full_practice($practice, $cm, $course);
    $display = practice_get_final_display_type($practice);
    if ($display == RESOURCELIB_DISPLAY_POPUP) {
        $jsfullpractice = addslashes_js($fullpractice);
        $options = empty($practice->displayoptions) ? array() : unserialize($practice->displayoptions);
        $width = empty($options['popupwidth']) ? 620 : $options['popupwidth'];
        $height = empty($options['popupheight']) ? 450 : $options['popupheight'];
        $wh = "width={$width},height={$height},toolbar=no,location=no,menubar=no,copyhistory=no,status=no,directories=no,scrollbars=yes,resizable=yes";
        $extra = "onclick=\"window.open('{$jsfullpractice}', '', '{$wh}'); return false;\"";
    } else {
        if ($display == RESOURCELIB_DISPLAY_NEW) {
            $extra = "onclick=\"this.target='_blank';\"";
        } else {
            $extra = '';
        }
    }
    echo '<div class="practiceworkaround">';
    print_string('clicktoopen', 'practice', "<a href=\"{$fullpractice}\" {$extra}>{$fullpractice}</a>");
    echo '</div>';
    echo $OUTPUT->footer();
    die;
}