Gitlab CSE Unil

Commit 9e4edd46 authored by M. Chardon's avatar M. Chardon
Browse files

init

parents
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* An event observer.
*
* @package assignfeedback_editpdfplus
* @copyright 2016 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace assignfeedback_editpdfplus\event;
/**
* An event observer.
* @copyright 2016 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class observer {
/**
* Listen to events and queue the submission for processing.
* @param \mod_assign\event\submission_created $event
*/
public static function submission_created(\mod_assign\event\submission_created $event) {
global $DB;
$submissionid = $event->other['submissionid'];
$submissionattempt = $event->other['submissionattempt'];
$fields = array( 'submissionid' => $submissionid, 'submissionattempt' => $submissionattempt);
$record = (object) $fields;
$exists = $DB->get_records('assignfeedback_editpp_queue', $fields);
if (!$exists) {
$DB->insert_record('assignfeedback_editpp_queue', $record);
}
}
/**
* Listen to events and queue the submission for processing.
* @param \mod_assign\event\submission_updated $event
*/
public static function submission_updated(\mod_assign\event\submission_updated $event) {
global $DB;
$submissionid = $event->other['submissionid'];
$submissionattempt = $event->other['submissionattempt'];
$fields = array( 'submissionid' => $submissionid, 'submissionattempt' => $submissionattempt);
$record = (object) $fields;
$exists = $DB->get_records('assignfeedback_editpp_queue', $fields);
if (!$exists) {
$DB->insert_record('assignfeedback_editpp_queue', $record);
}
}
}
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file contains the editor class for the assignfeedback_editpdfplus plugin
*
* @package assignfeedback_editpdfplus
* @copyright 2012 Davo Smith
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace assignfeedback_editpdfplus;
/**
* This class performs crud operations on comments and annotations from a page of a response.
*
* No capability checks are done - they should be done by the calling class.
*
* @package assignfeedback_editpdfplus
* @copyright 2012 Davo Smith
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class page_editor {
/**
* Get all comments for a page.
* @param int $gradeid
* @param int $pageno
* @param bool $draft
* @return comment[]
*/
public static function get_comments($gradeid, $pageno, $draft) {
global $DB;
$comments = array();
$params = array('gradeid'=>$gradeid, 'pageno'=>$pageno, 'draft'=>1);
if (!$draft) {
$params['draft'] = 0;
}
$records = $DB->get_records('assignfeedback_editpp_cmnt', $params);
foreach ($records as $record) {
array_push($comments, new comment($record));
}
return $comments;
}
/**
* Set all comments for a page.
* @param int $gradeid
* @param int $pageno
* @param comment[] $comments
* @return int - the number of comments.
*/
public static function set_comments($gradeid, $pageno, $comments) {
global $DB;
$DB->delete_records('assignfeedback_editpp_cmnt', array('gradeid'=>$gradeid, 'pageno'=>$pageno, 'draft'=>1));
$added = 0;
foreach ($comments as $record) {
// Force these.
if (!($record instanceof comment)) {
$comment = new comment($record);
} else {
$comment = $record;
}
if (trim($comment->rawtext) === '') {
continue;
}
$comment->gradeid = $gradeid;
$comment->pageno = $pageno;
$comment->draft = 1;
if (self::add_comment($comment)) {
$added++;
}
}
return $added;
}
/**
* Get a single comment by id.
* @param int $commentid
* @return comment or false
*/
public static function get_comment($commentid) {
$record = $DB->get_record('assignfeedback_editpp_cmnt', array('id'=>$commentid), '*', IGNORE_MISSING);
if ($record) {
return new comment($record);
}
return false;
}
/**
* Add a comment to a page.
* @param comment $comment
* @return bool
*/
public static function add_comment(comment $comment) {
global $DB;
$comment->id = null;
return $DB->insert_record('assignfeedback_editpp_cmnt', $comment);
}
/**
* Remove a comment from a page.
* @param int $commentid
* @return bool
*/
public static function remove_comment($commentid) {
global $DB;
return $DB->delete_records('assignfeedback_editpp_cmnt', array('id'=>$commentid));
}
/**
* Get all annotations for a page.
* @param int $gradeid
* @param int $pageno
* @param bool $draft
* @return annotation[]
*/
public static function get_annotations($gradeid, $pageno, $draft) {
global $DB;
$params = array('gradeid'=>$gradeid, 'pageno'=>$pageno, 'draft'=>1);
if (!$draft) {
$params['draft'] = 0;
}
$annotations = array();
$records = $DB->get_records('assignfeedback_editpp_annot', $params);
foreach ($records as $record) {
array_push($annotations, new annotation($record));
}
return $annotations;
}
/**
* Set all annotations for a page.
* @param int $gradeid
* @param int $pageno
* @param annotation[] $annotations
* @return int - the number of annotations.
*/
public static function set_annotations($gradeid, $pageno, $annotations) {
global $DB;
$DB->delete_records('assignfeedback_editpp_annot', array('gradeid' => $gradeid, 'pageno' => $pageno, 'draft' => 1));
$added = 0;
foreach ($annotations as $record) {
// Force these.
if (!($record instanceof annotation)) {
$annotation = new annotation($record);
} else {
$annotation = $record;
}
$annotation->gradeid = $gradeid;
$annotation->pageno = $pageno;
$annotation->draft = 1;
if (self::add_annotation($annotation)) {
$added++;
}
}
return $added;
}
/**
* Get a single annotation by id.
* @param int $annotationid
* @return annotation or false
*/
public static function get_annotation($annotationid) {
global $DB;
$record = $DB->get_record('assignfeedback_editpp_annot', array('id'=>$annotationid), '*', IGNORE_MISSING);
if ($record) {
return new annotation($record);
}
return false;
}
/**
* Unrelease drafts
* @param int $gradeid
* @return bool
*/
public static function unrelease_drafts($gradeid) {
global $DB;
// Delete the non-draft annotations and comments.
$result = $DB->delete_records('assignfeedback_editpp_cmnt', array('gradeid'=>$gradeid, 'draft'=>0));
$result = $DB->delete_records('assignfeedback_editpp_annot', array('gradeid'=>$gradeid, 'draft'=>0)) && $result;
return $result;
}
/**
* Release the draft comments and annotations to students.
* @param int $gradeid
* @return bool
*/
public static function release_drafts($gradeid) {
global $DB;
// Delete the previous non-draft annotations and comments.
$DB->delete_records('assignfeedback_editpp_cmnt', array('gradeid'=>$gradeid, 'draft'=>0));
$DB->delete_records('assignfeedback_editpp_annot', array('gradeid'=>$gradeid, 'draft'=>0));
// Copy all the draft annotations and comments to non-drafts.
$records = $DB->get_records('assignfeedback_editpp_annot', array('gradeid'=>$gradeid, 'draft'=>1));
foreach ($records as $record) {
unset($record->id);
$record->draft = 0;
$DB->insert_record('assignfeedback_editpp_annot', $record);
}
$records = $DB->get_records('assignfeedback_editpp_cmnt', array('gradeid'=>$gradeid, 'draft'=>1));
foreach ($records as $record) {
unset($record->id);
$record->draft = 0;
$DB->insert_record('assignfeedback_editpp_cmnt', $record);
}
return true;
}
/**
* Has annotations or comments.
* @param int $gradeid
* @return bool
*/
public static function has_annotations_or_comments($gradeid, $includedraft) {
global $DB;
$params = array('gradeid'=>$gradeid);
if (!$includedraft) {
$params['draft'] = 0;
}
if ($DB->count_records('assignfeedback_editpp_cmnt', $params)) {
return true;
}
if ($DB->count_records('assignfeedback_editpp_annot', $params)) {
return true;
}
return false;
}
/**
* Aborts all draft annotations and reverts to the last version released to students.
* @param int $gradeid
* @return bool
*/
public static function revert_drafts($gradeid) {
global $DB;
// Delete the previous non-draft annotations and comments.
$DB->delete_records('assignfeedback_editpp_cmnt', array('gradeid'=>$gradeid, 'draft'=>1));
$DB->delete_records('assignfeedback_editpp_annot', array('gradeid'=>$gradeid, 'draft'=>1));
// Copy all the draft annotations and comments to non-drafts.
$records = $DB->get_records('assignfeedback_editpp_annot', array('gradeid'=>$gradeid, 'draft'=>0));
foreach ($records as $record) {
unset($record->id);
$record->draft = 0;
$DB->insert_record('assignfeedback_editpp_annot', $record);
}
$records = $DB->get_records('assignfeedback_editpp_cmnt', array('gradeid'=>$gradeid, 'draft'=>0));
foreach ($records as $record) {
unset($record->id);
$record->draft = 0;
$DB->insert_record('assignfeedback_editpp_annot', $record);
}
return true;
}
/**
* Add a annotation to a page.
* @param annotation $annotation
* @return bool
*/
public static function add_annotation(annotation $annotation) {
global $DB;
$annotation->id = null;
return $DB->insert_record('assignfeedback_editpp_annot', $annotation);
}
/**
* Remove a annotation from a page.
* @param int $annotationid
* @return bool
*/
public static function remove_annotation($annotationid) {
global $DB;
return $DB->delete_records('assignfeedback_editpp_annot', array('id'=>$annotationid));
}
/**
* Copy annotations, comments, pages, and other required content from the source user to the current group member
* being procssed when using applytoall.
*
* @param int|\assign $assignment
* @param stdClass $grade
* @param int $sourceuserid
* @return bool
*/
public static function copy_drafts_from_to($assignment, $grade, $sourceuserid) {
global $DB;
// Delete any existing annotations and comments from current user.
$DB->delete_records('assignfeedback_editpp_annot', array('gradeid' => $grade->id));
$DB->delete_records('assignfeedback_editpp_cmnt', array('gradeid' => $grade->id));
// Get gradeid, annotations and comments from sourceuserid.
$sourceusergrade = $assignment->get_user_grade($sourceuserid, true, $grade->attemptnumber);
$annotations = $DB->get_records('assignfeedback_editpp_annot', array('gradeid' => $sourceusergrade->id, 'draft' => 1));
$comments = $DB->get_records('assignfeedback_editpp_cmnt', array('gradeid' => $sourceusergrade->id, 'draft' => 1));
$contextid = $assignment->get_context()->id;
$sourceitemid = $sourceusergrade->id;
// Add annotations and comments to current user to generate feedback file.
foreach ($annotations as $annotation) {
$annotation->gradeid = $grade->id;
$DB->insert_record('assignfeedback_editpp_annot', $annotation);
}
foreach ($comments as $comment) {
$comment->gradeid = $grade->id;
$DB->insert_record('assignfeedback_editpp_cmnt', $comment);
}
$fs = get_file_storage();
// Copy the stamp files.
self::replace_files_from_to($fs, $contextid, $sourceitemid, $grade->id, document_services::STAMPS_FILEAREA, true);
// Copy the PAGE_IMAGE_FILEAREA files.
self::replace_files_from_to($fs, $contextid, $sourceitemid, $grade->id, document_services::PAGE_IMAGE_FILEAREA);
return true;
}
/**
* Replace the area files in the specified area with those in the source item id.
*
* @param \file_storage $fs The file storage class
* @param int $contextid The ID of the context for the assignment.
* @param int $sourceitemid The itemid to copy from - typically the source grade id.
* @param int $itemid The itemid to copy to - typically the target grade id.
* @param string $area The file storage area.
* @param bool $includesubdirs Whether to copy the content of sub-directories too.
*/
public static function replace_files_from_to($fs, $contextid, $sourceitemid, $itemid, $area, $includesubdirs = false) {
$component = 'assignfeedback_editpdfplus';
// Remove the existing files within this area.
$fs->delete_area_files($contextid, $component, $area, $itemid);
// Copy the files from the source area.
if ($files = $fs->get_area_files($contextid, $component, $area, $sourceitemid,
"filename", $includesubdirs)) {
foreach ($files as $file) {
$newrecord = new \stdClass();
$newrecord->contextid = $contextid;
$newrecord->itemid = $itemid;
$fs->create_file_from_storedfile($newrecord, $file);
}
}
}
/**
* Delete the draft annotations and comments.
*
* This is intended to be used when the version of the PDF has changed and the annotations
* might not be relevant any more, therefore we should delete them.
*
* @param int $gradeid The grade ID.
* @return bool
*/
public static function delete_draft_content($gradeid) {
global $DB;
$conditions = array('gradeid' => $gradeid, 'draft' => 1);
$result = $DB->delete_records('assignfeedback_editpp_annot', $conditions);
$result = $result && $DB->delete_records('assignfeedback_editpp_cmnt', $conditions);
return $result;
}
}
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Library code for manipulating PDFs
*
* @package assignfeedback_editpdfplus
* @copyright 2012 Davo Smith
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace assignfeedback_editpdfplus;
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir.'/pdflib.php');
require_once($CFG->dirroot.'/mod/assign/feedback/editpdf/fpdi/fpdi.php');
/**
* Library code for manipulating PDFs
*
* @package assignfeedback_editpdfplus
* @copyright 2012 Davo Smith
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class pdf extends \FPDI {
/** @var int the number of the current page in the PDF being processed */
protected $currentpage = 0;
/** @var int the total number of pages in the PDF being processed */
protected $pagecount = 0;
/** @var float used to scale the pixel position of annotations (in the database) to the position in the final PDF */
protected $scale = 0.0;
/** @var string the path in which to store generated page images */
protected $imagefolder = null;
/** @var string the path to the PDF currently being processed */
protected $filename = null;
/** No errors */
const GSPATH_OK = 'ok';
/** Not set */
const GSPATH_EMPTY = 'empty';
/** Does not exist */
const GSPATH_DOESNOTEXIST = 'doesnotexist';
/** Is a dir */
const GSPATH_ISDIR = 'isdir';
/** Not executable */
const GSPATH_NOTEXECUTABLE = 'notexecutable';
/** Test file missing */
const GSPATH_NOTESTFILE = 'notestfile';
/** Any other error */
const GSPATH_ERROR = 'error';
/** Min. width an annotation should have */
const MIN_ANNOTATION_WIDTH = 5;
/** Min. height an annotation should have */
const MIN_ANNOTATION_HEIGHT = 5;
/**
* Combine the given PDF files into a single PDF. Optionally add a coversheet and coversheet fields.
* @param string[] $pdflist the filenames of the files to combine
* @param string $outfilename the filename to write to
* @return int the number of pages in the combined PDF
*/
public function combine_pdfs($pdflist, $outfilename) {
raise_memory_limit(MEMORY_EXTRA);
$olddebug = error_reporting(0);
$this->setPageUnit('pt');
$this->setPrintHeader(false);
$this->setPrintFooter(false);
$this->scale = 72.0 / 100.0;
$this->SetFont('helvetica', '', 16.0 * $this->scale);
$this->SetTextColor(0, 0, 0);
$totalpagecount = 0;
foreach ($pdflist as $file) {
$pagecount = $this->setSourceFile($file);
$totalpagecount += $pagecount;
for ($i = 1; $i<=$pagecount; $i++) {
$this->create_page_from_source($i);
}
}
$this->save_pdf($outfilename);
error_reporting($olddebug);
return $totalpagecount;
}
/**
* The number of the current page in the PDF being processed
* @return int
*/
public function current_page() {
return $this->currentpage;
}
/**
* The total number of pages in the PDF being processed
* @return int
*/
public function page_count() {
return $this->pagecount;
}
/**
* Load the specified PDF and set the initial output configuration
* Used when processing comments and outputting a new PDF
* @param string $filename the path to the PDF to load
* @return int the number of pages in the PDF
*/
public function load_pdf($filename) {
raise_memory_limit(MEMORY_EXTRA);
$olddebug = error_reporting(0);
$this->setPageUnit('pt');
$this->scale = 72.0 / 100.0;
$this->SetFont('helvetica', '', 16.0 * $this->scale);
$this->SetFillColor(255, 255, 176);
$this->SetDrawColor(0, 0, 0);
$this->SetLineWidth(1.0 * $this->scale);
$this->SetTextColor(0, 0, 0);
$this->setPrintHeader(false);
$this->setPrintFooter(false);
$this->pagecount = $this->setSourceFile($filename);
$this->filename = $filename;
error_reporting($olddebug);
return $this->pagecount;
}
/**
* Sets the name of the PDF to process, but only loads the file if the
* pagecount is zero (in order to count the number of pages)
* Used when generating page images (but not a new PDF)
* @param string $filename the path to the PDF to process