Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 and 403]

   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   * Output the user submission actionbar for this activity.
  19   *
  20   * @package   mod_assign
  21   * @copyright 2021 Adrian Greeve <adrian@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace mod_assign\output;
  26  
  27  use templatable;
  28  use renderable;
  29  use moodle_url;
  30  use help_icon;
  31  use single_button;
  32  use stdClass;
  33  
  34  /**
  35   * Output the user submission actionbar for this activity.
  36   *
  37   * @package   mod_assign
  38   * @copyright 2021 Adrian Greeve <adrian@moodle.com>
  39   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class user_submission_actionmenu implements templatable, renderable {
  42  
  43      /** @var int The course module ID. */
  44      protected $cmid;
  45      /** @var bool Whether to show the submit button. */
  46      protected $showsubmit;
  47      /** @var bool Whether to show the edit button. */
  48      protected $showedit;
  49      /** @var stdClass A submission for this activity. */
  50      protected $submission;
  51      /** @var stdClass A team submission for this activity. */
  52      protected $teamsubmission;
  53      /** @var int The time limit for the submission. 0 = no time limit. */
  54      protected $timelimit;
  55  
  56      /**
  57       * Constructor for this object.
  58       *
  59       * @param int $cmid The course module ID.
  60       * @param bool $showsubmit Whether to show the submit button.
  61       * @param bool $showedit Whether to show the edit button.
  62       * @param stdClass|null $submission A submission for this activity.
  63       * @param stdClass|null $teamsubmission A team submission for this activity.
  64       * @param int $timelimit The time limit for completing this activity.
  65       */
  66      public function __construct(int $cmid, bool $showsubmit, bool $showedit, stdClass $submission = null,
  67              stdClass $teamsubmission = null, int $timelimit = 0) {
  68  
  69          $this->cmid = $cmid;
  70          $this->showsubmit = $showsubmit;
  71          $this->showedit = $showedit;
  72          $this->submission = $submission;
  73          $this->teamsubmission = $teamsubmission;
  74          $this->timelimit = $timelimit;
  75      }
  76  
  77      /**
  78       * Get the submission status.
  79       *
  80       * @return string The status of the submission.
  81       */
  82      protected function get_current_status(): string {
  83          if (!is_null($this->teamsubmission) && isset($this->teamsubmission->status)) {
  84              return $this->teamsubmission->status;
  85          } else if (!empty((array)$this->submission)) {
  86              return $this->submission->status;
  87          } else {
  88              return ASSIGN_SUBMISSION_STATUS_NEW;
  89          }
  90      }
  91  
  92      /**
  93       * Export the submission buttons for the page.
  94       *
  95       * @param  \renderer_base $output renderer base output.
  96       * @return array The data to be rendered.
  97       */
  98      public function export_for_template(\renderer_base $output): array {
  99          $data = ['edit' => false, 'submit' => false, 'remove' => false, 'previoussubmission' => false];
 100          if ($this->showedit) {
 101              $url = new moodle_url('/mod/assign/view.php', ['id' => $this->cmid, 'action' => 'editsubmission']);
 102              $button = new single_button($url, get_string('editsubmission', 'mod_assign'), 'get');
 103              $data['edit'] = [
 104                  'button' => $button->export_for_template($output),
 105              ];
 106              $status = $this->get_current_status();
 107              if ($status !== ASSIGN_SUBMISSION_STATUS_NEW && $status !== ASSIGN_SUBMISSION_STATUS_REOPENED) {
 108                  $url = new moodle_url('/mod/assign/view.php', ['id' => $this->cmid, 'action' => 'removesubmissionconfirm']);
 109                  $button = new single_button($url, get_string('removesubmission', 'mod_assign'), 'get');
 110                  $data['remove'] = ['button' => $button->export_for_template($output)];
 111              }
 112              if ($status === ASSIGN_SUBMISSION_STATUS_REOPENED) {
 113                  $params = ['id' => $this->cmid, 'action' => 'editprevioussubmission', 'sesskey' => sesskey()];
 114                  $url = new moodle_url('/mod/assign/view.php', $params);
 115                  $button = new single_button($url, get_string('addnewattemptfromprevious', 'mod_assign'), 'get');
 116                  $help = new help_icon('addnewattemptfromprevious', 'mod_assign');
 117                  $data['previoussubmission'] = [
 118                      'button' => $button->export_for_template($output),
 119                      'help' => $help->export_for_template($output)
 120                  ];
 121                  $url = new moodle_url('/mod/assign/view.php', ['id' => $this->cmid, 'action' => 'editsubmission']);
 122                  $newattemptbutton = new single_button($url, get_string('addnewattempt', 'mod_assign'), 'get', true);
 123                  $newattempthelp = new help_icon('addnewattempt', 'mod_assign');
 124                  $data['edit']['button'] = $newattemptbutton->export_for_template($output);
 125                  $data['edit']['help'] = $newattempthelp->export_for_template($output);
 126              }
 127              if ($status === ASSIGN_SUBMISSION_STATUS_NEW) {
 128  
 129                  if ($this->timelimit && empty($this->submission->timestarted)) {
 130                      $confirmation = new \confirm_action(
 131                          get_string('confirmstart', 'assign', format_time($this->timelimit)),
 132                          null,
 133                          get_string('beginassignment', 'assign')
 134                      );
 135                      $urlparams = array('id' => $this->cmid, 'action' => 'editsubmission');
 136                      $beginbutton = new \action_link(
 137                          new moodle_url('/mod/assign/view.php', $urlparams),
 138                              get_string('beginassignment', 'assign'),
 139                              $confirmation,
 140                              ['class' => 'btn btn-primary']
 141                      );
 142                      $data['edit']['button'] = $beginbutton->export_for_template($output);
 143                      $data['edit']['begin'] = true;
 144                      $data['edit']['help'] = '';
 145                  } else {
 146                      $newattemptbutton = new single_button($url, get_string('addsubmission', 'mod_assign'), 'get', true);
 147                      $data['edit']['button'] = $newattemptbutton->export_for_template($output);
 148                      $data['edit']['help'] = '';
 149                  }
 150              }
 151          }
 152          if ($this->showsubmit) {
 153              $url = new moodle_url('/mod/assign/view.php', ['id' => $this->cmid, 'action' => 'submit']);
 154              $button = new single_button($url, get_string('submitassignment', 'mod_assign'), 'get', true);
 155              $help = new help_icon('submitassignment', 'mod_assign');
 156              $data['submit'] = [
 157                  'button' => $button->export_for_template($output),
 158                  'help' => $help->export_for_template($output)
 159              ];
 160          }
 161          return $data;
 162      }
 163  }