function get_content() { global $CFG, $PAGE; if (!$CFG->usecomments) { $this->content->text = ''; if ($this->page->user_is_editing()) { $this->content->text = get_string('disabledcomments'); } return $this->content; } if ($this->content !== NULL) { return $this->content; } if (empty($this->instance)) { return null; } $this->content->footer = ''; $this->content->text = ''; list($context, $course, $cm) = get_context_info_array($PAGE->context->id); $args = new stdClass(); $args->context = $PAGE->context; $args->course = $course; $args->area = 'page_comments'; $args->itemid = 0; // set 'env' to tell moodle tweak ui for this block $args->env = 'block_comments'; $args->component = 'block_comments'; $args->linktext = get_string('showcomments'); $comment = new comment($args); $comment->set_view_permission(true); $this->content = new stdClass(); $this->content->text = $comment->output(true); $this->content->footer = ''; return $this->content; }
/** * Local Redirect * * This local plugin that adds a 'friendly url' version of Google analytics * to Moodle * * @package local * @subpackage local_googleanalytics * @copyright 2013 Bas Brands, www.basbrands.nl * @author Bas Brands and Gavin Henrick. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ function ga_trackurl() { global $CFG, $DB, $PAGE, $COURSE, $OUTPUT; $pageinfo = get_context_info_array($PAGE->context->id); $trackurl = array(); if ($COURSE->id == 1) { return ''; } // Adds course category name. if (isset($pageinfo[1]->category)) { if ($category = $DB->get_record('course_categories', array('id'=>$pageinfo[1]->category))) { $trackurl[] = urlencode($category->name); } } // Adds course full name. if (isset($pageinfo[1]->fullname)) { $trackurl[] = urlencode($pageinfo[1]->fullname); } // Adds activity name. if (isset($pageinfo[2]->name)) { $trackurl[] = urlencode($pageinfo[2]->name); } return implode('/', $trackurl); }
/** * Analytics * * This module provides extensive analytics on a platform of choice * Currently support Google Analytics and Piwik * * @package local_analytics * @copyright David Bezemer <*****@*****.**>, www.davidbezemer.nl * @author David Bezemer <*****@*****.**>, Bas Brands <*****@*****.**>, Gavin Henrick <*****@*****.**> * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ function analytics_trackurl() { global $DB, $PAGE; $pageinfo = get_context_info_array($PAGE->context->id); $trackurl = "'/"; // Adds course category name. if (isset($pageinfo[1]->category)) { if ($category = $DB->get_record('course_categories', array('id' => $pageinfo[1]->category))) { $cats = explode("/", $category->path); foreach (array_filter($cats) as $cat) { if ($categorydepth = $DB->get_record("course_categories", array("id" => $cat))) { $trackurl .= urlencode($categorydepth->name) . '/'; } } } } // Adds course full name. if (isset($pageinfo[1]->fullname)) { if (isset($pageinfo[2]->name)) { $trackurl .= urlencode($pageinfo[1]->fullname) . '/'; } else { if ($PAGE->user_is_editing()) { $trackurl .= urlencode($pageinfo[1]->fullname) . '/' . get_string('edit'); } else { $trackurl .= urlencode($pageinfo[1]->fullname) . '/' . get_string('view'); } } } // Adds activity name. if (isset($pageinfo[2]->name)) { $trackurl .= urlencode($pageinfo[2]->modname) . '/' . urlencode($pageinfo[2]->name); } $trackurl .= "'"; return $trackurl; }
/** * Get file listing * * @param string $encodedpath * @return mixed */ public function get_listing($encodedpath = '', $page = '') { global $CFG, $USER, $OUTPUT; $ret = array(); $ret['dynload'] = true; $ret['nosearch'] = true; $ret['nologin'] = true; $list = array(); if (!empty($encodedpath)) { $params = unserialize(base64_decode($encodedpath)); if (is_array($params)) { $component = is_null($params['component']) ? NULL : clean_param($params['component'], PARAM_COMPONENT); $filearea = is_null($params['filearea']) ? NULL : clean_param($params['filearea'], PARAM_AREA); $itemid = is_null($params['itemid']) ? NULL : clean_param($params['itemid'], PARAM_INT); $filepath = is_null($params['filepath']) ? NULL : clean_param($params['filepath'], PARAM_PATH); $filename = is_null($params['filename']) ? NULL : clean_param($params['filename'], PARAM_FILE); $context = get_context_instance_by_id(clean_param($params['contextid'], PARAM_INT)); } } else { $itemid = null; $filename = null; $filearea = null; $filepath = null; $component = null; if (!empty($this->context)) { list($context, $course, $cm) = get_context_info_array($this->context->id); if (is_object($course)) { $context = get_context_instance(CONTEXT_COURSE, $course->id); } else { $context = get_system_context(); } } else { $context = get_system_context(); } } $browser = get_file_browser(); $list = array(); if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) { // build file tree $element = repository_local_file::retrieve_file_info($fileinfo, $this); $nonemptychildren = $element->get_non_empty_children(); foreach ($nonemptychildren as $child) { $list[] = (array) $child->get_node(); } } else { // if file doesn't exist, build path nodes root of current context $fileinfo = $browser->get_file_info($context, null, null, null, null, null); } // build path navigation $ret['path'] = array(); $element = repository_local_file::retrieve_file_info($fileinfo, $this); for ($level = $element; $level; $level = $level->get_parent()) { if ($level == $element || !$level->can_skip()) { array_unshift($ret['path'], $level->get_node_path()); } } $ret['list'] = array_filter($list, array($this, 'filter')); return $ret; }
function question_pluginfile_joomdle($course, $context, $component, $filearea, $args, $forcedownload) { global $DB, $CFG; list($context, $course, $cm) = get_context_info_array($context->id); $qubaid = (int) array_shift($args); $filename = array_shift($args); $module = $DB->get_field('question_usages', 'component', array('id' => $qubaid)); return question_preview_question_pluginfile_joomdle($course, $context, $component, $filearea, $qubaid, $slot, $filename, $forcedownload); }
/** * Serve question files when they are displayed in this export format. * * @param context $previewcontext the quiz context * @param int $questionid the question id. * @param context $filecontext the file (question) context * @param string $filecomponent the component the file belongs to. * @param string $filearea the file area. * @param array $args remaining file args. * @param bool $forcedownload. * @param array $options additional options affecting the file serving. */ function qformat_xhtml_questiontext_preview_pluginfile($context, $questionid, $args, $forcedownload, array $options = array()) { global $CFG; list($context, $course, $cm) = get_context_info_array($context->id); require_login($course, false, $cm); question_require_capability_on($questionid, 'view'); question_send_questiontext_file($questionid, $args, $forcedownload, $options); }
/** * Serve questiontext files in the question text when they are displayed in this report. * * @package mod_offlinequiz * @category files * @param stdClass $context the context * @param int $questionid the question id * @param array $args remaining file args * @param bool $forcedownload * @param array $options additional options affecting the file serving */ function offlinequiz_statistics_questiontext_preview_pluginfile($context, $questionid, $args, $forcedownload, array $options = array()) { global $CFG; require_once $CFG->dirroot . '/mod/offlinequiz/locallib.php'; list($context, $course, $cm) = get_context_info_array($context->id); require_login($course, false, $cm); // Assume only trusted people can see this report. There is no real way to // validate questionid, becuase of the complexity of random quetsions. require_capability('offlinequiz/statistics:view', $context); question_send_questiontext_file($questionid, $args, $forcedownload, $options); }
/** * Serve question files when they are displayed in this export format. * * @param context $previewcontext the quiz context * @param int $questionid the question id. * @param context $filecontext the file (question) context * @param string $filecomponent the component the file belongs to. * @param string $filearea the file area. * @param array $args remaining file args. * @param bool $forcedownload. * @param array $options additional options affecting the file serving. */ function qformat_xhtml_question_preview_pluginfile($previewcontext, $questionid, $filecontext, $filecomponent, $filearea, $args, $forcedownload, $options = array()) { global $CFG; list($context, $course, $cm) = get_context_info_array($previewcontext->id); require_login($course, false, $cm); question_require_capability_on($questionid, 'view'); $fs = get_file_storage(); $relativepath = implode('/', $args); $fullpath = "/{$filecontext->id}/{$filecomponent}/{$filearea}/{$relativepath}"; if (!($file = $fs->get_file_by_hash(sha1($fullpath))) or $file->is_directory()) { send_file_not_found(); } send_stored_file($file, 0, 0, $forcedownload, $options); }
/** * Serve questiontext files in the question text when they are displayed in this report. * * @package quiz_statistics * @category files * @param context $previewcontext the quiz context * @param int $questionid the question id. * @param context $filecontext the file (question) context * @param string $filecomponent the component the file belongs to. * @param string $filearea the file area. * @param array $args remaining file args. * @param bool $forcedownload. * @param array $options additional options affecting the file serving. */ function quiz_statistics_question_preview_pluginfile($previewcontext, $questionid, $filecontext, $filecomponent, $filearea, $args, $forcedownload, $options = array()) { global $CFG; require_once $CFG->dirroot . '/mod/quiz/locallib.php'; list($context, $course, $cm) = get_context_info_array($previewcontext->id); require_login($course, false, $cm); // Assume only trusted people can see this report. There is no real way to // validate questionid, becuase of the complexity of random quetsions. require_capability('quiz/statistics:view', $context); $fs = get_file_storage(); $relativepath = implode('/', $args); $fullpath = "/{$filecontext->id}/{$filecomponent}/{$filearea}/{$relativepath}"; if (!($file = $fs->get_file_by_hash(sha1($fullpath))) or $file->is_directory()) { send_file_not_found(); } send_stored_file($file, 0, 0, $forcedownload, $options); }
public function __construct($context, $itemid, $filearea = 'submission') { global $USER, $CFG; require_once $CFG->libdir . '/portfoliolib.php'; $this->context = $context; list($context, $course, $cm) = get_context_info_array($context->id); $this->cm = $cm; $this->course = $course; $fs = get_file_storage(); $this->dir = $fs->get_area_tree($this->context->id, 'mod_assignment', $filearea, $itemid); $files = $fs->get_area_files($this->context->id, 'mod_assignment', $filearea, $itemid, "timemodified", false); if (count($files) >= 1 && has_capability('mod/assignment:exportownsubmission', $this->context)) { $button = new portfolio_add_button(); $button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id), '/mod/assignment/locallib.php'); $button->reset_formats(); $this->portfolioform = $button->to_html(); } $this->preprocess($this->dir, $filearea); }
function get_content() { global $CFG, $PAGE; if ($this->content !== NULL) { return $this->content; } if (!$CFG->usecomments) { $this->content = new stdClass(); $this->content->text = ''; if ($this->page->user_is_editing()) { $this->content->text = get_string('disabledcomments'); } return $this->content; } $this->content = new stdClass(); $this->content->footer = ''; $this->content->text = ''; if (empty($this->instance)) { return $this->content; } list($context, $course, $cm) = get_context_info_array($PAGE->context->id); $args = new stdClass(); $args->context = $PAGE->context; $args->course = $course; $args->area = 'page_comments'; $args->itemid = 0; $args->component = 'block_comments'; $args->linktext = get_string('showcomments'); $args->notoggle = true; $args->autostart = true; $args->displaycancel = false; $comment = new comment($args); $comment->set_view_permission(true); $comment->set_fullwidth(); $this->content = new stdClass(); $this->content->text = $comment->output(true); $this->content->footer = ''; return $this->content; }
// get manager by id $manager = get_grading_manager($areaid); } else { // get manager by context and component if (is_null($contextid) or is_null($component) or is_null($area)) { throw new coding_exception('The caller script must identify the gradable area.'); } $context = context::instance_by_id($contextid, MUST_EXIST); $manager = get_grading_manager($context, $component, $area); } if ($manager->get_context()->contextlevel < CONTEXT_COURSE) { throw new coding_exception('Unsupported gradable area context level'); } // get the currently active method $method = $manager->get_active_method(); list($context, $course, $cm) = get_context_info_array($manager->get_context()->id); require_login($course, true, $cm); require_capability('moodle/grade:managegradingforms', $context); if (!empty($returnurl)) { $returnurl = new moodle_url($returnurl); } else { $returnurl = null; } $PAGE->set_url($manager->get_management_url($returnurl)); navigation_node::override_active_url($manager->get_management_url()); $PAGE->set_title(get_string('gradingmanagement', 'core_grading')); $PAGE->set_heading(get_string('gradingmanagement', 'core_grading')); $output = $PAGE->get_renderer('core_grading'); // process the eventual change of the active grading method if (!empty($setmethod)) { require_sesskey();
/** * Generate all options needed by filepicker * * @param array $args including following keys * context * accepted_types * return_types * * @return array the list of repository instances, including meta infomation, containing the following keys * externallink * repositories * accepted_types */ function initialise_filepicker($args) { global $CFG, $USER, $PAGE, $OUTPUT; static $templatesinitialized = array(); require_once $CFG->libdir . '/licenselib.php'; $return = new stdClass(); $licenses = array(); if (!empty($CFG->licenses)) { $array = explode(',', $CFG->licenses); foreach ($array as $license) { $l = new stdClass(); $l->shortname = $license; $l->fullname = get_string($license, 'license'); $licenses[] = $l; } } if (!empty($CFG->sitedefaultlicense)) { $return->defaultlicense = $CFG->sitedefaultlicense; } $return->licenses = $licenses; $return->author = fullname($USER); if (empty($args->context)) { $context = $PAGE->context; } else { $context = $args->context; } $disable_types = array(); if (!empty($args->disable_types)) { $disable_types = $args->disable_types; } $user_context = context_user::instance($USER->id); list($context, $course, $cm) = get_context_info_array($context->id); $contexts = array($user_context, context_system::instance()); if (!empty($course)) { // adding course context $contexts[] = context_course::instance($course->id); } $externallink = (int) get_config(null, 'repositoryallowexternallinks'); $repositories = repository::get_instances(array('context' => $contexts, 'currentcontext' => $context, 'accepted_types' => $args->accepted_types, 'return_types' => $args->return_types, 'disable_types' => $disable_types)); $return->repositories = array(); if (empty($externallink)) { $return->externallink = false; } else { $return->externallink = true; } $return->userprefs = array(); $return->userprefs['recentrepository'] = get_user_preferences('filepicker_recentrepository', ''); $return->userprefs['recentlicense'] = get_user_preferences('filepicker_recentlicense', ''); $return->userprefs['recentviewmode'] = get_user_preferences('filepicker_recentviewmode', ''); user_preference_allow_ajax_update('filepicker_recentrepository', PARAM_INT); user_preference_allow_ajax_update('filepicker_recentlicense', PARAM_SAFEDIR); user_preference_allow_ajax_update('filepicker_recentviewmode', PARAM_INT); // provided by form element $return->accepted_types = file_get_typegroup('extension', $args->accepted_types); $return->return_types = $args->return_types; $templates = array(); foreach ($repositories as $repository) { $meta = $repository->get_meta(); // Please note that the array keys for repositories are used within // JavaScript a lot, the key NEEDS to be the repository id. $return->repositories[$repository->id] = $meta; // Register custom repository template if it has one if (method_exists($repository, 'get_upload_template') && !array_key_exists('uploadform_' . $meta->type, $templatesinitialized)) { $templates['uploadform_' . $meta->type] = $repository->get_upload_template(); $templatesinitialized['uploadform_' . $meta->type] = true; } } if (!array_key_exists('core', $templatesinitialized)) { // we need to send each filepicker template to the browser just once $fprenderer = $PAGE->get_renderer('core', 'files'); $templates = array_merge($templates, $fprenderer->filepicker_js_templates()); $templatesinitialized['core'] = true; } if (sizeof($templates)) { $PAGE->requires->js_init_call('M.core_filepicker.set_templates', array($templates), true); } return $return; }
/** * Return a list of page types * @param string $pagetype current page type * @param stdClass $parentcontext Block's parent context * @param stdClass $currentcontext Current context of block */ function course_page_type_list($pagetype, $parentcontext, $currentcontext) { // if above course context ,display all course fomats list($currentcontext, $course, $cm) = get_context_info_array($currentcontext->id); if ($course->id == SITEID) { return array('*' => get_string('page-x', 'pagetype')); } else { return array('*' => get_string('page-x', 'pagetype'), 'course-*' => get_string('page-course-x', 'pagetype'), 'course-view-*' => get_string('page-course-view-x', 'pagetype')); } }
/** * takes a list of records, the current data, a search string, * and mode to display prints the translated template * * @global object * @global object * @param string $template * @param array $records * @param object $data * @param string $search * @param int $page * @param bool $return * @return mixed */ function data_print_template($template, $records, $data, $search='', $page=0, $return=false) { global $CFG, $DB, $OUTPUT; $cm = get_coursemodule_from_instance('data', $data->id); $context = get_context_instance(CONTEXT_MODULE, $cm->id); static $fields = NULL; static $isteacher; static $dataid = NULL; if (empty($dataid)) { $dataid = $data->id; } else if ($dataid != $data->id) { $fields = NULL; } if (empty($fields)) { $fieldrecords = $DB->get_records('data_fields', array('dataid'=>$data->id)); foreach ($fieldrecords as $fieldrecord) { $fields[]= data_get_field($fieldrecord, $data); } $isteacher = has_capability('mod/data:managetemplates', $context); } if (empty($records)) { return; } foreach ($records as $record) { // Might be just one for the single template // Replacing tags $patterns = array(); $replacement = array(); // Then we generate strings to replace for normal tags foreach ($fields as $field) { $patterns[]='[['.$field->field->name.']]'; $replacement[] = highlight($search, $field->display_browse_field($record->id, $template)); } // Replacing special tags (##Edit##, ##Delete##, ##More##) $patterns[]='##edit##'; $patterns[]='##delete##'; if (has_capability('mod/data:manageentries', $context) or data_isowner($record->id)) { $replacement[] = '<a href="'.$CFG->wwwroot.'/mod/data/edit.php?d=' .$data->id.'&rid='.$record->id.'&sesskey='.sesskey().'"><img src="'.$OUTPUT->pix_url('t/edit') . '" class="iconsmall" alt="'.get_string('edit').'" title="'.get_string('edit').'" /></a>'; $replacement[] = '<a href="'.$CFG->wwwroot.'/mod/data/view.php?d=' .$data->id.'&delete='.$record->id.'&sesskey='.sesskey().'"><img src="'.$OUTPUT->pix_url('t/delete') . '" class="iconsmall" alt="'.get_string('delete').'" title="'.get_string('delete').'" /></a>'; } else { $replacement[] = ''; $replacement[] = ''; } $moreurl = $CFG->wwwroot . '/mod/data/view.php?d=' . $data->id . '&rid=' . $record->id; if ($search) { $moreurl .= '&filter=1'; } $patterns[]='##more##'; $replacement[] = '<a href="' . $moreurl . '"><img src="' . $OUTPUT->pix_url('i/search') . '" class="iconsmall" alt="' . get_string('more', 'data') . '" title="' . get_string('more', 'data') . '" /></a>'; $patterns[]='##moreurl##'; $replacement[] = $moreurl; $patterns[]='##user##'; $replacement[] = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$record->userid. '&course='.$data->course.'">'.fullname($record).'</a>'; $patterns[]='##export##'; if (!empty($CFG->enableportfolios) && ($template == 'singletemplate' || $template == 'listtemplate') && ((has_capability('mod/data:exportentry', $context) || (data_isowner($record->id) && has_capability('mod/data:exportownentry', $context))))) { require_once($CFG->libdir . '/portfoliolib.php'); $button = new portfolio_add_button(); $button->set_callback_options('data_portfolio_caller', array('id' => $cm->id, 'recordid' => $record->id), '/mod/data/locallib.php'); list($formats, $files) = data_portfolio_caller::formats($fields, $record); $button->set_formats($formats); $replacement[] = $button->to_html(PORTFOLIO_ADD_ICON_LINK); } else { $replacement[] = ''; } $patterns[] = '##timeadded##'; $replacement[] = userdate($record->timecreated); $patterns[] = '##timemodified##'; $replacement [] = userdate($record->timemodified); $patterns[]='##approve##'; if (has_capability('mod/data:approve', $context) && ($data->approval) && (!$record->approved)){ $replacement[] = '<span class="approve"><a href="'.$CFG->wwwroot.'/mod/data/view.php?d='.$data->id.'&approve='.$record->id.'&sesskey='.sesskey().'"><img src="'.$OUTPUT->pix_url('i/approve') . '" class="icon" alt="'.get_string('approve').'" /></a></span>'; } else { $replacement[] = ''; } $patterns[]='##comments##'; if (($template == 'listtemplate') && ($data->comments)) { if (!empty($CFG->usecomments)) { require_once($CFG->dirroot . '/comment/lib.php'); list($context, $course, $cm) = get_context_info_array($context->id); $cmt = new stdClass(); $cmt->context = $context; $cmt->course = $course; $cmt->cm = $cm; $cmt->area = 'database_entry'; $cmt->itemid = $record->id; $cmt->showcount = true; $cmt->component = 'mod_data'; $comment = new comment($cmt); $replacement[] = $comment->output(true); } } else { $replacement[] = ''; } // actual replacement of the tags $newtext = str_ireplace($patterns, $replacement, $data->{$template}); // no more html formatting and filtering - see MDL-6635 if ($return) { return $newtext; } else { echo $newtext; // hack alert - return is always false in singletemplate anyway ;-) /********************************** * Printing Ratings Form * *********************************/ if ($template == 'singletemplate') { //prints ratings options data_print_ratings($data, $record); } /********************************** * Printing Comments Form * *********************************/ if (($template == 'singletemplate') && ($data->comments)) { if (!empty($CFG->usecomments)) { require_once($CFG->dirroot . '/comment/lib.php'); list($context, $course, $cm) = get_context_info_array($context->id); $cmt = new stdClass(); $cmt->context = $context; $cmt->course = $course; $cmt->cm = $cm; $cmt->area = 'database_entry'; $cmt->itemid = $record->id; $cmt->showcount = true; $cmt->component = 'mod_data'; $comment = new comment($cmt); $comment->output(false); } } } } }
/** * Called via pluginfile.php -> question_pluginfile to serve files belonging to * a question in a question_attempt when that attempt is a preview. * * @package core_question * @category files * @param stdClass $course course settings object * @param stdClass $context context object * @param string $component the name of the component we are serving files for. * @param string $filearea the name of the file area. * @param int $qubaid the question_usage this image belongs to. * @param int $slot the relevant slot within the usage. * @param array $args the remaining bits of the file path. * @param bool $forcedownload whether the user must be forced to download the file. * @param array $options additional options affecting the file serving * @return bool false if file not found, does not return if found - justsend the file */ function question_preview_question_pluginfile($course, $context, $component, $filearea, $qubaid, $slot, $args, $forcedownload, $fileoptions) { global $USER, $DB, $CFG; list($context, $course, $cm) = get_context_info_array($context->id); require_login($course, false, $cm); $quba = question_engine::load_questions_usage_by_activity($qubaid); if (!question_has_capability_on($quba->get_question($slot), 'use')) { send_file_not_found(); } $options = new question_display_options(); $options->feedback = question_display_options::VISIBLE; $options->numpartscorrect = question_display_options::VISIBLE; $options->generalfeedback = question_display_options::VISIBLE; $options->rightanswer = question_display_options::VISIBLE; $options->manualcomment = question_display_options::VISIBLE; $options->history = question_display_options::VISIBLE; if (!$quba->check_file_access($slot, $options, $component, $filearea, $args, $forcedownload)) { send_file_not_found(); } $fs = get_file_storage(); $relativepath = implode('/', $args); $fullpath = "/{$context->id}/{$component}/{$filearea}/{$relativepath}"; if (!($file = $fs->get_file_by_hash(sha1($fullpath))) or $file->is_directory()) { send_file_not_found(); } send_stored_file($file, 0, 0, $forcedownload, $fileoptions); }
/** * This function delegates file serving to individual plugins * * @param string $relativepath * @param bool $forcedownload * @param null|string $preview the preview mode, defaults to serving the original file * @todo MDL-31088 file serving improments */ function file_pluginfile($relativepath, $forcedownload, $preview = null) { global $DB, $CFG, $USER; // relative path must start with '/' if (!$relativepath) { print_error('invalidargorconf'); } else { if ($relativepath[0] != '/') { print_error('pathdoesnotstartslash'); } } // extract relative path components $args = explode('/', ltrim($relativepath, '/')); if (count($args) < 3) { // always at least context, component and filearea print_error('invalidarguments'); } $contextid = (int) array_shift($args); $component = clean_param(array_shift($args), PARAM_COMPONENT); $filearea = clean_param(array_shift($args), PARAM_AREA); list($context, $course, $cm) = get_context_info_array($contextid); $fs = get_file_storage(); // ======================================================================================================================== if ($component === 'blog') { // Blog file serving if ($context->contextlevel != CONTEXT_SYSTEM) { send_file_not_found(); } if ($filearea !== 'attachment' and $filearea !== 'post') { send_file_not_found(); } if (empty($CFG->enableblogs)) { print_error('siteblogdisable', 'blog'); } $entryid = (int) array_shift($args); if (!($entry = $DB->get_record('post', array('module' => 'blog', 'id' => $entryid)))) { send_file_not_found(); } if ($CFG->bloglevel < BLOG_GLOBAL_LEVEL) { require_login(); if (isguestuser()) { print_error('noguest'); } if ($CFG->bloglevel == BLOG_USER_LEVEL) { if ($USER->id != $entry->userid) { send_file_not_found(); } } } if ($entry->publishstate === 'public') { if ($CFG->forcelogin) { require_login(); } } else { if ($entry->publishstate === 'site') { require_login(); //ok } else { if ($entry->publishstate === 'draft') { require_login(); if ($USER->id != $entry->userid) { send_file_not_found(); } } } } $filename = array_pop($args); $filepath = $args ? '/' . implode('/', $args) . '/' : '/'; if (!($file = $fs->get_file($context->id, $component, $filearea, $entryid, $filepath, $filename)) or $file->is_directory()) { send_file_not_found(); } send_stored_file($file, 10 * 60, 0, true, array('preview' => $preview)); // download MUST be forced - security! // ======================================================================================================================== } else { if ($component === 'grade') { if (($filearea === 'outcome' or $filearea === 'scale') and $context->contextlevel == CONTEXT_SYSTEM) { // Global gradebook files if ($CFG->forcelogin) { require_login(); } $fullpath = "/{$context->id}/{$component}/{$filearea}/" . implode('/', $args); if (!($file = $fs->get_file_by_hash(sha1($fullpath))) or $file->is_directory()) { send_file_not_found(); } \core\session\manager::write_close(); // Unlock session during file serving. send_stored_file($file, 60 * 60, 0, $forcedownload, array('preview' => $preview)); } else { if ($filearea === 'feedback' and $context->contextlevel == CONTEXT_COURSE) { //TODO: nobody implemented this yet in grade edit form!! send_file_not_found(); if ($CFG->forcelogin || $course->id != SITEID) { require_login($course); } $fullpath = "/{$context->id}/{$component}/{$filearea}/" . implode('/', $args); if (!($file = $fs->get_file_by_hash(sha1($fullpath))) or $file->is_directory()) { send_file_not_found(); } \core\session\manager::write_close(); // Unlock session during file serving. send_stored_file($file, 60 * 60, 0, $forcedownload, array('preview' => $preview)); } else { send_file_not_found(); } } // ======================================================================================================================== } else { if ($component === 'tag') { if ($filearea === 'description' and $context->contextlevel == CONTEXT_SYSTEM) { // All tag descriptions are going to be public but we still need to respect forcelogin if ($CFG->forcelogin) { require_login(); } $fullpath = "/{$context->id}/tag/description/" . implode('/', $args); if (!($file = $fs->get_file_by_hash(sha1($fullpath))) or $file->is_directory()) { send_file_not_found(); } \core\session\manager::write_close(); // Unlock session during file serving. send_stored_file($file, 60 * 60, 0, true, array('preview' => $preview)); } else { send_file_not_found(); } // ======================================================================================================================== } else { if ($component === 'badges') { require_once $CFG->libdir . '/badgeslib.php'; $badgeid = (int) array_shift($args); $badge = new badge($badgeid); $filename = array_pop($args); if ($filearea === 'badgeimage') { if ($filename !== 'f1' && $filename !== 'f2') { send_file_not_found(); } if (!($file = $fs->get_file($context->id, 'badges', 'badgeimage', $badge->id, '/', $filename . '.png'))) { send_file_not_found(); } \core\session\manager::write_close(); send_stored_file($file, 60 * 60, 0, $forcedownload, array('preview' => $preview)); } else { if ($filearea === 'userbadge' and $context->contextlevel == CONTEXT_USER) { if (!($file = $fs->get_file($context->id, 'badges', 'userbadge', $badge->id, '/', $filename . '.png'))) { send_file_not_found(); } \core\session\manager::write_close(); send_stored_file($file, 60 * 60, 0, true, array('preview' => $preview)); } } // ======================================================================================================================== } else { if ($component === 'calendar') { if ($filearea === 'event_description' and $context->contextlevel == CONTEXT_SYSTEM) { // All events here are public the one requirement is that we respect forcelogin if ($CFG->forcelogin) { require_login(); } // Get the event if from the args array $eventid = array_shift($args); // Load the event from the database if (!($event = $DB->get_record('event', array('id' => (int) $eventid, 'eventtype' => 'site')))) { send_file_not_found(); } // Get the file and serve if successful $filename = array_pop($args); $filepath = $args ? '/' . implode('/', $args) . '/' : '/'; if (!($file = $fs->get_file($context->id, $component, $filearea, $eventid, $filepath, $filename)) or $file->is_directory()) { send_file_not_found(); } \core\session\manager::write_close(); // Unlock session during file serving. send_stored_file($file, 60 * 60, 0, $forcedownload, array('preview' => $preview)); } else { if ($filearea === 'event_description' and $context->contextlevel == CONTEXT_USER) { // Must be logged in, if they are not then they obviously can't be this user require_login(); // Don't want guests here, potentially saves a DB call if (isguestuser()) { send_file_not_found(); } // Get the event if from the args array $eventid = array_shift($args); // Load the event from the database - user id must match if (!($event = $DB->get_record('event', array('id' => (int) $eventid, 'userid' => $USER->id, 'eventtype' => 'user')))) { send_file_not_found(); } // Get the file and serve if successful $filename = array_pop($args); $filepath = $args ? '/' . implode('/', $args) . '/' : '/'; if (!($file = $fs->get_file($context->id, $component, $filearea, $eventid, $filepath, $filename)) or $file->is_directory()) { send_file_not_found(); } \core\session\manager::write_close(); // Unlock session during file serving. send_stored_file($file, 0, 0, true, array('preview' => $preview)); } else { if ($filearea === 'event_description' and $context->contextlevel == CONTEXT_COURSE) { // Respect forcelogin and require login unless this is the site.... it probably // should NEVER be the site if ($CFG->forcelogin || $course->id != SITEID) { require_login($course); } // Must be able to at least view the course. This does not apply to the front page. if ($course->id != SITEID && !is_enrolled($context) && !is_viewing($context)) { //TODO: hmm, do we really want to block guests here? send_file_not_found(); } // Get the event id $eventid = array_shift($args); // Load the event from the database we need to check whether it is // a) valid course event // b) a group event // Group events use the course context (there is no group context) if (!($event = $DB->get_record('event', array('id' => (int) $eventid, 'courseid' => $course->id)))) { send_file_not_found(); } // If its a group event require either membership of view all groups capability if ($event->eventtype === 'group') { if (!has_capability('moodle/site:accessallgroups', $context) && !groups_is_member($event->groupid, $USER->id)) { send_file_not_found(); } } else { if ($event->eventtype === 'course' || $event->eventtype === 'site') { // Ok. Please note that the event type 'site' still uses a course context. } else { // Some other type. send_file_not_found(); } } // If we get this far we can serve the file $filename = array_pop($args); $filepath = $args ? '/' . implode('/', $args) . '/' : '/'; if (!($file = $fs->get_file($context->id, $component, $filearea, $eventid, $filepath, $filename)) or $file->is_directory()) { send_file_not_found(); } \core\session\manager::write_close(); // Unlock session during file serving. send_stored_file($file, 60 * 60, 0, $forcedownload, array('preview' => $preview)); } else { send_file_not_found(); } } } // ======================================================================================================================== } else { if ($component === 'user') { if ($filearea === 'icon' and $context->contextlevel == CONTEXT_USER) { if (count($args) == 1) { $themename = theme_config::DEFAULT_THEME; $filename = array_shift($args); } else { $themename = array_shift($args); $filename = array_shift($args); } // fix file name automatically if ($filename !== 'f1' and $filename !== 'f2' and $filename !== 'f3') { $filename = 'f1'; } if ((!empty($CFG->forcelogin) and !isloggedin()) || !empty($CFG->forceloginforprofileimage) && (!isloggedin() || isguestuser())) { // protect images if login required and not logged in; // also if login is required for profile images and is not logged in or guest // do not use require_login() because it is expensive and not suitable here anyway $theme = theme_config::load($themename); redirect($theme->pix_url('u/' . $filename, 'moodle')); // intentionally not cached } if (!($file = $fs->get_file($context->id, 'user', 'icon', 0, '/', $filename . '.png'))) { if (!($file = $fs->get_file($context->id, 'user', 'icon', 0, '/', $filename . '.jpg'))) { if ($filename === 'f3') { // f3 512x512px was introduced in 2.3, there might be only the smaller version. if (!($file = $fs->get_file($context->id, 'user', 'icon', 0, '/', 'f1.png'))) { $file = $fs->get_file($context->id, 'user', 'icon', 0, '/', 'f1.jpg'); } } } } if (!$file) { // bad reference - try to prevent future retries as hard as possible! if ($user = $DB->get_record('user', array('id' => $context->instanceid), 'id, picture')) { if ($user->picture > 0) { $DB->set_field('user', 'picture', 0, array('id' => $user->id)); } } // no redirect here because it is not cached $theme = theme_config::load($themename); $imagefile = $theme->resolve_image_location('u/' . $filename, 'moodle', null); send_file($imagefile, basename($imagefile), 60 * 60 * 24 * 14); } $options = array('preview' => $preview); if (empty($CFG->forcelogin) && empty($CFG->forceloginforprofileimage)) { // Profile images should be cache-able by both browsers and proxies according // to $CFG->forcelogin and $CFG->forceloginforprofileimage. $options['cacheability'] = 'public'; } send_stored_file($file, 60 * 60 * 24 * 365, 0, false, $options); // enable long caching, there are many images on each page } else { if ($filearea === 'private' and $context->contextlevel == CONTEXT_USER) { require_login(); if (isguestuser()) { send_file_not_found(); } if ($USER->id !== $context->instanceid) { send_file_not_found(); } $filename = array_pop($args); $filepath = $args ? '/' . implode('/', $args) . '/' : '/'; if (!($file = $fs->get_file($context->id, $component, $filearea, 0, $filepath, $filename)) or $file->is_directory()) { send_file_not_found(); } \core\session\manager::write_close(); // Unlock session during file serving. send_stored_file($file, 0, 0, true, array('preview' => $preview)); // must force download - security! } else { if ($filearea === 'profile' and $context->contextlevel == CONTEXT_USER) { if ($CFG->forcelogin) { require_login(); } $userid = $context->instanceid; if ($USER->id == $userid) { // always can access own } else { if (!empty($CFG->forceloginforprofiles)) { require_login(); if (isguestuser()) { send_file_not_found(); } // we allow access to site profile of all course contacts (usually teachers) if (!has_coursecontact_role($userid) && !has_capability('moodle/user:viewdetails', $context)) { send_file_not_found(); } $canview = false; if (has_capability('moodle/user:viewdetails', $context)) { $canview = true; } else { $courses = enrol_get_my_courses(); } while (!$canview && count($courses) > 0) { $course = array_shift($courses); if (has_capability('moodle/user:viewdetails', context_course::instance($course->id))) { $canview = true; } } } } $filename = array_pop($args); $filepath = $args ? '/' . implode('/', $args) . '/' : '/'; if (!($file = $fs->get_file($context->id, $component, $filearea, 0, $filepath, $filename)) or $file->is_directory()) { send_file_not_found(); } \core\session\manager::write_close(); // Unlock session during file serving. send_stored_file($file, 0, 0, true, array('preview' => $preview)); // must force download - security! } else { if ($filearea === 'profile' and $context->contextlevel == CONTEXT_COURSE) { $userid = (int) array_shift($args); $usercontext = context_user::instance($userid); if ($CFG->forcelogin) { require_login(); } if (!empty($CFG->forceloginforprofiles)) { require_login(); if (isguestuser()) { print_error('noguest'); } //TODO: review this logic of user profile access prevention if (!has_coursecontact_role($userid) and !has_capability('moodle/user:viewdetails', $usercontext)) { print_error('usernotavailable'); } if (!has_capability('moodle/user:viewdetails', $context) && !has_capability('moodle/user:viewdetails', $usercontext)) { print_error('cannotviewprofile'); } if (!is_enrolled($context, $userid)) { print_error('notenrolledprofile'); } if (groups_get_course_groupmode($course) == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context)) { print_error('groupnotamember'); } } $filename = array_pop($args); $filepath = $args ? '/' . implode('/', $args) . '/' : '/'; if (!($file = $fs->get_file($usercontext->id, 'user', 'profile', 0, $filepath, $filename)) or $file->is_directory()) { send_file_not_found(); } \core\session\manager::write_close(); // Unlock session during file serving. send_stored_file($file, 0, 0, true, array('preview' => $preview)); // must force download - security! } else { if ($filearea === 'backup' and $context->contextlevel == CONTEXT_USER) { require_login(); if (isguestuser()) { send_file_not_found(); } $userid = $context->instanceid; if ($USER->id != $userid) { send_file_not_found(); } $filename = array_pop($args); $filepath = $args ? '/' . implode('/', $args) . '/' : '/'; if (!($file = $fs->get_file($context->id, 'user', 'backup', 0, $filepath, $filename)) or $file->is_directory()) { send_file_not_found(); } \core\session\manager::write_close(); // Unlock session during file serving. send_stored_file($file, 0, 0, true, array('preview' => $preview)); // must force download - security! } else { send_file_not_found(); } } } } } // ======================================================================================================================== } else { if ($component === 'coursecat') { if ($context->contextlevel != CONTEXT_COURSECAT) { send_file_not_found(); } if ($filearea === 'description') { if ($CFG->forcelogin) { // no login necessary - unless login forced everywhere require_login(); } $filename = array_pop($args); $filepath = $args ? '/' . implode('/', $args) . '/' : '/'; if (!($file = $fs->get_file($context->id, 'coursecat', 'description', 0, $filepath, $filename)) or $file->is_directory()) { send_file_not_found(); } \core\session\manager::write_close(); // Unlock session during file serving. send_stored_file($file, 60 * 60, 0, $forcedownload, array('preview' => $preview)); } else { send_file_not_found(); } // ======================================================================================================================== } else { if ($component === 'course') { if ($context->contextlevel != CONTEXT_COURSE) { send_file_not_found(); } if ($filearea === 'summary' || $filearea === 'overviewfiles') { if ($CFG->forcelogin) { require_login(); } $filename = array_pop($args); $filepath = $args ? '/' . implode('/', $args) . '/' : '/'; if (!($file = $fs->get_file($context->id, 'course', $filearea, 0, $filepath, $filename)) or $file->is_directory()) { send_file_not_found(); } \core\session\manager::write_close(); // Unlock session during file serving. send_stored_file($file, 60 * 60, 0, $forcedownload, array('preview' => $preview)); } else { if ($filearea === 'section') { if ($CFG->forcelogin) { require_login($course); } else { if ($course->id != SITEID) { require_login($course); } } $sectionid = (int) array_shift($args); if (!($section = $DB->get_record('course_sections', array('id' => $sectionid, 'course' => $course->id)))) { send_file_not_found(); } $filename = array_pop($args); $filepath = $args ? '/' . implode('/', $args) . '/' : '/'; if (!($file = $fs->get_file($context->id, 'course', 'section', $sectionid, $filepath, $filename)) or $file->is_directory()) { send_file_not_found(); } \core\session\manager::write_close(); // Unlock session during file serving. send_stored_file($file, 60 * 60, 0, $forcedownload, array('preview' => $preview)); } else { send_file_not_found(); } } } else { if ($component === 'cohort') { $cohortid = (int) array_shift($args); $cohort = $DB->get_record('cohort', array('id' => $cohortid), '*', MUST_EXIST); $cohortcontext = context::instance_by_id($cohort->contextid); // The context in the file URL must be either cohort context or context of the course underneath the cohort's context. if ($context->id != $cohort->contextid && ($context->contextlevel != CONTEXT_COURSE || !in_array($cohort->contextid, $context->get_parent_context_ids()))) { send_file_not_found(); } // User is able to access cohort if they have view cap on cohort level or // the cohort is visible and they have view cap on course level. $canview = has_capability('moodle/cohort:view', $cohortcontext) || $cohort->visible && has_capability('moodle/cohort:view', $context); if ($filearea === 'description' && $canview) { $filename = array_pop($args); $filepath = $args ? '/' . implode('/', $args) . '/' : '/'; if (($file = $fs->get_file($cohortcontext->id, 'cohort', 'description', $cohort->id, $filepath, $filename)) && !$file->is_directory()) { \core\session\manager::write_close(); // Unlock session during file serving. send_stored_file($file, 60 * 60, 0, $forcedownload, array('preview' => $preview)); } } send_file_not_found(); } else { if ($component === 'group') { if ($context->contextlevel != CONTEXT_COURSE) { send_file_not_found(); } require_course_login($course, true, null, false); $groupid = (int) array_shift($args); $group = $DB->get_record('groups', array('id' => $groupid, 'courseid' => $course->id), '*', MUST_EXIST); if ($course->groupmodeforce and $course->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context) and !groups_is_member($group->id, $USER->id)) { // do not allow access to separate group info if not member or teacher send_file_not_found(); } if ($filearea === 'description') { require_login($course); $filename = array_pop($args); $filepath = $args ? '/' . implode('/', $args) . '/' : '/'; if (!($file = $fs->get_file($context->id, 'group', 'description', $group->id, $filepath, $filename)) or $file->is_directory()) { send_file_not_found(); } \core\session\manager::write_close(); // Unlock session during file serving. send_stored_file($file, 60 * 60, 0, $forcedownload, array('preview' => $preview)); } else { if ($filearea === 'icon') { $filename = array_pop($args); if ($filename !== 'f1' and $filename !== 'f2') { send_file_not_found(); } if (!($file = $fs->get_file($context->id, 'group', 'icon', $group->id, '/', $filename . '.png'))) { if (!($file = $fs->get_file($context->id, 'group', 'icon', $group->id, '/', $filename . '.jpg'))) { send_file_not_found(); } } \core\session\manager::write_close(); // Unlock session during file serving. send_stored_file($file, 60 * 60, 0, false, array('preview' => $preview)); } else { send_file_not_found(); } } } else { if ($component === 'grouping') { if ($context->contextlevel != CONTEXT_COURSE) { send_file_not_found(); } require_login($course); $groupingid = (int) array_shift($args); // note: everybody has access to grouping desc images for now if ($filearea === 'description') { $filename = array_pop($args); $filepath = $args ? '/' . implode('/', $args) . '/' : '/'; if (!($file = $fs->get_file($context->id, 'grouping', 'description', $groupingid, $filepath, $filename)) or $file->is_directory()) { send_file_not_found(); } \core\session\manager::write_close(); // Unlock session during file serving. send_stored_file($file, 60 * 60, 0, $forcedownload, array('preview' => $preview)); } else { send_file_not_found(); } // ======================================================================================================================== } else { if ($component === 'backup') { if ($filearea === 'course' and $context->contextlevel == CONTEXT_COURSE) { require_login($course); require_capability('moodle/backup:downloadfile', $context); $filename = array_pop($args); $filepath = $args ? '/' . implode('/', $args) . '/' : '/'; if (!($file = $fs->get_file($context->id, 'backup', 'course', 0, $filepath, $filename)) or $file->is_directory()) { send_file_not_found(); } \core\session\manager::write_close(); // Unlock session during file serving. send_stored_file($file, 0, 0, $forcedownload, array('preview' => $preview)); } else { if ($filearea === 'section' and $context->contextlevel == CONTEXT_COURSE) { require_login($course); require_capability('moodle/backup:downloadfile', $context); $sectionid = (int) array_shift($args); $filename = array_pop($args); $filepath = $args ? '/' . implode('/', $args) . '/' : '/'; if (!($file = $fs->get_file($context->id, 'backup', 'section', $sectionid, $filepath, $filename)) or $file->is_directory()) { send_file_not_found(); } \core\session\manager::write_close(); send_stored_file($file, 60 * 60, 0, $forcedownload, array('preview' => $preview)); } else { if ($filearea === 'activity' and $context->contextlevel == CONTEXT_MODULE) { require_login($course, false, $cm); require_capability('moodle/backup:downloadfile', $context); $filename = array_pop($args); $filepath = $args ? '/' . implode('/', $args) . '/' : '/'; if (!($file = $fs->get_file($context->id, 'backup', 'activity', 0, $filepath, $filename)) or $file->is_directory()) { send_file_not_found(); } \core\session\manager::write_close(); send_stored_file($file, 60 * 60, 0, $forcedownload, array('preview' => $preview)); } else { if ($filearea === 'automated' and $context->contextlevel == CONTEXT_COURSE) { // Backup files that were generated by the automated backup systems. require_login($course); require_capability('moodle/site:config', $context); $filename = array_pop($args); $filepath = $args ? '/' . implode('/', $args) . '/' : '/'; if (!($file = $fs->get_file($context->id, 'backup', 'automated', 0, $filepath, $filename)) or $file->is_directory()) { send_file_not_found(); } \core\session\manager::write_close(); // Unlock session during file serving. send_stored_file($file, 0, 0, $forcedownload, array('preview' => $preview)); } else { send_file_not_found(); } } } } // ======================================================================================================================== } else { if ($component === 'question') { require_once $CFG->libdir . '/questionlib.php'; question_pluginfile($course, $context, 'question', $filearea, $args, $forcedownload); send_file_not_found(); // ======================================================================================================================== } else { if ($component === 'grading') { if ($filearea === 'description') { // files embedded into the form definition description if ($context->contextlevel == CONTEXT_SYSTEM) { require_login(); } else { if ($context->contextlevel >= CONTEXT_COURSE) { require_login($course, false, $cm); } else { send_file_not_found(); } } $formid = (int) array_shift($args); $sql = "SELECT ga.id\n FROM {grading_areas} ga\n JOIN {grading_definitions} gd ON (gd.areaid = ga.id)\n WHERE gd.id = ? AND ga.contextid = ?"; $areaid = $DB->get_field_sql($sql, array($formid, $context->id), IGNORE_MISSING); if (!$areaid) { send_file_not_found(); } $fullpath = "/{$context->id}/{$component}/{$filearea}/{$formid}/" . implode('/', $args); if (!($file = $fs->get_file_by_hash(sha1($fullpath))) or $file->is_directory()) { send_file_not_found(); } \core\session\manager::write_close(); // Unlock session during file serving. send_stored_file($file, 60 * 60, 0, $forcedownload, array('preview' => $preview)); } // ======================================================================================================================== } else { if (strpos($component, 'mod_') === 0) { $modname = substr($component, 4); if (!file_exists("{$CFG->dirroot}/mod/{$modname}/lib.php")) { send_file_not_found(); } require_once "{$CFG->dirroot}/mod/{$modname}/lib.php"; if ($context->contextlevel == CONTEXT_MODULE) { if ($cm->modname !== $modname) { // somebody tries to gain illegal access, cm type must match the component! send_file_not_found(); } } if ($filearea === 'intro') { if (!plugin_supports('mod', $modname, FEATURE_MOD_INTRO, true)) { send_file_not_found(); } require_course_login($course, true, $cm); // all users may access it $filename = array_pop($args); $filepath = $args ? '/' . implode('/', $args) . '/' : '/'; if (!($file = $fs->get_file($context->id, 'mod_' . $modname, 'intro', 0, $filepath, $filename)) or $file->is_directory()) { send_file_not_found(); } // finally send the file send_stored_file($file, null, 0, false, array('preview' => $preview)); } $filefunction = $component . '_pluginfile'; $filefunctionold = $modname . '_pluginfile'; if (function_exists($filefunction)) { // if the function exists, it must send the file and terminate. Whatever it returns leads to "not found" $filefunction($course, $cm, $context, $filearea, $args, $forcedownload, array('preview' => $preview)); } else { if (function_exists($filefunctionold)) { // if the function exists, it must send the file and terminate. Whatever it returns leads to "not found" $filefunctionold($course, $cm, $context, $filearea, $args, $forcedownload, array('preview' => $preview)); } } send_file_not_found(); // ======================================================================================================================== } else { if (strpos($component, 'block_') === 0) { $blockname = substr($component, 6); // note: no more class methods in blocks please, that is .... if (!file_exists("{$CFG->dirroot}/blocks/{$blockname}/lib.php")) { send_file_not_found(); } require_once "{$CFG->dirroot}/blocks/{$blockname}/lib.php"; if ($context->contextlevel == CONTEXT_BLOCK) { $birecord = $DB->get_record('block_instances', array('id' => $context->instanceid), '*', MUST_EXIST); if ($birecord->blockname !== $blockname) { // somebody tries to gain illegal access, cm type must match the component! send_file_not_found(); } if ($context->get_course_context(false)) { // If block is in course context, then check if user has capability to access course. require_course_login($course); } else { if ($CFG->forcelogin) { // If user is logged out, bp record will not be visible, even if the user would have access if logged in. require_login(); } } $bprecord = $DB->get_record('block_positions', array('contextid' => $context->id, 'blockinstanceid' => $context->instanceid)); // User can't access file, if block is hidden or doesn't have block:view capability if ($bprecord && !$bprecord->visible || !has_capability('moodle/block:view', $context)) { send_file_not_found(); } } else { $birecord = null; } $filefunction = $component . '_pluginfile'; if (function_exists($filefunction)) { // if the function exists, it must send the file and terminate. Whatever it returns leads to "not found" $filefunction($course, $birecord, $context, $filearea, $args, $forcedownload, array('preview' => $preview)); } send_file_not_found(); // ======================================================================================================================== } else { if (strpos($component, '_') === false) { // all core subsystems have to be specified above, no more guessing here! send_file_not_found(); } else { // try to serve general plugin file in arbitrary context $dir = core_component::get_component_directory($component); if (!file_exists("{$dir}/lib.php")) { send_file_not_found(); } include_once "{$dir}/lib.php"; $filefunction = $component . '_pluginfile'; if (function_exists($filefunction)) { // if the function exists, it must send the file and terminate. Whatever it returns leads to "not found" $filefunction($course, $cm, $context, $filearea, $args, $forcedownload, array('preview' => $preview)); } send_file_not_found(); } } } } } } } } } } } } } } } } } }
/** * Serve questiontext files in the question text when they are displayed in this report. * * @package core_files * @category files * @param context $previewcontext the context in which the preview is happening. * @param int $questionid the question id. * @param context $filecontext the file (question) context. * @param string $filecomponent the component the file belongs to. * @param string $filearea the file area. * @param array $args remaining file args. * @param bool $forcedownload. * @param array $options additional options affecting the file serving. */ function core_question_question_preview_pluginfile($previewcontext, $questionid, $filecontext, $filecomponent, $filearea, $args, $forcedownload, $options = array()) { global $DB; // Verify that contextid matches the question. $question = $DB->get_record_sql(' SELECT q.*, qc.contextid FROM {question} q JOIN {question_categories} qc ON qc.id = q.category WHERE q.id = :id AND qc.contextid = :contextid', array('id' => $questionid, 'contextid' => $filecontext->id), MUST_EXIST); // Check the capability. list($context, $course, $cm) = get_context_info_array($previewcontext->id); require_login($course, false, $cm); question_require_capability_on($question, 'use'); $fs = get_file_storage(); $relativepath = implode('/', $args); $fullpath = "/{$filecontext->id}/{$filecomponent}/{$filearea}/{$relativepath}"; if (!($file = $fs->get_file_by_hash(sha1($fullpath))) or $file->is_directory()) { send_file_not_found(); } send_stored_file($file, 0, 0, $forcedownload, $options); }
/** * Constructor * * @param stdClass $options options for filemanager * default options are: * maxbytes=>-1, * maxfiles=>-1, * itemid=>0, * subdirs=>false, * client_id=>uniqid(), * acepted_types=>'*', * return_types=>FILE_INTERNAL, * context=>$PAGE->context, * author=>fullname($USER), * licenses=>array build from $CFG->licenses, * defaultlicense=>$CFG->sitedefaultlicense */ public function __construct(stdClass $options) { global $CFG, $USER, $PAGE; require_once $CFG->dirroot . '/repository/lib.php'; $defaults = array('maxbytes' => -1, 'maxfiles' => -1, 'itemid' => 0, 'subdirs' => 0, 'client_id' => uniqid(), 'accepted_types' => '*', 'return_types' => FILE_INTERNAL, 'context' => $PAGE->context, 'author' => fullname($USER), 'licenses' => array()); if (!empty($CFG->licenses)) { $array = explode(',', $CFG->licenses); foreach ($array as $license) { $l = new stdClass(); $l->shortname = $license; $l->fullname = get_string($license, 'license'); $defaults['licenses'][] = $l; } } if (!empty($CFG->sitedefaultlicense)) { $defaults['defaultlicense'] = $CFG->sitedefaultlicense; } foreach ($defaults as $key => $value) { if (empty($options->{$key})) { $options->{$key} = $value; } } $fs = get_file_storage(); // initilise options, getting files in root path $this->options = file_get_drafarea_files($options->itemid, '/'); // calculate file count $usercontext = context_user::instance($USER->id); $files = $fs->get_area_files($usercontext->id, 'user', 'draft', $options->itemid, 'id', false); $filecount = count($files); $this->options->filecount = $filecount; // copying other options foreach ($options as $name => $value) { $this->options->{$name} = $value; } // calculate the maximum file size as minimum from what is specified in filepicker options, // course options, global configuration and php settings $coursebytes = $maxbytes = 0; list($context, $course, $cm) = get_context_info_array($this->options->context->id); if (is_object($course)) { $coursebytes = $course->maxbytes; } if (!empty($this->options->maxbytes) && $this->options->maxbytes > 0) { $maxbytes = $this->options->maxbytes; } $this->options->maxbytes = get_user_max_upload_file_size($context, $CFG->maxbytes, $coursebytes, $maxbytes); // building file picker options $params = new stdClass(); $params->accepted_types = $options->accepted_types; $params->return_types = $options->return_types; $params->context = $options->context; $params->env = 'filemanager'; $params->disable_types = !empty($options->disable_types) ? $options->disable_types : array(); $filepicker_options = initialise_filepicker($params); $this->options->filepicker = $filepicker_options; }
public function get_name() { list($context, $course, $cm) = get_context_info_array($this->context->id); if (!empty($course)) { return get_string('courselegacyfiles') . format_string($course->shortname, true, array('context' => get_course_context($context))); } else { return get_string('courselegacyfiles'); } }
/** * takes a list of records, the current data, a search string, * and mode to display prints the translated template * * @global object * @global object * @param string $template * @param array $records * @param object $data * @param string $search * @param int $page * @param bool $return * @param object $jumpurl a moodle_url by which to jump back to the record list (can be null) * @return mixed */ function data_print_template($template, $records, $data, $search = '', $page = 0, $return = false, moodle_url $jumpurl = null) { global $CFG, $DB, $OUTPUT; $cm = get_coursemodule_from_instance('data', $data->id); $context = context_module::instance($cm->id); static $fields = NULL; static $isteacher; static $dataid = NULL; if (empty($dataid)) { $dataid = $data->id; } else { if ($dataid != $data->id) { $fields = NULL; } } if (empty($fields)) { $fieldrecords = $DB->get_records('data_fields', array('dataid' => $data->id)); foreach ($fieldrecords as $fieldrecord) { $fields[] = data_get_field($fieldrecord, $data); } $isteacher = has_capability('mod/data:managetemplates', $context); } if (empty($records)) { return; } if (!$jumpurl) { $jumpurl = new moodle_url('/mod/data/view.php', array('d' => $data->id)); } $jumpurl = new moodle_url($jumpurl, array('page' => $page, 'sesskey' => sesskey())); // Check whether this activity is read-only at present $readonly = data_in_readonly_period($data); foreach ($records as $record) { // Might be just one for the single template // Replacing tags $patterns = array(); $replacement = array(); // Then we generate strings to replace for normal tags foreach ($fields as $field) { $patterns[] = '[[' . $field->field->name . ']]'; $replacement[] = highlight($search, $field->display_browse_field($record->id, $template)); } $canmanageentries = has_capability('mod/data:manageentries', $context); // Replacing special tags (##Edit##, ##Delete##, ##More##) $patterns[] = '##edit##'; $patterns[] = '##delete##'; if ($canmanageentries || !$readonly && data_isowner($record->id)) { $replacement[] = '<a href="' . $CFG->wwwroot . '/mod/data/edit.php?d=' . $data->id . '&rid=' . $record->id . '&sesskey=' . sesskey() . '"><img src="' . $OUTPUT->pix_url('t/edit') . '" class="iconsmall" alt="' . get_string('edit') . '" title="' . get_string('edit') . '" /></a>'; $replacement[] = '<a href="' . $CFG->wwwroot . '/mod/data/view.php?d=' . $data->id . '&delete=' . $record->id . '&sesskey=' . sesskey() . '"><img src="' . $OUTPUT->pix_url('t/delete') . '" class="iconsmall" alt="' . get_string('delete') . '" title="' . get_string('delete') . '" /></a>'; } else { $replacement[] = ''; $replacement[] = ''; } $moreurl = $CFG->wwwroot . '/mod/data/view.php?d=' . $data->id . '&rid=' . $record->id; if ($search) { $moreurl .= '&filter=1'; } $patterns[] = '##more##'; $replacement[] = '<a href="' . $moreurl . '"><img src="' . $OUTPUT->pix_url('t/preview') . '" class="iconsmall" alt="' . get_string('more', 'data') . '" title="' . get_string('more', 'data') . '" /></a>'; $patterns[] = '##moreurl##'; $replacement[] = $moreurl; $patterns[] = '##delcheck##'; if ($canmanageentries) { $replacement[] = html_writer::checkbox('delcheck[]', $record->id, false, '', array('class' => 'recordcheckbox')); } else { $replacement[] = ''; } $patterns[] = '##user##'; $replacement[] = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $record->userid . '&course=' . $data->course . '">' . fullname($record) . '</a>'; $patterns[] = '##userpicture##'; $ruser = user_picture::unalias($record, null, 'userid'); $replacement[] = $OUTPUT->user_picture($ruser, array('courseid' => $data->course)); $patterns[] = '##export##'; if (!empty($CFG->enableportfolios) && ($template == 'singletemplate' || $template == 'listtemplate') && (has_capability('mod/data:exportentry', $context) || data_isowner($record->id) && has_capability('mod/data:exportownentry', $context))) { require_once $CFG->libdir . '/portfoliolib.php'; $button = new portfolio_add_button(); $button->set_callback_options('data_portfolio_caller', array('id' => $cm->id, 'recordid' => $record->id), 'mod_data'); list($formats, $files) = data_portfolio_caller::formats($fields, $record); $button->set_formats($formats); $replacement[] = $button->to_html(PORTFOLIO_ADD_ICON_LINK); } else { $replacement[] = ''; } $patterns[] = '##timeadded##'; $replacement[] = userdate($record->timecreated); $patterns[] = '##timemodified##'; $replacement[] = userdate($record->timemodified); $patterns[] = '##approve##'; if (has_capability('mod/data:approve', $context) && $data->approval && !$record->approved) { $approveurl = new moodle_url($jumpurl, array('approve' => $record->id)); $approveicon = new pix_icon('t/approve', get_string('approve', 'data'), '', array('class' => 'iconsmall')); $replacement[] = html_writer::tag('span', $OUTPUT->action_icon($approveurl, $approveicon), array('class' => 'approve')); } else { $replacement[] = ''; } $patterns[] = '##disapprove##'; if (has_capability('mod/data:approve', $context) && $data->approval && $record->approved) { $disapproveurl = new moodle_url($jumpurl, array('disapprove' => $record->id)); $disapproveicon = new pix_icon('t/block', get_string('disapprove', 'data'), '', array('class' => 'iconsmall')); $replacement[] = html_writer::tag('span', $OUTPUT->action_icon($disapproveurl, $disapproveicon), array('class' => 'disapprove')); } else { $replacement[] = ''; } $patterns[] = '##comments##'; if ($template == 'listtemplate' && $data->comments) { if (!empty($CFG->usecomments)) { require_once $CFG->dirroot . '/comment/lib.php'; list($context, $course, $cm) = get_context_info_array($context->id); $cmt = new stdClass(); $cmt->context = $context; $cmt->course = $course; $cmt->cm = $cm; $cmt->area = 'database_entry'; $cmt->itemid = $record->id; $cmt->showcount = true; $cmt->component = 'mod_data'; $comment = new comment($cmt); $replacement[] = $comment->output(true); } } else { $replacement[] = ''; } // actual replacement of the tags $newtext = str_ireplace($patterns, $replacement, $data->{$template}); // no more html formatting and filtering - see MDL-6635 if ($return) { return $newtext; } else { echo $newtext; // hack alert - return is always false in singletemplate anyway ;-) /********************************** * Printing Ratings Form * *********************************/ if ($template == 'singletemplate') { //prints ratings options data_print_ratings($data, $record); } /********************************** * Printing Comments Form * *********************************/ if ($template == 'singletemplate' && $data->comments) { if (!empty($CFG->usecomments)) { require_once $CFG->dirroot . '/comment/lib.php'; list($context, $course, $cm) = get_context_info_array($context->id); $cmt = new stdClass(); $cmt->context = $context; $cmt->course = $course; $cmt->cm = $cm; $cmt->area = 'database_entry'; $cmt->itemid = $record->id; $cmt->showcount = true; $cmt->component = 'mod_data'; $comment = new comment($cmt); $comment->output(false); } } } } }
/** * Print the file manager * * <pre> * $OUTPUT->file_manager($options); * </pre> * * @param array $options associative array with file manager options * options are: * maxbytes=>-1, * maxfiles=>-1, * itemid=>0, * subdirs=>false, * client_id=>uniqid(), * acepted_types=>'*', * return_types=>FILE_INTERNAL, * context=>$PAGE->context * @return string HTML fragment */ function form_filemanager_render($options) { global $CFG, $OUTPUT, $PAGE; $fm = new form_filemanaer_x($options); //TODO: this is unnecessary here, the nested options are getting too complex static $filemanagertemplateloaded; $html = ''; $options = $fm->options; $straddfile = get_string('addfile', 'repository'); $strmakedir = get_string('makeafolder', 'moodle'); $strdownload = get_string('downloadfolder', 'repository'); $strloading = get_string('loading', 'repository'); $icon_progress = $OUTPUT->pix_icon('i/loading_small', $strloading) . ''; $client_id = $options->client_id; $itemid = $options->itemid; list($context, $course, $cm) = get_context_info_array($options->context->id); if (is_object($course)) { $course_maxbytes = $course->maxbytes; } else { $course_maxbytes = $CFG->maxbytes; } if ($options->maxbytes == -1 || empty($options->maxbytes)) { $options->maxbytes = $CFG->maxbytes; } if (empty($options->filecount)) { $extra = ' style="display:none"'; } else { $extra = ''; } $maxsize = get_string('maxfilesize', 'moodle', display_size(get_max_upload_file_size($CFG->maxbytes, $course_maxbytes, $options->maxbytes))); $strdndenabled = get_string('dndenabled_insentence', 'moodle') . $OUTPUT->help_icon('dndenabled'); $loading = get_string('loading', 'repository'); $html .= <<<FMHTML <div class="filemanager-loading mdl-align" id='filemanager-loading-{$client_id}'> {$icon_progress} </div> <div id="filemanager-wrapper-{$client_id}" style="display:none"> <div class="fm-breadcrumb" id="fm-path-{$client_id}"></div> <div class="filemanager-toolbar"> <input type="button" class="fm-btn-add" id="btnadd-{$client_id}" onclick="return false" value="{$straddfile}" /> <input type="button" class="fm-btn-mkdir" id="btncrt-{$client_id}" onclick="return false" value="{$strmakedir}" /> <input type="button" class="fm-btn-download" id="btndwn-{$client_id}" onclick="return false" {$extra} value="{$strdownload}" /> <span> {$maxsize} </span> <span id="dndenabled-{$client_id}" style="display: none"> - {$strdndenabled} </span> </div> <div class="filemanager-container" id="filemanager-{$client_id}" style="position: relative" > <ul id="draftfiles-{$client_id}" class="fm-filelist"> <li>{$loading}</li> </ul> </div> </div> <div class='clearer'></div> FMHTML; if (empty($filemanagertemplateloaded)) { $filemanagertemplateloaded = true; $html .= <<<FMHTML <div id="fm-template" style="display:none">___fullname___ ___action___</div> FMHTML; } $module = array('name' => 'form_filemanager', 'fullpath' => '/lib/form/filemanager.js', 'requires' => array('core_filepicker', 'base', 'io-base', 'node', 'json', 'yui2-button', 'yui2-container', 'yui2-layout', 'yui2-menu', 'yui2-treeview', 'core_dndupload'), 'strings' => array(array('loading', 'repository'), array('nomorefiles', 'repository'), array('confirmdeletefile', 'repository'), array('add', 'repository'), array('accessiblefilepicker', 'repository'), array('move', 'moodle'), array('cancel', 'moodle'), array('download', 'moodle'), array('ok', 'moodle'), array('emptylist', 'repository'), array('nofilesattached', 'repository'), array('entername', 'repository'), array('enternewname', 'repository'), array('zip', 'editor'), array('unzip', 'moodle'), array('rename', 'moodle'), array('delete', 'moodle'), array('cannotdeletefile', 'error'), array('confirmdeletefile', 'repository'), array('nopathselected', 'repository'), array('popupblockeddownload', 'repository'), array('draftareanofiles', 'repository'), array('path', 'moodle'), array('setmainfile', 'repository'), array('moving', 'repository'), array('files', 'moodle'), array('serverconnection', 'error'))); $PAGE->requires->js_module($module); $PAGE->requires->js_init_call('M.form_filemanager.init', array($options), true, $module); // non javascript file manager $filemanagerurl = new moodle_url('/repository/draftfiles_manager.php', array('env' => 'filemanager', 'action' => 'browse', 'itemid' => $itemid, 'subdirs' => $options->subdirs, 'maxbytes' => $options->maxbytes, 'maxfiles' => $options->maxfiles, 'ctx_id' => $PAGE->context->id, 'course' => $PAGE->course->id, 'sesskey' => sesskey())); $html .= '<noscript>'; $html .= "<div><object type='text/html' data='{$filemanagerurl}' height='160' width='600' style='border:1px solid #000'></object></div>"; $html .= '</noscript>'; return $html; }
* Handling all ajax request for comments API * * @package core * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ define('AJAX_SCRIPT', true); define('NO_DEBUG_DISPLAY', true); require_once '../config.php'; require_once $CFG->dirroot . '/comment/lib.php'; $contextid = optional_param('contextid', SYSCONTEXTID, PARAM_INT); $action = optional_param('action', '', PARAM_ALPHA); if (empty($CFG->usecomments)) { throw new comment_exception('commentsnotenabled', 'moodle'); } list($context, $course, $cm) = get_context_info_array($contextid); if ($contextid == SYSCONTEXTID) { $course = $SITE; } $PAGE->set_url('/comment/comment_ajax.php'); // Allow anonymous user to view comments providing forcelogin now enabled require_course_login($course, true, $cm); $PAGE->set_context($context); if (!empty($cm)) { $PAGE->set_cm($cm, $course); } else { if (!empty($course)) { $PAGE->set_course($course); } } if (!confirm_sesskey()) {
/** * Returns the list of gradable areas in the given context and component * * This performs a callback to the library of the relevant plugin to obtain * the list of supported areas. * @return array of (string)areacode => (string)localized title of the area */ public function get_available_areas() { global $CFG; $this->ensure_isset(array('context', 'component')); if ($this->get_context()->contextlevel == CONTEXT_SYSTEM) { if ($this->get_component() !== 'core_grading') { throw new coding_exception('Unsupported component at the system context'); } else { return array(); } } else { if ($this->get_context()->contextlevel == CONTEXT_MODULE) { list($context, $course, $cm) = get_context_info_array($this->get_context()->id); return self::available_areas('mod_' . $cm->modname); } else { throw new coding_exception('Unsupported gradable area context level'); } } }
/** * Get file listing * * @param string $encodedpath * @return mixed */ public function get_listing($encodedpath = '', $page = '') { global $CFG, $USER, $OUTPUT; $ret = array(); $ret['dynload'] = true; $ret['nosearch'] = true; $ret['nologin'] = true; $list = array(); $component = 'course'; $filearea = 'legacy'; $itemid = 0; $browser = get_file_browser(); if (!empty($encodedpath)) { $params = json_decode(base64_decode($encodedpath), true); if (is_array($params)) { $filepath = is_null($params['filepath']) ? NULL : clean_param($params['filepath'], PARAM_PATH); $filename = is_null($params['filename']) ? NULL : clean_param($params['filename'], PARAM_FILE); $context = context::instance_by_id(clean_param($params['contextid'], PARAM_INT)); } } else { $filename = null; $filepath = null; list($context, $course, $cm) = get_context_info_array($this->context->id); $courseid = is_object($course) ? $course->id : SITEID; $context = context_course::instance($courseid); } if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) { // build path navigation $pathnodes = array(); $encodedpath = base64_encode(json_encode($fileinfo->get_params())); $pathnodes[] = array('name' => $fileinfo->get_visible_name(), 'path' => $encodedpath); $level = $fileinfo->get_parent(); while ($level) { $params = $level->get_params(); $encodedpath = base64_encode(json_encode($params)); if ($params['contextid'] != $context->id) { break; } $pathnodes[] = array('name' => $level->get_visible_name(), 'path' => $encodedpath); $level = $level->get_parent(); } if (!empty($pathnodes) && is_array($pathnodes)) { $pathnodes = array_reverse($pathnodes); $ret['path'] = $pathnodes; } // build file tree $children = $fileinfo->get_children(); foreach ($children as $child) { if ($child->is_directory()) { $params = $child->get_params(); $subdir_children = $child->get_children(); $encodedpath = base64_encode(json_encode($params)); $node = array('title' => $child->get_visible_name(), 'datemodified' => $child->get_timemodified(), 'datecreated' => $child->get_timecreated(), 'path' => $encodedpath, 'children' => array(), 'thumbnail' => $OUTPUT->pix_url(file_folder_icon(90))->out(false)); $list[] = $node; } else { $encodedpath = base64_encode(json_encode($child->get_params())); $node = array('title' => $child->get_visible_name(), 'size' => $child->get_filesize(), 'author' => $child->get_author(), 'license' => $child->get_license(), 'datemodified' => $child->get_timemodified(), 'datecreated' => $child->get_timecreated(), 'source' => $encodedpath, 'isref' => $child->is_external_file(), 'thumbnail' => $OUTPUT->pix_url(file_file_icon($child, 90))->out(false)); if ($child->get_status() == 666) { $node['originalmissing'] = true; } if ($imageinfo = $child->get_imageinfo()) { $fileurl = new moodle_url($child->get_url()); $node['realthumbnail'] = $fileurl->out(false, array('preview' => 'thumb', 'oid' => $child->get_timemodified())); $node['realicon'] = $fileurl->out(false, array('preview' => 'tinyicon', 'oid' => $child->get_timemodified())); $node['image_width'] = $imageinfo['width']; $node['image_height'] = $imageinfo['height']; } $list[] = $node; } } } else { $list = array(); } $ret['list'] = array_filter($list, array($this, 'filter')); return $ret; }
/** * Called by pluginfile.php to serve files related to the 'question' core * component and for files belonging to qtypes. * * For files that relate to questions in a question_attempt, then we delegate to * a function in the component that owns the attempt (for example in the quiz, * or in core question preview) to get necessary inforation. * * (Note that, at the moment, all question file areas relate to questions in * attempts, so the If at the start of the last paragraph is always true.) * * Does not return, either calls send_file_not_found(); or serves the file. * * @param object $course course settings object * @param object $context context object * @param string $component the name of the component we are serving files for. * @param string $filearea the name of the file area. * @param array $args the remaining bits of the file path. * @param bool $forcedownload whether the user must be forced to download the file. */ function question_pluginfile($course, $context, $component, $filearea, $args, $forcedownload) { global $DB, $CFG; list($context, $course, $cm) = get_context_info_array($context->id); require_login($course, false, $cm); if ($filearea === 'export') { require_once $CFG->dirroot . '/question/editlib.php'; $contexts = new question_edit_contexts($context); // check export capability $contexts->require_one_edit_tab_cap('export'); $category_id = (int) array_shift($args); $format = array_shift($args); $cattofile = array_shift($args); $contexttofile = array_shift($args); $filename = array_shift($args); // load parent class for import/export require_once $CFG->dirroot . '/question/format.php'; require_once $CFG->dirroot . '/question/editlib.php'; require_once $CFG->dirroot . '/question/format/' . $format . '/format.php'; $classname = 'qformat_' . $format; if (!class_exists($classname)) { send_file_not_found(); } $qformat = new $classname(); if (!($category = $DB->get_record('question_categories', array('id' => $category_id)))) { send_file_not_found(); } $qformat->setCategory($category); $qformat->setContexts($contexts->having_one_edit_tab_cap('export')); $qformat->setCourse($course); if ($cattofile == 'withcategories') { $qformat->setCattofile(true); } else { $qformat->setCattofile(false); } if ($contexttofile == 'withcontexts') { $qformat->setContexttofile(true); } else { $qformat->setContexttofile(false); } if (!$qformat->exportpreprocess()) { send_file_not_found(); print_error('exporterror', 'question', $thispageurl->out()); } // export data to moodle file pool if (!($content = $qformat->exportprocess(true))) { send_file_not_found(); } //DEBUG //echo '<textarea cols=90 rows=20>'; //echo $content; //echo '</textarea>'; //die; send_file($content, $filename, 0, 0, true, true, $qformat->mime_type()); } $attemptid = (int) array_shift($args); $questionid = (int) array_shift($args); if ($attemptid === 0) { // preview require_once $CFG->dirroot . '/question/previewlib.php'; return question_preview_question_pluginfile($course, $context, $component, $filearea, $attemptid, $questionid, $args, $forcedownload); } else { $module = $DB->get_field('question_attempts', 'modulename', array('id' => $attemptid)); $dir = get_component_directory($module); if (!file_exists("{$dir}/lib.php")) { send_file_not_found(); } include_once "{$dir}/lib.php"; $filefunction = $module . '_question_pluginfile'; if (!function_exists($filefunction)) { send_file_not_found(); } $filefunction($course, $context, $component, $filearea, $attemptid, $questionid, $args, $forcedownload); send_file_not_found(); } }
function print_content() { global $CFG, $OUTPUT, $USER, $PAGE; require_once($CFG->dirroot . '/mod/wiki/locallib.php'); $page = $this->page; $subwiki = $this->subwiki; $wiki = $PAGE->activityrecord; list($context, $course, $cm) = get_context_info_array($this->modcontext->id); require_capability('mod/wiki:viewcomment', $this->modcontext, NULL, true, 'noviewcommentpermission', 'wiki'); $comments = wiki_get_comments($this->modcontext->id, $page->id); if (has_capability('mod/wiki:editcomment', $this->modcontext)) { echo '<div class="midpad"><a href="' . $CFG->wwwroot . '/mod/wiki/editcomments.php?action=add&pageid=' . $page->id . '">' . get_string('addcomment', 'wiki') . '</a></div>'; } $options = array('swid' => $this->page->subwikiid, 'pageid' => $page->id); $version = wiki_get_current_version($this->page->id); $format = $version->contentformat; if (empty($comments)) { echo $OUTPUT->heading(get_string('nocomments', 'wiki')); } foreach ($comments as $comment) { $user = wiki_get_user_info($comment->userid); $fullname = fullname($user, has_capability('moodle/site:viewfullnames', context_course::instance($course->id))); $by = new stdclass(); $by->name = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $user->id . '&course=' . $course->id . '">' . $fullname . '</a>'; $by->date = userdate($comment->timecreated); $t = new html_table(); $cell1 = new html_table_cell($OUTPUT->user_picture($user, array('popup' => true))); $cell2 = new html_table_cell(get_string('bynameondate', 'forum', $by)); $cell3 = new html_table_cell(); $cell3->atributtes ['width'] = "80%"; $cell4 = new html_table_cell(); $cell5 = new html_table_cell(); $row1 = new html_table_row(); $row1->cells[] = $cell1; $row1->cells[] = $cell2; $row2 = new html_table_row(); $row2->cells[] = $cell3; if ($format != 'html') { if ($format == 'creole') { $parsedcontent = wiki_parse_content('creole', $comment->content, $options); } else if ($format == 'nwiki') { $parsedcontent = wiki_parse_content('nwiki', $comment->content, $options); } $cell4->text = format_text(html_entity_decode($parsedcontent['parsed_text'], ENT_QUOTES, 'UTF-8'), FORMAT_HTML); } else { $cell4->text = format_text($comment->content, FORMAT_HTML); } $row2->cells[] = $cell4; $t->data = array($row1, $row2); $actionicons = false; if ((has_capability('mod/wiki:managecomment', $this->modcontext))) { $urledit = new moodle_url('/mod/wiki/editcomments.php', array('commentid' => $comment->id, 'pageid' => $page->id, 'action' => 'edit')); $urldelet = new moodle_url('/mod/wiki/instancecomments.php', array('commentid' => $comment->id, 'pageid' => $page->id, 'action' => 'delete')); $actionicons = true; } else if ((has_capability('mod/wiki:editcomment', $this->modcontext)) and ($USER->id == $user->id)) { $urledit = new moodle_url('/mod/wiki/editcomments.php', array('commentid' => $comment->id, 'pageid' => $page->id, 'action' => 'edit')); $urldelet = new moodle_url('/mod/wiki/instancecomments.php', array('commentid' => $comment->id, 'pageid' => $page->id, 'action' => 'delete')); $actionicons = true; } if ($actionicons) { $cell6 = new html_table_cell($OUTPUT->action_icon($urledit, new pix_icon('t/edit', get_string('edit'), '', array('class' => 'iconsmall'))) . $OUTPUT->action_icon($urldelet, new pix_icon('t/delete', get_string('delete'), '', array('class' => 'iconsmall')))); $row3 = new html_table_row(); $row3->cells[] = $cell5; $row3->cells[] = $cell6; $t->data[] = $row3; } echo html_writer::tag('div', html_writer::table($t), array('class'=>'no-overflow')); } }
/** * Get file listing * * @param string $encodedpath * @return mixed */ public function get_listing($encodedpath = '') { global $CFG, $USER, $OUTPUT; $ret = array(); $ret['dynload'] = true; $ret['nosearch'] = true; $ret['nologin'] = true; $list = array(); if (!empty($encodedpath)) { $params = unserialize(base64_decode($encodedpath)); if (is_array($params)) { $component = is_null($params['component']) ? NULL : clean_param($params['component'], PARAM_COMPONENT); $filearea = is_null($params['filearea']) ? NULL : clean_param($params['filearea'], PARAM_AREA); $itemid = is_null($params['itemid']) ? NULL : clean_param($params['itemid'], PARAM_INT); $filepath = is_null($params['filepath']) ? NULL : clean_param($params['filepath'], PARAM_PATH);; $filename = is_null($params['filename']) ? NULL : clean_param($params['filename'], PARAM_FILE); $context = get_context_instance_by_id(clean_param($params['contextid'], PARAM_INT)); } } else { $itemid = null; $filename = null; $filearea = null; $filepath = null; $component = null; if (!empty($this->context)) { list($context, $course, $cm) = get_context_info_array($this->context->id); $courseid = is_object($course) ? $course->id : SITEID; $context = get_context_instance(CONTEXT_COURSE, $courseid); } else { $context = get_system_context(); } } $browser = get_file_browser(); if ($fileinfo = $browser->get_file_info($context, $component, $filearea, $itemid, $filepath, $filename)) { // build path navigation $pathnodes = array(); $encodedpath = base64_encode(serialize($fileinfo->get_params())); $pathnodes[] = array('name'=>$fileinfo->get_visible_name(), 'path'=>$encodedpath); $level = $fileinfo->get_parent(); while ($level) { $encodedpath = base64_encode(serialize($level->get_params())); $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath); $level = $level->get_parent(); } if (!empty($pathnodes) && is_array($pathnodes)) { $pathnodes = array_reverse($pathnodes); $ret['path'] = $pathnodes; } // build file tree $children = $fileinfo->get_children(); foreach ($children as $child) { if ($child->is_directory()) { if ($child->is_empty_area()) { continue; } $params = $child->get_params(); $encodedpath = base64_encode(serialize($params)); // hide user_private area from local plugin, user should // use private file plugin to access private files //if ($params['filearea'] == 'user_private') { //continue; //} $node = array( 'title' => $child->get_visible_name(), 'size' => 0, 'date' => '', 'path' => $encodedpath, 'children'=>array(), 'thumbnail' => $OUTPUT->pix_url('f/folder-32')->out(false) ); $list[] = $node; } else { $encodedpath = base64_encode(serialize($child->get_params())); $node = array( 'title' => $child->get_visible_name(), 'size' => 0, 'date' => '', 'source'=> $encodedpath, 'thumbnail' => $OUTPUT->pix_url(file_extension_icon($child->get_visible_name(), 32))->out(false) ); $list[] = $node; } } } else { // if file doesn't exist, build path nodes root of current context $pathnodes = array(); $fileinfo = $browser->get_file_info($context, null, null, null, null, null); $encodedpath = base64_encode(serialize($fileinfo->get_params())); $pathnodes[] = array('name'=>$fileinfo->get_visible_name(), 'path'=>$encodedpath); $level = $fileinfo->get_parent(); while ($level) { $encodedpath = base64_encode(serialize($level->get_params())); $pathnodes[] = array('name'=>$level->get_visible_name(), 'path'=>$encodedpath); $level = $level->get_parent(); } if (!empty($pathnodes) && is_array($pathnodes)) { $pathnodes = array_reverse($pathnodes); $ret['path'] = $pathnodes; } $list = array(); } $ret['list'] = array_filter($list, array($this, 'filter')); return $ret; }
/** * Delete comments from database * * @param $idcomment. Id of comment which will be deleted * @param $context. Current context * @param $pageid. Current pageid **/ function wiki_delete_comment($idcomment, $context, $pageid) { global $CFG; require_once $CFG->dirroot . '/comment/lib.php'; list($context, $course, $cm) = get_context_info_array($context->id); $cmt = new stdClass(); $cmt->context = $context; $cmt->itemid = $pageid; $cmt->area = 'wiki_page'; $cmt->course = $course; $cmt->component = 'mod_wiki'; $manager = new comment($cmt); $manager->delete($idcomment); }
/** * Test context fetching. */ public function test_get_context_info_array() { $this->resetAfterTest(); $syscontext = context_system::instance(); $user = $this->getDataGenerator()->create_user(); $usercontext = context_user::instance($user->id); $course = $this->getDataGenerator()->create_course(); $catcontext = context_coursecat::instance($course->category); $coursecontext = context_course::instance($course->id); $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id)); $modcontext = context_module::instance($page->cmid); $cm = get_coursemodule_from_instance('page', $page->id); $block1 = $this->getDataGenerator()->create_block('online_users', array('parentcontextid' => $coursecontext->id)); $block1context = context_block::instance($block1->id); $block2 = $this->getDataGenerator()->create_block('online_users', array('parentcontextid' => $modcontext->id)); $block2context = context_block::instance($block2->id); $result = get_context_info_array($syscontext->id); $this->assertCount(3, $result); $this->assertEquals($syscontext, $result[0]); $this->assertNull($result[1]); $this->assertNull($result[2]); $result = get_context_info_array($usercontext->id); $this->assertCount(3, $result); $this->assertEquals($usercontext, $result[0]); $this->assertNull($result[1]); $this->assertNull($result[2]); $result = get_context_info_array($catcontext->id); $this->assertCount(3, $result); $this->assertEquals($catcontext, $result[0]); $this->assertNull($result[1]); $this->assertNull($result[2]); $result = get_context_info_array($coursecontext->id); $this->assertCount(3, $result); $this->assertEquals($coursecontext, $result[0]); $this->assertEquals($course->id, $result[1]->id); $this->assertSame($course->shortname, $result[1]->shortname); $this->assertNull($result[2]); $result = get_context_info_array($block1context->id); $this->assertCount(3, $result); $this->assertEquals($block1context, $result[0]); $this->assertEquals($course->id, $result[1]->id); $this->assertEquals($course->shortname, $result[1]->shortname); $this->assertNull($result[2]); $result = get_context_info_array($modcontext->id); $this->assertCount(3, $result); $this->assertEquals($modcontext, $result[0]); $this->assertEquals($course->id, $result[1]->id); $this->assertSame($course->shortname, $result[1]->shortname); $this->assertEquals($cm->id, $result[2]->id); $this->assertEquals($cm->groupmembersonly, $result[2]->groupmembersonly); $result = get_context_info_array($block2context->id); $this->assertCount(3, $result); $this->assertEquals($block2context, $result[0]); $this->assertEquals($course->id, $result[1]->id); $this->assertSame($course->shortname, $result[1]->shortname); $this->assertEquals($cm->id, $result[2]->id); $this->assertEquals($cm->groupmembersonly, $result[2]->groupmembersonly); }