Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 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   * Contains class containing completion API.
  19   *
  20   * @package    core_completion
  21   * @copyright  2017 Mark Nelson <markn@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_completion;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Class containing completion API.
  31   *
  32   * @package    core_completion
  33   * @copyright  2017 Mark Nelson <markn@moodle.com>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class api {
  37  
  38      /**
  39       * @var string The completion expected on event.
  40       */
  41      const COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED = 'expectcompletionon';
  42  
  43      /**
  44       * Creates, updates or deletes an event for the expected completion date.
  45       *
  46       * @param int $cmid The course module id
  47       * @param string $modulename The name of the module (eg. assign, quiz)
  48       * @param \stdClass|int $instanceorid The instance object or ID.
  49       * @param int|null $completionexpectedtime The time completion is expected, null if not set
  50       * @return bool
  51       */
  52      public static function update_completion_date_event($cmid, $modulename, $instanceorid, $completionexpectedtime) {
  53          global $CFG, $DB;
  54  
  55          // Required for calendar constant CALENDAR_EVENT_TYPE_ACTION.
  56          require_once($CFG->dirroot . '/calendar/lib.php');
  57  
  58          $instance = null;
  59          if (is_object($instanceorid)) {
  60              $instance = $instanceorid;
  61          } else {
  62              $instance = $DB->get_record($modulename, array('id' => $instanceorid), '*', IGNORE_MISSING);
  63          }
  64          if (!$instance) {
  65              return false;
  66          }
  67          $course = get_course($instance->course);
  68  
  69          $completion = new \completion_info($course);
  70  
  71          // Can not create/update an event if completion is disabled.
  72          if (!$completion->is_enabled() && $completionexpectedtime !== null) {
  73              return true;
  74          }
  75  
  76          // Create the \stdClass we will be using for our language strings.
  77          $lang = new \stdClass();
  78          $lang->modulename = get_string('pluginname', $modulename);
  79          $lang->instancename = $instance->name;
  80  
  81          // Create the calendar event.
  82          $event = new \stdClass();
  83          $event->type = CALENDAR_EVENT_TYPE_ACTION;
  84          $event->eventtype = self::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED;
  85          if ($event->id = $DB->get_field('event', 'id', array('modulename' => $modulename,
  86                  'instance' => $instance->id, 'eventtype' => $event->eventtype))) {
  87              if ($completionexpectedtime !== null) {
  88                  // Calendar event exists so update it.
  89                  $event->name = get_string('completionexpectedfor', 'completion', $lang);
  90                  $event->description = format_module_intro($modulename, $instance, $cmid, false);
  91                  $event->format = FORMAT_HTML;
  92                  $event->timestart = $completionexpectedtime;
  93                  $event->timesort = $completionexpectedtime;
  94                  $event->visible = instance_is_visible($modulename, $instance);
  95                  $event->timeduration = 0;
  96  
  97                  $calendarevent = \calendar_event::load($event->id);
  98                  $calendarevent->update($event, false);
  99              } else {
 100                  // Calendar event is no longer needed.
 101                  $calendarevent = \calendar_event::load($event->id);
 102                  $calendarevent->delete();
 103              }
 104          } else {
 105              // Event doesn't exist so create one.
 106              if ($completionexpectedtime !== null) {
 107                  $event->name = get_string('completionexpectedfor', 'completion', $lang);
 108                  $event->description = format_module_intro($modulename, $instance, $cmid, false);
 109                  $event->format = FORMAT_HTML;
 110                  $event->courseid = $instance->course;
 111                  $event->groupid = 0;
 112                  $event->userid = 0;
 113                  $event->modulename = $modulename;
 114                  $event->instance = $instance->id;
 115                  $event->timestart = $completionexpectedtime;
 116                  $event->timesort = $completionexpectedtime;
 117                  $event->visible = instance_is_visible($modulename, $instance);
 118                  $event->timeduration = 0;
 119  
 120                  \calendar_event::create($event, false);
 121              }
 122          }
 123  
 124          return true;
 125      }
 126  }