Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

   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      print_error('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  if ($edit) {
  51      $PAGE->navbar->add(get_string('exampleediting', 'workshop'));
  52  } else {
  53      $PAGE->navbar->add(get_string('example', 'workshop'));
  54  }
  55  $output = $PAGE->get_renderer('mod_workshop');
  56  
  57  if ($id) { // example is specified
  58      $example = $workshop->get_example_by_id($id);
  59  } else { // no example specified - create new one
  60      require_capability('mod/workshop:manageexamples', $workshop->context);
  61      $example = new stdclass();
  62      $example->id = null;
  63      $example->authorid = $USER->id;
  64      $example->example = 1;
  65  }
  66  
  67  $canmanage  = has_capability('mod/workshop:manageexamples', $workshop->context);
  68  $canassess  = has_capability('mod/workshop:peerassess', $workshop->context);
  69  $refasid    = $DB->get_field('workshop_assessments', 'id', array('submissionid' => $example->id, 'weight' => 1));
  70  
  71  if ($example->id and ($canmanage or ($workshop->assessing_examples_allowed() and $canassess))) {
  72      // ok you can go
  73  } elseif (is_null($example->id) and $canmanage) {
  74      // ok you can go
  75  } else {
  76      print_error('nopermissions', 'error', $workshop->view_url(), 'view or manage example submission');
  77  }
  78  
  79  if ($id and $delete and $confirm and $canmanage) {
  80      require_sesskey();
  81      $workshop->delete_submission($example);
  82      redirect($workshop->view_url());
  83  }
  84  
  85  if ($id and $assess and $canmanage) {
  86      // reference assessment of an example is the assessment with the weight = 1. There should be just one
  87      // such assessment
  88      require_sesskey();
  89      if (!$refasid) {
  90          $refasid = $workshop->add_allocation($example, $USER->id, 1);
  91      }
  92      redirect($workshop->exassess_url($refasid));
  93  }
  94  
  95  if ($id and $assess and $canassess) {
  96      // training assessment of an example is the assessment with the weight = 0
  97      require_sesskey();
  98      $asid = $DB->get_field('workshop_assessments', 'id',
  99              array('submissionid' => $example->id, 'weight' => 0, 'reviewerid' => $USER->id));
 100      if (!$asid) {
 101          $asid = $workshop->add_allocation($example, $USER->id, 0);
 102      }
 103      if ($asid == workshop::ALLOCATION_EXISTS) {
 104          // the training assessment of the example was not found but the allocation already
 105          // exists. this probably means that the user is the author of the reference assessment.
 106          echo $output->header();
 107          echo $output->box(get_string('assessmentreferenceconflict', 'workshop'));
 108          echo $output->continue_button($workshop->view_url());
 109          echo $output->footer();
 110          die();
 111      }
 112      redirect($workshop->exassess_url($asid));
 113  }
 114  
 115  if ($edit and $canmanage) {
 116      require_once (__DIR__.'/submission_form.php');
 117  
 118      $example = file_prepare_standard_editor($example, 'content', $workshop->submission_content_options(),
 119          $workshop->context, 'mod_workshop', 'submission_content', $example->id);
 120  
 121      $example = file_prepare_standard_filemanager($example, 'attachment', $workshop->submission_attachment_options(),
 122          $workshop->context, 'mod_workshop', 'submission_attachment', $example->id);
 123  
 124      $mform = new workshop_submission_form($PAGE->url, array('current' => $example, 'workshop' => $workshop,
 125          'contentopts' => $workshop->submission_content_options(), 'attachmentopts' => $workshop->submission_attachment_options()));
 126  
 127      if ($mform->is_cancelled()) {
 128          redirect($workshop->view_url());
 129  
 130      } elseif ($canmanage and $formdata = $mform->get_data()) {
 131          if ($formdata->example == 1) {
 132              // this was used just for validation, it must be set to one when dealing with example submissions
 133              unset($formdata->example);
 134          } else {
 135              throw new coding_exception('Invalid submission form data value: example');
 136          }
 137          $timenow = time();
 138          if (is_null($example->id)) {
 139              $formdata->workshopid     = $workshop->id;
 140              $formdata->example        = 1;
 141              $formdata->authorid       = $USER->id;
 142              $formdata->timecreated    = $timenow;
 143              $formdata->feedbackauthorformat = editors_get_preferred_format();
 144          }
 145          $formdata->timemodified       = $timenow;
 146          $formdata->title              = trim($formdata->title);
 147          $formdata->content            = '';          // updated later
 148          $formdata->contentformat      = FORMAT_HTML; // updated later
 149          $formdata->contenttrust       = 0;           // updated later
 150          if (is_null($example->id)) {
 151              $example->id = $formdata->id = $DB->insert_record('workshop_submissions', $formdata);
 152          } else {
 153              if (empty($formdata->id) or empty($example->id) or ($formdata->id != $example->id)) {
 154                  throw new moodle_exception('err_examplesubmissionid', 'workshop');
 155              }
 156          }
 157  
 158          // Save and relink embedded images and save attachments.
 159          $formdata = file_postupdate_standard_editor($formdata, 'content', $workshop->submission_content_options(),
 160              $workshop->context, 'mod_workshop', 'submission_content', $example->id);
 161          $formdata = file_postupdate_standard_filemanager($formdata, 'attachment', $workshop->submission_attachment_options(),
 162              $workshop->context, 'mod_workshop', 'submission_attachment', $example->id);
 163  
 164          if (empty($formdata->attachment)) {
 165              // explicit cast to zero integer
 166              $formdata->attachment = 0;
 167          }
 168          // store the updated values or re-save the new example (re-saving needed because URLs are now rewritten)
 169          $DB->update_record('workshop_submissions', $formdata);
 170          redirect($workshop->exsubmission_url($formdata->id));
 171      }
 172  }
 173  
 174  // Output starts here
 175  echo $output->header();
 176  echo $output->heading(format_string($workshop->name), 2);
 177  
 178  // show instructions for submitting as they may contain some list of questions and we need to know them
 179  // while reading the submitted answer
 180  if (trim($workshop->instructauthors)) {
 181      $instructions = file_rewrite_pluginfile_urls($workshop->instructauthors, 'pluginfile.php', $PAGE->context->id,
 182          'mod_workshop', 'instructauthors', null, workshop::instruction_editors_options($PAGE->context));
 183      print_collapsible_region_start('', 'workshop-viewlet-instructauthors', get_string('instructauthors', 'workshop'),
 184              'workshop-viewlet-instructauthors-collapsed');
 185      echo $output->box(format_text($instructions, $workshop->instructauthorsformat, array('overflowdiv'=>true)), array('generalbox', 'instructions'));
 186      print_collapsible_region_end();
 187  }
 188  
 189  // if in edit mode, display the form to edit the example
 190  if ($edit and $canmanage) {
 191      $mform->display();
 192      echo $output->footer();
 193      die();
 194  }
 195  
 196  // else display the example...
 197  if ($example->id) {
 198      if ($canmanage and $delete) {
 199      echo $output->confirm(get_string('exampledeleteconfirm', 'workshop'),
 200              new moodle_url($PAGE->url, array('delete' => 1, 'confirm' => 1)), $workshop->view_url());
 201      }
 202      if ($canmanage and !$delete and !$DB->record_exists_select('workshop_assessments',
 203              'grade IS NOT NULL AND weight=1 AND submissionid = ?', array($example->id))) {
 204          echo $output->confirm(get_string('assessmentreferenceneeded', 'workshop'),
 205                  new moodle_url($PAGE->url, array('assess' => 1)), $workshop->view_url());
 206      }
 207      echo $output->render($workshop->prepare_example_submission($example));
 208  }
 209  // ...with an option to edit or remove it
 210  echo $output->container_start('buttonsbar');
 211  if ($canmanage) {
 212      if (empty($edit) and empty($delete)) {
 213          $aurl = new moodle_url($workshop->exsubmission_url($example->id), array('edit' => 'on'));
 214          echo $output->single_button($aurl, get_string('exampleedit', 'workshop'), 'get');
 215  
 216          $aurl = new moodle_url($workshop->exsubmission_url($example->id), array('delete' => 'on'));
 217          echo $output->single_button($aurl, get_string('exampledelete', 'workshop'), 'get');
 218      }
 219  }
 220  // ...and optionally assess it
 221  if ($canassess or ($canmanage and empty($edit) and empty($delete))) {
 222      $aurl = new moodle_url($workshop->exsubmission_url($example->id), array('assess' => 'on', 'sesskey' => sesskey()));
 223      echo $output->single_button($aurl, get_string('exampleassess', 'workshop'), 'get');
 224  }
 225  echo $output->container_end(); // buttonsbar
 226  // and possibly display the example's review(s) - todo
 227  echo $output->footer();