MDL-22893, assignment module should use filepicker and filemanager
[moodle.git] / mod / assignment / renderer.php
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.
14 //
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.
21  *
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 {
28     /**
29      * @return string
30      */
31     public function assignment_files($context, $itemid) {
32         return $this->render(new assignment_files($context, $itemid));
33     }
35     public function render_assignment_files(assignment_files $tree) {
36         $module = array('name'=>'mod_assignment_files', 'fullpath'=>'/mod/assignment/assignment.js', 'requires'=>array('yui2-treeview'));
37         $htmlid = 'assignment_files_tree_'.uniqid();
38         $this->page->requires->js_init_call('M.mod_assignment.init_tree', array(true, $htmlid));
39         $html = '<div id="'.$htmlid.'">';
40         $html .= $this->htmllize_tree($tree, $tree->dir);
41         $html .= '</div>';
42         return $html;
43     }
45     /**
46      * Internal function - creates htmls structure suitable for YUI tree.
47      */
48     protected function htmllize_tree($tree, $dir) {
49         global $CFG;
50         $yuiconfig = array();
51         $yuiconfig['type'] = 'html';
53         if (empty($dir['subdirs']) and empty($dir['files'])) {
54             return '';
55         }
56         $result = '<ul>';
57         foreach ($dir['subdirs'] as $subdir) {
58             $image = $this->output->pix_icon("/f/folder", $subdir['dirname'], 'moodle', array('class'=>'icon'));
59             $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.s($subdir['dirname']).'</div> '.$this->htmllize_tree($tree, $subdir).'</li>';
60         }
61         foreach ($dir['files'] as $file) {
62             $url = file_encode_url("$CFG->wwwroot/pluginfile.php", '/'.$tree->context->id.'/mod_assignment/submission/'.$file->get_itemid().'/'.$file->get_filepath().$file->get_filename(), true);
63             $filename = $file->get_filename();
64             $icon = substr(mimeinfo("icon", $filename), 0, -4);
65             $image = $this->output->pix_icon("/f/$icon", $filename, 'moodle', array('class'=>'icon'));
66             $result .= '<li yuiConfig=\''.json_encode($yuiconfig).'\'><div>'.$image.' '.html_writer::link($url, $filename).'</div></li>';
67         }
68         $result .= '</ul>';
70         return $result;
71     }
72 }
74 class assignment_files implements renderable {
75     public $context;
76     public $dir;
77     public function __construct($context, $itemid) {
78         global $USER;
79         $this->context = $context;
80         $fs = get_file_storage();
81         $this->dir = $fs->get_area_tree($this->context->id, 'mod_assignment', 'submission', $itemid);
82     }
83 }