[ Index ]

PHP Cross Reference of Unnamed Project

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          $renderer = $PAGE->get_renderer('assignfeedback_editpdf');
 172  
 173          $widget = $this->get_widget($userid, $grade, false);
 174  
 175          $html = $renderer->render($widget);
 176          $mform->addElement('static', 'editpdf', get_string('editpdf', 'assignfeedback_editpdf'), $html);
 177          $mform->addHelpButton('editpdf', 'editpdf', 'assignfeedback_editpdf');
 178          $mform->addElement('hidden', 'editpdf_source_userid', $userid);
 179          $mform->setType('editpdf_source_userid', PARAM_INT);
 180          $mform->setConstant('editpdf_source_userid', $userid);
 181      }
 182  
 183      /**
 184       * Check to see if the grade feedback for the pdf has been modified.
 185       *
 186       * @param stdClass $grade Grade object.
 187       * @param stdClass $data Data from the form submission (not used).
 188       * @return boolean True if the pdf has been modified, else false.
 189       */
 190      public function is_feedback_modified(stdClass $grade, stdClass $data) {
 191          // We only need to know if the source user's PDF has changed. If so then all
 192          // following users will have the same status. If it's only an individual annotation
 193          // then only one user will come through this method.
 194          // Source user id is only added to the form if there was a pdf.
 195          if (!empty($data->editpdf_source_userid)) {
 196              $sourceuserid = $data->editpdf_source_userid;
 197              // Retrieve the grade information for the source user.
 198              $sourcegrade = $this->assignment->get_user_grade($sourceuserid, true, $grade->attemptnumber);
 199              $pagenumbercount = document_services::page_number_for_attempt($this->assignment, $sourceuserid, $sourcegrade->attemptnumber);
 200              for ($i = 0; $i < $pagenumbercount; $i++) {
 201                  // Select all annotations.
 202                  $draftannotations = page_editor::get_annotations($sourcegrade->id, $i, true);
 203                  $nondraftannotations = page_editor::get_annotations($grade->id, $i, false);
 204                  // Check to see if the count is the same.
 205                  if (count($draftannotations) != count($nondraftannotations)) {
 206                      // The count is different so we have a modification.
 207                      return true;
 208                  } else {
 209                      $matches = 0;
 210                      // Have a closer look and see if the draft files match all the non draft files.
 211                      foreach ($nondraftannotations as $ndannotation) {
 212                          foreach ($draftannotations as $dannotation) {
 213                              foreach ($ndannotation as $key => $value) {
 214                                  if ($key != 'id' && $value != $dannotation->{$key}) {
 215                                      continue 2;
 216                                  }
 217                              }
 218                              $matches++;
 219                          }
 220                      }
 221                      if ($matches !== count($nondraftannotations)) {
 222                          return true;
 223                      }
 224                  }
 225                  // Select all comments.
 226                  $draftcomments = page_editor::get_comments($sourcegrade->id, $i, true);
 227                  $nondraftcomments = page_editor::get_comments($grade->id, $i, false);
 228                  if (count($draftcomments) != count($nondraftcomments)) {
 229                      return true;
 230                  } else {
 231                      // Go for a closer inspection.
 232                      $matches = 0;
 233                      foreach ($nondraftcomments as $ndcomment) {
 234                          foreach ($draftcomments as $dcomment) {
 235                              foreach ($ndcomment as $key => $value) {
 236                                  if ($key != 'id' && $value != $dcomment->{$key}) {
 237                                      continue 2;
 238                                  }
 239                              }
 240                              $matches++;
 241                          }
 242                      }
 243                      if ($matches !== count($nondraftcomments)) {
 244                          return true;
 245                      }
 246                  }
 247              }
 248          }
 249          return false;
 250      }
 251  
 252      /**
 253       * Generate the pdf.
 254       *
 255       * @param stdClass $grade
 256       * @param stdClass $data
 257       * @return bool
 258       */
 259      public function save(stdClass $grade, stdClass $data) {
 260          // Source user id is only added to the form if there was a pdf.
 261          if (!empty($data->editpdf_source_userid)) {
 262              $sourceuserid = $data->editpdf_source_userid;
 263              // Copy drafts annotations and comments if current user is different to sourceuserid.
 264              if ($sourceuserid != $grade->userid) {
 265                  page_editor::copy_drafts_from_to($this->assignment, $grade, $sourceuserid);
 266              }
 267          }
 268          if (page_editor::has_annotations_or_comments($grade->id, true)) {
 269              document_services::generate_feedback_document($this->assignment, $grade->userid, $grade->attemptnumber);
 270          }
 271  
 272          return true;
 273      }
 274  
 275      /**
 276       * Display the list of files in the feedback status table.
 277       *
 278       * @param stdClass $grade
 279       * @param bool $showviewlink (Always set to false).
 280       * @return string
 281       */
 282      public function view_summary(stdClass $grade, & $showviewlink) {
 283          $showviewlink = false;
 284          return $this->view($grade);
 285      }
 286  
 287      /**
 288       * Display the list of files in the feedback status table.
 289       *
 290       * @param stdClass $grade
 291       * @return string
 292       */
 293      public function view(stdClass $grade) {
 294          global $PAGE;
 295          $html = '';
 296          // Show a link to download the pdf.
 297          if (page_editor::has_annotations_or_comments($grade->id, false)) {
 298              $html = $this->assignment->render_area_files('assignfeedback_editpdf',
 299                                                           document_services::FINAL_PDF_FILEAREA,
 300                                                           $grade->id);
 301  
 302              // Also show the link to the read-only interface.
 303              $renderer = $PAGE->get_renderer('assignfeedback_editpdf');
 304              $widget = $this->get_widget($grade->userid, $grade, true);
 305  
 306              $html .= $renderer->render($widget);
 307          }
 308          return $html;
 309      }
 310  
 311      /**
 312       * Return true if there are no released comments/annotations.
 313       *
 314       * @param stdClass $grade
 315       */
 316      public function is_empty(stdClass $grade) {
 317          global $DB;
 318  
 319          $comments = $DB->count_records('assignfeedback_editpdf_cmnt', array('gradeid'=>$grade->id, 'draft'=>0));
 320          $annotations = $DB->count_records('assignfeedback_editpdf_annot', array('gradeid'=>$grade->id, 'draft'=>0));
 321          return $comments == 0 && $annotations == 0;
 322      }
 323  
 324      /**
 325       * The assignment has been deleted - remove the plugin specific data
 326       *
 327       * @return bool
 328       */
 329      public function delete_instance() {
 330          global $DB;
 331          $grades = $DB->get_records('assign_grades', array('assignment'=>$this->assignment->get_instance()->id), '', 'id');
 332          if ($grades) {
 333              list($gradeids, $params) = $DB->get_in_or_equal(array_keys($grades), SQL_PARAMS_NAMED);
 334              $DB->delete_records_select('assignfeedback_editpdf_annot', 'gradeid ' . $gradeids, $params);
 335              $DB->delete_records_select('assignfeedback_editpdf_cmnt', 'gradeid ' . $gradeids, $params);
 336          }
 337          return true;
 338      }
 339  
 340      /**
 341       * Automatically enable or disable editpdf feedback plugin based on
 342       * whether the ghostscript path is set correctly.
 343       *
 344       * @return bool
 345       */
 346      public function is_enabled() {
 347          if ($this->enabledcache === null) {
 348              $testpath = assignfeedback_editpdf\pdf::test_gs_path(false);
 349              $this->enabledcache = ($testpath->status == assignfeedback_editpdf\pdf::GSPATH_OK);
 350          }
 351          return $this->enabledcache;
 352      }
 353      /**
 354       * Automatically hide the setting for the editpdf feedback plugin.
 355       *
 356       * @return bool false
 357       */
 358      public function is_configurable() {
 359          return false;
 360      }
 361  
 362      /**
 363       * Get file areas returns a list of areas this plugin stores files.
 364       *
 365       * @return array - An array of fileareas (keys) and descriptions (values)
 366       */
 367      public function get_file_areas() {
 368          return array(document_services::FINAL_PDF_FILEAREA => $this->get_name());
 369      }
 370  
 371      /**
 372       * This plugin will inject content into the review panel with javascript.
 373       * @return bool true
 374       */
 375      public function supports_review_panel() {
 376          return true;
 377      }
 378  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1