Search moodle.org's
Developer Documentation


  • Bug fixes for general core bugs in 2.8.x ended 9 November 2015 (12 months).
  • Bug fixes for security issues in 2.8.x ended 9 May 2016 (18 months).
  • minimum PHP 5.4.4 (always use latest PHP 5.4.x or 5.5.x on Windows - http://windows.php.net/download/), PHP 7 is NOT supported
  • /mod/assign/ -> lib.php (source)

    Differences Between: [Versions 28 and 29] [Versions 28 and 30] [Versions 28 and 31] [Versions 28 and 32] [Versions 28 and 33] [Versions 28 and 34] [Versions 28 and 35] [Versions 28 and 36] [Versions 28 and 37]

       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   * This file contains the moodle hooks for the assign module.
      19   *
      20   * It delegates most functions to the assignment class.
      21   *
      22   * @package   mod_assign
      23   * @copyright 2012 NetSpot {@link http://www.netspot.com.au}
      24   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
      25   */
      26  defined('MOODLE_INTERNAL') || die();
      27  
      28  /**
      29   * Adds an assignment instance
      30   *
      31   * This is done by calling the add_instance() method of the assignment type class
      32   * @param stdClass $data
      33   * @param mod_assign_mod_form $form
      34   * @return int The instance id of the new assignment
      35   */
      36  function assign_add_instance(stdClass $data, mod_assign_mod_form $form = null) {
      37      global $CFG;
      38      require_once($CFG->dirroot . '/mod/assign/locallib.php');
      39  
      40      $assignment = new assign(context_module::instance($data->coursemodule), null, null);
      41      return $assignment->add_instance($data, true);
      42  }
      43  
      44  /**
      45   * delete an assignment instance
      46   * @param int $id
      47   * @return bool
      48   */
      49  function assign_delete_instance($id) {
      50      global $CFG;
      51      require_once($CFG->dirroot . '/mod/assign/locallib.php');
      52      $cm = get_coursemodule_from_instance('assign', $id, 0, false, MUST_EXIST);
      53      $context = context_module::instance($cm->id);
      54  
      55      $assignment = new assign($context, null, null);
      56      return $assignment->delete_instance();
      57  }
      58  
      59  /**
      60   * This function is used by the reset_course_userdata function in moodlelib.
      61   * This function will remove all assignment submissions and feedbacks in the database
      62   * and clean up any related data.
      63   *
      64   * @param stdClass $data the data submitted from the reset course.
      65   * @return array
      66   */
      67  function assign_reset_userdata($data) {
      68      global $CFG, $DB;
      69      require_once($CFG->dirroot . '/mod/assign/locallib.php');
      70  
      71      $status = array();
      72      $params = array('courseid'=>$data->courseid);
      73      $sql = "SELECT a.id FROM {assign} a WHERE a.course=:courseid";
      74      $course = $DB->get_record('course', array('id'=>$data->courseid), '*', MUST_EXIST);
      75      if ($assigns = $DB->get_records_sql($sql, $params)) {
      76          foreach ($assigns as $assign) {
      77              $cm = get_coursemodule_from_instance('assign',
      78                                                   $assign->id,
      79                                                   $data->courseid,
      80                                                   false,
      81                                                   MUST_EXIST);
      82              $context = context_module::instance($cm->id);
      83              $assignment = new assign($context, $cm, $course);
      84              $status = array_merge($status, $assignment->reset_userdata($data));
      85          }
      86      }
      87      return $status;
      88  }
      89  
      90  /**
      91   * This standard function will check all instances of this module
      92   * and make sure there are up-to-date events created for each of them.
      93   * If courseid = 0, then every assignment event in the site is checked, else
      94   * only assignment events belonging to the course specified are checked.
      95   *
      96   * @param int $courseid
      97   * @return bool
      98   */
      99  function assign_refresh_events($courseid = 0) {
     100      global $CFG, $DB;
     101      require_once($CFG->dirroot . '/mod/assign/locallib.php');
     102  
     103      if ($courseid) {
     104          // Make sure that the course id is numeric.
     105          if (!is_numeric($courseid)) {
     106              return false;
     107          }
     108          if (!$assigns = $DB->get_records('assign', array('course' => $courseid))) {
     109              return false;
     110          }
     111          // Get course from courseid parameter.
     112          if (!$course = $DB->get_record('course', array('id' => $courseid), '*')) {
     113              return false;
     114          }
     115      } else {
     116          if (!$assigns = $DB->get_records('assign')) {
     117              return false;
     118          }
     119      }
     120      foreach ($assigns as $assign) {
     121          // Use assignment's course column if courseid parameter is not given.
     122          if (!$courseid) {
     123              $courseid = $assign->course;
     124              if (!$course = $DB->get_record('course', array('id' => $courseid), '*')) {
     125                  continue;
     126              }
     127          }
     128          if (!$cm = get_coursemodule_from_instance('assign', $assign->id, $courseid, false)) {
     129              continue;
     130          }
     131          $context = context_module::instance($cm->id);
     132          $assignment = new assign($context, $cm, $course);
     133          $assignment->update_calendar($cm->id);
     134      }
     135  
     136      return true;
     137  }
     138  
     139  /**
     140   * Removes all grades from gradebook
     141   *
     142   * @param int $courseid The ID of the course to reset
     143   * @param string $type Optional type of assignment to limit the reset to a particular assignment type
     144   */
     145  function assign_reset_gradebook($courseid, $type='') {
     146      global $CFG, $DB;
     147  
     148      $params = array('moduletype'=>'assign', 'courseid'=>$courseid);
     149      $sql = 'SELECT a.*, cm.idnumber as cmidnumber, a.course as courseid
     150              FROM {assign} a, {course_modules} cm, {modules} m
     151              WHERE m.name=:moduletype AND m.id=cm.module AND cm.instance=a.id AND a.course=:courseid';
     152  
     153      if ($assignments = $DB->get_records_sql($sql, $params)) {
     154          foreach ($assignments as $assignment) {
     155              assign_grade_item_update($assignment, 'reset');
     156          }
     157      }
     158  }
     159  
     160  /**
     161   * Implementation of the function for printing the form elements that control
     162   * whether the course reset functionality affects the assignment.
     163   * @param moodleform $mform form passed by reference
     164   */
     165  function assign_reset_course_form_definition(&$mform) {
     166      $mform->addElement('header', 'assignheader', get_string('modulenameplural', 'assign'));
     167      $name = get_string('deleteallsubmissions', 'assign');
     168      $mform->addElement('advcheckbox', 'reset_assign_submissions', $name);
     169  }
     170  
     171  /**
     172   * Course reset form defaults.
     173   * @param  object $course
     174   * @return array
     175   */
     176  function assign_reset_course_form_defaults($course) {
     177      return array('reset_assign_submissions'=>1);
     178  }
     179  
     180  /**
     181   * Update an assignment instance
     182   *
     183   * This is done by calling the update_instance() method of the assignment type class
     184   * @param stdClass $data
     185   * @param stdClass $form - unused
     186   * @return object
     187   */
     188  function assign_update_instance(stdClass $data, $form) {
     189      global $CFG;
     190      require_once($CFG->dirroot . '/mod/assign/locallib.php');
     191      $context = context_module::instance($data->coursemodule);
     192      $assignment = new assign($context, null, null);
     193      return $assignment->update_instance($data);
     194  }
     195  
     196  /**
     197   * Return the list if Moodle features this module supports
     198   *
     199   * @param string $feature FEATURE_xx constant for requested feature
     200   * @return mixed True if module supports feature, null if doesn't know
     201   */
     202  function assign_supports($feature) {
     203      switch($feature) {
     204          case FEATURE_GROUPS:
     205              return true;
     206          case FEATURE_GROUPINGS:
     207              return true;
     208          case FEATURE_MOD_INTRO:
     209              return true;
     210          case FEATURE_COMPLETION_TRACKS_VIEWS:
     211              return true;
     212          case FEATURE_COMPLETION_HAS_RULES:
     213              return true;
     214          case FEATURE_GRADE_HAS_GRADE:
     215              return true;
     216          case FEATURE_GRADE_OUTCOMES:
     217              return true;
     218          case FEATURE_BACKUP_MOODLE2:
     219              return true;
     220          case FEATURE_SHOW_DESCRIPTION:
     221              return true;
     222          case FEATURE_ADVANCED_GRADING:
     223              return true;
     224          case FEATURE_PLAGIARISM:
     225              return true;
     226  
     227          default:
     228              return null;
     229      }
     230  }
     231  
     232  /**
     233   * Lists all gradable areas for the advanced grading methods gramework
     234   *
     235   * @return array('string'=>'string') An array with area names as keys and descriptions as values
     236   */
     237  function assign_grading_areas_list() {
     238      return array('submissions'=>get_string('submissions', 'assign'));
     239  }
     240  
     241  
     242  /**
     243   * extend an assigment navigation settings
     244   *
     245   * @param settings_navigation $settings
     246   * @param navigation_node $navref
     247   * @return void
     248   */
     249  function assign_extend_settings_navigation(settings_navigation $settings, navigation_node $navref) {
     250      global $PAGE, $DB;
     251  
     252      $cm = $PAGE->cm;
     253      if (!$cm) {
     254          return;
     255      }
     256  
     257      $context = $cm->context;
     258      $course = $PAGE->course;
     259  
     260      if (!$course) {
     261          return;
     262      }
     263  
     264      // Link to gradebook.
     265      if (has_capability('gradereport/grader:view', $cm->context) &&
     266              has_capability('moodle/grade:viewall', $cm->context)) {
     267          $link = new moodle_url('/grade/report/grader/index.php', array('id' => $course->id));
     268          $linkname = get_string('viewgradebook', 'assign');
     269          $node = $navref->add($linkname, $link, navigation_node::TYPE_SETTING);
     270      }
     271  
     272      // Link to download all submissions.
     273      if (has_any_capability(array('mod/assign:grade', 'mod/assign:viewgrades'), $context)) {
     274          $link = new moodle_url('/mod/assign/view.php', array('id' => $cm->id, 'action'=>'grading'));
     275          $node = $navref->add(get_string('viewgrading', 'assign'), $link, navigation_node::TYPE_SETTING);
     276  
     277          $link = new moodle_url('/mod/assign/view.php', array('id' => $cm->id, 'action'=>'downloadall'));
     278          $node = $navref->add(get_string('downloadall', 'assign'), $link, navigation_node::TYPE_SETTING);
     279      }
     280  
     281      if (has_capability('mod/assign:revealidentities', $context)) {
     282          $dbparams = array('id'=>$cm->instance);
     283          $assignment = $DB->get_record('assign', $dbparams, 'blindmarking, revealidentities');
     284  
     285          if ($assignment && $assignment->blindmarking && !$assignment->revealidentities) {
     286              $urlparams = array('id' => $cm->id, 'action'=>'revealidentities');
     287              $url = new moodle_url('/mod/assign/view.php', $urlparams);
     288              $linkname = get_string('revealidentities', 'assign');
     289              $node = $navref->add($linkname, $url, navigation_node::TYPE_SETTING);
     290          }
     291      }
     292  }
     293  
     294  /**
     295   * Add a get_coursemodule_info function in case any assignment type wants to add 'extra' information
     296   * for the course (see resource).
     297   *
     298   * Given a course_module object, this function returns any "extra" information that may be needed
     299   * when printing this activity in a course listing.  See get_array_of_activities() in course/lib.php.
     300   *
     301   * @param stdClass $coursemodule The coursemodule object (record).
     302   * @return cached_cm_info An object on information that the courses
     303   *                        will know about (most noticeably, an icon).
     304   */
     305  function assign_get_coursemodule_info($coursemodule) {
     306      global $CFG, $DB;
     307  
     308      $dbparams = array('id'=>$coursemodule->instance);
     309      $fields = 'id, name, alwaysshowdescription, allowsubmissionsfromdate, intro, introformat';
     310      if (! $assignment = $DB->get_record('assign', $dbparams, $fields)) {
     311          return false;
     312      }
     313  
     314      $result = new cached_cm_info();
     315      $result->name = $assignment->name;
     316      if ($coursemodule->showdescription) {
     317          if ($assignment->alwaysshowdescription || time() > $assignment->allowsubmissionsfromdate) {
     318              // Convert intro to html. Do not filter cached version, filters run at display time.
     319              $result->content = format_module_intro('assign', $assignment, $coursemodule->id, false);
     320          }
     321      }
     322      return $result;
     323  }
     324  
     325  /**
     326   * Return a list of page types
     327   * @param string $pagetype current page type
     328   * @param stdClass $parentcontext Block's parent context
     329   * @param stdClass $currentcontext Current context of block
     330   */
     331  function assign_page_type_list($pagetype, $parentcontext, $currentcontext) {
     332      $modulepagetype = array(
     333          'mod-assign-*' => get_string('page-mod-assign-x', 'assign'),
     334          'mod-assign-view' => get_string('page-mod-assign-view', 'assign'),
     335      );
     336      return $modulepagetype;
     337  }
     338  
     339  /**
     340   * Print an overview of all assignments
     341   * for the courses.
     342   *
     343   * @param mixed $courses The list of courses to print the overview for
     344   * @param array $htmlarray The array of html to return
     345   */
     346  function assign_print_overview($courses, &$htmlarray) {
     347      global $USER, $CFG, $DB;
     348  
     349      if (empty($courses) || !is_array($courses) || count($courses) == 0) {
     350          return array();
     351      }
     352  
     353      if (!$assignments = get_all_instances_in_courses('assign', $courses)) {
     354          return;
     355      }
     356  
     357      $assignmentids = array();
     358  
     359      // Do assignment_base::isopen() here without loading the whole thing for speed.
     360      foreach ($assignments as $key => $assignment) {
     361          $time = time();
     362          $isopen = false;
     363          if ($assignment->duedate) {
     364              $duedate = false;
     365              if ($assignment->cutoffdate) {
     366                  $duedate = $assignment->cutoffdate;
     367              }
     368              if ($duedate) {
     369                  $isopen = ($assignment->allowsubmissionsfromdate <= $time && $time <= $duedate);
     370              } else {
     371                  $isopen = ($assignment->allowsubmissionsfromdate <= $time);
     372              }
     373          }
     374          if ($isopen) {
     375              $assignmentids[] = $assignment->id;
     376          }
     377      }
     378  
     379      if (empty($assignmentids)) {
     380          // No assignments to look at - we're done.
     381          return true;
     382      }
     383  
     384      // Definitely something to print, now include the constants we need.
     385      require_once($CFG->dirroot . '/mod/assign/locallib.php');
     386  
     387      $strduedate = get_string('duedate', 'assign');
     388      $strcutoffdate = get_string('nosubmissionsacceptedafter', 'assign');
     389      $strnolatesubmissions = get_string('nolatesubmissions', 'assign');
     390      $strduedateno = get_string('duedateno', 'assign');
     391      $strduedateno = get_string('duedateno', 'assign');
     392      $strgraded = get_string('graded', 'assign');
     393      $strnotgradedyet = get_string('notgradedyet', 'assign');
     394      $strnotsubmittedyet = get_string('notsubmittedyet', 'assign');
     395      $strsubmitted = get_string('submitted', 'assign');
     396      $strassignment = get_string('modulename', 'assign');
     397      $strreviewed = get_string('reviewed', 'assign');
     398  
     399      // We do all possible database work here *outside* of the loop to ensure this scales.
     400      list($sqlassignmentids, $assignmentidparams) = $DB->get_in_or_equal($assignmentids);
     401  
     402      $mysubmissions = null;
     403      $unmarkedsubmissions = null;
     404  
     405      foreach ($assignments as $assignment) {
     406          // Do not show assignments that are not open.
     407          if (!in_array($assignment->id, $assignmentids)) {
     408              continue;
     409          }
     410          $dimmedclass = '';
     411          if (!$assignment->visible) {
     412              $dimmedclass = ' class="dimmed"';
     413          }
     414          $href = $CFG->wwwroot . '/mod/assign/view.php?id=' . $assignment->coursemodule;
     415          $str = '<div class="assign overview">' .
     416                 '<div class="name">' .
     417                 $strassignment . ': '.
     418                 '<a ' . $dimmedclass .
     419                     'title="' . $strassignment . '" ' .
     420                     'href="' . $href . '">' .
     421                 format_string($assignment->name) .
     422                 '</a></div>';
     423          if ($assignment->duedate) {
     424              $userdate = userdate($assignment->duedate);
     425              $str .= '<div class="info">' . $strduedate . ': ' . $userdate . '</div>';
     426          } else {
     427              $str .= '<div class="info">' . $strduedateno . '</div>';
     428          }
     429          if ($assignment->cutoffdate) {
     430              if ($assignment->cutoffdate == $assignment->duedate) {
     431                  $str .= '<div class="info">' . $strnolatesubmissions . '</div>';
     432              } else {
     433                  $userdate = userdate($assignment->cutoffdate);
     434                  $str .= '<div class="info">' . $strcutoffdate . ': ' . $userdate . '</div>';
     435              }
     436          }
     437          $context = context_module::instance($assignment->coursemodule);
     438          if (has_capability('mod/assign:grade', $context)) {
     439              if (!isset($unmarkedsubmissions)) {
     440                  // Build up and array of unmarked submissions indexed by assignment id/ userid
     441                  // for use where the user has grading rights on assignment.
     442                  $dbparams = array_merge(array(ASSIGN_SUBMISSION_STATUS_SUBMITTED), $assignmentidparams);
     443                  $rs = $DB->get_recordset_sql('SELECT
     444                                                    s.assignment as assignment,
     445                                                    s.userid as userid,
     446                                                    s.id as id,
     447                                                    s.status as status,
     448                                                    g.timemodified as timegraded
     449                                                FROM {assign_submission} s
     450                                                LEFT JOIN {assign_grades} g ON
     451                                                    s.userid = g.userid AND
     452                                                    s.assignment = g.assignment AND
     453                                                    g.attemptnumber = s.attemptnumber
     454                                                WHERE
     455                                                    ( g.timemodified is NULL OR
     456                                                    s.timemodified > g.timemodified OR
     457                                                    g.grade IS NULL ) AND
     458                                                    s.timemodified IS NOT NULL AND
     459                                                    s.status = ? AND
     460                                                    s.latest = 1 AND
     461                                                    s.assignment ' . $sqlassignmentids, $dbparams);
     462  
     463                  $unmarkedsubmissions = array();
     464                  foreach ($rs as $rd) {
     465                      $unmarkedsubmissions[$rd->assignment][$rd->userid] = $rd->id;
     466                  }
     467                  $rs->close();
     468              }
     469  
     470              // Count how many people can submit.
     471              $submissions = 0;
     472              if ($students = get_enrolled_users($context, 'mod/assign:view', 0, 'u.id')) {
     473                  foreach ($students as $student) {
     474                      if (isset($unmarkedsubmissions[$assignment->id][$student->id])) {
     475                          $submissions++;
     476                      }
     477                  }
     478              }
     479  
     480              if ($submissions) {
     481                  $urlparams = array('id'=>$assignment->coursemodule, 'action'=>'grading');
     482                  $url = new moodle_url('/mod/assign/view.php', $urlparams);
     483                  $str .= '<div class="details">' .
     484                          '<a href="' . $url . '">' .
     485                          get_string('submissionsnotgraded', 'assign', $submissions) .
     486                          '</a></div>';
     487              }
     488          }
     489          if (has_capability('mod/assign:submit', $context)) {
     490              if (!isset($mysubmissions)) {
     491  
     492                  // Get all user submissions, indexed by assignment id.
     493                  $dbparams = array_merge(array($USER->id), $assignmentidparams, array($USER->id));
     494                  $mysubmissions = $DB->get_records_sql('SELECT
     495                                                             a.id AS assignment,
     496                                                             a.nosubmissions AS nosubmissions,
     497                                                             g.timemodified AS timemarked,
     498                                                             g.grader AS grader,
     499                                                             g.grade AS grade,
     500                                                             s.status AS status
     501                                                         FROM {assign} a, {assign_submission} s
     502                                                         LEFT JOIN {assign_grades} g ON
     503                                                             g.assignment = s.assignment AND
     504                                                             g.userid = ? AND
     505                                                             g.attemptnumber = s.attemptnumber
     506                                                         WHERE a.id ' . $sqlassignmentids . ' AND
     507                                                             s.latest = 1 AND
     508                                                             s.assignment = a.id AND
     509                                                             s.userid = ?', $dbparams);
     510              }
     511  
     512              $str .= '<div class="details">';
     513              $str .= get_string('mysubmission', 'assign');
     514              $submission = false;
     515              if (isset($mysubmissions[$assignment->id])) {
     516                  $submission = $mysubmissions[$assignment->id];
     517              }
     518              if ($submission && $submission->nosubmissions) {
     519                  $str .= get_string('offline', 'assign');
     520              } else if (!$submission ||
     521                      !$submission->status ||
     522                      $submission->status == 'draft' ||
     523                      $submission->status == 'new') {
     524                  $str .= $strnotsubmittedyet;
     525              } else {
     526                  $str .= get_string('submissionstatus_' . $submission->status, 'assign');
     527              }
     528  
     529              if ($assignment->markingworkflow) {
     530                  $workflowstate = $DB->get_field('assign_user_flags', 'workflowstate', array('assignment' =>
     531                      $assignment->id, 'userid' => $USER->id));
     532                  if ($workflowstate) {
     533                      $gradingstatus = 'markingworkflowstate' . $workflowstate;
     534                  } else {
     535                      $gradingstatus = 'markingworkflowstate' . ASSIGN_MARKING_WORKFLOW_STATE_NOTMARKED;
     536                  }
     537              } else if (!empty($submission->grade) && $submission->grade !== null && $submission->grade >= 0) {
     538                  $gradingstatus = ASSIGN_GRADING_STATUS_GRADED;
     539              } else {
     540                  $gradingstatus = ASSIGN_GRADING_STATUS_NOT_GRADED;
     541              }
     542              $str .= ', ' . get_string($gradingstatus, 'assign');
     543              $str .= '</div>';
     544          }
     545          $str .= '</div>';
     546          if (empty($htmlarray[$assignment->course]['assign'])) {
     547              $htmlarray[$assignment->course]['assign'] = $str;
     548          } else {
     549              $htmlarray[$assignment->course]['assign'] .= $str;
     550          }
     551      }
     552  }
     553  
     554  /**
     555   * Print recent activity from all assignments in a given course
     556   *
     557   * This is used by the recent activity block
     558   * @param mixed $course the course to print activity for
     559   * @param bool $viewfullnames boolean to determine whether to show full names or not
     560   * @param int $timestart the time the rendering started
     561   * @return bool true if activity was printed, false otherwise.
     562   */
     563  function assign_print_recent_activity($course, $viewfullnames, $timestart) {
     564      global $CFG, $USER, $DB, $OUTPUT;
     565      require_once($CFG->dirroot . '/mod/assign/locallib.php');
     566  
     567      // Do not use log table if possible, it may be huge.
     568  
     569      $dbparams = array($timestart, $course->id, 'assign', ASSIGN_SUBMISSION_STATUS_SUBMITTED);
     570      $namefields = user_picture::fields('u', null, 'userid');
     571      if (!$submissions = $DB->get_records_sql("SELECT asb.id, asb.timemodified, cm.id AS cmid,
     572                                                       $namefields
     573                                                  FROM {assign_submission} asb
     574                                                       JOIN {assign} a      ON a.id = asb.assignment
     575                                                       JOIN {course_modules} cm ON cm.instance = a.id
     576                                                       JOIN {modules} md        ON md.id = cm.module
     577                                                       JOIN {user} u            ON u.id = asb.userid
     578                                                 WHERE asb.timemodified > ? AND
     579                                                       asb.latest = 1 AND
     580                                                       a.course = ? AND
     581                                                       md.name = ? AND
     582                                                       asb.status = ?
     583                                              ORDER BY asb.timemodified ASC", $dbparams)) {
     584           return false;
     585      }
     586  
     587      $modinfo = get_fast_modinfo($course);
     588      $show    = array();
     589      $grader  = array();
     590  
     591      $showrecentsubmissions = get_config('assign', 'showrecentsubmissions');
     592  
     593      foreach ($submissions as $submission) {
     594          if (!array_key_exists($submission->cmid, $modinfo->get_cms())) {
     595              continue;
     596          }
     597          $cm = $modinfo->get_cm($submission->cmid);
     598          if (!$cm->uservisible) {
     599              continue;
     600          }
     601          if ($submission->userid == $USER->id) {
     602              $show[] = $submission;
     603              continue;
     604          }
     605  
     606          $context = context_module::instance($submission->cmid);
     607          // The act of submitting of assignment may be considered private -
     608          // only graders will see it if specified.
     609          if (empty($showrecentsubmissions)) {
     610              if (!array_key_exists($cm->id, $grader)) {
     611                  $grader[$cm->id] = has_capability('moodle/grade:viewall', $context);
     612              }
     613              if (!$grader[$cm->id]) {
     614                  continue;
     615              }
     616          }
     617  
     618          $groupmode = groups_get_activity_groupmode($cm, $course);
     619  
     620          if ($groupmode == SEPARATEGROUPS &&
     621                  !has_capability('moodle/site:accessallgroups',  $context)) {
     622              if (isguestuser()) {
     623                  // Shortcut - guest user does not belong into any group.
     624                  continue;
     625              }
     626  
     627              // This will be slow - show only users that share group with me in this cm.
     628              if (!$modinfo->get_groups($cm->groupingid)) {
     629                  continue;
     630              }
     631              $usersgroups =  groups_get_all_groups($course->id, $submission->userid, $cm->groupingid);
     632              if (is_array($usersgroups)) {
     633                  $usersgroups = array_keys($usersgroups);
     634                  $intersect = array_intersect($usersgroups, $modinfo->get_groups($cm->groupingid));
     635                  if (empty($intersect)) {
     636                      continue;
     637                  }
     638              }
     639          }
     640          $show[] = $submission;
     641      }
     642  
     643      if (empty($show)) {
     644          return false;
     645      }
     646  
     647      echo $OUTPUT->heading(get_string('newsubmissions', 'assign').':', 3);
     648  
     649      foreach ($show as $submission) {
     650          $cm = $modinfo->get_cm($submission->cmid);
     651          $context = context_module::instance($submission->cmid);
     652          $assign = new assign($context, $cm, $cm->course);
     653          $link = $CFG->wwwroot.'/mod/assign/view.php?id='.$cm->id;
     654          // Obscure first and last name if blind marking enabled.
     655          if ($assign->is_blind_marking()) {
     656              $submission->firstname = get_string('participant', 'mod_assign');
     657              $submission->lastname = $assign->get_uniqueid_for_user($submission->userid);
     658          }
     659          print_recent_activity_note($submission->timemodified,
     660                                     $submission,
     661                                     $cm->name,
     662                                     $link,
     663                                     false,
     664                                     $viewfullnames);
     665      }
     666  
     667      return true;
     668  }
     669  
     670  /**
     671   * Returns all assignments since a given time.
     672   *
     673   * @param array $activities The activity information is returned in this array
     674   * @param int $index The current index in the activities array
     675   * @param int $timestart The earliest activity to show
     676   * @param int $courseid Limit the search to this course
     677   * @param int $cmid The course module id
     678   * @param int $userid Optional user id
     679   * @param int $groupid Optional group id
     680   * @return void
     681   */
     682  function assign_get_recent_mod_activity(&$activities,
     683                                          &$index,
     684                                          $timestart,
     685                                          $courseid,
     686                                          $cmid,
     687                                          $userid=0,
     688                                          $groupid=0) {
     689      global $CFG, $COURSE, $USER, $DB;
     690  
     691      require_once($CFG->dirroot . '/mod/assign/locallib.php');
     692  
     693      if ($COURSE->id == $courseid) {
     694          $course = $COURSE;
     695      } else {
     696          $course = $DB->get_record('course', array('id'=>$courseid));
     697      }
     698  
     699      $modinfo = get_fast_modinfo($course);
     700  
     701      $cm = $modinfo->get_cm($cmid);
     702      $params = array();
     703      if ($userid) {
     704          $userselect = 'AND u.id = :userid';
     705          $params['userid'] = $userid;
     706      } else {
     707          $userselect = '';
     708      }
     709  
     710      if ($groupid) {
     711          $groupselect = 'AND gm.groupid = :groupid';
     712          $groupjoin   = 'JOIN {groups_members} gm ON  gm.userid=u.id';
     713          $params['groupid'] = $groupid;
     714      } else {
     715          $groupselect = '';
     716          $groupjoin   = '';
     717      }
     718  
     719      $params['cminstance'] = $cm->instance;
     720      $params['timestart'] = $timestart;
     721      $params['submitted'] = ASSIGN_SUBMISSION_STATUS_SUBMITTED;
     722  
     723      $userfields = user_picture::fields('u', null, 'userid');
     724  
     725      if (!$submissions = $DB->get_records_sql('SELECT asb.id, asb.timemodified, ' .
     726                                                       $userfields .
     727                                               '  FROM {assign_submission} asb
     728                                                  JOIN {assign} a ON a.id = asb.assignment
     729                                                  JOIN {user} u ON u.id = asb.userid ' .
     730                                            $groupjoin .
     731                                              '  WHERE asb.timemodified > :timestart AND
     732                                                       asb.status = :submitted AND
     733                                                       a.id = :cminstance
     734                                                       ' . $userselect . ' ' . $groupselect .
     735                                              ' ORDER BY asb.timemodified ASC', $params)) {
     736           return;
     737      }
     738  
     739      $groupmode       = groups_get_activity_groupmode($cm, $course);
     740      $cmcontext      = context_module::instance($cm->id);
     741      $grader          = has_capability('moodle/grade:viewall', $cmcontext);
     742      $accessallgroups = has_capability('moodle/site:accessallgroups', $cmcontext);
     743      $viewfullnames   = has_capability('moodle/site:viewfullnames', $cmcontext);
     744  
     745  
     746      $showrecentsubmissions = get_config('assign', 'showrecentsubmissions');
     747      $show = array();
     748      foreach ($submissions as $submission) {
     749          if ($submission->userid == $USER->id) {
     750              $show[] = $submission;
     751              continue;
     752          }
     753          // The act of submitting of assignment may be considered private -
     754          // only graders will see it if specified.
     755          if (empty($showrecentsubmissions)) {
     756              if (!$grader) {
     757                  continue;
     758              }
     759          }
     760  
     761          if ($groupmode == SEPARATEGROUPS and !$accessallgroups) {
     762              if (isguestuser()) {
     763                  // Shortcut - guest user does not belong into any group.
     764                  continue;
     765              }
     766  
     767              // This will be slow - show only users that share group with me in this cm.
     768              if (!$modinfo->get_groups($cm->groupingid)) {
     769                  continue;
     770              }
     771              $usersgroups =  groups_get_all_groups($course->id, $submission->userid, $cm->groupingid);
     772              if (is_array($usersgroups)) {
     773                  $usersgroups = array_keys($usersgroups);
     774                  $intersect = array_intersect($usersgroups, $modinfo->get_groups($cm->groupingid));
     775                  if (empty($intersect)) {
     776                      continue;
     777                  }
     778              }
     779          }
     780          $show[] = $submission;
     781      }
     782  
     783      if (empty($show)) {
     784          return;
     785      }
     786  
     787      if ($grader) {
     788          require_once($CFG->libdir.'/gradelib.php');
     789          $userids = array();
     790          foreach ($show as $id => $submission) {
     791              $userids[] = $submission->userid;
     792          }
     793          $grades = grade_get_grades($courseid, 'mod', 'assign', $cm->instance, $userids);
     794      }
     795  
     796      $aname = format_string($cm->name, true);
     797      foreach ($show as $submission) {
     798          $activity = new stdClass();
     799  
     800          $activity->type         = 'assign';
     801          $activity->cmid         = $cm->id;
     802          $activity->name         = $aname;
     803          $activity->sectionnum   = $cm->sectionnum;
     804          $activity->timestamp    = $submission->timemodified;
     805          $activity->user         = new stdClass();
     806          if ($grader) {
     807              $activity->grade = $grades->items[0]->grades[$submission->userid]->str_long_grade;
     808          }
     809  
     810          $userfields = explode(',', user_picture::fields());
     811          foreach ($userfields as $userfield) {
     812              if ($userfield == 'id') {
     813                  // Aliased in SQL above.
     814                  $activity->user->{$userfield} = $submission->userid;
     815              } else {
     816                  $activity->user->{$userfield} = $submission->{$userfield};
     817              }
     818          }
     819          $activity->user->fullname = fullname($submission, $viewfullnames);
     820  
     821          $activities[$index++] = $activity;
     822      }
     823  
     824      return;
     825  }
     826  
     827  /**
     828   * Print recent activity from all assignments in a given course
     829   *
     830   * This is used by course/recent.php
     831   * @param stdClass $activity
     832   * @param int $courseid
     833   * @param bool $detail
     834   * @param array $modnames
     835   */
     836  function assign_print_recent_mod_activity($activity, $courseid, $detail, $modnames) {
     837      global $CFG, $OUTPUT;
     838  
     839      echo '<table border="0" cellpadding="3" cellspacing="0" class="assignment-recent">';
     840  
     841      echo '<tr><td class="userpicture" valign="top">';
     842      echo $OUTPUT->user_picture($activity->user);
     843      echo '</td><td>';
     844  
     845      if ($detail) {
     846          $modname = $modnames[$activity->type];
     847          echo '<div class="title">';
     848          echo '<img src="' . $OUTPUT->pix_url('icon', 'assign') . '" '.
     849               'class="icon" alt="' . $modname . '">';
     850          echo '<a href="' . $CFG->wwwroot . '/mod/assign/view.php?id=' . $activity->cmid . '">';
     851          echo $activity->name;
     852          echo '</a>';
     853          echo '</div>';
     854      }
     855  
     856      if (isset($activity->grade)) {
     857          echo '<div class="grade">';
     858          echo get_string('grade').': ';
     859          echo $activity->grade;
     860          echo '</div>';
     861      }
     862  
     863      echo '<div class="user">';
     864      echo "<a href=\"$CFG->wwwroot/user/view.php?id={$activity->user->id}&amp;course=$courseid\">";
     865      echo "{$activity->user->fullname}</a>  - " . userdate($activity->timestamp);
     866      echo '</div>';
     867  
     868      echo '</td></tr></table>';
     869  }
     870  
     871  /**
     872   * Checks if a scale is being used by an assignment.
     873   *
     874   * This is used by the backup code to decide whether to back up a scale
     875   * @param int $assignmentid
     876   * @param int $scaleid
     877   * @return boolean True if the scale is used by the assignment
     878   */
     879  function assign_scale_used($assignmentid, $scaleid) {
     880      global $DB;
     881  
     882      $return = false;
     883      $rec = $DB->get_record('assign', array('id'=>$assignmentid, 'grade'=>-$scaleid));
     884  
     885      if (!empty($rec) && !empty($scaleid)) {
     886          $return = true;
     887      }
     888  
     889      return $return;
     890  }
     891  
     892  /**
     893   * Checks if scale is being used by any instance of assignment
     894   *
     895   * This is used to find out if scale used anywhere
     896   * @param int $scaleid
     897   * @return boolean True if the scale is used by any assignment
     898   */
     899  function assign_scale_used_anywhere($scaleid) {
     900      global $DB;
     901  
     902      if ($scaleid and $DB->record_exists('assign', array('grade'=>-$scaleid))) {
     903          return true;
     904      } else {
     905          return false;
     906      }
     907  }
     908  
     909  /**
     910   * List the actions that correspond to a view of this module.
     911   * This is used by the participation report.
     912   *
     913   * Note: This is not used by new logging system. Event with
     914   *       crud = 'r' and edulevel = LEVEL_PARTICIPATING will
     915   *       be considered as view action.
     916   *
     917   * @return array
     918   */
     919  function assign_get_view_actions() {
     920      return array('view submission', 'view feedback');
     921  }
     922  
     923  /**
     924   * List the actions that correspond to a post of this module.
     925   * This is used by the participation report.
     926   *
     927   * Note: This is not used by new logging system. Event with
     928   *       crud = ('c' || 'u' || 'd') and edulevel = LEVEL_PARTICIPATING
     929   *       will be considered as post action.
     930   *
     931   * @return array
     932   */
     933  function assign_get_post_actions() {
     934      return array('upload', 'submit', 'submit for grading');
     935  }
     936  
     937  /**
     938   * Call cron on the assign module.
     939   */
     940  function assign_cron() {
     941      global $CFG;
     942  
     943      require_once($CFG->dirroot . '/mod/assign/locallib.php');
     944      assign::cron();
     945  
     946      $plugins = core_component::get_plugin_list('assignsubmission');
     947  
     948      foreach ($plugins as $name => $plugin) {
     949          $disabled = get_config('assignsubmission_' . $name, 'disabled');
     950          if (!$disabled) {
     951              $class = 'assign_submission_' . $name;
     952              require_once($CFG->dirroot . '/mod/assign/submission/' . $name . '/locallib.php');
     953              $class::cron();
     954          }
     955      }
     956      $plugins = core_component::get_plugin_list('assignfeedback');
     957  
     958      foreach ($plugins as $name => $plugin) {
     959          $disabled = get_config('assignfeedback_' . $name, 'disabled');
     960          if (!$disabled) {
     961              $class = 'assign_feedback_' . $name;
     962              require_once($CFG->dirroot . '/mod/assign/feedback/' . $name . '/locallib.php');
     963              $class::cron();
     964          }
     965      }
     966  
     967      return true;
     968  }
     969  
     970  /**
     971   * Returns all other capabilities used by this module.
     972   * @return array Array of capability strings
     973   */
     974  function assign_get_extra_capabilities() {
     975      return array('gradereport/grader:view',
     976                   'moodle/grade:viewall',
     977                   'moodle/site:viewfullnames',
     978                   'moodle/site:config');
     979  }
     980  
     981  /**
     982   * Create grade item for given assignment.
     983   *
     984   * @param stdClass $assign record with extra cmidnumber
     985   * @param array $grades optional array/object of grade(s); 'reset' means reset grades in gradebook
     986   * @return int 0 if ok, error code otherwise
     987   */
     988  function assign_grade_item_update($assign, $grades=null) {
     989      global $CFG;
     990      require_once($CFG->libdir.'/gradelib.php');
     991  
     992      if (!isset($assign->courseid)) {
     993          $assign->courseid = $assign->course;
     994      }
     995  
     996      $params = array('itemname'=>$assign->name, 'idnumber'=>$assign->cmidnumber);
     997  
     998      // Check if feedback plugin for gradebook is enabled, if yes then
     999      // gradetype = GRADE_TYPE_TEXT else GRADE_TYPE_NONE.
    1000      $gradefeedbackenabled = false;
    1001  
    1002      if (isset($assign->gradefeedbackenabled)) {
    1003          $gradefeedbackenabled = $assign->gradefeedbackenabled;
    1004      } else if ($assign->grade == 0) { // Grade feedback is needed only when grade == 0.
    1005          require_once($CFG->dirroot . '/mod/assign/locallib.php');
    1006          $mod = get_coursemodule_from_instance('assign', $assign->id, $assign->courseid);
    1007          $cm = context_module::instance($mod->id);
    1008          $assignment = new assign($cm, null, null);
    1009          $gradefeedbackenabled = $assignment->is_gradebook_feedback_enabled();
    1010      }
    1011  
    1012      if ($assign->grade > 0) {
    1013          $params['gradetype'] = GRADE_TYPE_VALUE;
    1014          $params['grademax']  = $assign->grade;
    1015          $params['grademin']  = 0;
    1016  
    1017      } else if ($assign->grade < 0) {
    1018          $params['gradetype'] = GRADE_TYPE_SCALE;
    1019          $params['scaleid']   = -$assign->grade;
    1020  
    1021      } else if ($gradefeedbackenabled) {
    1022          // $assign->grade == 0 and feedback enabled.
    1023          $params['gradetype'] = GRADE_TYPE_TEXT;
    1024      } else {
    1025          // $assign->grade == 0 and no feedback enabled.
    1026          $params['gradetype'] = GRADE_TYPE_NONE;
    1027      }
    1028  
    1029      if ($grades  === 'reset') {
    1030          $params['reset'] = true;
    1031          $grades = null;
    1032      }
    1033  
    1034      return grade_update('mod/assign',
    1035                          $assign->courseid,
    1036                          'mod',
    1037                          'assign',
    1038                          $assign->id,
    1039                          0,
    1040                          $grades,
    1041                          $params);
    1042  }
    1043  
    1044  /**
    1045   * Return grade for given user or all users.
    1046   *
    1047   * @param stdClass $assign record of assign with an additional cmidnumber
    1048   * @param int $userid optional user id, 0 means all users
    1049   * @return array array of grades, false if none
    1050   */
    1051  function assign_get_user_grades($assign, $userid=0) {
    1052      global $CFG;
    1053  
    1054      require_once($CFG->dirroot . '/mod/assign/locallib.php');
    1055  
    1056      $cm = get_coursemodule_from_instance('assign', $assign->id, 0, false, MUST_EXIST);
    1057      $context = context_module::instance($cm->id);
    1058      $assignment = new assign($context, null, null);
    1059      $assignment->set_instance($assign);
    1060      return $assignment->get_user_grades_for_gradebook($userid);
    1061  }
    1062  
    1063  /**
    1064   * Update activity grades.
    1065   *
    1066   * @param stdClass $assign database record
    1067   * @param int $userid specific user only, 0 means all
    1068   * @param bool $nullifnone - not used
    1069   */
    1070  function assign_update_grades($assign, $userid=0, $nullifnone=true) {
    1071      global $CFG;
    1072      require_once($CFG->libdir.'/gradelib.php');
    1073  
    1074      if ($assign->grade == 0) {
    1075          assign_grade_item_update($assign);
    1076  
    1077      } else if ($grades = assign_get_user_grades($assign, $userid)) {
    1078          foreach ($grades as $k => $v) {
    1079              if ($v->rawgrade == -1) {
    1080                  $grades[$k]->rawgrade = null;
    1081              }
    1082          }
    1083          assign_grade_item_update($assign, $grades);
    1084  
    1085      } else {
    1086          assign_grade_item_update($assign);
    1087      }
    1088  }
    1089  
    1090  /**
    1091   * List the file areas that can be browsed.
    1092   *
    1093   * @param stdClass $course
    1094   * @param stdClass $cm
    1095   * @param stdClass $context
    1096   * @return array
    1097   */
    1098  function assign_get_file_areas($course, $cm, $context) {
    1099      global $CFG;
    1100      require_once($CFG->dirroot . '/mod/assign/locallib.php');
    1101  
    1102      $areas = array(ASSIGN_INTROATTACHMENT_FILEAREA => get_string('introattachments', 'mod_assign'));
    1103  
    1104      $assignment = new assign($context, $cm, $course);
    1105      foreach ($assignment->get_submission_plugins() as $plugin) {
    1106          if ($plugin->is_visible()) {
    1107              $pluginareas = $plugin->get_file_areas();
    1108  
    1109              if ($pluginareas) {
    1110                  $areas = array_merge($areas, $pluginareas);
    1111              }
    1112          }
    1113      }
    1114      foreach ($assignment->get_feedback_plugins() as $plugin) {
    1115          if ($plugin->is_visible()) {
    1116              $pluginareas = $plugin->get_file_areas();
    1117  
    1118              if ($pluginareas) {
    1119                  $areas = array_merge($areas, $pluginareas);
    1120              }
    1121          }
    1122      }
    1123  
    1124      return $areas;
    1125  }
    1126  
    1127  /**
    1128   * File browsing support for assign module.
    1129   *
    1130   * @param file_browser $browser
    1131   * @param object $areas
    1132   * @param object $course
    1133   * @param object $cm
    1134   * @param object $context
    1135   * @param string $filearea
    1136   * @param int $itemid
    1137   * @param string $filepath
    1138   * @param string $filename
    1139   * @return object file_info instance or null if not found
    1140   */
    1141  function assign_get_file_info($browser,
    1142                                $areas,
    1143                                $course,
    1144                                $cm,
    1145                                $context,
    1146                                $filearea,
    1147                                $itemid,
    1148                                $filepath,
    1149                                $filename) {
    1150      global $CFG;
    1151      require_once($CFG->dirroot . '/mod/assign/locallib.php');
    1152  
    1153      if ($context->contextlevel != CONTEXT_MODULE) {
    1154          return null;
    1155      }
    1156  
    1157      $urlbase = $CFG->wwwroot.'/pluginfile.php';
    1158      $fs = get_file_storage();
    1159      $filepath = is_null($filepath) ? '/' : $filepath;
    1160      $filename = is_null($filename) ? '.' : $filename;
    1161  
    1162      // Need to find where this belongs to.
    1163      $assignment = new assign($context, $cm, $course);
    1164      if ($filearea === ASSIGN_INTROATTACHMENT_FILEAREA) {
    1165          if (!has_capability('moodle/course:managefiles', $context)) {
    1166              // Students can not peak here!
    1167              return null;
    1168          }
    1169          if (!($storedfile = $fs->get_file($assignment->get_context()->id,
    1170                                            'mod_assign', $filearea, 0, $filepath, $filename))) {
    1171              return null;
    1172          }
    1173          return new file_info_stored($browser,
    1174                          $assignment->get_context(),
    1175                          $storedfile,
    1176                          $urlbase,
    1177                          $filearea,
    1178                          $itemid,
    1179                          true,
    1180                          true,
    1181                          false);
    1182      }
    1183  
    1184      $pluginowner = null;
    1185      foreach ($assignment->get_submission_plugins() as $plugin) {
    1186          if ($plugin->is_visible()) {
    1187              $pluginareas = $plugin->get_file_areas();
    1188  
    1189              if (array_key_exists($filearea, $pluginareas)) {
    1190                  $pluginowner = $plugin;
    1191                  break;
    1192              }
    1193          }
    1194      }
    1195      if (!$pluginowner) {
    1196          foreach ($assignment->get_feedback_plugins() as $plugin) {
    1197              if ($plugin->is_visible()) {
    1198                  $pluginareas = $plugin->get_file_areas();
    1199  
    1200                  if (array_key_exists($filearea, $pluginareas)) {
    1201                      $pluginowner = $plugin;
    1202                      break;
    1203                  }
    1204              }
    1205          }
    1206      }
    1207  
    1208      if (!$pluginowner) {
    1209          return null;
    1210      }
    1211  
    1212      $result = $pluginowner->get_file_info($browser, $filearea, $itemid, $filepath, $filename);
    1213      return $result;
    1214  }
    1215  
    1216  /**
    1217   * Prints the complete info about a user's interaction with an assignment.
    1218   *
    1219   * @param stdClass $course
    1220   * @param stdClass $user
    1221   * @param stdClass $coursemodule
    1222   * @param stdClass $assign the database assign record
    1223   *
    1224   * This prints the submission summary and feedback summary for this student.
    1225   */
    1226  function assign_user_complete($course, $user, $coursemodule, $assign) {
    1227      global $CFG;
    1228      require_once($CFG->dirroot . '/mod/assign/locallib.php');
    1229  
    1230      $context = context_module::instance($coursemodule->id);
    1231  
    1232      $assignment = new assign($context, $coursemodule, $course);
    1233  
    1234      echo $assignment->view_student_summary($user, false);
    1235  }
    1236  
    1237  /**
    1238   * Print the grade information for the assignment for this user.
    1239   *
    1240   * @param stdClass $course
    1241   * @param stdClass $user
    1242   * @param stdClass $coursemodule
    1243   * @param stdClass $assignment
    1244   */
    1245  function assign_user_outline($course, $user, $coursemodule, $assignment) {
    1246      global $CFG;
    1247      require_once($CFG->libdir.'/gradelib.php');
    1248      require_once($CFG->dirroot.'/grade/grading/lib.php');
    1249  
    1250      $gradinginfo = grade_get_grades($course->id,
    1251                                          'mod',
    1252                                          'assign',
    1253                                          $assignment->id,
    1254                                          $user->id);
    1255  
    1256      $gradingitem = $gradinginfo->items[0];
    1257      $gradebookgrade = $gradingitem->grades[$user->id];
    1258  
    1259      if (empty($gradebookgrade->str_long_grade)) {
    1260          return null;
    1261      }
    1262      $result = new stdClass();
    1263      $result->info = get_string('outlinegrade', 'assign', $gradebookgrade->str_long_grade);
    1264      $result->time = $gradebookgrade->dategraded;
    1265  
    1266      return $result;
    1267  }
    1268  
    1269  /**
    1270   * Obtains the automatic completion state for this module based on any conditions
    1271   * in assign settings.
    1272   *
    1273   * @param object $course Course
    1274   * @param object $cm Course-module
    1275   * @param int $userid User ID
    1276   * @param bool $type Type of comparison (or/and; can be used as return value if no conditions)
    1277   * @return bool True if completed, false if not, $type if conditions not set.
    1278   */
    1279  function assign_get_completion_state($course, $cm, $userid, $type) {
    1280      global $CFG, $DB;
    1281      require_once($CFG->dirroot . '/mod/assign/locallib.php');
    1282  
    1283      $assign = new assign(null, $cm, $course);
    1284  
    1285      // If completion option is enabled, evaluate it and return true/false.
    1286      if ($assign->get_instance()->completionsubmit) {
    1287          $submission = $assign->get_user_submission($userid, false);
    1288          return $submission && $submission->status == ASSIGN_SUBMISSION_STATUS_SUBMITTED;
    1289      } else {
    1290          // Completion option is not enabled so just return $type.
    1291          return $type;
    1292      }
    1293  }
    1294  
    1295  /**
    1296   * Serves intro attachment files.
    1297   *
    1298   * @param mixed $course course or id of the course
    1299   * @param mixed $cm course module or id of the course module
    1300   * @param context $context
    1301   * @param string $filearea
    1302   * @param array $args
    1303   * @param bool $forcedownload
    1304   * @param array $options additional options affecting the file serving
    1305   * @return bool false if file not found, does not return if found - just send the file
    1306   */
    1307  function assign_pluginfile($course,
    1308                  $cm,
    1309                  context $context,
    1310                  $filearea,
    1311                  $args,
    1312                  $forcedownload,
    1313                  array $options=array()) {
    1314      global $CFG;
    1315  
    1316      if ($context->contextlevel != CONTEXT_MODULE) {
    1317          return false;
    1318      }
    1319  
    1320      require_login($course, false, $cm);
    1321      if (!has_capability('mod/assign:view', $context)) {
    1322          return false;
    1323      }
    1324  
    1325      require_once($CFG->dirroot . '/mod/assign/locallib.php');
    1326      $assign = new assign($context, $cm, $course);
    1327  
    1328      if ($filearea !== ASSIGN_INTROATTACHMENT_FILEAREA) {
    1329          return false;
    1330      }
    1331      if (!$assign->show_intro()) {
    1332          return false;
    1333      }
    1334  
    1335      $itemid = (int)array_shift($args);
    1336      if ($itemid != 0) {
    1337          return false;
    1338      }
    1339  
    1340      $relativepath = implode('/', $args);
    1341  
    1342      $fullpath = "/{$context->id}/mod_assign/$filearea/$itemid/$relativepath";
    1343  
    1344      $fs = get_file_storage();
    1345      if (!$file = $fs->get_file_by_hash(sha1($fullpath)) or $file->is_directory()) {
    1346          return false;
    1347      }
    1348      send_stored_file($file, 0, 0, $forcedownload, $options);
    1349  }
    

    Search This Site: