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
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
* @ package core
* @ subpackage 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 ;
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' );
2012-09-18 09:57:37 +08:00
require_once ( $CFG -> dirroot . '/course/format/lib.php' );
2001-11-22 06:23:56 +00:00
2006-05-15 05:01:00 +00: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 );
2006-01-16 03:15:48 +00:00
define ( 'COURSE_MAX_COURSES_PER_DROPDOWN' , 1000 ); // max courses in log dropdown before switching to optional
2006-05-15 05:01:00 +00:00
define ( 'COURSE_MAX_USERS_PER_DROPDOWN' , 1000 ); // max users in log dropdown before switching to optional
2007-12-19 17:35:20 +00:00
define ( 'FRONTPAGENEWS' , '0' );
2013-03-26 13:04:42 +11:00
define ( 'FRONTPAGECOURSELIST' , '1' ); // Not used. TODO MDL-38832 remove
2007-12-19 17:35:20 +00:00
define ( 'FRONTPAGECATEGORYNAMES' , '2' );
2013-03-26 13:04:42 +11:00
define ( 'FRONTPAGETOPICONLY' , '3' ); // Not used. TODO MDL-38832 remove
2007-12-19 17:35:20 +00:00
define ( 'FRONTPAGECATEGORYCOMBO' , '4' );
2013-03-26 13:04:42 +11:00
define ( 'FRONTPAGEENROLLEDCOURSELIST' , '5' );
define ( 'FRONTPAGEALLCOURSELIST' , '6' );
define ( 'FRONTPAGECOURSESEARCH' , '7' );
define ( 'FRONTPAGECOURSELIMIT' , 200 ); // Important! Replaced with $CFG->frontpagecourselimit - maximum number of courses displayed on the frontpage. TODO MDL-38832 remove
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 );
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' :
case 'calendar' :
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 ;
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 ;
}
2006-05-15 05:01:00 +00:00
function build_logs_array ( $course , $user = 0 , $date = 0 , $order = " l.time ASC " , $limitfrom = '' , $limitnum = '' ,
$modname = " " , $modid = 0 , $modaction = " " , $groupid = 0 ) {
2010-09-17 12:08:46 +00:00
global $DB , $SESSION , $USER ;
2004-07-25 14:00:29 +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.
2001-11-22 06:23:56 +00:00
2004-07-28 15:52:51 +00:00
/// Setup for group handling.
2006-11-09 18:25:58 +00:00
2004-07-28 15:52: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 ))) {
2009-10-16 03:12:41 +00:00
if ( isset ( $SESSION -> currentgroup [ $course -> id ])) {
$groupid = $SESSION -> currentgroup [ $course -> id ];
} else {
$groupid = groups_get_all_groups ( $course -> id , $USER -> id );
if ( is_array ( $groupid )) {
$groupid = array_shift ( array_keys ( $groupid ));
$SESSION -> currentgroup [ $course -> id ] = $groupid ;
} else {
$groupid = 0 ;
}
}
2004-07-28 15:52:51 +00:00
}
/// If this course doesn't have groups, no groupid can be specified.
else if ( ! $course -> groupmode ) {
$groupid = 0 ;
}
2004-07-25 14:00:29 +00:00
$joins = array ();
2010-10-19 15:38:53 +00:00
$params = array ();
2002-08-08 16:22:44 +00:00
2006-04-21 05:39:20 +00:00
if ( $course -> id != SITEID || $modid != 0 ) {
2008-05-30 16:47:21 +00:00
$joins [] = " l.course = :courseid " ;
$params [ 'courseid' ] = $course -> id ;
2006-04-21 05:39:20 +00:00
}
2001-11-22 06:23:56 +00:00
2004-02-14 02:44:35 +00:00
if ( $modname ) {
2008-05-30 16:47:21 +00:00
$joins [] = " l.module = :modname " ;
2008-06-04 06:38:03 +00:00
$params [ 'modname' ] = $modname ;
2004-01-15 07:17:12 +00:00
}
2004-08-12 09:55:12 +00:00
if ( 'site_errors' === $modid ) {
2004-12-09 21:35:20 +00:00
$joins [] = " ( l.action='error' OR l.action='infected' ) " ;
2004-08-12 09:55:12 +00:00
} else if ( $modid ) {
2008-05-30 16:47:21 +00:00
$joins [] = " l.cmid = :modid " ;
$params [ 'modid' ] = $modid ;
2004-01-31 14:47:57 +00:00
}
if ( $modaction ) {
2006-03-28 17:33:40 +00:00
$firstletter = substr ( $modaction , 0 , 1 );
2010-09-04 12:36:00 +00:00
if ( $firstletter == '-' ) {
2010-09-04 14:52:47 +00:00
$joins [] = $DB -> sql_like ( 'l.action' , ':modaction' , false , true , true );
2008-05-30 16:47:21 +00:00
$params [ 'modaction' ] = '%' . substr ( $modaction , 1 ) . '%' ;
2010-09-04 12:36:00 +00:00
} else {
$joins [] = $DB -> sql_like ( 'l.action' , ':modaction' , false );
$params [ 'modaction' ] = '%' . $modaction . '%' ;
2006-03-28 17:33:40 +00:00
}
2004-01-15 07:17:12 +00:00
}
2008-06-04 06:38:03 +00:00
2004-07-28 15:52:51 +00:00
/// Getting all members of a group.
if ( $groupid and ! $user ) {
2007-08-16 09:28:18 +00:00
if ( $gusers = groups_get_members ( $groupid )) {
$gusers = array_keys ( $gusers );
2007-06-07 16:04:23 +00:00
$joins [] = 'l.userid IN (' . implode ( ',' , $gusers ) . ')' ;
} else {
2007-10-05 04:24:42 +00:00
$joins [] = 'l.userid = 0' ; // No users in groups, so we want something that will always be false.
2004-07-28 15:52:51 +00:00
}
}
else if ( $user ) {
2008-05-30 16:47:21 +00:00
$joins [] = " l.userid = :userid " ;
$params [ 'userid' ] = $user ;
2001-11-22 06:23:56 +00:00
}
if ( $date ) {
$enddate = $date + 86400 ;
2008-05-30 16:47:21 +00:00
$joins [] = " l.time > :date AND l.time < :enddate " ;
$params [ 'date' ] = $date ;
$params [ 'enddate' ] = $enddate ;
2001-11-22 06:23:56 +00:00
}
2007-06-07 16:04:23 +00:00
$selector = implode ( ' AND ' , $joins );
2004-07-25 14:00:29 +00:00
2003-08-28 11:57:21 +00:00
$totalcount = 0 ; // Initialise
2006-05-15 05:01:00 +00:00
$result = array ();
2008-05-30 16:47:21 +00:00
$result [ 'logs' ] = get_logs ( $selector , $params , $order , $limitfrom , $limitnum , $totalcount );
2006-05-15 05:01:00 +00:00
$result [ 'totalcount' ] = $totalcount ;
return $result ;
}
2006-11-09 18:25:58 +00:00
2006-05-15 05:01:00 +00:00
function print_log ( $course , $user = 0 , $date = 0 , $order = " l.time ASC " , $page = 0 , $perpage = 100 ,
$url = " " , $modname = " " , $modid = 0 , $modaction = " " , $groupid = 0 ) {
2006-11-09 18:25:58 +00:00
2009-08-06 14:10:33 +00:00
global $CFG , $DB , $OUTPUT ;
2006-11-09 18:25:58 +00:00
2006-05-15 05:01:00 +00:00
if ( ! $logs = build_logs_array ( $course , $user , $date , $order , $page * $perpage , $perpage ,
$modname , $modid , $modaction , $groupid )) {
2009-08-18 04:58:26 +00:00
echo $OUTPUT -> notification ( " No logs found! " );
2009-08-06 14:10:33 +00:00
echo $OUTPUT -> footer ();
2001-11-22 06:23:56 +00:00
exit ;
}
2006-11-09 18:25:58 +00:00
2006-12-21 10:58:18 +00:00
$courses = array ();
2006-05-15 05:01:00 +00:00
if ( $course -> id == SITEID ) {
$courses [ 0 ] = '' ;
2008-05-07 18:28:59 +00:00
if ( $ccc = get_courses ( 'all' , 'c.id ASC' , 'c.id,c.shortname' )) {
2006-05-15 05:01:00 +00:00
foreach ( $ccc as $cc ) {
2008-05-07 18:28:59 +00:00
$courses [ $cc -> id ] = $cc -> shortname ;
2006-05-15 05:01:00 +00:00
}
}
2006-12-21 10:58:18 +00:00
} else {
2008-05-07 18:28:59 +00:00
$courses [ $course -> id ] = $course -> shortname ;
2006-05-15 05:01:00 +00:00
}
2006-11-09 18:25:58 +00:00
2006-05-15 05:01:00 +00:00
$totalcount = $logs [ 'totalcount' ];
2001-11-22 06:23:56 +00:00
$count = 0 ;
2004-07-29 18:01:32 +00:00
$ldcache = array ();
2001-11-22 06:23:56 +00:00
$tt = getdate ( time ());
$today = mktime ( 0 , 0 , 0 , $tt [ " mon " ], $tt [ " mday " ], $tt [ " year " ]);
2003-01-01 08:21:19 +00:00
2003-01-15 10:55:54 +00:00
$strftimedatetime = get_string ( " strftimedatetime " );
2007-01-15 21:28:25 +00:00
echo " <div class= \" info \" > \n " ;
2003-08-22 06:07:18 +00:00
print_string ( " displayingrecords " , " " , $totalcount );
2007-01-15 21:28:25 +00:00
echo " </div> \n " ;
2003-01-01 08:21:19 +00:00
2010-02-17 16:59:41 +00:00
echo $OUTPUT -> paging_bar ( $totalcount , $page , $perpage , " $url &perpage= $perpage " );
2003-08-22 06:07:18 +00:00
2010-01-28 08:49:28 +00:00
$table = new html_table ();
2013-01-21 23:12:20 +00:00
$table -> classes = array ( 'logtable' , 'generaltable' );
2010-01-28 08:49:28 +00:00
$table -> align = array ( 'right' , 'left' , 'left' );
$table -> head = array (
get_string ( 'time' ),
get_string ( 'ip_address' ),
2011-07-03 22:12:05 +01:00
get_string ( 'fullnameuser' ),
2010-01-28 08:49:28 +00:00
get_string ( 'action' ),
get_string ( 'info' )
);
$table -> data = array ();
2010-06-17 22:50:25 +00:00
2005-05-12 08:04:47 +00:00
if ( $course -> id == SITEID ) {
2010-01-28 08:49:28 +00:00
array_unshift ( $table -> align , 'left' );
array_unshift ( $table -> head , get_string ( 'course' ));
2005-05-12 08:04:47 +00:00
}
2007-06-07 16:04:23 +00:00
// Make sure that the logs array is an array, even it is empty, to avoid warnings from the foreach.
2006-09-27 07:41:38 +00:00
if ( empty ( $logs [ 'logs' ])) {
2007-06-07 16:04:23 +00:00
$logs [ 'logs' ] = array ();
2006-09-27 07:41:38 +00:00
}
2008-06-04 06:38:03 +00:00
2006-05-15 05:01:00 +00:00
foreach ( $logs [ 'logs' ] as $log ) {
2002-05-31 09:34:50 +00:00
2004-07-29 18:01:32 +00:00
if ( isset ( $ldcache [ $log -> module ][ $log -> action ])) {
$ld = $ldcache [ $log -> module ][ $log -> action ];
} else {
2008-06-01 19:05:07 +00:00
$ld = $DB -> get_record ( 'log_display' , array ( 'module' => $log -> module , 'action' => $log -> action ));
2004-07-29 18:01:32 +00:00
$ldcache [ $log -> module ][ $log -> action ] = $ld ;
}
2007-06-07 15:33:56 +00:00
if ( $ld && is_numeric ( $log -> info )) {
2005-01-16 17:27:40 +00:00
// ugly hack to make sure fullname is shown correctly
2010-01-28 08:49:28 +00:00
if ( $ld -> mtable == 'user' && $ld -> field == $DB -> sql_concat ( 'firstname' , " ' ' " , 'lastname' )) {
2008-06-01 19:05:07 +00:00
$log -> info = fullname ( $DB -> get_record ( $ld -> mtable , array ( 'id' => $log -> info )), true );
2005-01-16 17:27:40 +00:00
} else {
2008-06-01 19:05:07 +00:00
$log -> info = $DB -> get_field ( $ld -> mtable , $ld -> field , array ( 'id' => $log -> info ));
2005-01-16 17:27:40 +00:00
}
2002-05-31 09:34:50 +00:00
}
2006-11-09 18:25:58 +00:00
//Filter log->info
2005-03-27 22:31:54 +00:00
$log -> info = format_string ( $log -> info );
2008-03-13 15:32:08 +00:00
// If $log->url has been trimmed short by the db size restriction
// code in add_to_log, keep a note so we don't add a link to a broken url
2013-08-06 20:58:28 +02:00
$brokenurl = ( core_text :: strlen ( $log -> url ) == 100 && core_text :: substr ( $log -> url , 97 ) == '...' );
2008-03-13 15:32:08 +00:00
2010-01-28 08:49:28 +00:00
$row = array ();
2005-05-12 08:04:47 +00:00
if ( $course -> id == SITEID ) {
2007-08-06 18:19:15 +00:00
if ( empty ( $log -> course )) {
2010-01-28 08:49:28 +00:00
$row [] = get_string ( 'site' );
2007-08-06 18:19:15 +00:00
} else {
2010-01-28 08:49:28 +00:00
$row [] = " <a href= \" { $CFG -> wwwroot } /course/view.php?id= { $log -> course } \" > " . format_string ( $courses [ $log -> course ]) . " </a> " ;
2007-08-06 18:19:15 +00:00
}
2002-06-12 04:22:41 +00:00
}
2010-06-17 22:50:25 +00:00
2010-01-28 08:49:28 +00:00
$row [] = userdate ( $log -> time , '%a' ) . ' ' . userdate ( $log -> time , $strftimedatetime );
2010-06-17 22:50:25 +00:00
2010-02-11 16:27:53 +00:00
$link = new moodle_url ( " /iplookup/index.php?ip= $log->ip &user= $log->userid " );
$row [] = $OUTPUT -> action_link ( $link , $log -> ip , new popup_action ( 'click' , $link , 'iplookup' , array ( 'height' => 440 , 'width' => 700 )));
2010-01-28 08:49:28 +00:00
2012-07-23 15:59:04 +08:00
$row [] = html_writer :: link ( new moodle_url ( " /user/view.php?id= { $log -> userid } &course= { $log -> course } " ), fullname ( $log , has_capability ( 'moodle/site:viewfullnames' , context_course :: instance ( $course -> id ))));
2010-01-28 08:49:28 +00:00
2008-03-13 15:32:08 +00:00
$displayaction = " $log->module $log->action " ;
2010-01-28 08:49:28 +00:00
if ( $brokenurl ) {
$row [] = $displayaction ;
2008-03-13 15:32:08 +00:00
} else {
2010-02-11 16:27:53 +00:00
$link = make_log_url ( $log -> module , $log -> url );
$row [] = $OUTPUT -> action_link ( $link , $displayaction , new popup_action ( 'click' , $link , 'fromloglive' ), array ( 'height' => 440 , 'width' => 700 ));
2008-03-13 15:32:08 +00:00
}
2010-01-28 08:49:28 +00:00
$row [] = $log -> info ;
2010-01-28 09:11:32 +00:00
$table -> data [] = $row ;
2001-11-22 06:23:56 +00:00
}
2003-08-22 06:07:18 +00:00
2010-03-20 22:15:54 +00:00
echo html_writer :: table ( $table );
2010-02-17 16:59:41 +00:00
echo $OUTPUT -> paging_bar ( $totalcount , $page , $perpage , " $url &perpage= $perpage " );
2001-11-22 06:23:56 +00:00
}
2007-01-04 03:22:51 +00:00
function print_mnet_log ( $hostid , $course , $user = 0 , $date = 0 , $order = " l.time ASC " , $page = 0 , $perpage = 100 ,
$url = " " , $modname = " " , $modid = 0 , $modaction = " " , $groupid = 0 ) {
2008-06-04 06:38:03 +00:00
2009-08-06 14:10:33 +00:00
global $CFG , $DB , $OUTPUT ;
2008-06-04 06:38:03 +00:00
2007-01-04 03:22:51 +00:00
if ( ! $logs = build_mnet_logs_array ( $hostid , $course , $user , $date , $order , $page * $perpage , $perpage ,
$modname , $modid , $modaction , $groupid )) {
2009-08-18 04:58:26 +00:00
echo $OUTPUT -> notification ( " No logs found! " );
2009-08-06 14:10:33 +00:00
echo $OUTPUT -> footer ();
2007-01-04 03:22:51 +00:00
exit ;
}
2008-06-04 06:38:03 +00:00
2007-01-04 03:22:51 +00:00
if ( $course -> id == SITEID ) {
$courses [ 0 ] = '' ;
if ( $ccc = get_courses ( 'all' , 'c.id ASC' , 'c.id,c.shortname,c.visible' )) {
foreach ( $ccc as $cc ) {
$courses [ $cc -> id ] = $cc -> shortname ;
}
}
}
2008-06-04 06:38:03 +00:00
2007-01-04 03:22:51 +00:00
$totalcount = $logs [ 'totalcount' ];
$count = 0 ;
$ldcache = array ();
$tt = getdate ( time ());
$today = mktime ( 0 , 0 , 0 , $tt [ " mon " ], $tt [ " mday " ], $tt [ " year " ]);
$strftimedatetime = get_string ( " strftimedatetime " );
2007-01-15 21:28:25 +00:00
echo " <div class= \" info \" > \n " ;
2007-01-04 03:22:51 +00:00
print_string ( " displayingrecords " , " " , $totalcount );
2007-01-15 21:28:25 +00:00
echo " </div> \n " ;
2007-01-04 03:22:51 +00:00
2010-02-17 16:59:41 +00:00
echo $OUTPUT -> paging_bar ( $totalcount , $page , $perpage , " $url &perpage= $perpage " );
2007-01-04 03:22:51 +00:00
2007-01-15 21:28:25 +00:00
echo " <table class= \" logtable \" cellpadding= \" 3 \" cellspacing= \" 0 \" > \n " ;
2007-01-04 03:22:51 +00:00
echo " <tr> " ;
if ( $course -> id == SITEID ) {
echo " <th class= \" c0 header \" > " . get_string ( 'course' ) . " </th> \n " ;
}
echo " <th class= \" c1 header \" > " . get_string ( 'time' ) . " </th> \n " ;
echo " <th class= \" c2 header \" > " . get_string ( 'ip_address' ) . " </th> \n " ;
2011-07-03 22:12:05 +01:00
echo " <th class= \" c3 header \" > " . get_string ( 'fullnameuser' ) . " </th> \n " ;
2007-01-04 03:22:51 +00:00
echo " <th class= \" c4 header \" > " . get_string ( 'action' ) . " </th> \n " ;
echo " <th class= \" c5 header \" > " . get_string ( 'info' ) . " </th> \n " ;
echo " </tr> \n " ;
if ( empty ( $logs [ 'logs' ])) {
echo " </table> \n " ;
return ;
}
$row = 1 ;
foreach ( $logs [ 'logs' ] as $log ) {
2008-06-04 06:38:03 +00:00
2007-01-04 03:22:51 +00:00
$log -> info = $log -> coursename ;
$row = ( $row + 1 ) % 2 ;
if ( isset ( $ldcache [ $log -> module ][ $log -> action ])) {
$ld = $ldcache [ $log -> module ][ $log -> action ];
} else {
2008-06-02 08:13:24 +00:00
$ld = $DB -> get_record ( 'log_display' , array ( 'module' => $log -> module , 'action' => $log -> action ));
2007-01-04 03:22:51 +00:00
$ldcache [ $log -> module ][ $log -> action ] = $ld ;
}
if ( 0 && $ld && ! empty ( $log -> info )) {
// ugly hack to make sure fullname is shown correctly
2008-06-01 19:05:07 +00:00
if (( $ld -> mtable == 'user' ) and ( $ld -> field == $DB -> sql_concat ( 'firstname' , " ' ' " , 'lastname' ))) {
$log -> info = fullname ( $DB -> get_record ( $ld -> mtable , array ( 'id' => $log -> info )), true );
2007-01-04 03:22:51 +00:00
} else {
2008-06-01 19:05:07 +00:00
$log -> info = $DB -> get_field ( $ld -> mtable , $ld -> field , array ( 'id' => $log -> info ));
2007-01-04 03:22:51 +00:00
}
}
2008-06-04 06:38:03 +00:00
//Filter log->info
2007-01-04 03:22:51 +00:00
$log -> info = format_string ( $log -> info );
echo '<tr class="r' . $row . '">' ;
if ( $course -> id == SITEID ) {
2012-07-23 15:59:04 +08:00
$courseshortname = format_string ( $courses [ $log -> course ], true , array ( 'context' => context_course :: instance ( SITEID )));
2007-01-15 21:28:25 +00:00
echo " <td class= \" r $row c0 \" > \n " ;
2011-09-07 11:46:28 +12:00
echo " <a href= \" { $CFG -> wwwroot } /course/view.php?id= { $log -> course } \" > " . $courseshortname . " </a> \n " ;
2007-01-04 03:22:51 +00:00
echo " </td> \n " ;
}
2007-01-15 21:28:25 +00:00
echo " <td class= \" r $row c1 \" align= \" right \" > " . userdate ( $log -> time , '%a' ) .
2007-01-04 03:22:51 +00:00
' ' . userdate ( $log -> time , $strftimedatetime ) . " </td> \n " ;
2007-01-15 21:28:25 +00:00
echo " <td class= \" r $row c2 \" > \n " ;
2010-02-11 16:27:53 +00:00
$link = new moodle_url ( " /iplookup/index.php?ip= $log->ip &user= $log->userid " );
echo $OUTPUT -> action_link ( $link , $log -> ip , new popup_action ( 'click' , $link , 'iplookup' , array ( 'height' => 400 , 'width' => 700 )));
2007-01-04 03:22:51 +00:00
echo " </td> \n " ;
2012-07-23 15:59:04 +08:00
$fullname = fullname ( $log , has_capability ( 'moodle/site:viewfullnames' , context_course :: instance ( $course -> id )));
2007-01-15 21:28:25 +00:00
echo " <td class= \" r $row c3 \" > \n " ;
2007-01-04 03:22:51 +00:00
echo " <a href= \" $CFG->wwwroot /user/view.php?id= { $log -> userid } \" > $fullname </a> \n " ;
echo " </td> \n " ;
2007-01-15 21:28:25 +00:00
echo " <td class= \" r $row c4 \" > \n " ;
2007-01-04 03:22:51 +00:00
echo $log -> action . ': ' . $log -> module ;
2012-11-15 09:51:26 +08:00
echo " </td> \n " ;
2007-01-15 21:28:25 +00:00
echo " <td class= \" r $row c5 \" > { $log -> info } </td> \n " ;
2007-01-04 03:22:51 +00:00
echo " </tr> \n " ;
}
echo " </table> \n " ;
2010-02-17 16:59:41 +00:00
echo $OUTPUT -> paging_bar ( $totalcount , $page , $perpage , " $url &perpage= $perpage " );
2007-01-04 03:22:51 +00:00
}
2006-05-15 05:01:00 +00:00
function print_log_csv ( $course , $user , $date , $order = 'l.time DESC' , $modname ,
$modid , $modaction , $groupid ) {
2012-08-14 13:25:36 +08:00
global $DB , $CFG ;
2006-09-26 05:03:46 +00:00
2012-08-14 13:25:36 +08:00
require_once ( $CFG -> libdir . '/csvlib.class.php' );
$csvexporter = new csv_export_writer ( 'tab' );
$header = array ();
$header [] = get_string ( 'course' );
$header [] = get_string ( 'time' );
$header [] = get_string ( 'ip_address' );
$header [] = get_string ( 'fullnameuser' );
$header [] = get_string ( 'action' );
$header [] = get_string ( 'info' );
2006-11-09 18:25:58 +00:00
2006-08-25 07:09:28 +00:00
if ( ! $logs = build_logs_array ( $course , $user , $date , $order , '' , '' ,
2006-05-15 05:01:00 +00:00
$modname , $modid , $modaction , $groupid )) {
return false ;
}
2006-11-09 18:25:58 +00:00
2006-12-21 10:58:18 +00:00
$courses = array ();
2006-05-15 05:01:00 +00:00
if ( $course -> id == SITEID ) {
$courses [ 0 ] = '' ;
if ( $ccc = get_courses ( 'all' , 'c.id ASC' , 'c.id,c.shortname' )) {
foreach ( $ccc as $cc ) {
$courses [ $cc -> id ] = $cc -> shortname ;
}
}
2006-12-21 10:58:18 +00:00
} else {
$courses [ $course -> id ] = $course -> shortname ;
2006-05-15 05:01:00 +00:00
}
2006-11-09 18:25:58 +00:00
2006-05-15 05:01:00 +00:00
$count = 0 ;
$ldcache = array ();
$tt = getdate ( time ());
$today = mktime ( 0 , 0 , 0 , $tt [ " mon " ], $tt [ " mday " ], $tt [ " year " ]);
$strftimedatetime = get_string ( " strftimedatetime " );
2012-08-14 13:25:36 +08:00
$csvexporter -> set_filename ( 'logs' , '.txt' );
$title = array ( get_string ( 'savedat' ) . userdate ( time (), $strftimedatetime ));
2012-08-27 12:07:15 +08:00
$csvexporter -> add_data ( $title );
2012-08-14 13:25:36 +08:00
$csvexporter -> add_data ( $header );
2006-08-25 07:09:28 +00:00
2006-09-27 07:41:38 +00:00
if ( empty ( $logs [ 'logs' ])) {
return true ;
}
2006-08-25 07:09:28 +00:00
foreach ( $logs [ 'logs' ] as $log ) {
if ( isset ( $ldcache [ $log -> module ][ $log -> action ])) {
$ld = $ldcache [ $log -> module ][ $log -> action ];
} else {
2008-06-02 08:13:24 +00:00
$ld = $DB -> get_record ( 'log_display' , array ( 'module' => $log -> module , 'action' => $log -> action ));
2006-08-25 07:09:28 +00:00
$ldcache [ $log -> module ][ $log -> action ] = $ld ;
}
2012-11-15 09:35:37 +00:00
if ( $ld && is_numeric ( $log -> info )) {
2006-08-25 07:09:28 +00:00
// ugly hack to make sure fullname is shown correctly
2008-06-01 19:05:07 +00:00
if (( $ld -> mtable == 'user' ) and ( $ld -> field == $DB -> sql_concat ( 'firstname' , " ' ' " , 'lastname' ))) {
$log -> info = fullname ( $DB -> get_record ( $ld -> mtable , array ( 'id' => $log -> info )), true );
2006-08-25 07:09:28 +00:00
} else {
2008-06-01 19:05:07 +00:00
$log -> info = $DB -> get_field ( $ld -> mtable , $ld -> field , array ( 'id' => $log -> info ));
2006-08-25 07:09:28 +00:00
}
}
2006-11-09 18:25:58 +00:00
//Filter log->info
2006-08-25 07:09:28 +00:00
$log -> info = format_string ( $log -> info );
$log -> info = strip_tags ( urldecode ( $log -> info )); // Some XSS protection
2012-07-23 15:59:04 +08:00
$coursecontext = context_course :: instance ( $course -> id );
2011-09-07 11:46:28 +12:00
$firstField = format_string ( $courses [ $log -> course ], true , array ( 'context' => $coursecontext ));
$fullname = fullname ( $log , has_capability ( 'moodle/site:viewfullnames' , $coursecontext ));
2012-10-15 16:18:55 +08:00
$actionurl = $CFG -> wwwroot . make_log_url ( $log -> module , $log -> url );
2012-10-03 09:27:36 +01:00
$row = array ( $firstField , userdate ( $log -> time , $strftimedatetime ), $log -> ip , $fullname , $log -> module . ' ' . $log -> action . ' (' . $actionurl . ')' , $log -> info );
2012-08-14 13:25:36 +08:00
$csvexporter -> add_data ( $row );
2006-08-25 07:09:28 +00:00
}
2012-08-14 13:25:36 +08:00
$csvexporter -> download_file ();
2006-08-25 07:09:28 +00:00
return true ;
2006-05-15 05:01:00 +00:00
}
function print_log_xls ( $course , $user , $date , $order = 'l.time DESC' , $modname ,
$modid , $modaction , $groupid ) {
2006-11-09 18:25:58 +00:00
2008-06-01 19:05:07 +00:00
global $CFG , $DB ;
2006-05-15 05:01:00 +00:00
2006-08-25 07:09:28 +00:00
require_once ( " $CFG->libdir /excellib.class.php " );
2006-11-09 18:25:58 +00:00
2006-08-25 07:09:28 +00:00
if ( ! $logs = build_logs_array ( $course , $user , $date , $order , '' , '' ,
2006-05-15 05:01:00 +00:00
$modname , $modid , $modaction , $groupid )) {
return false ;
}
2006-11-09 18:25:58 +00:00
2006-12-21 10:58:18 +00:00
$courses = array ();
2006-05-15 05:01:00 +00:00
if ( $course -> id == SITEID ) {
$courses [ 0 ] = '' ;
if ( $ccc = get_courses ( 'all' , 'c.id ASC' , 'c.id,c.shortname' )) {
foreach ( $ccc as $cc ) {
$courses [ $cc -> id ] = $cc -> shortname ;
}
}
2006-12-21 10:58:18 +00:00
} else {
$courses [ $course -> id ] = $course -> shortname ;
2006-05-15 05:01:00 +00:00
}
2006-11-09 18:25:58 +00:00
2006-05-15 05:01:00 +00:00
$count = 0 ;
$ldcache = array ();
$tt = getdate ( time ());
$today = mktime ( 0 , 0 , 0 , $tt [ " mon " ], $tt [ " mday " ], $tt [ " year " ]);
$strftimedatetime = get_string ( " strftimedatetime " );
2006-08-25 07:09:28 +00:00
$nroPages = ceil ( count ( $logs ) / ( EXCELROWS - FIRSTUSEDEXCELROW + 1 ));
2010-04-10 17:35:19 +00:00
$filename = 'logs_' . userdate ( time (), get_string ( 'backupnameformat' , 'langconfig' ), 99 , false );
2006-08-25 07:09:28 +00:00
$filename .= '.xls' ;
2006-11-09 18:25:58 +00:00
2006-05-15 05:01:00 +00:00
$workbook = new MoodleExcelWorkbook ( '-' );
$workbook -> send ( $filename );
2006-11-09 18:25:58 +00:00
2006-08-25 07:09:28 +00:00
$worksheet = array ();
$headers = array ( get_string ( 'course' ), get_string ( 'time' ), get_string ( 'ip_address' ),
2011-07-03 22:12:05 +01:00
get_string ( 'fullnameuser' ), get_string ( 'action' ), get_string ( 'info' ));
2006-11-09 18:25:58 +00:00
2006-08-25 07:09:28 +00:00
// Creating worksheets
for ( $wsnumber = 1 ; $wsnumber <= $nroPages ; $wsnumber ++ ) {
2008-04-11 05:47:20 +00:00
$sheettitle = get_string ( 'logs' ) . ' ' . $wsnumber . '-' . $nroPages ;
2012-12-15 12:36:40 +01:00
$worksheet [ $wsnumber ] = $workbook -> add_worksheet ( $sheettitle );
2006-08-25 07:09:28 +00:00
$worksheet [ $wsnumber ] -> set_column ( 1 , 1 , 30 );
$worksheet [ $wsnumber ] -> write_string ( 0 , 0 , get_string ( 'savedat' ) .
userdate ( time (), $strftimedatetime ));
$col = 0 ;
foreach ( $headers as $item ) {
$worksheet [ $wsnumber ] -> write ( FIRSTUSEDEXCELROW - 1 , $col , $item , '' );
$col ++ ;
}
}
2006-09-27 07:41:38 +00:00
if ( empty ( $logs [ 'logs' ])) {
$workbook -> close ();
return true ;
}
2006-08-25 07:09:28 +00:00
$formatDate =& $workbook -> add_format ();
$formatDate -> set_num_format ( get_string ( 'log_excel_date_format' ));
$row = FIRSTUSEDEXCELROW ;
$wsnumber = 1 ;
$myxls =& $worksheet [ $wsnumber ];
foreach ( $logs [ 'logs' ] as $log ) {
if ( isset ( $ldcache [ $log -> module ][ $log -> action ])) {
$ld = $ldcache [ $log -> module ][ $log -> action ];
} else {
2008-06-01 19:05:07 +00:00
$ld = $DB -> get_record ( 'log_display' , array ( 'module' => $log -> module , 'action' => $log -> action ));
2006-08-25 07:09:28 +00:00
$ldcache [ $log -> module ][ $log -> action ] = $ld ;
}
2012-11-15 09:35:37 +00:00
if ( $ld && is_numeric ( $log -> info )) {
2006-08-25 07:09:28 +00:00
// ugly hack to make sure fullname is shown correctly
2008-06-01 19:05:07 +00:00
if (( $ld -> mtable == 'user' ) and ( $ld -> field == $DB -> sql_concat ( 'firstname' , " ' ' " , 'lastname' ))) {
$log -> info = fullname ( $DB -> get_record ( $ld -> mtable , array ( 'id' => $log -> info )), true );
2006-08-25 07:09:28 +00:00
} else {
2008-06-01 19:05:07 +00:00
$log -> info = $DB -> get_field ( $ld -> mtable , $ld -> field , array ( 'id' => $log -> info ));
2006-08-25 07:09:28 +00:00
}
}
// Filter log->info
$log -> info = format_string ( $log -> info );
$log -> info = strip_tags ( urldecode ( $log -> info )); // Some XSS protection
if ( $nroPages > 1 ) {
if ( $row > EXCELROWS ) {
$wsnumber ++ ;
$myxls =& $worksheet [ $wsnumber ];
$row = FIRSTUSEDEXCELROW ;
}
}
2006-11-09 18:25:58 +00:00
2012-07-23 15:59:04 +08:00
$coursecontext = context_course :: instance ( $course -> id );
2011-09-07 11:46:28 +12:00
$myxls -> write ( $row , 0 , format_string ( $courses [ $log -> course ], true , array ( 'context' => $coursecontext )), '' );
2010-02-25 10:17:46 +00:00
$myxls -> write_date ( $row , 1 , $log -> time , $formatDate ); // write_date() does conversion/timezone support. MDL-14934
2006-08-25 07:09:28 +00:00
$myxls -> write ( $row , 2 , $log -> ip , '' );
2011-09-07 11:46:28 +12:00
$fullname = fullname ( $log , has_capability ( 'moodle/site:viewfullnames' , $coursecontext ));
2006-08-25 07:09:28 +00:00
$myxls -> write ( $row , 3 , $fullname , '' );
2012-10-03 09:27:36 +01:00
$actionurl = $CFG -> wwwroot . make_log_url ( $log -> module , $log -> url );
$myxls -> write ( $row , 4 , $log -> module . ' ' . $log -> action . ' (' . $actionurl . ')' , '' );
2006-08-25 07:09:28 +00:00
$myxls -> write ( $row , 5 , $log -> info , '' );
2006-11-09 18:25:58 +00:00
2006-08-25 07:09:28 +00:00
$row ++ ;
}
$workbook -> close ();
2006-12-21 10:58:18 +00:00
return true ;
}
function print_log_ods ( $course , $user , $date , $order = 'l.time DESC' , $modname ,
$modid , $modaction , $groupid ) {
2008-06-01 19:05:07 +00:00
global $CFG , $DB ;
2006-12-21 10:58:18 +00:00
require_once ( " $CFG->libdir /odslib.class.php " );
if ( ! $logs = build_logs_array ( $course , $user , $date , $order , '' , '' ,
$modname , $modid , $modaction , $groupid )) {
return false ;
}
$courses = array ();
if ( $course -> id == SITEID ) {
$courses [ 0 ] = '' ;
if ( $ccc = get_courses ( 'all' , 'c.id ASC' , 'c.id,c.shortname' )) {
foreach ( $ccc as $cc ) {
$courses [ $cc -> id ] = $cc -> shortname ;
}
}
} else {
$courses [ $course -> id ] = $course -> shortname ;
}
$count = 0 ;
$ldcache = array ();
$tt = getdate ( time ());
$today = mktime ( 0 , 0 , 0 , $tt [ " mon " ], $tt [ " mday " ], $tt [ " year " ]);
$strftimedatetime = get_string ( " strftimedatetime " );
$nroPages = ceil ( count ( $logs ) / ( EXCELROWS - FIRSTUSEDEXCELROW + 1 ));
2010-04-10 17:35:19 +00:00
$filename = 'logs_' . userdate ( time (), get_string ( 'backupnameformat' , 'langconfig' ), 99 , false );
2006-12-21 10:58:18 +00:00
$filename .= '.ods' ;
$workbook = new MoodleODSWorkbook ( '-' );
$workbook -> send ( $filename );
$worksheet = array ();
$headers = array ( get_string ( 'course' ), get_string ( 'time' ), get_string ( 'ip_address' ),
2011-07-03 22:12:05 +01:00
get_string ( 'fullnameuser' ), get_string ( 'action' ), get_string ( 'info' ));
2006-12-21 10:58:18 +00:00
// Creating worksheets
for ( $wsnumber = 1 ; $wsnumber <= $nroPages ; $wsnumber ++ ) {
2008-04-11 05:47:20 +00:00
$sheettitle = get_string ( 'logs' ) . ' ' . $wsnumber . '-' . $nroPages ;
2012-12-15 12:36:40 +01:00
$worksheet [ $wsnumber ] = $workbook -> add_worksheet ( $sheettitle );
2006-12-21 10:58:18 +00:00
$worksheet [ $wsnumber ] -> set_column ( 1 , 1 , 30 );
$worksheet [ $wsnumber ] -> write_string ( 0 , 0 , get_string ( 'savedat' ) .
userdate ( time (), $strftimedatetime ));
$col = 0 ;
foreach ( $headers as $item ) {
$worksheet [ $wsnumber ] -> write ( FIRSTUSEDEXCELROW - 1 , $col , $item , '' );
$col ++ ;
}
}
if ( empty ( $logs [ 'logs' ])) {
$workbook -> close ();
return true ;
}
$formatDate =& $workbook -> add_format ();
$formatDate -> set_num_format ( get_string ( 'log_excel_date_format' ));
$row = FIRSTUSEDEXCELROW ;
$wsnumber = 1 ;
$myxls =& $worksheet [ $wsnumber ];
foreach ( $logs [ 'logs' ] as $log ) {
if ( isset ( $ldcache [ $log -> module ][ $log -> action ])) {
$ld = $ldcache [ $log -> module ][ $log -> action ];
} else {
2008-06-01 19:05:07 +00:00
$ld = $DB -> get_record ( 'log_display' , array ( 'module' => $log -> module , 'action' => $log -> action ));
2006-12-21 10:58:18 +00:00
$ldcache [ $log -> module ][ $log -> action ] = $ld ;
}
2012-11-15 09:35:37 +00:00
if ( $ld && is_numeric ( $log -> info )) {
2006-12-21 10:58:18 +00:00
// ugly hack to make sure fullname is shown correctly
2008-06-02 21:56:06 +00:00
if (( $ld -> mtable == 'user' ) and ( $ld -> field == $DB -> sql_concat ( 'firstname' , " ' ' " , 'lastname' ))) {
2008-06-01 19:05:07 +00:00
$log -> info = fullname ( $DB -> get_record ( $ld -> mtable , array ( 'id' => $log -> info )), true );
2006-12-21 10:58:18 +00:00
} else {
2008-06-01 19:05:07 +00:00
$log -> info = $DB -> get_field ( $ld -> mtable , $ld -> field , array ( 'id' => $log -> info ));
2006-12-21 10:58:18 +00:00
}
}
// Filter log->info
$log -> info = format_string ( $log -> info );
$log -> info = strip_tags ( urldecode ( $log -> info )); // Some XSS protection
if ( $nroPages > 1 ) {
if ( $row > EXCELROWS ) {
$wsnumber ++ ;
$myxls =& $worksheet [ $wsnumber ];
$row = FIRSTUSEDEXCELROW ;
}
}
2012-07-23 15:59:04 +08:00
$coursecontext = context_course :: instance ( $course -> id );
2011-09-07 11:46:28 +12:00
2012-01-21 19:51:28 +01:00
$myxls -> write_string ( $row , 0 , format_string ( $courses [ $log -> course ], true , array ( 'context' => $coursecontext )));
2006-12-21 19:46:29 +00:00
$myxls -> write_date ( $row , 1 , $log -> time );
$myxls -> write_string ( $row , 2 , $log -> ip );
2011-09-07 11:46:28 +12:00
$fullname = fullname ( $log , has_capability ( 'moodle/site:viewfullnames' , $coursecontext ));
2006-12-21 19:46:29 +00:00
$myxls -> write_string ( $row , 3 , $fullname );
2012-10-03 09:27:36 +01:00
$actionurl = $CFG -> wwwroot . make_log_url ( $log -> module , $log -> url );
$myxls -> write_string ( $row , 4 , $log -> module . ' ' . $log -> action . ' (' . $actionurl . ')' );
2006-12-21 19:46:29 +00:00
$myxls -> write_string ( $row , 5 , $log -> info );
2006-12-21 10:58:18 +00:00
$row ++ ;
}
$workbook -> close ();
2006-08-25 07:09:28 +00:00
return true ;
2006-05-15 05:01:00 +00:00
}
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
/**
* For a given course , returns an array of course activity objects
* Each item in the array contains he following properties :
*/
2002-11-10 07:37:15 +00:00
function get_array_of_activities ( $courseid ) {
// cm - course module id
// mod - name of the module (eg forum)
// section - the number of the section (eg week or topic)
// name - the name of the instance
2003-05-09 17:24:17 +00:00
// visible - is the instance visible or not
2007-08-20 10:52:59 +00:00
// groupingid - grouping id
// groupmembersonly - is this instance visible to group members only
2003-07-20 13:53:31 +00:00
// extra - contains extra string to include in any link
2008-06-01 19:05:07 +00:00
global $CFG , $DB ;
2008-12-17 16:37:35 +00:00
if ( ! empty ( $CFG -> enableavailability )) {
require_once ( $CFG -> libdir . '/conditionlib.php' );
}
2003-10-22 13:14:56 +00:00
2009-04-02 06:59:14 +00:00
$course = $DB -> get_record ( 'course' , array ( 'id' => $courseid ));
if ( empty ( $course )) {
throw new moodle_exception ( 'courseidnotfound' );
}
2002-11-10 07:37:15 +00:00
$mod = array ();
2009-04-02 06:59:14 +00:00
$rawmods = get_course_mods ( $courseid );
if ( empty ( $rawmods )) {
2008-01-24 20:33:50 +00:00
return $mod ; // always return array
2002-11-10 07:37:15 +00:00
}
2013-09-11 10:17:57 +08:00
if ( $sections = $DB -> get_records ( 'course_sections' , array ( 'course' => $courseid ), 'section ASC' , 'id,section,sequence' )) {
// First check and correct obvious mismatches between course_sections.sequence and course_modules.section.
if ( $errormessages = course_integrity_check ( $courseid , $rawmods , $sections )) {
debugging ( join ( '<br>' , $errormessages ));
$rawmods = get_course_mods ( $courseid );
$sections = $DB -> get_records ( 'course_sections' , array ( 'course' => $courseid ), 'section ASC' , 'id,section,sequence' );
}
// Build array of activities.
2002-11-10 07:37:15 +00:00
foreach ( $sections as $section ) {
2003-03-21 04:53:08 +00:00
if ( ! empty ( $section -> sequence )) {
2002-11-10 07:37:15 +00:00
$sequence = explode ( " , " , $section -> sequence );
foreach ( $sequence as $seq ) {
2003-01-14 14:56:57 +00:00
if ( empty ( $rawmods [ $seq ])) {
continue ;
}
2012-01-15 13:37:53 +01:00
$mod [ $seq ] = new stdClass ();
2008-01-24 20:33:50 +00:00
$mod [ $seq ] -> id = $rawmods [ $seq ] -> instance ;
$mod [ $seq ] -> cm = $rawmods [ $seq ] -> id ;
$mod [ $seq ] -> mod = $rawmods [ $seq ] -> modname ;
2011-03-11 17:26:23 +00:00
// Oh dear. Inconsistent names left here for backward compatibility.
2008-01-24 20:33:50 +00:00
$mod [ $seq ] -> section = $section -> section ;
2011-03-11 17:26:23 +00:00
$mod [ $seq ] -> sectionid = $rawmods [ $seq ] -> section ;
$mod [ $seq ] -> module = $rawmods [ $seq ] -> module ;
$mod [ $seq ] -> added = $rawmods [ $seq ] -> added ;
$mod [ $seq ] -> score = $rawmods [ $seq ] -> score ;
2010-05-28 01:53:09 +00:00
$mod [ $seq ] -> idnumber = $rawmods [ $seq ] -> idnumber ;
2008-01-24 20:33:50 +00:00
$mod [ $seq ] -> visible = $rawmods [ $seq ] -> visible ;
2011-03-11 17:26:23 +00:00
$mod [ $seq ] -> visibleold = $rawmods [ $seq ] -> visibleold ;
2008-01-24 20:33:50 +00:00
$mod [ $seq ] -> groupmode = $rawmods [ $seq ] -> groupmode ;
$mod [ $seq ] -> groupingid = $rawmods [ $seq ] -> groupingid ;
2007-08-20 10:52:59 +00:00
$mod [ $seq ] -> groupmembersonly = $rawmods [ $seq ] -> groupmembersonly ;
2008-12-17 16:37:35 +00:00
$mod [ $seq ] -> indent = $rawmods [ $seq ] -> indent ;
$mod [ $seq ] -> completion = $rawmods [ $seq ] -> completion ;
2008-01-24 20:33:50 +00:00
$mod [ $seq ] -> extra = " " ;
2011-03-11 17:26:23 +00:00
$mod [ $seq ] -> completiongradeitemnumber =
$rawmods [ $seq ] -> completiongradeitemnumber ;
$mod [ $seq ] -> completionview = $rawmods [ $seq ] -> completionview ;
$mod [ $seq ] -> completionexpected = $rawmods [ $seq ] -> completionexpected ;
$mod [ $seq ] -> availablefrom = $rawmods [ $seq ] -> availablefrom ;
$mod [ $seq ] -> availableuntil = $rawmods [ $seq ] -> availableuntil ;
$mod [ $seq ] -> showavailability = $rawmods [ $seq ] -> showavailability ;
2011-07-20 17:24:55 +01:00
$mod [ $seq ] -> showdescription = $rawmods [ $seq ] -> showdescription ;
2011-03-11 17:26:23 +00:00
if ( ! empty ( $CFG -> enableavailability )) {
2008-12-17 16:37:35 +00:00
condition_info :: fill_availability_conditions ( $rawmods [ $seq ]);
$mod [ $seq ] -> conditionscompletion = $rawmods [ $seq ] -> conditionscompletion ;
$mod [ $seq ] -> conditionsgrade = $rawmods [ $seq ] -> conditionsgrade ;
2011-09-26 21:20:17 +08:00
$mod [ $seq ] -> conditionsfield = $rawmods [ $seq ] -> conditionsfield ;
2008-12-17 16:37:35 +00:00
}
2003-10-22 13:14:56 +00:00
$modname = $mod [ $seq ] -> mod ;
$functionname = $modname . " _get_coursemodule_info " ;
2008-04-30 12:24:54 +00:00
if ( ! file_exists ( " $CFG->dirroot /mod/ $modname /lib.php " )) {
continue ;
}
2003-10-22 13:14:56 +00:00
include_once ( " $CFG->dirroot /mod/ $modname /lib.php " );
2011-07-21 15:05:36 +01:00
if ( $hasfunction = function_exists ( $functionname )) {
2004-04-26 15:31:22 +00:00
if ( $info = $functionname ( $rawmods [ $seq ])) {
if ( ! empty ( $info -> icon )) {
$mod [ $seq ] -> icon = $info -> icon ;
}
2010-02-14 20:18:10 +00:00
if ( ! empty ( $info -> iconcomponent )) {
$mod [ $seq ] -> iconcomponent = $info -> iconcomponent ;
}
2008-02-01 08:15:38 +00:00
if ( ! empty ( $info -> name )) {
2010-02-14 20:18:10 +00:00
$mod [ $seq ] -> name = $info -> name ;
2008-02-01 08:15:38 +00:00
}
2011-01-26 10:29:16 +00:00
if ( $info instanceof cached_cm_info ) {
// When using cached_cm_info you can include three new fields
// that aren't available for legacy code
if ( ! empty ( $info -> content )) {
$mod [ $seq ] -> content = $info -> content ;
}
if ( ! empty ( $info -> extraclasses )) {
$mod [ $seq ] -> extraclasses = $info -> extraclasses ;
}
2011-11-07 17:42:56 +01:00
if ( ! empty ( $info -> iconurl )) {
2013-08-07 13:46:08 +10:00
// Convert URL to string as it's easier to store. Also serialized object contains \0 byte and can not be written to Postgres DB.
$url = new moodle_url ( $info -> iconurl );
$mod [ $seq ] -> iconurl = $url -> out ( false );
2011-11-07 17:42:56 +01:00
}
2011-01-26 10:29:16 +00:00
if ( ! empty ( $info -> onclick )) {
$mod [ $seq ] -> onclick = $info -> onclick ;
}
if ( ! empty ( $info -> customdata )) {
$mod [ $seq ] -> customdata = $info -> customdata ;
}
} else {
// When using a stdclass, the (horrible) deprecated ->extra field
// is available for BC
if ( ! empty ( $info -> extra )) {
$mod [ $seq ] -> extra = $info -> extra ;
}
}
2003-09-14 12:25:16 +00:00
}
}
2011-07-21 15:05:36 +01:00
// When there is no modname_get_coursemodule_info function,
// but showdescriptions is enabled, then we use the 'intro'
// and 'introformat' fields in the module table
if ( ! $hasfunction && $rawmods [ $seq ] -> showdescription ) {
if ( $modvalues = $DB -> get_record ( $rawmods [ $seq ] -> modname ,
array ( 'id' => $rawmods [ $seq ] -> instance ), 'name, intro, introformat' )) {
// Set content from intro and introformat. Filters are disabled
// because we filter it with format_text at display time
$mod [ $seq ] -> content = format_module_intro ( $rawmods [ $seq ] -> modname ,
$modvalues , $rawmods [ $seq ] -> id , false );
// To save making another query just below, put name in here
$mod [ $seq ] -> name = $modvalues -> name ;
}
}
2008-02-01 08:15:38 +00:00
if ( ! isset ( $mod [ $seq ] -> name )) {
2010-02-14 20:18:10 +00:00
$mod [ $seq ] -> name = $DB -> get_field ( $rawmods [ $seq ] -> modname , " name " , array ( " id " => $rawmods [ $seq ] -> instance ));
2008-02-01 08:15:38 +00:00
}
2011-01-26 10:29:16 +00:00
// Minimise the database size by unsetting default options when they are
// 'empty'. This list corresponds to code in the cm_info constructor.
2011-07-20 17:24:55 +01:00
foreach ( array ( 'idnumber' , 'groupmode' , 'groupingid' , 'groupmembersonly' ,
2011-11-07 17:42:56 +01:00
'indent' , 'completion' , 'extra' , 'extraclasses' , 'iconurl' , 'onclick' , 'content' ,
2011-03-11 17:26:23 +00:00
'icon' , 'iconcomponent' , 'customdata' , 'showavailability' , 'availablefrom' ,
'availableuntil' , 'conditionscompletion' , 'conditionsgrade' ,
2011-07-20 17:24:55 +01:00
'completionview' , 'completionexpected' , 'score' , 'showdescription' )
as $property ) {
2011-01-26 10:29:16 +00:00
if ( property_exists ( $mod [ $seq ], $property ) &&
empty ( $mod [ $seq ] -> { $property })) {
unset ( $mod [ $seq ] -> { $property });
}
}
2011-03-11 17:26:23 +00:00
// Special case: this value is usually set to null, but may be 0
if ( property_exists ( $mod [ $seq ], 'completiongradeitemnumber' ) &&
is_null ( $mod [ $seq ] -> completiongradeitemnumber )) {
unset ( $mod [ $seq ] -> completiongradeitemnumber );
}
2002-11-10 07:37:15 +00:00
}
}
}
}
return $mod ;
}
2008-06-01 19:05:07 +00:00
/**
2012-09-25 10:34:18 +08:00
* Returns the localised human - readable names of all used modules
*
* @ param bool $plural if true returns the plural forms of the names
* @ return array where key is the module name ( component name without 'mod_' ) and
* the value is the human - readable string . Array sorted alphabetically by value
2008-06-01 19:05:07 +00:00
*/
2012-09-25 10:34:18 +08:00
function get_module_types_names ( $plural = false ) {
static $modnames = null ;
global $DB , $CFG ;
if ( $modnames === null ) {
$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 ) {
$modnames [ 0 ][ $mod -> name ] = get_string ( " modulename " , " $mod->name " );
$modnames [ 1 ][ $mod -> name ] = get_string ( " modulenameplural " , " $mod->name " );
}
2007-08-20 10:52:59 +00:00
}
2013-08-06 20:58:28 +02:00
core_collator :: asort ( $modnames [ 0 ]);
core_collator :: asort ( $modnames [ 1 ]);
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 ) {
global $DB ;
$DB -> set_field ( " course " , " marker " , $marker , array ( 'id' => $courseid ));
2012-11-15 16:20:05 +08:00
format_base :: reset_course_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 ))) {
$DB -> set_field ( " course_sections " , " visible " , " $visibility " , array ( " id " => $section -> id ));
2003-05-04 07:59:46 +00:00
if ( ! empty ( $section -> sequence )) {
$modules = explode ( " , " , $section -> sequence );
foreach ( $modules as $moduleid ) {
2012-11-07 15:13:07 +08:00
if ( $cm = $DB -> get_record ( 'course_modules' , array ( 'id' => $moduleid ), 'visible, visibleold' )) {
if ( $visibility ) {
// As we unhide the section, we use the previously saved visibility stored in visibleold.
set_coursemodule_visible ( $moduleid , $cm -> visibleold );
} else {
// We hide the section, so we hide the module but we store the original state in visibleold.
set_coursemodule_visible ( $moduleid , 0 );
$DB -> set_field ( 'course_modules' , 'visibleold' , $cm -> visible , array ( 'id' => $moduleid ));
}
}
2003-05-04 07:59:46 +00:00
}
}
2012-10-10 11:54:48 +08:00
rebuild_course_cache ( $courseid , 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
// Determine which modules are visible for AJAX update
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
2011-12-06 15:50:30 +00:00
/**
* Retrieve all metadata for the requested modules
*
* @ param object $course The Course
* @ param array $modnames An array containing the list of modules and their
* names
2012-06-15 16:16:52 +08:00
* @ param int $sectionreturn The section to return to
2011-12-06 15:50:30 +00:00
* @ return array A list of stdClass objects containing metadata about each
* module
*/
2012-09-13 11:00:34 +08:00
function get_module_metadata ( $course , $modnames , $sectionreturn = null ) {
2011-12-06 15:50:30 +00:00
global $CFG , $OUTPUT ;
// get_module_metadata will be called once per section on the page and courses may show
// different modules to one another
static $modlist = array ();
if ( ! isset ( $modlist [ $course -> id ])) {
$modlist [ $course -> id ] = array ();
}
$return = array ();
2012-12-12 13:45:01 +08:00
$urlbase = new moodle_url ( '/course/mod.php' , array ( 'id' => $course -> id , 'sesskey' => sesskey ()));
if ( $sectionreturn !== null ) {
$urlbase -> param ( 'sr' , $sectionreturn );
}
2011-12-06 15:50:30 +00:00
foreach ( $modnames as $modname => $modnamestr ) {
if ( ! course_allowed_module ( $course , $modname )) {
continue ;
}
2012-11-29 12:56:52 +13:00
if ( isset ( $modlist [ $course -> id ][ $modname ])) {
2011-12-06 15:50:30 +00:00
// This module is already cached
$return [ $modname ] = $modlist [ $course -> id ][ $modname ];
continue ;
}
// Include the module lib
$libfile = " $CFG->dirroot /mod/ $modname /lib.php " ;
if ( ! file_exists ( $libfile )) {
continue ;
}
include_once ( $libfile );
// NOTE: this is legacy stuff, module subtypes are very strongly discouraged!!
$gettypesfunc = $modname . '_get_types' ;
2013-08-27 09:11:44 -07:00
$types = MOD_SUBTYPE_NO_CHILDREN ;
2011-12-06 15:50:30 +00:00
if ( function_exists ( $gettypesfunc )) {
2012-11-29 09:27:47 +00:00
$types = $gettypesfunc ();
2013-08-27 09:11:44 -07:00
}
if ( $types !== MOD_SUBTYPE_NO_CHILDREN ) {
2012-11-29 09:27:47 +00:00
if ( is_array ( $types ) && count ( $types ) > 0 ) {
2011-12-06 15:50:30 +00:00
$group = new stdClass ();
$group -> name = $modname ;
$group -> icon = $OUTPUT -> pix_icon ( 'icon' , '' , $modname , array ( 'class' => 'icon' ));
foreach ( $types as $type ) {
if ( $type -> typestr === '--' ) {
continue ;
}
if ( strpos ( $type -> typestr , '--' ) === 0 ) {
$group -> title = str_replace ( '--' , '' , $type -> typestr );
continue ;
}
// Set the Sub Type metadata
$subtype = new stdClass ();
$subtype -> title = $type -> typestr ;
$subtype -> type = str_replace ( '&' , '&' , $type -> type );
$subtype -> name = preg_replace ( '/.*type=/' , '' , $subtype -> type );
$subtype -> archetype = $type -> modclass ;
// The group archetype should match the subtype archetypes and all subtypes
// should have the same archetype
$group -> archetype = $subtype -> archetype ;
2013-08-27 09:11:44 -07:00
if ( ! empty ( $type -> help )) {
$subtype -> help = $type -> help ;
} else if ( get_string_manager () -> string_exists ( 'help' . $subtype -> name , $modname )) {
2011-12-06 15:50:30 +00:00
$subtype -> help = get_string ( 'help' . $subtype -> name , $modname );
}
2013-01-17 12:45:24 +11:00
$subtype -> link = new moodle_url ( $urlbase , array ( 'add' => $modname , 'type' => $subtype -> name ));
2011-12-06 15:50:30 +00:00
$group -> types [] = $subtype ;
}
$modlist [ $course -> id ][ $modname ] = $group ;
}
} else {
$module = new stdClass ();
2012-12-12 13:45:01 +08:00
$module -> title = $modnamestr ;
2011-12-06 15:50:30 +00:00
$module -> name = $modname ;
2012-12-12 13:45:01 +08:00
$module -> link = new moodle_url ( $urlbase , array ( 'add' => $modname ));
2011-12-06 15:50:30 +00:00
$module -> icon = $OUTPUT -> pix_icon ( 'icon' , '' , $module -> name , array ( 'class' => 'icon' ));
2011-11-25 10:20:34 +00:00
$sm = get_string_manager ();
if ( $sm -> string_exists ( 'modulename_help' , $modname )) {
2011-12-06 15:50:30 +00:00
$module -> help = get_string ( 'modulename_help' , $modname );
2011-11-25 10:20:34 +00:00
if ( $sm -> string_exists ( 'modulename_link' , $modname )) { // Link to further info in Moodle docs
$link = get_string ( 'modulename_link' , $modname );
$linktext = get_string ( 'morehelp' );
2012-10-07 09:55:08 +01:00
$module -> help .= html_writer :: tag ( 'div' , $OUTPUT -> doc_link ( $link , $linktext , true ), array ( 'class' => 'helpdoclink' ));
2011-11-25 10:20:34 +00:00
}
2011-12-06 15:50:30 +00:00
}
$module -> archetype = plugin_supports ( 'mod' , $modname , FEATURE_MOD_ARCHETYPE , MOD_ARCHETYPE_OTHER );
$modlist [ $course -> id ][ $modname ] = $module ;
}
2012-11-29 09:27:47 +00:00
if ( isset ( $modlist [ $course -> id ][ $modname ])) {
$return [ $modname ] = $modlist [ $course -> id ][ $modname ];
} else {
debugging ( " Invalid module metadata configuration for { $modname } " );
}
2011-12-06 15:50:30 +00:00
}
return $return ;
}
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.
* @ return object the corresponding context
*/
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-06-01 19:05:07 +00:00
/**
2013-02-21 10:37:52 +11:00
* Returns full course categories trees to be used in html_writer :: select ()
*
* Calls { @ link coursecat :: make_categories_list ()} to build the tree and
* adds whitespace to denote nesting
*
* @ return array array mapping coursecat id to the display name
2008-06-01 19:05:07 +00:00
*/
2005-08-16 06:15:49 +00:00
function make_categories_options () {
2013-02-21 10:37:52 +11:00
global $CFG ;
require_once ( $CFG -> libdir . '/coursecatlib.php' );
2013-08-11 22:38:16 +10:00
$cats = coursecat :: make_categories_list ( '' , 0 , ' / ' );
2005-08-16 06:15:49 +00:00
foreach ( $cats as $key => $value ) {
2013-08-11 22:38:16 +10:00
// Prefix the value with the number of spaces equal to category depth (number of separators in the value).
$cats [ $key ] = str_repeat ( ' ' , substr_count ( $value , ' / ' )) . $value ;
2005-08-16 06:15:49 +00:00
}
return $cats ;
}
2003-07-30 13:02:45 +00:00
2008-12-05 08:56:54 +00:00
/**
* Print the buttons relating to course requests .
*
2013-03-19 15:45:55 +11:00
* @ param object $context current page context .
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 ;
2008-12-05 08:56:54 +00:00
if ( empty ( $CFG -> enablecourserequests )) {
return ;
}
2013-03-19 15:45:55 +11:00
if ( ! has_capability ( 'moodle/course:create' , $context ) && has_capability ( 'moodle/course:request' , $context )) {
2008-12-05 08:56:54 +00:00
/// Print a button to request a new course
2013-03-19 15:45:55 +11:00
echo $OUTPUT -> single_button ( new moodle_url ( '/course/request.php' ), get_string ( 'requestcourse' ), 'get' );
2008-12-05 08:56:54 +00:00
}
/// Print a button to manage pending requests
2013-03-19 15:45:55 +11:00
if ( $context -> contextlevel == CONTEXT_SYSTEM && 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
}
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 ) {
2008-06-01 19:05:07 +00:00
global $DB ;
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 ());
if ( is_object ( $courseorid )) {
$courseorid = $courseorid -> id ;
}
2012-10-08 10:36:01 +08:00
$coursechanged = false ;
foreach ( $sections as $sectionnum ) {
if ( ! in_array ( $sectionnum , $existing )) {
$cw = new stdClass ();
2012-10-10 12:41:04 +08:00
$cw -> course = $courseorid ;
2012-10-08 10:36:01 +08:00
$cw -> section = $sectionnum ;
$cw -> summary = '' ;
$cw -> summaryformat = FORMAT_HTML ;
$cw -> sequence = '' ;
$id = $DB -> insert_record ( " course_sections " , $cw );
$coursechanged = true ;
}
}
if ( $coursechanged ) {
2012-10-10 12:41:04 +08:00
rebuild_course_cache ( $courseorid , true );
2012-10-08 10:36:01 +08:00
}
return $coursechanged ;
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 ));
2012-10-10 12:41:04 +08:00
if ( is_object ( $courseorid )) {
rebuild_course_cache ( $courseorid -> id , true );
} else {
rebuild_course_cache ( $courseorid , 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
}
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 ));
rebuild_course_cache ( $cm -> course , true );
}
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 ));
rebuild_course_cache ( $cm -> course , true );
}
return ( $cm -> idnumber != $idnumber );
2007-04-11 09:19:24 +00:00
}
2008-07-28 12:31:29 +00:00
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 .
*
* 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
* @ return bool false when the module was not found , true otherwise
*/
function set_coursemodule_visible ( $id , $visible ) {
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
2012-11-07 15:13:07 +08:00
// Trigger developer's attention when using the previously removed argument.
if ( func_num_args () > 2 ) {
debugging ( ' Wrong number of arguments passed to set_coursemodule_visible (), $prevstateoverrides
has been removed . ' , DEBUG_DEVELOPER );
}
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.
if ( $cm -> visible == $visible ) {
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 ;
}
2008-06-01 19:05:07 +00:00
if ( $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-12-19 09:46:16 +08:00
// Hide the associated grade items so the teacher doesn't also have to go to the gradebook and hide them there.
2010-09-16 08:16:26 +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 );
}
2010-09-03 02:49:04 +00:00
}
2010-09-03 18:14:55 +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 ;
$cminfo -> visibleold = $visible ;
$DB -> update_record ( 'course_modules' , $cminfo );
2012-10-10 11:54:48 +08:00
rebuild_course_cache ( $cm -> course , true );
return true ;
2003-04-14 15:11:09 +00:00
}
2008-06-01 19:05:07 +00:00
/**
2012-12-10 17:33:09 +08:00
* This function will handles the whole deletion process of a module . This includes calling
* 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
* @ since 2.5
2005-03-28 06:47:54 +00:00
*/
2012-12-10 17:33:09 +08:00
function course_delete_module ( $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' );
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. " );
}
// 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
}
// 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 ))) {
2004-05-05 07:07:56 +00:00
foreach ( $events as $event ) {
2013-07-17 13:46:31 +08:00
$calendarevent = calendar_event :: load ( $event -> id );
$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
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 ));
$DB -> delete_records ( 'course_modules_availability' , array ( 'coursemoduleid' => $cm -> id ));
2012-05-10 11:49:01 +08:00
$DB -> delete_records ( 'course_modules_avail_fields' , array ( 'coursemoduleid' => $cm -> id ));
2012-03-06 14:06:04 +00:00
$DB -> delete_records ( 'course_completion_criteria' , array ( 'moduleinstance' => $cm -> id ,
'criteriatype' => COMPLETION_CRITERIA_TYPE_ACTIVITY ));
2009-01-09 05:32:08 +00:00
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 (
'modulename' => $modulename ,
'instanceid' => $cm -> instance ,
)
));
$event -> add_record_snapshot ( 'course_modules' , $cm );
$event -> trigger ();
2012-10-10 11:54:48 +08:00
rebuild_course_cache ( $cm -> course , true );
2002-08-01 03:50:27 +00:00
}
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
}
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
* @ return boolean Result
*/
function move_section_to ( $course , $section , $destination ) {
/// 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 ();
if (( array_key_exists ( 'numsections' , $courseformatoptions ) &&
( $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 ) {
if ( $sections [ $id ] !== $position ) {
$DB -> set_field ( 'course_sections' , 'section' , - $position , array ( 'id' => $id ));
}
}
2012-04-23 12:20:57 +08:00
foreach ( $movedsections as $id => $position ) {
if ( $sections [ $id ] !== $position ) {
$DB -> set_field ( 'course_sections' , 'section' , $position , array ( 'id' => $id ));
}
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 );
2012-06-11 11:47:04 +08:00
} elseif ( $section > $course -> marker && $course -> marker >= $destination ) {
2012-05-09 11:25:08 +01:00
course_set_marker ( $course -> id , $course -> marker + 1 );
2012-06-11 11:47:04 +08:00
} elseif ( $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 ();
2012-10-10 11:54:48 +08:00
rebuild_course_cache ( $course -> id , true );
2008-08-08 05:25:54 +00:00
return true ;
}
/**
* 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
}
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
// Add the module into the new section.
2012-09-24 17:08:02 +08:00
course_add_cm_to_section ( $section -> course , $mod -> id , $section -> section , $beforemod );
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 ) {
global $COURSE , $SITE ;
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 );
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' ,
'update' , 'duplicate' , 'hide' , 'show' , 'edittitle' ), 'moodle' );
$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 " ));
$str -> forcedgroupsnone = get_string ( 'forcedmodeinbrackets' , 'moodle' , get_string ( " groupsnone " ));
$str -> forcedgroupsseparate = get_string ( 'forcedmodeinbrackets' , 'moodle' , get_string ( " groupsseparate " ));
$str -> forcedgroupsvisible = get_string ( 'forcedmodeinbrackets' , '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-07-08 11:44:32 +12:00
// AJAX edit title.
2013-01-18 16:44:16 +11:00
if ( $mod -> has_view () && $hasmanageactivities &&
2012-12-11 13:22:51 +08:00
(( $mod -> course == $COURSE -> id && course_ajax_enabled ( $COURSE )) ||
( $mod -> course == SITEID && course_ajax_enabled ( $SITE )))) {
// we will not display link if we are on some other-course page (where we should not see this module anyway)
2013-07-18 10:42:11 +12:00
$actions [ 'title' ] = new action_menu_link_secondary (
2012-05-08 12:13:19 +01:00
new moodle_url ( $baseurl , array ( 'update' => $mod -> id )),
2013-07-08 11:44:32 +12:00
new pix_icon ( 't/editstring' , $str -> edittitle , 'moodle' , array ( 'class' => 'iconsmall visibleifjs' , 'title' => '' )),
2013-07-05 10:45:05 +12:00
$str -> edittitle ,
2013-07-08 11:44:32 +12:00
array ( 'class' => 'editing_title' , 'data-action' => 'edittitle' )
2012-05-08 12:13:19 +01:00
);
}
2013-07-08 11:44:32 +12:00
// Indent.
2011-12-18 11:54:02 +01:00
if ( $hasmanageactivities ) {
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-07-05 10:45:05 +12:00
$hiddenclass = 'hidden' ;
2011-08-25 18:02:35 +12:00
if ( $indent > 0 ) {
2013-07-05 10:45:05 +12:00
$hiddenclass = '' ;
2011-08-25 18:02:35 +12:00
}
2013-07-18 10:42:11 +12:00
$actions [ 'moveleft' ] = new action_menu_link_secondary (
2013-07-05 10:45:05 +12:00
new moodle_url ( $baseurl , array ( 'id' => $mod -> id , 'indent' => '-1' )),
2013-07-08 11:44:32 +12:00
new pix_icon ( $leftarrow , $str -> moveleft , 'moodle' , array ( 'class' => 'iconsmall' , 'title' => '' )),
2013-07-05 10:45:05 +12:00
$str -> moveleft ,
2013-07-08 11:44:32 +12:00
array ( 'class' => 'editing_moveleft ' . $hiddenclass , 'data-action' => 'moveleft' )
2013-07-05 10:45:05 +12:00
);
$hiddenclass = 'hidden' ;
2011-08-25 18:02:35 +12:00
if ( $indent >= 0 ) {
2013-07-05 10:45:05 +12:00
$hiddenclass = '' ;
2011-08-25 18:02:35 +12:00
}
2013-07-18 10:42:11 +12:00
$actions [ 'moveright' ] = new action_menu_link_secondary (
2013-07-05 10:45:05 +12:00
new moodle_url ( $baseurl , array ( 'id' => $mod -> id , 'indent' => '1' )),
2013-07-08 11:44:32 +12:00
new pix_icon ( $rightarrow , $str -> moveright , 'moodle' , array ( 'class' => 'iconsmall' , 'title' => '' )),
2013-07-05 10:45:05 +12:00
$str -> moveright ,
2013-07-08 11:44:32 +12:00
array ( 'class' => 'editing_moveright ' . $hiddenclass , 'data-action' => 'moveright' )
2013-07-05 10:45:05 +12:00
);
2003-04-28 04:32:55 +00:00
}
2011-08-25 18:02:35 +12:00
2013-07-08 11:44:32 +12:00
// Move.
2011-12-18 11:54:02 +01:00
if ( $hasmanageactivities ) {
2013-07-18 10:42:11 +12:00
$actions [ 'move' ] = new action_menu_link_primary (
2012-12-11 13:22:51 +08:00
new moodle_url ( $baseurl , array ( 'copy' => $mod -> id )),
2013-07-08 11:44:32 +12:00
new pix_icon ( 't/move' , $str -> move , 'moodle' , array ( 'class' => 'iconsmall' , 'title' => '' )),
2013-07-05 10:45:05 +12:00
$str -> move ,
2013-07-08 11:44:32 +12:00
array ( 'class' => 'editing_move status' , 'data-action' => 'move' )
2012-12-11 13:22:51 +08:00
);
2011-08-25 18:02:35 +12:00
}
2013-07-08 11:44:32 +12:00
// Update.
2011-12-18 11:54:02 +01:00
if ( $hasmanageactivities ) {
2013-07-18 10:42:11 +12:00
$actions [ 'update' ] = new action_menu_link_secondary (
2011-12-18 11:54:02 +01:00
new moodle_url ( $baseurl , array ( 'update' => $mod -> id )),
2013-07-08 11:44:32 +12:00
new pix_icon ( 't/edit' , $str -> update , 'moodle' , array ( 'class' => 'iconsmall' , 'title' => '' )),
2013-07-05 10:45:05 +12:00
$str -> update ,
2013-07-08 11:44:32 +12:00
array ( 'class' => 'editing_update' , 'data-action' => 'update' )
2011-12-18 11:54:02 +01: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)
2013-07-08 16:00:50 +12:00
// Note that restoring on front page is never allowed.
2012-12-11 13:22:51 +08:00
if ( $mod -> course != SITEID && has_all_capabilities ( $dupecaps , $coursecontext ) &&
plugin_supports ( 'mod' , $mod -> modname , FEATURE_BACKUP_MOODLE2 )) {
2013-07-18 10:42:11 +12:00
$actions [ 'duplicate' ] = new action_menu_link_secondary (
2011-09-09 15:53:02 +02:00
new moodle_url ( $baseurl , array ( 'duplicate' => $mod -> id )),
2013-07-08 11:44:32 +12:00
new pix_icon ( 't/copy' , $str -> duplicate , 'moodle' , array ( 'class' => 'iconsmall' , 'title' => '' )),
2013-07-05 10:45:05 +12:00
$str -> duplicate ,
2013-07-08 11:44:32 +12:00
array ( 'class' => 'editing_duplicate' , 'data-action' => 'duplicate' )
2011-09-09 15:53:02 +02:00
);
}
2011-08-25 18:02:35 +12:00
2013-07-08 11:44:32 +12:00
// Delete.
2011-12-18 11:54:02 +01:00
if ( $hasmanageactivities ) {
2013-07-18 10:42:11 +12:00
$actions [ 'delete' ] = new action_menu_link_secondary (
2011-12-18 11:54:02 +01:00
new moodle_url ( $baseurl , array ( 'delete' => $mod -> id )),
2013-07-08 11:44:32 +12:00
new pix_icon ( 't/delete' , $str -> delete , 'moodle' , array ( 'class' => 'iconsmall' , 'title' => '' )),
2013-07-05 10:45:05 +12:00
$str -> delete ,
2013-07-08 11:44:32 +12:00
array ( 'class' => 'editing_delete' , 'data-action' => 'delete' )
2011-12-18 11:54:02 +01:00
);
}
2011-08-25 18:02:35 +12:00
2013-07-08 11:44:32 +12:00
// Hide/Show.
2007-02-16 07:57:19 +00:00
if ( has_capability ( 'moodle/course:activityvisibility' , $modcontext )) {
if ( $mod -> visible ) {
2013-07-18 10:42:11 +12:00
$actions [ 'hide' ] = new action_menu_link_primary (
2011-08-25 18:02:35 +12:00
new moodle_url ( $baseurl , array ( 'hide' => $mod -> id )),
2013-07-08 11:44:32 +12:00
new pix_icon ( 't/hide' , $str -> hide , 'moodle' , array ( 'class' => 'iconsmall' , 'title' => '' )),
2013-07-05 10:45:05 +12:00
$str -> hide ,
2013-07-08 11:44:32 +12:00
array ( 'class' => 'editing_hide' , 'data-action' => 'hide' )
2011-08-25 18:02:35 +12:00
);
2007-02-16 07:57:19 +00:00
} else {
2013-07-18 10:42:11 +12:00
$actions [ 'show' ] = new action_menu_link_primary (
2011-08-25 18:02:35 +12:00
new moodle_url ( $baseurl , array ( 'show' => $mod -> id )),
2013-07-08 11:44:32 +12:00
new pix_icon ( 't/show' , $str -> show , 'moodle' , array ( 'class' => 'iconsmall' , 'title' => '' )),
2013-07-05 10:45:05 +12:00
$str -> show ,
2013-07-08 11:44:32 +12:00
array ( 'class' => 'editing_show' , 'data-action' => 'show' )
2011-08-25 18:02:35 +12:00
);
2007-02-16 07:57:19 +00:00
}
2003-07-14 13:08:38 +00:00
}
2008-04-25 14:03:17 +00:00
2013-07-08 11:44:32 +12:00
// Groupmode.
2012-12-13 14:27:11 +08:00
if ( $hasmanageactivities and plugin_supports ( 'mod' , $mod -> modname , FEATURE_GROUPS , 0 )) {
2013-08-27 12:08:53 +10:00
if ( $mod -> effectivegroupmode == SEPARATEGROUPS ) {
2013-07-08 16:00:50 +12:00
$nextgroupmode = VISIBLEGROUPS ;
2004-02-15 04:13:25 +00:00
$grouptitle = $str -> groupsseparate ;
2012-01-06 08:58:56 +00:00
$forcedgrouptitle = $str -> forcedgroupsseparate ;
2012-12-11 13:22:51 +08:00
$actionname = 'groupsseparate' ;
2011-08-25 18:02:35 +12:00
$groupimage = 't/groups' ;
2013-08-27 12:08:53 +10:00
} else if ( $mod -> effectivegroupmode == VISIBLEGROUPS ) {
2013-07-08 16:00:50 +12:00
$nextgroupmode = NOGROUPS ;
2004-02-15 04:13:25 +00:00
$grouptitle = $str -> groupsvisible ;
2012-01-06 08:58:56 +00:00
$forcedgrouptitle = $str -> forcedgroupsvisible ;
2012-12-11 13:22:51 +08:00
$actionname = 'groupsvisible' ;
2011-08-25 18:02:35 +12:00
$groupimage = 't/groupv' ;
2004-02-15 04:13:25 +00:00
} else {
2013-07-08 16:00:50 +12:00
$nextgroupmode = SEPARATEGROUPS ;
2004-02-15 04:13:25 +00:00
$grouptitle = $str -> groupsnone ;
2012-01-06 08:58:56 +00:00
$forcedgrouptitle = $str -> forcedgroupsnone ;
2012-12-11 13:22:51 +08:00
$actionname = 'groupsnone' ;
2011-08-25 18:02:35 +12:00
$groupimage = 't/groupn' ;
2004-02-15 04:13:25 +00:00
}
2012-12-13 14:27:11 +08:00
if ( ! $mod -> coursegroupmodeforce ) {
2013-07-18 10:42:11 +12:00
$actions [ $actionname ] = new action_menu_link_primary (
2013-07-08 16:00:50 +12:00
new moodle_url ( $baseurl , array ( 'id' => $mod -> id , 'groupmode' => $nextgroupmode )),
2013-07-08 11:44:32 +12:00
new pix_icon ( $groupimage , $grouptitle , 'moodle' , array ( 'class' => 'iconsmall' , 'title' => '' )),
2013-07-05 10:45:05 +12:00
$grouptitle ,
2013-07-08 16:00:50 +12:00
array ( 'class' => 'editing_' . $actionname , 'data-action' => $actionname , 'data-nextgroupmode' => $nextgroupmode )
2011-08-25 18:02:35 +12:00
);
2004-01-31 08:40:38 +00:00
} else {
2013-08-01 14:55:32 +12:00
$actions [ $actionname ] = new pix_icon ( $groupimage , $forcedgrouptitle , 'moodle' , array ( 'class' => 'iconsmall' ));
2004-01-31 08:40:38 +00:00
}
2003-07-14 13:08:38 +00: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 )),
2013-07-08 11:44:32 +12:00
new pix_icon ( 't/assignroles' , $str -> assign , 'moodle' , array ( 'class' => 'iconsmall' , 'title' => '' )),
2013-07-05 10:45:05 +12:00
$str -> assign ,
2013-07-08 11:44:32 +12:00
array ( 'class' => 'editing_assign' , 'data-action' => 'assignroles' )
2011-08-25 18:02:35 +12:00
);
}
2012-12-11 13:22:51 +08:00
return $actions ;
2002-08-02 17:38:18 +00:00
}
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' .
* @ 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
*/
2012-03-07 11:33:50 +00:00
function course_allowed_module ( $course , $modname ) {
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 );
return has_capability ( $capability , $coursecontext );
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.
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 ;
}
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 ;
$course -> category = $category -> id ;
$course -> sortorder = $category -> sortorder + MAX_COURSES_IN_CATEGORY - $i ++ ;
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 ,
'fullname' => $dbcourse -> fullname )
));
$event -> set_legacy_logdata ( array ( $course -> id , 'course' , 'move' , 'edit.php?id=' . $course -> id , $course -> id ));
$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
* @ see format_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 .
* The property ( array ) testedbrowsers can be used as a parameter for { @ see ajaxenabled ()} .
*
* @ 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 ) {
2009-05-04 19:56:32 +00:00
global $USER , $DB ;
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 ;
$params = array ( 'userid' => $USER -> id , 'url' => " view.php?id= $courseid " , 'since' => $since );
$select = " module = 'course' AND action = 'new' AND userid = :userid AND url = :url AND time > :since " ;
return $DB -> record_exists_select ( 'log' , $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 );
}
}
}
2007-02-16 08:46:55 +00:00
2013-03-28 12:18:32 +11:00
/**
* Returns options to use in course overviewfiles filemanager
*
* @ param null | stdClass | course_in_list | int $course either object that has 'id' property or just the course id ;
* 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 ;
}
$accepted_types = preg_split ( '/\s*,\s*/' , trim ( $CFG -> courseoverviewfilesext ), - 1 , PREG_SPLIT_NO_EMPTY );
if ( in_array ( '*' , $accepted_types ) || empty ( $accepted_types )) {
$accepted_types = '*' ;
} else {
// Since config for $CFG->courseoverviewfilesext is a text box, human factor must be considered.
// Make sure extensions are prefixed with dot unless they are valid typegroups
foreach ( $accepted_types as $i => $type ) {
if ( substr ( $type , 0 , 1 ) !== '.' ) {
require_once ( $CFG -> libdir . '/filelib.php' );
if ( ! count ( file_get_typegroup ( 'extension' , $type ))) {
// It does not start with dot and is not a valid typegroup, this is most likely extension.
$accepted_types [ $i ] = '.' . $type ;
$corrected = true ;
}
}
}
if ( ! empty ( $corrected )) {
set_config ( 'courseoverviewfilesext' , join ( ',' , $accepted_types ));
}
}
$options = array (
'maxfiles' => $CFG -> courseoverviewfileslimit ,
'maxbytes' => $CFG -> maxbytes ,
'subdirs' => 0 ,
'accepted_types' => $accepted_types
);
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 ) {
2013-08-08 16:48:17 +08:00
global $DB ;
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
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 -> timecreated = time ();
$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 = '' ;
$data -> summary_format = FORMAT_HTML ;
}
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
// Setup the blocks
blocks_add_default_course_blocks ( $course );
2007-03-23 02:29:05 +00:00
2012-10-08 10:36:01 +08:00
// Create a default section.
course_create_sections_if_missing ( $course , 0 );
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
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
// new context created - better mark it as dirty
2013-07-08 10:56:26 +08:00
$context -> mark_dirty ();
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.
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
2013-08-08 16:48:17 +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 )
));
$event -> trigger ();
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
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 ) {
2013-08-08 16:50:10 +08:00
global $DB ;
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
$data -> timemodified = time ();
2008-02-21 15:17:41 +00:00
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
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 ) {
if ( $DB -> record_exists ( 'course' , array ( 'shortname' => $data -> shortname ))) {
throw new moodle_exception ( 'shortnametaken' , '' , '' , $data -> shortname );
}
}
// Check we don't have a duplicate idnumber.
if ( ! empty ( $data -> idnumber ) && $oldcourse -> idnumber != $data -> idnumber ) {
if ( $DB -> record_exists ( 'course' , array ( 'idnumber' => $data -> idnumber ))) {
throw new moodle_exception ( 'courseidnumbertaken' , '' , '' , $data -> idnumber );
}
}
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
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
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 );
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 ,
'fullname' => $course -> fullname )
));
2013-09-04 11:07:13 +08:00
2013-08-09 17:58:30 +08:00
$event -> set_legacy_logdata ( array ( $course -> id , 'course' , 'update' , 'edit.php?id=' . $course -> id , $course -> id ));
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
/**
2010-10-18 08:01:32 +00:00
* Average number of participants
2010-05-01 05:05:55 +00:00
* @ return integer
*/
function average_number_of_participants () {
2010-10-18 08:01:32 +00:00
global $DB , $SITE ;
//count total of enrolments for visible course (except front page)
$sql = ' SELECT COUNT ( * ) FROM (
SELECT DISTINCT ue . userid , e . courseid
FROM { user_enrolments } ue , { enrol } e , { course } c
2010-11-17 06:23:56 +00:00
WHERE ue . enrolid = e . id
2010-10-18 08:01:32 +00:00
AND e . courseid <> : siteid
AND c . id = e . courseid
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 );
$enrolmenttotal = $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 enrolment
if ( empty ( $coursetotal )) {
$participantaverage = 0 ;
} 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.
2012-08-16 14:10:39 +08:00
if ( empty ( $data -> category ) || empty ( $CFG -> requestcategoryselection )) {
$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 )))) {
print_error ( 'unknowncourserequest' );
}
} 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 ;
}
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'
*
* @ return coursecat
*/
public function get_category () {
global $CFG ;
require_once ( $CFG -> libdir . '/coursecatlib.php' );
// If the category is not set, if the current user does not have the rights to change the category, or if the
// category does not exist, we set the default category to the course to be approved.
// The system level is used because the capability moodle/site:approvecourse is based on a system level.
if ( empty ( $this -> properties -> category ) || ! has_capability ( 'moodle/course:changecategory' , context_system :: instance ()) ||
( ! $category = coursecat :: get ( $this -> properties -> category , IGNORE_MISSING , true ))) {
$category = coursecat :: get ( $CFG -> defaultrequestcategory , IGNORE_MISSING , true );
}
if ( ! $category ) {
$category = coursecat :: get_default ();
}
return $category ;
}
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
$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 ;
2009-11-04 06:14:06 +00:00
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 ;
$this -> notify ( $user , $USER , 'courserequestapproved' , get_string ( 'courseapprovedsubject' ), get_string ( 'courseapprovedemail2' , 'moodle' , $a ));
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
*/
protected function notify ( $touser , $fromuser , $name = 'courserequested' , $subject , $message ) {
2010-09-21 08:13:11 +00:00
$eventdata = new stdClass ();
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
}
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 ;
// Ajax must be enabled globally
if ( ! $CFG -> enableajax ) {
return false ;
}
// 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
// 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
}
if ( ! $config ) {
$config = new stdClass ();
}
// The URL to use for resource changes
if ( ! isset ( $config -> resourceurl )) {
$config -> resourceurl = '/course/rest.php' ;
}
// The URL to use for section changes
if ( ! isset ( $config -> sectionurl )) {
$config -> sectionurl = '/course/rest.php' ;
}
// Any additional parameters which need to be included on page submission
if ( ! isset ( $config -> pageparams )) {
$config -> pageparams = array ();
}
// Include toolboxes
$PAGE -> requires -> yui_module ( 'moodle-course-toolboxes' ,
'M.course.init_resource_toolbox' ,
array ( array (
'courseid' => $course -> id ,
'ajaxurl' => $config -> resourceurl ,
'config' => $config ,
))
);
$PAGE -> requires -> yui_module ( 'moodle-course-toolboxes' ,
'M.course.init_section_toolbox' ,
array ( array (
'courseid' => $course -> id ,
'format' => $course -> format ,
'ajaxurl' => $config -> sectionurl ,
'config' => $config ,
))
);
2012-01-18 10:53:23 +00:00
// Include course dragdrop
2012-05-09 10:18:39 +01:00
if ( $course -> id != $SITE -> id ) {
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
}
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
// Require various strings for the command toolbox
$PAGE -> requires -> strings_for_js ( array (
'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' ,
'groupsnone' ,
'groupsvisible' ,
'groupsseparate' ,
'clicktochangeinbrackets' ,
'markthistopic' ,
'markedthistopic' ,
2012-01-18 10:53:23 +00:00
'move' ,
'movesection' ,
2013-08-15 14:39:00 +08:00
'movecontent' ,
2013-08-15 16:03:44 +08:00
'tocontent' ,
2013-08-15 14:39:00 +08:00
'emptydragdropregion'
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' );
// Include format-specific strings
2012-05-09 10:18:39 +01:00
if ( $course -> id != $SITE -> id ) {
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
$PAGE -> requires -> strings_for_js ( array (
'showfromothers' ,
'hidefromothers' ,
), 'format_' . $course -> format );
}
// For confirming resource deletion we need the name of the module in question
2012-05-17 10:55:35 +12:00
foreach ( $usedmodules as $module => $modname ) {
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
$PAGE -> requires -> string_for_js ( 'pluginname' , $module );
}
2012-05-09 15:31:41 +01:00
2012-02-17 21:23:28 +00:00
// Load drag and drop upload AJAX.
2013-08-12 11:34:51 +08:00
require_once ( $CFG -> dirroot . '/course/dnduploadlib.php' );
2012-05-17 10:55:35 +12:00
dndupload_add_to_course ( $course , $enabledmodules );
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
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
*/
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 );
// Load module library.
include_modulelib ( $module -> name );
// 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
*/
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
// Load module library.
2013-01-02 13:58:42 +08:00
include_modulelib ( $module -> name );
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-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 ;
}