[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/mod/assign/feedback/editpdf/ -> locallib.php (source)

   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 definition for the library class for PDF feedback plugin
  19   *
  20   *
  21   * @package   assignfeedback_editpdf
  22   * @copyright 2012 Davo Smith
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  use \assignfeedback_editpdf\document_services;
  29  use \assignfeedback_editpdf\page_editor;
  30  
  31  /**
  32   * library class for editpdf feedback plugin extending feedback plugin base class
  33   *
  34   * @package   assignfeedback_editpdf
  35   * @copyright 2012 Davo Smith
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class assign_feedback_editpdf extends assign_feedback_plugin {
  39  
  40      /** @var boolean|null $enabledcache Cached lookup of the is_enabled function */
  41      private $enabledcache = null;
  42  
  43      /**
  44       * Get the name of the file feedback plugin
  45       * @return string
  46       */
  47      public function get_name() {
  48          return get_string('pluginname', 'assignfeedback_editpdf');
  49      }
  50  
  51      /**
  52       * Create a widget for rendering the editor.
  53       *
  54       * @param int $userid
  55       * @param stdClass $grade
  56       * @param bool $readonly
  57       * @return assignfeedback_editpdf_widget
  58       */
  59      public function get_widget($userid, $grade, $readonly) {
  60          $attempt = -1;
  61          if ($grade && $grade->attemptnumber) {
  62              $attempt = $grade->attemptnumber;
  63          } else {
  64              $grade = $this->assignment->get_user_grade($userid, true);
  65          }
  66  
  67          $feedbackfile = document_services::get_feedback_document($this->assignment->get_instance()->id,
  68                                                                   $userid,
  69                                                                   $attempt);
  70  
  71          $stampfiles = array();
  72          $fs = get_file_storage();
  73          $syscontext = context_system::instance();
  74  
  75          // Copy any new stamps to this instance.
  76          if ($files = $fs->get_area_files($syscontext->id,
  77                                           'assignfeedback_editpdf',
  78                                           'stamps',
  79                                           0,
  80                                           "filename",
  81                                           false)) {
  82              foreach ($files as $file) {
  83                  $filename = $file->get_filename();
  84                  if ($filename !== '.') {
  85  
  86                      $existingfile = $fs->get_file($this->assignment->get_context()->id,
  87                                                    'assignfeedback_editpdf',
  88                                                    'stamps',
  89                                                    $grade->id,
  90                                                    '/',
  91                                                    $file->get_filename());
  92                      if (!$existingfile) {
  93                          $newrecord = new stdClass();
  94                          $newrecord->contextid = $this->assignment->get_context()->id;
  95                          $newrecord->itemid = $grade->id;
  96                          $fs->create_file_from_storedfile($newrecord, $file);
  97                      }
  98                  }
  99              }
 100          }
 101  
 102          // Now get the full list of stamp files for this instance.
 103          if ($files = $fs->get_area_files($this->assignment->get_context()->id,
 104                                           'assignfeedback_editpdf',
 105                                           'stamps',
 106                                           $grade->id,
 107                                           "filename",
 108                                           false)) {
 109              foreach ($files as $file) {
 110                  $filename = $file->get_filename();
 111                  if ($filename !== '.') {
 112                      $url = moodle_url::make_pluginfile_url($this->assignment->get_context()->id,
 113                                                     'assignfeedback_editpdf',
 114                                                     'stamps',
 115                                                     $grade->id,
 116                                                     '/',
 117                                                     $file->get_filename(),
 118                                                     false);
 119                      array_push($stampfiles, $url->out());
 120                  }
 121              }
 122          }
 123  
 124          $url = false;
 125          $filename = '';
 126          if ($feedbackfile) {
 127              $url = moodle_url::make_pluginfile_url($this->assignment->get_context()->id,
 128                                                     'assignfeedback_editpdf',
 129                                                     document_services::FINAL_PDF_FILEAREA,
 130                                                     $grade->id,
 131                                                     '/',
 132                                                     $feedbackfile->get_filename(),
 133                                                     false);
 134             $filename = $feedbackfile->get_filename();
 135          }
 136  
 137          // Retrieve total number of pages.
 138          $pagetotal = document_services::page_number_for_attempt($this->assignment->get_instance()->id,
 139                  $userid,
 140                  $attempt,
 141                  $readonly);
 142  
 143          $widget = new assignfeedback_editpdf_widget($this->assignment->get_instance()->id,
 144                                                      $userid,
 145                                                      $attempt,
 146                                                      $url,
 147                                                      $filename,
 148                                                      $stampfiles,
 149                                                      $readonly,
 150                                                      $pagetotal);
 151          return $widget;
 152      }
 153  
 154      /**
 155       * Get form elements for grading form
 156       *
 157       * @param stdClass $grade
 158       * @param MoodleQuickForm $mform
 159       * @param stdClass $data
 160       * @param int $userid
 161       * @return bool true if elements were added to the form
 162       */
 163      public function get_form_elements_for_user($grade, MoodleQuickForm $mform, stdClass $data, $userid) {
 164          global $PAGE;
 165  
 166          $attempt = -1;
 167          if ($grade) {
 168              $attempt = $grade->attemptnumber;
 169          }
 170  
 171          $files = document_services::list_compatible_submission_files_for_attempt($this->assignment, $userid, $attempt);
 172          // Only show the editor if there was a compatible file submitted.
 173          if (count($files)) {
 174  
 175              $renderer = $PAGE->get_renderer('assignfeedback_editpdf');
 176  
 177              $widget = $this->get_widget($userid, $grade, false);
 178  
 179              $html = $renderer->render($widget);
 180              $mform->addElement('static', 'editpdf', get_string('editpdf', 'assignfeedback_editpdf'), $html);
 181              $mform->addHelpButton('editpdf', 'editpdf', 'assignfeedback_editpdf');
 182              $mform->addElement('hidden', 'editpdf_source_userid', $userid);
 183              $mform->setType('editpdf_source_userid', PARAM_INT);
 184              $mform->setConstant('editpdf_source_userid', $userid);
 185          }
 186      }
 187  
 188      /**
 189       * Generate the pdf.
 190       *
 191       * @param stdClass $grade
 192       * @param stdClass $data
 193       * @return bool
 194       */
 195      public function save(stdClass $grade, stdClass $data) {
 196          // Source user id is only added to the form if there was a pdf.
 197          if (!empty($data->editpdf_source_userid)) {
 198              $sourceuserid = $data->editpdf_source_userid;
 199              // Copy drafts annotations and comments if current user is different to sourceuserid.
 200              if ($sourceuserid != $grade->userid) {
 201                  page_editor::copy_drafts_from_to($this->assignment, $grade, $sourceuserid);
 202              }
 203          }
 204          if (page_editor::has_annotations_or_comments($grade->id, true)) {
 205              document_services::generate_feedback_document($this->assignment, $grade->userid, $grade->attemptnumber);
 206          }
 207  
 208          return true;
 209      }
 210  
 211      /**
 212       * Display the list of files in the feedback status table.
 213       *
 214       * @param stdClass $grade
 215       * @param bool $showviewlink (Always set to false).
 216       * @return string
 217       */
 218      public function view_summary(stdClass $grade, & $showviewlink) {
 219          $showviewlink = false;
 220          return $this->view($grade);
 221      }
 222  
 223      /**
 224       * Display the list of files in the feedback status table.
 225       *
 226       * @param stdClass $grade
 227       * @return string
 228       */
 229      public function view(stdClass $grade) {
 230          global $PAGE;
 231          $html = '';
 232          // Show a link to download the pdf.
 233          if (page_editor::has_annotations_or_comments($grade->id, false)) {
 234              $html = $this->assignment->render_area_files('assignfeedback_editpdf',
 235                                                           document_services::FINAL_PDF_FILEAREA,
 236                                                           $grade->id);
 237  
 238              // Also show the link to the read-only interface.
 239              $renderer = $PAGE->get_renderer('assignfeedback_editpdf');
 240              $widget = $this->get_widget($grade->userid, $grade, true);
 241  
 242              $html .= $renderer->render($widget);
 243          }
 244          return $html;
 245      }
 246  
 247      /**
 248       * Return true if there are no released comments/annotations.
 249       *
 250       * @param stdClass $grade
 251       */
 252      public function is_empty(stdClass $grade) {
 253          global $DB;
 254  
 255          $comments = $DB->count_records('assignfeedback_editpdf_cmnt', array('gradeid'=>$grade->id, 'draft'=>0));
 256          $annotations = $DB->count_records('assignfeedback_editpdf_annot', array('gradeid'=>$grade->id, 'draft'=>0));
 257          return $comments == 0 && $annotations == 0;
 258      }
 259  
 260      /**
 261       * The assignment has been deleted - remove the plugin specific data
 262       *
 263       * @return bool
 264       */
 265      public function delete_instance() {
 266          global $DB;
 267          $grades = $DB->get_records('assign_grades', array('assignment'=>$this->assignment->get_instance()->id), '', 'id');
 268          if ($grades) {
 269              list($gradeids, $params) = $DB->get_in_or_equal(array_keys($grades), SQL_PARAMS_NAMED);
 270              $DB->delete_records_select('assignfeedback_editpdf_annot', 'gradeid ' . $gradeids, $params);
 271              $DB->delete_records_select('assignfeedback_editpdf_cmnt', 'gradeid ' . $gradeids, $params);
 272          }
 273          return true;
 274      }
 275  
 276      /**
 277       * Automatically enable or disable editpdf feedback plugin based on
 278       * whether the ghostscript path is set correctly.
 279       *
 280       * @return bool
 281       */
 282      public function is_enabled() {
 283          if ($this->enabledcache === null) {
 284              $testpath = assignfeedback_editpdf\pdf::test_gs_path(false);
 285              $this->enabledcache = ($testpath->status == assignfeedback_editpdf\pdf::GSPATH_OK);
 286          }
 287          return $this->enabledcache;
 288      }
 289      /**
 290       * Automatically hide the setting for the editpdf feedback plugin.
 291       *
 292       * @return bool false
 293       */
 294      public function is_configurable() {
 295          return false;
 296      }
 297  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1