-
Notifications
You must be signed in to change notification settings - Fork 3
/
exportposts.php
151 lines (130 loc) · 5.53 KB
/
exportposts.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @package mod_openstudio
* @copyright 2017 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../config.php');
use mod_openstudio\local\api\export;
use mod_openstudio\local\api\stream;
use mod_openstudio\local\util;
use mod_openstudio\local\api\content;
use mod_openstudio\local\api\folder;
$id = required_param('id', PARAM_INT); // Course_module ID.
$vid = required_param('vid', PARAM_INT); // Visibility.
$contentids = explode(',', required_param('contentids', PARAM_TEXT)); // Content IDs.
// Page init and security checks.
$coursedata = util::render_page_init($id, array('mod/openstudio:view'));
$cm = $coursedata->cm;
$cminstance = $coursedata->cminstance;
$course = $coursedata->course;
$theme = $coursedata->theme;
require_login($course, true, $cm);
// Set up page and breadcrumb.
$pagetitle = $pageheading = 'Export posts';
$pageurl = util::get_current_url();
$exportpageurl = new moodle_url('/mod/openstudio/view.php', array('id' => $id, 'vid' => $vid));
util::page_setup($PAGE, $pagetitle, $pageheading, $pageurl, $course, $cm);
util::add_breadcrumb($PAGE, $cm->id, navigation_node::TYPE_ACTIVITY, array(
get_string('menumycontent', 'mod_openstudio') => $exportpageurl,
get_string('navmypinboard', 'mod_openstudio') => $exportpageurl));
// Get contents.
$contentdatatemp = stream::get_contents_by_ids($USER->id, $contentids);
$contentdata = (object) array('contents' => array());
$index = 1;
$folderitemfilesizes = array();
foreach ($contentdatatemp as $content) {
$contentids[] = $content->id;
// Skip empty slot and empty folder.
if (empty($content->name)) {
if (!empty($content->l1name)) {
$content->name = $content->l1name;
} else if (!empty($content->l2name)) {
$content->name = $content->l2name;
} else if (!empty($content->l3name)) {
$content->name = $content->l3name;
}
}
// Calculate folder size.
if ($content->contenttype == content::TYPE_FOLDER) {
$numberofemptycontent = 0;
// Get content IDs inside folder.
$foldercontentids = array();
$foldercontenttemp = folder::get_contents($content->id);
$numberofcontent = count($foldercontenttemp);
foreach ($foldercontenttemp as $folderitem) {
if (empty($folderitem->description) && empty($folderitem->fileid)
&& empty($folderitem->content)) {
$numberofemptycontent++;
continue;
}
$foldercontentids[] = $folderitem->id;
}
// Calculate content size with content type is file.
$rs = export::get_files($cminstance->id, $USER->id, null, $foldercontentids, 0, count($foldercontentids));
$folderitemfilesizes[$content->id] = 0;
foreach ($rs as $r) {
$folderitemfilesizes[$content->id] += $r->filesize;
}
if ($numberofcontent == $numberofemptycontent) {
// It mean all the content in folder is empty or don't have any content.
// We need to check description of the folder.
if (empty($content->description) && empty($content->fileid) && empty($content->content)) {
continue;
}
}
} else {
if (empty($content->description) && empty($content->fileid) && empty($content->content)) {
continue;
}
}
$content->icon = util::get_content_file_icon($content->contenttype);
if ($content->levelid == 0) {
$placeholdertext = $theme->themepinboardname;
} else {
$placeholdertext = $theme->themestudioname;
}
$content->location = $placeholdertext;
$content->index = $index ++;
$contentdata->contents[] = $content;
}
// Calculate content size with content type is file.
$rs = export::get_files($cminstance->id, $USER->id, null, $contentids, 0, count($contentids));
$contentfilesizes = array();
foreach ($rs as $r) {
$contentfilesizes[$r->id] = $r->filesize;
}
foreach ($contentdata->contents as $key => $content) {
if (isset($contentfilesizes[$content->id])) {
$contentdata->contents[$key]->size = util::human_filesize(@$contentfilesizes[$content->id]);
} else if (isset($folderitemfilesizes[$content->id])) {
$contentdata->contents[$key]->size = util::human_filesize(@$folderitemfilesizes[$content->id]);
} else {
$contentdata->contents[$key]->size = 0;
}
}
// Render page.
$renderer = $PAGE->get_renderer('mod_openstudio');
$PAGE->set_button($renderer->searchform($theme, $vid, $id));
echo $OUTPUT->header(); // Header.
echo $renderer->exportposts($contentdata); // Body.
$PAGE->requires->js_call_amd('mod_openstudio/export', 'init', [[
"id" => $id,
"vid" => $vid,
"contentids" => $contentids]]);
echo $OUTPUT->footer();
util::trigger_event($cm->id, 'export_viewed', '', util::get_page_name_and_params(true));