Skip to content

Commit 564639b

Browse files
committed
Add file question type
1 parent b8be86b commit 564639b

File tree

16 files changed

+862
-5
lines changed

16 files changed

+862
-5
lines changed

classes/question/file.php

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
/**
18+
* This file contains the parent class for text question types.
19+
*
20+
* @author Laurent David
21+
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
22+
* @package questiontypes
23+
*/
24+
25+
namespace mod_questionnaire\question;
26+
27+
use context_module;
28+
use core_media_manager;
29+
use form_filemanager;
30+
use mod_questionnaire\file_storage;
31+
use moodle_url;
32+
use stdClass;
33+
34+
defined('MOODLE_INTERNAL') || die();
35+
36+
class file extends question {
37+
38+
/**
39+
* @return string
40+
*/
41+
public function helpname() {
42+
return 'file';
43+
}
44+
45+
/**
46+
* Override and return a form template if provided. Output of question_survey_display is iterpreted based on this.
47+
*
48+
* @return boolean | string
49+
*/
50+
public function question_template() {
51+
return false;
52+
}
53+
54+
/**
55+
* Override and return a response template if provided. Output of response_survey_display is iterpreted based on this.
56+
*
57+
* @return boolean | string
58+
*/
59+
public function response_template() {
60+
return false;
61+
}
62+
63+
/**
64+
* @return object|string
65+
*/
66+
protected function responseclass() {
67+
return '\\mod_questionnaire\\responsetype\\file';
68+
}
69+
70+
/**
71+
* @param \mod_questionnaire\responsetype\response\response $response
72+
* @param $descendantsdata
73+
* @param bool $blankquestionnaire
74+
* @return object|string
75+
*/
76+
protected function question_survey_display($response, $descendantsdata, $blankquestionnaire = false) {
77+
global $CFG, $PAGE;
78+
require_once($CFG->libdir . '/filelib.php');
79+
$elname = 'q' . $this->id;
80+
$draftitemid = file_get_submitted_draft_itemid($elname);
81+
$component = 'mod_questionnaire';
82+
$options = $this->get_file_manager_option();
83+
file_prepare_draft_area($draftitemid, $this->context->id, $component, 'file', $this->id, $options);
84+
// Filemanager form element implementation is far from optimal, we need to rework this if we ever fix it...
85+
require_once("$CFG->dirroot/lib/form/filemanager.php");
86+
87+
$fmoptions = array_merge(
88+
$options,
89+
[
90+
'client_id' => uniqid(),
91+
'itemid' => $draftitemid,
92+
'target' => $this->id,
93+
'name' => $elname
94+
]
95+
);
96+
$fm = new form_filemanager((object) $fmoptions);
97+
$output = $PAGE->get_renderer('core', 'files');
98+
$html = $output->render($fm);
99+
100+
$html .= '<input value="' . $draftitemid . '" name="' . $elname . '" type="hidden" />';
101+
$html .= '<input value="" id="' . $this->id . '" type="hidden" />';
102+
103+
return $html;
104+
}
105+
106+
private function get_file_manager_option() {
107+
return [
108+
'mainfile' => '',
109+
'subdirs' => false,
110+
'accepted_types' => array('image', '.pdf')
111+
];
112+
}
113+
114+
/**
115+
* @param \mod_questionnaire\responsetype\response\response $response
116+
* @return object|string
117+
*/
118+
protected function response_survey_display($response) {
119+
global $PAGE, $CFG;
120+
require_once($CFG->libdir . '/filelib.php');
121+
require_once($CFG->libdir . '/resourcelib.php');
122+
if (isset($response->answers[$this->id])) {
123+
$answer = reset($response->answers[$this->id]);
124+
} else {
125+
return '';
126+
}
127+
$fs = get_file_storage();
128+
$file = $fs->get_file_by_id($answer->value);
129+
130+
$moodleurl = moodle_url::make_pluginfile_url(
131+
$file->get_contextid(),
132+
$file->get_component(),
133+
$file->get_filearea(),
134+
$file->get_itemid(),
135+
$file->get_filepath(),
136+
$file->get_filename());
137+
138+
$mimetype = $file->get_mimetype();
139+
$title = '';
140+
141+
$extension = resourcelib_get_extension($file->get_filename());
142+
143+
$mediamanager = core_media_manager::instance($PAGE);
144+
$embedoptions = array(
145+
core_media_manager::OPTION_TRUSTED => true,
146+
core_media_manager::OPTION_BLOCK => true,
147+
);
148+
149+
if (file_mimetype_in_typegroup($mimetype, 'web_image')) { // It's an image
150+
$code = resourcelib_embed_image($moodleurl->out(), $title);
151+
152+
} else if ($mimetype === 'application/pdf') {
153+
// PDF document
154+
$code = resourcelib_embed_pdf($moodleurl->out(), $title, get_string('view'));
155+
156+
} else if ($mediamanager->can_embed_url($moodleurl, $embedoptions)) {
157+
// Media (audio/video) file.
158+
$code = $mediamanager->embed_url($moodleurl, $title, 0, 0, $embedoptions);
159+
160+
} else {
161+
// We need a way to discover if we are loading remote docs inside an iframe.
162+
$moodleurl->param('embed', 1);
163+
164+
// anything else - just try object tag enlarged as much as possible
165+
$code = resourcelib_embed_general($moodleurl, $title, get_string('view'), $mimetype);
166+
}
167+
168+
$output = '';
169+
$output .= '<div class="response text">';
170+
$output .= $code;
171+
$output .= '</div>';
172+
return $output;
173+
}
174+
175+
protected function form_length(\MoodleQuickForm $mform, $helpname = '') {
176+
return question::form_length_hidden($mform);
177+
}
178+
179+
protected function form_precise(\MoodleQuickForm $mform, $helpname = '') {
180+
return question::form_precise_hidden($mform);
181+
}
182+
183+
}

classes/question/question.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
define('QUESRATE', 8);
4848
define('QUESDATE', 9);
4949
define('QUESNUMERIC', 10);
50+
define('QUESFILE', 12);
5051
define('QUESPAGEBREAK', 99);
5152
define('QUESSECTIONTEXT', 100);
5253

@@ -113,6 +114,7 @@ abstract class question {
113114
QUESDROP => 'drop',
114115
QUESRATE => 'rate',
115116
QUESDATE => 'date',
117+
QUESFILE => 'file',
116118
QUESNUMERIC => 'numerical',
117119
QUESPAGEBREAK => 'pagebreak',
118120
QUESSECTIONTEXT => 'sectiontext'

0 commit comments

Comments
 (0)