2009-09-25 05:24:13 +00:00
|
|
|
<?php
|
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Library of useful functions
|
|
|
|
*
|
|
|
|
* @copyright 1999 Martin Dougiamas http://dougiamas.com
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2013-09-30 09:08:07 +13:00
|
|
|
* @package core_course
|
2009-09-25 05:24:13 +00:00
|
|
|
*/
|
2001-11-22 06:23:56 +00:00
|
|
|
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
defined('MOODLE_INTERNAL') || die;
|
|
|
|
|
2021-06-21 11:59:53 +02:00
|
|
|
use core_courseformat\base as course_format;
|
|
|
|
|
2008-07-28 12:31:29 +00:00
|
|
|
require_once($CFG->libdir.'/completionlib.php');
|
2009-11-04 06:14:06 +00:00
|
|
|
require_once($CFG->libdir.'/filelib.php');
|
2018-11-09 13:09:54 +08:00
|
|
|
require_once($CFG->libdir.'/datalib.php');
|
2012-09-18 09:57:37 +08:00
|
|
|
require_once($CFG->dirroot.'/course/format/lib.php');
|
2001-11-22 06:23:56 +00:00
|
|
|
|
2013-09-30 09:08:07 +13:00
|
|
|
define('COURSE_MAX_LOGS_PER_PAGE', 1000); // Records.
|
|
|
|
define('COURSE_MAX_RECENT_PERIOD', 172800); // Two days, in seconds.
|
2012-10-04 17:07:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of courses to display when summaries are included.
|
|
|
|
* @var int
|
|
|
|
* @deprecated since 2.4, use $CFG->courseswithsummarieslimit instead.
|
|
|
|
*/
|
|
|
|
define('COURSE_MAX_SUMMARIES_PER_PAGE', 10);
|
|
|
|
|
2013-09-30 09:08:07 +13:00
|
|
|
// Max courses in log dropdown before switching to optional.
|
|
|
|
define('COURSE_MAX_COURSES_PER_DROPDOWN', 1000);
|
|
|
|
// Max users in log dropdown before switching to optional.
|
|
|
|
define('COURSE_MAX_USERS_PER_DROPDOWN', 1000);
|
|
|
|
define('FRONTPAGENEWS', '0');
|
|
|
|
define('FRONTPAGECATEGORYNAMES', '2');
|
|
|
|
define('FRONTPAGECATEGORYCOMBO', '4');
|
2013-03-26 13:04:42 +11:00
|
|
|
define('FRONTPAGEENROLLEDCOURSELIST', '5');
|
2013-09-30 09:08:07 +13:00
|
|
|
define('FRONTPAGEALLCOURSELIST', '6');
|
|
|
|
define('FRONTPAGECOURSESEARCH', '7');
|
|
|
|
// Important! Replaced with $CFG->frontpagecourselimit - maximum number of courses displayed on the frontpage.
|
2006-07-19 12:10:07 +00:00
|
|
|
define('EXCELROWS', 65535);
|
|
|
|
define('FIRSTUSEDEXCELROW', 3);
|
2004-11-23 00:16:24 +00:00
|
|
|
|
2007-01-02 09:33:07 +00:00
|
|
|
define('MOD_CLASS_ACTIVITY', 0);
|
|
|
|
define('MOD_CLASS_RESOURCE', 1);
|
|
|
|
|
2019-07-01 15:07:14 +02:00
|
|
|
define('COURSE_TIMELINE_ALLINCLUDINGHIDDEN', 'allincludinghidden');
|
2018-10-15 08:42:08 +02:00
|
|
|
define('COURSE_TIMELINE_ALL', 'all');
|
2017-06-06 12:05:09 +08:00
|
|
|
define('COURSE_TIMELINE_PAST', 'past');
|
|
|
|
define('COURSE_TIMELINE_INPROGRESS', 'inprogress');
|
|
|
|
define('COURSE_TIMELINE_FUTURE', 'future');
|
2021-04-15 15:05:00 +08:00
|
|
|
define('COURSE_TIMELINE_SEARCH', 'search');
|
2018-10-20 17:53:35 +02:00
|
|
|
define('COURSE_FAVOURITES', 'favourites');
|
2018-10-16 15:19:19 +08:00
|
|
|
define('COURSE_TIMELINE_HIDDEN', 'hidden');
|
2019-08-22 16:10:34 +01:00
|
|
|
define('COURSE_CUSTOMFIELD', 'customfield');
|
2018-08-27 16:43:56 +08:00
|
|
|
define('COURSE_DB_QUERY_LIMIT', 1000);
|
2019-08-22 16:10:34 +01:00
|
|
|
/** Searching for all courses that have no value for the specified custom field. */
|
|
|
|
define('COURSE_CUSTOMFIELD_EMPTY', -1);
|
2017-06-06 12:05:09 +08:00
|
|
|
|
2020-08-07 16:27:24 +08:00
|
|
|
// Course activity chooser footer default display option.
|
|
|
|
define('COURSE_CHOOSER_FOOTER_NONE', 'hidden');
|
|
|
|
|
2020-09-02 12:17:42 +08:00
|
|
|
// Download course content options.
|
|
|
|
define('DOWNLOAD_COURSE_CONTENT_DISABLED', 0);
|
|
|
|
define('DOWNLOAD_COURSE_CONTENT_ENABLED', 1);
|
|
|
|
define('DOWNLOAD_COURSE_CONTENT_SITE_DEFAULT', 2);
|
|
|
|
|
2002-05-31 09:34:50 +00:00
|
|
|
function make_log_url($module, $url) {
|
|
|
|
switch ($module) {
|
2006-05-01 06:04:22 +00:00
|
|
|
case 'course':
|
2012-01-15 19:02:08 +01:00
|
|
|
if (strpos($url, 'report/') === 0) {
|
2012-01-16 14:44:57 +08:00
|
|
|
// there is only one report type, course reports are deprecated
|
2012-01-15 19:02:08 +01:00
|
|
|
$url = "/$url";
|
|
|
|
break;
|
|
|
|
}
|
2006-05-01 06:04:22 +00:00
|
|
|
case 'file':
|
|
|
|
case 'login':
|
|
|
|
case 'lib':
|
|
|
|
case 'admin':
|
2012-11-05 14:31:37 +08:00
|
|
|
case 'category':
|
2007-08-06 18:19:15 +00:00
|
|
|
case 'mnet course':
|
2009-01-07 23:00:59 +00:00
|
|
|
if (strpos($url, '../') === 0) {
|
|
|
|
$url = ltrim($url, '.');
|
|
|
|
} else {
|
|
|
|
$url = "/course/$url";
|
|
|
|
}
|
2007-08-06 18:19:15 +00:00
|
|
|
break;
|
2014-02-26 14:22:04 +08:00
|
|
|
case 'calendar':
|
|
|
|
$url = "/calendar/$url";
|
|
|
|
break;
|
2008-03-19 22:48:13 +00:00
|
|
|
case 'user':
|
2006-05-01 06:04:22 +00:00
|
|
|
case 'blog':
|
2009-01-07 23:00:59 +00:00
|
|
|
$url = "/$module/$url";
|
2002-05-31 09:34:50 +00:00
|
|
|
break;
|
2006-05-01 06:04:22 +00:00
|
|
|
case 'upload':
|
2009-01-07 23:00:59 +00:00
|
|
|
$url = $url;
|
2004-09-17 00:10:53 +00:00
|
|
|
break;
|
2008-06-30 09:04:07 +00:00
|
|
|
case 'coursetags':
|
2009-01-07 23:00:59 +00:00
|
|
|
$url = '/'.$url;
|
2008-06-30 09:04:07 +00:00
|
|
|
break;
|
2006-05-01 06:04:22 +00:00
|
|
|
case 'library':
|
|
|
|
case '':
|
2009-01-07 23:00:59 +00:00
|
|
|
$url = '/';
|
2004-08-17 14:26:47 +00:00
|
|
|
break;
|
2008-01-09 10:12:31 +00:00
|
|
|
case 'message':
|
2009-01-07 23:00:59 +00:00
|
|
|
$url = "/message/$url";
|
|
|
|
break;
|
|
|
|
case 'notes':
|
|
|
|
$url = "/notes/$url";
|
2008-01-09 10:12:31 +00:00
|
|
|
break;
|
2009-09-09 02:43:43 +00:00
|
|
|
case 'tag':
|
|
|
|
$url = "/tag/$url";
|
|
|
|
break;
|
2009-09-09 12:45:45 +00:00
|
|
|
case 'role':
|
|
|
|
$url = '/'.$url;
|
|
|
|
break;
|
2013-01-28 12:03:59 +08:00
|
|
|
case 'grade':
|
|
|
|
$url = "/grade/$url";
|
|
|
|
break;
|
2002-05-31 09:34:50 +00:00
|
|
|
default:
|
2009-01-07 23:00:59 +00:00
|
|
|
$url = "/mod/$module/$url";
|
2002-05-31 09:34:50 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-01-07 23:00:59 +00:00
|
|
|
|
|
|
|
//now let's sanitise urls - there might be some ugly nasties:-(
|
|
|
|
$parts = explode('?', $url);
|
|
|
|
$script = array_shift($parts);
|
|
|
|
if (strpos($script, 'http') === 0) {
|
|
|
|
$script = clean_param($script, PARAM_URL);
|
|
|
|
} else {
|
|
|
|
$script = clean_param($script, PARAM_PATH);
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = '';
|
|
|
|
if ($parts) {
|
|
|
|
$query = implode('', $parts);
|
|
|
|
$query = str_replace('&', '&', $query); // both & and & are stored in db :-|
|
|
|
|
$parts = explode('&', $query);
|
|
|
|
$eq = urlencode('=');
|
|
|
|
foreach ($parts as $key=>$part) {
|
|
|
|
$part = urlencode(urldecode($part));
|
|
|
|
$part = str_replace($eq, '=', $part);
|
|
|
|
$parts[$key] = $part;
|
|
|
|
}
|
|
|
|
$query = '?'.implode('&', $parts);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $script.$query;
|
2002-05-31 09:34:50 +00:00
|
|
|
}
|
|
|
|
|
2006-05-15 05:01:00 +00:00
|
|
|
|
2007-01-04 03:22:51 +00:00
|
|
|
function build_mnet_logs_array($hostid, $course, $user=0, $date=0, $order="l.time ASC", $limitfrom='', $limitnum='',
|
|
|
|
$modname="", $modid=0, $modaction="", $groupid=0) {
|
2008-06-01 19:05:07 +00:00
|
|
|
global $CFG, $DB;
|
2007-01-04 03:22:51 +00:00
|
|
|
|
|
|
|
// It is assumed that $date is the GMT time of midnight for that day,
|
|
|
|
// and so the next 86400 seconds worth of logs are printed.
|
|
|
|
|
|
|
|
/// Setup for group handling.
|
2008-06-04 06:38:03 +00:00
|
|
|
|
|
|
|
// TODO: I don't understand group/context/etc. enough to be able to do
|
2007-01-04 03:22:51 +00:00
|
|
|
// something interesting with it here
|
|
|
|
// What is the context of a remote course?
|
2008-06-04 06:38:03 +00:00
|
|
|
|
2007-01-04 03:22:51 +00:00
|
|
|
/// If the group mode is separate, and this user does not have editing privileges,
|
|
|
|
/// then only the user's group can be viewed.
|
2012-07-23 15:59:04 +08:00
|
|
|
//if ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/course:managegroups', context_course::instance($course->id))) {
|
2007-01-04 03:22:51 +00:00
|
|
|
// $groupid = get_current_group($course->id);
|
|
|
|
//}
|
|
|
|
/// If this course doesn't have groups, no groupid can be specified.
|
|
|
|
//else if (!$course->groupmode) {
|
|
|
|
// $groupid = 0;
|
|
|
|
//}
|
2008-06-01 19:05:07 +00:00
|
|
|
|
2007-01-04 03:22:51 +00:00
|
|
|
$groupid = 0;
|
|
|
|
|
|
|
|
$joins = array();
|
2012-01-15 21:49:30 +01:00
|
|
|
$where = '';
|
2007-01-04 03:22:51 +00:00
|
|
|
|
2008-06-01 19:05:07 +00:00
|
|
|
$qry = "SELECT l.*, u.firstname, u.lastname, u.picture
|
|
|
|
FROM {mnet_log} l
|
|
|
|
LEFT JOIN {user} u ON l.userid = u.id
|
|
|
|
WHERE ";
|
|
|
|
$params = array();
|
|
|
|
|
|
|
|
$where .= "l.hostid = :hostid";
|
|
|
|
$params['hostid'] = $hostid;
|
2007-01-04 03:22:51 +00:00
|
|
|
|
|
|
|
// TODO: Is 1 really a magic number referring to the sitename?
|
2008-06-01 19:05:07 +00:00
|
|
|
if ($course != SITEID || $modid != 0) {
|
|
|
|
$where .= " AND l.course=:courseid";
|
|
|
|
$params['courseid'] = $course;
|
2007-01-04 03:22:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($modname) {
|
2008-06-01 19:05:07 +00:00
|
|
|
$where .= " AND l.module = :modname";
|
|
|
|
$params['modname'] = $modname;
|
2007-01-04 03:22:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ('site_errors' === $modid) {
|
2008-06-01 19:05:07 +00:00
|
|
|
$where .= " AND ( l.action='error' OR l.action='infected' )";
|
2007-01-04 03:22:51 +00:00
|
|
|
} else if ($modid) {
|
2008-06-04 06:38:03 +00:00
|
|
|
//TODO: This assumes that modids are the same across sites... probably
|
2007-01-04 03:22:51 +00:00
|
|
|
//not true
|
2008-06-01 19:05:07 +00:00
|
|
|
$where .= " AND l.cmid = :modid";
|
|
|
|
$params['modid'] = $modid;
|
2007-01-04 03:22:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($modaction) {
|
|
|
|
$firstletter = substr($modaction, 0, 1);
|
2010-09-04 12:36:00 +00:00
|
|
|
if ($firstletter == '-') {
|
2010-09-04 14:52:47 +00:00
|
|
|
$where .= " AND ".$DB->sql_like('l.action', ':modaction', false, true, true);
|
2010-09-04 12:36:00 +00:00
|
|
|
$params['modaction'] = '%'.substr($modaction, 1).'%';
|
|
|
|
} else {
|
|
|
|
$where .= " AND ".$DB->sql_like('l.action', ':modaction', false);
|
|
|
|
$params['modaction'] = '%'.$modaction.'%';
|
2007-01-04 03:22:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($user) {
|
2008-06-01 19:05:07 +00:00
|
|
|
$where .= " AND l.userid = :user";
|
|
|
|
$params['user'] = $user;
|
2007-01-04 03:22:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($date) {
|
|
|
|
$enddate = $date + 86400;
|
2008-06-01 19:05:07 +00:00
|
|
|
$where .= " AND l.time > :date AND l.time < :enddate";
|
|
|
|
$params['date'] = $date;
|
|
|
|
$params['enddate'] = $enddate;
|
2007-01-04 03:22:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$result = array();
|
2008-06-01 19:05:07 +00:00
|
|
|
$result['totalcount'] = $DB->count_records_sql("SELECT COUNT('x') FROM {mnet_log} l WHERE $where", $params);
|
2007-01-04 03:22:51 +00:00
|
|
|
if(!empty($result['totalcount'])) {
|
2008-06-01 19:05:07 +00:00
|
|
|
$where .= " ORDER BY $order";
|
|
|
|
$result['logs'] = $DB->get_records_sql("$qry $where", $params, $limitfrom, $limitnum);
|
2007-01-04 03:22:51 +00:00
|
|
|
} else {
|
|
|
|
$result['logs'] = array();
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2013-09-11 10:17:57 +08:00
|
|
|
/**
|
|
|
|
* Checks the integrity of the course data.
|
|
|
|
*
|
|
|
|
* In summary - compares course_sections.sequence and course_modules.section.
|
|
|
|
*
|
|
|
|
* More detailed, checks that:
|
|
|
|
* - course_sections.sequence contains each module id not more than once in the course
|
|
|
|
* - for each moduleid from course_sections.sequence the field course_modules.section
|
|
|
|
* refers to the same section id (this means course_sections.sequence is more
|
|
|
|
* important if they are different)
|
|
|
|
* - ($fullcheck only) each module in the course is present in one of
|
|
|
|
* course_sections.sequence
|
|
|
|
* - ($fullcheck only) removes non-existing course modules from section sequences
|
|
|
|
*
|
|
|
|
* If there are any mismatches, the changes are made and records are updated in DB.
|
|
|
|
*
|
|
|
|
* Course cache is NOT rebuilt if there are any errors!
|
|
|
|
*
|
|
|
|
* This function is used each time when course cache is being rebuilt with $fullcheck = false
|
|
|
|
* and in CLI script admin/cli/fix_course_sequence.php with $fullcheck = true
|
|
|
|
*
|
|
|
|
* @param int $courseid id of the course
|
|
|
|
* @param array $rawmods result of funciton {@link get_course_mods()} - containst
|
|
|
|
* the list of enabled course modules in the course. Retrieved from DB if not specified.
|
|
|
|
* Argument ignored in cashe of $fullcheck, the list is retrieved form DB anyway.
|
|
|
|
* @param array $sections records from course_sections table for this course.
|
|
|
|
* Retrieved from DB if not specified
|
|
|
|
* @param bool $fullcheck Will add orphaned modules to their sections and remove non-existing
|
|
|
|
* course modules from sequences. Only to be used in site maintenance mode when we are
|
|
|
|
* sure that another user is not in the middle of the process of moving/removing a module.
|
|
|
|
* @param bool $checkonly Only performs the check without updating DB, outputs all errors as debug messages.
|
|
|
|
* @return array array of messages with found problems. Empty output means everything is ok
|
|
|
|
*/
|
|
|
|
function course_integrity_check($courseid, $rawmods = null, $sections = null, $fullcheck = false, $checkonly = false) {
|
|
|
|
global $DB;
|
|
|
|
$messages = array();
|
|
|
|
if ($sections === null) {
|
|
|
|
$sections = $DB->get_records('course_sections', array('course' => $courseid), 'section', 'id,section,sequence');
|
|
|
|
}
|
|
|
|
if ($fullcheck) {
|
|
|
|
// Retrieve all records from course_modules regardless of module type visibility.
|
|
|
|
$rawmods = $DB->get_records('course_modules', array('course' => $courseid), 'id', 'id,section');
|
|
|
|
}
|
|
|
|
if ($rawmods === null) {
|
|
|
|
$rawmods = get_course_mods($courseid);
|
|
|
|
}
|
|
|
|
if (!$fullcheck && (empty($sections) || empty($rawmods))) {
|
|
|
|
// If either of the arrays is empty, no modules are displayed anyway.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
$debuggingprefix = 'Failed integrity check for course ['.$courseid.']. ';
|
|
|
|
|
|
|
|
// First make sure that each module id appears in section sequences only once.
|
|
|
|
// If it appears in several section sequences the last section wins.
|
|
|
|
// If it appears twice in one section sequence, the first occurence wins.
|
|
|
|
$modsection = array();
|
|
|
|
foreach ($sections as $sectionid => $section) {
|
|
|
|
$sections[$sectionid]->newsequence = $section->sequence;
|
|
|
|
if (!empty($section->sequence)) {
|
|
|
|
$sequence = explode(",", $section->sequence);
|
|
|
|
$sequenceunique = array_unique($sequence);
|
|
|
|
if (count($sequenceunique) != count($sequence)) {
|
|
|
|
// Some course module id appears in this section sequence more than once.
|
|
|
|
ksort($sequenceunique); // Preserve initial order of modules.
|
|
|
|
$sequence = array_values($sequenceunique);
|
|
|
|
$sections[$sectionid]->newsequence = join(',', $sequence);
|
|
|
|
$messages[] = $debuggingprefix.'Sequence for course section ['.
|
|
|
|
$sectionid.'] is "'.$sections[$sectionid]->sequence.'", must be "'.$sections[$sectionid]->newsequence.'"';
|
|
|
|
}
|
|
|
|
foreach ($sequence as $cmid) {
|
|
|
|
if (array_key_exists($cmid, $modsection) && isset($rawmods[$cmid])) {
|
|
|
|
// Some course module id appears to be in more than one section's sequences.
|
|
|
|
$wrongsectionid = $modsection[$cmid];
|
|
|
|
$sections[$wrongsectionid]->newsequence = trim(preg_replace("/,$cmid,/", ',', ','.$sections[$wrongsectionid]->newsequence. ','), ',');
|
|
|
|
$messages[] = $debuggingprefix.'Course module ['.$cmid.'] must be removed from sequence of section ['.
|
|
|
|
$wrongsectionid.'] because it is also present in sequence of section ['.$sectionid.']';
|
|
|
|
}
|
|
|
|
$modsection[$cmid] = $sectionid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add orphaned modules to their sections if they exist or to section 0 otherwise.
|
|
|
|
if ($fullcheck) {
|
|
|
|
foreach ($rawmods as $cmid => $mod) {
|
|
|
|
if (!isset($modsection[$cmid])) {
|
|
|
|
// This is a module that is not mentioned in course_section.sequence at all.
|
|
|
|
// Add it to the section $mod->section or to the last available section.
|
|
|
|
if ($mod->section && isset($sections[$mod->section])) {
|
|
|
|
$modsection[$cmid] = $mod->section;
|
|
|
|
} else {
|
|
|
|
$firstsection = reset($sections);
|
|
|
|
$modsection[$cmid] = $firstsection->id;
|
|
|
|
}
|
|
|
|
$sections[$modsection[$cmid]]->newsequence = trim($sections[$modsection[$cmid]]->newsequence.','.$cmid, ',');
|
|
|
|
$messages[] = $debuggingprefix.'Course module ['.$cmid.'] is missing from sequence of section ['.
|
2013-09-13 10:28:52 +10:00
|
|
|
$modsection[$cmid].']';
|
2013-09-11 10:17:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ($modsection as $cmid => $sectionid) {
|
|
|
|
if (!isset($rawmods[$cmid])) {
|
|
|
|
// Section $sectionid refers to module id that does not exist.
|
|
|
|
$sections[$sectionid]->newsequence = trim(preg_replace("/,$cmid,/", ',', ','.$sections[$sectionid]->newsequence.','), ',');
|
|
|
|
$messages[] = $debuggingprefix.'Course module ['.$cmid.
|
|
|
|
'] does not exist but is present in the sequence of section ['.$sectionid.']';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update changed sections.
|
|
|
|
if (!$checkonly && !empty($messages)) {
|
|
|
|
foreach ($sections as $sectionid => $section) {
|
|
|
|
if ($section->newsequence !== $section->sequence) {
|
|
|
|
$DB->update_record('course_sections', array('id' => $sectionid, 'sequence' => $section->newsequence));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now make sure that all modules point to the correct sections.
|
|
|
|
foreach ($rawmods as $cmid => $mod) {
|
|
|
|
if (isset($modsection[$cmid]) && $modsection[$cmid] != $mod->section) {
|
|
|
|
if (!$checkonly) {
|
|
|
|
$DB->update_record('course_modules', array('id' => $cmid, 'section' => $modsection[$cmid]));
|
|
|
|
}
|
|
|
|
$messages[] = $debuggingprefix.'Course module ['.$cmid.
|
|
|
|
'] points to section ['.$mod->section.'] instead of ['.$modsection[$cmid].']';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $messages;
|
|
|
|
}
|
|
|
|
|
2008-06-01 19:05:07 +00:00
|
|
|
/**
|
2022-03-03 10:39:09 +11:00
|
|
|
* Returns an array where the key is the module name (component name without 'mod_')
|
|
|
|
* and the value is a lang_string object with a human-readable string.
|
2012-09-25 10:34:18 +08:00
|
|
|
*
|
2022-03-03 10:39:09 +11:00
|
|
|
* @param bool $plural If true, the function returns the plural forms of the names.
|
2022-05-05 18:33:20 +08:00
|
|
|
* @param bool $resetcache If true, the static cache will be reset
|
2022-03-03 10:39:09 +11:00
|
|
|
* @return lang_string[] Localised human-readable names of all used modules.
|
2008-06-01 19:05:07 +00:00
|
|
|
*/
|
2022-05-05 18:33:20 +08:00
|
|
|
function get_module_types_names($plural = false, $resetcache = false) {
|
2012-09-25 10:34:18 +08:00
|
|
|
static $modnames = null;
|
|
|
|
global $DB, $CFG;
|
2022-05-05 18:33:20 +08:00
|
|
|
if ($modnames === null || $resetcache) {
|
2012-09-25 10:34:18 +08:00
|
|
|
$modnames = array(0 => array(), 1 => array());
|
|
|
|
if ($allmods = $DB->get_records("modules")) {
|
|
|
|
foreach ($allmods as $mod) {
|
|
|
|
if (file_exists("$CFG->dirroot/mod/$mod->name/lib.php") && $mod->visible) {
|
2019-04-10 16:27:36 +01:00
|
|
|
$modnames[0][$mod->name] = get_string("modulename", "$mod->name", null, true);
|
|
|
|
$modnames[1][$mod->name] = get_string("modulenameplural", "$mod->name", null, true);
|
2012-09-25 10:34:18 +08:00
|
|
|
}
|
2007-08-20 10:52:59 +00:00
|
|
|
}
|
2003-04-22 20:46:12 +00:00
|
|
|
}
|
2002-07-23 16:24:12 +00:00
|
|
|
}
|
2012-09-25 10:34:18 +08:00
|
|
|
return $modnames[(int)$plural];
|
2002-07-23 16:24:12 +00:00
|
|
|
}
|
|
|
|
|
2012-01-19 11:35:46 +00:00
|
|
|
/**
|
|
|
|
* Set highlighted section. Only one section can be highlighted at the time.
|
|
|
|
*
|
|
|
|
* @param int $courseid course id
|
|
|
|
* @param int $marker highlight section with this number, 0 means remove higlightin
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function course_set_marker($courseid, $marker) {
|
2017-02-03 09:40:46 +08:00
|
|
|
global $DB, $COURSE;
|
2012-01-19 11:35:46 +00:00
|
|
|
$DB->set_field("course", "marker", $marker, array('id' => $courseid));
|
2017-02-03 09:40:46 +08:00
|
|
|
if ($COURSE && $COURSE->id == $courseid) {
|
|
|
|
$COURSE->marker = $marker;
|
|
|
|
}
|
2021-06-22 09:50:35 +02:00
|
|
|
core_courseformat\base::reset_course_cache($courseid);
|
2017-02-03 09:40:46 +08:00
|
|
|
course_modinfo::clear_instance_cache($courseid);
|
2012-01-19 11:35:46 +00:00
|
|
|
}
|
|
|
|
|
2008-06-01 19:05:07 +00:00
|
|
|
/**
|
2010-09-17 12:16:27 +00:00
|
|
|
* For a given course section, marks it visible or hidden,
|
2008-06-01 19:05:07 +00:00
|
|
|
* and does the same for every activity in that section
|
MDL-31096 Convert course resource/section javascript to YUI3 Module
AMOS BEGIN
MOV [hideweekfromothers,moodle],[hidefromothers,format_week]
MOV [showweekfromothers,moodle],[showfromothers,format_week]
MOV [hidetopicfromothers,moodle],[hidefromothers,format_topics]
MOV [showtopicfromothers,moodle],[showfromothers,format_topics]
AMOS END
2012-01-05 16:53:02 +00:00
|
|
|
*
|
|
|
|
* @param int $courseid course id
|
|
|
|
* @param int $sectionnumber The section number to adjust
|
|
|
|
* @param int $visibility The new visibility
|
|
|
|
* @return array A list of resources which were hidden in the section
|
2008-06-01 19:05:07 +00:00
|
|
|
*/
|
2003-05-04 07:59:46 +00:00
|
|
|
function set_section_visible($courseid, $sectionnumber, $visibility) {
|
2008-06-01 19:05:07 +00:00
|
|
|
global $DB;
|
2003-05-04 07:59:46 +00:00
|
|
|
|
MDL-31096 Convert course resource/section javascript to YUI3 Module
AMOS BEGIN
MOV [hideweekfromothers,moodle],[hidefromothers,format_week]
MOV [showweekfromothers,moodle],[showfromothers,format_week]
MOV [hidetopicfromothers,moodle],[hidefromothers,format_topics]
MOV [showtopicfromothers,moodle],[showfromothers,format_topics]
AMOS END
2012-01-05 16:53:02 +00:00
|
|
|
$resourcestotoggle = array();
|
2008-06-01 19:05:07 +00:00
|
|
|
if ($section = $DB->get_record("course_sections", array("course"=>$courseid, "section"=>$sectionnumber))) {
|
2016-02-03 17:28:56 +08:00
|
|
|
course_update_section($courseid, $section, array('visible' => $visibility));
|
MDL-31096 Convert course resource/section javascript to YUI3 Module
AMOS BEGIN
MOV [hideweekfromothers,moodle],[hidefromothers,format_week]
MOV [showweekfromothers,moodle],[showfromothers,format_week]
MOV [hidetopicfromothers,moodle],[hidefromothers,format_topics]
MOV [showtopicfromothers,moodle],[showfromothers,format_topics]
AMOS END
2012-01-05 16:53:02 +00:00
|
|
|
|
|
|
|
// Determine which modules are visible for AJAX update
|
2016-02-03 17:28:56 +08:00
|
|
|
$modules = !empty($section->sequence) ? explode(',', $section->sequence) : array();
|
MDL-31096 Convert course resource/section javascript to YUI3 Module
AMOS BEGIN
MOV [hideweekfromothers,moodle],[hidefromothers,format_week]
MOV [showweekfromothers,moodle],[showfromothers,format_week]
MOV [hidetopicfromothers,moodle],[hidefromothers,format_topics]
MOV [showtopicfromothers,moodle],[showfromothers,format_topics]
AMOS END
2012-01-05 16:53:02 +00:00
|
|
|
if (!empty($modules)) {
|
|
|
|
list($insql, $params) = $DB->get_in_or_equal($modules);
|
|
|
|
$select = 'id ' . $insql . ' AND visible = ?';
|
|
|
|
array_push($params, $visibility);
|
|
|
|
if (!$visibility) {
|
|
|
|
$select .= ' AND visibleold = 1';
|
|
|
|
}
|
|
|
|
$resourcestotoggle = $DB->get_fieldset_select('course_modules', 'id', $select, $params);
|
|
|
|
}
|
2003-05-04 07:59:46 +00:00
|
|
|
}
|
MDL-31096 Convert course resource/section javascript to YUI3 Module
AMOS BEGIN
MOV [hideweekfromothers,moodle],[hidefromothers,format_week]
MOV [showweekfromothers,moodle],[showfromothers,format_week]
MOV [hidetopicfromothers,moodle],[hidefromothers,format_topics]
MOV [showtopicfromothers,moodle],[showfromothers,format_topics]
AMOS END
2012-01-05 16:53:02 +00:00
|
|
|
return $resourcestotoggle;
|
2003-05-04 07:59:46 +00:00
|
|
|
}
|
2002-09-09 11:48:11 +00:00
|
|
|
|
course categories: Fix many bugs with category editing and permissions. Clean up code.
Bugs: MDL-17479, MDL-16426, MDL-16063, MDL-16013, MDL-15658, MDL-15556, MDL-15161, MDL-14925, MDL-13742, MDL-11557.
* Simplify category editing permissions to just moodle/category:manage and moodle/category:seehiddencategories.
* Enforce those correctly. (Note MDL 17502 is still outstanding.)
* Don't screw up category sort order when you just edit name or description.
* Niceties like where redirects go when you cancel or submit forms.
* Make sure a global course creator can see the site admin block.
* Don't allow a category to be made the child of one of its children!
* General code cleanup to bring key files more in line with best pracitice.
Apologies for the fact it is one big patch, rather than a series of smaller patches. However, categoryedit.php, category.php and index.php where in pretty bad shape and needed significant cleaning up. categoryedit.php, in particular, was almost completely rewritten.
Merged from MOODLE_19_STABLE.
2008-12-04 08:53:10 +00:00
|
|
|
/**
|
|
|
|
* Return the course category context for the category with id $categoryid, except
|
|
|
|
* that if $categoryid is 0, return the system context.
|
|
|
|
*
|
|
|
|
* @param integer $categoryid a category id or 0.
|
2013-09-30 09:08:07 +13:00
|
|
|
* @return context the corresponding context
|
course categories: Fix many bugs with category editing and permissions. Clean up code.
Bugs: MDL-17479, MDL-16426, MDL-16063, MDL-16013, MDL-15658, MDL-15556, MDL-15161, MDL-14925, MDL-13742, MDL-11557.
* Simplify category editing permissions to just moodle/category:manage and moodle/category:seehiddencategories.
* Enforce those correctly. (Note MDL 17502 is still outstanding.)
* Don't screw up category sort order when you just edit name or description.
* Niceties like where redirects go when you cancel or submit forms.
* Make sure a global course creator can see the site admin block.
* Don't allow a category to be made the child of one of its children!
* General code cleanup to bring key files more in line with best pracitice.
Apologies for the fact it is one big patch, rather than a series of smaller patches. However, categoryedit.php, category.php and index.php where in pretty bad shape and needed significant cleaning up. categoryedit.php, in particular, was almost completely rewritten.
Merged from MOODLE_19_STABLE.
2008-12-04 08:53:10 +00:00
|
|
|
*/
|
|
|
|
function get_category_or_system_context($categoryid) {
|
|
|
|
if ($categoryid) {
|
2012-07-24 11:52:38 +08:00
|
|
|
return context_coursecat::instance($categoryid, IGNORE_MISSING);
|
course categories: Fix many bugs with category editing and permissions. Clean up code.
Bugs: MDL-17479, MDL-16426, MDL-16063, MDL-16013, MDL-15658, MDL-15556, MDL-15161, MDL-14925, MDL-13742, MDL-11557.
* Simplify category editing permissions to just moodle/category:manage and moodle/category:seehiddencategories.
* Enforce those correctly. (Note MDL 17502 is still outstanding.)
* Don't screw up category sort order when you just edit name or description.
* Niceties like where redirects go when you cancel or submit forms.
* Make sure a global course creator can see the site admin block.
* Don't allow a category to be made the child of one of its children!
* General code cleanup to bring key files more in line with best pracitice.
Apologies for the fact it is one big patch, rather than a series of smaller patches. However, categoryedit.php, category.php and index.php where in pretty bad shape and needed significant cleaning up. categoryedit.php, in particular, was almost completely rewritten.
Merged from MOODLE_19_STABLE.
2008-12-04 08:53:10 +00:00
|
|
|
} else {
|
2012-07-23 15:59:04 +08:00
|
|
|
return context_system::instance();
|
course categories: Fix many bugs with category editing and permissions. Clean up code.
Bugs: MDL-17479, MDL-16426, MDL-16063, MDL-16013, MDL-15658, MDL-15556, MDL-15161, MDL-14925, MDL-13742, MDL-11557.
* Simplify category editing permissions to just moodle/category:manage and moodle/category:seehiddencategories.
* Enforce those correctly. (Note MDL 17502 is still outstanding.)
* Don't screw up category sort order when you just edit name or description.
* Niceties like where redirects go when you cancel or submit forms.
* Make sure a global course creator can see the site admin block.
* Don't allow a category to be made the child of one of its children!
* General code cleanup to bring key files more in line with best pracitice.
Apologies for the fact it is one big patch, rather than a series of smaller patches. However, categoryedit.php, category.php and index.php where in pretty bad shape and needed significant cleaning up. categoryedit.php, in particular, was almost completely rewritten.
Merged from MOODLE_19_STABLE.
2008-12-04 08:53:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-05 08:56:54 +00:00
|
|
|
/**
|
|
|
|
* Print the buttons relating to course requests.
|
|
|
|
*
|
2019-09-23 16:37:23 +02:00
|
|
|
* @param context $context current page context.
|
2022-02-02 11:17:41 +08:00
|
|
|
* @deprecated since Moodle 4.0
|
|
|
|
* @todo Final deprecation MDL-73976
|
2008-12-05 08:56:54 +00:00
|
|
|
*/
|
2013-03-19 15:45:55 +11:00
|
|
|
function print_course_request_buttons($context) {
|
2009-08-20 08:40:19 +00:00
|
|
|
global $CFG, $DB, $OUTPUT;
|
2022-02-02 11:17:41 +08:00
|
|
|
debugging("print_course_request_buttons() is deprecated. " .
|
|
|
|
"This is replaced with the category_action_bar tertiary navigation.", DEBUG_DEVELOPER);
|
2008-12-05 08:56:54 +00:00
|
|
|
if (empty($CFG->enablecourserequests)) {
|
|
|
|
return;
|
|
|
|
}
|
2019-09-23 16:37:23 +02:00
|
|
|
if (course_request::can_request($context)) {
|
|
|
|
// Print a button to request a new course.
|
|
|
|
$params = [];
|
|
|
|
if ($context instanceof context_coursecat) {
|
|
|
|
$params['category'] = $context->instanceid;
|
|
|
|
}
|
|
|
|
echo $OUTPUT->single_button(new moodle_url('/course/request.php', $params),
|
|
|
|
get_string('requestcourse'), 'get');
|
2008-12-05 08:56:54 +00:00
|
|
|
}
|
|
|
|
/// Print a button to manage pending requests
|
2018-06-21 13:34:59 +08:00
|
|
|
if (has_capability('moodle/site:approvecourse', $context)) {
|
2010-01-03 15:46:14 +00:00
|
|
|
$disabled = !$DB->record_exists('course_request', array());
|
2013-03-19 15:45:55 +11:00
|
|
|
echo $OUTPUT->single_button(new moodle_url('/course/pending.php'), get_string('coursespending'), 'get', array('disabled' => $disabled));
|
2008-12-05 08:56:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-28 03:03:35 +00:00
|
|
|
/**
|
|
|
|
* Does the user have permission to edit things in this category?
|
|
|
|
*
|
|
|
|
* @param integer $categoryid The id of the category we are showing, or 0 for system context.
|
|
|
|
* @return boolean has_any_capability(array(...), ...); in the appropriate context.
|
|
|
|
*/
|
|
|
|
function can_edit_in_category($categoryid = 0) {
|
|
|
|
$context = get_category_or_system_context($categoryid);
|
|
|
|
return has_any_capability(array('moodle/category:manage', 'moodle/course:create'), $context);
|
|
|
|
}
|
|
|
|
|
2002-08-01 03:50:27 +00:00
|
|
|
/// MODULE FUNCTIONS /////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
function add_course_module($mod) {
|
2008-06-01 19:05:07 +00:00
|
|
|
global $DB;
|
2002-08-01 03:50:27 +00:00
|
|
|
|
2002-10-21 03:00:40 +00:00
|
|
|
$mod->added = time();
|
2004-08-08 15:14:25 +00:00
|
|
|
unset($mod->id);
|
2002-08-01 03:50:27 +00:00
|
|
|
|
2012-10-10 11:54:48 +08:00
|
|
|
$cmid = $DB->insert_record("course_modules", $mod);
|
|
|
|
rebuild_course_cache($mod->course, true);
|
|
|
|
return $cmid;
|
2002-08-01 03:50:27 +00:00
|
|
|
}
|
|
|
|
|
2017-02-02 12:33:16 +08:00
|
|
|
/**
|
|
|
|
* Creates a course section and adds it to the specified position
|
|
|
|
*
|
|
|
|
* @param int|stdClass $courseorid course id or course object
|
|
|
|
* @param int $position position to add to, 0 means to the end. If position is greater than
|
|
|
|
* number of existing secitons, the section is added to the end. This will become sectionnum of the
|
|
|
|
* new section. All existing sections at this or bigger position will be shifted down.
|
|
|
|
* @param bool $skipcheck the check has already been made and we know that the section with this position does not exist
|
|
|
|
* @return stdClass created section object
|
|
|
|
*/
|
|
|
|
function course_create_section($courseorid, $position = 0, $skipcheck = false) {
|
|
|
|
global $DB;
|
|
|
|
$courseid = is_object($courseorid) ? $courseorid->id : $courseorid;
|
|
|
|
|
|
|
|
// Find the last sectionnum among existing sections.
|
|
|
|
if ($skipcheck) {
|
|
|
|
$lastsection = $position - 1;
|
|
|
|
} else {
|
|
|
|
$lastsection = (int)$DB->get_field_sql('SELECT max(section) from {course_sections} WHERE course = ?', [$courseid]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// First add section to the end.
|
|
|
|
$cw = new stdClass();
|
|
|
|
$cw->course = $courseid;
|
|
|
|
$cw->section = $lastsection + 1;
|
|
|
|
$cw->summary = '';
|
|
|
|
$cw->summaryformat = FORMAT_HTML;
|
|
|
|
$cw->sequence = '';
|
|
|
|
$cw->name = null;
|
|
|
|
$cw->visible = 1;
|
|
|
|
$cw->availability = null;
|
2017-03-31 19:58:40 +05:30
|
|
|
$cw->timemodified = time();
|
2017-02-02 12:33:16 +08:00
|
|
|
$cw->id = $DB->insert_record("course_sections", $cw);
|
|
|
|
|
|
|
|
// Now move it to the specified position.
|
|
|
|
if ($position > 0 && $position <= $lastsection) {
|
|
|
|
$course = is_object($courseorid) ? $courseorid : get_course($courseorid);
|
|
|
|
move_section_to($course, $cw->section, $position, true);
|
|
|
|
$cw->section = $position;
|
|
|
|
}
|
|
|
|
|
|
|
|
core\event\course_section_created::create_from_section($cw)->trigger();
|
|
|
|
|
|
|
|
rebuild_course_cache($courseid, true);
|
|
|
|
return $cw;
|
|
|
|
}
|
|
|
|
|
2007-08-26 15:30:51 +00:00
|
|
|
/**
|
2012-10-08 10:36:01 +08:00
|
|
|
* Creates missing course section(s) and rebuilds course cache
|
2012-09-13 16:30:32 +08:00
|
|
|
*
|
2012-10-10 12:41:04 +08:00
|
|
|
* @param int|stdClass $courseorid course id or course object
|
2012-10-08 10:36:01 +08:00
|
|
|
* @param int|array $sections list of relative section numbers to create
|
|
|
|
* @return bool if there were any sections created
|
2007-08-26 15:30:51 +00:00
|
|
|
*/
|
2012-10-10 12:41:04 +08:00
|
|
|
function course_create_sections_if_missing($courseorid, $sections) {
|
2012-10-08 10:36:01 +08:00
|
|
|
if (!is_array($sections)) {
|
|
|
|
$sections = array($sections);
|
|
|
|
}
|
2012-10-10 12:41:04 +08:00
|
|
|
$existing = array_keys(get_fast_modinfo($courseorid)->get_section_info_all());
|
2017-02-02 12:33:16 +08:00
|
|
|
if ($newsections = array_diff($sections, $existing)) {
|
|
|
|
foreach ($newsections as $sectionnum) {
|
|
|
|
course_create_section($courseorid, $sectionnum, true);
|
2012-10-08 10:36:01 +08:00
|
|
|
}
|
2017-02-02 12:33:16 +08:00
|
|
|
return true;
|
2012-10-08 10:36:01 +08:00
|
|
|
}
|
2017-02-02 12:33:16 +08:00
|
|
|
return false;
|
2007-08-26 15:30:51 +00:00
|
|
|
}
|
2012-05-17 13:37:33 +01:00
|
|
|
|
2007-10-03 12:22:25 +00:00
|
|
|
/**
|
2012-09-24 17:08:02 +08:00
|
|
|
* Adds an existing module to the section
|
2007-10-03 12:22:25 +00:00
|
|
|
*
|
2012-09-24 17:08:02 +08:00
|
|
|
* Updates both tables {course_sections} and {course_modules}
|
|
|
|
*
|
2013-08-06 10:29:37 +01:00
|
|
|
* Note: This function does not use modinfo PROVIDED that the section you are
|
|
|
|
* adding the module to already exists. If the section does not exist, it will
|
|
|
|
* build modinfo if necessary and create the section.
|
|
|
|
*
|
2012-09-24 17:08:02 +08:00
|
|
|
* @param int|stdClass $courseorid course id or course object
|
2012-11-14 19:03:07 +01:00
|
|
|
* @param int $cmid id of the module already existing in course_modules table
|
2012-09-24 17:08:02 +08:00
|
|
|
* @param int $sectionnum relative number of the section (field course_sections.section)
|
|
|
|
* If section does not exist it will be created
|
|
|
|
* @param int|stdClass $beforemod id or object with field id corresponding to the module
|
|
|
|
* before which the module needs to be included. Null for inserting in the
|
|
|
|
* end of the section
|
|
|
|
* @return int The course_sections ID where the module is inserted
|
2007-10-03 12:22:25 +00:00
|
|
|
*/
|
2012-11-14 19:03:07 +01:00
|
|
|
function course_add_cm_to_section($courseorid, $cmid, $sectionnum, $beforemod = null) {
|
2012-09-24 17:08:02 +08:00
|
|
|
global $DB, $COURSE;
|
|
|
|
if (is_object($beforemod)) {
|
|
|
|
$beforemod = $beforemod->id;
|
|
|
|
}
|
2012-11-14 19:03:07 +01:00
|
|
|
if (is_object($courseorid)) {
|
|
|
|
$courseid = $courseorid->id;
|
|
|
|
} else {
|
|
|
|
$courseid = $courseorid;
|
|
|
|
}
|
|
|
|
// Do not try to use modinfo here, there is no guarantee it is valid!
|
2013-08-06 10:29:37 +01:00
|
|
|
$section = $DB->get_record('course_sections',
|
|
|
|
array('course' => $courseid, 'section' => $sectionnum), '*', IGNORE_MISSING);
|
|
|
|
if (!$section) {
|
|
|
|
// This function call requires modinfo.
|
|
|
|
course_create_sections_if_missing($courseorid, $sectionnum);
|
|
|
|
$section = $DB->get_record('course_sections',
|
|
|
|
array('course' => $courseid, 'section' => $sectionnum), '*', MUST_EXIST);
|
|
|
|
}
|
|
|
|
|
2012-09-24 17:08:02 +08:00
|
|
|
$modarray = explode(",", trim($section->sequence));
|
2012-11-06 17:00:47 +08:00
|
|
|
if (empty($section->sequence)) {
|
2012-11-14 19:03:07 +01:00
|
|
|
$newsequence = "$cmid";
|
2012-09-24 17:08:02 +08:00
|
|
|
} else if ($beforemod && ($key = array_keys($modarray, $beforemod))) {
|
2012-11-14 19:03:07 +01:00
|
|
|
$insertarray = array($cmid, $beforemod);
|
2012-09-24 17:08:02 +08:00
|
|
|
array_splice($modarray, $key[0], 1, $insertarray);
|
|
|
|
$newsequence = implode(",", $modarray);
|
|
|
|
} else {
|
2012-11-14 19:03:07 +01:00
|
|
|
$newsequence = "$section->sequence,$cmid";
|
2012-09-24 17:08:02 +08:00
|
|
|
}
|
|
|
|
$DB->set_field("course_sections", "sequence", $newsequence, array("id" => $section->id));
|
2012-11-14 19:03:07 +01:00
|
|
|
$DB->set_field('course_modules', 'section', $section->id, array('id' => $cmid));
|
2021-07-16 13:49:35 +10:00
|
|
|
rebuild_course_cache($courseid, true);
|
2012-09-24 17:08:02 +08:00
|
|
|
return $section->id; // Return course_sections ID that was used.
|
2002-08-01 03:50:27 +00:00
|
|
|
}
|
|
|
|
|
2014-03-31 10:13:44 +08:00
|
|
|
/**
|
|
|
|
* Change the group mode of a course module.
|
|
|
|
*
|
|
|
|
* Note: Do not forget to trigger the event \core\event\course_module_updated as it needs
|
|
|
|
* to be triggered manually, refer to {@link \core\event\course_module_updated::create_from_cm()}.
|
|
|
|
*
|
|
|
|
* @param int $id course module ID.
|
|
|
|
* @param int $groupmode the new groupmode value.
|
|
|
|
* @return bool True if the $groupmode was updated.
|
|
|
|
*/
|
2005-02-12 21:41:22 +00:00
|
|
|
function set_coursemodule_groupmode($id, $groupmode) {
|
2008-06-01 19:05:07 +00:00
|
|
|
global $DB;
|
2012-10-10 11:54:48 +08:00
|
|
|
$cm = $DB->get_record('course_modules', array('id' => $id), 'id,course,groupmode', MUST_EXIST);
|
|
|
|
if ($cm->groupmode != $groupmode) {
|
|
|
|
$DB->set_field('course_modules', 'groupmode', $groupmode, array('id' => $cm->id));
|
2021-07-16 13:49:35 +10:00
|
|
|
\course_modinfo::purge_course_module_cache($cm->course, $cm->id);
|
|
|
|
rebuild_course_cache($cm->course, false, true);
|
2012-10-10 11:54:48 +08:00
|
|
|
}
|
|
|
|
return ($cm->groupmode != $groupmode);
|
2004-01-31 08:40:38 +00:00
|
|
|
}
|
|
|
|
|
2007-04-11 09:19:24 +00:00
|
|
|
function set_coursemodule_idnumber($id, $idnumber) {
|
2008-06-01 19:05:07 +00:00
|
|
|
global $DB;
|
2012-10-10 11:54:48 +08:00
|
|
|
$cm = $DB->get_record('course_modules', array('id' => $id), 'id,course,idnumber', MUST_EXIST);
|
|
|
|
if ($cm->idnumber != $idnumber) {
|
|
|
|
$DB->set_field('course_modules', 'idnumber', $idnumber, array('id' => $cm->id));
|
2021-07-16 13:49:35 +10:00
|
|
|
\course_modinfo::purge_course_module_cache($cm->course, $cm->id);
|
|
|
|
rebuild_course_cache($cm->course, false, true);
|
2012-10-10 11:54:48 +08:00
|
|
|
}
|
|
|
|
return ($cm->idnumber != $idnumber);
|
2007-04-11 09:19:24 +00:00
|
|
|
}
|
2008-07-28 12:31:29 +00:00
|
|
|
|
2020-11-24 13:00:38 +08:00
|
|
|
/**
|
|
|
|
* Set downloadcontent value to course module.
|
|
|
|
*
|
|
|
|
* @param int $id The id of the module.
|
|
|
|
* @param bool $downloadcontent Whether the module can be downloaded when download course content is enabled.
|
|
|
|
* @return bool True if downloadcontent has been updated, false otherwise.
|
|
|
|
*/
|
|
|
|
function set_downloadcontent(int $id, bool $downloadcontent): bool {
|
|
|
|
global $DB;
|
|
|
|
$cm = $DB->get_record('course_modules', ['id' => $id], 'id, course, downloadcontent', MUST_EXIST);
|
|
|
|
if ($cm->downloadcontent != $downloadcontent) {
|
|
|
|
$DB->set_field('course_modules', 'downloadcontent', $downloadcontent, ['id' => $cm->id]);
|
|
|
|
rebuild_course_cache($cm->course, true);
|
|
|
|
}
|
|
|
|
return ($cm->downloadcontent != $downloadcontent);
|
|
|
|
}
|
|
|
|
|
2006-04-12 03:11:34 +00:00
|
|
|
/**
|
2012-11-07 15:13:07 +08:00
|
|
|
* Set the visibility of a module and inherent properties.
|
|
|
|
*
|
2014-03-31 10:13:44 +08:00
|
|
|
* Note: Do not forget to trigger the event \core\event\course_module_updated as it needs
|
|
|
|
* to be triggered manually, refer to {@link \core\event\course_module_updated::create_from_cm()}.
|
|
|
|
*
|
2012-11-07 15:13:07 +08:00
|
|
|
* From 2.4 the parameter $prevstateoverrides has been removed, the logic it triggered
|
|
|
|
* has been moved to {@link set_section_visible()} which was the only place from which
|
|
|
|
* the parameter was used.
|
|
|
|
*
|
|
|
|
* @param int $id of the module
|
|
|
|
* @param int $visible state of the module
|
2017-02-03 09:40:46 +08:00
|
|
|
* @param int $visibleoncoursepage state of the module on the course page
|
2012-11-07 15:13:07 +08:00
|
|
|
* @return bool false when the module was not found, true otherwise
|
|
|
|
*/
|
2017-02-03 09:40:46 +08:00
|
|
|
function set_coursemodule_visible($id, $visible, $visibleoncoursepage = 1) {
|
2010-09-03 02:49:04 +00:00
|
|
|
global $DB, $CFG;
|
2010-09-16 08:16:26 +00:00
|
|
|
require_once($CFG->libdir.'/gradelib.php');
|
2013-07-17 13:46:31 +08:00
|
|
|
require_once($CFG->dirroot.'/calendar/lib.php');
|
2010-09-16 08:16:26 +00:00
|
|
|
|
2008-06-01 19:05:07 +00:00
|
|
|
if (!$cm = $DB->get_record('course_modules', array('id'=>$id))) {
|
2006-09-24 06:06:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-12-19 09:46:16 +08:00
|
|
|
|
|
|
|
// Create events and propagate visibility to associated grade items if the value has changed.
|
|
|
|
// Only do this if it's changed to avoid accidently overwriting manual showing/hiding of student grades.
|
2017-02-03 09:40:46 +08:00
|
|
|
if ($cm->visible == $visible && $cm->visibleoncoursepage == $visibleoncoursepage) {
|
2012-12-19 09:46:16 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-06-01 19:05:07 +00:00
|
|
|
if (!$modulename = $DB->get_field('modules', 'name', array('id'=>$cm->module))) {
|
2006-09-24 06:06:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-02-03 09:40:46 +08:00
|
|
|
if (($cm->visible != $visible) &&
|
|
|
|
($events = $DB->get_records('event', array('instance' => $cm->instance, 'modulename' => $modulename)))) {
|
2004-05-05 07:07:56 +00:00
|
|
|
foreach($events as $event) {
|
2005-02-12 21:41:22 +00:00
|
|
|
if ($visible) {
|
2013-07-17 02:34:07 +02:00
|
|
|
$event = new calendar_event($event);
|
|
|
|
$event->toggle_visibility(true);
|
2005-02-12 21:41:22 +00:00
|
|
|
} else {
|
2013-07-17 02:34:07 +02:00
|
|
|
$event = new calendar_event($event);
|
|
|
|
$event->toggle_visibility(false);
|
2005-02-12 21:41:22 +00:00
|
|
|
}
|
2004-05-05 07:07:56 +00:00
|
|
|
}
|
|
|
|
}
|
2010-09-03 02:49:04 +00:00
|
|
|
|
2012-11-07 15:13:07 +08:00
|
|
|
// Updating visible and visibleold to keep them in sync. Only changing a section visibility will
|
|
|
|
// affect visibleold to allow for an original visibility restore. See set_section_visible().
|
|
|
|
$cminfo = new stdClass();
|
|
|
|
$cminfo->id = $id;
|
|
|
|
$cminfo->visible = $visible;
|
2017-02-03 09:40:46 +08:00
|
|
|
$cminfo->visibleoncoursepage = $visibleoncoursepage;
|
2012-11-07 15:13:07 +08:00
|
|
|
$cminfo->visibleold = $visible;
|
|
|
|
$DB->update_record('course_modules', $cminfo);
|
|
|
|
|
2013-11-27 17:40:40 +00:00
|
|
|
// Hide the associated grade items so the teacher doesn't also have to go to the gradebook and hide them there.
|
|
|
|
// Note that this must be done after updating the row in course_modules, in case
|
|
|
|
// the modules grade_item_update function needs to access $cm->visible.
|
2017-02-03 09:40:46 +08:00
|
|
|
if ($cm->visible != $visible &&
|
|
|
|
plugin_supports('mod', $modulename, FEATURE_CONTROLS_GRADE_VISIBILITY) &&
|
2013-11-27 17:40:40 +00:00
|
|
|
component_callback_exists('mod_' . $modulename, 'grade_item_update')) {
|
|
|
|
$instance = $DB->get_record($modulename, array('id' => $cm->instance), '*', MUST_EXIST);
|
|
|
|
component_callback('mod_' . $modulename, 'grade_item_update', array($instance));
|
2017-02-03 09:40:46 +08:00
|
|
|
} else if ($cm->visible != $visible) {
|
2013-11-27 17:40:40 +00:00
|
|
|
$grade_items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$modulename, 'iteminstance'=>$cm->instance, 'courseid'=>$cm->course));
|
|
|
|
if ($grade_items) {
|
|
|
|
foreach ($grade_items as $grade_item) {
|
|
|
|
$grade_item->set_hidden(!$visible);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 13:49:35 +10:00
|
|
|
\course_modinfo::purge_course_module_cache($cm->course, $cm->id);
|
|
|
|
rebuild_course_cache($cm->course, false, true);
|
2012-10-10 11:54:48 +08:00
|
|
|
return true;
|
2003-04-14 15:11:09 +00:00
|
|
|
}
|
|
|
|
|
2016-01-22 13:01:29 +08:00
|
|
|
/**
|
|
|
|
* Changes the course module name
|
|
|
|
*
|
|
|
|
* @param int $id course module id
|
|
|
|
* @param string $name new value for a name
|
|
|
|
* @return bool whether a change was made
|
|
|
|
*/
|
|
|
|
function set_coursemodule_name($id, $name) {
|
|
|
|
global $CFG, $DB;
|
|
|
|
require_once($CFG->libdir . '/gradelib.php');
|
|
|
|
|
|
|
|
$cm = get_coursemodule_from_id('', $id, 0, false, MUST_EXIST);
|
|
|
|
|
|
|
|
$module = new \stdClass();
|
|
|
|
$module->id = $cm->instance;
|
|
|
|
|
|
|
|
// Escape strings as they would be by mform.
|
|
|
|
if (!empty($CFG->formatstringstriptags)) {
|
|
|
|
$module->name = clean_param($name, PARAM_TEXT);
|
|
|
|
} else {
|
|
|
|
$module->name = clean_param($name, PARAM_CLEANHTML);
|
|
|
|
}
|
|
|
|
if ($module->name === $cm->name || strval($module->name) === '') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (\core_text::strlen($module->name) > 255) {
|
|
|
|
throw new \moodle_exception('maximumchars', 'moodle', '', 255);
|
|
|
|
}
|
|
|
|
|
|
|
|
$module->timemodified = time();
|
|
|
|
$DB->update_record($cm->modname, $module);
|
|
|
|
$cm->name = $module->name;
|
|
|
|
\core\event\course_module_updated::create_from_cm($cm)->trigger();
|
2021-07-16 13:49:35 +10:00
|
|
|
\course_modinfo::purge_course_module_cache($cm->course, $cm->id);
|
|
|
|
rebuild_course_cache($cm->course, false, true);
|
2016-01-22 13:01:29 +08:00
|
|
|
|
|
|
|
// Attempt to update the grade item if relevant.
|
|
|
|
$grademodule = $DB->get_record($cm->modname, array('id' => $cm->instance));
|
|
|
|
$grademodule->cmidnumber = $cm->idnumber;
|
|
|
|
$grademodule->modname = $cm->modname;
|
|
|
|
grade_update_mod_grades($grademodule);
|
|
|
|
|
2016-09-02 10:42:29 +08:00
|
|
|
// Update calendar events with the new name.
|
2017-07-04 09:21:17 +08:00
|
|
|
course_module_update_calendar_events($cm->modname, $grademodule, $cm);
|
2016-09-02 10:42:29 +08:00
|
|
|
|
2016-01-22 13:01:29 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-06-01 19:05:07 +00:00
|
|
|
/**
|
2015-03-30 17:35:11 -04:00
|
|
|
* This function will handle the whole deletion process of a module. This includes calling
|
2012-12-10 17:33:09 +08:00
|
|
|
* the modules delete_instance function, deleting files, events, grades, conditional data,
|
|
|
|
* the data in the course_module and course_sections table and adding a module deletion
|
|
|
|
* event to the DB.
|
2005-03-28 06:47:54 +00:00
|
|
|
*
|
2012-12-10 17:33:09 +08:00
|
|
|
* @param int $cmid the course module id
|
2016-11-02 15:07:55 +08:00
|
|
|
* @param bool $async whether or not to try to delete the module using an adhoc task. Async also depends on a plugin hook.
|
|
|
|
* @throws moodle_exception
|
2014-05-19 17:03:04 +01:00
|
|
|
* @since Moodle 2.5
|
2005-03-28 06:47:54 +00:00
|
|
|
*/
|
2016-11-02 15:07:55 +08:00
|
|
|
function course_delete_module($cmid, $async = false) {
|
|
|
|
// Check the 'course_module_background_deletion_recommended' hook first.
|
|
|
|
// Only use asynchronous deletion if at least one plugin returns true and if async deletion has been requested.
|
|
|
|
// Both are checked because plugins should not be allowed to dictate the deletion behaviour, only support/decline it.
|
|
|
|
// It's up to plugins to handle things like whether or not they are enabled.
|
|
|
|
if ($async && $pluginsfunction = get_plugins_with_function('course_module_background_deletion_recommended')) {
|
|
|
|
foreach ($pluginsfunction as $plugintype => $plugins) {
|
|
|
|
foreach ($plugins as $pluginfunction) {
|
|
|
|
if ($pluginfunction()) {
|
|
|
|
return course_module_flag_for_async_deletion($cmid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-16 13:42:21 +08:00
|
|
|
global $CFG, $DB;
|
2012-12-10 17:33:09 +08:00
|
|
|
|
2007-08-03 19:25:22 +00:00
|
|
|
require_once($CFG->libdir.'/gradelib.php');
|
2015-03-30 17:35:11 -04:00
|
|
|
require_once($CFG->libdir.'/questionlib.php');
|
2009-09-04 00:36:43 +00:00
|
|
|
require_once($CFG->dirroot.'/blog/lib.php');
|
2013-07-17 13:46:31 +08:00
|
|
|
require_once($CFG->dirroot.'/calendar/lib.php');
|
2007-08-03 19:25:22 +00:00
|
|
|
|
2012-12-10 17:33:09 +08:00
|
|
|
// Get the course module.
|
|
|
|
if (!$cm = $DB->get_record('course_modules', array('id' => $cmid))) {
|
2005-03-28 06:47:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-12-10 17:33:09 +08:00
|
|
|
|
|
|
|
// Get the module context.
|
|
|
|
$modcontext = context_module::instance($cm->id);
|
|
|
|
|
|
|
|
// Get the course module name.
|
|
|
|
$modulename = $DB->get_field('modules', 'name', array('id' => $cm->module), MUST_EXIST);
|
|
|
|
|
|
|
|
// Get the file location of the delete_instance function for this module.
|
|
|
|
$modlib = "$CFG->dirroot/mod/$modulename/lib.php";
|
|
|
|
|
|
|
|
// Include the file required to call the delete_instance function for this module.
|
|
|
|
if (file_exists($modlib)) {
|
|
|
|
require_once($modlib);
|
|
|
|
} else {
|
2013-02-05 15:19:42 +08:00
|
|
|
throw new moodle_exception('cannotdeletemodulemissinglib', '', '', null,
|
|
|
|
"Cannot delete this module as the file mod/$modulename/lib.php is missing.");
|
2012-12-10 17:33:09 +08:00
|
|
|
}
|
|
|
|
|
2013-02-05 15:19:42 +08:00
|
|
|
$deleteinstancefunction = $modulename . '_delete_instance';
|
2012-12-10 17:33:09 +08:00
|
|
|
|
2013-02-05 15:19:42 +08:00
|
|
|
// Ensure the delete_instance function exists for this module.
|
|
|
|
if (!function_exists($deleteinstancefunction)) {
|
|
|
|
throw new moodle_exception('cannotdeletemodulemissingfunc', '', '', null,
|
|
|
|
"Cannot delete this module as the function {$modulename}_delete_instance is missing in mod/$modulename/lib.php.");
|
|
|
|
}
|
|
|
|
|
2016-02-23 17:22:27 +08:00
|
|
|
// Allow plugins to use this course module before we completely delete it.
|
|
|
|
if ($pluginsfunction = get_plugins_with_function('pre_course_module_delete')) {
|
|
|
|
foreach ($pluginsfunction as $plugintype => $plugins) {
|
|
|
|
foreach ($plugins as $pluginfunction) {
|
|
|
|
$pluginfunction($cm);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-05 15:19:42 +08:00
|
|
|
// Call the delete_instance function, if it returns false throw an exception.
|
2012-12-10 17:33:09 +08:00
|
|
|
if (!$deleteinstancefunction($cm->instance)) {
|
2013-02-05 15:19:42 +08:00
|
|
|
throw new moodle_exception('cannotdeletemoduleinstance', '', '', null,
|
|
|
|
"Cannot delete the module $modulename (instance).");
|
2012-12-10 17:33:09 +08:00
|
|
|
}
|
|
|
|
|
2021-09-03 16:43:49 +10:00
|
|
|
question_delete_activity($cm);
|
|
|
|
|
2012-12-10 17:33:09 +08:00
|
|
|
// Remove all module files in case modules forget to do that.
|
|
|
|
$fs = get_file_storage();
|
|
|
|
$fs->delete_area_files($modcontext->id);
|
|
|
|
|
|
|
|
// Delete events from calendar.
|
|
|
|
if ($events = $DB->get_records('event', array('instance' => $cm->instance, 'modulename' => $modulename))) {
|
2017-06-22 13:12:57 +08:00
|
|
|
$coursecontext = context_course::instance($cm->course);
|
2004-05-05 07:07:56 +00:00
|
|
|
foreach($events as $event) {
|
2017-06-22 13:12:57 +08:00
|
|
|
$event->context = $coursecontext;
|
|
|
|
$calendarevent = calendar_event::load($event);
|
2013-07-17 13:46:31 +08:00
|
|
|
$calendarevent->delete();
|
2004-05-05 07:07:56 +00:00
|
|
|
}
|
|
|
|
}
|
2012-12-10 17:33:09 +08:00
|
|
|
|
|
|
|
// Delete grade items, outcome items and grades attached to modules.
|
|
|
|
if ($grade_items = grade_item::fetch_all(array('itemtype' => 'mod', 'itemmodule' => $modulename,
|
|
|
|
'iteminstance' => $cm->instance, 'courseid' => $cm->course))) {
|
2007-08-03 19:25:22 +00:00
|
|
|
foreach ($grade_items as $grade_item) {
|
|
|
|
$grade_item->delete('moddelete');
|
|
|
|
}
|
|
|
|
}
|
2012-12-10 17:33:09 +08:00
|
|
|
|
2018-08-30 14:45:09 +08:00
|
|
|
// Delete associated blogs and blog tag instances.
|
|
|
|
blog_remove_associations_for_module($modcontext->id);
|
|
|
|
|
2010-09-01 17:02:56 +00:00
|
|
|
// Delete completion and availability data; it is better to do this even if the
|
|
|
|
// features are not turned on, in case they were turned on previously (these will be
|
2012-12-10 17:33:09 +08:00
|
|
|
// very quick on an empty table).
|
2010-09-01 17:02:56 +00:00
|
|
|
$DB->delete_records('course_modules_completion', array('coursemoduleid' => $cm->id));
|
2022-04-26 16:46:26 +07:00
|
|
|
$DB->delete_records('course_modules_viewed', ['coursemoduleid' => $cm->id]);
|
2012-03-06 14:06:04 +00:00
|
|
|
$DB->delete_records('course_completion_criteria', array('moduleinstance' => $cm->id,
|
2016-06-02 15:42:16 +09:30
|
|
|
'course' => $cm->course,
|
2012-03-06 14:06:04 +00:00
|
|
|
'criteriatype' => COMPLETION_CRITERIA_TYPE_ACTIVITY));
|
2009-01-09 05:32:08 +00:00
|
|
|
|
2014-02-04 17:02:56 -08:00
|
|
|
// Delete all tag instances associated with the instance of this module.
|
2015-09-05 23:05:45 +08:00
|
|
|
core_tag_tag::delete_instances('mod_' . $modulename, null, $modcontext->id);
|
2016-02-21 16:10:47 +02:00
|
|
|
core_tag_tag::remove_all_item_tags('core', 'course_modules', $cm->id);
|
2014-01-12 18:41:18 -08:00
|
|
|
|
2016-04-11 18:21:11 +08:00
|
|
|
// Notify the competency subsystem.
|
|
|
|
\core_competency\api::hook_course_module_deleted($cm);
|
|
|
|
|
2012-12-10 17:33:09 +08:00
|
|
|
// Delete the context.
|
2013-07-05 15:20:34 +08:00
|
|
|
context_helper::delete_instance(CONTEXT_MODULE, $cm->id);
|
2012-12-10 17:33:09 +08:00
|
|
|
|
|
|
|
// Delete the module from the course_modules table.
|
|
|
|
$DB->delete_records('course_modules', array('id' => $cm->id));
|
|
|
|
|
|
|
|
// Delete module from that section.
|
|
|
|
if (!delete_mod_from_section($cm->id, $cm->section)) {
|
2013-02-05 15:19:42 +08:00
|
|
|
throw new moodle_exception('cannotdeletemodulefromsection', '', '', null,
|
|
|
|
"Cannot delete the module $modulename (instance) from section.");
|
2012-12-10 17:33:09 +08:00
|
|
|
}
|
|
|
|
|
2013-09-16 13:42:21 +08:00
|
|
|
// Trigger event for course module delete action.
|
|
|
|
$event = \core\event\course_module_deleted::create(array(
|
|
|
|
'courseid' => $cm->course,
|
|
|
|
'context' => $modcontext,
|
|
|
|
'objectid' => $cm->id,
|
|
|
|
'other' => array(
|
2021-07-16 13:49:35 +10:00
|
|
|
'modulename' => $modulename,
|
2013-09-16 13:42:21 +08:00
|
|
|
'instanceid' => $cm->instance,
|
|
|
|
)
|
|
|
|
));
|
|
|
|
$event->add_record_snapshot('course_modules', $cm);
|
|
|
|
$event->trigger();
|
2021-07-16 13:49:35 +10:00
|
|
|
\course_modinfo::purge_course_module_cache($cm->course, $cm->id);
|
|
|
|
rebuild_course_cache($cm->course, false, true);
|
2002-08-01 03:50:27 +00:00
|
|
|
}
|
|
|
|
|
2016-11-02 15:07:55 +08:00
|
|
|
/**
|
|
|
|
* Schedule a course module for deletion in the background using an adhoc task.
|
|
|
|
*
|
|
|
|
* This method should not be called directly. Instead, please use course_delete_module($cmid, true), to denote async deletion.
|
|
|
|
* The real deletion of the module is handled by the task, which calls 'course_delete_module($cmid)'.
|
|
|
|
*
|
|
|
|
* @param int $cmid the course module id.
|
|
|
|
* @return bool whether the module was successfully scheduled for deletion.
|
|
|
|
* @throws \moodle_exception
|
|
|
|
*/
|
|
|
|
function course_module_flag_for_async_deletion($cmid) {
|
2016-11-08 08:24:02 +08:00
|
|
|
global $CFG, $DB, $USER;
|
2016-11-02 15:07:55 +08:00
|
|
|
require_once($CFG->libdir.'/gradelib.php');
|
|
|
|
require_once($CFG->libdir.'/questionlib.php');
|
|
|
|
require_once($CFG->dirroot.'/blog/lib.php');
|
|
|
|
require_once($CFG->dirroot.'/calendar/lib.php');
|
|
|
|
|
|
|
|
// Get the course module.
|
|
|
|
if (!$cm = $DB->get_record('course_modules', array('id' => $cmid))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to be reasonably certain the deletion is going to succeed before we background the process.
|
|
|
|
// Make the necessary delete_instance checks, etc. before proceeding further. Throw exceptions if required.
|
|
|
|
|
|
|
|
// Get the course module name.
|
|
|
|
$modulename = $DB->get_field('modules', 'name', array('id' => $cm->module), MUST_EXIST);
|
|
|
|
|
|
|
|
// Get the file location of the delete_instance function for this module.
|
|
|
|
$modlib = "$CFG->dirroot/mod/$modulename/lib.php";
|
|
|
|
|
|
|
|
// Include the file required to call the delete_instance function for this module.
|
|
|
|
if (file_exists($modlib)) {
|
|
|
|
require_once($modlib);
|
|
|
|
} else {
|
|
|
|
throw new \moodle_exception('cannotdeletemodulemissinglib', '', '', null,
|
|
|
|
"Cannot delete this module as the file mod/$modulename/lib.php is missing.");
|
|
|
|
}
|
|
|
|
|
|
|
|
$deleteinstancefunction = $modulename . '_delete_instance';
|
|
|
|
|
|
|
|
// Ensure the delete_instance function exists for this module.
|
|
|
|
if (!function_exists($deleteinstancefunction)) {
|
|
|
|
throw new \moodle_exception('cannotdeletemodulemissingfunc', '', '', null,
|
|
|
|
"Cannot delete this module as the function {$modulename}_delete_instance is missing in mod/$modulename/lib.php.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// We are going to defer the deletion as we can't be sure how long the module's pre_delete code will run for.
|
|
|
|
$cm->deletioninprogress = '1';
|
|
|
|
$DB->update_record('course_modules', $cm);
|
|
|
|
|
|
|
|
// Create an adhoc task for the deletion of the course module. The task takes an array of course modules for removal.
|
|
|
|
$removaltask = new \core_course\task\course_delete_modules();
|
2016-11-08 08:24:02 +08:00
|
|
|
$removaltask->set_custom_data(array(
|
|
|
|
'cms' => array($cm),
|
|
|
|
'userid' => $USER->id,
|
|
|
|
'realuserid' => \core\session\manager::get_realuser()->id
|
|
|
|
));
|
2016-11-02 15:07:55 +08:00
|
|
|
|
|
|
|
// Queue the task for the next run.
|
|
|
|
\core\task\manager::queue_adhoc_task($removaltask);
|
|
|
|
|
|
|
|
// Reset the course cache to hide the module.
|
|
|
|
rebuild_course_cache($cm->course, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the given course has any course modules scheduled for adhoc deletion.
|
|
|
|
*
|
|
|
|
* @param int $courseid the id of the course.
|
2019-06-22 01:56:37 +10:00
|
|
|
* @param bool $onlygradable whether to check only gradable modules or all modules.
|
2016-11-02 15:07:55 +08:00
|
|
|
* @return bool true if the course contains any modules pending deletion, false otherwise.
|
|
|
|
*/
|
2019-06-22 01:56:37 +10:00
|
|
|
function course_modules_pending_deletion(int $courseid, bool $onlygradable = false) : bool {
|
2016-11-02 15:07:55 +08:00
|
|
|
if (empty($courseid)) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-06-22 01:56:37 +10:00
|
|
|
|
|
|
|
if ($onlygradable) {
|
|
|
|
// Fetch modules with grade items.
|
|
|
|
if (!$coursegradeitems = grade_item::fetch_all(['itemtype' => 'mod', 'courseid' => $courseid])) {
|
|
|
|
// Return early when there is none.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-02 15:07:55 +08:00
|
|
|
$modinfo = get_fast_modinfo($courseid);
|
|
|
|
foreach ($modinfo->get_cms() as $module) {
|
|
|
|
if ($module->deletioninprogress == '1') {
|
2019-06-22 01:56:37 +10:00
|
|
|
if ($onlygradable) {
|
|
|
|
// Check if the module being deleted is in the list of course modules with grade items.
|
|
|
|
foreach ($coursegradeitems as $coursegradeitem) {
|
|
|
|
if ($coursegradeitem->itemmodule == $module->modname && $coursegradeitem->iteminstance == $module->instance) {
|
|
|
|
// The module being deleted is within the gradable modules.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
2016-11-02 15:07:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks whether the course module, as defined by modulename and instanceid, is scheduled for deletion within the given course.
|
|
|
|
*
|
|
|
|
* @param int $courseid the course id.
|
|
|
|
* @param string $modulename the module name. E.g. 'assign', 'book', etc.
|
|
|
|
* @param int $instanceid the module instance id.
|
|
|
|
* @return bool true if the course module is pending deletion, false otherwise.
|
|
|
|
*/
|
|
|
|
function course_module_instance_pending_deletion($courseid, $modulename, $instanceid) {
|
|
|
|
if (empty($courseid) || empty($modulename) || empty($instanceid)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$modinfo = get_fast_modinfo($courseid);
|
|
|
|
$instances = $modinfo->get_instances_of($modulename);
|
|
|
|
return isset($instances[$instanceid]) && $instances[$instanceid]->deletioninprogress;
|
|
|
|
}
|
|
|
|
|
2012-09-24 17:08:02 +08:00
|
|
|
function delete_mod_from_section($modid, $sectionid) {
|
2008-06-01 19:05:07 +00:00
|
|
|
global $DB;
|
2002-08-01 03:50:27 +00:00
|
|
|
|
2012-09-24 17:08:02 +08:00
|
|
|
if ($section = $DB->get_record("course_sections", array("id"=>$sectionid)) ) {
|
2002-08-01 03:50:27 +00:00
|
|
|
|
2002-10-21 03:00:40 +00:00
|
|
|
$modarray = explode(",", $section->sequence);
|
2002-08-01 03:50:27 +00:00
|
|
|
|
2012-09-24 17:08:02 +08:00
|
|
|
if ($key = array_keys ($modarray, $modid)) {
|
2002-08-01 03:50:27 +00:00
|
|
|
array_splice($modarray, $key[0], 1);
|
|
|
|
$newsequence = implode(",", $modarray);
|
2012-10-10 11:54:48 +08:00
|
|
|
$DB->set_field("course_sections", "sequence", $newsequence, array("id"=>$section->id));
|
|
|
|
rebuild_course_cache($section->course, true);
|
|
|
|
return true;
|
2002-08-01 03:50:27 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2004-05-28 10:53:54 +00:00
|
|
|
|
2002-08-01 03:50:27 +00:00
|
|
|
}
|
2003-07-14 13:08:38 +00:00
|
|
|
return false;
|
2002-08-01 03:50:27 +00:00
|
|
|
}
|
|
|
|
|
2017-07-04 14:43:25 +08:00
|
|
|
/**
|
|
|
|
* This function updates the calendar events from the information stored in the module table and the course
|
|
|
|
* module table.
|
|
|
|
*
|
|
|
|
* @param string $modulename Module name
|
|
|
|
* @param stdClass $instance Module object. Either the $instance or the $cm must be supplied.
|
|
|
|
* @param stdClass $cm Course module object. Either the $instance or the $cm must be supplied.
|
|
|
|
* @return bool Returns true if calendar events are updated.
|
|
|
|
* @since Moodle 3.3.4
|
|
|
|
*/
|
|
|
|
function course_module_update_calendar_events($modulename, $instance = null, $cm = null) {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
if (isset($instance) || isset($cm)) {
|
|
|
|
|
|
|
|
if (!isset($instance)) {
|
|
|
|
$instance = $DB->get_record($modulename, array('id' => $cm->instance), '*', MUST_EXIST);
|
|
|
|
}
|
|
|
|
if (!isset($cm)) {
|
|
|
|
$cm = get_coursemodule_from_instance($modulename, $instance->id, $instance->course);
|
|
|
|
}
|
2017-10-25 15:42:14 +08:00
|
|
|
if (!empty($cm)) {
|
|
|
|
course_module_calendar_event_update_process($instance, $cm);
|
|
|
|
}
|
2017-07-04 14:43:25 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update all instances through out the site or in a course.
|
|
|
|
*
|
|
|
|
* @param string $modulename Module type to update.
|
|
|
|
* @param integer $courseid Course id to update events. 0 for the whole site.
|
|
|
|
* @return bool Returns True if the update was successful.
|
|
|
|
* @since Moodle 3.3.4
|
|
|
|
*/
|
|
|
|
function course_module_bulk_update_calendar_events($modulename, $courseid = 0) {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$instances = null;
|
|
|
|
if ($courseid) {
|
|
|
|
if (!$instances = $DB->get_records($modulename, array('course' => $courseid))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!$instances = $DB->get_records($modulename)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($instances as $instance) {
|
2017-10-25 15:42:14 +08:00
|
|
|
if ($cm = get_coursemodule_from_instance($modulename, $instance->id, $instance->course)) {
|
|
|
|
course_module_calendar_event_update_process($instance, $cm);
|
|
|
|
}
|
2017-07-04 14:43:25 +08:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calendar events for a module instance are updated.
|
|
|
|
*
|
|
|
|
* @param stdClass $instance Module instance object.
|
|
|
|
* @param stdClass $cm Course Module object.
|
|
|
|
* @since Moodle 3.3.4
|
|
|
|
*/
|
|
|
|
function course_module_calendar_event_update_process($instance, $cm) {
|
|
|
|
// We need to call *_refresh_events() first because some modules delete 'old' events at the end of the code which
|
|
|
|
// will remove the completion events.
|
|
|
|
$refresheventsfunction = $cm->modname . '_refresh_events';
|
|
|
|
if (function_exists($refresheventsfunction)) {
|
|
|
|
call_user_func($refresheventsfunction, $cm->course, $instance, $cm);
|
|
|
|
}
|
|
|
|
$completionexpected = (!empty($cm->completionexpected)) ? $cm->completionexpected : null;
|
|
|
|
\core_completion\api::update_completion_date_event($cm->id, $cm->modname, $instance, $completionexpected);
|
|
|
|
}
|
|
|
|
|
2008-08-08 05:25:54 +00:00
|
|
|
/**
|
|
|
|
* Moves a section within a course, from a position to another.
|
|
|
|
* Be very careful: $section and $destination refer to section number,
|
|
|
|
* not id!.
|
|
|
|
*
|
|
|
|
* @param object $course
|
|
|
|
* @param int $section Section number (not id!!!)
|
|
|
|
* @param int $destination
|
2015-01-15 17:13:56 +08:00
|
|
|
* @param bool $ignorenumsections
|
2008-08-08 05:25:54 +00:00
|
|
|
* @return boolean Result
|
|
|
|
*/
|
2015-01-15 17:13:56 +08:00
|
|
|
function move_section_to($course, $section, $destination, $ignorenumsections = false) {
|
2008-08-08 05:25:54 +00:00
|
|
|
/// Moves a whole course section up and down within the course
|
|
|
|
global $USER, $DB;
|
|
|
|
|
2008-10-08 12:22:04 +00:00
|
|
|
if (!$destination && $destination != 0) {
|
2008-08-08 05:25:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-11-02 14:32:17 +08:00
|
|
|
// compartibility with course formats using field 'numsections'
|
|
|
|
$courseformatoptions = course_get_format($course)->get_format_options();
|
2015-01-15 17:13:56 +08:00
|
|
|
if ((!$ignorenumsections && array_key_exists('numsections', $courseformatoptions) &&
|
2012-11-02 14:32:17 +08:00
|
|
|
($destination > $courseformatoptions['numsections'])) || ($destination < 1)) {
|
2008-08-08 05:25:54 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all sections for this course and re-order them (2 of them should now share the same section number)
|
|
|
|
if (!$sections = $DB->get_records_menu('course_sections', array('course' => $course->id),
|
|
|
|
'section ASC, id ASC', 'id, section')) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-04-13 17:51:20 +01:00
|
|
|
$movedsections = reorder_sections($sections, $section, $destination);
|
2008-08-08 05:25:54 +00:00
|
|
|
|
2012-04-13 17:51:20 +01:00
|
|
|
// Update all sections. Do this in 2 steps to avoid breaking database
|
|
|
|
// uniqueness constraint
|
|
|
|
$transaction = $DB->start_delegated_transaction();
|
|
|
|
foreach ($movedsections as $id => $position) {
|
2022-01-18 10:29:42 +07:00
|
|
|
if ((int) $sections[$id] !== $position) {
|
2021-07-16 13:49:35 +10:00
|
|
|
$DB->set_field('course_sections', 'section', -$position, ['id' => $id]);
|
|
|
|
// Invalidate the section cache by given section id.
|
|
|
|
course_modinfo::purge_course_section_cache_by_id($course->id, $id);
|
2012-04-13 17:51:20 +01:00
|
|
|
}
|
|
|
|
}
|
2012-04-23 12:20:57 +08:00
|
|
|
foreach ($movedsections as $id => $position) {
|
2022-01-18 10:29:42 +07:00
|
|
|
if ((int) $sections[$id] !== $position) {
|
2021-07-16 13:49:35 +10:00
|
|
|
$DB->set_field('course_sections', 'section', $position, ['id' => $id]);
|
|
|
|
// Invalidate the section cache by given section id.
|
|
|
|
course_modinfo::purge_course_section_cache_by_id($course->id, $id);
|
2012-04-23 12:20:57 +08:00
|
|
|
}
|
2008-08-08 05:25:54 +00:00
|
|
|
}
|
|
|
|
|
2012-01-18 10:53:23 +00:00
|
|
|
// If we move the highlighted section itself, then just highlight the destination.
|
|
|
|
// Adjust the higlighted section location if we move something over it either direction.
|
|
|
|
if ($section == $course->marker) {
|
2012-05-09 11:25:08 +01:00
|
|
|
course_set_marker($course->id, $destination);
|
2021-07-16 13:49:35 +10:00
|
|
|
} else if ($section > $course->marker && $course->marker >= $destination) {
|
2012-05-09 11:25:08 +01:00
|
|
|
course_set_marker($course->id, $course->marker+1);
|
2021-07-16 13:49:35 +10:00
|
|
|
} else if ($section < $course->marker && $course->marker <= $destination) {
|
2012-05-09 11:25:08 +01:00
|
|
|
course_set_marker($course->id, $course->marker-1);
|
2012-01-18 10:53:23 +00:00
|
|
|
}
|
|
|
|
|
2012-04-13 17:51:20 +01:00
|
|
|
$transaction->allow_commit();
|
2021-07-16 13:49:35 +10:00
|
|
|
rebuild_course_cache($course->id, true, true);
|
2008-08-08 05:25:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-15 17:13:56 +08:00
|
|
|
/**
|
|
|
|
* This method will delete a course section and may delete all modules inside it.
|
|
|
|
*
|
|
|
|
* No permissions are checked here, use {@link course_can_delete_section()} to
|
|
|
|
* check if section can actually be deleted.
|
|
|
|
*
|
|
|
|
* @param int|stdClass $course
|
|
|
|
* @param int|stdClass|section_info $section
|
|
|
|
* @param bool $forcedeleteifnotempty if set to false section will not be deleted if it has modules in it.
|
2016-11-02 15:07:55 +08:00
|
|
|
* @param bool $async whether or not to try to delete the section using an adhoc task. Async also depends on a plugin hook.
|
2015-01-15 17:13:56 +08:00
|
|
|
* @return bool whether section was deleted
|
|
|
|
*/
|
2016-11-02 15:07:55 +08:00
|
|
|
function course_delete_section($course, $section, $forcedeleteifnotempty = true, $async = false) {
|
2015-10-19 12:19:37 +01:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
// Prepare variables.
|
|
|
|
$courseid = (is_object($course)) ? $course->id : (int)$course;
|
|
|
|
$sectionnum = (is_object($section)) ? $section->section : (int)$section;
|
|
|
|
$section = $DB->get_record('course_sections', array('course' => $courseid, 'section' => $sectionnum));
|
|
|
|
if (!$section) {
|
|
|
|
// No section exists, can't proceed.
|
|
|
|
return false;
|
|
|
|
}
|
2016-11-02 15:07:55 +08:00
|
|
|
|
|
|
|
// Check the 'course_module_background_deletion_recommended' hook first.
|
|
|
|
// Only use asynchronous deletion if at least one plugin returns true and if async deletion has been requested.
|
|
|
|
// Both are checked because plugins should not be allowed to dictate the deletion behaviour, only support/decline it.
|
|
|
|
// It's up to plugins to handle things like whether or not they are enabled.
|
|
|
|
if ($async && $pluginsfunction = get_plugins_with_function('course_module_background_deletion_recommended')) {
|
|
|
|
foreach ($pluginsfunction as $plugintype => $plugins) {
|
|
|
|
foreach ($plugins as $pluginfunction) {
|
|
|
|
if ($pluginfunction()) {
|
|
|
|
return course_delete_section_async($section, $forcedeleteifnotempty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-19 12:19:37 +01:00
|
|
|
$format = course_get_format($course);
|
|
|
|
$sectionname = $format->get_section_name($section);
|
|
|
|
|
|
|
|
// Delete section.
|
|
|
|
$result = $format->delete_section($section, $forcedeleteifnotempty);
|
|
|
|
|
|
|
|
// Trigger an event for course section deletion.
|
|
|
|
if ($result) {
|
|
|
|
$context = context_course::instance($courseid);
|
|
|
|
$event = \core\event\course_section_deleted::create(
|
2016-11-02 15:07:55 +08:00
|
|
|
array(
|
|
|
|
'objectid' => $section->id,
|
|
|
|
'courseid' => $courseid,
|
|
|
|
'context' => $context,
|
|
|
|
'other' => array(
|
|
|
|
'sectionnum' => $section->section,
|
|
|
|
'sectionname' => $sectionname,
|
2015-10-19 12:19:37 +01:00
|
|
|
)
|
2016-11-02 15:07:55 +08:00
|
|
|
)
|
|
|
|
);
|
2015-10-19 12:19:37 +01:00
|
|
|
$event->add_record_snapshot('course_sections', $section);
|
|
|
|
$event->trigger();
|
|
|
|
}
|
|
|
|
return $result;
|
2015-01-15 17:13:56 +08:00
|
|
|
}
|
|
|
|
|
2016-11-02 15:07:55 +08:00
|
|
|
/**
|
|
|
|
* Course section deletion, using an adhoc task for deletion of the modules it contains.
|
|
|
|
* 1. Schedule all modules within the section for adhoc removal.
|
|
|
|
* 2. Move all modules to course section 0.
|
|
|
|
* 3. Delete the resulting empty section.
|
|
|
|
*
|
|
|
|
* @param \stdClass $section the section to schedule for deletion.
|
|
|
|
* @param bool $forcedeleteifnotempty whether to force section deletion if it contains modules.
|
|
|
|
* @return bool true if the section was scheduled for deletion, false otherwise.
|
|
|
|
*/
|
|
|
|
function course_delete_section_async($section, $forcedeleteifnotempty = true) {
|
2016-11-08 08:24:02 +08:00
|
|
|
global $DB, $USER;
|
2016-11-02 15:07:55 +08:00
|
|
|
|
|
|
|
// Objects only, and only valid ones.
|
|
|
|
if (!is_object($section) || empty($section->id)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Does the object currently exist in the DB for removal (check for stale objects).
|
|
|
|
$section = $DB->get_record('course_sections', array('id' => $section->id));
|
|
|
|
if (!$section || !$section->section) {
|
|
|
|
// No section exists, or the section is 0. Can't proceed.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check whether the section can be removed.
|
|
|
|
if (!$forcedeleteifnotempty && (!empty($section->sequence) || !empty($section->summary))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$format = course_get_format($section->course);
|
|
|
|
$sectionname = $format->get_section_name($section);
|
|
|
|
|
|
|
|
// Flag those modules having no existing deletion flag. Some modules may have been scheduled for deletion manually, and we don't
|
|
|
|
// want to create additional adhoc deletion tasks for these. Moving them to section 0 will suffice.
|
|
|
|
$affectedmods = $DB->get_records_select('course_modules', 'course = ? AND section = ? AND deletioninprogress <> ?',
|
|
|
|
[$section->course, $section->id, 1], '', 'id');
|
|
|
|
$DB->set_field('course_modules', 'deletioninprogress', '1', ['course' => $section->course, 'section' => $section->id]);
|
|
|
|
|
|
|
|
// Move all modules to section 0.
|
|
|
|
$modules = $DB->get_records('course_modules', ['section' => $section->id], '');
|
|
|
|
$sectionzero = $DB->get_record('course_sections', ['course' => $section->course, 'section' => '0']);
|
|
|
|
foreach ($modules as $mod) {
|
|
|
|
moveto_module($mod, $sectionzero);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create and queue an adhoc task for the deletion of the modules.
|
|
|
|
$removaltask = new \core_course\task\course_delete_modules();
|
|
|
|
$data = array(
|
2016-11-08 08:24:02 +08:00
|
|
|
'cms' => $affectedmods,
|
|
|
|
'userid' => $USER->id,
|
|
|
|
'realuserid' => \core\session\manager::get_realuser()->id
|
2016-11-02 15:07:55 +08:00
|
|
|
);
|
|
|
|
$removaltask->set_custom_data($data);
|
|
|
|
\core\task\manager::queue_adhoc_task($removaltask);
|
|
|
|
|
|
|
|
// Delete the now empty section, passing in only the section number, which forces the function to fetch a new object.
|
|
|
|
// The refresh is needed because the section->sequence is now stale.
|
|
|
|
$result = $format->delete_section($section->section, $forcedeleteifnotempty);
|
|
|
|
|
|
|
|
// Trigger an event for course section deletion.
|
|
|
|
if ($result) {
|
|
|
|
$context = \context_course::instance($section->course);
|
|
|
|
$event = \core\event\course_section_deleted::create(
|
|
|
|
array(
|
|
|
|
'objectid' => $section->id,
|
|
|
|
'courseid' => $section->course,
|
|
|
|
'context' => $context,
|
|
|
|
'other' => array(
|
|
|
|
'sectionnum' => $section->section,
|
|
|
|
'sectionname' => $sectionname,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$event->add_record_snapshot('course_sections', $section);
|
|
|
|
$event->trigger();
|
|
|
|
}
|
|
|
|
rebuild_course_cache($section->course, true);
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2016-02-03 17:28:56 +08:00
|
|
|
/**
|
|
|
|
* Updates the course section
|
|
|
|
*
|
|
|
|
* This function does not check permissions or clean values - this has to be done prior to calling it.
|
|
|
|
*
|
|
|
|
* @param int|stdClass $course
|
|
|
|
* @param stdClass $section record from course_sections table - it will be updated with the new values
|
|
|
|
* @param array|stdClass $data
|
|
|
|
*/
|
|
|
|
function course_update_section($course, $section, $data) {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
$courseid = (is_object($course)) ? $course->id : (int)$course;
|
|
|
|
|
|
|
|
// Some fields can not be updated using this method.
|
|
|
|
$data = array_diff_key((array)$data, array('id', 'course', 'section', 'sequence'));
|
|
|
|
$changevisibility = (array_key_exists('visible', $data) && (bool)$data['visible'] != (bool)$section->visible);
|
|
|
|
if (array_key_exists('name', $data) && \core_text::strlen($data['name']) > 255) {
|
|
|
|
throw new moodle_exception('maximumchars', 'moodle', '', 255);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update record in the DB and course format options.
|
|
|
|
$data['id'] = $section->id;
|
2017-03-31 19:58:40 +05:30
|
|
|
$data['timemodified'] = time();
|
2016-02-03 17:28:56 +08:00
|
|
|
$DB->update_record('course_sections', $data);
|
2021-07-16 13:49:35 +10:00
|
|
|
// Invalidate the section cache by given section id.
|
|
|
|
course_modinfo::purge_course_section_cache_by_id($courseid, $section->id);
|
|
|
|
rebuild_course_cache($courseid, false, true);
|
2016-02-03 17:28:56 +08:00
|
|
|
course_get_format($courseid)->update_section_format_options($data);
|
|
|
|
|
|
|
|
// Update fields of the $section object.
|
|
|
|
foreach ($data as $key => $value) {
|
|
|
|
if (property_exists($section, $key)) {
|
|
|
|
$section->$key = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trigger an event for course section update.
|
|
|
|
$event = \core\event\course_section_updated::create(
|
|
|
|
array(
|
|
|
|
'objectid' => $section->id,
|
|
|
|
'courseid' => $courseid,
|
|
|
|
'context' => context_course::instance($courseid),
|
|
|
|
'other' => array('sectionnum' => $section->section)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$event->trigger();
|
|
|
|
|
|
|
|
// If section visibility was changed, hide the modules in this section too.
|
|
|
|
if ($changevisibility && !empty($section->sequence)) {
|
|
|
|
$modules = explode(',', $section->sequence);
|
|
|
|
foreach ($modules as $moduleid) {
|
|
|
|
if ($cm = get_coursemodule_from_id(null, $moduleid, $courseid)) {
|
|
|
|
if ($data['visible']) {
|
|
|
|
// As we unhide the section, we use the previously saved visibility stored in visibleold.
|
2017-02-03 09:40:46 +08:00
|
|
|
set_coursemodule_visible($moduleid, $cm->visibleold, $cm->visibleoncoursepage);
|
2016-02-03 17:28:56 +08:00
|
|
|
} else {
|
|
|
|
// We hide the section, so we hide the module but we store the original state in visibleold.
|
2017-02-03 09:40:46 +08:00
|
|
|
set_coursemodule_visible($moduleid, 0, $cm->visibleoncoursepage);
|
2021-07-16 13:49:35 +10:00
|
|
|
$DB->set_field('course_modules', 'visibleold', $cm->visible, ['id' => $moduleid]);
|
|
|
|
\course_modinfo::purge_course_module_cache($cm->course, $cm->id);
|
2016-02-03 17:28:56 +08:00
|
|
|
}
|
|
|
|
\core\event\course_module_updated::create_from_cm($cm)->trigger();
|
|
|
|
}
|
|
|
|
}
|
2021-07-16 13:49:35 +10:00
|
|
|
rebuild_course_cache($courseid, false, true);
|
2016-02-03 17:28:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-15 17:13:56 +08:00
|
|
|
/**
|
|
|
|
* Checks if the current user can delete a section (if course format allows it and user has proper permissions).
|
|
|
|
*
|
|
|
|
* @param int|stdClass $course
|
|
|
|
* @param int|stdClass|section_info $section
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function course_can_delete_section($course, $section) {
|
|
|
|
if (is_object($section)) {
|
|
|
|
$section = $section->section;
|
|
|
|
}
|
|
|
|
if (!$section) {
|
|
|
|
// Not possible to delete 0-section.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Course format should allow to delete sections.
|
|
|
|
if (!course_get_format($course)->can_delete_section($section)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Make sure user has capability to update course and move sections.
|
|
|
|
$context = context_course::instance(is_object($course) ? $course->id : $course);
|
|
|
|
if (!has_all_capabilities(array('moodle/course:movesections', 'moodle/course:update'), $context)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Make sure user has capability to delete each activity in this section.
|
|
|
|
$modinfo = get_fast_modinfo($course);
|
|
|
|
if (!empty($modinfo->sections[$section])) {
|
|
|
|
foreach ($modinfo->sections[$section] as $cmid) {
|
|
|
|
if (!has_capability('moodle/course:manageactivities', context_module::instance($cmid))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-08-08 05:25:54 +00:00
|
|
|
/**
|
|
|
|
* Reordering algorithm for course sections. Given an array of section->section indexed by section->id,
|
|
|
|
* an original position number and a target position number, rebuilds the array so that the
|
|
|
|
* move is made without any duplication of section positions.
|
|
|
|
* Note: The target_position is the position AFTER WHICH the moved section will be inserted. If you want to
|
|
|
|
* insert a section before the first one, you must give 0 as the target (section 0 can never be moved).
|
|
|
|
*
|
|
|
|
* @param array $sections
|
|
|
|
* @param int $origin_position
|
|
|
|
* @param int $target_position
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function reorder_sections($sections, $origin_position, $target_position) {
|
|
|
|
if (!is_array($sections)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can't move section position 0
|
|
|
|
if ($origin_position < 1) {
|
|
|
|
echo "We can't move section position 0";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Locate origin section in sections array
|
|
|
|
if (!$origin_key = array_search($origin_position, $sections)) {
|
|
|
|
echo "searched position not in sections array";
|
|
|
|
return false; // searched position not in sections array
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract origin section
|
|
|
|
$origin_section = $sections[$origin_key];
|
|
|
|
unset($sections[$origin_key]);
|
|
|
|
|
|
|
|
// Find offset of target position (stupid PHP's array_splice requires offset instead of key index!)
|
|
|
|
$found = false;
|
|
|
|
$append_array = array();
|
|
|
|
foreach ($sections as $id => $position) {
|
|
|
|
if ($found) {
|
|
|
|
$append_array[$id] = $position;
|
|
|
|
unset($sections[$id]);
|
|
|
|
}
|
|
|
|
if ($position == $target_position) {
|
2012-05-28 10:11:25 +08:00
|
|
|
if ($target_position < $origin_position) {
|
|
|
|
$append_array[$id] = $position;
|
|
|
|
unset($sections[$id]);
|
|
|
|
}
|
2008-08-08 05:25:54 +00:00
|
|
|
$found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append moved section
|
|
|
|
$sections[$origin_key] = $origin_section;
|
|
|
|
|
|
|
|
// Append rest of array (if applicable)
|
|
|
|
if (!empty($append_array)) {
|
|
|
|
foreach ($append_array as $id => $position) {
|
|
|
|
$sections[$id] = $position;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Renumber positions
|
|
|
|
$position = 0;
|
|
|
|
foreach ($sections as $id => $p) {
|
|
|
|
$sections[$id] = $position;
|
|
|
|
$position++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sections;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2008-06-01 19:05:07 +00:00
|
|
|
/**
|
|
|
|
* Move the module object $mod to the specified $section
|
|
|
|
* If $beforemod exists then that is the module
|
|
|
|
* before which $modid should be inserted
|
2013-08-27 15:36:00 +10:00
|
|
|
*
|
|
|
|
* @param stdClass|cm_info $mod
|
|
|
|
* @param stdClass|section_info $section
|
|
|
|
* @param int|stdClass $beforemod id or object with field id corresponding to the module
|
|
|
|
* before which the module needs to be included. Null for inserting in the
|
|
|
|
* end of the section
|
|
|
|
* @return int new value for module visibility (0 or 1)
|
2008-06-01 19:05:07 +00:00
|
|
|
*/
|
2003-07-14 13:08:38 +00:00
|
|
|
function moveto_module($mod, $section, $beforemod=NULL) {
|
2013-01-09 13:32:48 +08:00
|
|
|
global $OUTPUT, $DB;
|
2003-07-14 13:08:38 +00:00
|
|
|
|
2013-08-27 15:36:00 +10:00
|
|
|
// Current module visibility state - return value of this function.
|
|
|
|
$modvisible = $mod->visible;
|
|
|
|
|
|
|
|
// Remove original module from original section.
|
2003-07-14 13:08:38 +00:00
|
|
|
if (! delete_mod_from_section($mod->id, $mod->section)) {
|
2009-08-18 04:58:26 +00:00
|
|
|
echo $OUTPUT->notification("Could not delete module from existing section");
|
2003-07-14 13:08:38 +00:00
|
|
|
}
|
|
|
|
|
2021-12-07 14:44:53 +01:00
|
|
|
// Add the module into the new section.
|
|
|
|
course_add_cm_to_section($section->course, $mod->id, $section->section, $beforemod);
|
|
|
|
|
2013-08-27 15:36:00 +10:00
|
|
|
// If moving to a hidden section then hide module.
|
2013-03-25 12:59:50 -07:00
|
|
|
if ($mod->section != $section->id) {
|
|
|
|
if (!$section->visible && $mod->visible) {
|
2013-08-27 15:36:00 +10:00
|
|
|
// Module was visible but must become hidden after moving to hidden section.
|
|
|
|
$modvisible = 0;
|
2013-03-25 12:59:50 -07:00
|
|
|
set_coursemodule_visible($mod->id, 0);
|
|
|
|
// Set visibleold to 1 so module will be visible when section is made visible.
|
|
|
|
$DB->set_field('course_modules', 'visibleold', 1, array('id' => $mod->id));
|
|
|
|
}
|
|
|
|
if ($section->visible && !$mod->visible) {
|
2013-08-27 15:36:00 +10:00
|
|
|
// Hidden module was moved to the visible section, restore the module visibility from visibleold.
|
2013-03-25 12:59:50 -07:00
|
|
|
set_coursemodule_visible($mod->id, $mod->visibleold);
|
2013-08-27 15:36:00 +10:00
|
|
|
$modvisible = $mod->visibleold;
|
2013-03-25 12:59:50 -07:00
|
|
|
}
|
2003-07-14 13:08:38 +00:00
|
|
|
}
|
|
|
|
|
2013-08-27 15:36:00 +10:00
|
|
|
return $modvisible;
|
2003-07-14 13:08:38 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 13:22:51 +08:00
|
|
|
/**
|
|
|
|
* Returns the list of all editing actions that current user can perform on the module
|
|
|
|
*
|
|
|
|
* @param cm_info $mod The module to produce editing buttons for
|
|
|
|
* @param int $indent The current indenting (default -1 means no move left-right actions)
|
|
|
|
* @param int $sr The section to link back to (used for creating the links)
|
|
|
|
* @return array array of action_link or pix_icon objects
|
|
|
|
*/
|
|
|
|
function course_get_cm_edit_actions(cm_info $mod, $indent = -1, $sr = null) {
|
2017-02-03 09:40:46 +08:00
|
|
|
global $COURSE, $SITE, $CFG;
|
2002-08-03 08:16:31 +00:00
|
|
|
|
2004-01-31 08:40:38 +00:00
|
|
|
static $str;
|
|
|
|
|
2012-07-23 15:59:04 +08:00
|
|
|
$coursecontext = context_course::instance($mod->course);
|
|
|
|
$modcontext = context_module::instance($mod->id);
|
2017-02-03 09:40:46 +08:00
|
|
|
$courseformat = course_get_format($mod->get_course());
|
2022-04-12 15:53:29 +02:00
|
|
|
$usecomponents = $courseformat->supports_components();
|
2011-08-25 18:02:35 +12:00
|
|
|
|
2011-12-18 11:54:02 +01:00
|
|
|
$editcaps = array('moodle/course:manageactivities', 'moodle/course:activityvisibility', 'moodle/role:assign');
|
|
|
|
$dupecaps = array('moodle/backup:backuptargetimport', 'moodle/restore:restoretargetimport');
|
|
|
|
|
2013-07-08 11:44:32 +12:00
|
|
|
// No permission to edit anything.
|
2011-12-18 11:54:02 +01:00
|
|
|
if (!has_any_capability($editcaps, $modcontext) and !has_all_capabilities($dupecaps, $coursecontext)) {
|
2012-12-11 13:22:51 +08:00
|
|
|
return array();
|
2007-02-16 07:57:19 +00:00
|
|
|
}
|
|
|
|
|
2011-12-18 11:54:02 +01:00
|
|
|
$hasmanageactivities = has_capability('moodle/course:manageactivities', $modcontext);
|
|
|
|
|
2004-01-31 08:40:38 +00:00
|
|
|
if (!isset($str)) {
|
2012-12-11 13:22:51 +08:00
|
|
|
$str = get_strings(array('delete', 'move', 'moveright', 'moveleft',
|
MDL-57961 course: change wording of module availability
renamed Visible to Availability and added help strings
AMOS BEGIN
CPY [availability,core_moodle],[modvisible,core_moodle]
CPY [availability,core_moodle],[modvisiblewithstealth,core_moodle]
CPY [availability,core_moodle],[modvisiblehiddensection,core_moodle]
CPY [show,core_moodle],[modshow,core_moodle]
CPY [hide,core_moodle],[modhide,core_moodle]
AMOS END
2017-03-20 16:18:04 +08:00
|
|
|
'editsettings', 'duplicate', 'modhide', 'makeavailable', 'makeunavailable', 'modshow'), 'moodle');
|
2012-12-11 13:22:51 +08:00
|
|
|
$str->assign = get_string('assignroles', 'role');
|
2012-01-06 08:58:56 +00:00
|
|
|
$str->groupsnone = get_string('clicktochangeinbrackets', 'moodle', get_string("groupsnone"));
|
|
|
|
$str->groupsseparate = get_string('clicktochangeinbrackets', 'moodle', get_string("groupsseparate"));
|
|
|
|
$str->groupsvisible = get_string('clicktochangeinbrackets', 'moodle', get_string("groupsvisible"));
|
2003-04-14 15:11:09 +00:00
|
|
|
}
|
2002-08-03 08:16:31 +00:00
|
|
|
|
2011-12-18 11:54:02 +01:00
|
|
|
$baseurl = new moodle_url('/course/mod.php', array('sesskey' => sesskey()));
|
2005-03-05 05:19:26 +00:00
|
|
|
|
2012-12-11 13:22:51 +08:00
|
|
|
if ($sr !== null) {
|
|
|
|
$baseurl->param('sr', $sr);
|
2011-08-25 18:02:35 +12:00
|
|
|
}
|
|
|
|
$actions = array();
|
|
|
|
|
2013-10-25 14:36:46 +08:00
|
|
|
// Update.
|
|
|
|
if ($hasmanageactivities) {
|
|
|
|
$actions['update'] = new action_menu_link_secondary(
|
2012-05-08 12:13:19 +01:00
|
|
|
new moodle_url($baseurl, array('update' => $mod->id)),
|
2019-02-11 09:34:18 +08:00
|
|
|
new pix_icon('t/edit', '', 'moodle', array('class' => 'iconsmall')),
|
2013-10-25 14:36:46 +08:00
|
|
|
$str->editsettings,
|
|
|
|
array('class' => 'editing_update', 'data-action' => 'update')
|
2012-05-08 12:13:19 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-30 18:06:56 +02:00
|
|
|
// Move (only for component compatible formats).
|
2022-04-12 15:53:29 +02:00
|
|
|
if ($usecomponents) {
|
2021-06-30 18:06:56 +02:00
|
|
|
$actions['move'] = new action_menu_link_secondary(
|
|
|
|
new moodle_url($baseurl, [
|
|
|
|
'sesskey' => sesskey(),
|
|
|
|
'copy' => $mod->id,
|
|
|
|
]),
|
|
|
|
new pix_icon('i/dragdrop', '', 'moodle', ['class' => 'iconsmall']),
|
|
|
|
$str->move,
|
|
|
|
[
|
|
|
|
'class' => 'editing_movecm',
|
|
|
|
'data-action' => 'moveCm',
|
|
|
|
'data-id' => $mod->id,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-07-08 11:44:32 +12:00
|
|
|
// Indent.
|
2014-01-09 17:01:58 +08:00
|
|
|
if ($hasmanageactivities && $indent >= 0) {
|
2013-10-25 14:36:46 +08:00
|
|
|
$indentlimits = new stdClass();
|
|
|
|
$indentlimits->min = 0;
|
2023-02-08 17:04:04 +01:00
|
|
|
// Legacy indentation could continue using a limit of 16,
|
|
|
|
// but components based formats will be forced to use one level indentation only.
|
|
|
|
$indentlimits->max = ($usecomponents) ? 1 : 16;
|
2011-08-25 18:02:35 +12:00
|
|
|
if (right_to_left()) { // Exchange arrows on RTL
|
|
|
|
$rightarrow = 't/left';
|
|
|
|
$leftarrow = 't/right';
|
|
|
|
} else {
|
|
|
|
$rightarrow = 't/right';
|
|
|
|
$leftarrow = 't/left';
|
|
|
|
}
|
|
|
|
|
2013-10-25 14:36:46 +08:00
|
|
|
if ($indent >= $indentlimits->max) {
|
|
|
|
$enabledclass = 'hidden';
|
|
|
|
} else {
|
|
|
|
$enabledclass = '';
|
|
|
|
}
|
|
|
|
$actions['moveright'] = new action_menu_link_secondary(
|
2023-02-08 17:04:04 +01:00
|
|
|
new moodle_url($baseurl, ['id' => $mod->id, 'indent' => '1']),
|
|
|
|
new pix_icon($rightarrow, '', 'moodle', ['class' => 'iconsmall']),
|
2013-10-25 14:36:46 +08:00
|
|
|
$str->moveright,
|
2023-02-08 17:04:04 +01:00
|
|
|
[
|
|
|
|
'class' => 'editing_moveright ' . $enabledclass,
|
|
|
|
'data-action' => ($usecomponents) ? 'cmMoveRight' : 'moveright',
|
|
|
|
'data-keepopen' => true,
|
|
|
|
'data-sectionreturn' => $sr,
|
|
|
|
'data-id' => $mod->id,
|
|
|
|
]
|
2013-10-25 14:36:46 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($indent <= $indentlimits->min) {
|
|
|
|
$enabledclass = 'hidden';
|
|
|
|
} else {
|
|
|
|
$enabledclass = '';
|
2011-08-25 18:02:35 +12:00
|
|
|
}
|
2013-07-18 10:42:11 +12:00
|
|
|
$actions['moveleft'] = new action_menu_link_secondary(
|
2023-02-08 17:04:04 +01:00
|
|
|
new moodle_url($baseurl, ['id' => $mod->id, 'indent' => '0']),
|
|
|
|
new pix_icon($leftarrow, '', 'moodle', ['class' => 'iconsmall']),
|
2013-07-05 10:45:05 +12:00
|
|
|
$str->moveleft,
|
2023-02-08 17:04:04 +01:00
|
|
|
[
|
|
|
|
'class' => 'editing_moveleft ' . $enabledclass,
|
|
|
|
'data-action' => ($usecomponents) ? 'cmMoveLeft' : 'moveleft',
|
|
|
|
'data-keepopen' => true,
|
|
|
|
'data-sectionreturn' => $sr,
|
|
|
|
'data-id' => $mod->id,
|
|
|
|
]
|
2013-07-05 10:45:05 +12:00
|
|
|
);
|
2013-10-25 14:36:46 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-03 09:40:46 +08:00
|
|
|
// Hide/Show/Available/Unavailable.
|
2013-10-25 14:36:46 +08:00
|
|
|
if (has_capability('moodle/course:activityvisibility', $modcontext)) {
|
2017-02-03 09:40:46 +08:00
|
|
|
$allowstealth = !empty($CFG->allowstealth) && $courseformat->allow_stealth_module_visibility($mod, $mod->get_section_info());
|
|
|
|
|
|
|
|
$sectionvisible = $mod->get_section_info()->visible;
|
|
|
|
// The module on the course page may be in one of the following states:
|
|
|
|
// - Available and displayed on the course page ($displayedoncoursepage);
|
|
|
|
// - Not available and not displayed on the course page ($unavailable);
|
|
|
|
// - Available but not displayed on the course page ($stealth) - this can also be a visible activity in a hidden section.
|
|
|
|
$displayedoncoursepage = $mod->visible && $mod->visibleoncoursepage && $sectionvisible;
|
|
|
|
$unavailable = !$mod->visible;
|
|
|
|
$stealth = $mod->visible && (!$mod->visibleoncoursepage || !$sectionvisible);
|
|
|
|
if ($displayedoncoursepage) {
|
2013-10-25 14:36:46 +08:00
|
|
|
$actions['hide'] = new action_menu_link_secondary(
|
|
|
|
new moodle_url($baseurl, array('hide' => $mod->id)),
|
2019-02-11 09:34:18 +08:00
|
|
|
new pix_icon('t/hide', '', 'moodle', array('class' => 'iconsmall')),
|
MDL-57961 course: change wording of module availability
renamed Visible to Availability and added help strings
AMOS BEGIN
CPY [availability,core_moodle],[modvisible,core_moodle]
CPY [availability,core_moodle],[modvisiblewithstealth,core_moodle]
CPY [availability,core_moodle],[modvisiblehiddensection,core_moodle]
CPY [show,core_moodle],[modshow,core_moodle]
CPY [hide,core_moodle],[modhide,core_moodle]
AMOS END
2017-03-20 16:18:04 +08:00
|
|
|
$str->modhide,
|
2022-04-12 15:53:29 +02:00
|
|
|
[
|
|
|
|
'class' => 'editing_hide',
|
|
|
|
'data-action' => ($usecomponents) ? 'cmHide' : 'hide',
|
|
|
|
'data-id' => $mod->id,
|
|
|
|
]
|
2013-10-25 14:36:46 +08:00
|
|
|
);
|
2017-02-03 09:40:46 +08:00
|
|
|
} else if (!$displayedoncoursepage && $sectionvisible) {
|
|
|
|
// Offer to "show" only if the section is visible.
|
2013-10-25 14:36:46 +08:00
|
|
|
$actions['show'] = new action_menu_link_secondary(
|
|
|
|
new moodle_url($baseurl, array('show' => $mod->id)),
|
2019-02-11 09:34:18 +08:00
|
|
|
new pix_icon('t/show', '', 'moodle', array('class' => 'iconsmall')),
|
MDL-57961 course: change wording of module availability
renamed Visible to Availability and added help strings
AMOS BEGIN
CPY [availability,core_moodle],[modvisible,core_moodle]
CPY [availability,core_moodle],[modvisiblewithstealth,core_moodle]
CPY [availability,core_moodle],[modvisiblehiddensection,core_moodle]
CPY [show,core_moodle],[modshow,core_moodle]
CPY [hide,core_moodle],[modhide,core_moodle]
AMOS END
2017-03-20 16:18:04 +08:00
|
|
|
$str->modshow,
|
2022-04-12 15:53:29 +02:00
|
|
|
[
|
|
|
|
'class' => 'editing_show',
|
|
|
|
'data-action' => ($usecomponents) ? 'cmShow' : 'show',
|
|
|
|
'data-id' => $mod->id,
|
|
|
|
]
|
2013-10-25 14:36:46 +08:00
|
|
|
);
|
2011-08-25 18:02:35 +12:00
|
|
|
}
|
2017-02-03 09:40:46 +08:00
|
|
|
|
|
|
|
if ($stealth) {
|
|
|
|
// When making the "stealth" module unavailable we perform the same action as hiding the visible module.
|
|
|
|
$actions['hide'] = new action_menu_link_secondary(
|
|
|
|
new moodle_url($baseurl, array('hide' => $mod->id)),
|
2019-02-11 09:34:18 +08:00
|
|
|
new pix_icon('t/unblock', '', 'moodle', array('class' => 'iconsmall')),
|
2017-02-03 09:40:46 +08:00
|
|
|
$str->makeunavailable,
|
2022-04-12 15:53:29 +02:00
|
|
|
[
|
|
|
|
'class' => 'editing_makeunavailable',
|
|
|
|
'data-action' => ($usecomponents) ? 'cmHide' : 'hide',
|
|
|
|
'data-sectionreturn' => $sr,
|
|
|
|
'data-id' => $mod->id,
|
|
|
|
]
|
2017-02-03 09:40:46 +08:00
|
|
|
);
|
|
|
|
} else if ($unavailable && (!$sectionvisible || $allowstealth) && $mod->has_view()) {
|
|
|
|
// Allow to make visually hidden module available in gradebook and other reports by making it a "stealth" module.
|
|
|
|
// When the section is hidden it is an equivalent of "showing" the module.
|
|
|
|
// Activities without the link (i.e. labels) can not be made available but hidden on course page.
|
|
|
|
$action = $sectionvisible ? 'stealth' : 'show';
|
2022-04-12 15:53:29 +02:00
|
|
|
if ($usecomponents) {
|
|
|
|
$action = 'cm' . ucfirst($action);
|
|
|
|
}
|
2017-02-03 09:40:46 +08:00
|
|
|
$actions[$action] = new action_menu_link_secondary(
|
2022-04-12 15:53:29 +02:00
|
|
|
new moodle_url($baseurl, array('stealth' => $mod->id)),
|
2019-02-11 09:34:18 +08:00
|
|
|
new pix_icon('t/block', '', 'moodle', array('class' => 'iconsmall')),
|
2017-02-03 09:40:46 +08:00
|
|
|
$str->makeavailable,
|
2022-04-12 15:53:29 +02:00
|
|
|
[
|
|
|
|
'class' => 'editing_makeavailable',
|
|
|
|
'data-action' => $action,
|
|
|
|
'data-sectionreturn' => $sr,
|
|
|
|
'data-id' => $mod->id,
|
|
|
|
]
|
2017-02-03 09:40:46 +08:00
|
|
|
);
|
|
|
|
}
|
2003-04-28 04:32:55 +00:00
|
|
|
}
|
2011-08-25 18:02:35 +12:00
|
|
|
|
2012-07-16 14:54:06 +08:00
|
|
|
// Duplicate (require both target import caps to be able to duplicate and backup2 support, see modduplicate.php)
|
2014-11-13 10:45:15 +08:00
|
|
|
if (has_all_capabilities($dupecaps, $coursecontext) &&
|
2017-02-03 09:40:46 +08:00
|
|
|
plugin_supports('mod', $mod->modname, FEATURE_BACKUP_MOODLE2) &&
|
|
|
|
course_allowed_module($mod->get_course(), $mod->modname)) {
|
2013-07-18 10:42:11 +12:00
|
|
|
$actions['duplicate'] = new action_menu_link_secondary(
|
2023-01-03 11:35:37 +01:00
|
|
|
new moodle_url($baseurl, ['duplicate' => $mod->id]),
|
2019-02-11 09:34:18 +08:00
|
|
|
new pix_icon('t/copy', '', 'moodle', array('class' => 'iconsmall')),
|
2013-07-05 10:45:05 +12:00
|
|
|
$str->duplicate,
|
2023-01-03 11:35:37 +01:00
|
|
|
[
|
|
|
|
'class' => 'editing_duplicate',
|
|
|
|
'data-action' => ($courseformat->supports_components()) ? 'cmDuplicate' : 'duplicate',
|
|
|
|
'data-sectionreturn' => $sr,
|
|
|
|
'data-id' => $mod->id,
|
|
|
|
]
|
2011-09-09 15:53:02 +02:00
|
|
|
);
|
|
|
|
}
|
2011-08-25 18:02:35 +12:00
|
|
|
|
2013-07-08 11:44:32 +12:00
|
|
|
// Assign.
|
2011-12-18 11:54:02 +01:00
|
|
|
if (has_capability('moodle/role:assign', $modcontext)){
|
2013-07-18 10:42:11 +12:00
|
|
|
$actions['assign'] = new action_menu_link_secondary(
|
2012-12-11 13:22:51 +08:00
|
|
|
new moodle_url('/admin/roles/assign.php', array('contextid' => $modcontext->id)),
|
2019-02-11 09:34:18 +08:00
|
|
|
new pix_icon('t/assignroles', '', 'moodle', array('class' => 'iconsmall')),
|
2013-07-05 10:45:05 +12:00
|
|
|
$str->assign,
|
2017-02-03 09:40:46 +08:00
|
|
|
array('class' => 'editing_assign', 'data-action' => 'assignroles', 'data-sectionreturn' => $sr)
|
2011-08-25 18:02:35 +12:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-10-25 14:36:46 +08:00
|
|
|
// Delete.
|
|
|
|
if ($hasmanageactivities) {
|
|
|
|
$actions['delete'] = new action_menu_link_secondary(
|
2023-01-02 16:23:17 +01:00
|
|
|
new moodle_url($baseurl, ['delete' => $mod->id]),
|
|
|
|
new pix_icon('t/delete', '', 'moodle', ['class' => 'iconsmall']),
|
2013-10-25 14:36:46 +08:00
|
|
|
$str->delete,
|
2023-01-02 16:23:17 +01:00
|
|
|
[
|
|
|
|
'class' => 'editing_delete',
|
|
|
|
'data-action' => ($usecomponents) ? 'cmDelete' : 'delete',
|
|
|
|
'data-sectionreturn' => $sr,
|
|
|
|
'data-id' => $mod->id,
|
|
|
|
]
|
2013-10-25 14:36:46 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-12-11 13:22:51 +08:00
|
|
|
return $actions;
|
2002-08-02 17:38:18 +00:00
|
|
|
}
|
|
|
|
|
2013-10-29 09:57:47 +08:00
|
|
|
/**
|
|
|
|
* Returns the move action.
|
|
|
|
*
|
|
|
|
* @param cm_info $mod The module to produce a move button for
|
|
|
|
* @param int $sr The section to link back to (used for creating the links)
|
|
|
|
* @return The markup for the move action, or an empty string if not available.
|
|
|
|
*/
|
|
|
|
function course_get_cm_move(cm_info $mod, $sr = null) {
|
|
|
|
global $OUTPUT;
|
|
|
|
|
|
|
|
static $str;
|
|
|
|
static $baseurl;
|
|
|
|
|
|
|
|
$modcontext = context_module::instance($mod->id);
|
|
|
|
$hasmanageactivities = has_capability('moodle/course:manageactivities', $modcontext);
|
|
|
|
|
|
|
|
if (!isset($str)) {
|
|
|
|
$str = get_strings(array('move'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($baseurl)) {
|
|
|
|
$baseurl = new moodle_url('/course/mod.php', array('sesskey' => sesskey()));
|
|
|
|
|
|
|
|
if ($sr !== null) {
|
|
|
|
$baseurl->param('sr', $sr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($hasmanageactivities) {
|
|
|
|
$pixicon = 'i/dragdrop';
|
|
|
|
|
2013-12-03 08:15:56 +08:00
|
|
|
if (!course_ajax_enabled($mod->get_course())) {
|
2013-10-29 09:57:47 +08:00
|
|
|
// Override for course frontpage until we get drag/drop working there.
|
|
|
|
$pixicon = 't/move';
|
|
|
|
}
|
|
|
|
|
2020-12-10 04:02:09 +11:00
|
|
|
$attributes = [
|
|
|
|
'class' => 'editing_move',
|
|
|
|
'data-action' => 'move',
|
|
|
|
'data-sectionreturn' => $sr,
|
|
|
|
'title' => $str->move,
|
|
|
|
'aria-label' => $str->move,
|
|
|
|
];
|
2013-10-29 09:57:47 +08:00
|
|
|
return html_writer::link(
|
2020-12-10 04:02:09 +11:00
|
|
|
new moodle_url($baseurl, ['copy' => $mod->id]),
|
|
|
|
$OUTPUT->pix_icon($pixicon, '', 'moodle', ['class' => 'iconsmall']),
|
|
|
|
$attributes
|
2013-10-29 09:57:47 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2005-01-24 22:21:28 +00:00
|
|
|
/**
|
2006-11-09 18:25:58 +00:00
|
|
|
* given a course object with shortname & fullname, this function will
|
2005-01-24 22:21:28 +00:00
|
|
|
* truncate the the number of chars allowed and add ... if it was too long
|
|
|
|
*/
|
|
|
|
function course_format_name ($course,$max=100) {
|
2006-11-09 18:25:58 +00:00
|
|
|
|
2012-07-23 15:59:04 +08:00
|
|
|
$context = context_course::instance($course->id);
|
2011-09-07 11:46:28 +12:00
|
|
|
$shortname = format_string($course->shortname, true, array('context' => $context));
|
2012-07-23 15:59:04 +08:00
|
|
|
$fullname = format_string($course->fullname, true, array('context' => context_course::instance($course->id)));
|
2011-09-13 16:06:21 +08:00
|
|
|
$str = $shortname.': '. $fullname;
|
2013-08-06 20:58:28 +02:00
|
|
|
if (core_text::strlen($str) <= $max) {
|
2005-01-24 22:21:28 +00:00
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
else {
|
2013-08-06 20:58:28 +02:00
|
|
|
return core_text::substr($str,0,$max-3).'...';
|
2005-01-24 22:21:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-08-16 06:15:49 +00:00
|
|
|
/**
|
2012-03-07 11:33:50 +00:00
|
|
|
* Is the user allowed to add this type of module to this course?
|
|
|
|
* @param object $course the course settings. Only $course->id is used.
|
|
|
|
* @param string $modname the module name. E.g. 'forum' or 'quiz'.
|
2020-01-21 13:21:48 +08:00
|
|
|
* @param \stdClass $user the user to check, defaults to the global user if not provided.
|
2012-03-07 11:33:50 +00:00
|
|
|
* @return bool whether the current user is allowed to add this type of module to this course.
|
2005-08-16 06:15:49 +00:00
|
|
|
*/
|
2020-01-21 13:21:48 +08:00
|
|
|
function course_allowed_module($course, $modname, \stdClass $user = null) {
|
|
|
|
global $USER;
|
|
|
|
$user = $user ?? $USER;
|
2012-03-07 11:33:50 +00:00
|
|
|
if (is_numeric($modname)) {
|
|
|
|
throw new coding_exception('Function course_allowed_module no longer
|
|
|
|
supports numeric module ids. Please update your code to pass the module name.');
|
2005-08-16 06:15:49 +00:00
|
|
|
}
|
2006-11-09 18:25:58 +00:00
|
|
|
|
2012-03-07 11:33:50 +00:00
|
|
|
$capability = 'mod/' . $modname . ':addinstance';
|
|
|
|
if (!get_capability_info($capability)) {
|
|
|
|
// Debug warning that the capability does not exist, but no more than once per page.
|
|
|
|
static $warned = array();
|
|
|
|
$archetype = plugin_supports('mod', $modname, FEATURE_MOD_ARCHETYPE, MOD_ARCHETYPE_OTHER);
|
|
|
|
if (!isset($warned[$modname]) && $archetype !== MOD_ARCHETYPE_SYSTEM) {
|
|
|
|
debugging('The module ' . $modname . ' does not define the standard capability ' .
|
|
|
|
$capability , DEBUG_DEVELOPER);
|
|
|
|
$warned[$modname] = 1;
|
|
|
|
}
|
2008-02-21 15:17:41 +00:00
|
|
|
|
2012-03-07 11:33:50 +00:00
|
|
|
// If the capability does not exist, the module can always be added.
|
|
|
|
return true;
|
2005-08-16 06:15:49 +00:00
|
|
|
}
|
2008-06-04 06:38:03 +00:00
|
|
|
|
2012-03-07 11:33:50 +00:00
|
|
|
$coursecontext = context_course::instance($course->id);
|
2020-01-21 13:21:48 +08:00
|
|
|
return has_capability($capability, $coursecontext, $user);
|
2005-08-16 06:15:49 +00:00
|
|
|
}
|
|
|
|
|
2008-06-01 19:05:07 +00:00
|
|
|
/**
|
|
|
|
* Efficiently moves many courses around while maintaining
|
|
|
|
* sortorder in order.
|
|
|
|
*
|
2010-09-17 12:04:48 +00:00
|
|
|
* @param array $courseids is an array of course ids
|
|
|
|
* @param int $categoryid
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
* @return bool success
|
2008-06-01 19:05:07 +00:00
|
|
|
*/
|
|
|
|
function move_courses($courseids, $categoryid) {
|
2013-08-09 18:01:05 +08:00
|
|
|
global $DB;
|
2005-09-01 04:48:22 +00:00
|
|
|
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
if (empty($courseids)) {
|
2013-08-09 18:01:05 +08:00
|
|
|
// Nothing to do.
|
2013-09-30 09:08:07 +13:00
|
|
|
return false;
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
}
|
2006-11-09 18:25:58 +00:00
|
|
|
|
2013-08-09 18:01:05 +08:00
|
|
|
if (!$category = $DB->get_record('course_categories', array('id' => $categoryid))) {
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
2007-09-19 07:14:23 +00:00
|
|
|
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
$courseids = array_reverse($courseids);
|
2012-07-23 15:59:04 +08:00
|
|
|
$newparent = context_coursecat::instance($category->id);
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
$i = 1;
|
2008-06-16 14:25:53 +00:00
|
|
|
|
2013-08-30 16:29:59 +10:00
|
|
|
list($where, $params) = $DB->get_in_or_equal($courseids);
|
2013-09-04 11:07:13 +08:00
|
|
|
$dbcourses = $DB->get_records_select('course', 'id ' . $where, $params, '', 'id, category, shortname, fullname');
|
2013-09-03 14:21:28 +10:00
|
|
|
foreach ($dbcourses as $dbcourse) {
|
|
|
|
$course = new stdClass();
|
|
|
|
$course->id = $dbcourse->id;
|
2020-01-08 15:31:46 +07:00
|
|
|
$course->timemodified = time();
|
2013-09-03 14:21:28 +10:00
|
|
|
$course->category = $category->id;
|
2020-08-31 14:59:59 +10:00
|
|
|
$course->sortorder = $category->sortorder + get_max_courses_in_category() - $i++;
|
2013-09-03 14:21:28 +10:00
|
|
|
if ($category->visible == 0) {
|
|
|
|
// Hide the course when moving into hidden category, do not update the visibleold flag - we want to get
|
|
|
|
// to previous state if somebody unhides the category.
|
|
|
|
$course->visible = 0;
|
|
|
|
}
|
2013-08-09 18:01:05 +08:00
|
|
|
|
2013-09-03 14:21:28 +10:00
|
|
|
$DB->update_record('course', $course);
|
2013-08-09 18:01:05 +08:00
|
|
|
|
2013-09-04 11:07:13 +08:00
|
|
|
// Update context, so it can be passed to event.
|
2013-09-03 14:21:28 +10:00
|
|
|
$context = context_course::instance($course->id);
|
2013-09-04 11:07:13 +08:00
|
|
|
$context->update_moved($newparent);
|
2013-09-03 14:21:28 +10:00
|
|
|
|
|
|
|
// Trigger a course updated event.
|
|
|
|
$event = \core\event\course_updated::create(array(
|
|
|
|
'objectid' => $course->id,
|
2013-09-04 11:07:13 +08:00
|
|
|
'context' => context_course::instance($course->id),
|
2013-09-03 14:21:28 +10:00
|
|
|
'other' => array('shortname' => $dbcourse->shortname,
|
2019-04-16 18:31:51 +01:00
|
|
|
'fullname' => $dbcourse->fullname,
|
|
|
|
'updatedfields' => array('category' => $category->id))
|
2013-09-03 14:21:28 +10:00
|
|
|
));
|
|
|
|
$event->trigger();
|
2008-06-16 14:25:53 +00:00
|
|
|
}
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
fix_course_sortorder();
|
2013-02-28 12:29:15 +11:00
|
|
|
cache_helper::purge_by_event('changesincourse');
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
|
2005-09-01 04:48:22 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-12-11 15:47:23 +00:00
|
|
|
/**
|
2012-09-18 09:57:37 +08:00
|
|
|
* Returns the display name of the given section that the course prefers
|
2010-06-08 06:21:25 +00:00
|
|
|
*
|
2012-09-18 09:57:37 +08:00
|
|
|
* Implementation of this function is provided by course format
|
2021-06-22 09:50:35 +02:00
|
|
|
* @see core_courseformat\base::get_section_name()
|
2010-06-08 06:21:25 +00:00
|
|
|
*
|
2012-09-18 09:57:37 +08:00
|
|
|
* @param int|stdClass $courseorid The course to get the section name for (object or just course id)
|
|
|
|
* @param int|stdClass $section Section object from database or just field course_sections.section
|
|
|
|
* @return string Display name that the course format prefers, e.g. "Week 2"
|
2006-12-11 15:47:23 +00:00
|
|
|
*/
|
2012-09-18 09:57:37 +08:00
|
|
|
function get_section_name($courseorid, $section) {
|
|
|
|
return course_get_format($courseorid)->get_section_name($section);
|
2010-06-08 06:21:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-09-18 09:57:37 +08:00
|
|
|
* Tells if current course format uses sections
|
2010-06-17 22:50:25 +00:00
|
|
|
*
|
2010-06-08 06:21:25 +00:00
|
|
|
* @param string $format Course format ID e.g. 'weeks' $course->format
|
2012-09-18 09:57:37 +08:00
|
|
|
* @return bool
|
2010-06-08 06:21:25 +00:00
|
|
|
*/
|
|
|
|
function course_format_uses_sections($format) {
|
2012-09-18 09:57:37 +08:00
|
|
|
$course = new stdClass();
|
|
|
|
$course->format = $format;
|
|
|
|
return course_get_format($course)->uses_sections();
|
2006-12-11 15:47:23 +00:00
|
|
|
}
|
|
|
|
|
2010-11-09 11:54:58 +00:00
|
|
|
/**
|
|
|
|
* Returns the information about the ajax support in the given source format
|
|
|
|
*
|
|
|
|
* The returned object's property (boolean)capable indicates that
|
|
|
|
* the course format supports Moodle course ajax features.
|
|
|
|
*
|
|
|
|
* @param string $format
|
|
|
|
* @return stdClass
|
|
|
|
*/
|
|
|
|
function course_format_ajax_support($format) {
|
2012-09-18 09:57:37 +08:00
|
|
|
$course = new stdClass();
|
|
|
|
$course->format = $format;
|
|
|
|
return course_get_format($course)->supports_ajax();
|
2010-11-09 11:54:58 +00:00
|
|
|
}
|
|
|
|
|
2007-02-16 08:46:55 +00:00
|
|
|
/**
|
|
|
|
* Can the current user delete this course?
|
2009-05-04 19:56:32 +00:00
|
|
|
* Course creators have exception,
|
|
|
|
* 1 day after the creation they can sill delete the course.
|
2007-02-16 08:46:55 +00:00
|
|
|
* @param int $courseid
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
function can_delete_course($courseid) {
|
2014-02-27 12:12:54 +08:00
|
|
|
global $USER;
|
2007-02-16 08:46:55 +00:00
|
|
|
|
2012-07-23 15:59:04 +08:00
|
|
|
$context = context_course::instance($courseid);
|
2007-02-16 08:46:55 +00:00
|
|
|
|
2009-05-04 19:56:32 +00:00
|
|
|
if (has_capability('moodle/course:delete', $context)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// hack: now try to find out if creator created this course recently (1 day)
|
|
|
|
if (!has_capability('moodle/course:create', $context)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$since = time() - 60*60*24;
|
2014-02-27 12:12:54 +08:00
|
|
|
$course = get_course($courseid);
|
|
|
|
|
|
|
|
if ($course->timecreated < $since) {
|
|
|
|
return false; // Return if the course was not created in last 24 hours.
|
|
|
|
}
|
|
|
|
|
|
|
|
$logmanger = get_log_manager();
|
2015-02-23 16:18:51 +08:00
|
|
|
$readers = $logmanger->get_readers('\core\log\sql_reader');
|
2014-02-27 12:12:54 +08:00
|
|
|
$reader = reset($readers);
|
|
|
|
|
|
|
|
if (empty($reader)) {
|
|
|
|
return false; // No log reader found.
|
|
|
|
}
|
2009-05-04 19:56:32 +00:00
|
|
|
|
2014-02-27 12:12:54 +08:00
|
|
|
// A proper reader.
|
|
|
|
$select = "userid = :userid AND courseid = :courseid AND eventname = :eventname AND timecreated > :since";
|
|
|
|
$params = array('userid' => $USER->id, 'since' => $since, 'courseid' => $course->id, 'eventname' => '\core\event\course_created');
|
2009-05-04 19:56:32 +00:00
|
|
|
|
2014-02-27 12:12:54 +08:00
|
|
|
return (bool)$reader->get_events_select_count($select, $params);
|
2007-02-16 08:46:55 +00:00
|
|
|
}
|
|
|
|
|
2008-11-13 08:11:10 +00:00
|
|
|
/**
|
|
|
|
* Save the Your name for 'Some role' strings.
|
|
|
|
*
|
|
|
|
* @param integer $courseid the id of this course.
|
|
|
|
* @param array $data the data that came from the course settings form.
|
|
|
|
*/
|
|
|
|
function save_local_role_names($courseid, $data) {
|
|
|
|
global $DB;
|
2012-07-23 15:59:04 +08:00
|
|
|
$context = context_course::instance($courseid);
|
2008-11-13 08:11:10 +00:00
|
|
|
|
|
|
|
foreach ($data as $fieldname => $value) {
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
if (strpos($fieldname, 'role_') !== 0) {
|
2008-11-13 08:11:10 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
list($ignored, $roleid) = explode('_', $fieldname);
|
|
|
|
|
|
|
|
// make up our mind whether we want to delete, update or insert
|
|
|
|
if (!$value) {
|
|
|
|
$DB->delete_records('role_names', array('contextid' => $context->id, 'roleid' => $roleid));
|
|
|
|
|
|
|
|
} else if ($rolename = $DB->get_record('role_names', array('contextid' => $context->id, 'roleid' => $roleid))) {
|
|
|
|
$rolename->name = $value;
|
|
|
|
$DB->update_record('role_names', $rolename);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$rolename = new stdClass;
|
|
|
|
$rolename->contextid = $context->id;
|
|
|
|
$rolename->roleid = $roleid;
|
|
|
|
$rolename->name = $value;
|
|
|
|
$DB->insert_record('role_names', $rolename);
|
|
|
|
}
|
2015-12-01 12:43:32 +00:00
|
|
|
// This will ensure the course contacts cache is purged..
|
2018-06-16 11:57:27 +08:00
|
|
|
core_course_category::role_assignment_changed($roleid, $context);
|
2008-11-13 08:11:10 +00:00
|
|
|
}
|
|
|
|
}
|
2007-02-16 08:46:55 +00:00
|
|
|
|
2013-03-28 12:18:32 +11:00
|
|
|
/**
|
|
|
|
* Returns options to use in course overviewfiles filemanager
|
|
|
|
*
|
2018-06-16 11:57:27 +08:00
|
|
|
* @param null|stdClass|core_course_list_element|int $course either object that has 'id' property or just the course id;
|
2013-03-28 12:18:32 +11:00
|
|
|
* may be empty if course does not exist yet (course create form)
|
|
|
|
* @return array|null array of options such as maxfiles, maxbytes, accepted_types, etc.
|
|
|
|
* or null if overviewfiles are disabled
|
|
|
|
*/
|
|
|
|
function course_overviewfiles_options($course) {
|
|
|
|
global $CFG;
|
|
|
|
if (empty($CFG->courseoverviewfileslimit)) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-06-03 22:07:02 +01:00
|
|
|
|
|
|
|
// Create accepted file types based on config value, falling back to default all.
|
|
|
|
$acceptedtypes = (new \core_form\filetypes_util)->normalize_file_types($CFG->courseoverviewfilesext);
|
|
|
|
if (in_array('*', $acceptedtypes) || empty($acceptedtypes)) {
|
|
|
|
$acceptedtypes = '*';
|
2013-03-28 12:18:32 +11:00
|
|
|
}
|
2021-06-03 22:07:02 +01:00
|
|
|
|
2013-03-28 12:18:32 +11:00
|
|
|
$options = array(
|
|
|
|
'maxfiles' => $CFG->courseoverviewfileslimit,
|
|
|
|
'maxbytes' => $CFG->maxbytes,
|
|
|
|
'subdirs' => 0,
|
2021-06-03 22:07:02 +01:00
|
|
|
'accepted_types' => $acceptedtypes
|
2013-03-28 12:18:32 +11:00
|
|
|
);
|
|
|
|
if (!empty($course->id)) {
|
|
|
|
$options['context'] = context_course::instance($course->id);
|
|
|
|
} else if (is_int($course) && $course > 0) {
|
|
|
|
$options['context'] = context_course::instance($course);
|
|
|
|
}
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
2008-05-30 16:47:21 +00:00
|
|
|
/**
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
* Create a course and either return a $course object
|
|
|
|
*
|
|
|
|
* Please note this functions does not verify any access control,
|
|
|
|
* the calling code is responsible for all validation (usually it is the form definition).
|
2007-03-23 02:29:05 +00:00
|
|
|
*
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
* @param array $editoroptions course description editor options
|
2007-03-23 02:29:05 +00:00
|
|
|
* @param object $data - all the data needed for an entry in the 'course' table
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
* @return object new course instance
|
2007-03-23 02:29:05 +00:00
|
|
|
*/
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
function create_course($data, $editoroptions = NULL) {
|
2015-08-22 20:55:37 +08:00
|
|
|
global $DB, $CFG;
|
2007-03-23 02:29:05 +00:00
|
|
|
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
//check the categoryid - must be given for all new courses
|
|
|
|
$category = $DB->get_record('course_categories', array('id'=>$data->category), '*', MUST_EXIST);
|
2009-03-20 06:53:03 +00:00
|
|
|
|
2013-08-21 09:13:39 -07:00
|
|
|
// Check if the shortname already exists.
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
if (!empty($data->shortname)) {
|
|
|
|
if ($DB->record_exists('course', array('shortname' => $data->shortname))) {
|
2013-08-21 09:13:39 -07:00
|
|
|
throw new moodle_exception('shortnametaken', '', '', $data->shortname);
|
2009-03-20 06:53:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-21 09:13:39 -07:00
|
|
|
// Check if the idnumber already exists.
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
if (!empty($data->idnumber)) {
|
|
|
|
if ($DB->record_exists('course', array('idnumber' => $data->idnumber))) {
|
2013-08-21 09:13:39 -07:00
|
|
|
throw new moodle_exception('courseidnumbertaken', '', '', $data->idnumber);
|
2009-03-20 06:53:03 +00:00
|
|
|
}
|
|
|
|
}
|
2009-08-07 07:18:01 +00:00
|
|
|
|
2019-07-16 10:28:15 +08:00
|
|
|
if (empty($CFG->enablecourserelativedates)) {
|
|
|
|
// Make sure we're not setting the relative dates mode when the setting is disabled.
|
|
|
|
unset($data->relativedatesmode);
|
|
|
|
}
|
|
|
|
|
2016-09-01 13:29:19 +08:00
|
|
|
if ($errorcode = course_validate_dates((array)$data)) {
|
|
|
|
throw new moodle_exception($errorcode);
|
|
|
|
}
|
|
|
|
|
2014-07-10 16:15:26 -04:00
|
|
|
// Check if timecreated is given.
|
|
|
|
$data->timecreated = !empty($data->timecreated) ? $data->timecreated : time();
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
$data->timemodified = $data->timecreated;
|
2009-03-20 06:53:03 +00:00
|
|
|
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
// place at beginning of any category
|
|
|
|
$data->sortorder = 0;
|
2009-05-08 14:24:56 +00:00
|
|
|
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
if ($editoroptions) {
|
|
|
|
// summary text is updated later, we need context to store the files first
|
|
|
|
$data->summary = '';
|
2023-03-17 06:49:50 +01:00
|
|
|
$data->summary_format = $data->summary_editor['format'];
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
}
|
|
|
|
|
2021-04-22 22:41:03 +08:00
|
|
|
// Get default completion settings as a fallback in case the enablecompletion field is not set.
|
|
|
|
$courseconfig = get_config('moodlecourse');
|
|
|
|
$defaultcompletion = !empty($CFG->enablecompletion) ? $courseconfig->enablecompletion : COMPLETION_DISABLED;
|
|
|
|
$enablecompletion = $data->enablecompletion ?? $defaultcompletion;
|
|
|
|
// Unset showcompletionconditions when completion tracking is not enabled for the course.
|
|
|
|
if ($enablecompletion == COMPLETION_DISABLED) {
|
|
|
|
unset($data->showcompletionconditions);
|
|
|
|
} else if (!isset($data->showcompletionconditions)) {
|
|
|
|
// Show completion conditions should have a default value when completion is enabled. Set it to the site defaults.
|
|
|
|
// This scenario can happen when a course is created through data generators or through a web service.
|
|
|
|
$data->showcompletionconditions = $courseconfig->showcompletionconditions;
|
|
|
|
}
|
|
|
|
|
2010-07-14 14:23:32 +00:00
|
|
|
if (!isset($data->visible)) {
|
|
|
|
// data not from form, add missing visibility info
|
2010-07-19 17:44:08 +00:00
|
|
|
$data->visible = $category->visible;
|
2007-03-23 02:29:05 +00:00
|
|
|
}
|
2010-07-14 14:23:32 +00:00
|
|
|
$data->visibleold = $data->visible;
|
2007-02-16 08:46:55 +00:00
|
|
|
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
$newcourseid = $DB->insert_record('course', $data);
|
2012-07-23 15:59:04 +08:00
|
|
|
$context = context_course::instance($newcourseid, MUST_EXIST);
|
2007-03-23 02:29:05 +00:00
|
|
|
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
if ($editoroptions) {
|
|
|
|
// Save the files used in the summary editor and store
|
2010-07-03 13:37:13 +00:00
|
|
|
$data = file_postupdate_standard_editor($data, 'summary', $editoroptions, $context, 'course', 'summary', 0);
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
$DB->set_field('course', 'summary', $data->summary, array('id'=>$newcourseid));
|
|
|
|
$DB->set_field('course', 'summaryformat', $data->summary_format, array('id'=>$newcourseid));
|
|
|
|
}
|
2013-03-28 12:18:32 +11:00
|
|
|
if ($overviewfilesoptions = course_overviewfiles_options($newcourseid)) {
|
|
|
|
// Save the course overviewfiles
|
|
|
|
$data = file_postupdate_standard_filemanager($data, 'overviewfiles', $overviewfilesoptions, $context, 'course', 'overviewfiles', 0);
|
|
|
|
}
|
2007-03-23 02:29:05 +00:00
|
|
|
|
2012-10-11 13:31:55 +08:00
|
|
|
// update course format options
|
|
|
|
course_get_format($newcourseid)->update_course_format_options($data);
|
|
|
|
|
|
|
|
$course = course_get_format($newcourseid)->get_course();
|
2007-03-23 02:29:05 +00:00
|
|
|
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
fix_course_sortorder();
|
2013-02-28 12:29:15 +11:00
|
|
|
// purge appropriate caches in case fix_course_sortorder() did not change anything
|
|
|
|
cache_helper::purge_by_event('changesincourse');
|
2007-03-23 02:29:05 +00:00
|
|
|
|
2015-09-30 12:49:58 +08:00
|
|
|
// Trigger a course created event.
|
|
|
|
$event = \core\event\course_created::create(array(
|
|
|
|
'objectid' => $course->id,
|
|
|
|
'context' => context_course::instance($course->id),
|
|
|
|
'other' => array('shortname' => $course->shortname,
|
|
|
|
'fullname' => $course->fullname)
|
|
|
|
));
|
2016-11-10 13:51:02 +08:00
|
|
|
|
2015-09-30 12:49:58 +08:00
|
|
|
$event->trigger();
|
|
|
|
|
|
|
|
// Setup the blocks
|
|
|
|
blocks_add_default_course_blocks($course);
|
|
|
|
|
2017-02-02 12:33:16 +08:00
|
|
|
// Create default section and initial sections if specified (unless they've already been created earlier).
|
|
|
|
// We do not want to call course_create_sections_if_missing() because to avoid creating course cache.
|
|
|
|
$numsections = isset($data->numsections) ? $data->numsections : 0;
|
|
|
|
$existingsections = $DB->get_fieldset_sql('SELECT section from {course_sections} WHERE course = ?', [$newcourseid]);
|
|
|
|
$newsections = array_diff(range(0, $numsections), $existingsections);
|
|
|
|
foreach ($newsections as $sectionnum) {
|
|
|
|
course_create_section($newcourseid, $sectionnum, true);
|
|
|
|
}
|
2015-09-30 12:49:58 +08:00
|
|
|
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
// Save any custom role names.
|
2010-09-17 12:04:48 +00:00
|
|
|
save_local_role_names($course->id, (array)$data);
|
2007-03-23 02:29:05 +00:00
|
|
|
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
// set up enrolments
|
|
|
|
enrol_course_updated(true, $course, $data);
|
2008-11-13 08:11:10 +00:00
|
|
|
|
2015-08-22 20:55:37 +08:00
|
|
|
// Update course tags.
|
2015-09-05 23:05:45 +08:00
|
|
|
if (isset($data->tags)) {
|
|
|
|
core_tag_tag::set_item_tags('core', 'course', $course->id, context_course::instance($course->id), $data->tags);
|
2015-08-22 20:55:37 +08:00
|
|
|
}
|
|
|
|
|
2019-01-11 10:56:14 +01:00
|
|
|
// Save custom fields if there are any of them in the form.
|
|
|
|
$handler = core_course\customfield\course_handler::create();
|
2019-01-21 15:45:12 +08:00
|
|
|
// Make sure to set the handler's parent context first.
|
|
|
|
$coursecatcontext = context_coursecat::instance($category->id);
|
|
|
|
$handler->set_parent_context($coursecatcontext);
|
|
|
|
// Save the custom field data.
|
2019-01-11 10:56:14 +01:00
|
|
|
$data->id = $course->id;
|
|
|
|
$handler->instance_form_save($data, true);
|
|
|
|
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
return $course;
|
2007-03-23 02:29:05 +00:00
|
|
|
}
|
|
|
|
|
2008-05-30 16:47:21 +00:00
|
|
|
/**
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
* Update a course.
|
|
|
|
*
|
|
|
|
* Please note this functions does not verify any access control,
|
|
|
|
* the calling code is responsible for all validation (usually it is the form definition).
|
2007-03-23 02:29:05 +00:00
|
|
|
*
|
|
|
|
* @param object $data - all the data needed for an entry in the 'course' table
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
* @param array $editoroptions course description editor options
|
|
|
|
* @return void
|
2007-03-23 02:29:05 +00:00
|
|
|
*/
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
function update_course($data, $editoroptions = NULL) {
|
2015-08-22 20:55:37 +08:00
|
|
|
global $DB, $CFG;
|
2007-03-23 02:29:05 +00:00
|
|
|
|
2016-09-15 15:06:53 +08:00
|
|
|
// Prevent changes on front page course.
|
|
|
|
if ($data->id == SITEID) {
|
|
|
|
throw new moodle_exception('invalidcourse', 'error');
|
|
|
|
}
|
|
|
|
|
2012-10-11 13:31:55 +08:00
|
|
|
$oldcourse = course_get_format($data->id)->get_course();
|
2012-07-23 15:59:04 +08:00
|
|
|
$context = context_course::instance($oldcourse->id);
|
2007-03-23 02:29:05 +00:00
|
|
|
|
2019-07-16 10:28:15 +08:00
|
|
|
// Make sure we're not changing whatever the course's relativedatesmode setting is.
|
|
|
|
unset($data->relativedatesmode);
|
|
|
|
|
2019-04-16 18:31:51 +01:00
|
|
|
// Capture the updated fields for the log data.
|
|
|
|
$updatedfields = [];
|
|
|
|
foreach (get_object_vars($oldcourse) as $field => $value) {
|
|
|
|
if ($field == 'summary_editor') {
|
|
|
|
if (($data->$field)['text'] !== $value['text']) {
|
|
|
|
// The summary might be very long, we don't wan't to fill up the log record with the full text.
|
|
|
|
$updatedfields[$field] = '(updated)';
|
|
|
|
}
|
2020-01-24 12:19:22 -07:00
|
|
|
} else if ($field == 'tags' && isset($data->tags)) {
|
2019-04-16 18:31:51 +01:00
|
|
|
// Tags might not have the same array keys, just check the values.
|
|
|
|
if (array_values($data->$field) !== array_values($value)) {
|
|
|
|
$updatedfields[$field] = $data->$field;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (isset($data->$field) && $data->$field != $value) {
|
|
|
|
$updatedfields[$field] = $data->$field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$data->timemodified = time();
|
|
|
|
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
if ($editoroptions) {
|
2010-07-03 13:37:13 +00:00
|
|
|
$data = file_postupdate_standard_editor($data, 'summary', $editoroptions, $context, 'course', 'summary', 0);
|
2009-03-20 06:53:03 +00:00
|
|
|
}
|
2013-03-28 12:18:32 +11:00
|
|
|
if ($overviewfilesoptions = course_overviewfiles_options($data->id)) {
|
|
|
|
$data = file_postupdate_standard_filemanager($data, 'overviewfiles', $overviewfilesoptions, $context, 'course', 'overviewfiles', 0);
|
|
|
|
}
|
2009-03-20 06:53:03 +00:00
|
|
|
|
2013-09-04 21:21:41 +12:00
|
|
|
// Check we don't have a duplicate shortname.
|
|
|
|
if (!empty($data->shortname) && $oldcourse->shortname != $data->shortname) {
|
2014-04-03 11:07:01 +08:00
|
|
|
if ($DB->record_exists_sql('SELECT id from {course} WHERE shortname = ? AND id <> ?', array($data->shortname, $data->id))) {
|
2013-09-04 21:21:41 +12:00
|
|
|
throw new moodle_exception('shortnametaken', '', '', $data->shortname);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check we don't have a duplicate idnumber.
|
|
|
|
if (!empty($data->idnumber) && $oldcourse->idnumber != $data->idnumber) {
|
2014-04-03 11:07:01 +08:00
|
|
|
if ($DB->record_exists_sql('SELECT id from {course} WHERE idnumber = ? AND id <> ?', array($data->idnumber, $data->id))) {
|
2013-09-04 21:21:41 +12:00
|
|
|
throw new moodle_exception('courseidnumbertaken', '', '', $data->idnumber);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-01 13:29:19 +08:00
|
|
|
if ($errorcode = course_validate_dates((array)$data)) {
|
|
|
|
throw new moodle_exception($errorcode);
|
|
|
|
}
|
|
|
|
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
if (!isset($data->category) or empty($data->category)) {
|
|
|
|
// prevent nulls and 0 in category field
|
2007-03-23 02:29:05 +00:00
|
|
|
unset($data->category);
|
|
|
|
}
|
2013-05-08 11:02:45 +10:00
|
|
|
$changesincoursecat = $movecat = (isset($data->category) and $oldcourse->category != $data->category);
|
2007-03-23 02:29:05 +00:00
|
|
|
|
2010-07-14 14:23:32 +00:00
|
|
|
if (!isset($data->visible)) {
|
|
|
|
// data not from form, add missing visibility info
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
$data->visible = $oldcourse->visible;
|
|
|
|
}
|
2007-03-23 02:29:05 +00:00
|
|
|
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
if ($data->visible != $oldcourse->visible) {
|
|
|
|
// reset the visibleold flag when manually hiding/unhiding course
|
|
|
|
$data->visibleold = $data->visible;
|
2013-05-09 14:22:26 +08:00
|
|
|
$changesincoursecat = true;
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
} else {
|
2007-09-19 07:13:57 +00:00
|
|
|
if ($movecat) {
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
$newcategory = $DB->get_record('course_categories', array('id'=>$data->category));
|
|
|
|
if (empty($newcategory->visible)) {
|
|
|
|
// make sure when moving into hidden category the course is hidden automatically
|
|
|
|
$data->visible = 0;
|
|
|
|
}
|
2007-09-19 07:13:57 +00:00
|
|
|
}
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
}
|
2007-09-19 07:13:57 +00:00
|
|
|
|
2017-03-10 18:20:30 +01:00
|
|
|
// Set newsitems to 0 if format does not support announcements.
|
|
|
|
if (isset($data->format)) {
|
|
|
|
$newcourseformat = course_get_format((object)['format' => $data->format]);
|
2017-03-22 09:26:13 +08:00
|
|
|
if (!$newcourseformat->supports_news()) {
|
2017-03-10 18:20:30 +01:00
|
|
|
$data->newsitems = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-22 22:41:03 +08:00
|
|
|
// Set showcompletionconditions to null when completion tracking has been disabled for the course.
|
|
|
|
if (isset($data->enablecompletion) && $data->enablecompletion == COMPLETION_DISABLED) {
|
|
|
|
$data->showcompletionconditions = null;
|
|
|
|
}
|
|
|
|
|
2019-01-11 10:56:14 +01:00
|
|
|
// Update custom fields if there are any of them in the form.
|
|
|
|
$handler = core_course\customfield\course_handler::create();
|
|
|
|
$handler->instance_form_save($data);
|
|
|
|
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
// Update with the new data
|
2013-08-21 15:11:59 +12:00
|
|
|
$DB->update_record('course', $data);
|
2012-10-10 11:54:48 +08:00
|
|
|
// make sure the modinfo cache is reset
|
|
|
|
rebuild_course_cache($data->id);
|
2007-03-23 02:29:05 +00:00
|
|
|
|
2021-03-15 12:50:35 +11:00
|
|
|
// Purge course image cache in case if course image has been updated.
|
|
|
|
\cache::make('core', 'course_image')->delete($data->id);
|
|
|
|
|
2012-10-11 13:31:55 +08:00
|
|
|
// update course format options with full course data
|
|
|
|
course_get_format($data->id)->update_course_format_options($data, $oldcourse);
|
|
|
|
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
$course = $DB->get_record('course', array('id'=>$data->id));
|
2007-03-23 02:29:05 +00:00
|
|
|
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
if ($movecat) {
|
2012-07-23 15:59:04 +08:00
|
|
|
$newparent = context_coursecat::instance($course->category);
|
2013-07-05 11:27:23 +08:00
|
|
|
$context->update_moved($newparent);
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
}
|
2013-09-04 11:07:13 +08:00
|
|
|
$fixcoursesortorder = $movecat || (isset($data->sortorder) && ($oldcourse->sortorder != $data->sortorder));
|
|
|
|
if ($fixcoursesortorder) {
|
2013-09-03 13:45:00 +10:00
|
|
|
fix_course_sortorder();
|
|
|
|
}
|
|
|
|
|
2013-02-28 12:29:15 +11:00
|
|
|
// purge appropriate caches in case fix_course_sortorder() did not change anything
|
|
|
|
cache_helper::purge_by_event('changesincourse');
|
2013-05-09 14:22:26 +08:00
|
|
|
if ($changesincoursecat) {
|
2013-05-08 11:02:45 +10:00
|
|
|
cache_helper::purge_by_event('changesincoursecat');
|
|
|
|
}
|
2007-03-23 02:29:05 +00:00
|
|
|
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
// Test for and remove blocks which aren't appropriate anymore
|
|
|
|
blocks_remove_inappropriate($course);
|
2007-03-23 02:29:05 +00:00
|
|
|
|
MDL-21782 reworked enrolment framework, the core infrastructure is in place, the basic plugins are all implemented; see the tracker issue for list of unfinished bits, expect more changes and improvements during the next week
AMOS START
MOV [sendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage,enrol_self]
MOV [configsendcoursewelcomemessage,core_admin],[sendcoursewelcomemessage_desc,enrol_self]
MOV [enrolstartdate,core],[enrolstartdate,enrol_self]
MOV [enrolenddate,core],[enrolenddate,enrol_self]
CPY [welcometocourse,core],[welcometocourse,enrol_self]
CPY [welcometocoursetext,core],[welcometocoursetext,enrol_self]
MOV [notenrollable,core],[notenrollable,core_enrol]
MOV [enrolenddaterror,core],[enrolenddaterror,enrol_self]
MOV [enrolmentkeyhint,core],[passwordinvalidhint,enrol_self]
MOV [coursemanager,core_admin],[coursecontact,core_admin]
MOV [configcoursemanager,core_admin],[coursecontact_desc,core_admin]
MOV [enrolledincourserole,core],[enrolledincourserole,enrol_manual]
MOV [enrolme,core],[enrolme,core_enrol]
MOV [unenrol,core],[unenrol,core_enrol]
MOV [unenrolme,core],[unenrolme,core_enrol]
MOV [enrolmentnew,core],[enrolmentnew,core_enrol]
MOV [enrolmentnewuser,core],[enrolmentnewuser,core_enrol]
MOV [enrolments,core],[enrolments,core_enrol]
MOV [enrolperiod,core],[enrolperiod,core_enrol]
MOV [unenrolroleusers,core],[unenrolroleusers,core_enrol]
AMOS END
2010-06-21 15:30:49 +00:00
|
|
|
// Save any custom role names.
|
|
|
|
save_local_role_names($course->id, $data);
|
|
|
|
|
|
|
|
// update enrol settings
|
|
|
|
enrol_course_updated(false, $course, $data);
|
|
|
|
|
2015-08-22 20:55:37 +08:00
|
|
|
// Update course tags.
|
2015-09-05 23:05:45 +08:00
|
|
|
if (isset($data->tags)) {
|
|
|
|
core_tag_tag::set_item_tags('core', 'course', $course->id, context_course::instance($course->id), $data->tags);
|
2015-08-22 20:55:37 +08:00
|
|
|
}
|
|
|
|
|
2013-08-08 16:50:10 +08:00
|
|
|
// Trigger a course updated event.
|
|
|
|
$event = \core\event\course_updated::create(array(
|
|
|
|
'objectid' => $course->id,
|
2013-09-04 11:07:13 +08:00
|
|
|
'context' => context_course::instance($course->id),
|
2013-08-08 16:50:10 +08:00
|
|
|
'other' => array('shortname' => $course->shortname,
|
2019-04-16 18:31:51 +01:00
|
|
|
'fullname' => $course->fullname,
|
|
|
|
'updatedfields' => $updatedfields)
|
2013-08-08 16:50:10 +08:00
|
|
|
));
|
2013-09-04 11:07:13 +08:00
|
|
|
|
2013-08-08 16:50:10 +08:00
|
|
|
$event->trigger();
|
2012-10-11 13:31:55 +08:00
|
|
|
|
|
|
|
if ($oldcourse->format !== $course->format) {
|
|
|
|
// Remove all options stored for the previous format
|
|
|
|
// We assume that new course format migrated everything it needed watching trigger
|
|
|
|
// 'course_updated' and in method format_XXX::update_course_format_options()
|
|
|
|
$DB->delete_records('course_format_options',
|
|
|
|
array('courseid' => $course->id, 'format' => $oldcourse->format));
|
|
|
|
}
|
2007-03-23 02:29:05 +00:00
|
|
|
}
|
2007-02-16 08:46:55 +00:00
|
|
|
|
2010-05-01 05:05:55 +00:00
|
|
|
/**
|
2020-02-26 15:36:26 +01:00
|
|
|
* Calculate the average number of enrolled participants per course.
|
|
|
|
*
|
|
|
|
* This is intended for statistics purposes during the site registration. Only visible courses are taken into account.
|
|
|
|
* Front page enrolments are excluded.
|
|
|
|
*
|
|
|
|
* @param bool $onlyactive Consider only active enrolments in enabled plugins and obey the enrolment time restrictions.
|
|
|
|
* @param int $lastloginsince If specified, count only users who logged in after this timestamp.
|
|
|
|
* @return float
|
2010-05-01 05:05:55 +00:00
|
|
|
*/
|
2020-02-26 15:36:26 +01:00
|
|
|
function average_number_of_participants(bool $onlyactive = false, int $lastloginsince = null): float {
|
|
|
|
global $DB;
|
2010-10-18 08:01:32 +00:00
|
|
|
|
MDL-75208 oracle: Amend a couple of queries to make Oracle happy
The changes introduced here are completely safe, just we stop
binding SITEID and, instead, embed it in the SQL.
Why? Because Oracle 21 has started to return non-sense results
when SITEID is bound.
After lots of tests, attempts, debugging... we have been unable
to find any logic to the need of this change and also, have been
unable to reproduce the problem with a standalone script that
pretty much runs the same queries that the ones changed here.
I'm sure that there is something, somewhere, but have failed
to find it, grrr.
Please read MDL-75208 and linked issues to find more information
about this problem, that is one of the biggest mysteries I've
seen recently. Maybe at the end there is a tiny detail that
explains it all, but it's really well hidden.
2022-07-13 23:44:16 +02:00
|
|
|
$params = [];
|
2020-02-26 15:36:26 +01:00
|
|
|
|
|
|
|
$sql = "SELECT DISTINCT ue.userid, e.courseid
|
|
|
|
FROM {user_enrolments} ue
|
|
|
|
JOIN {enrol} e ON e.id = ue.enrolid
|
|
|
|
JOIN {course} c ON c.id = e.courseid ";
|
|
|
|
|
|
|
|
if ($onlyactive || $lastloginsince) {
|
|
|
|
$sql .= "JOIN {user} u ON u.id = ue.userid ";
|
|
|
|
}
|
2010-10-18 08:01:32 +00:00
|
|
|
|
MDL-75208 oracle: Amend a couple of queries to make Oracle happy
The changes introduced here are completely safe, just we stop
binding SITEID and, instead, embed it in the SQL.
Why? Because Oracle 21 has started to return non-sense results
when SITEID is bound.
After lots of tests, attempts, debugging... we have been unable
to find any logic to the need of this change and also, have been
unable to reproduce the problem with a standalone script that
pretty much runs the same queries that the ones changed here.
I'm sure that there is something, somewhere, but have failed
to find it, grrr.
Please read MDL-75208 and linked issues to find more information
about this problem, that is one of the biggest mysteries I've
seen recently. Maybe at the end there is a tiny detail that
explains it all, but it's really well hidden.
2022-07-13 23:44:16 +02:00
|
|
|
$sql .= "WHERE e.courseid <> " . SITEID . " AND c.visible = 1 ";
|
2010-10-18 08:01:32 +00:00
|
|
|
|
2020-02-26 15:36:26 +01:00
|
|
|
if ($onlyactive) {
|
|
|
|
$sql .= "AND ue.status = :active
|
|
|
|
AND e.status = :enabled
|
|
|
|
AND ue.timestart < :now1
|
|
|
|
AND (ue.timeend = 0 OR ue.timeend > :now2) ";
|
|
|
|
|
|
|
|
// Same as in the enrollib - the rounding should help caching in the database.
|
|
|
|
$now = round(time(), -2);
|
|
|
|
|
|
|
|
$params += [
|
|
|
|
'active' => ENROL_USER_ACTIVE,
|
|
|
|
'enabled' => ENROL_INSTANCE_ENABLED,
|
|
|
|
'now1' => $now,
|
|
|
|
'now2' => $now,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($lastloginsince) {
|
|
|
|
$sql .= "AND u.lastlogin > :lastlogin ";
|
|
|
|
$params['lastlogin'] = $lastloginsince;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sql = "SELECT COUNT(*)
|
|
|
|
FROM ($sql) total";
|
|
|
|
|
|
|
|
$enrolmenttotal = $DB->count_records_sql($sql, $params);
|
|
|
|
|
|
|
|
// Get the number of visible courses (exclude the front page).
|
|
|
|
$coursetotal = $DB->count_records('course', ['visible' => 1]);
|
|
|
|
$coursetotal = $coursetotal - 1;
|
2010-10-18 08:01:32 +00:00
|
|
|
|
|
|
|
if (empty($coursetotal)) {
|
|
|
|
$participantaverage = 0;
|
2020-02-26 15:36:26 +01:00
|
|
|
|
2010-10-18 08:01:32 +00:00
|
|
|
} else {
|
|
|
|
$participantaverage = $enrolmenttotal / $coursetotal;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $participantaverage;
|
2010-05-01 05:05:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-10-18 08:01:32 +00:00
|
|
|
* Average number of course modules
|
2010-05-01 05:05:55 +00:00
|
|
|
* @return integer
|
|
|
|
*/
|
|
|
|
function average_number_of_courses_modules() {
|
2010-10-18 08:01:32 +00:00
|
|
|
global $DB, $SITE;
|
|
|
|
|
|
|
|
//count total of visible course module (except front page)
|
|
|
|
$sql = 'SELECT COUNT(*) FROM (
|
|
|
|
SELECT cm.course, cm.module
|
|
|
|
FROM {course} c, {course_modules} cm
|
2010-11-17 06:23:56 +00:00
|
|
|
WHERE c.id = cm.course
|
2010-10-18 08:01:32 +00:00
|
|
|
AND c.id <> :siteid
|
|
|
|
AND cm.visible = 1
|
2012-07-30 20:04:55 +12:00
|
|
|
AND c.visible = 1) total';
|
2010-10-18 08:01:32 +00:00
|
|
|
$params = array('siteid' => $SITE->id);
|
|
|
|
$moduletotal = $DB->count_records_sql($sql, $params);
|
|
|
|
|
|
|
|
|
|
|
|
//count total of visible courses (minus front page)
|
|
|
|
$coursetotal = $DB->count_records('course', array('visible' => 1));
|
|
|
|
$coursetotal = $coursetotal - 1 ;
|
|
|
|
|
|
|
|
//average of course module
|
|
|
|
if (empty($coursetotal)) {
|
|
|
|
$coursemoduleaverage = 0;
|
|
|
|
} else {
|
|
|
|
$coursemoduleaverage = $moduletotal / $coursetotal;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $coursemoduleaverage;
|
2010-05-01 05:05:55 +00:00
|
|
|
}
|
|
|
|
|
2009-11-04 06:14:06 +00:00
|
|
|
/**
|
|
|
|
* This class pertains to course requests and contains methods associated with
|
|
|
|
* create, approving, and removing course requests.
|
|
|
|
*
|
2010-07-03 13:37:13 +00:00
|
|
|
* Please note we do not allow embedded images here because there is no context
|
|
|
|
* to store them with proper access control.
|
|
|
|
*
|
2009-11-04 06:14:06 +00:00
|
|
|
* @copyright 2009 Sam Hemelryk
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
* @since Moodle 2.0
|
|
|
|
*
|
|
|
|
* @property-read int $id
|
|
|
|
* @property-read string $fullname
|
|
|
|
* @property-read string $shortname
|
|
|
|
* @property-read string $summary
|
|
|
|
* @property-read int $summaryformat
|
|
|
|
* @property-read int $summarytrust
|
|
|
|
* @property-read string $reason
|
|
|
|
* @property-read int $requester
|
|
|
|
*/
|
|
|
|
class course_request {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the stdClass that stores the properties for the course request
|
2010-09-17 12:16:27 +00:00
|
|
|
* and is externally accessed through the __get magic method
|
2009-11-04 06:14:06 +00:00
|
|
|
* @var stdClass
|
|
|
|
*/
|
|
|
|
protected $properties;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An array of options for the summary editor used by course request forms.
|
|
|
|
* This is initially set by {@link summary_editor_options()}
|
|
|
|
* @var array
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
protected static $summaryeditoroptions;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Static function to prepare the summary editor for working with a course
|
|
|
|
* request.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @param null|stdClass $data Optional, an object containing the default values
|
|
|
|
* for the form, these may be modified when preparing the
|
|
|
|
* editor so this should be called before creating the form
|
|
|
|
* @return stdClass An object that can be used to set the default values for
|
|
|
|
* an mforms form
|
|
|
|
*/
|
|
|
|
public static function prepare($data=null) {
|
|
|
|
if ($data === null) {
|
|
|
|
$data = new stdClass;
|
|
|
|
}
|
2010-07-03 13:37:13 +00:00
|
|
|
$data = file_prepare_standard_editor($data, 'summary', self::summary_editor_options());
|
2009-11-04 06:14:06 +00:00
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Static function to create a new course request when passed an array of properties
|
|
|
|
* for it.
|
|
|
|
*
|
|
|
|
* This function also handles saving any files that may have been used in the editor
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
* @param stdClass $data
|
|
|
|
* @return course_request The newly created course request
|
|
|
|
*/
|
|
|
|
public static function create($data) {
|
|
|
|
global $USER, $DB, $CFG;
|
|
|
|
$data->requester = $USER->id;
|
2010-07-03 13:37:13 +00:00
|
|
|
|
2012-09-19 16:50:18 +08:00
|
|
|
// Setting the default category if none set.
|
2019-10-14 12:16:31 +02:00
|
|
|
if (empty($data->category) || !empty($CFG->lockrequestcategory)) {
|
2012-08-16 14:10:39 +08:00
|
|
|
$data->category = $CFG->defaultrequestcategory;
|
|
|
|
}
|
|
|
|
|
2010-07-03 13:37:13 +00:00
|
|
|
// Summary is a required field so copy the text over
|
|
|
|
$data->summary = $data->summary_editor['text'];
|
|
|
|
$data->summaryformat = $data->summary_editor['format'];
|
|
|
|
|
2009-11-04 06:14:06 +00:00
|
|
|
$data->id = $DB->insert_record('course_request', $data);
|
2010-07-03 13:37:13 +00:00
|
|
|
|
2009-11-04 06:14:06 +00:00
|
|
|
// Create a new course_request object and return it
|
|
|
|
$request = new course_request($data);
|
|
|
|
|
|
|
|
// Notify the admin if required.
|
2010-05-18 16:59:08 +00:00
|
|
|
if ($users = get_users_from_config($CFG->courserequestnotify, 'moodle/site:approvecourse')) {
|
2009-11-04 08:11:02 +00:00
|
|
|
|
2009-11-04 06:14:06 +00:00
|
|
|
$a = new stdClass;
|
|
|
|
$a->link = "$CFG->wwwroot/course/pending.php";
|
|
|
|
$a->user = fullname($USER);
|
|
|
|
$subject = get_string('courserequest');
|
|
|
|
$message = get_string('courserequestnotifyemail', 'admin', $a);
|
|
|
|
foreach ($users as $user) {
|
2010-08-01 09:52:54 +00:00
|
|
|
$request->notify($user, $USER, 'courserequested', $subject, $message);
|
2009-11-04 06:14:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $request;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of options to use with a summary editor
|
|
|
|
*
|
|
|
|
* @uses course_request::$summaryeditoroptions
|
|
|
|
* @return array An array of options to use with the editor
|
|
|
|
*/
|
|
|
|
public static function summary_editor_options() {
|
|
|
|
global $CFG;
|
|
|
|
if (self::$summaryeditoroptions === null) {
|
2010-07-03 13:37:13 +00:00
|
|
|
self::$summaryeditoroptions = array('maxfiles' => 0, 'maxbytes'=>0);
|
2009-11-04 06:14:06 +00:00
|
|
|
}
|
|
|
|
return self::$summaryeditoroptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads the properties for this course request object. Id is required and if
|
|
|
|
* only id is provided then we load the rest of the properties from the database
|
|
|
|
*
|
|
|
|
* @param stdClass|int $properties Either an object containing properties
|
|
|
|
* or the course_request id to load
|
|
|
|
*/
|
|
|
|
public function __construct($properties) {
|
|
|
|
global $DB;
|
|
|
|
if (empty($properties->id)) {
|
|
|
|
if (empty($properties)) {
|
|
|
|
throw new coding_exception('You must provide a course request id when creating a course_request object');
|
|
|
|
}
|
|
|
|
$id = $properties;
|
|
|
|
$properties = new stdClass;
|
|
|
|
$properties->id = (int)$id;
|
|
|
|
unset($id);
|
|
|
|
}
|
|
|
|
if (empty($properties->requester)) {
|
|
|
|
if (!($this->properties = $DB->get_record('course_request', array('id' => $properties->id)))) {
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('unknowncourserequest');
|
2009-11-04 06:14:06 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->properties = $properties;
|
|
|
|
}
|
|
|
|
$this->properties->collision = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the requested property
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function __get($key) {
|
|
|
|
return $this->properties->$key;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override this to ensure empty($request->blah) calls return a reliable answer...
|
|
|
|
*
|
|
|
|
* This is required because we define the __get method
|
|
|
|
*
|
|
|
|
* @param mixed $key
|
|
|
|
* @return bool True is it not empty, false otherwise
|
|
|
|
*/
|
|
|
|
public function __isset($key) {
|
|
|
|
return (!empty($this->properties->$key));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the user who requested this course
|
|
|
|
*
|
|
|
|
* Uses a static var to cache the results and cut down the number of db queries
|
|
|
|
*
|
|
|
|
* @staticvar array $requesters An array of cached users
|
|
|
|
* @return stdClass The user who requested the course
|
|
|
|
*/
|
|
|
|
public function get_requester() {
|
|
|
|
global $DB;
|
|
|
|
static $requesters= array();
|
|
|
|
if (!array_key_exists($this->properties->requester, $requesters)) {
|
|
|
|
$requesters[$this->properties->requester] = $DB->get_record('user', array('id'=>$this->properties->requester));
|
|
|
|
}
|
|
|
|
return $requesters[$this->properties->requester];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks that the shortname used by the course does not conflict with any other
|
|
|
|
* courses that exist
|
|
|
|
*
|
|
|
|
* @param string|null $shortnamemark The string to append to the requests shortname
|
|
|
|
* should a conflict be found
|
|
|
|
* @return bool true is there is a conflict, false otherwise
|
|
|
|
*/
|
|
|
|
public function check_shortname_collision($shortnamemark = '[*]') {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
if ($this->properties->collision !== null) {
|
|
|
|
return $this->properties->collision;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($this->properties->shortname)) {
|
|
|
|
debugging('Attempting to check a course request shortname before it has been set', DEBUG_DEVELOPER);
|
|
|
|
$this->properties->collision = false;
|
|
|
|
} else if ($DB->record_exists('course', array('shortname' => $this->properties->shortname))) {
|
|
|
|
if (!empty($shortnamemark)) {
|
|
|
|
$this->properties->shortname .= ' '.$shortnamemark;
|
|
|
|
}
|
|
|
|
$this->properties->collision = true;
|
|
|
|
} else {
|
|
|
|
$this->properties->collision = false;
|
|
|
|
}
|
|
|
|
return $this->properties->collision;
|
|
|
|
}
|
|
|
|
|
2019-09-23 16:37:23 +02:00
|
|
|
/**
|
|
|
|
* Checks user capability to approve a requested course
|
|
|
|
*
|
|
|
|
* If course was requested without category for some reason (might happen if $CFG->defaultrequestcategory is
|
|
|
|
* misconfigured), we check capabilities 'moodle/site:approvecourse' and 'moodle/course:changecategory'.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function can_approve() {
|
|
|
|
global $CFG;
|
|
|
|
$category = null;
|
|
|
|
if ($this->properties->category) {
|
|
|
|
$category = core_course_category::get($this->properties->category, IGNORE_MISSING);
|
|
|
|
} else if ($CFG->defaultrequestcategory) {
|
|
|
|
$category = core_course_category::get($CFG->defaultrequestcategory, IGNORE_MISSING);
|
|
|
|
}
|
|
|
|
if ($category) {
|
|
|
|
return has_capability('moodle/site:approvecourse', $category->get_context());
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can not determine the context where the course should be created. The approver should have
|
|
|
|
// both capabilities to approve courses and change course category in the system context.
|
|
|
|
return has_all_capabilities(['moodle/site:approvecourse', 'moodle/course:changecategory'], context_system::instance());
|
|
|
|
}
|
|
|
|
|
2013-02-21 11:22:20 +11:00
|
|
|
/**
|
|
|
|
* Returns the category where this course request should be created
|
|
|
|
*
|
|
|
|
* Note that we don't check here that user has a capability to view
|
|
|
|
* hidden categories if he has capabilities 'moodle/site:approvecourse' and
|
|
|
|
* 'moodle/course:changecategory'
|
|
|
|
*
|
2018-06-16 11:57:27 +08:00
|
|
|
* @return core_course_category
|
2013-02-21 11:22:20 +11:00
|
|
|
*/
|
|
|
|
public function get_category() {
|
|
|
|
global $CFG;
|
2019-09-23 16:37:23 +02:00
|
|
|
if ($this->properties->category && ($category = core_course_category::get($this->properties->category, IGNORE_MISSING))) {
|
|
|
|
return $category;
|
|
|
|
} else if ($CFG->defaultrequestcategory &&
|
|
|
|
($category = core_course_category::get($CFG->defaultrequestcategory, IGNORE_MISSING))) {
|
|
|
|
return $category;
|
|
|
|
} else {
|
|
|
|
return core_course_category::get_default();
|
2013-02-21 11:22:20 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-04 06:14:06 +00:00
|
|
|
/**
|
|
|
|
* This function approves the request turning it into a course
|
|
|
|
*
|
|
|
|
* This function converts the course request into a course, at the same time
|
2010-09-17 12:16:27 +00:00
|
|
|
* transferring any files used in the summary to the new course and then removing
|
2009-11-04 06:14:06 +00:00
|
|
|
* the course request and the files associated with it.
|
|
|
|
*
|
|
|
|
* @return int The id of the course that was created from this request
|
|
|
|
*/
|
|
|
|
public function approve() {
|
|
|
|
global $CFG, $DB, $USER;
|
2010-07-10 15:46:07 +00:00
|
|
|
|
2018-04-17 11:49:34 +02:00
|
|
|
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
|
|
|
|
|
2010-07-10 15:46:07 +00:00
|
|
|
$user = $DB->get_record('user', array('id' => $this->properties->requester, 'deleted'=>0), '*', MUST_EXIST);
|
|
|
|
|
2009-11-04 06:14:06 +00:00
|
|
|
$courseconfig = get_config('moodlecourse');
|
|
|
|
|
|
|
|
// Transfer appropriate settings
|
2010-07-10 15:46:07 +00:00
|
|
|
$data = clone($this->properties);
|
|
|
|
unset($data->id);
|
|
|
|
unset($data->reason);
|
|
|
|
unset($data->requester);
|
2009-11-04 06:14:06 +00:00
|
|
|
|
|
|
|
// Set category
|
2013-02-21 11:22:20 +11:00
|
|
|
$category = $this->get_category();
|
2010-07-10 15:46:07 +00:00
|
|
|
$data->category = $category->id;
|
2009-11-04 06:14:06 +00:00
|
|
|
// Set misc settings
|
2010-07-10 15:46:07 +00:00
|
|
|
$data->requested = 1;
|
2009-11-04 06:14:06 +00:00
|
|
|
|
|
|
|
// Apply course default settings
|
2010-07-10 15:46:07 +00:00
|
|
|
$data->format = $courseconfig->format;
|
|
|
|
$data->newsitems = $courseconfig->newsitems;
|
|
|
|
$data->showgrades = $courseconfig->showgrades;
|
|
|
|
$data->showreports = $courseconfig->showreports;
|
|
|
|
$data->maxbytes = $courseconfig->maxbytes;
|
|
|
|
$data->groupmode = $courseconfig->groupmode;
|
|
|
|
$data->groupmodeforce = $courseconfig->groupmodeforce;
|
|
|
|
$data->visible = $courseconfig->visible;
|
|
|
|
$data->visibleold = $data->visible;
|
|
|
|
$data->lang = $courseconfig->lang;
|
2016-04-27 15:55:30 +08:00
|
|
|
$data->enablecompletion = $courseconfig->enablecompletion;
|
2017-06-30 09:32:18 +08:00
|
|
|
$data->numsections = $courseconfig->numsections;
|
2017-12-29 20:13:48 +05:30
|
|
|
$data->startdate = usergetmidnight(time());
|
|
|
|
if ($courseconfig->courseenddateenabled) {
|
|
|
|
$data->enddate = usergetmidnight(time()) + $courseconfig->courseduration;
|
|
|
|
}
|
2009-11-04 06:14:06 +00:00
|
|
|
|
2018-04-17 11:49:34 +02:00
|
|
|
list($data->fullname, $data->shortname) = restore_dbops::calculate_course_names(0, $data->fullname, $data->shortname);
|
|
|
|
|
2010-07-10 15:46:07 +00:00
|
|
|
$course = create_course($data);
|
2012-07-24 11:49:44 +08:00
|
|
|
$context = context_course::instance($course->id, MUST_EXIST);
|
2009-11-04 06:14:06 +00:00
|
|
|
|
2010-07-10 15:46:07 +00:00
|
|
|
// add enrol instances
|
|
|
|
if (!$DB->record_exists('enrol', array('courseid'=>$course->id, 'enrol'=>'manual'))) {
|
|
|
|
if ($manual = enrol_get_plugin('manual')) {
|
|
|
|
$manual->add_default_instance($course);
|
|
|
|
}
|
2009-11-04 06:14:06 +00:00
|
|
|
}
|
2010-07-10 15:46:07 +00:00
|
|
|
|
|
|
|
// enrol the requester as teacher if necessary
|
|
|
|
if (!empty($CFG->creatornewroleid) and !is_viewing($context, $user, 'moodle/role:assign') and !is_enrolled($context, $user, 'moodle/role:assign')) {
|
|
|
|
enrol_try_internal_enrol($course->id, $user->id, $CFG->creatornewroleid);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->delete();
|
|
|
|
|
2010-09-21 08:13:11 +00:00
|
|
|
$a = new stdClass();
|
2012-07-23 15:59:04 +08:00
|
|
|
$a->name = format_string($course->fullname, true, array('context' => context_course::instance($course->id)));
|
2010-07-10 15:46:07 +00:00
|
|
|
$a->url = $CFG->wwwroot.'/course/view.php?id=' . $course->id;
|
2016-10-31 14:13:13 -04:00
|
|
|
$this->notify($user, $USER, 'courserequestapproved', get_string('courseapprovedsubject'), get_string('courseapprovedemail2', 'moodle', $a), $course->id);
|
2010-07-10 15:46:07 +00:00
|
|
|
|
|
|
|
return $course->id;
|
2009-11-04 06:14:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reject a course request
|
|
|
|
*
|
|
|
|
* This function rejects a course request, emailing the requesting user the
|
|
|
|
* provided notice and then removing the request from the database
|
|
|
|
*
|
|
|
|
* @param string $notice The message to display to the user
|
|
|
|
*/
|
|
|
|
public function reject($notice) {
|
2010-07-10 15:46:07 +00:00
|
|
|
global $USER, $DB;
|
|
|
|
$user = $DB->get_record('user', array('id' => $this->properties->requester), '*', MUST_EXIST);
|
2009-11-04 06:14:06 +00:00
|
|
|
$this->notify($user, $USER, 'courserequestrejected', get_string('courserejectsubject'), get_string('courserejectemail', 'moodle', $notice));
|
|
|
|
$this->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes the course request and any associated files
|
|
|
|
*/
|
|
|
|
public function delete() {
|
|
|
|
global $DB;
|
|
|
|
$DB->delete_records('course_request', array('id' => $this->properties->id));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send a message from one user to another using events_trigger
|
|
|
|
*
|
|
|
|
* @param object $touser
|
|
|
|
* @param object $fromuser
|
|
|
|
* @param string $name
|
|
|
|
* @param string $subject
|
|
|
|
* @param string $message
|
2016-10-31 14:13:13 -04:00
|
|
|
* @param int|null $courseid
|
2009-11-04 06:14:06 +00:00
|
|
|
*/
|
2021-02-15 15:17:38 +01:00
|
|
|
protected function notify($touser, $fromuser, $name, $subject, $message, $courseid = null) {
|
2016-07-20 12:40:34 +01:00
|
|
|
$eventdata = new \core\message\message();
|
2016-10-31 14:13:13 -04:00
|
|
|
$eventdata->courseid = empty($courseid) ? SITEID : $courseid;
|
2011-01-04 13:35:08 +08:00
|
|
|
$eventdata->component = 'moodle';
|
2009-11-04 06:14:06 +00:00
|
|
|
$eventdata->name = $name;
|
|
|
|
$eventdata->userfrom = $fromuser;
|
|
|
|
$eventdata->userto = $touser;
|
|
|
|
$eventdata->subject = $subject;
|
|
|
|
$eventdata->fullmessage = $message;
|
|
|
|
$eventdata->fullmessageformat = FORMAT_PLAIN;
|
|
|
|
$eventdata->fullmessagehtml = '';
|
|
|
|
$eventdata->smallmessage = '';
|
2011-01-04 13:35:08 +08:00
|
|
|
$eventdata->notification = 1;
|
2009-11-07 10:27:57 +00:00
|
|
|
message_send($eventdata);
|
2009-11-04 06:14:06 +00:00
|
|
|
}
|
2019-09-23 16:37:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if current user can request a course in this context
|
|
|
|
*
|
|
|
|
* @param context $context
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function can_request(context $context) {
|
|
|
|
global $CFG;
|
|
|
|
if (empty($CFG->enablecourserequests)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (has_capability('moodle/course:create', $context)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($context instanceof context_system) {
|
|
|
|
$defaultcontext = context_coursecat::instance($CFG->defaultrequestcategory, IGNORE_MISSING);
|
|
|
|
return $defaultcontext &&
|
|
|
|
has_capability('moodle/course:request', $defaultcontext);
|
|
|
|
} else if ($context instanceof context_coursecat) {
|
2019-10-14 12:16:31 +02:00
|
|
|
if (!$CFG->lockrequestcategory || $CFG->defaultrequestcategory == $context->instanceid) {
|
2019-09-23 16:37:23 +02:00
|
|
|
return has_capability('moodle/course:request', $context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2010-02-25 10:17:46 +00:00
|
|
|
}
|
MDL-26105 Block settings should contains less options, and be more user friendly
AMOS BEGIN
MOV [page-blog-index, pagetype], [page-blog-index, blog]
MOV [page-blog-x, pagetype], [page-blog-x, blog]
MOV [page-tag-x, pagetype], [page-tag-x, tag]
MOV [page-course-view-weeks, pagetype], [page-course-view-weeks, format_weeks]
MOV [page-course-view-weeks-x, pagetype], [page-course-view-weeks-x, format_weeks]
MOV [page-course-view-topics, pagetype], [page-course-view-topics, format_topics]
MOV [page-course-view-topics-x, pagetype], [page-course-view-topics-x, format_topics]
AMOS END
2011-04-28 11:20:30 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a list of page types
|
|
|
|
* @param string $pagetype current page type
|
2013-03-28 16:18:54 +11:00
|
|
|
* @param context $parentcontext Block's parent context
|
|
|
|
* @param context $currentcontext Current context of block
|
|
|
|
* @return array array of page types
|
MDL-26105 Block settings should contains less options, and be more user friendly
AMOS BEGIN
MOV [page-blog-index, pagetype], [page-blog-index, blog]
MOV [page-blog-x, pagetype], [page-blog-x, blog]
MOV [page-tag-x, pagetype], [page-tag-x, tag]
MOV [page-course-view-weeks, pagetype], [page-course-view-weeks, format_weeks]
MOV [page-course-view-weeks-x, pagetype], [page-course-view-weeks-x, format_weeks]
MOV [page-course-view-topics, pagetype], [page-course-view-topics, format_topics]
MOV [page-course-view-topics-x, pagetype], [page-course-view-topics-x, format_topics]
AMOS END
2011-04-28 11:20:30 +08:00
|
|
|
*/
|
2011-06-17 16:23:10 +08:00
|
|
|
function course_page_type_list($pagetype, $parentcontext, $currentcontext) {
|
2013-03-28 16:18:54 +11:00
|
|
|
if ($pagetype === 'course-index' || $pagetype === 'course-index-category') {
|
|
|
|
// For courses and categories browsing pages (/course/index.php) add option to show on ANY category page
|
|
|
|
$pagetypes = array('*' => get_string('page-x', 'pagetype'),
|
|
|
|
'course-index-*' => get_string('page-course-index-x', 'pagetype'),
|
|
|
|
);
|
|
|
|
} else if ($currentcontext && (!($coursecontext = $currentcontext->get_course_context(false)) || $coursecontext->instanceid == SITEID)) {
|
|
|
|
// We know for sure that despite pagetype starts with 'course-' this is not a page in course context (i.e. /course/search.php, etc.)
|
|
|
|
$pagetypes = array('*' => get_string('page-x', 'pagetype'));
|
|
|
|
} else {
|
|
|
|
// Otherwise consider it a page inside a course even if $currentcontext is null
|
|
|
|
$pagetypes = array('*' => get_string('page-x', 'pagetype'),
|
|
|
|
'course-*' => get_string('page-course-x', 'pagetype'),
|
|
|
|
'course-view-*' => get_string('page-course-view-x', 'pagetype')
|
|
|
|
);
|
MDL-26105 Block settings should contains less options, and be more user friendly
AMOS BEGIN
MOV [page-blog-index, pagetype], [page-blog-index, blog]
MOV [page-blog-x, pagetype], [page-blog-x, blog]
MOV [page-tag-x, pagetype], [page-tag-x, tag]
MOV [page-course-view-weeks, pagetype], [page-course-view-weeks, format_weeks]
MOV [page-course-view-weeks-x, pagetype], [page-course-view-weeks-x, format_weeks]
MOV [page-course-view-topics, pagetype], [page-course-view-topics, format_topics]
MOV [page-course-view-topics-x, pagetype], [page-course-view-topics-x, format_topics]
AMOS END
2011-04-28 11:20:30 +08:00
|
|
|
}
|
2013-03-28 16:18:54 +11:00
|
|
|
return $pagetypes;
|
2011-07-03 22:12:05 +01:00
|
|
|
}
|
MDL-31096 Convert course resource/section javascript to YUI3 Module
AMOS BEGIN
MOV [hideweekfromothers,moodle],[hidefromothers,format_week]
MOV [showweekfromothers,moodle],[showfromothers,format_week]
MOV [hidetopicfromothers,moodle],[hidefromothers,format_topics]
MOV [showtopicfromothers,moodle],[showfromothers,format_topics]
AMOS END
2012-01-05 16:53:02 +00:00
|
|
|
|
2012-05-09 10:18:39 +01:00
|
|
|
/**
|
|
|
|
* Determine whether course ajax should be enabled for the specified course
|
|
|
|
*
|
2012-05-14 13:41:15 +12:00
|
|
|
* @param stdClass $course The course to test against
|
2012-05-09 10:18:39 +01:00
|
|
|
* @return boolean Whether course ajax is enabled or note
|
|
|
|
*/
|
|
|
|
function course_ajax_enabled($course) {
|
|
|
|
global $CFG, $PAGE, $SITE;
|
|
|
|
|
|
|
|
// The user must be editing for AJAX to be included
|
|
|
|
if (!$PAGE->user_is_editing()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the theme suports
|
|
|
|
if (!$PAGE->theme->enablecourseajax) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the course format supports ajax functionality
|
|
|
|
// The site 'format' doesn't have information on course format support
|
|
|
|
if ($SITE->id !== $course->id) {
|
|
|
|
$courseformatajaxsupport = course_format_ajax_support($course->format);
|
|
|
|
if (!$courseformatajaxsupport->capable) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// All conditions have been met so course ajax should be enabled
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
MDL-31096 Convert course resource/section javascript to YUI3 Module
AMOS BEGIN
MOV [hideweekfromothers,moodle],[hidefromothers,format_week]
MOV [showweekfromothers,moodle],[showfromothers,format_week]
MOV [hidetopicfromothers,moodle],[hidefromothers,format_topics]
MOV [showtopicfromothers,moodle],[showfromothers,format_topics]
AMOS END
2012-01-05 16:53:02 +00:00
|
|
|
/**
|
|
|
|
* Include the relevant javascript and language strings for the resource
|
|
|
|
* toolbox YUI module
|
|
|
|
*
|
|
|
|
* @param integer $id The ID of the course being applied to
|
2012-05-17 10:55:35 +12:00
|
|
|
* @param array $usedmodules An array containing the names of the modules in use on the page
|
|
|
|
* @param array $enabledmodules An array containing the names of the enabled (visible) modules on this site
|
2012-05-14 13:41:15 +12:00
|
|
|
* @param stdClass $config An object containing configuration parameters for ajax modules including:
|
MDL-31096 Convert course resource/section javascript to YUI3 Module
AMOS BEGIN
MOV [hideweekfromothers,moodle],[hidefromothers,format_week]
MOV [showweekfromothers,moodle],[showfromothers,format_week]
MOV [hidetopicfromothers,moodle],[hidefromothers,format_topics]
MOV [showtopicfromothers,moodle],[showfromothers,format_topics]
AMOS END
2012-01-05 16:53:02 +00:00
|
|
|
* * resourceurl The URL to post changes to for resource changes
|
|
|
|
* * sectionurl The URL to post changes to for section changes
|
|
|
|
* * pageparams Additional parameters to pass through in the post
|
2012-05-17 10:55:35 +12:00
|
|
|
* @return bool
|
MDL-31096 Convert course resource/section javascript to YUI3 Module
AMOS BEGIN
MOV [hideweekfromothers,moodle],[hidefromothers,format_week]
MOV [showweekfromothers,moodle],[showfromothers,format_week]
MOV [hidetopicfromothers,moodle],[hidefromothers,format_topics]
MOV [showtopicfromothers,moodle],[showfromothers,format_topics]
AMOS END
2012-01-05 16:53:02 +00:00
|
|
|
*/
|
2012-05-17 10:55:35 +12:00
|
|
|
function include_course_ajax($course, $usedmodules = array(), $enabledmodules = null, $config = null) {
|
2013-08-12 11:34:51 +08:00
|
|
|
global $CFG, $PAGE, $SITE;
|
MDL-31096 Convert course resource/section javascript to YUI3 Module
AMOS BEGIN
MOV [hideweekfromothers,moodle],[hidefromothers,format_week]
MOV [showweekfromothers,moodle],[showfromothers,format_week]
MOV [hidetopicfromothers,moodle],[hidefromothers,format_topics]
MOV [showtopicfromothers,moodle],[showfromothers,format_topics]
AMOS END
2012-01-05 16:53:02 +00:00
|
|
|
|
2021-06-22 13:27:59 +02:00
|
|
|
// Init the course editor module to support UI components.
|
|
|
|
$format = course_get_format($course);
|
|
|
|
include_course_editor($format);
|
|
|
|
|
MDL-31096 Convert course resource/section javascript to YUI3 Module
AMOS BEGIN
MOV [hideweekfromothers,moodle],[hidefromothers,format_week]
MOV [showweekfromothers,moodle],[showfromothers,format_week]
MOV [hidetopicfromothers,moodle],[hidefromothers,format_topics]
MOV [showtopicfromothers,moodle],[showfromothers,format_topics]
AMOS END
2012-01-05 16:53:02 +00:00
|
|
|
// Ensure that ajax should be included
|
2012-05-09 10:18:39 +01:00
|
|
|
if (!course_ajax_enabled($course)) {
|
|
|
|
return false;
|
MDL-31096 Convert course resource/section javascript to YUI3 Module
AMOS BEGIN
MOV [hideweekfromothers,moodle],[hidefromothers,format_week]
MOV [showweekfromothers,moodle],[showfromothers,format_week]
MOV [hidetopicfromothers,moodle],[hidefromothers,format_topics]
MOV [showtopicfromothers,moodle],[showfromothers,format_topics]
AMOS END
2012-01-05 16:53:02 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 13:27:59 +02:00
|
|
|
// Component based formats don't use YUI drag and drop anymore.
|
|
|
|
if (!$format->supports_components() && course_format_uses_sections($course->format)) {
|
MDL-31096 Convert course resource/section javascript to YUI3 Module
AMOS BEGIN
MOV [hideweekfromothers,moodle],[hidefromothers,format_week]
MOV [showweekfromothers,moodle],[showfromothers,format_week]
MOV [hidetopicfromothers,moodle],[hidefromothers,format_topics]
MOV [showtopicfromothers,moodle],[showfromothers,format_topics]
AMOS END
2012-01-05 16:53:02 +00:00
|
|
|
|
2021-06-22 13:27:59 +02:00
|
|
|
if (!$config) {
|
|
|
|
$config = new stdClass();
|
|
|
|
}
|
MDL-31096 Convert course resource/section javascript to YUI3 Module
AMOS BEGIN
MOV [hideweekfromothers,moodle],[hidefromothers,format_week]
MOV [showweekfromothers,moodle],[showfromothers,format_week]
MOV [hidetopicfromothers,moodle],[hidefromothers,format_topics]
MOV [showtopicfromothers,moodle],[showfromothers,format_topics]
AMOS END
2012-01-05 16:53:02 +00:00
|
|
|
|
2021-06-30 18:06:56 +02:00
|
|
|
// The URL to use for resource changes.
|
2021-06-22 13:27:59 +02:00
|
|
|
if (!isset($config->resourceurl)) {
|
|
|
|
$config->resourceurl = '/course/rest.php';
|
|
|
|
}
|
MDL-31096 Convert course resource/section javascript to YUI3 Module
AMOS BEGIN
MOV [hideweekfromothers,moodle],[hidefromothers,format_week]
MOV [showweekfromothers,moodle],[showfromothers,format_week]
MOV [hidetopicfromothers,moodle],[hidefromothers,format_topics]
MOV [showtopicfromothers,moodle],[showfromothers,format_topics]
AMOS END
2012-01-05 16:53:02 +00:00
|
|
|
|
2021-06-30 18:06:56 +02:00
|
|
|
// The URL to use for section changes.
|
2021-06-22 13:27:59 +02:00
|
|
|
if (!isset($config->sectionurl)) {
|
|
|
|
$config->sectionurl = '/course/rest.php';
|
|
|
|
}
|
|
|
|
|
2021-06-30 18:06:56 +02:00
|
|
|
// Any additional parameters which need to be included on page submission.
|
2021-06-22 13:27:59 +02:00
|
|
|
if (!isset($config->pageparams)) {
|
|
|
|
$config->pageparams = array();
|
|
|
|
}
|
MDL-31096 Convert course resource/section javascript to YUI3 Module
AMOS BEGIN
MOV [hideweekfromothers,moodle],[hidefromothers,format_week]
MOV [showweekfromothers,moodle],[showfromothers,format_week]
MOV [hidetopicfromothers,moodle],[hidefromothers,format_topics]
MOV [showtopicfromothers,moodle],[showfromothers,format_topics]
AMOS END
2012-01-05 16:53:02 +00:00
|
|
|
|
2012-04-27 15:01:02 +01:00
|
|
|
$PAGE->requires->yui_module('moodle-course-dragdrop', 'M.course.init_section_dragdrop',
|
2012-01-18 10:53:23 +00:00
|
|
|
array(array(
|
|
|
|
'courseid' => $course->id,
|
2012-04-20 15:55:40 +01:00
|
|
|
'ajaxurl' => $config->sectionurl,
|
2012-01-18 10:53:23 +00:00
|
|
|
'config' => $config,
|
|
|
|
)), null, true);
|
|
|
|
|
2012-04-27 15:01:02 +01:00
|
|
|
$PAGE->requires->yui_module('moodle-course-dragdrop', 'M.course.init_resource_dragdrop',
|
2012-01-18 10:53:23 +00:00
|
|
|
array(array(
|
|
|
|
'courseid' => $course->id,
|
2012-04-20 15:55:40 +01:00
|
|
|
'ajaxurl' => $config->resourceurl,
|
2012-01-18 10:53:23 +00:00
|
|
|
'config' => $config,
|
|
|
|
)), null, true);
|
2012-04-20 15:55:40 +01:00
|
|
|
|
2022-12-01 15:44:38 +01:00
|
|
|
// Require various strings for the command toolbox.
|
|
|
|
$PAGE->requires->strings_for_js(array(
|
MDL-31096 Convert course resource/section javascript to YUI3 Module
AMOS BEGIN
MOV [hideweekfromothers,moodle],[hidefromothers,format_week]
MOV [showweekfromothers,moodle],[showfromothers,format_week]
MOV [hidetopicfromothers,moodle],[hidefromothers,format_topics]
MOV [showtopicfromothers,moodle],[showfromothers,format_topics]
AMOS END
2012-01-05 16:53:02 +00:00
|
|
|
'moveleft',
|
|
|
|
'deletechecktype',
|
|
|
|
'deletechecktypename',
|
2012-05-08 12:13:19 +01:00
|
|
|
'edittitle',
|
|
|
|
'edittitleinstructions',
|
MDL-31096 Convert course resource/section javascript to YUI3 Module
AMOS BEGIN
MOV [hideweekfromothers,moodle],[hidefromothers,format_week]
MOV [showweekfromothers,moodle],[showfromothers,format_week]
MOV [hidetopicfromothers,moodle],[hidefromothers,format_topics]
MOV [showtopicfromothers,moodle],[showfromothers,format_topics]
AMOS END
2012-01-05 16:53:02 +00:00
|
|
|
'show',
|
|
|
|
'hide',
|
2015-09-17 10:40:31 -04:00
|
|
|
'highlight',
|
|
|
|
'highlightoff',
|
MDL-31096 Convert course resource/section javascript to YUI3 Module
AMOS BEGIN
MOV [hideweekfromothers,moodle],[hidefromothers,format_week]
MOV [showweekfromothers,moodle],[showfromothers,format_week]
MOV [hidetopicfromothers,moodle],[hidefromothers,format_topics]
MOV [showtopicfromothers,moodle],[showfromothers,format_topics]
AMOS END
2012-01-05 16:53:02 +00:00
|
|
|
'groupsnone',
|
|
|
|
'groupsvisible',
|
|
|
|
'groupsseparate',
|
|
|
|
'clicktochangeinbrackets',
|
|
|
|
'markthistopic',
|
|
|
|
'markedthistopic',
|
2012-01-18 10:53:23 +00:00
|
|
|
'movesection',
|
2014-01-30 00:32:25 +08:00
|
|
|
'movecoursemodule',
|
|
|
|
'movecoursesection',
|
2013-08-15 14:39:00 +08:00
|
|
|
'movecontent',
|
2013-08-15 16:03:44 +08:00
|
|
|
'tocontent',
|
2014-01-30 00:32:25 +08:00
|
|
|
'emptydragdropregion',
|
|
|
|
'afterresource',
|
|
|
|
'aftersection',
|
|
|
|
'totopofsection',
|
MDL-31096 Convert course resource/section javascript to YUI3 Module
AMOS BEGIN
MOV [hideweekfromothers,moodle],[hidefromothers,format_week]
MOV [showweekfromothers,moodle],[showfromothers,format_week]
MOV [hidetopicfromothers,moodle],[hidefromothers,format_topics]
MOV [showtopicfromothers,moodle],[showfromothers,format_topics]
AMOS END
2012-01-05 16:53:02 +00:00
|
|
|
), 'moodle');
|
|
|
|
|
2022-12-01 15:44:38 +01:00
|
|
|
// Include section-specific strings for formats which support sections.
|
|
|
|
if (course_format_uses_sections($course->format)) {
|
|
|
|
$PAGE->requires->strings_for_js(array(
|
|
|
|
'showfromothers',
|
|
|
|
'hidefromothers',
|
|
|
|
), 'format_' . $course->format);
|
|
|
|
}
|
MDL-31096 Convert course resource/section javascript to YUI3 Module
AMOS BEGIN
MOV [hideweekfromothers,moodle],[hidefromothers,format_week]
MOV [showweekfromothers,moodle],[showfromothers,format_week]
MOV [hidetopicfromothers,moodle],[hidefromothers,format_topics]
MOV [showtopicfromothers,moodle],[showfromothers,format_topics]
AMOS END
2012-01-05 16:53:02 +00:00
|
|
|
|
2022-12-01 15:44:38 +01:00
|
|
|
// For confirming resource deletion we need the name of the module in question.
|
|
|
|
foreach ($usedmodules as $module => $modname) {
|
|
|
|
$PAGE->requires->string_for_js('pluginname', $module);
|
|
|
|
}
|
2012-05-09 15:31:41 +01:00
|
|
|
|
2022-12-01 15:44:38 +01:00
|
|
|
// Load drag and drop upload AJAX.
|
|
|
|
require_once($CFG->dirroot.'/course/dnduploadlib.php');
|
|
|
|
dndupload_add_to_course($course, $enabledmodules);
|
|
|
|
}
|
2012-05-17 10:55:35 +12:00
|
|
|
|
2017-02-03 09:40:46 +08:00
|
|
|
$PAGE->requires->js_call_amd('core_course/actions', 'initCoursePage', array($course->format));
|
|
|
|
|
2012-05-09 10:18:39 +01:00
|
|
|
return true;
|
MDL-31096 Convert course resource/section javascript to YUI3 Module
AMOS BEGIN
MOV [hideweekfromothers,moodle],[hidefromothers,format_week]
MOV [showweekfromothers,moodle],[showfromothers,format_week]
MOV [hidetopicfromothers,moodle],[hidefromothers,format_topics]
MOV [showtopicfromothers,moodle],[showfromothers,format_topics]
AMOS END
2012-01-05 16:53:02 +00:00
|
|
|
}
|
2012-04-26 21:36:02 +08:00
|
|
|
|
2021-06-21 11:59:53 +02:00
|
|
|
/**
|
|
|
|
* Include and configure the course editor modules.
|
|
|
|
*
|
|
|
|
* @param course_format $format the course format instance.
|
|
|
|
*/
|
|
|
|
function include_course_editor(course_format $format) {
|
|
|
|
global $PAGE, $SITE;
|
|
|
|
|
|
|
|
$course = $format->get_course();
|
|
|
|
|
|
|
|
if ($SITE->id === $course->id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-24 12:39:58 +01:00
|
|
|
$statekey = course_format::session_cache($course);
|
|
|
|
|
2021-06-21 11:59:53 +02:00
|
|
|
// Edition mode and some format specs must be passed to the init method.
|
|
|
|
$setup = (object)[
|
|
|
|
'editing' => $format->show_editor(),
|
2021-06-21 15:47:09 +02:00
|
|
|
'supportscomponents' => $format->supports_components(),
|
2022-01-24 12:39:58 +01:00
|
|
|
'statekey' => $statekey,
|
2023-01-31 17:29:14 +01:00
|
|
|
'overriddenStrings' => $format->get_editor_custom_strings(),
|
2021-06-21 11:59:53 +02:00
|
|
|
];
|
|
|
|
// All the new editor elements will be loaded after the course is presented and
|
|
|
|
// the initial course state will be generated using core_course_get_state webservice.
|
|
|
|
$PAGE->requires->js_call_amd('core_courseformat/courseeditor', 'setViewFormat', [$course->id, $setup]);
|
|
|
|
}
|
|
|
|
|
2012-10-18 16:03:11 +08:00
|
|
|
/**
|
|
|
|
* Returns the sorted list of available course formats, filtered by enabled if necessary
|
|
|
|
*
|
|
|
|
* @param bool $enabledonly return only formats that are enabled
|
|
|
|
* @return array array of sorted format names
|
|
|
|
*/
|
|
|
|
function get_sorted_course_formats($enabledonly = false) {
|
|
|
|
global $CFG;
|
2013-07-16 22:36:11 +02:00
|
|
|
$formats = core_component::get_plugin_list('format');
|
2012-10-18 16:03:11 +08:00
|
|
|
|
|
|
|
if (!empty($CFG->format_plugins_sortorder)) {
|
|
|
|
$order = explode(',', $CFG->format_plugins_sortorder);
|
|
|
|
$order = array_merge(array_intersect($order, array_keys($formats)),
|
|
|
|
array_diff(array_keys($formats), $order));
|
|
|
|
} else {
|
|
|
|
$order = array_keys($formats);
|
|
|
|
}
|
|
|
|
if (!$enabledonly) {
|
|
|
|
return $order;
|
|
|
|
}
|
|
|
|
$sortedformats = array();
|
|
|
|
foreach ($order as $formatname) {
|
|
|
|
if (!get_config('format_'.$formatname, 'disabled')) {
|
|
|
|
$sortedformats[] = $formatname;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $sortedformats;
|
|
|
|
}
|
|
|
|
|
2012-04-26 21:36:02 +08:00
|
|
|
/**
|
|
|
|
* The URL to use for the specified course (with section)
|
|
|
|
*
|
2012-09-18 09:57:37 +08:00
|
|
|
* @param int|stdClass $courseorid The course to get the section name for (either object or just course id)
|
|
|
|
* @param int|stdClass $section Section object from database or just field course_sections.section
|
2012-09-13 11:00:34 +08:00
|
|
|
* if omitted the course view page is returned
|
|
|
|
* @param array $options options for view URL. At the moment core uses:
|
|
|
|
* 'navigation' (bool) if true and section has no separate page, the function returns null
|
|
|
|
* 'sr' (int) used by multipage formats to specify to which section to return
|
2012-04-26 21:36:02 +08:00
|
|
|
* @return moodle_url The url of course
|
|
|
|
*/
|
2012-09-18 09:57:37 +08:00
|
|
|
function course_get_url($courseorid, $section = null, $options = array()) {
|
|
|
|
return course_get_format($courseorid)->get_view_url($section, $options);
|
2012-04-26 21:36:02 +08:00
|
|
|
}
|
2013-01-02 13:58:42 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a module.
|
2013-01-22 14:03:48 +08:00
|
|
|
*
|
2013-01-02 13:58:42 +08:00
|
|
|
* It includes:
|
|
|
|
* - capability checks and other checks
|
|
|
|
* - create the module from the module info
|
|
|
|
*
|
|
|
|
* @param object $module
|
|
|
|
* @return object the created module info
|
2013-10-18 10:46:09 +11:00
|
|
|
* @throws moodle_exception if user is not allowed to perform the action or module is not allowed in this course
|
2013-01-02 13:58:42 +08:00
|
|
|
*/
|
|
|
|
function create_module($moduleinfo) {
|
2013-08-21 13:42:30 +08:00
|
|
|
global $DB, $CFG;
|
2013-03-15 17:45:56 +08:00
|
|
|
|
|
|
|
require_once($CFG->dirroot . '/course/modlib.php');
|
|
|
|
|
2013-01-02 13:58:42 +08:00
|
|
|
// Check manadatory attributs.
|
|
|
|
$mandatoryfields = array('modulename', 'course', 'section', 'visible');
|
|
|
|
if (plugin_supports('mod', $moduleinfo->modulename, FEATURE_MOD_INTRO, true)) {
|
|
|
|
$mandatoryfields[] = 'introeditor';
|
|
|
|
}
|
|
|
|
foreach($mandatoryfields as $mandatoryfield) {
|
|
|
|
if (!isset($moduleinfo->{$mandatoryfield})) {
|
|
|
|
throw new moodle_exception('createmodulemissingattribut', '', '', $mandatoryfield);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-22 14:03:48 +08:00
|
|
|
// Some additional checks (capability / existing instances).
|
2013-08-21 13:42:30 +08:00
|
|
|
$course = $DB->get_record('course', array('id'=>$moduleinfo->course), '*', MUST_EXIST);
|
2013-01-02 13:58:42 +08:00
|
|
|
list($module, $context, $cw) = can_add_moduleinfo($course, $moduleinfo->modulename, $moduleinfo->section);
|
|
|
|
|
|
|
|
// Add the module.
|
|
|
|
$moduleinfo->module = $module->id;
|
|
|
|
$moduleinfo = add_moduleinfo($moduleinfo, $course, null);
|
|
|
|
|
|
|
|
return $moduleinfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a module.
|
2013-01-22 14:03:48 +08:00
|
|
|
*
|
2013-01-02 13:58:42 +08:00
|
|
|
* It includes:
|
2013-01-22 14:03:48 +08:00
|
|
|
* - capability and other checks
|
2013-01-02 13:58:42 +08:00
|
|
|
* - update the module
|
|
|
|
*
|
|
|
|
* @param object $module
|
|
|
|
* @return object the updated module info
|
2013-10-18 10:46:09 +11:00
|
|
|
* @throws moodle_exception if current user is not allowed to update the module
|
2013-01-02 13:58:42 +08:00
|
|
|
*/
|
|
|
|
function update_module($moduleinfo) {
|
2013-08-21 13:42:30 +08:00
|
|
|
global $DB, $CFG;
|
2013-03-15 17:45:56 +08:00
|
|
|
|
|
|
|
require_once($CFG->dirroot . '/course/modlib.php');
|
2013-01-02 13:58:42 +08:00
|
|
|
|
|
|
|
// Check the course module exists.
|
|
|
|
$cm = get_coursemodule_from_id('', $moduleinfo->coursemodule, 0, false, MUST_EXIST);
|
|
|
|
|
|
|
|
// Check the course exists.
|
2013-08-21 13:42:30 +08:00
|
|
|
$course = $DB->get_record('course', array('id'=>$cm->course), '*', MUST_EXIST);
|
2013-01-02 13:58:42 +08:00
|
|
|
|
2013-01-22 14:03:48 +08:00
|
|
|
// Some checks (capaibility / existing instances).
|
2013-01-02 13:58:42 +08:00
|
|
|
list($cm, $context, $module, $data, $cw) = can_update_moduleinfo($cm);
|
|
|
|
|
2013-01-22 14:03:48 +08:00
|
|
|
// Retrieve few information needed by update_moduleinfo.
|
2013-01-02 13:58:42 +08:00
|
|
|
$moduleinfo->modulename = $cm->modname;
|
|
|
|
if (!isset($moduleinfo->scale)) {
|
|
|
|
$moduleinfo->scale = 0;
|
|
|
|
}
|
|
|
|
$moduleinfo->type = 'mod';
|
|
|
|
|
2013-01-22 14:03:48 +08:00
|
|
|
// Update the module.
|
2013-01-02 13:58:42 +08:00
|
|
|
list($cm, $moduleinfo) = update_moduleinfo($cm, $moduleinfo, $course, null);
|
|
|
|
|
|
|
|
return $moduleinfo;
|
|
|
|
}
|
2013-03-26 17:22:22 +08:00
|
|
|
|
2013-08-07 15:51:56 +01:00
|
|
|
/**
|
2014-06-13 13:40:50 +08:00
|
|
|
* Duplicate a module on the course for ajax.
|
2013-08-07 15:51:56 +01:00
|
|
|
*
|
2014-06-13 13:40:50 +08:00
|
|
|
* @see mod_duplicate_module()
|
2013-08-07 15:51:56 +01:00
|
|
|
* @param object $course The course
|
|
|
|
* @param object $cm The course module to duplicate
|
2014-06-13 13:40:50 +08:00
|
|
|
* @param int $sr The section to link back to (used for creating the links)
|
2013-08-07 15:51:56 +01:00
|
|
|
* @throws moodle_exception if the plugin doesn't support duplication
|
|
|
|
* @return Object containing:
|
|
|
|
* - fullcontent: The HTML markup for the created CM
|
|
|
|
* - cmid: The CMID of the newly created CM
|
|
|
|
* - redirect: Whether to trigger a redirect following this change
|
|
|
|
*/
|
|
|
|
function mod_duplicate_activity($course, $cm, $sr = null) {
|
2014-06-13 13:40:50 +08:00
|
|
|
global $PAGE;
|
|
|
|
|
|
|
|
$newcm = duplicate_module($course, $cm);
|
|
|
|
|
|
|
|
$resp = new stdClass();
|
|
|
|
if ($newcm) {
|
2013-08-07 15:51:56 +01:00
|
|
|
|
2020-10-22 09:22:54 +02:00
|
|
|
$format = course_get_format($course);
|
|
|
|
$renderer = $format->get_renderer($PAGE);
|
|
|
|
$modinfo = $format->get_modinfo();
|
|
|
|
$section = $modinfo->get_section_info($newcm->sectionnum);
|
|
|
|
|
|
|
|
// Get the new element html content.
|
|
|
|
$resp->fullcontent = $renderer->course_section_updated_cm_item($format, $section, $newcm);
|
|
|
|
|
2014-06-13 13:40:50 +08:00
|
|
|
$resp->cmid = $newcm->id;
|
|
|
|
} else {
|
|
|
|
// Trigger a redirect.
|
|
|
|
$resp->redirect = true;
|
|
|
|
}
|
|
|
|
return $resp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Api to duplicate a module.
|
|
|
|
*
|
|
|
|
* @param object $course course object.
|
|
|
|
* @param object $cm course module object to be duplicated.
|
2022-11-29 13:08:44 +13:00
|
|
|
* @param int $sectionid section ID new course module will be placed in.
|
|
|
|
* @param bool $changename updates module name with text from duplicatedmodule lang string.
|
2014-06-13 13:40:50 +08:00
|
|
|
* @since Moodle 2.8
|
|
|
|
*
|
|
|
|
* @throws Exception
|
|
|
|
* @throws coding_exception
|
|
|
|
* @throws moodle_exception
|
|
|
|
* @throws restore_controller_exception
|
|
|
|
*
|
|
|
|
* @return cm_info|null cminfo object if we sucessfully duplicated the mod and found the new cm.
|
|
|
|
*/
|
2022-11-29 13:08:44 +13:00
|
|
|
function duplicate_module($course, $cm, int $sectionid = null, bool $changename = true): ?cm_info {
|
2014-06-13 13:40:50 +08:00
|
|
|
global $CFG, $DB, $USER;
|
2013-08-07 15:51:56 +01:00
|
|
|
require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
|
|
|
|
require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');
|
|
|
|
require_once($CFG->libdir . '/filelib.php');
|
|
|
|
|
|
|
|
$a = new stdClass();
|
|
|
|
$a->modtype = get_string('modulename', $cm->modname);
|
|
|
|
$a->modname = format_string($cm->name);
|
|
|
|
|
|
|
|
if (!plugin_supports('mod', $cm->modname, FEATURE_BACKUP_MOODLE2)) {
|
2014-06-13 13:40:50 +08:00
|
|
|
throw new moodle_exception('duplicatenosupport', 'error', '', $a);
|
2013-08-07 15:51:56 +01:00
|
|
|
}
|
|
|
|
|
2014-06-13 13:40:50 +08:00
|
|
|
// Backup the activity.
|
2013-08-07 15:51:56 +01:00
|
|
|
|
|
|
|
$bc = new backup_controller(backup::TYPE_1ACTIVITY, $cm->id, backup::FORMAT_MOODLE,
|
|
|
|
backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id);
|
|
|
|
|
|
|
|
$backupid = $bc->get_backupid();
|
|
|
|
$backupbasepath = $bc->get_plan()->get_basepath();
|
|
|
|
|
|
|
|
$bc->execute_plan();
|
|
|
|
|
|
|
|
$bc->destroy();
|
|
|
|
|
2014-06-13 13:40:50 +08:00
|
|
|
// Restore the backup immediately.
|
2013-08-07 15:51:56 +01:00
|
|
|
|
|
|
|
$rc = new restore_controller($backupid, $course->id,
|
|
|
|
backup::INTERACTIVE_NO, backup::MODE_IMPORT, $USER->id, backup::TARGET_CURRENT_ADDING);
|
|
|
|
|
2019-01-21 13:56:23 +08:00
|
|
|
// Make sure that the restore_general_groups setting is always enabled when duplicating an activity.
|
2018-10-15 14:24:19 +01:00
|
|
|
$plan = $rc->get_plan();
|
|
|
|
$groupsetting = $plan->get_setting('groups');
|
2019-01-21 13:56:23 +08:00
|
|
|
if (empty($groupsetting->get_value())) {
|
|
|
|
$groupsetting->set_value(true);
|
|
|
|
}
|
2018-10-15 14:24:19 +01:00
|
|
|
|
2013-08-07 15:51:56 +01:00
|
|
|
$cmcontext = context_module::instance($cm->id);
|
|
|
|
if (!$rc->execute_precheck()) {
|
|
|
|
$precheckresults = $rc->get_precheck_results();
|
|
|
|
if (is_array($precheckresults) && !empty($precheckresults['errors'])) {
|
|
|
|
if (empty($CFG->keeptempdirectoriesonbackup)) {
|
|
|
|
fulldelete($backupbasepath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$rc->execute_plan();
|
|
|
|
|
2014-06-13 13:40:50 +08:00
|
|
|
// Now a bit hacky part follows - we try to get the cmid of the newly
|
|
|
|
// restored copy of the module.
|
2013-08-07 15:51:56 +01:00
|
|
|
$newcmid = null;
|
|
|
|
$tasks = $rc->get_plan()->get_tasks();
|
|
|
|
foreach ($tasks as $task) {
|
|
|
|
if (is_subclass_of($task, 'restore_activity_task')) {
|
|
|
|
if ($task->get_old_contextid() == $cmcontext->id) {
|
|
|
|
$newcmid = $task->get_moduleid();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-22 10:17:59 +08:00
|
|
|
$rc->destroy();
|
|
|
|
|
|
|
|
if (empty($CFG->keeptempdirectoriesonbackup)) {
|
|
|
|
fulldelete($backupbasepath);
|
|
|
|
}
|
|
|
|
|
2014-06-13 13:40:50 +08:00
|
|
|
// If we know the cmid of the new course module, let us move it
|
2013-08-07 15:51:56 +01:00
|
|
|
// right below the original one. otherwise it will stay at the
|
2014-06-13 13:40:50 +08:00
|
|
|
// end of the section.
|
2013-08-07 15:51:56 +01:00
|
|
|
if ($newcmid) {
|
2018-09-19 16:55:20 +02:00
|
|
|
// Proceed with activity renaming before everything else. We don't use APIs here to avoid
|
|
|
|
// triggering a lot of create/update duplicated events.
|
|
|
|
$newcm = get_coursemodule_from_id($cm->modname, $newcmid, $cm->course);
|
2022-11-29 13:08:44 +13:00
|
|
|
if ($changename) {
|
|
|
|
// Add ' (copy)' to duplicates. Note we don't cleanup or validate lengths here. It comes
|
|
|
|
// from original name that was valid, so the copy should be too.
|
|
|
|
$newname = get_string('duplicatedmodule', 'moodle', $newcm->name);
|
|
|
|
$DB->set_field($cm->modname, 'name', $newname, ['id' => $newcm->instance]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$section = $DB->get_record('course_sections', ['id' => $sectionid ?? $cm->section, 'course' => $cm->course]);
|
|
|
|
if (isset($sectionid)) {
|
|
|
|
moveto_module($newcm, $section);
|
|
|
|
} else {
|
|
|
|
$modarray = explode(",", trim($section->sequence));
|
|
|
|
$cmindex = array_search($cm->id, $modarray);
|
|
|
|
if ($cmindex !== false && $cmindex < count($modarray) - 1) {
|
|
|
|
moveto_module($newcm, $section, $modarray[$cmindex + 1]);
|
|
|
|
}
|
2017-06-22 10:17:59 +08:00
|
|
|
}
|
2014-06-18 15:40:31 +08:00
|
|
|
|
2016-09-02 10:42:29 +08:00
|
|
|
// Update calendar events with the duplicated module.
|
2017-07-04 09:21:17 +08:00
|
|
|
// The following line is to be removed in MDL-58906.
|
|
|
|
course_module_update_calendar_events($newcm->modname, null, $newcm);
|
2016-09-02 10:42:29 +08:00
|
|
|
|
2014-06-18 15:40:31 +08:00
|
|
|
// Trigger course module created event. We can trigger the event only if we know the newcmid.
|
2017-06-22 10:17:59 +08:00
|
|
|
$newcm = get_fast_modinfo($cm->course)->get_cm($newcmid);
|
2014-06-18 15:40:31 +08:00
|
|
|
$event = \core\event\course_module_created::create_from_cm($newcm);
|
|
|
|
$event->trigger();
|
2013-08-07 15:51:56 +01:00
|
|
|
}
|
|
|
|
|
2014-06-13 13:40:50 +08:00
|
|
|
return isset($newcm) ? $newcm : null;
|
2013-08-07 15:51:56 +01:00
|
|
|
}
|
|
|
|
|
2013-03-20 13:17:48 +08:00
|
|
|
/**
|
|
|
|
* Compare two objects to find out their correct order based on timestamp (to be used by usort).
|
|
|
|
* Sorts by descending order of time.
|
|
|
|
*
|
|
|
|
* @param stdClass $a First object
|
|
|
|
* @param stdClass $b Second object
|
|
|
|
* @return int 0,1,-1 representing the order
|
|
|
|
*/
|
|
|
|
function compare_activities_by_time_desc($a, $b) {
|
|
|
|
// Make sure the activities actually have a timestamp property.
|
|
|
|
if ((!property_exists($a, 'timestamp')) && (!property_exists($b, 'timestamp'))) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// We treat instances without timestamp as if they have a timestamp of 0.
|
|
|
|
if ((!property_exists($a, 'timestamp')) && (property_exists($b,'timestamp'))) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if ((property_exists($a, 'timestamp')) && (!property_exists($b, 'timestamp'))) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ($a->timestamp == $b->timestamp) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ($a->timestamp > $b->timestamp) ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compare two objects to find out their correct order based on timestamp (to be used by usort).
|
|
|
|
* Sorts by ascending order of time.
|
|
|
|
*
|
|
|
|
* @param stdClass $a First object
|
|
|
|
* @param stdClass $b Second object
|
|
|
|
* @return int 0,1,-1 representing the order
|
|
|
|
*/
|
|
|
|
function compare_activities_by_time_asc($a, $b) {
|
|
|
|
// Make sure the activities actually have a timestamp property.
|
|
|
|
if ((!property_exists($a, 'timestamp')) && (!property_exists($b, 'timestamp'))) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
// We treat instances without timestamp as if they have a timestamp of 0.
|
|
|
|
if ((!property_exists($a, 'timestamp')) && (property_exists($b, 'timestamp'))) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if ((property_exists($a, 'timestamp')) && (!property_exists($b, 'timestamp'))) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if ($a->timestamp == $b->timestamp) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ($a->timestamp < $b->timestamp) ? -1 : 1;
|
|
|
|
}
|
2013-09-30 09:08:07 +13:00
|
|
|
|
|
|
|
/**
|
2013-10-01 09:02:08 +13:00
|
|
|
* Changes the visibility of a course.
|
2013-09-30 09:08:07 +13:00
|
|
|
*
|
|
|
|
* @param int $courseid The course to change.
|
|
|
|
* @param bool $show True to make it visible, false otherwise.
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function course_change_visibility($courseid, $show = true) {
|
|
|
|
$course = new stdClass;
|
|
|
|
$course->id = $courseid;
|
|
|
|
$course->visible = ($show) ? '1' : '0';
|
|
|
|
$course->visibleold = $course->visible;
|
|
|
|
update_course($course);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-10-01 09:40:29 +13:00
|
|
|
* Changes the course sortorder by one, moving it up or down one in respect to sort order.
|
2013-09-30 09:08:07 +13:00
|
|
|
*
|
2018-06-16 11:57:27 +08:00
|
|
|
* @param stdClass|core_course_list_element $course
|
2013-09-30 09:08:07 +13:00
|
|
|
* @param bool $up If set to true the course will be moved up one. Otherwise down one.
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-10-01 09:40:29 +13:00
|
|
|
function course_change_sortorder_by_one($course, $up) {
|
2013-09-30 09:08:07 +13:00
|
|
|
global $DB;
|
|
|
|
$params = array($course->sortorder, $course->category);
|
|
|
|
if ($up) {
|
|
|
|
$select = 'sortorder < ? AND category = ?';
|
|
|
|
$sort = 'sortorder DESC';
|
|
|
|
} else {
|
|
|
|
$select = 'sortorder > ? AND category = ?';
|
|
|
|
$sort = 'sortorder ASC';
|
|
|
|
}
|
|
|
|
fix_course_sortorder();
|
|
|
|
$swapcourse = $DB->get_records_select('course', $select, $params, $sort, '*', 0, 1);
|
|
|
|
if ($swapcourse) {
|
|
|
|
$swapcourse = reset($swapcourse);
|
|
|
|
$DB->set_field('course', 'sortorder', $swapcourse->sortorder, array('id' => $course->id));
|
|
|
|
$DB->set_field('course', 'sortorder', $course->sortorder, array('id' => $swapcourse->id));
|
|
|
|
// Finally reorder courses.
|
|
|
|
fix_course_sortorder();
|
|
|
|
cache_helper::purge_by_event('changesincourse');
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-10-01 09:40:29 +13:00
|
|
|
* Changes the sort order of courses in a category so that the first course appears after the second.
|
2013-09-30 09:08:07 +13:00
|
|
|
*
|
2013-10-01 09:40:29 +13:00
|
|
|
* @param int|stdClass $courseorid The course to focus on.
|
|
|
|
* @param int $moveaftercourseid The course to shifter after or 0 if you want it to be the first course in the category.
|
2013-09-30 09:08:07 +13:00
|
|
|
* @return bool
|
|
|
|
*/
|
2013-10-01 09:40:29 +13:00
|
|
|
function course_change_sortorder_after_course($courseorid, $moveaftercourseid) {
|
2013-09-30 09:08:07 +13:00
|
|
|
global $DB;
|
|
|
|
|
|
|
|
if (!is_object($courseorid)) {
|
|
|
|
$course = get_course($courseorid);
|
|
|
|
} else {
|
|
|
|
$course = $courseorid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((int)$moveaftercourseid === 0) {
|
|
|
|
// We've moving the course to the start of the queue.
|
2013-11-05 10:52:24 +08:00
|
|
|
$sql = 'SELECT sortorder
|
|
|
|
FROM {course}
|
|
|
|
WHERE category = :categoryid
|
|
|
|
ORDER BY sortorder';
|
2013-09-30 09:08:07 +13:00
|
|
|
$params = array(
|
|
|
|
'categoryid' => $course->category
|
|
|
|
);
|
|
|
|
$sortorder = $DB->get_field_sql($sql, $params, IGNORE_MULTIPLE);
|
|
|
|
|
2013-11-05 10:52:24 +08:00
|
|
|
$sql = 'UPDATE {course}
|
2013-09-30 09:08:07 +13:00
|
|
|
SET sortorder = sortorder + 1
|
2013-11-05 10:52:24 +08:00
|
|
|
WHERE category = :categoryid
|
|
|
|
AND id <> :id';
|
2013-09-30 09:08:07 +13:00
|
|
|
$params = array(
|
|
|
|
'categoryid' => $course->category,
|
|
|
|
'id' => $course->id,
|
|
|
|
);
|
|
|
|
$DB->execute($sql, $params);
|
|
|
|
$DB->set_field('course', 'sortorder', $sortorder, array('id' => $course->id));
|
|
|
|
} else if ($course->id === $moveaftercourseid) {
|
|
|
|
// They're the same - moronic.
|
|
|
|
debugging("Invalid move after course given.", DEBUG_DEVELOPER);
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
// Moving this course after the given course. It could be before it could be after.
|
|
|
|
$moveaftercourse = get_course($moveaftercourseid);
|
|
|
|
if ($course->category !== $moveaftercourse->category) {
|
|
|
|
debugging("Cannot re-order courses. The given courses do not belong to the same category.", DEBUG_DEVELOPER);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Increment all courses in the same category that are ordered after the moveafter course.
|
|
|
|
// This makes a space for the course we're moving.
|
2013-11-05 10:52:24 +08:00
|
|
|
$sql = 'UPDATE {course}
|
2013-09-30 09:08:07 +13:00
|
|
|
SET sortorder = sortorder + 1
|
2013-11-05 10:52:24 +08:00
|
|
|
WHERE category = :categoryid
|
2013-09-30 09:08:07 +13:00
|
|
|
AND sortorder > :sortorder';
|
|
|
|
$params = array(
|
|
|
|
'categoryid' => $moveaftercourse->category,
|
|
|
|
'sortorder' => $moveaftercourse->sortorder
|
|
|
|
);
|
|
|
|
$DB->execute($sql, $params);
|
|
|
|
$DB->set_field('course', 'sortorder', $moveaftercourse->sortorder + 1, array('id' => $course->id));
|
|
|
|
}
|
|
|
|
fix_course_sortorder();
|
|
|
|
cache_helper::purge_by_event('changesincourse');
|
|
|
|
return true;
|
|
|
|
}
|
2015-03-26 12:39:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Trigger course viewed event. This API function is used when course view actions happens,
|
|
|
|
* usually in course/view.php but also in external functions.
|
|
|
|
*
|
|
|
|
* @param stdClass $context course context object
|
|
|
|
* @param int $sectionnumber section number
|
|
|
|
* @since Moodle 2.9
|
|
|
|
*/
|
|
|
|
function course_view($context, $sectionnumber = 0) {
|
|
|
|
|
|
|
|
$eventdata = array('context' => $context);
|
|
|
|
|
|
|
|
if (!empty($sectionnumber)) {
|
|
|
|
$eventdata['other']['coursesectionnumber'] = $sectionnumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
$event = \core\event\course_viewed::create($eventdata);
|
|
|
|
$event->trigger();
|
2018-11-09 13:09:54 +08:00
|
|
|
|
|
|
|
user_accesstime_log($context->instanceid);
|
2015-03-26 12:39:55 +01:00
|
|
|
}
|
2015-09-05 23:05:45 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns courses tagged with a specified tag.
|
|
|
|
*
|
|
|
|
* @param core_tag_tag $tag
|
|
|
|
* @param bool $exclusivemode if set to true it means that no other entities tagged with this tag
|
|
|
|
* are displayed on the page and the per-page limit may be bigger
|
|
|
|
* @param int $fromctx context id where the link was displayed, may be used by callbacks
|
|
|
|
* to display items in the same context first
|
|
|
|
* @param int $ctx context id where to search for records
|
|
|
|
* @param bool $rec search in subcontexts as well
|
|
|
|
* @param int $page 0-based number of page being displayed
|
|
|
|
* @return \core_tag\output\tagindex
|
|
|
|
*/
|
|
|
|
function course_get_tagged_courses($tag, $exclusivemode = false, $fromctx = 0, $ctx = 0, $rec = 1, $page = 0) {
|
|
|
|
global $CFG, $PAGE;
|
|
|
|
|
|
|
|
$perpage = $exclusivemode ? $CFG->coursesperpage : 5;
|
|
|
|
$displayoptions = array(
|
|
|
|
'limit' => $perpage,
|
|
|
|
'offset' => $page * $perpage,
|
|
|
|
'viewmoreurl' => null,
|
|
|
|
);
|
|
|
|
|
|
|
|
$courserenderer = $PAGE->get_renderer('core', 'course');
|
2018-06-16 11:57:27 +08:00
|
|
|
$totalcount = core_course_category::search_courses_count(array('tagid' => $tag->id, 'ctx' => $ctx, 'rec' => $rec));
|
2015-09-05 23:05:45 +08:00
|
|
|
$content = $courserenderer->tagged_courses($tag->id, $exclusivemode, $ctx, $rec, $displayoptions);
|
|
|
|
$totalpages = ceil($totalcount / $perpage);
|
|
|
|
|
|
|
|
return new core_tag\output\tagindex($tag, 'core', 'course', $content,
|
|
|
|
$exclusivemode, $fromctx, $ctx, $rec, $page, $totalpages);
|
|
|
|
}
|
2016-01-22 13:01:29 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements callback inplace_editable() allowing to edit values in-place
|
|
|
|
*
|
|
|
|
* @param string $itemtype
|
|
|
|
* @param int $itemid
|
|
|
|
* @param mixed $newvalue
|
|
|
|
* @return \core\output\inplace_editable
|
|
|
|
*/
|
|
|
|
function core_course_inplace_editable($itemtype, $itemid, $newvalue) {
|
|
|
|
if ($itemtype === 'activityname') {
|
2022-06-14 12:28:55 +02:00
|
|
|
return \core_courseformat\output\local\content\cm\title::update($itemid, $newvalue);
|
2016-01-22 13:01:29 +08:00
|
|
|
}
|
|
|
|
}
|
2016-03-08 15:51:56 +08:00
|
|
|
|
2018-08-06 16:42:10 +10:00
|
|
|
/**
|
|
|
|
* This function calculates the minimum and maximum cutoff values for the timestart of
|
|
|
|
* the given event.
|
|
|
|
*
|
|
|
|
* It will return an array with two values, the first being the minimum cutoff value and
|
|
|
|
* the second being the maximum cutoff value. Either or both values can be null, which
|
|
|
|
* indicates there is no minimum or maximum, respectively.
|
|
|
|
*
|
|
|
|
* If a cutoff is required then the function must return an array containing the cutoff
|
|
|
|
* timestamp and error string to display to the user if the cutoff value is violated.
|
|
|
|
*
|
|
|
|
* A minimum and maximum cutoff return value will look like:
|
|
|
|
* [
|
|
|
|
* [1505704373, 'The date must be after this date'],
|
|
|
|
* [1506741172, 'The date must be before this date']
|
|
|
|
* ]
|
|
|
|
*
|
|
|
|
* @param calendar_event $event The calendar event to get the time range for
|
|
|
|
* @param stdClass $course The course object to get the range from
|
|
|
|
* @return array Returns an array with min and max date.
|
|
|
|
*/
|
|
|
|
function core_course_core_calendar_get_valid_event_timestart_range(\calendar_event $event, $course) {
|
|
|
|
$mindate = null;
|
|
|
|
$maxdate = null;
|
|
|
|
|
|
|
|
if ($course->startdate) {
|
|
|
|
$mindate = [
|
|
|
|
$course->startdate,
|
|
|
|
get_string('errorbeforecoursestart', 'calendar')
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [$mindate, $maxdate];
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:59:53 +02:00
|
|
|
/**
|
2021-06-22 13:27:59 +02:00
|
|
|
* Render the message drawer to be included in the top of the body of each page.
|
2021-06-21 11:59:53 +02:00
|
|
|
*
|
|
|
|
* @return string HTML
|
|
|
|
*/
|
|
|
|
function core_course_drawer(): string {
|
|
|
|
global $PAGE;
|
2021-08-31 19:06:46 +02:00
|
|
|
|
2021-12-22 16:05:38 +01:00
|
|
|
// Only add course index on non-site course pages.
|
|
|
|
if (!$PAGE->course || $PAGE->course->id == SITEID) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show course index to users can access the course only.
|
|
|
|
if (!can_access_course($PAGE->course)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$format = course_get_format($PAGE->course);
|
|
|
|
$renderer = $format->get_renderer($PAGE);
|
|
|
|
if (method_exists($renderer, 'course_index_drawer')) {
|
|
|
|
return $renderer->course_index_drawer($format);
|
2021-06-21 11:59:53 +02:00
|
|
|
}
|
|
|
|
|
2021-08-31 19:06:46 +02:00
|
|
|
return '';
|
2021-06-21 11:59:53 +02:00
|
|
|
}
|
|
|
|
|
2016-03-08 15:51:56 +08:00
|
|
|
/**
|
|
|
|
* Returns course modules tagged with a specified tag ready for output on tag/index.php page
|
|
|
|
*
|
|
|
|
* This is a callback used by the tag area core/course_modules to search for course modules
|
|
|
|
* tagged with a specific tag.
|
|
|
|
*
|
|
|
|
* @param core_tag_tag $tag
|
|
|
|
* @param bool $exclusivemode if set to true it means that no other entities tagged with this tag
|
|
|
|
* are displayed on the page and the per-page limit may be bigger
|
|
|
|
* @param int $fromcontextid context id where the link was displayed, may be used by callbacks
|
|
|
|
* to display items in the same context first
|
|
|
|
* @param int $contextid context id where to search for records
|
|
|
|
* @param bool $recursivecontext search in subcontexts as well
|
|
|
|
* @param int $page 0-based number of page being displayed
|
|
|
|
* @return \core_tag\output\tagindex
|
|
|
|
*/
|
|
|
|
function course_get_tagged_course_modules($tag, $exclusivemode = false, $fromcontextid = 0, $contextid = 0,
|
|
|
|
$recursivecontext = 1, $page = 0) {
|
|
|
|
global $OUTPUT;
|
|
|
|
$perpage = $exclusivemode ? 20 : 5;
|
|
|
|
|
|
|
|
// Build select query.
|
|
|
|
$ctxselect = context_helper::get_preload_record_columns_sql('ctx');
|
|
|
|
$query = "SELECT cm.id AS cmid, c.id AS courseid, $ctxselect
|
|
|
|
FROM {course_modules} cm
|
|
|
|
JOIN {tag_instance} tt ON cm.id = tt.itemid
|
|
|
|
JOIN {course} c ON cm.course = c.id
|
|
|
|
JOIN {context} ctx ON ctx.instanceid = cm.id AND ctx.contextlevel = :coursemodulecontextlevel
|
|
|
|
WHERE tt.itemtype = :itemtype AND tt.tagid = :tagid AND tt.component = :component
|
2016-11-15 14:33:46 +08:00
|
|
|
AND cm.deletioninprogress = 0
|
2016-03-08 15:51:56 +08:00
|
|
|
AND c.id %COURSEFILTER% AND cm.id %ITEMFILTER%";
|
|
|
|
|
|
|
|
$params = array('itemtype' => 'course_modules', 'tagid' => $tag->id, 'component' => 'core',
|
|
|
|
'coursemodulecontextlevel' => CONTEXT_MODULE);
|
|
|
|
if ($contextid) {
|
|
|
|
$context = context::instance_by_id($contextid);
|
|
|
|
$query .= $recursivecontext ? ' AND (ctx.id = :contextid OR ctx.path LIKE :path)' : ' AND ctx.id = :contextid';
|
|
|
|
$params['contextid'] = $context->id;
|
|
|
|
$params['path'] = $context->path.'/%';
|
|
|
|
}
|
|
|
|
|
|
|
|
$query .= ' ORDER BY';
|
|
|
|
if ($fromcontextid) {
|
|
|
|
// In order-clause specify that modules from inside "fromctx" context should be returned first.
|
|
|
|
$fromcontext = context::instance_by_id($fromcontextid);
|
|
|
|
$query .= ' (CASE WHEN ctx.id = :fromcontextid OR ctx.path LIKE :frompath THEN 0 ELSE 1 END),';
|
|
|
|
$params['fromcontextid'] = $fromcontext->id;
|
|
|
|
$params['frompath'] = $fromcontext->path.'/%';
|
|
|
|
}
|
|
|
|
$query .= ' c.sortorder, cm.id';
|
|
|
|
$totalpages = $page + 1;
|
|
|
|
|
|
|
|
// Use core_tag_index_builder to build and filter the list of items.
|
|
|
|
// Request one item more than we need so we know if next page exists.
|
|
|
|
$builder = new core_tag_index_builder('core', 'course_modules', $query, $params, $page * $perpage, $perpage + 1);
|
|
|
|
while ($item = $builder->has_item_that_needs_access_check()) {
|
|
|
|
context_helper::preload_from_record($item);
|
|
|
|
$courseid = $item->courseid;
|
|
|
|
if (!$builder->can_access_course($courseid)) {
|
|
|
|
$builder->set_accessible($item, false);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$modinfo = get_fast_modinfo($builder->get_course($courseid));
|
|
|
|
// Set accessibility of this item and all other items in the same course.
|
|
|
|
$builder->walk(function ($taggeditem) use ($courseid, $modinfo, $builder) {
|
|
|
|
if ($taggeditem->courseid == $courseid) {
|
|
|
|
$cm = $modinfo->get_cm($taggeditem->cmid);
|
|
|
|
$builder->set_accessible($taggeditem, $cm->uservisible);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$items = $builder->get_items();
|
|
|
|
if (count($items) > $perpage) {
|
|
|
|
$totalpages = $page + 2; // We don't need exact page count, just indicate that the next page exists.
|
|
|
|
array_pop($items);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the display contents.
|
|
|
|
if ($items) {
|
|
|
|
$tagfeed = new core_tag\output\tagfeed();
|
|
|
|
foreach ($items as $item) {
|
|
|
|
context_helper::preload_from_record($item);
|
|
|
|
$course = $builder->get_course($item->courseid);
|
|
|
|
$modinfo = get_fast_modinfo($course);
|
|
|
|
$cm = $modinfo->get_cm($item->cmid);
|
|
|
|
$courseurl = course_get_url($item->courseid, $cm->sectionnum);
|
|
|
|
$cmname = $cm->get_formatted_name();
|
|
|
|
if (!$exclusivemode) {
|
|
|
|
$cmname = shorten_text($cmname, 100);
|
|
|
|
}
|
|
|
|
$cmname = html_writer::link($cm->url?:$courseurl, $cmname);
|
|
|
|
$coursename = format_string($course->fullname, true,
|
|
|
|
array('context' => context_course::instance($item->courseid)));
|
|
|
|
$coursename = html_writer::link($courseurl, $coursename);
|
|
|
|
$icon = html_writer::empty_tag('img', array('src' => $cm->get_icon_url()));
|
|
|
|
$tagfeed->add($icon, $cmname, $coursename);
|
|
|
|
}
|
|
|
|
|
|
|
|
$content = $OUTPUT->render_from_template('core_tag/tagfeed',
|
|
|
|
$tagfeed->export_for_template($OUTPUT));
|
|
|
|
|
|
|
|
return new core_tag\output\tagindex($tag, 'core', 'course_modules', $content,
|
|
|
|
$exclusivemode, $fromcontextid, $contextid, $recursivecontext, $page, $totalpages);
|
|
|
|
}
|
|
|
|
}
|
2016-08-02 11:49:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an object with the list of navigation options in a course that are avaialable or not for the current user.
|
|
|
|
* This function also handles the frontpage course.
|
|
|
|
*
|
|
|
|
* @param stdClass $context context object (it can be a course context or the system context for frontpage settings)
|
2016-10-14 11:58:14 +08:00
|
|
|
* @param stdClass $course the course where the settings are being rendered
|
2016-08-02 11:49:14 +02:00
|
|
|
* @return stdClass the navigation options in a course and their availability status
|
|
|
|
* @since Moodle 3.2
|
|
|
|
*/
|
|
|
|
function course_get_user_navigation_options($context, $course = null) {
|
2022-01-20 10:30:42 +01:00
|
|
|
global $CFG, $USER;
|
2016-08-02 11:49:14 +02:00
|
|
|
|
|
|
|
$isloggedin = isloggedin();
|
|
|
|
$isguestuser = isguestuser();
|
|
|
|
$isfrontpage = $context->contextlevel == CONTEXT_SYSTEM;
|
|
|
|
|
|
|
|
if ($isfrontpage) {
|
|
|
|
$sitecontext = $context;
|
|
|
|
} else {
|
|
|
|
$sitecontext = context_system::instance();
|
|
|
|
}
|
|
|
|
|
2016-11-21 11:37:51 +08:00
|
|
|
// Sets defaults for all options.
|
|
|
|
$options = (object) [
|
|
|
|
'badges' => false,
|
|
|
|
'blogs' => false,
|
|
|
|
'competencies' => false,
|
|
|
|
'grades' => false,
|
|
|
|
'notes' => false,
|
|
|
|
'participants' => false,
|
|
|
|
'search' => false,
|
|
|
|
'tags' => false,
|
|
|
|
];
|
|
|
|
|
2016-08-02 11:49:14 +02:00
|
|
|
$options->blogs = !empty($CFG->enableblogs) &&
|
|
|
|
($CFG->bloglevel == BLOG_GLOBAL_LEVEL ||
|
|
|
|
($CFG->bloglevel == BLOG_SITE_LEVEL and ($isloggedin and !$isguestuser)))
|
|
|
|
&& has_capability('moodle/blog:view', $sitecontext);
|
|
|
|
|
|
|
|
$options->notes = !empty($CFG->enablenotes) && has_any_capability(array('moodle/notes:manage', 'moodle/notes:view'), $context);
|
|
|
|
|
|
|
|
// Frontpage settings?
|
|
|
|
if ($isfrontpage) {
|
2017-08-28 12:47:57 +08:00
|
|
|
// We are on the front page, so make sure we use the proper capability (site:viewparticipants).
|
2017-08-30 15:11:20 +08:00
|
|
|
$options->participants = course_can_view_participants($sitecontext);
|
2016-08-02 11:49:14 +02:00
|
|
|
$options->badges = !empty($CFG->enablebadges) && has_capability('moodle/badges:viewbadges', $sitecontext);
|
|
|
|
$options->tags = !empty($CFG->usetags) && $isloggedin;
|
|
|
|
$options->search = !empty($CFG->enableglobalsearch) && has_capability('moodle/search:query', $sitecontext);
|
|
|
|
} else {
|
2017-08-28 12:47:57 +08:00
|
|
|
// We are in a course, so make sure we use the proper capability (course:viewparticipants).
|
2017-08-30 15:11:20 +08:00
|
|
|
$options->participants = course_can_view_participants($context);
|
2022-01-20 10:30:42 +01:00
|
|
|
|
2022-01-26 09:25:31 +01:00
|
|
|
// Only display badges if they are enabled and the current user can manage them or if they can view them and have,
|
|
|
|
// at least, one available badge.
|
|
|
|
if (!empty($CFG->enablebadges) && !empty($CFG->badges_allowcoursebadges)) {
|
|
|
|
$canmanage = has_any_capability([
|
|
|
|
'moodle/badges:createbadge',
|
|
|
|
'moodle/badges:awardbadge',
|
|
|
|
'moodle/badges:configurecriteria',
|
|
|
|
'moodle/badges:configuremessages',
|
|
|
|
'moodle/badges:configuredetails',
|
|
|
|
'moodle/badges:deletebadge',
|
|
|
|
],
|
|
|
|
$context
|
|
|
|
);
|
|
|
|
$totalbadges = [];
|
|
|
|
$canview = false;
|
|
|
|
if (!$canmanage) {
|
|
|
|
// This only needs to be calculated if the user can't manage badges (to improve performance).
|
|
|
|
$canview = has_capability('moodle/badges:viewbadges', $context);
|
|
|
|
if ($canview) {
|
|
|
|
require_once($CFG->dirroot.'/lib/badgeslib.php');
|
|
|
|
if (is_null($course)) {
|
|
|
|
$totalbadges = count(badges_get_badges(BADGE_TYPE_SITE, 0, '', '', 0, 0, $USER->id));
|
|
|
|
} else {
|
|
|
|
$totalbadges = count(badges_get_badges(BADGE_TYPE_COURSE, $course->id, '', '', 0, 0, $USER->id));
|
|
|
|
}
|
|
|
|
}
|
2022-01-20 10:30:42 +01:00
|
|
|
}
|
|
|
|
|
2022-01-26 09:25:31 +01:00
|
|
|
$options->badges = ($canmanage || ($canview && $totalbadges > 0));
|
|
|
|
}
|
2016-10-06 14:10:14 +08:00
|
|
|
// Add view grade report is permitted.
|
|
|
|
$grades = false;
|
2016-10-14 11:58:14 +08:00
|
|
|
|
2016-10-06 14:10:14 +08:00
|
|
|
if (has_capability('moodle/grade:viewall', $context)) {
|
|
|
|
$grades = true;
|
|
|
|
} else if (!empty($course->showgrades)) {
|
|
|
|
$reports = core_component::get_plugin_list('gradereport');
|
|
|
|
if (is_array($reports) && count($reports) > 0) { // Get all installed reports.
|
|
|
|
arsort($reports); // User is last, we want to test it first.
|
|
|
|
foreach ($reports as $plugin => $plugindir) {
|
|
|
|
if (has_capability('gradereport/'.$plugin.':view', $context)) {
|
|
|
|
// Stop when the first visible plugin is found.
|
|
|
|
$grades = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$options->grades = $grades;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (\core_competency\api::is_enabled()) {
|
|
|
|
$capabilities = array('moodle/competency:coursecompetencyview', 'moodle/competency:coursecompetencymanage');
|
|
|
|
$options->competencies = has_any_capability($capabilities, $context);
|
2016-08-02 11:49:14 +02:00
|
|
|
}
|
|
|
|
return $options;
|
|
|
|
}
|
2016-08-02 15:17:14 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an object with the list of administration options in a course that are available or not for the current user.
|
|
|
|
* This function also handles the frontpage settings.
|
|
|
|
*
|
|
|
|
* @param stdClass $course course object (for frontpage it should be a clone of $SITE)
|
|
|
|
* @param stdClass $context context object (course context)
|
|
|
|
* @return stdClass the administration options in a course and their availability status
|
|
|
|
* @since Moodle 3.2
|
|
|
|
*/
|
|
|
|
function course_get_user_administration_options($course, $context) {
|
|
|
|
global $CFG;
|
|
|
|
$isfrontpage = $course->id == SITEID;
|
2017-04-12 11:50:43 +08:00
|
|
|
$completionenabled = $CFG->enablecompletion && $course->enablecompletion;
|
2022-02-14 01:26:51 +08:00
|
|
|
$hascompletionoptions = count(core_completion\manager::get_available_completion_options($course->id)) > 0;
|
2016-08-02 15:17:14 +02:00
|
|
|
$options = new stdClass;
|
|
|
|
$options->update = has_capability('moodle/course:update', $context);
|
2022-02-14 01:26:51 +08:00
|
|
|
$options->editcompletion = $CFG->enablecompletion && $course->enablecompletion &&
|
|
|
|
($options->update || $hascompletionoptions);
|
2016-08-02 15:17:14 +02:00
|
|
|
$options->filters = has_capability('moodle/filter:manage', $context) &&
|
|
|
|
count(filter_get_available_in_context($context)) > 0;
|
|
|
|
$options->reports = has_capability('moodle/site:viewreports', $context);
|
|
|
|
$options->backup = has_capability('moodle/backup:backupcourse', $context);
|
|
|
|
$options->restore = has_capability('moodle/restore:restorecourse', $context);
|
2020-03-02 04:24:19 +11:00
|
|
|
$options->copy = \core_course\management\helper::can_copy_course($course->id);
|
2016-12-02 14:10:06 +01:00
|
|
|
$options->files = ($course->legacyfiles == 2 && has_capability('moodle/course:managefiles', $context));
|
2016-08-02 15:17:14 +02:00
|
|
|
|
|
|
|
if (!$isfrontpage) {
|
|
|
|
$options->tags = has_capability('moodle/course:tag', $context);
|
|
|
|
$options->gradebook = has_capability('moodle/grade:manage', $context);
|
|
|
|
$options->outcomes = !empty($CFG->enableoutcomes) && has_capability('moodle/course:update', $context);
|
|
|
|
$options->badges = !empty($CFG->enablebadges);
|
|
|
|
$options->import = has_capability('moodle/restore:restoretargetimport', $context);
|
|
|
|
$options->reset = has_capability('moodle/course:reset', $context);
|
|
|
|
$options->roles = has_capability('moodle/role:switchroles', $context);
|
2016-09-07 17:09:42 +08:00
|
|
|
} else {
|
|
|
|
// Set default options to false.
|
|
|
|
$listofoptions = array('tags', 'gradebook', 'outcomes', 'badges', 'import', 'publish', 'reset', 'roles', 'grades');
|
|
|
|
|
|
|
|
foreach ($listofoptions as $option) {
|
|
|
|
$options->$option = false;
|
|
|
|
}
|
2016-08-02 15:17:14 +02:00
|
|
|
}
|
2016-09-07 17:09:42 +08:00
|
|
|
|
2016-08-02 15:17:14 +02:00
|
|
|
return $options;
|
|
|
|
}
|
2016-09-01 13:29:19 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates course start and end dates.
|
|
|
|
*
|
|
|
|
* Checks that the end course date is not greater than the start course date.
|
|
|
|
*
|
|
|
|
* $coursedata['startdate'] or $coursedata['enddate'] may not be set, it depends on the form and user input.
|
|
|
|
*
|
|
|
|
* @param array $coursedata May contain startdate and enddate timestamps, depends on the user input.
|
|
|
|
* @return mixed False if everything alright, error codes otherwise.
|
|
|
|
*/
|
|
|
|
function course_validate_dates($coursedata) {
|
|
|
|
|
|
|
|
// If both start and end dates are set end date should be later than the start date.
|
|
|
|
if (!empty($coursedata['startdate']) && !empty($coursedata['enddate']) &&
|
|
|
|
($coursedata['enddate'] < $coursedata['startdate'])) {
|
|
|
|
return 'enddatebeforestartdate';
|
|
|
|
}
|
|
|
|
|
|
|
|
// If start date is not set end date can not be set.
|
|
|
|
if (empty($coursedata['startdate']) && !empty($coursedata['enddate'])) {
|
|
|
|
return 'nostartdatenoenddate';
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2016-10-11 13:48:24 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check for course updates in the given context level instances (only modules supported right Now)
|
|
|
|
*
|
|
|
|
* @param stdClass $course course object
|
|
|
|
* @param array $tocheck instances to check for updates
|
|
|
|
* @param array $filter check only for updates in these areas
|
|
|
|
* @return array list of warnings and instances with updates information
|
|
|
|
* @since Moodle 3.2
|
|
|
|
*/
|
|
|
|
function course_check_updates($course, $tocheck, $filter = array()) {
|
|
|
|
global $CFG, $DB;
|
|
|
|
|
|
|
|
$instances = array();
|
|
|
|
$warnings = array();
|
|
|
|
$modulescallbacksupport = array();
|
|
|
|
$modinfo = get_fast_modinfo($course);
|
|
|
|
|
|
|
|
$supportedplugins = get_plugin_list_with_function('mod', 'check_updates_since');
|
|
|
|
|
|
|
|
// Check instances.
|
|
|
|
foreach ($tocheck as $instance) {
|
|
|
|
if ($instance['contextlevel'] == 'module') {
|
|
|
|
// Check module visibility.
|
|
|
|
try {
|
|
|
|
$cm = $modinfo->get_cm($instance['id']);
|
|
|
|
} catch (Exception $e) {
|
2016-10-20 20:08:32 +01:00
|
|
|
$warnings[] = array(
|
|
|
|
'item' => 'module',
|
|
|
|
'itemid' => $instance['id'],
|
|
|
|
'warningcode' => 'cmidnotincourse',
|
|
|
|
'message' => 'This module id does not belong to this course.'
|
|
|
|
);
|
|
|
|
continue;
|
2016-10-11 13:48:24 +01:00
|
|
|
}
|
|
|
|
|
2016-10-20 20:08:32 +01:00
|
|
|
if (!$cm->uservisible) {
|
2016-10-11 13:48:24 +01:00
|
|
|
$warnings[] = array(
|
|
|
|
'item' => 'module',
|
|
|
|
'itemid' => $instance['id'],
|
|
|
|
'warningcode' => 'nonuservisible',
|
|
|
|
'message' => 'You don\'t have access to this module.'
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (empty($supportedplugins['mod_' . $cm->modname])) {
|
|
|
|
$warnings[] = array(
|
|
|
|
'item' => 'module',
|
|
|
|
'itemid' => $instance['id'],
|
|
|
|
'warningcode' => 'missingcallback',
|
|
|
|
'message' => 'This module does not implement the check_updates_since callback: ' . $instance['contextlevel'],
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Retrieve the module instance.
|
|
|
|
$instances[] = array(
|
|
|
|
'contextlevel' => $instance['contextlevel'],
|
|
|
|
'id' => $instance['id'],
|
|
|
|
'updates' => call_user_func($cm->modname . '_check_updates_since', $cm, $instance['since'], $filter)
|
|
|
|
);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$warnings[] = array(
|
|
|
|
'item' => 'contextlevel',
|
|
|
|
'itemid' => $instance['id'],
|
|
|
|
'warningcode' => 'contextlevelnotsupported',
|
|
|
|
'message' => 'Context level not yet supported ' . $instance['contextlevel'],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return array($instances, $warnings);
|
|
|
|
}
|
|
|
|
|
2017-06-06 12:05:09 +08:00
|
|
|
/**
|
|
|
|
* This function classifies a course as past, in progress or future.
|
|
|
|
*
|
|
|
|
* This function may incur a DB hit to calculate course completion.
|
|
|
|
* @param stdClass $course Course record
|
|
|
|
* @param stdClass $user User record (optional - defaults to $USER).
|
|
|
|
* @param completion_info $completioninfo Completion record for the user (optional - will be fetched if required).
|
|
|
|
* @return string (one of COURSE_TIMELINE_FUTURE, COURSE_TIMELINE_INPROGRESS or COURSE_TIMELINE_PAST)
|
|
|
|
*/
|
|
|
|
function course_classify_for_timeline($course, $user = null, $completioninfo = null) {
|
|
|
|
global $USER;
|
|
|
|
|
|
|
|
if ($user == null) {
|
|
|
|
$user = $USER;
|
|
|
|
}
|
|
|
|
|
2021-10-25 17:52:33 +11:00
|
|
|
if ($completioninfo == null) {
|
|
|
|
$completioninfo = new completion_info($course);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let plugins override data for timeline classification.
|
|
|
|
$pluginsfunction = get_plugins_with_function('extend_course_classify_for_timeline', 'lib.php');
|
|
|
|
foreach ($pluginsfunction as $plugintype => $plugins) {
|
|
|
|
foreach ($plugins as $pluginfunction) {
|
|
|
|
$pluginfunction($course, $user, $completioninfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-06 12:05:09 +08:00
|
|
|
$today = time();
|
|
|
|
// End date past.
|
2018-01-10 16:35:02 -08:00
|
|
|
if (!empty($course->enddate) && (course_classify_end_date($course) < $today)) {
|
2017-06-06 12:05:09 +08:00
|
|
|
return COURSE_TIMELINE_PAST;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Course was completed.
|
|
|
|
if ($completioninfo->is_enabled() && $completioninfo->is_course_complete($user->id)) {
|
|
|
|
return COURSE_TIMELINE_PAST;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start date not reached.
|
2018-01-10 16:35:02 -08:00
|
|
|
if (!empty($course->startdate) && (course_classify_start_date($course) > $today)) {
|
2017-06-06 12:05:09 +08:00
|
|
|
return COURSE_TIMELINE_FUTURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Everything else is in progress.
|
|
|
|
return COURSE_TIMELINE_INPROGRESS;
|
|
|
|
}
|
|
|
|
|
2018-01-10 16:35:02 -08:00
|
|
|
/**
|
|
|
|
* This function calculates the end date to use for display classification purposes,
|
|
|
|
* incorporating the grace period, if any.
|
|
|
|
*
|
|
|
|
* @param stdClass $course The course record.
|
|
|
|
* @return int The new enddate.
|
|
|
|
*/
|
|
|
|
function course_classify_end_date($course) {
|
|
|
|
global $CFG;
|
|
|
|
$coursegraceperiodafter = (empty($CFG->coursegraceperiodafter)) ? 0 : $CFG->coursegraceperiodafter;
|
|
|
|
$enddate = (new \DateTimeImmutable())->setTimestamp($course->enddate)->modify("+{$coursegraceperiodafter} days");
|
|
|
|
return $enddate->getTimestamp();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function calculates the start date to use for display classification purposes,
|
|
|
|
* incorporating the grace period, if any.
|
|
|
|
*
|
|
|
|
* @param stdClass $course The course record.
|
|
|
|
* @return int The new startdate.
|
|
|
|
*/
|
|
|
|
function course_classify_start_date($course) {
|
|
|
|
global $CFG;
|
|
|
|
$coursegraceperiodbefore = (empty($CFG->coursegraceperiodbefore)) ? 0 : $CFG->coursegraceperiodbefore;
|
|
|
|
$startdate = (new \DateTimeImmutable())->setTimestamp($course->startdate)->modify("-{$coursegraceperiodbefore} days");
|
|
|
|
return $startdate->getTimestamp();
|
|
|
|
}
|
|
|
|
|
2018-08-27 16:43:56 +08:00
|
|
|
/**
|
|
|
|
* Group a list of courses into either past, future, or in progress.
|
|
|
|
*
|
|
|
|
* The return value will be an array indexed by the COURSE_TIMELINE_* constants
|
|
|
|
* with each value being an array of courses in that group.
|
|
|
|
* E.g.
|
|
|
|
* [
|
|
|
|
* COURSE_TIMELINE_PAST => [... list of past courses ...],
|
|
|
|
* COURSE_TIMELINE_FUTURE => [],
|
|
|
|
* COURSE_TIMELINE_INPROGRESS => []
|
|
|
|
* ]
|
|
|
|
*
|
|
|
|
* @param array $courses List of courses to be grouped.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function course_classify_courses_for_timeline(array $courses) {
|
|
|
|
return array_reduce($courses, function($carry, $course) {
|
|
|
|
$classification = course_classify_for_timeline($course);
|
|
|
|
array_push($carry[$classification], $course);
|
|
|
|
|
|
|
|
return $carry;
|
|
|
|
}, [
|
|
|
|
COURSE_TIMELINE_PAST => [],
|
|
|
|
COURSE_TIMELINE_FUTURE => [],
|
|
|
|
COURSE_TIMELINE_INPROGRESS => []
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the list of enrolled courses for the current user.
|
|
|
|
*
|
|
|
|
* This function returns a Generator. The courses will be loaded from the database
|
|
|
|
* in chunks rather than a single query.
|
|
|
|
*
|
|
|
|
* @param int $limit Restrict result set to this amount
|
|
|
|
* @param int $offset Skip this number of records from the start of the result set
|
|
|
|
* @param string|null $sort SQL string for sorting
|
|
|
|
* @param string|null $fields SQL string for fields to be returned
|
|
|
|
* @param int $dbquerylimit The number of records to load per DB request
|
2018-10-26 12:37:31 +08:00
|
|
|
* @param array $includecourses courses ids to be restricted
|
|
|
|
* @param array $hiddencourses courses ids to be excluded
|
2018-08-27 16:43:56 +08:00
|
|
|
* @return Generator
|
|
|
|
*/
|
|
|
|
function course_get_enrolled_courses_for_logged_in_user(
|
|
|
|
int $limit = 0,
|
|
|
|
int $offset = 0,
|
|
|
|
string $sort = null,
|
|
|
|
string $fields = null,
|
2018-10-26 12:37:31 +08:00
|
|
|
int $dbquerylimit = COURSE_DB_QUERY_LIMIT,
|
|
|
|
array $includecourses = [],
|
|
|
|
array $hiddencourses = []
|
2018-08-27 16:43:56 +08:00
|
|
|
) : Generator {
|
|
|
|
|
|
|
|
$haslimit = !empty($limit);
|
|
|
|
$recordsloaded = 0;
|
|
|
|
$querylimit = (!$haslimit || $limit > $dbquerylimit) ? $dbquerylimit : $limit;
|
|
|
|
|
2018-10-26 12:37:31 +08:00
|
|
|
while ($courses = enrol_get_my_courses($fields, $sort, $querylimit, $includecourses, false, $offset, $hiddencourses)) {
|
2018-08-27 16:43:56 +08:00
|
|
|
yield from $courses;
|
|
|
|
|
|
|
|
$recordsloaded += $querylimit;
|
|
|
|
|
|
|
|
if (count($courses) < $querylimit) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ($haslimit && $recordsloaded >= $limit) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$offset += $querylimit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-15 15:05:00 +08:00
|
|
|
/**
|
|
|
|
* Get the list of enrolled courses the current user searched for.
|
|
|
|
*
|
|
|
|
* This function returns a Generator. The courses will be loaded from the database
|
|
|
|
* in chunks rather than a single query.
|
|
|
|
*
|
|
|
|
* @param int $limit Restrict result set to this amount
|
|
|
|
* @param int $offset Skip this number of records from the start of the result set
|
|
|
|
* @param string|null $sort SQL string for sorting
|
|
|
|
* @param string|null $fields SQL string for fields to be returned
|
|
|
|
* @param int $dbquerylimit The number of records to load per DB request
|
|
|
|
* @param array $searchcriteria contains search criteria
|
|
|
|
* @param array $options display options, same as in get_courses() except 'recursive' is ignored -
|
|
|
|
* search is always category-independent
|
|
|
|
* @return Generator
|
|
|
|
*/
|
|
|
|
function course_get_enrolled_courses_for_logged_in_user_from_search(
|
|
|
|
int $limit = 0,
|
|
|
|
int $offset = 0,
|
|
|
|
string $sort = null,
|
|
|
|
string $fields = null,
|
|
|
|
int $dbquerylimit = COURSE_DB_QUERY_LIMIT,
|
|
|
|
array $searchcriteria = [],
|
|
|
|
array $options = []
|
|
|
|
) : Generator {
|
|
|
|
|
|
|
|
$haslimit = !empty($limit);
|
|
|
|
$recordsloaded = 0;
|
|
|
|
$querylimit = (!$haslimit || $limit > $dbquerylimit) ? $dbquerylimit : $limit;
|
|
|
|
$ids = core_course_category::search_courses($searchcriteria, $options);
|
|
|
|
|
|
|
|
// If no courses were found matching the criteria return back.
|
|
|
|
if (empty($ids)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ($courses = enrol_get_my_courses($fields, $sort, $querylimit, $ids, false, $offset)) {
|
|
|
|
yield from $courses;
|
|
|
|
|
|
|
|
$recordsloaded += $querylimit;
|
|
|
|
|
|
|
|
if (count($courses) < $querylimit) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ($haslimit && $recordsloaded >= $limit) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$offset += $querylimit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-27 16:43:56 +08:00
|
|
|
/**
|
|
|
|
* Search the given $courses for any that match the given $classification up to the specified
|
|
|
|
* $limit.
|
|
|
|
*
|
|
|
|
* This function will return the subset of courses that match the classification as well as the
|
|
|
|
* number of courses it had to process to build that subset.
|
|
|
|
*
|
|
|
|
* It is recommended that for larger sets of courses this function is given a Generator that loads
|
|
|
|
* the courses from the database in chunks.
|
|
|
|
*
|
|
|
|
* @param array|Traversable $courses List of courses to process
|
|
|
|
* @param string $classification One of the COURSE_TIMELINE_* constants
|
|
|
|
* @param int $limit Limit the number of results to this amount
|
|
|
|
* @return array First value is the filtered courses, second value is the number of courses processed
|
|
|
|
*/
|
|
|
|
function course_filter_courses_by_timeline_classification(
|
|
|
|
$courses,
|
|
|
|
string $classification,
|
|
|
|
int $limit = 0
|
|
|
|
) : array {
|
|
|
|
|
2018-10-15 08:42:08 +02:00
|
|
|
if (!in_array($classification,
|
2019-07-01 15:07:14 +02:00
|
|
|
[COURSE_TIMELINE_ALLINCLUDINGHIDDEN, COURSE_TIMELINE_ALL, COURSE_TIMELINE_PAST, COURSE_TIMELINE_INPROGRESS,
|
2021-04-15 15:05:00 +08:00
|
|
|
COURSE_TIMELINE_FUTURE, COURSE_TIMELINE_HIDDEN, COURSE_TIMELINE_SEARCH])) {
|
2019-07-01 15:07:14 +02:00
|
|
|
$message = 'Classification must be one of COURSE_TIMELINE_ALLINCLUDINGHIDDEN, COURSE_TIMELINE_ALL, COURSE_TIMELINE_PAST, '
|
2021-04-15 15:05:00 +08:00
|
|
|
. 'COURSE_TIMELINE_INPROGRESS, COURSE_TIMELINE_SEARCH or COURSE_TIMELINE_FUTURE';
|
2018-08-27 16:43:56 +08:00
|
|
|
throw new moodle_exception($message);
|
|
|
|
}
|
|
|
|
|
|
|
|
$filteredcourses = [];
|
|
|
|
$numberofcoursesprocessed = 0;
|
|
|
|
$filtermatches = 0;
|
|
|
|
|
|
|
|
foreach ($courses as $course) {
|
|
|
|
$numberofcoursesprocessed++;
|
2018-10-16 15:19:19 +08:00
|
|
|
$pref = get_user_preferences('block_myoverview_hidden_course_' . $course->id, 0);
|
2018-08-27 16:43:56 +08:00
|
|
|
|
2018-10-16 15:19:19 +08:00
|
|
|
// Added as of MDL-63457 toggle viewability for each user.
|
2019-07-01 15:07:14 +02:00
|
|
|
if ($classification == COURSE_TIMELINE_ALLINCLUDINGHIDDEN || ($classification == COURSE_TIMELINE_HIDDEN && $pref) ||
|
2021-04-15 15:05:00 +08:00
|
|
|
$classification == COURSE_TIMELINE_SEARCH||
|
2018-10-16 15:19:19 +08:00
|
|
|
(($classification == COURSE_TIMELINE_ALL || $classification == course_classify_for_timeline($course)) && !$pref)) {
|
2018-08-27 16:43:56 +08:00
|
|
|
$filteredcourses[] = $course;
|
|
|
|
$filtermatches++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($limit && $filtermatches >= $limit) {
|
|
|
|
// We've found the number of requested courses. No need to continue searching.
|
2018-10-20 17:53:35 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the number of filtered courses as well as the number of courses that were searched
|
|
|
|
// in order to find the matching courses. This allows the calling code to do some kind of
|
|
|
|
// pagination.
|
|
|
|
return [$filteredcourses, $numberofcoursesprocessed];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search the given $courses for any that match the given $classification up to the specified
|
|
|
|
* $limit.
|
|
|
|
*
|
|
|
|
* This function will return the subset of courses that are favourites as well as the
|
|
|
|
* number of courses it had to process to build that subset.
|
|
|
|
*
|
|
|
|
* It is recommended that for larger sets of courses this function is given a Generator that loads
|
|
|
|
* the courses from the database in chunks.
|
|
|
|
*
|
|
|
|
* @param array|Traversable $courses List of courses to process
|
|
|
|
* @param array $favouritecourseids Array of favourite courses.
|
|
|
|
* @param int $limit Limit the number of results to this amount
|
|
|
|
* @return array First value is the filtered courses, second value is the number of courses processed
|
|
|
|
*/
|
|
|
|
function course_filter_courses_by_favourites(
|
|
|
|
$courses,
|
|
|
|
$favouritecourseids,
|
|
|
|
int $limit = 0
|
|
|
|
) : array {
|
|
|
|
|
|
|
|
$filteredcourses = [];
|
|
|
|
$numberofcoursesprocessed = 0;
|
|
|
|
$filtermatches = 0;
|
|
|
|
|
|
|
|
foreach ($courses as $course) {
|
|
|
|
$numberofcoursesprocessed++;
|
|
|
|
|
2018-10-26 12:37:31 +08:00
|
|
|
if (in_array($course->id, $favouritecourseids)) {
|
2018-10-20 17:53:35 +02:00
|
|
|
$filteredcourses[] = $course;
|
|
|
|
$filtermatches++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($limit && $filtermatches >= $limit) {
|
|
|
|
// We've found the number of requested courses. No need to continue searching.
|
2018-08-27 16:43:56 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the number of filtered courses as well as the number of courses that were searched
|
2019-08-22 16:10:34 +01:00
|
|
|
// in order to find the matching courses. This allows the calling code to do some kind of
|
|
|
|
// pagination.
|
|
|
|
return [$filteredcourses, $numberofcoursesprocessed];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search the given $courses for any that have a $customfieldname value that matches the given
|
|
|
|
* $customfieldvalue, up to the specified $limit.
|
|
|
|
*
|
|
|
|
* This function will return the subset of courses that matches the value as well as the
|
|
|
|
* number of courses it had to process to build that subset.
|
|
|
|
*
|
|
|
|
* It is recommended that for larger sets of courses this function is given a Generator that loads
|
|
|
|
* the courses from the database in chunks.
|
|
|
|
*
|
|
|
|
* @param array|Traversable $courses List of courses to process
|
|
|
|
* @param string $customfieldname the shortname of the custom field to match against
|
|
|
|
* @param string $customfieldvalue the value this custom field needs to match
|
|
|
|
* @param int $limit Limit the number of results to this amount
|
|
|
|
* @return array First value is the filtered courses, second value is the number of courses processed
|
|
|
|
*/
|
|
|
|
function course_filter_courses_by_customfield(
|
|
|
|
$courses,
|
|
|
|
$customfieldname,
|
|
|
|
$customfieldvalue,
|
|
|
|
int $limit = 0
|
|
|
|
) : array {
|
|
|
|
global $DB;
|
|
|
|
|
|
|
|
if (!$courses) {
|
|
|
|
return [[], 0];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare the list of courses to search through.
|
|
|
|
$coursesbyid = [];
|
|
|
|
foreach ($courses as $course) {
|
|
|
|
$coursesbyid[$course->id] = $course;
|
|
|
|
}
|
|
|
|
if (!$coursesbyid) {
|
|
|
|
return [[], 0];
|
|
|
|
}
|
|
|
|
list($csql, $params) = $DB->get_in_or_equal(array_keys($coursesbyid), SQL_PARAMS_NAMED);
|
|
|
|
|
|
|
|
// Get the id of the custom field.
|
|
|
|
$sql = "
|
|
|
|
SELECT f.id
|
|
|
|
FROM {customfield_field} f
|
|
|
|
JOIN {customfield_category} cat ON cat.id = f.categoryid
|
|
|
|
WHERE f.shortname = ?
|
|
|
|
AND cat.component = 'core_course'
|
|
|
|
AND cat.area = 'course'
|
|
|
|
";
|
|
|
|
$fieldid = $DB->get_field_sql($sql, [$customfieldname]);
|
|
|
|
if (!$fieldid) {
|
|
|
|
return [[], 0];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get a list of courseids that match that custom field value.
|
|
|
|
if ($customfieldvalue == COURSE_CUSTOMFIELD_EMPTY) {
|
2019-10-10 16:28:08 +01:00
|
|
|
$comparevalue = $DB->sql_compare_text('cd.value');
|
2019-08-22 16:10:34 +01:00
|
|
|
$sql = "
|
|
|
|
SELECT c.id
|
|
|
|
FROM {course} c
|
|
|
|
LEFT JOIN {customfield_data} cd ON cd.instanceid = c.id AND cd.fieldid = :fieldid
|
|
|
|
WHERE c.id $csql
|
2019-10-10 16:28:08 +01:00
|
|
|
AND (cd.value IS NULL OR $comparevalue = '' OR $comparevalue = '0')
|
2019-08-22 16:10:34 +01:00
|
|
|
";
|
|
|
|
$params['fieldid'] = $fieldid;
|
|
|
|
$matchcourseids = $DB->get_fieldset_sql($sql, $params);
|
|
|
|
} else {
|
2019-10-10 16:28:08 +01:00
|
|
|
$comparevalue = $DB->sql_compare_text('value');
|
|
|
|
$select = "fieldid = :fieldid AND $comparevalue = :customfieldvalue AND instanceid $csql";
|
2019-08-22 16:10:34 +01:00
|
|
|
$params['fieldid'] = $fieldid;
|
|
|
|
$params['customfieldvalue'] = $customfieldvalue;
|
|
|
|
$matchcourseids = $DB->get_fieldset_select('customfield_data', 'instanceid', $select, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare the list of courses to return.
|
|
|
|
$filteredcourses = [];
|
|
|
|
$numberofcoursesprocessed = 0;
|
|
|
|
$filtermatches = 0;
|
|
|
|
|
|
|
|
foreach ($coursesbyid as $course) {
|
|
|
|
$numberofcoursesprocessed++;
|
|
|
|
|
|
|
|
if (in_array($course->id, $matchcourseids)) {
|
|
|
|
$filteredcourses[] = $course;
|
|
|
|
$filtermatches++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($limit && $filtermatches >= $limit) {
|
|
|
|
// We've found the number of requested courses. No need to continue searching.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the number of filtered courses as well as the number of courses that were searched
|
2018-08-27 16:43:56 +08:00
|
|
|
// in order to find the matching courses. This allows the calling code to do some kind of
|
|
|
|
// pagination.
|
|
|
|
return [$filteredcourses, $numberofcoursesprocessed];
|
|
|
|
}
|
|
|
|
|
2016-10-11 13:48:24 +01:00
|
|
|
/**
|
|
|
|
* Check module updates since a given time.
|
|
|
|
* This function checks for updates in the module config, file areas, completion, grades, comments and ratings.
|
|
|
|
*
|
|
|
|
* @param cm_info $cm course module data
|
|
|
|
* @param int $from the time to check
|
|
|
|
* @param array $fileareas additional file ares to check
|
|
|
|
* @param array $filter if we need to filter and return only selected updates
|
|
|
|
* @return stdClass object with the different updates
|
|
|
|
* @since Moodle 3.2
|
|
|
|
*/
|
|
|
|
function course_check_module_updates_since($cm, $from, $fileareas = array(), $filter = array()) {
|
|
|
|
global $DB, $CFG, $USER;
|
|
|
|
|
|
|
|
$context = $cm->context;
|
|
|
|
$mod = $DB->get_record($cm->modname, array('id' => $cm->instance), '*', MUST_EXIST);
|
|
|
|
|
|
|
|
$updates = new stdClass();
|
|
|
|
$course = get_course($cm->course);
|
|
|
|
$component = 'mod_' . $cm->modname;
|
|
|
|
|
|
|
|
// Check changes in the module configuration.
|
|
|
|
if (isset($mod->timemodified) and (empty($filter) or in_array('configuration', $filter))) {
|
2016-10-20 20:08:32 +01:00
|
|
|
$updates->configuration = (object) array('updated' => false);
|
|
|
|
if ($updates->configuration->updated = $mod->timemodified > $from) {
|
|
|
|
$updates->configuration->timeupdated = $mod->timemodified;
|
|
|
|
}
|
2016-10-11 13:48:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for updates in files.
|
|
|
|
if (plugin_supports('mod', $cm->modname, FEATURE_MOD_INTRO)) {
|
|
|
|
$fileareas[] = 'intro';
|
|
|
|
}
|
|
|
|
if (!empty($fileareas) and (empty($filter) or in_array('fileareas', $filter))) {
|
|
|
|
$fs = get_file_storage();
|
2016-11-19 16:54:31 +00:00
|
|
|
$files = $fs->get_area_files($context->id, $component, $fileareas, false, "filearea, timemodified DESC", false, $from);
|
2016-10-18 12:25:21 +11:00
|
|
|
foreach ($fileareas as $filearea) {
|
2016-10-20 20:08:32 +01:00
|
|
|
$updates->{$filearea . 'files'} = (object) array('updated' => false);
|
2016-10-18 12:25:21 +11:00
|
|
|
}
|
|
|
|
foreach ($files as $file) {
|
2016-10-20 20:08:32 +01:00
|
|
|
$updates->{$file->get_filearea() . 'files'}->updated = true;
|
|
|
|
$updates->{$file->get_filearea() . 'files'}->itemids[] = $file->get_id();
|
2016-10-11 13:48:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check completion.
|
|
|
|
$supportcompletion = plugin_supports('mod', $cm->modname, FEATURE_COMPLETION_HAS_RULES);
|
|
|
|
$supportcompletion = $supportcompletion or plugin_supports('mod', $cm->modname, FEATURE_COMPLETION_TRACKS_VIEWS);
|
|
|
|
if ($supportcompletion and (empty($filter) or in_array('completion', $filter))) {
|
2016-10-20 20:08:32 +01:00
|
|
|
$updates->completion = (object) array('updated' => false);
|
2016-10-11 13:48:24 +01:00
|
|
|
$completion = new completion_info($course);
|
|
|
|
// Use wholecourse to cache all the modules the first time.
|
|
|
|
$completiondata = $completion->get_data($cm, true);
|
2016-10-20 20:08:32 +01:00
|
|
|
if ($updates->completion->updated = !empty($completiondata->timemodified) && $completiondata->timemodified > $from) {
|
|
|
|
$updates->completion->timemodified = $completiondata->timemodified;
|
|
|
|
}
|
2016-10-11 13:48:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check grades.
|
|
|
|
$supportgrades = plugin_supports('mod', $cm->modname, FEATURE_GRADE_HAS_GRADE);
|
|
|
|
$supportgrades = $supportgrades or plugin_supports('mod', $cm->modname, FEATURE_GRADE_OUTCOMES);
|
|
|
|
if ($supportgrades and (empty($filter) or (in_array('gradeitems', $filter) or in_array('outcomes', $filter)))) {
|
|
|
|
require_once($CFG->libdir . '/gradelib.php');
|
|
|
|
$grades = grade_get_grades($course->id, 'mod', $cm->modname, $mod->id, $USER->id);
|
2016-10-20 20:08:32 +01:00
|
|
|
|
2016-10-11 13:48:24 +01:00
|
|
|
if (empty($filter) or in_array('gradeitems', $filter)) {
|
2016-10-20 20:08:32 +01:00
|
|
|
$updates->gradeitems = (object) array('updated' => false);
|
2016-10-11 13:48:24 +01:00
|
|
|
foreach ($grades->items as $gradeitem) {
|
|
|
|
foreach ($gradeitem->grades as $grade) {
|
|
|
|
if ($grade->datesubmitted > $from or $grade->dategraded > $from) {
|
2016-10-20 20:08:32 +01:00
|
|
|
$updates->gradeitems->updated = true;
|
|
|
|
$updates->gradeitems->itemids[] = $gradeitem->id;
|
2016-10-11 13:48:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-10-20 20:08:32 +01:00
|
|
|
|
2016-10-11 13:48:24 +01:00
|
|
|
if (empty($filter) or in_array('outcomes', $filter)) {
|
2016-10-20 20:08:32 +01:00
|
|
|
$updates->outcomes = (object) array('updated' => false);
|
2016-10-11 13:48:24 +01:00
|
|
|
foreach ($grades->outcomes as $outcome) {
|
|
|
|
foreach ($outcome->grades as $grade) {
|
|
|
|
if ($grade->datesubmitted > $from or $grade->dategraded > $from) {
|
2016-10-20 20:08:32 +01:00
|
|
|
$updates->outcomes->updated = true;
|
|
|
|
$updates->outcomes->itemids[] = $outcome->id;
|
2016-10-11 13:48:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check comments.
|
|
|
|
if (plugin_supports('mod', $cm->modname, FEATURE_COMMENT) and (empty($filter) or in_array('comments', $filter))) {
|
2016-10-20 20:08:32 +01:00
|
|
|
$updates->comments = (object) array('updated' => false);
|
2016-11-19 17:27:01 +00:00
|
|
|
require_once($CFG->dirroot . '/comment/lib.php');
|
2016-10-11 13:48:24 +01:00
|
|
|
require_once($CFG->dirroot . '/comment/locallib.php');
|
|
|
|
$manager = new comment_manager();
|
2016-10-20 20:08:32 +01:00
|
|
|
$comments = $manager->get_component_comments_since($course, $context, $component, $from, $cm);
|
|
|
|
if (!empty($comments)) {
|
|
|
|
$updates->comments->updated = true;
|
|
|
|
$updates->comments->itemids = array_keys($comments);
|
|
|
|
}
|
2016-10-11 13:48:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check ratings.
|
|
|
|
if (plugin_supports('mod', $cm->modname, FEATURE_RATE) and (empty($filter) or in_array('ratings', $filter))) {
|
2016-10-20 20:08:32 +01:00
|
|
|
$updates->ratings = (object) array('updated' => false);
|
2016-10-11 13:48:24 +01:00
|
|
|
require_once($CFG->dirroot . '/rating/lib.php');
|
|
|
|
$manager = new rating_manager();
|
2016-10-20 20:08:32 +01:00
|
|
|
$ratings = $manager->get_component_ratings_since($context, $component, $from);
|
|
|
|
if (!empty($ratings)) {
|
|
|
|
$updates->ratings->updated = true;
|
|
|
|
$updates->ratings->itemids = array_keys($ratings);
|
|
|
|
}
|
2016-10-11 13:48:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $updates;
|
|
|
|
}
|
2017-08-30 15:11:20 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the user can view the participant page, false otherwise,
|
|
|
|
*
|
|
|
|
* @param context $context The context we are checking.
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function course_can_view_participants($context) {
|
|
|
|
$viewparticipantscap = 'moodle/course:viewparticipants';
|
|
|
|
if ($context->contextlevel == CONTEXT_SYSTEM) {
|
|
|
|
$viewparticipantscap = 'moodle/site:viewparticipants';
|
|
|
|
}
|
|
|
|
|
|
|
|
return has_any_capability([$viewparticipantscap, 'moodle/course:enrolreview'], $context);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a user can view the participant page, if not throws an exception.
|
|
|
|
*
|
|
|
|
* @param context $context The context we are checking.
|
|
|
|
* @throws required_capability_exception
|
|
|
|
*/
|
|
|
|
function course_require_view_participants($context) {
|
|
|
|
if (!course_can_view_participants($context)) {
|
|
|
|
$viewparticipantscap = 'moodle/course:viewparticipants';
|
|
|
|
if ($context->contextlevel == CONTEXT_SYSTEM) {
|
|
|
|
$viewparticipantscap = 'moodle/site:viewparticipants';
|
|
|
|
}
|
|
|
|
throw new required_capability_exception($context, $viewparticipantscap, 'nopermissions', '');
|
|
|
|
}
|
|
|
|
}
|
2017-10-06 11:12:22 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether the user can download from the specified backup file area in the given context.
|
|
|
|
*
|
|
|
|
* @param string $filearea the backup file area. E.g. 'course', 'backup' or 'automated'.
|
|
|
|
* @param \context $context
|
|
|
|
* @param stdClass $user the user object. If not provided, the current user will be checked.
|
|
|
|
* @return bool true if the user is allowed to download in the context, false otherwise.
|
|
|
|
*/
|
|
|
|
function can_download_from_backup_filearea($filearea, \context $context, stdClass $user = null) {
|
|
|
|
$candownload = false;
|
|
|
|
switch ($filearea) {
|
|
|
|
case 'course':
|
|
|
|
case 'backup':
|
|
|
|
$candownload = has_capability('moodle/backup:downloadfile', $context, $user);
|
|
|
|
break;
|
|
|
|
case 'automated':
|
|
|
|
// Given the automated backups may contain userinfo, we restrict access such that only users who are able to
|
|
|
|
// restore with userinfo are able to download the file. Users can't create these backups, so checking 'backup:userinfo'
|
|
|
|
// doesn't make sense here.
|
|
|
|
$candownload = has_capability('moodle/backup:downloadfile', $context, $user) &&
|
|
|
|
has_capability('moodle/restore:userinfo', $context, $user);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
return $candownload;
|
|
|
|
}
|
2018-10-26 12:37:31 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a list of hidden courses
|
|
|
|
*
|
|
|
|
* @param int|object|null $user User override to get the filter from. Defaults to current user
|
|
|
|
* @return array $ids List of hidden courses
|
|
|
|
* @throws coding_exception
|
|
|
|
*/
|
|
|
|
function get_hidden_courses_on_timeline($user = null) {
|
|
|
|
global $USER;
|
|
|
|
|
|
|
|
if (empty($user)) {
|
|
|
|
$user = $USER->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
$preferences = get_user_preferences(null, null, $user);
|
|
|
|
$ids = [];
|
|
|
|
foreach ($preferences as $key => $value) {
|
|
|
|
if (preg_match('/block_myoverview_hidden_course_(\d)+/', $key)) {
|
|
|
|
$id = preg_split('/block_myoverview_hidden_course_/', $key);
|
|
|
|
$ids[] = $id[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ids;
|
|
|
|
}
|
2018-11-05 11:41:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of the most recently courses accessed by a user
|
|
|
|
*
|
|
|
|
* @param int $userid User id from which the courses will be obtained
|
|
|
|
* @param int $limit Restrict result set to this amount
|
|
|
|
* @param int $offset Skip this number of records from the start of the result set
|
|
|
|
* @param string|null $sort SQL string for sorting
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function course_get_recent_courses(int $userid = null, int $limit = 0, int $offset = 0, string $sort = null) {
|
|
|
|
|
|
|
|
global $CFG, $USER, $DB;
|
|
|
|
|
|
|
|
if (empty($userid)) {
|
|
|
|
$userid = $USER->id;
|
|
|
|
}
|
|
|
|
|
2021-04-20 20:41:53 +10:00
|
|
|
$basefields = [
|
|
|
|
'id', 'idnumber', 'summary', 'summaryformat', 'startdate', 'enddate', 'category',
|
|
|
|
'shortname', 'fullname', 'timeaccess', 'component', 'visible',
|
2023-02-17 15:30:25 +11:00
|
|
|
'showactivitydates', 'showcompletionconditions', 'pdfexportfont'
|
2021-04-20 20:41:53 +10:00
|
|
|
];
|
2018-11-05 11:41:55 +00:00
|
|
|
|
|
|
|
if (empty($sort)) {
|
|
|
|
$sort = 'timeaccess DESC';
|
|
|
|
} else {
|
2021-05-12 17:46:31 +08:00
|
|
|
// The SQL string for sorting can define sorting by multiple columns.
|
2018-11-05 11:41:55 +00:00
|
|
|
$rawsorts = explode(',', $sort);
|
|
|
|
$sorts = array();
|
2021-05-12 17:46:31 +08:00
|
|
|
// Validate and trim the sort parameters in the SQL string for sorting.
|
2018-11-05 11:41:55 +00:00
|
|
|
foreach ($rawsorts as $rawsort) {
|
2021-05-12 17:46:31 +08:00
|
|
|
$sort = trim($rawsort);
|
|
|
|
$sortparams = explode(' ', $sort);
|
|
|
|
// A valid sort statement can not have more than 2 params (ex. 'summary desc' or 'timeaccess').
|
|
|
|
if (count($sortparams) > 2) {
|
|
|
|
throw new invalid_parameter_exception(
|
|
|
|
'Invalid structure of the sort parameter, allowed structure: fieldname [ASC|DESC].');
|
|
|
|
}
|
|
|
|
$sortfield = trim($sortparams[0]);
|
|
|
|
// Validate the value which defines the field to sort by.
|
|
|
|
if (!in_array($sortfield, $basefields)) {
|
|
|
|
throw new invalid_parameter_exception('Invalid field in the sort parameter, allowed fields: ' .
|
|
|
|
implode(', ', $basefields) . '.');
|
|
|
|
}
|
|
|
|
$sortdirection = isset($sortparams[1]) ? trim($sortparams[1]) : '';
|
|
|
|
// Validate the value which defines the sort direction (if present).
|
|
|
|
$allowedsortdirections = ['asc', 'desc'];
|
|
|
|
if (!empty($sortdirection) && !in_array(strtolower($sortdirection), $allowedsortdirections)) {
|
|
|
|
throw new invalid_parameter_exception('Invalid sort direction in the sort parameter, allowed values: ' .
|
|
|
|
implode(', ', $allowedsortdirections) . '.');
|
|
|
|
}
|
|
|
|
$sorts[] = $sort;
|
2018-11-05 11:41:55 +00:00
|
|
|
}
|
|
|
|
$sort = implode(',', $sorts);
|
|
|
|
}
|
|
|
|
|
|
|
|
$ctxfields = context_helper::get_preload_record_columns_sql('ctx');
|
|
|
|
|
2021-04-20 20:41:53 +10:00
|
|
|
$coursefields = 'c.' . join(',', $basefields);
|
2018-11-05 11:41:55 +00:00
|
|
|
|
2018-11-28 10:21:50 +08:00
|
|
|
// Ask the favourites service to give us the join SQL for favourited courses,
|
|
|
|
// so we can include favourite information in the query.
|
|
|
|
$usercontext = \context_user::instance($userid);
|
|
|
|
$favservice = \core_favourites\service_factory::get_service_for_user_context($usercontext);
|
|
|
|
list($favsql, $favparams) = $favservice->get_join_sql_by_type('core_course', 'courses', 'fav', 'ul.courseid');
|
|
|
|
|
2018-11-28 13:46:56 +00:00
|
|
|
$sql = "SELECT $coursefields, $ctxfields
|
2018-11-05 11:41:55 +00:00
|
|
|
FROM {course} c
|
2018-11-05 23:51:49 +00:00
|
|
|
JOIN {context} ctx
|
|
|
|
ON ctx.contextlevel = :contextlevel
|
2018-11-05 11:41:55 +00:00
|
|
|
AND ctx.instanceid = c.id
|
2018-11-05 23:51:49 +00:00
|
|
|
JOIN {user_lastaccess} ul
|
|
|
|
ON ul.courseid = c.id
|
2018-11-28 10:21:50 +08:00
|
|
|
$favsql
|
2020-10-26 12:23:23 +01:00
|
|
|
LEFT JOIN {enrol} eg ON eg.courseid = c.id AND eg.status = :statusenrolg AND eg.enrol = :guestenrol
|
2018-11-05 11:41:55 +00:00
|
|
|
WHERE ul.userid = :userid
|
2018-11-28 13:46:56 +00:00
|
|
|
AND c.visible = :visible
|
2020-10-26 12:23:23 +01:00
|
|
|
AND (eg.id IS NOT NULL
|
|
|
|
OR EXISTS (SELECT e.id
|
2018-11-28 13:46:56 +00:00
|
|
|
FROM {enrol} e
|
2020-10-26 12:23:23 +01:00
|
|
|
JOIN {user_enrolments} ue ON ue.enrolid = e.id
|
2018-11-28 13:46:56 +00:00
|
|
|
WHERE e.courseid = c.id
|
|
|
|
AND e.status = :statusenrol
|
2020-10-26 12:23:23 +01:00
|
|
|
AND ue.status = :status
|
|
|
|
AND ue.userid = :userid2
|
|
|
|
AND ue.timestart < :now1
|
|
|
|
AND (ue.timeend = 0 OR ue.timeend > :now2)
|
|
|
|
))
|
2021-05-12 17:46:31 +08:00
|
|
|
ORDER BY $sort";
|
2018-11-28 13:46:56 +00:00
|
|
|
|
|
|
|
$now = round(time(), -2); // Improves db caching.
|
|
|
|
$params = ['userid' => $userid, 'contextlevel' => CONTEXT_COURSE, 'visible' => 1, 'status' => ENROL_USER_ACTIVE,
|
2020-10-26 12:23:23 +01:00
|
|
|
'statusenrol' => ENROL_INSTANCE_ENABLED, 'guestenrol' => 'guest', 'now1' => $now, 'now2' => $now,
|
|
|
|
'userid2' => $userid, 'statusenrolg' => ENROL_INSTANCE_ENABLED] + $favparams;
|
2018-11-05 11:41:55 +00:00
|
|
|
|
|
|
|
$recentcourses = $DB->get_records_sql($sql, $params, $offset, $limit);
|
|
|
|
|
|
|
|
// Filter courses if last access field is hidden.
|
|
|
|
$hiddenfields = array_flip(explode(',', $CFG->hiddenuserfields));
|
|
|
|
|
|
|
|
if ($userid != $USER->id && isset($hiddenfields['lastaccess'])) {
|
|
|
|
$recentcourses = array_filter($recentcourses, function($course) {
|
|
|
|
context_helper::preload_from_record($course);
|
|
|
|
$context = context_course::instance($course->id, IGNORE_MISSING);
|
|
|
|
// If last access was a hidden field, a user requesting info about another user would need permission to view hidden
|
|
|
|
// fields.
|
|
|
|
return has_capability('moodle/course:viewhiddenuserfields', $context);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return $recentcourses;
|
|
|
|
}
|
2019-07-05 10:05:33 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the course start date and offset for the given user ids.
|
|
|
|
*
|
|
|
|
* If the course is a fixed date course then the course start date will be returned.
|
|
|
|
* If the course is a relative date course then the course date will be calculated and
|
|
|
|
* and offset provided.
|
|
|
|
*
|
|
|
|
* The dates are returned as an array with the index being the user id. The array
|
|
|
|
* contains the start date and start offset values for the user.
|
|
|
|
*
|
|
|
|
* If the user is not enrolled in the course then the course start date will be returned.
|
|
|
|
*
|
|
|
|
* If we have a course which starts on 1563244000 and 2 users, id 123 and 456, where the
|
|
|
|
* former is enrolled in the course at 1563244693 and the latter is not enrolled then the
|
|
|
|
* return value would look like:
|
|
|
|
* [
|
|
|
|
* '123' => [
|
|
|
|
* 'start' => 1563244693,
|
|
|
|
* 'startoffset' => 693
|
|
|
|
* ],
|
|
|
|
* '456' => [
|
|
|
|
* 'start' => 1563244000,
|
|
|
|
* 'startoffset' => 0
|
|
|
|
* ]
|
|
|
|
* ]
|
|
|
|
*
|
|
|
|
* @param stdClass $course The course to fetch dates for.
|
|
|
|
* @param array $userids The list of user ids to get dates for.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function course_get_course_dates_for_user_ids(stdClass $course, array $userids): array {
|
|
|
|
if (empty($course->relativedatesmode)) {
|
|
|
|
// This course isn't set to relative dates so we can early return with the course
|
|
|
|
// start date.
|
|
|
|
return array_reduce($userids, function($carry, $userid) use ($course) {
|
|
|
|
$carry[$userid] = [
|
|
|
|
'start' => $course->startdate,
|
|
|
|
'startoffset' => 0
|
|
|
|
];
|
|
|
|
return $carry;
|
|
|
|
}, []);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're dealing with a relative dates course now so we need to calculate some dates.
|
|
|
|
$cache = cache::make('core', 'course_user_dates');
|
|
|
|
$dates = [];
|
|
|
|
$uncacheduserids = [];
|
|
|
|
|
|
|
|
// Try fetching the values from the cache so that we don't need to do a DB request.
|
|
|
|
foreach ($userids as $userid) {
|
|
|
|
$cachekey = "{$course->id}_{$userid}";
|
|
|
|
$cachedvalue = $cache->get($cachekey);
|
|
|
|
|
|
|
|
if ($cachedvalue === false) {
|
|
|
|
// Looks like we haven't seen this user for this course before so we'll have
|
|
|
|
// to fetch it.
|
|
|
|
$uncacheduserids[] = $userid;
|
|
|
|
} else {
|
|
|
|
[$start, $startoffset] = $cachedvalue;
|
|
|
|
$dates[$userid] = [
|
|
|
|
'start' => $start,
|
|
|
|
'startoffset' => $startoffset
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($uncacheduserids)) {
|
|
|
|
// Load the enrolments for any users we haven't seen yet. Set the "onlyactive" param
|
|
|
|
// to false because it filters out users with enrolment start times in the future which
|
|
|
|
// we don't want.
|
|
|
|
$enrolments = enrol_get_course_users($course->id, false, $uncacheduserids);
|
|
|
|
|
|
|
|
foreach ($uncacheduserids as $userid) {
|
|
|
|
// Find the user enrolment that has the earliest start date.
|
|
|
|
$enrolment = array_reduce(array_values($enrolments), function($carry, $enrolment) use ($userid) {
|
|
|
|
// Only consider enrolments for this user if the user enrolment is active and the
|
|
|
|
// enrolment method is enabled.
|
|
|
|
if (
|
|
|
|
$enrolment->uestatus == ENROL_USER_ACTIVE &&
|
|
|
|
$enrolment->estatus == ENROL_INSTANCE_ENABLED &&
|
|
|
|
$enrolment->id == $userid
|
|
|
|
) {
|
|
|
|
if (is_null($carry)) {
|
|
|
|
// Haven't found an enrolment yet for this user so use the one we just found.
|
|
|
|
$carry = $enrolment;
|
|
|
|
} else {
|
|
|
|
// We've already found an enrolment for this user so let's use which ever one
|
|
|
|
// has the earliest start time.
|
|
|
|
$carry = $carry->uetimestart < $enrolment->uetimestart ? $carry : $enrolment;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $carry;
|
|
|
|
}, null);
|
|
|
|
|
|
|
|
if ($enrolment) {
|
|
|
|
// The course is in relative dates mode so we calculate the student's start
|
|
|
|
// date based on their enrolment start date.
|
|
|
|
$start = $course->startdate > $enrolment->uetimestart ? $course->startdate : $enrolment->uetimestart;
|
|
|
|
$startoffset = $start - $course->startdate;
|
|
|
|
} else {
|
|
|
|
// The user is not enrolled in the course so default back to the course start date.
|
|
|
|
$start = $course->startdate;
|
|
|
|
$startoffset = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$dates[$userid] = [
|
|
|
|
'start' => $start,
|
|
|
|
'startoffset' => $startoffset
|
|
|
|
];
|
|
|
|
|
|
|
|
$cachekey = "{$course->id}_{$userid}";
|
|
|
|
$cache->set($cachekey, [$start, $startoffset]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $dates;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the course start date and offset for the given user id.
|
|
|
|
*
|
|
|
|
* If the course is a fixed date course then the course start date will be returned.
|
|
|
|
* If the course is a relative date course then the course date will be calculated and
|
|
|
|
* and offset provided.
|
|
|
|
*
|
|
|
|
* The return array contains the start date and start offset values for the user.
|
|
|
|
*
|
|
|
|
* If the user is not enrolled in the course then the course start date will be returned.
|
|
|
|
*
|
|
|
|
* If we have a course which starts on 1563244000. If a user's enrolment starts on 1563244693
|
|
|
|
* then the return would be:
|
|
|
|
* [
|
|
|
|
* 'start' => 1563244693,
|
|
|
|
* 'startoffset' => 693
|
|
|
|
* ]
|
|
|
|
*
|
|
|
|
* If the use was not enrolled then the return would be:
|
|
|
|
* [
|
|
|
|
* 'start' => 1563244000,
|
|
|
|
* 'startoffset' => 0
|
|
|
|
* ]
|
|
|
|
*
|
|
|
|
* @param stdClass $course The course to fetch dates for.
|
|
|
|
* @param int $userid The user id to get dates for.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function course_get_course_dates_for_user_id(stdClass $course, int $userid): array {
|
|
|
|
return (course_get_course_dates_for_user_ids($course, [$userid]))[$userid];
|
|
|
|
}
|
2020-03-02 04:24:19 +11:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the course copy form for the modal on the course management screen.
|
|
|
|
*
|
|
|
|
* @param array $args
|
|
|
|
* @return string $o Form HTML.
|
|
|
|
*/
|
|
|
|
function course_output_fragment_new_base_form($args) {
|
|
|
|
|
|
|
|
$serialiseddata = json_decode($args['jsonformdata'], true);
|
|
|
|
$formdata = [];
|
|
|
|
if (!empty($serialiseddata)) {
|
|
|
|
parse_str($serialiseddata, $formdata);
|
|
|
|
}
|
|
|
|
|
|
|
|
$context = context_course::instance($args['courseid']);
|
|
|
|
$copycaps = \core_course\management\helper::get_course_copy_capabilities();
|
|
|
|
require_all_capabilities($copycaps, $context);
|
|
|
|
|
|
|
|
$course = get_course($args['courseid']);
|
|
|
|
$mform = new \core_backup\output\copy_form(
|
|
|
|
null,
|
|
|
|
array('course' => $course, 'returnto' => '', 'returnurl' => ''),
|
|
|
|
'post', '', ['class' => 'ignoredirty'], true, $formdata);
|
|
|
|
|
|
|
|
if (!empty($serialiseddata)) {
|
|
|
|
// If we were passed non-empty form data we want the mform to call validation functions and show errors.
|
|
|
|
$mform->is_validated();
|
|
|
|
}
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$mform->display();
|
|
|
|
$o = ob_get_contents();
|
|
|
|
ob_end_clean();
|
|
|
|
|
|
|
|
return $o;
|
|
|
|
}
|