Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/question/type/essay/renderer.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00027 defined('MOODLE_INTERNAL') || die();
00028 
00029 
00036 class qtype_essay_renderer extends qtype_renderer {
00037     public function formulation_and_controls(question_attempt $qa,
00038             question_display_options $options) {
00039 
00040         $question = $qa->get_question();
00041         $responseoutput = $question->get_format_renderer($this->page);
00042 
00043         // Answer field.
00044         $step = $qa->get_last_step_with_qt_var('answer');
00045         if (empty($options->readonly)) {
00046             $answer = $responseoutput->response_area_input('answer', $qa,
00047                     $step, $question->responsefieldlines, $options->context);
00048 
00049         } else {
00050             $answer = $responseoutput->response_area_read_only('answer', $qa,
00051                     $step, $question->responsefieldlines, $options->context);
00052         }
00053 
00054         $files = '';
00055         if ($question->attachments) {
00056             if (empty($options->readonly)) {
00057                 $files = $this->files_input($qa, $question->attachments, $options);
00058 
00059             } else {
00060                 $files = $this->files_read_only($qa, $options);
00061             }
00062         }
00063 
00064         $result = '';
00065         $result .= html_writer::tag('div', $question->format_questiontext($qa),
00066                 array('class' => 'qtext'));
00067 
00068         $result .= html_writer::start_tag('div', array('class' => 'ablock'));
00069         $result .= html_writer::tag('div', $answer, array('class' => 'answer'));
00070         $result .= html_writer::tag('div', $files, array('class' => 'attachments'));
00071         $result .= html_writer::end_tag('div');
00072 
00073         return $result;
00074     }
00075 
00082     public function files_read_only(question_attempt $qa, question_display_options $options) {
00083         $files = $qa->get_last_qt_files('attachments', $options->context->id);
00084         $output = array();
00085 
00086         foreach ($files as $file) {
00087             $mimetype = $file->get_mimetype();
00088             $output[] = html_writer::tag('p', html_writer::link($qa->get_response_file_url($file),
00089                     $this->output->pix_icon(file_mimetype_icon($mimetype), $mimetype,
00090                     'moodle', array('class' => 'icon')) . ' ' . s($file->get_filename())));
00091         }
00092         return implode($output);
00093     }
00094 
00102     public function files_input(question_attempt $qa, $numallowed,
00103             question_display_options $options) {
00104         global $CFG;
00105         require_once($CFG->dirroot . '/lib/form/filemanager.php');
00106 
00107         $pickeroptions = new stdClass();
00108         $pickeroptions->mainfile = null;
00109         $pickeroptions->maxfiles = $numallowed;
00110         $pickeroptions->itemid = $qa->prepare_response_files_draft_itemid(
00111                 'attachments', $options->context->id);
00112         $pickeroptions->context = $options->context;
00113 
00114         $pickeroptions->itemid = $qa->prepare_response_files_draft_itemid(
00115                 'attachments', $options->context->id);
00116 
00117         return form_filemanager_render($pickeroptions) . html_writer::empty_tag(
00118                 'input', array('type' => 'hidden', 'name' => $qa->get_qt_field_name('attachments'),
00119                 'value' => $pickeroptions->itemid));
00120     }
00121 
00122     public function manual_comment(question_attempt $qa, question_display_options $options) {
00123         if ($options->manualcomment != question_display_options::EDITABLE) {
00124             return '';
00125         }
00126 
00127         $question = $qa->get_question();
00128         return html_writer::nonempty_tag('div', $question->format_text(
00129                 $question->graderinfo, $question->graderinfo, $qa, 'qtype_essay',
00130                 'graderinfo', $question->id), array('class' => 'graderinfo'));
00131     }
00132 }
00133 
00134 
00142 abstract class qtype_essay_format_renderer_base extends plugin_renderer_base {
00152     public abstract function response_area_read_only($name, question_attempt $qa,
00153             question_attempt_step $step, $lines, $context);
00154 
00164     public abstract function response_area_input($name, question_attempt $qa,
00165             question_attempt_step $step, $lines, $context);
00166 
00170     protected abstract function class_name();
00171 }
00172 
00173 
00181 class qtype_essay_format_editor_renderer extends plugin_renderer_base {
00182     protected function class_name() {
00183         return 'qtype_essay_editor';
00184     }
00185 
00186     public function response_area_read_only($name, $qa, $step, $lines, $context) {
00187         return html_writer::tag('div', $this->prepare_response($name, $qa, $step, $context),
00188                 array('class' => $this->class_name() . ' qtype_essay_response readonly'));
00189     }
00190 
00191     public function response_area_input($name, $qa, $step, $lines, $context) {
00192         global $CFG;
00193         require_once($CFG->dirroot . '/repository/lib.php');
00194 
00195         $inputname = $qa->get_qt_field_name($name);
00196         $responseformat = $step->get_qt_var($name . 'format');
00197         $id = $inputname . '_id';
00198 
00199         $editor = editors_get_preferred_editor($responseformat);
00200         $strformats = format_text_menu();
00201         $formats = $editor->get_supported_formats();
00202         foreach ($formats as $fid) {
00203             $formats[$fid] = $strformats[$fid];
00204         }
00205 
00206         list($draftitemid, $reponse) = $this->prepare_response_for_editing(
00207                 $name, $step, $context);
00208 
00209         $editor->use_editor($id, $this->get_editor_options($context),
00210                 $this->get_filepicker_options($context, $draftitemid));
00211 
00212         $output = '';
00213         $output .= html_writer::start_tag('div', array('class' =>
00214                 $this->class_name() . ' qtype_essay_response'));
00215 
00216         $output .= html_writer::tag('div', html_writer::tag('textarea', s($reponse),
00217                 array('id' => $id, 'name' => $inputname, 'rows' => $lines, 'cols' => 60)));
00218 
00219         $output .= html_writer::start_tag('div');
00220         if (count($formats == 1)) {
00221             reset($formats);
00222             $output .= html_writer::empty_tag('input', array('type' => 'hidden',
00223                     'name' => $inputname . 'format', 'value' => key($formats)));
00224 
00225         } else {
00226             $output .= html_writer::select($formats, $inputname . 'format', $responseformat, '');
00227         }
00228         $output .= html_writer::end_tag('div');
00229 
00230         $output .= $this->filepicker_html($inputname, $draftitemid);
00231 
00232         $output .= html_writer::end_tag('div');
00233         return $output;
00234     }
00235 
00244     protected function prepare_response($name, question_attempt $qa,
00245             question_attempt_step $step, $context) {
00246         if (!$step->has_qt_var($name)) {
00247             return '';
00248         }
00249 
00250         $formatoptions = new stdClass();
00251         $formatoptions->para = false;
00252         return format_text($step->get_qt_var($name), $step->get_qt_var($name . 'format'),
00253                 $formatoptions);
00254     }
00255 
00263     protected function prepare_response_for_editing($name,
00264             question_attempt_step $step, $context) {
00265         return array(0, $step->get_qt_var($name));
00266     }
00267 
00272     protected function get_editor_options($context) {
00273         return array('context' => $context);
00274     }
00275 
00281     protected function get_filepicker_options($context, $draftitemid) {
00282         return array();
00283     }
00284 
00290     protected function filepicker_html($inputname, $draftitemid) {
00291         return '';
00292     }
00293 }
00294 
00295 
00303 class qtype_essay_format_editorfilepicker_renderer extends qtype_essay_format_editor_renderer {
00304     protected function class_name() {
00305         return 'qtype_essay_editorfilepicker';
00306     }
00307 
00308     protected function prepare_response($name, question_attempt $qa,
00309             question_attempt_step $step, $context) {
00310         if (!$step->has_qt_var($name)) {
00311             return '';
00312         }
00313 
00314         $formatoptions = new stdClass();
00315         $formatoptions->para = false;
00316         $text = $qa->rewrite_response_pluginfile_urls($step->get_qt_var($name),
00317                 $context->id, 'answer', $step);
00318         return format_text($text, $step->get_qt_var($name . 'format'), $formatoptions);
00319     }
00320 
00321     protected function prepare_response_for_editing($name,
00322             question_attempt_step $step, $context) {
00323         return $step->prepare_response_files_draft_itemid_with_text(
00324                 $name, $context->id, $step->get_qt_var($name));
00325     }
00326 
00327     protected function get_editor_options($context) {
00328         return array(
00329             'subdirs' => 0,
00330             'maxbytes' => 0,
00331             'maxfiles' => -1,
00332             'context' => $context,
00333             'noclean' => 0,
00334             'trusttext'=>0
00335         );
00336     }
00337 
00346     protected function specific_filepicker_options($acceptedtypes, $draftitemid, $context) {
00347         $filepickeroptions = new stdClass();
00348         $filepickeroptions->accepted_types = $acceptedtypes;
00349         $filepickeroptions->return_types = FILE_INTERNAL | FILE_EXTERNAL;
00350         $filepickeroptions->context = $context;
00351         $filepickeroptions->env = 'filepicker';
00352 
00353         $options = initialise_filepicker($filepickeroptions);
00354         $options->context = $context;
00355         $options->client_id = uniqid();
00356         $options->env = 'editor';
00357         $options->itemid = $draftitemid;
00358 
00359         return $options;
00360     }
00361 
00362     protected function get_filepicker_options($context, $draftitemid) {
00363         global $CFG;
00364 
00365         return array(
00366             'image' => $this->specific_filepicker_options(array('image'),
00367                             $draftitemid, $context),
00368             'media' => $this->specific_filepicker_options(array('video', 'audio'),
00369                             $draftitemid, $context),
00370             'link'  => $this->specific_filepicker_options('*',
00371                             $draftitemid, $context),
00372         );
00373     }
00374 
00375     protected function filepicker_html($inputname, $draftitemid) {
00376         $nonjspickerurl = new moodle_url('/repository/draftfiles_manager.php', array(
00377             'action' => 'browse',
00378             'env' => 'editor',
00379             'itemid' => $draftitemid,
00380             'subdirs' => false,
00381             'maxfiles' => -1,
00382             'sesskey' => sesskey(),
00383         ));
00384 
00385         return html_writer::empty_tag('input', array('type' => 'hidden',
00386                 'name' => $inputname . ':itemid', 'value' => $draftitemid)) .
00387                 html_writer::tag('noscript', html_writer::tag('div',
00388                     html_writer::tag('object', '', array('type' => 'text/html',
00389                         'data' => $nonjspickerurl, 'height' => 160, 'width' => 600,
00390                         'style' => 'border: 1px solid #000;'))));
00391     }
00392 }
00393 
00394 
00402 class qtype_essay_format_plain_renderer extends plugin_renderer_base {
00406     protected function textarea($response, $lines, $attributes) {
00407         $attributes['class'] = $this->class_name() . ' qtype_essay_response';
00408         $attributes['rows'] = $lines;
00409         $attributes['cols'] = 60;
00410         return html_writer::tag('textarea', s($response), $attributes);
00411     }
00412 
00413     protected function class_name() {
00414         return 'qtype_essay_plain';
00415     }
00416 
00417     public function response_area_read_only($name, $qa, $step, $lines, $context) {
00418         return $this->textarea($step->get_qt_var($name), $lines, array('readonly' => 'readonly'));
00419     }
00420 
00421     public function response_area_input($name, $qa, $step, $lines, $context) {
00422         $inputname = $qa->get_qt_field_name($name);
00423         return $this->textarea($step->get_qt_var($name), $lines, array('name' => $inputname)) .
00424                 html_writer::empty_tag('input', array('type' => 'hidden',
00425                     'name' => $inputname . 'format', 'value' => FORMAT_PLAIN));
00426     }
00427 }
00428 
00429 
00438 class qtype_essay_format_monospaced_renderer extends qtype_essay_format_plain_renderer {
00439     protected function class_name() {
00440         return 'qtype_essay_monospaced';
00441     }
00442 }
 All Data Structures Namespaces Files Functions Variables Enumerations