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 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * View, create or edit single example submission
  20   *
  21   * @package    mod_workshop
  22   * @copyright  2009 David Mudrak <david.mudrak@gmail.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  require(__DIR__.'/../../config.php');
  27  require_once (__DIR__.'/locallib.php');
  28  
  29  $cmid       = required_param('cmid', PARAM_INT);            // course module id
  30  $id         = required_param('id', PARAM_INT);              // example submission id, 0 for the new one
  31  $edit       = optional_param('edit', false, PARAM_BOOL);    // open for editing?
  32  $delete     = optional_param('delete', false, PARAM_BOOL);  // example removal requested
  33  $confirm    = optional_param('confirm', false, PARAM_BOOL); // example removal request confirmed
  34  $assess     = optional_param('assess', false, PARAM_BOOL);  // assessment required
  35  
  36  $cm         = get_coursemodule_from_id('workshop', $cmid, 0, false, MUST_EXIST);
  37  $course     = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
  38  
  39  require_login($course, false, $cm);
  40  if (isguestuser()) {
  41      throw new \moodle_exception('guestsarenotallowed');
  42  }
  43  
  44  $workshop = $DB->get_record('workshop', array('id' => $cm->instance), '*', MUST_EXIST);
  45  $workshop = new workshop($workshop, $cm, $course);
  46  
  47  $PAGE->set_url($workshop->exsubmission_url($id), array('edit' => $edit));
  48  $PAGE->set_title($workshop->name);
  49  $PAGE->set_heading($course->fullname);
  50  $PAGE->set_secondary_active_tab('modulepage');
  51  if ($edit) {
  52      $PAGE->navbar->add(get_string('exampleediting', 'workshop'));
  53  } else {
  54      $PAGE->navbar->add(get_string('example', 'workshop'));
  55  }
  56  $output = $PAGE->get_renderer('mod_workshop');
  57  
  58  if ($id) { // example is specified
  59      $example = $workshop->get_example_by_id($id);
  60  } else { // no example specified - create new one
  61      require_capability('mod/workshop:manageexamples', $workshop->context);
  62      $example = new stdclass();
  63      $example->id = null;
  64      $example->authorid = $USER->id;
  65      $example->example = 1;
  66  }
  67  
  68  $canmanage  = has_capability('mod/workshop:manageexamples', $workshop->context);
  69  $canassess  = has_capability('mod/workshop:peerassess', $workshop->context);
  70  $refasid    = $DB->get_field('workshop_assessments', 'id', array('submissionid' => $example->id, 'weight' => 1));
  71  
  72  if ($example->id and ($canmanage or ($workshop->assessing_examples_allowed() and $canassess))) {
  73      // ok you can go
  74  } elseif (is_null($example->id) and $canmanage) {
  75      // ok you can go
  76  } else {
  77      throw new \moodle_exception('nopermissions', 'error', $workshop->view_url(), 'view or manage example submission');
  78  }
  79  
  80  if ($id and $delete and $confirm and $canmanage) {
  81      require_sesskey();
  82      $workshop->delete_submission($example);
  83      redirect($workshop->view_url());
  84  }
  85  
  86  if ($id and $assess and $canmanage) {
  87      // reference assessment of an example is the assessment with the weight = 1. There should be just one
  88      // such assessment
  89      require_sesskey();
  90      if (!$refasid) {
  91          $refasid = $workshop->add_allocation($example, $USER->id, 1);
  92      }
  93      redirect($workshop->exassess_url($refasid));
  94  }
  95  
  96  if ($id and $assess and $canassess) {
  97      // training assessment of an example is the assessment with the weight = 0
  98      require_sesskey();
  99      $asid = $DB->get_field('workshop_assessments', 'id',
 100              array('submissionid' => $example->id, 'weight' => 0, 'reviewerid' => $USER->id));
 101      if (!$asid) {
 102          $asid = $workshop->add_allocation($example, $USER->id, 0);
 103      }
 104      if ($asid == workshop::ALLOCATION_EXISTS) {
 105          // the training assessment of the example was not found but the allocation already
 106          // exists. this probably means that the user is the author of the reference assessment.
 107          echo $output->header();
 108          echo $output->box(get_string('assessmentreferenceconflict', 'workshop'));
 109          echo $output->continue_button($workshop->view_url());
 110          echo $output->footer();
 111          die();
 112      }
 113      redirect($workshop->exassess_url($asid));
 114  }
 115  
 116  if ($edit and $canmanage) {
 117      require_once (__DIR__.'/submission_form.php');
 118  
 119      $example = file_prepare_standard_editor($example, 'content', $workshop->submission_content_options(),
 120          $workshop->context, 'mod_workshop', 'submission_content', $example->id);
 121  
 122      $example = file_prepare_standard_filemanager($example, 'attachment', $workshop->submission_attachment_options(),
 123          $workshop->context, 'mod_workshop', 'submission_attachment', $example->id);
 124  
 125      $mform = new workshop_submission_form($PAGE->url, array('current' => $example, 'workshop' => $workshop,
 126          'contentopts' => $workshop->submission_content_options(), 'attachmentopts' => $workshop->submission_attachment_options()));
 127  
 128      if ($mform->is_cancelled()) {
 129          redirect($workshop->view_url());
 130  
 131      } elseif ($canmanage and $formdata = $mform->get_data()) {
 132          if ($formdata->example == 1) {
 133              // this was used just for validation, it must be set to one when dealing with example submissions
 134              unset($formdata->example);
 135          } else {
 136              throw new coding_exception('Invalid submission form data value: example');
 137          }
 138          $timenow = time();
 139          if (is_null($example->id)) {
 140              $formdata->workshopid     = $workshop->id;
 141              $formdata->example        = 1;
 142              $formdata->authorid       = $USER->id;
 143              $formdata->timecreated    = $timenow;
 144              $formdata->feedbackauthorformat = editors_get_preferred_format();
 145          }
 146          $formdata->timemodified       = $timenow;
 147          $formdata->title              = trim($formdata->title);
 148          $formdata->content            = '';          // updated later
 149          $formdata->contentformat      = FORMAT_HTML; // updated later
 150          $formdata->contenttrust       = 0;           // updated later
 151          if (is_null($example->id)) {
 152              $example->id = $formdata->id = $DB->insert_record('workshop_submissions', $formdata);
 153          } else {
 154              if (empty($formdata->id) or empty($example->id) or ($formdata->id != $example->id)) {
 155                  throw new moodle_exception('err_examplesubmissionid', 'workshop');
 156              }
 157          }
 158  
 159          // Save and relink embedded images and save attachments.
 160          // To be used when Online text is allowed as a submission type.
 161          if (!empty($formdata->content_editor)) {
 162              $formdata = file_postupdate_standard_editor($formdata, 'content', $workshop->submission_content_options(),
 163                  $workshop->context, 'mod_workshop', 'submission_content', $example->id);
 164              $formdata = file_postupdate_standard_filemanager($formdata, 'attachment', $workshop->submission_attachment_options(),
 165                  $workshop->context, 'mod_workshop', 'submission_attachment', $example->id);
 166          }
 167  
 168          if (empty($formdata->attachment)) {
 169              // explicit cast to zero integer
 170              $formdata->attachment = 0;
 171          }
 172          // store the updated values or re-save the new example (re-saving needed because URLs are now rewritten)
 173          $DB->update_record('workshop_submissions', $formdata);
 174          redirect($workshop->exsubmission_url($formdata->id));
 175      }
 176  }
 177  
 178  // Output starts here
 179  echo $output->header();
 180  if (!$PAGE->has_secondary_navigation()) {
 181      echo $output->heading(format_string($workshop->name), 2);
 182  }
 183  
 184  // show instructions for submitting as they may contain some list of questions and we need to know them
 185  // while reading the submitted answer
 186  if (trim($workshop->instructauthors)) {
 187      $instructions = file_rewrite_pluginfile_urls($workshop->instructauthors, 'pluginfile.php', $PAGE->context->id,
 188          'mod_workshop', 'instructauthors', null, workshop::instruction_editors_options($PAGE->context));
 189      print_collapsible_region_start('', 'workshop-viewlet-instructauthors', get_string('instructauthors', 'workshop'),
 190              'workshop-viewlet-instructauthors-collapsed');
 191      echo $output->box(format_text($instructions, $workshop->instructauthorsformat, array('overflowdiv'=>true)), array('generalbox', 'instructions'));
 192      print_collapsible_region_end();
 193  }
 194  
 195  // if in edit mode, display the form to edit the example
 196  if ($edit and $canmanage) {
 197      $mform->display();
 198      echo $output->footer();
 199      die();
 200  }
 201  
 202  // else display the example...
 203  if ($example->id) {
 204      if ($canmanage and $delete) {
 205      echo $output->confirm(get_string('exampledeleteconfirm', 'workshop'),
 206              new moodle_url($PAGE->url, array('delete' => 1, 'confirm' => 1)), $workshop->view_url());
 207      }
 208      if ($canmanage and !$delete and !$DB->record_exists_select('workshop_assessments',
 209              'grade IS NOT NULL AND weight=1 AND submissionid = ?', array($example->id))) {
 210          echo $output->confirm(get_string('assessmentreferenceneeded', 'workshop'),
 211                  new moodle_url($PAGE->url, array('assess' => 1)), $workshop->view_url());
 212      }
 213      echo $output->render($workshop->prepare_example_submission($example));
 214  }
 215  // ...with an option to edit or remove it
 216  echo $output->container_start('buttonsbar');
 217  if ($canmanage) {
 218      if (empty($edit) and empty($delete)) {
 219          $aurl = new moodle_url($workshop->exsubmission_url($example->id), array('edit' => 'on'));
 220          echo $output->single_button($aurl, get_string('exampleedit', 'workshop'), 'get');
 221  
 222          $aurl = new moodle_url($workshop->exsubmission_url($example->id), array('delete' => 'on'));
 223          echo $output->single_button($aurl, get_string('exampledelete', 'workshop'), 'get');
 224      }
 225  }
 226  // ...and optionally assess it
 227  if ($canassess or ($canmanage and empty($edit) and empty($delete))) {
 228      $aurl = new moodle_url($workshop->exsubmission_url($example->id), array('assess' => 'on', 'sesskey' => sesskey()));
 229      echo $output->single_button($aurl, get_string('exampleassess', 'workshop'), 'get');
 230  }
 231  echo $output->container_end(); // buttonsbar
 232  // and possibly display the example's review(s) - todo
 233  echo $output->footer();