PageRenderTime 33ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/assign/feedback/editpdf/locallib.php

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