From 50fc1ec44977683b10d83028e2dcc156c6ac0ca9 Mon Sep 17 00:00:00 2001
From: Daniel Nolte <>
Date: Thu, 24 Aug 2023 13:35:31 +0200
Subject: [PATCH 1/9] feat(submission): Teachers can now add submissions that
participants can later annotate. The submission of an AnnoPy can now be
viewed on the overview page.
---
CHANGES.md | 5 +
classes/local/helper.php | 67 ++++++++
classes/local/submissionstats.php | 136 +++++++++++++++
classes/output/annopy_view.php | 36 +++-
db/install.xml | 1 +
lang/de/annopy.php | 35 +++-
lang/en/annopy.php | 35 +++-
lib.php | 39 +----
styles.css | 179 +++++++++++++++++++-
submit.php | 253 ++++++++++++++++++++++++++++
example_form.php => submit_form.php | 36 ++--
templates/annopy_view.mustache | 107 +++++++++++-
version.php | 4 +-
view.php | 91 ++--------
14 files changed, 888 insertions(+), 136 deletions(-)
create mode 100644 classes/local/helper.php
create mode 100644 classes/local/submissionstats.php
create mode 100644 submit.php
rename example_form.php => submit_form.php (58%)
diff --git a/CHANGES.md b/CHANGES.md
index 70e95ef..4ac3448 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -1,4 +1,9 @@
## Changelog ##
+
+- [0.1]:
+ - Teachers can now add submissions that participants can later annotate.
+ - The submission of an AnnoPy can now be viewed on the overview page.
+
- [0.0.1]:
- Added plugin template files.
- Added first capabilities, events and database structure.
diff --git a/classes/local/helper.php b/classes/local/helper.php
new file mode 100644
index 0000000..37a7d76
--- /dev/null
+++ b/classes/local/helper.php
@@ -0,0 +1,67 @@
+.
+
+/**
+ * Helper utilities for the module.
+ *
+ * @package mod_annopy
+ * @copyright 2023 coactum GmbH
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+namespace mod_annopy\local;
+
+/**
+ * Utility class for the module.
+ *
+ * @package mod_annopy
+ * @copyright 2023 coactum GmbH
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class helper {
+ /**
+ * Return the editor and attachment options for a submission.
+ * @param stdClass $course The course object.
+ * @param stdClass $context The context object.
+
+ * @return array $editoroptions Array containing the editor options.
+ * @return array $attachmentoptions Array containing the attachment options.
+ */
+ public static function annopy_get_editor_and_attachment_options($course, $context) {
+ // For the editor.
+ $editoroptions = array(
+ 'trusttext' => true,
+ 'maxfiles' => EDITOR_UNLIMITED_FILES,
+ 'maxbytes' => $course->maxbytes,
+ 'context' => $context,
+ 'subdirs' => false,
+ );
+
+ // If maxfiles would be set to an int and more files are given the editor saves them all but
+ // saves the overcouting incorrect so that white box is displayed.
+
+ // For a file attachments field (not really needed here).
+ $attachmentoptions = array(
+ 'subdirs' => false,
+ 'maxfiles' => 1,
+ 'maxbytes' => $course->maxbytes
+ );
+
+ return array(
+ $editoroptions,
+ $attachmentoptions
+ );
+ }
+}
diff --git a/classes/local/submissionstats.php b/classes/local/submissionstats.php
new file mode 100644
index 0000000..cb93ae8
--- /dev/null
+++ b/classes/local/submissionstats.php
@@ -0,0 +1,136 @@
+.
+
+/**
+ * Stats utilities for AnnoPy submissions.
+ *
+ * @package mod_annopy
+ * @copyright 2023 coactum GmbH
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+namespace mod_annopy\local;
+
+use stdClass;
+use core_text;
+
+/**
+ * Utility class for AnnoPy submission stats.
+ *
+ * @package mod_annopy
+ * @copyright 2023 coactum GmbH
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class submissionstats {
+
+ /**
+ * Get the statistics for this submission.
+ *
+ * @param string $submissiontext The text for this submission.
+ * @param string $submissiontimecreated The time then the submission was created.
+ * @return array submissionstats Array with the statistics of the submission.
+ */
+ public static function get_submission_stats($submissiontext, $submissiontimecreated) {
+
+ $cleantext = preg_replace('#<[^>]+>#', ' ', $submissiontext, -1, $replacementspacescount);
+
+ $submissionstats = array();
+ $submissionstats['words'] = self::get_stats_words($cleantext);
+ $submissionstats['chars'] = self::get_stats_chars($cleantext) - $replacementspacescount;
+ $submissionstats['sentences'] = self::get_stats_sentences($cleantext);
+ $submissionstats['paragraphs'] = self::get_stats_paragraphs($cleantext);
+ $submissionstats['uniquewords'] = self::get_stats_uniquewords($cleantext);
+ $submissionstats['spaces'] = self::get_stats_spaces($cleantext) - $replacementspacescount;
+ $submissionstats['charswithoutspaces'] = $submissionstats['chars'] - $submissionstats['spaces'];
+
+ $timenow = new \DateTime(date('Y-m-d G:i:s', time()));
+ $timesubmissioncreated = new \DateTime(date('Y-m-d G:i:s', $submissiontimecreated));
+
+ if ($timenow >= $timesubmissioncreated) {
+ $submissionstats['datediff'] = date_diff($timenow, $timesubmissioncreated);
+ } else {
+ $submissionstats['datediff'] = false;
+ }
+
+ return $submissionstats;
+ }
+
+ /**
+ * Get the character count statistics for this annopy submission.
+ *
+ * @param string $submissiontext The text for this submission.
+ * @ return int The number of characters.
+ */
+ public static function get_stats_chars($submissiontext) {
+ return core_text::strlen($submissiontext);
+ }
+
+ /**
+ * Get the word count statistics for this annopy submission.
+ *
+ * @param string $submissiontext The text for this submission.
+ * @ return int The number of words.
+ */
+ public static function get_stats_words($submissiontext) {
+ return count_words($submissiontext);
+ }
+
+ /**
+ * Get the sentence count statistics for this annopy submission.
+ *
+ * @param string $submissiontext The text for this submission.
+ * @ return int The number of sentences.
+ */
+ public static function get_stats_sentences($submissiontext) {
+ $sentences = preg_split('/[!?.]+(?![0-9])/', $submissiontext);
+ $sentences = array_filter($sentences);
+ return count($sentences);
+ }
+
+ /**
+ * Get the paragraph count statistics for this annopy submission.
+ *
+ * @param string $submissiontext The text for this submission.
+ * @ return int The number of paragraphs.
+ */
+ public static function get_stats_paragraphs($submissiontext) {
+ $paragraphs = explode("\n", $submissiontext);
+ $paragraphs = array_filter($paragraphs);
+ return count($paragraphs);
+ }
+
+ /**
+ * Get the unique word count statistics for this annopy submission.
+ *
+ * @param string $submissiontext The text for this submission.
+ * @return int The number of unique words.
+ */
+ public static function get_stats_uniquewords($submissiontext) {
+ $items = core_text::strtolower($submissiontext);
+ $items = str_word_count($items, 1);
+ $items = array_unique($items);
+ return count($items);
+ }
+
+ /**
+ * Get the raw spaces count statistics for this annopy submission.
+ *
+ * @param string $submissiontext The text for this submission.
+ * @return int The number of spaces.
+ */
+ public static function get_stats_spaces($submissiontext) {
+ return substr_count($submissiontext, ' ');
+ }
+}
diff --git a/classes/output/annopy_view.php b/classes/output/annopy_view.php
index eccad46..e87603c 100644
--- a/classes/output/annopy_view.php
+++ b/classes/output/annopy_view.php
@@ -23,6 +23,7 @@
*/
namespace mod_annopy\output;
+use mod_annopy\local\submissionstats;
use renderable;
use renderer_base;
use templatable;
@@ -39,13 +40,25 @@ class annopy_view implements renderable, templatable {
/** @var int */
protected $cmid;
+ /** @var object */
+ protected $course;
+ /** @var object */
+ protected $context;
+ /** @var object */
+ protected $submission;
/**
* Construct this renderable.
* @param int $cmid The course module id
+ * @param object $course The course
+ * @param object $context The context
+ * @param object $submission The submission
*/
- public function __construct($cmid) {
+ public function __construct($cmid, $course, $context, $submission) {
$this->cmid = $cmid;
+ $this->course = $course;
+ $this->context = $context;
+ $this->submission = $submission;
}
/**
@@ -55,8 +68,29 @@ public function __construct($cmid) {
* @return stdClass
*/
public function export_for_template(renderer_base $output) {
+ global $DB, $USER, $OUTPUT;
+
$data = new stdClass();
$data->cmid = $this->cmid;
+ $data->submission = $this->submission;
+
+ if ($data->submission) {
+ // If submission can be edited.
+ $data->submission->canbeedited = has_capability('mod/annopy:editsubmission', $this->context);
+
+ // Set submission user.
+ $data->submission->user = $DB->get_record('user', array('id' => $data->submission->author));
+ $data->submission->user->userpicture = $OUTPUT->user_picture($data->submission->user,
+ array('courseid' => $this->course->id, 'link' => true, 'includefullname' => true, 'size' => 25));
+
+ // Submission stats.
+ $data->submission->stats = submissionstats::get_submission_stats($data->submission->content,
+ $data->submission->timecreated);
+ $data->submission->canviewdetails = has_capability('mod/annopy:addsubmission', $this->context);
+
+ }
+
+ $data->canaddsubmission = has_capability('mod/annopy:addsubmission', $this->context);
return $data;
}
}
diff --git a/db/install.xml b/db/install.xml
index 6bf00bd..1f6ad2a 100644
--- a/db/install.xml
+++ b/db/install.xml
@@ -41,6 +41,7 @@
{{#str}}nosubmission, mod_annopy{{/str}}
{{/content}} + {{#canviewdetails}}{{#str}}participant, mod_annopy{{/str}} | + {{#annopyannotationtypes}} ++ {{name}} + + | + {{/annopyannotationtypes}} +
---|---|
{{firstname}} {{lastname}} | + {{#annotations}}{{.}} | {{/annotations}} + {{/participants}} +