/** * Serves the files from the elang file areas * * @param stdClass $course the course object * @param stdClass $cm the course module object * @param stdClass $context the elang's context * @param string $filearea the name of the file area * @param array $args extra arguments (itemid, path) * @param boolean $forcedownload whether or not force download * @param array $options additional options affecting the file serving * * @return void * * @category files * * @since 0.0.1 */ function elang_pluginfile($course, $cm, $context, $filearea, array $args, $forcedownload, array $options = array()) { global $DB, $CFG, $USER; require_once dirname(__FILE__) . '/locallib.php'; if ($context->contextlevel != CONTEXT_MODULE) { send_file_not_found(); } require_login($course, true, $cm); if (!has_capability('mod/elang:view', $context)) { send_file_not_found(); } if ($filearea == 'subtitle') { $vtt = new \Captioning\Format\WebvttFile(); $idlang = $cm->instance; $records = $DB->get_records('elang_cues', array('id_elang' => $idlang), 'begin ASC'); $elang = $DB->get_record('elang', array('id' => $idlang)); $options = json_decode($elang->options, true); $repeatedunderscore = isset($options['repeatedunderscore']) ? $options['repeatedunderscore'] : 10; $i = 0; $users = $DB->get_records('elang_users', array('id_elang' => $idlang, 'id_user' => $USER->id), '', 'id_cue,json'); foreach ($records as $id => $record) { if (isset($users[$id])) { $data = json_decode($users[$id]->json, true); } else { $data = array(); } $cue = new \Captioning\Format\WebvttCue(\Captioning\Format\WebvttCue::ms2tc($record->begin), \Captioning\Format\WebvttCue::ms2tc($record->end), Elang\generateCueText(json_decode($record->json, true), $data, '-', $repeatedunderscore)); $i++; $cue->setIdentifier($i); $vtt->addCue($cue); } send_file($vtt->build()->getFileContent(), end($args), 0, 0, true, false, 'text/vtt'); } elseif ($filearea == 'pdf') { $idlang = $cm->instance; $records = $DB->get_records('elang_cues', array('id_elang' => $idlang), 'begin ASC'); $elang = $DB->get_record('elang', array('id' => $idlang)); $options = json_decode($elang->options, true); $repeatedunderscore = isset($options['repeatedunderscore']) ? $options['repeatedunderscore'] : 10; require_once $CFG->libdir . '/pdflib.php'; $doc = new pdf(); $doc->SetMargins(isset($options['left']) ? $options['left'] : 20, isset($options['top']) ? $options['top'] : 20); $doc->SetFont('', '', isset($options['size']) ? $options['size'] : 16); $doc->setPrintHeader(false); $doc->setPrintFooter(false); $doc->AddPage(); $doc->WriteHtml('<h1>' . sprintf(get_string('pdftitle', 'elang'), $course->fullname) . '</h1>'); $doc->WriteHtml('<h2>' . sprintf(get_string('pdfsubtitle', 'elang'), Elang\generateTitle($elang, $options), userdate($elang->timecreated, get_string('strftimedaydate'))) . '</h2>'); $doc->WriteHtml($elang->intro); $i = 1; foreach ($records as $id => $record) { $doc->Write(5, '', '', false, '', true); $doc->WriteHtml('<h3>' . sprintf(get_string('pdfcue', 'elang'), $i++, \Captioning\Format\WebvttCue::ms2tc($record->begin), \Captioning\Format\WebvttCue::ms2tc($record->end)) . '</h3>'); $doc->Write(5, Elang\generateCueText(json_decode($record->json, true), array(), '_', $repeatedunderscore), '', false, '', true); } send_file($doc->Output('', 'S'), end($args), 0, 0, true, false, 'application/pdf'); } else { $fs = get_file_storage(); $relativepath = implode('/', $args); $fullpath = rtrim('/' . $context->id . '/mod_elang/' . $filearea . '/0/' . $relativepath, '/'); $file = $fs->get_file_by_hash(sha1($fullpath)); if (!$file) { send_file_not_found(); } send_stored_file($file, 86400, 0, $forcedownload, $options); } }
} // Log action $help_id = $DB->insert_record('elang_help', array('id_elang' => $elang->id, 'cue' => $cue->number, 'guess' => $elements[$number]['order'], 'info' => $elements[$number]['content'])); if (version_compare($version, '2.7') < 0) { add_to_log($course->id, 'elang', 'view help', 'view.php?id=' . $cm->id, $help_id, $cm->id); } else { $event = \mod_elang\event\help_viewed::create(array('objectid' => $help_id, 'context' => $context, 'courseid' => $course->id, 'other' => array('cue' => $cue->number, 'guess' => $elements[$number]['order'], 'info' => $elements[$number]['content']))); $event->trigger(); } if ($user) { $data[$number] = array('help' => true, 'content' => ''); $user->json = json_encode($data); $DB->update_record('elang_users', $user); } else { $data = array($number => array('help' => true, 'content' => '')); $DB->insert_record('elang_users', array('id_cue' => $id_cue, 'id_elang' => $elang->id, 'id_user' => $USER->id, 'json' => json_encode($data))); } // Check completion $completion = new completion_info($course); if ($completion->is_enabled($cm)) { $completion->update_state($cm, COMPLETION_COMPLETE); } // Send the response $cue_text = Elang\generateCueText($elements, $data, '-', $repeatedunderscore); Elang\sendResponse(array('cue' => $cue_text, 'content' => $elements[$number]['content'])); break; default: header('HTTP/1.1 400 Bad Request'); die; break; }