/** * Migrate geogebra package to new area if found * * @return */ function geogebra_migrate_files() { global $CFG, $DB; $fs = get_file_storage(); $sqlfrom = "FROM {geogebra} j\n JOIN {modules} m ON m.name = 'geogebra'\n JOIN {course_modules} cm ON (cm.module = m.id AND cm.instance = j.id)"; $count = $DB->count_records_sql("SELECT COUNT('x') {$sqlfrom}"); $rs = $DB->get_recordset_sql("SELECT j.id, j.url, j.course, cm.id AS cmid {$sqlfrom} ORDER BY j.course, j.id"); if ($rs->valid()) { $pbar = new progress_bar('migrategeogebrafiles', 500, true); $i = 0; foreach ($rs as $geogebra) { $i++; upgrade_set_timeout(180); // set up timeout, may also abort execution $pbar->update($i, $count, "Migrating geogebra files - {$i}/{$count}."); $context = context_module::instance($geogebra->cmid); $coursecontext = context_course::instance($geogebra->course); if (!geogebra_is_valid_external_url($geogebra->url)) { // first copy local files if found - do not delete in case they are shared ;-) $geogebrafile = clean_param($geogebra->url, PARAM_PATH); $pathnamehash = sha1("/{$coursecontext->id}/course/legacy/0/{$geogebrafile}"); if ($file = $fs->get_file_by_hash($pathnamehash)) { $file_record = array('contextid' => $context->id, 'component' => 'mod_geogebra', 'filearea' => 'content', 'itemid' => 0, 'filepath' => '/'); try { $fs->create_file_from_storedfile($file_record, $file); } catch (Exception $x) { // ignore any errors, we can not do much anyway } $geogebra->url = $pathnamehash; } else { $geogebra->url = ''; } $DB->update_record('geogebra', $geogebra); } } } $rs->close(); }
/** * Execute Javascript that is embedded in the geogebra file, if it exists * File must be named geogebra_javascript.js * @param object $context Of the activity to get the files * @param object $geogebra object with the activity info */ function geogebra_get_js_from_geogebra($context, $geogebra) { global $CFG; $content = false; if (geogebra_is_valid_external_url($geogebra->url)) { require_once "{$CFG->libdir}/filestorage/zip_packer.php"; // Prepare tmp dir (create if not exists, download ggb file...) $dirname = 'mod_geogebra_' . time(); $tmpdir = make_temp_directory($dirname); if (!$tmpdir) { debugging("Cannot create temp directory {$dirname}"); return; } $materialid = geogebra_get_id($geogebra->url); if ($materialid) { $ggbfile = "http://tube.geogebra.org/material/download/format/file/id/{$materialid}"; } else { $ggbfile = $geogebra->url; } $filename = pathinfo($ggbfile, PATHINFO_FILENAME); $tmpggbfile = tempnam($tmpdir, $filename . '_'); // Download external GGB and extract javascript file if (!download_file_content($ggbfile, null, null, false, 300, 20, false, $tmpggbfile)) { debugging("Error copying from {$ggbfile}"); return; } // Extract geogebra js from GGB file $zip = new zip_packer(); $extract = $zip->extract_to_pathname($tmpggbfile, $tmpdir, array('geogebra_javascript.js')); if ($extract && $extract['geogebra_javascript.js']) { unlink($tmpggbfile); } else { @unlink($tmpggbfile); @rmdir($tmpdir); debugging("Cannot open zipfile {$tmpggbfile}"); return; } $content = file_get_contents($tmpdir . '/geogebra_javascript.js'); // Delete temporary files unlink($tmpdir . '/geogebra_javascript.js'); rmdir($tmpdir); } else { $fs = get_file_storage(); $file = $fs->get_file($context->id, 'mod_geogebra', 'extracted_files', 0, '/', 'geogebra_javascript.js'); if ($file) { $content = $file->get_content(); } } if (empty($content)) { debugging("Empty content"); return; } echo '<script type="text/javascript"> if (typeof ggbApplet == \'undefined\') { ggbApplet = document.ggbApplet; } ' . $content . '</script>'; }
function set_data($values) { global $CFG; $values = (array) $values; if (isset($values['url'])) { // Need to translate the "url" field if (geogebra_is_valid_external_url($values['url'])) { $values['filetype'] = GEOGEBRA_FILE_TYPE_EXTERNAL; $values['geogebraurl'] = $values['url']; } else { $values['filetype'] = GEOGEBRA_FILE_TYPE_LOCAL; $values['geogebrafile'] = $values['url']; } // Load attributes parse_str($values['attributes'], $attributes); $values['enableRightClick'] = isset($attributes['enableRightClick']) ? $attributes['enableRightClick'] : 0; $values['enableLabelDrags'] = isset($attributes['enableLabelDrags']) ? $attributes['enableLabelDrags'] : 0; $values['showResetIcon'] = isset($attributes['showResetIcon']) ? $attributes['showResetIcon'] : 0; $values['showMenuBar'] = isset($attributes['showMenuBar']) ? $attributes['showMenuBar'] : 0; $values['showToolBar'] = isset($attributes['showToolBar']) ? $attributes['showToolBar'] : 0; $values['showToolBarHelp'] = isset($attributes['showToolBarHelp']) ? $attributes['showToolBarHelp'] : 0; $values['showAlgebraInput'] = isset($attributes['showAlgebraInput']) ? $attributes['showAlgebraInput'] : 0; $values['language'] = isset($attributes['language']) ? $attributes['language'] : 0; } unset($values['url']); $this->data_preprocessing($values); parent::set_data($values); }