Merge branch 'install_21_STABLE' of git://git.moodle.cz/moodle-install into MOODLE_21...
[moodle.git] / mod / assignment / renderer.php
blob2faed7ac531d832e98949ed7c4f967f21f71029e
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * A custom renderer class that extends the plugin_renderer_base and
20 * is used by the assignment module.
22 * @package mod-assignment
23 * @copyright 2010 Dongsheng Cai <dongsheng@moodle.com>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 **/
26 class mod_assignment_renderer extends plugin_renderer_base {
27 /**
28 * @return string
30 public function assignment_files($context, $itemid, $filearea='submission') {
31 return $this->render(new assignment_files($context, $itemid, $filearea));
34 public function render_assignment_files(assignment_files $tree) {
35 $module = array('name'=>'mod_assignment_files', 'fullpath'=>'/mod/assignment/assignment.js', 'requires'=>array('yui2-treeview'));
36 $this->htmlid = 'assignment_files_tree_'.uniqid();
37 $this->page->requires->js_init_call('M.mod_assignment.init_tree', array(true, $this->htmlid));
38 $html = '<div id="'.$this->htmlid.'">';
39 $html .= $this->htmllize_tree($tree, $tree->dir);
40 $html .= '</div>';
42 if ($tree->portfolioform) {
43 $html .= $tree->portfolioform;
45 return $html;
48 /**
49 * Internal function - creates htmls structure suitable for YUI tree.
51 protected function htmllize_tree($tree, $dir) {
52 global $CFG;
53 $yuiconfig = array();
54 $yuiconfig['type'] = 'html';
56 if (empty($dir['subdirs']) and empty($dir['files'])) {
57 return '';
60 $result = '<ul>';
61 foreach ($dir['subdirs'] as $subdir) {
62 $image = $this->output->pix_icon("f/folder", $subdir['dirname'], 'moodle', array('class'=>'icon'));
63 $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.s($subdir['dirname']).'</div> '.$this->htmllize_tree($tree, $subdir).'</li>';
66 foreach ($dir['files'] as $file) {
67 $filename = $file->get_filename();
68 $icon = mimeinfo("icon", $filename);
69 if ($CFG->enableplagiarism) {
70 require_once($CFG->libdir.'/plagiarismlib.php');
71 $plagiarsmlinks = plagiarism_get_links(array('userid'=>$file->get_userid(), 'file'=>$file, 'cmid'=>$tree->cm->id, 'course'=>$tree->course));
72 } else {
73 $plagiarsmlinks = '';
75 $image = $this->output->pix_icon("f/$icon", $filename, 'moodle', array('class'=>'icon'));
76 $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.$file->fileurl.' '.$plagiarsmlinks.$file->portfoliobutton.'</div></li>';
79 $result .= '</ul>';
81 return $result;
85 class assignment_files implements renderable {
86 public $context;
87 public $dir;
88 public $portfolioform;
89 public $cm;
90 public $course;
91 public function __construct($context, $itemid, $filearea='submission') {
92 global $USER, $CFG;
93 $this->context = $context;
94 list($context, $course, $cm) = get_context_info_array($context->id);
95 $this->cm = $cm;
96 $this->course = $course;
97 $fs = get_file_storage();
98 $this->dir = $fs->get_area_tree($this->context->id, 'mod_assignment', $filearea, $itemid);
99 if (!empty($CFG->enableportfolios)) {
100 require_once($CFG->libdir . '/portfoliolib.php');
101 $files = $fs->get_area_files($this->context->id, 'mod_assignment', $filearea, $itemid, "timemodified", false);
102 if (count($files) >= 1 && has_capability('mod/assignment:exportownsubmission', $this->context)) {
103 $button = new portfolio_add_button();
104 $button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id, 'submissionid' => $itemid), '/mod/assignment/locallib.php');
105 $button->reset_formats();
106 $this->portfolioform = $button->to_html(PORTFOLIO_ADD_TEXT_LINK);
109 $this->preprocess($this->dir, $filearea);
111 public function preprocess($dir, $filearea) {
112 global $CFG;
113 foreach ($dir['subdirs'] as $subdir) {
114 $this->preprocess($subdir, $filearea);
116 foreach ($dir['files'] as $file) {
117 $file->portfoliobutton = '';
118 if (!empty($CFG->enableportfolios)) {
119 $button = new portfolio_add_button();
120 if (has_capability('mod/assignment:exportownsubmission', $this->context)) {
121 $button->set_callback_options('assignment_portfolio_caller', array('id' => $this->cm->id, 'fileid' => $file->get_id()), '/mod/assignment/locallib.php');
122 $button->set_format_by_file($file);
123 $file->portfoliobutton = $button->to_html(PORTFOLIO_ADD_ICON_LINK);
126 $url = file_encode_url("$CFG->wwwroot/pluginfile.php", '/'.$this->context->id.'/mod_assignment/'.$filearea.'/'.$file->get_itemid(). $file->get_filepath().$file->get_filename(), true);
127 $filename = $file->get_filename();
128 $file->fileurl = html_writer::link($url, $filename);