original_string: public static function update_end_date($courseid) {
          global $DB, $COURSE;
  
          // Use one DB query to retrieve necessary fields in course, value for automaticenddate and number of the last
          // section. This query will also validate that the course is indeed in 'weeks' format.
          $insql = "SELECT c.id, c.format, c.startdate, c.enddate, MAX(s.section) AS lastsection
                      FROM {course} c
                      JOIN {course_sections} s
                        ON s.course = c.id
                     WHERE c.format = :format
                       AND c.id = :courseid
                  GROUP BY c.id, c.format, c.startdate, c.enddate";
          $sql = "SELECT co.id, co.format, co.startdate, co.enddate, co.lastsection, fo.value AS automaticenddate
                    FROM ($insql) co
               LEFT JOIN {course_format_options} fo
                      ON fo.courseid = co.id
                     AND fo.format = co.format
                     AND fo.name = :optionname
                     AND fo.sectionid = 0";
          $course = $DB->get_record_sql($sql,
              ['optionname' => 'automaticenddate', 'format' => 'weeks', 'courseid' => $courseid]);
  
          if (!$course) {
              // Looks like it is a course in a different format, nothing to do here.
              return;
          }
  
          // Create an instance of this class and mock the course object.
          $format = new format_weeks('weeks', $courseid);
          $format->course = $course;
  
          // If automaticenddate is not specified take the default value.
          if (!isset($course->automaticenddate)) {
              $defaults = $format->course_format_options();
              $course->automaticenddate = $defaults['automaticenddate']['default'];
          }
  
          // Check that the course format for setting an automatic date is set.
          if (!empty($course->automaticenddate)) {
              // Get the final week's last day.
              $dates = $format->get_section_dates((int)$course->lastsection);
  
              // Set the course end date.
              if ($course->enddate != $dates->end) {
                  $DB->set_field('course', 'enddate', $dates->end, array('id' => $course->id));
                  if (isset($COURSE->id) && $COURSE->id == $courseid) {
                      $COURSE->enddate = $dates->end;
                  }
              }
          }
      }
code: public static function update_end_date($courseid) {
          global $DB, $COURSE;
  
          // Use one DB query to retrieve necessary fields in course, value for automaticenddate and number of the last
          // section. This query will also validate that the course is indeed in 'weeks' format.
          $insql = "SELECT c.id, c.format, c.startdate, c.enddate, MAX(s.section) AS lastsection
                      FROM {course} c
                      JOIN {course_sections} s
                        ON s.course = c.id
                     WHERE c.format = :format
                       AND c.id = :courseid
                  GROUP BY c.id, c.format, c.startdate, c.enddate";
          $sql = "SELECT co.id, co.format, co.startdate, co.enddate, co.lastsection, fo.value AS automaticenddate
                    FROM ($insql) co
               LEFT JOIN {course_format_options} fo
                      ON fo.courseid = co.id
                     AND fo.format = co.format
                     AND fo.name = :optionname
                     AND fo.sectionid = 0";
          $course = $DB->get_record_sql($sql,
              ['optionname' => 'automaticenddate', 'format' => 'weeks', 'courseid' => $courseid]);
  
          if (!$course) {
              // Looks like it is a course in a different format, nothing to do here.
              return;
          }
  
          // Create an instance of this class and mock the course object.
          $format = new format_weeks('weeks', $courseid);
          $format->course = $course;
  
          // If automaticenddate is not specified take the default value.
          if (!isset($course->automaticenddate)) {
              $defaults = $format->course_format_options();
              $course->automaticenddate = $defaults['automaticenddate']['default'];
          }
  
          // Check that the course format for setting an automatic date is set.
          if (!empty($course->automaticenddate)) {
              // Get the final week's last day.
              $dates = $format->get_section_dates((int)$course->lastsection);
  
              // Set the course end date.
              if ($course->enddate != $dates->end) {
                  $DB->set_field('course', 'enddate', $dates->end, array('id' => $course->id));
                  if (isset($COURSE->id) && $COURSE->id == $courseid) {
                      $COURSE->enddate = $dates->end;
                  }
              }
          }
      }
code_tokens: public static function update_end_date ( $ courseid ) { global $ DB , $ COURSE ; // Use one DB query to retrieve necessary fields in course, value for automaticenddate and number of the last // section. This query will also validate that the course is indeed in 'weeks' format. $ insql = "SELECT c.id, c.format, c.startdate, c.enddate, MAX(s.section) AS lastsection
                      FROM {course} c
                      JOIN {course_sections} s
                        ON s.course = c.id
                     WHERE c.format = :format
                       AND c.id = :courseid
                  GROUP BY c.id, c.format, c.startdate, c.enddate" ; $ sql = "SELECT co.id, co.format, co.startdate, co.enddate, co.lastsection, fo.value AS automaticenddate
                    FROM ($insql) co
               LEFT JOIN {course_format_options} fo
                      ON fo.courseid = co.id
                     AND fo.format = co.format
                     AND fo.name = :optionname
                     AND fo.sectionid = 0" ; $ course = $ DB -> get_record_sql ( $ sql , [ 'optionname' => 'automaticenddate' , 'format' => 'weeks' , 'courseid' => $ courseid ] ) ; if ( ! $ course ) { // Looks like it is a course in a different format, nothing to do here. return ; } // Create an instance of this class and mock the course object. $ format = new format_weeks ( 'weeks' , $ courseid ) ; $ format -> course = $ course ; // If automaticenddate is not specified take the default value. if ( ! isset ( $ course -> automaticenddate ) ) { $ defaults = $ format -> course_format_options ( ) ; $ course -> automaticenddate = $ defaults [ 'automaticenddate' ] [ 'default' ] ; } // Check that the course format for setting an automatic date is set. if ( ! empty ( $ course -> automaticenddate ) ) { // Get the final week's last day. $ dates = $ format -> get_section_dates ( ( int ) $ course -> lastsection ) ; // Set the course end date. if ( $ course -> enddate != $ dates -> end ) { $ DB -> set_field ( 'course' , 'enddate' , $ dates -> end , array ( 'id' => $ course -> id ) ) ; if ( isset ( $ COURSE -> id ) && $ COURSE -> id == $ courseid ) { $ COURSE -> enddate = $ dates -> end ; } } } }
docstring: Updates the end date for a course in weeks format if option automaticenddate is set.
  
  This method is called from event observers and it can not use any modinfo or format caches because
  events are triggered before the caches are reset.
  
  @param int $courseid
docstring_tokens: Updates the end date for a course in weeks format if option automaticenddate is set .