[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/assign/feedback/editpdf/classes/ -> page_editor.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 editor class for the assignfeedback_editpdf plugin
  19   *
  20   * @package   assignfeedback_editpdf
  21   * @copyright 2012 Davo Smith
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace assignfeedback_editpdf;
  26  
  27  /**
  28   * This class performs crud operations on comments and annotations from a page of a response.
  29   *
  30   * No capability checks are done - they should be done by the calling class.
  31   *
  32   * @package   assignfeedback_editpdf
  33   * @copyright 2012 Davo Smith
  34   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class page_editor {
  37  
  38      /**
  39       * Get all comments for a page.
  40       * @param int $gradeid
  41       * @param int $pageno
  42       * @param bool $draft
  43       * @return comment[]
  44       */
  45      public static function get_comments($gradeid, $pageno, $draft) {
  46          global $DB;
  47  
  48          $comments = array();
  49          $params = array('gradeid'=>$gradeid, 'pageno'=>$pageno, 'draft'=>1);
  50          if (!$draft) {
  51              $params['draft'] = 0;
  52          }
  53          $records = $DB->get_records('assignfeedback_editpdf_cmnt', $params);
  54          foreach ($records as $record) {
  55              array_push($comments, new comment($record));
  56          }
  57  
  58          return $comments;
  59      }
  60  
  61      /**
  62       * Set all comments for a page.
  63       * @param int $gradeid
  64       * @param int $pageno
  65       * @param comment[] $comments
  66       * @return int - the number of comments.
  67       */
  68      public static function set_comments($gradeid, $pageno, $comments) {
  69          global $DB;
  70  
  71          $DB->delete_records('assignfeedback_editpdf_cmnt', array('gradeid'=>$gradeid, 'pageno'=>$pageno, 'draft'=>1));
  72  
  73          $added = 0;
  74          foreach ($comments as $record) {
  75              // Force these.
  76              if (!($record instanceof comment)) {
  77                  $comment = new comment($record);
  78              } else {
  79                  $comment = $record;
  80              }
  81              if (trim($comment->rawtext) === '') {
  82                  continue;
  83              }
  84              $comment->gradeid = $gradeid;
  85              $comment->pageno = $pageno;
  86              $comment->draft = 1;
  87              if (self::add_comment($comment)) {
  88                  $added++;
  89              }
  90          }
  91  
  92          return $added;
  93      }
  94  
  95      /**
  96       * Get a single comment by id.
  97       * @param int $commentid
  98       * @return comment or false
  99       */
 100      public static function get_comment($commentid) {
 101          $record = $DB->get_record('assignfeedback_editpdf_cmnt', array('id'=>$commentid), '*', IGNORE_MISSING);
 102          if ($record) {
 103              return new comment($record);
 104          }
 105          return false;
 106      }
 107  
 108      /**
 109       * Add a comment to a page.
 110       * @param comment $comment
 111       * @return bool
 112       */
 113      public static function add_comment(comment $comment) {
 114          global $DB;
 115          $comment->id = null;
 116          return $DB->insert_record('assignfeedback_editpdf_cmnt', $comment);
 117      }
 118  
 119      /**
 120       * Remove a comment from a page.
 121       * @param int $commentid
 122       * @return bool
 123       */
 124      public static function remove_comment($commentid) {
 125          global $DB;
 126          return $DB->delete_records('assignfeedback_editpdf_cmnt', array('id'=>$commentid));
 127      }
 128  
 129      /**
 130       * Get all annotations for a page.
 131       * @param int $gradeid
 132       * @param int $pageno
 133       * @param bool $draft
 134       * @return annotation[]
 135       */
 136      public static function get_annotations($gradeid, $pageno, $draft) {
 137          global $DB;
 138  
 139          $params = array('gradeid'=>$gradeid, 'pageno'=>$pageno, 'draft'=>1);
 140          if (!$draft) {
 141              $params['draft'] = 0;
 142          }
 143          $annotations = array();
 144          $records = $DB->get_records('assignfeedback_editpdf_annot', $params);
 145          foreach ($records as $record) {
 146              array_push($annotations, new annotation($record));
 147          }
 148  
 149          return $annotations;
 150      }
 151  
 152      /**
 153       * Set all annotations for a page.
 154       * @param int $gradeid
 155       * @param int $pageno
 156       * @param annotation[] $annotations
 157       * @return int - the number of annotations.
 158       */
 159      public static function set_annotations($gradeid, $pageno, $annotations) {
 160          global $DB;
 161  
 162          $DB->delete_records('assignfeedback_editpdf_annot', array('gradeid' => $gradeid, 'pageno' => $pageno, 'draft' => 1));
 163          $added = 0;
 164          foreach ($annotations as $record) {
 165              // Force these.
 166              if (!($record instanceof annotation)) {
 167                  $annotation = new annotation($record);
 168              } else {
 169                  $annotation = $record;
 170              }
 171              $annotation->gradeid = $gradeid;
 172              $annotation->pageno = $pageno;
 173              $annotation->draft = 1;
 174              if (self::add_annotation($annotation)) {
 175                  $added++;
 176              }
 177          }
 178  
 179          return $added;
 180      }
 181  
 182      /**
 183       * Get a single annotation by id.
 184       * @param int $annotationid
 185       * @return annotation or false
 186       */
 187      public static function get_annotation($annotationid) {
 188          global $DB;
 189  
 190          $record = $DB->get_record('assignfeedback_editpdf_annot', array('id'=>$annotationid), '*', IGNORE_MISSING);
 191          if ($record) {
 192              return new annotation($record);
 193          }
 194          return false;
 195      }
 196  
 197      /**
 198       * Unrelease drafts
 199       * @param int $gradeid
 200       * @return bool
 201       */
 202      public static function unrelease_drafts($gradeid) {
 203          global $DB;
 204  
 205          // Delete the non-draft annotations and comments.
 206          $result = $DB->delete_records('assignfeedback_editpdf_cmnt', array('gradeid'=>$gradeid, 'draft'=>0));
 207          $result = $DB->delete_records('assignfeedback_editpdf_annot', array('gradeid'=>$gradeid, 'draft'=>0)) && $result;
 208          return $result;
 209      }
 210  
 211      /**
 212       * Release the draft comments and annotations to students.
 213       * @param int $gradeid
 214       * @return bool
 215       */
 216      public static function release_drafts($gradeid) {
 217          global $DB;
 218  
 219          // Delete the previous non-draft annotations and comments.
 220          $DB->delete_records('assignfeedback_editpdf_cmnt', array('gradeid'=>$gradeid, 'draft'=>0));
 221          $DB->delete_records('assignfeedback_editpdf_annot', array('gradeid'=>$gradeid, 'draft'=>0));
 222  
 223          // Copy all the draft annotations and comments to non-drafts.
 224          $records = $DB->get_records('assignfeedback_editpdf_annot', array('gradeid'=>$gradeid, 'draft'=>1));
 225          foreach ($records as $record) {
 226              unset($record->id);
 227              $record->draft = 0;
 228              $DB->insert_record('assignfeedback_editpdf_annot', $record);
 229          }
 230          $records = $DB->get_records('assignfeedback_editpdf_cmnt', array('gradeid'=>$gradeid, 'draft'=>1));
 231          foreach ($records as $record) {
 232              unset($record->id);
 233              $record->draft = 0;
 234              $DB->insert_record('assignfeedback_editpdf_cmnt', $record);
 235          }
 236  
 237          return true;
 238      }
 239  
 240      /**
 241       * Has annotations or comments.
 242       * @param int $gradeid
 243       * @return bool
 244       */
 245      public static function has_annotations_or_comments($gradeid, $includedraft) {
 246          global $DB;
 247          $params = array('gradeid'=>$gradeid);
 248          if (!$includedraft) {
 249              $params['draft'] = 0;
 250          }
 251          if ($DB->count_records('assignfeedback_editpdf_cmnt', $params)) {
 252              return true;
 253          }
 254          if ($DB->count_records('assignfeedback_editpdf_annot', $params)) {
 255              return true;
 256          }
 257          return false;
 258      }
 259  
 260      /**
 261       * Aborts all draft annotations and reverts to the last version released to students.
 262       * @param int $gradeid
 263       * @return bool
 264       */
 265      public static function revert_drafts($gradeid) {
 266          global $DB;
 267  
 268          // Delete the previous non-draft annotations and comments.
 269          $DB->delete_records('assignfeedback_editpdf_cmnt', array('gradeid'=>$gradeid, 'draft'=>1));
 270          $DB->delete_records('assignfeedback_editpdf_annot', array('gradeid'=>$gradeid, 'draft'=>1));
 271  
 272          // Copy all the draft annotations and comments to non-drafts.
 273          $records = $DB->get_records('assignfeedback_editpdf_annot', array('gradeid'=>$gradeid, 'draft'=>0));
 274          foreach ($records as $record) {
 275              unset($record->id);
 276              $record->draft = 0;
 277              $DB->insert_record('assignfeedback_editpdf_annot', $record);
 278          }
 279          $records = $DB->get_records('assignfeedback_editpdf_cmnt', array('gradeid'=>$gradeid, 'draft'=>0));
 280          foreach ($records as $record) {
 281              unset($record->id);
 282              $record->draft = 0;
 283              $DB->insert_record('assignfeedback_editpdf_annot', $record);
 284          }
 285  
 286          return true;
 287      }
 288  
 289      /**
 290       * Add a annotation to a page.
 291       * @param annotation $annotation
 292       * @return bool
 293       */
 294      public static function add_annotation(annotation $annotation) {
 295          global $DB;
 296  
 297          $annotation->id = null;
 298          return $DB->insert_record('assignfeedback_editpdf_annot', $annotation);
 299      }
 300  
 301      /**
 302       * Remove a annotation from a page.
 303       * @param int $annotationid
 304       * @return bool
 305       */
 306      public static function remove_annotation($annotationid) {
 307          global $DB;
 308  
 309          return $DB->delete_records('assignfeedback_editpdf_annot', array('id'=>$annotationid));
 310      }
 311  
 312      /**
 313       * Copy annotations, comments, pages, and other required content from the source user to the current group member
 314       * being procssed when using applytoall.
 315       *
 316       * @param int|\assign $assignment
 317       * @param stdClass $grade
 318       * @param int $sourceuserid
 319       * @return bool
 320       */
 321      public static function copy_drafts_from_to($assignment, $grade, $sourceuserid) {
 322          global $DB;
 323  
 324          // Delete any existing annotations and comments from current user.
 325          $DB->delete_records('assignfeedback_editpdf_annot', array('gradeid' => $grade->id));
 326          $DB->delete_records('assignfeedback_editpdf_cmnt', array('gradeid' => $grade->id));
 327          // Get gradeid, annotations and comments from sourceuserid.
 328          $sourceusergrade = $assignment->get_user_grade($sourceuserid, true, $grade->attemptnumber);
 329          $annotations = $DB->get_records('assignfeedback_editpdf_annot', array('gradeid' => $sourceusergrade->id, 'draft' => 1));
 330          $comments = $DB->get_records('assignfeedback_editpdf_cmnt', array('gradeid' => $sourceusergrade->id, 'draft' => 1));
 331          $contextid = $assignment->get_context()->id;
 332          $sourceitemid = $sourceusergrade->id;
 333  
 334          // Add annotations and comments to current user to generate feedback file.
 335          foreach ($annotations as $annotation) {
 336              $annotation->gradeid = $grade->id;
 337              $DB->insert_record('assignfeedback_editpdf_annot', $annotation);
 338          }
 339          foreach ($comments as $comment) {
 340              $comment->gradeid = $grade->id;
 341              $DB->insert_record('assignfeedback_editpdf_cmnt', $comment);
 342          }
 343  
 344          $fs = get_file_storage();
 345  
 346          // Copy the stamp files.
 347          self::replace_files_from_to($fs, $contextid, $sourceitemid, $grade->id, document_services::STAMPS_FILEAREA, true);
 348  
 349          // Copy the PAGE_IMAGE_FILEAREA files.
 350          self::replace_files_from_to($fs, $contextid, $sourceitemid, $grade->id, document_services::PAGE_IMAGE_FILEAREA);
 351  
 352          return true;
 353      }
 354  
 355      /**
 356       * Replace the area files in the specified area with those in the source item id.
 357       *
 358       * @param \file_storage $fs The file storage class
 359       * @param int $contextid The ID of the context for the assignment.
 360       * @param int $sourceitemid The itemid to copy from - typically the source grade id.
 361       * @param int $itemid The itemid to copy to - typically the target grade id.
 362       * @param string $area The file storage area.
 363       * @param bool $includesubdirs Whether to copy the content of sub-directories too.
 364       */
 365      public static function replace_files_from_to($fs, $contextid, $sourceitemid, $itemid, $area, $includesubdirs = false) {
 366          $component = 'assignfeedback_editpdf';
 367          // Remove the existing files within this area.
 368          $fs->delete_area_files($contextid, $component, $area, $itemid);
 369  
 370          // Copy the files from the source area.
 371          if ($files = $fs->get_area_files($contextid, $component, $area, $sourceitemid,
 372                                           "filename", $includesubdirs)) {
 373              foreach ($files as $file) {
 374                  $newrecord = new \stdClass();
 375                  $newrecord->contextid = $contextid;
 376                  $newrecord->itemid = $itemid;
 377                  $fs->create_file_from_storedfile($newrecord, $file);
 378              }
 379          }
 380      }
 381  
 382      /**
 383       * Delete the draft annotations and comments.
 384       *
 385       * This is intended to be used when the version of the PDF has changed and the annotations
 386       * might not be relevant any more, therefore we should delete them.
 387       *
 388       * @param int $gradeid The grade ID.
 389       * @return bool
 390       */
 391      public static function delete_draft_content($gradeid) {
 392          global $DB;
 393          $conditions = array('gradeid' => $gradeid, 'draft' => 1);
 394          $result = $DB->delete_records('assignfeedback_editpdf_annot', $conditions);
 395          $result = $result && $DB->delete_records('assignfeedback_editpdf_cmnt', $conditions);
 396          return $result;
 397      }
 398  }


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