Adding attendance programmatically

Adding attendance programmatically

by Perry Way -
Number of replies: 3

Has anyone worked out how to programmatically add the attendance activity to any course and get it to show up on the top (course) level? I have been doing what I have detected has been done in the database but it's not showing up in courses. Wondering what I missed. I have had my initial foray into the php code and haven't found anything yet. If someone else has gone before me, I'd appreciate any shortcuts or information you might have. Thanks!

Average of ratings: -
In reply to Perry Way

Re: Adding attendance programmatically

by Zvonko Martinović -

I've found that easiest way to add programmatically any resource or activity to course is via backup. Just restore backup into a course

//restore course from template
$destdir = $CFG->dataroot.'/temp/backup/'.md5($course->shortname);
$zipfile = $CFG->dataroot.'/course_templates/backup_file.mbz';
if(file_exists($zipfile)){
	define('CLI_SCRIPT', true);
	require_once($CFG->dirroot.'/backup/util/includes/restore_includes.php');
	$packer = new zip_packer();
	$packer->extract_to_pathname($zipfile, $destdir);

	// Transaction
	$transaction = $DB->start_delegated_transaction();
	// Restore backup into course
	$controller = new restore_controller(md5($course->shortname), $course->id, backup::INTERACTIVE_NO, backup::MODE_SAMESITE, 2, backup::TARGET_CURRENT_ADDING);
	$controller->execute_precheck();
	$controller->execute_plan();
	// Commit
	$transaction->allow_commit();
	define('CLI_SCRIPT', false);

	$mods = unserialize($course->modinfo);
	//here you can edit and resource or activity

	rebuild_course_cache($course->id, true);
}

Hope this helps you.

Average of ratings: Useful (1)
In reply to Zvonko Martinović

Re: Adding attendance programmatically

by Perry Way -

Thanks Zvonko! I didn't think about that. I will give that a try, and thanks for the script idea!

Oddly enough however, I had some luck with creating database records for certain items like URLs. And they would show up in the course if you hadn't already done a lot of sorting and moving around of previously created items. But now my database hack won't work. Backing up from one course and restoring to another course is a great idea, particularly for automating the task of making all courses have an attendance block!

In reply to Zvonko Martinović

Re: Adding attendance programmatically

by Jason Allshorn -

This is code great. 

It helps with so many of our processes. 


Do you have any experience populating/creating attendance schedules programmatically?

For example our back-end database contains all time-table data for a class. But, instead of a teacher or course admin manually adding the data to create the course attendance record we'd like it to automatically be added when we open the class. 

The reason for this is we have hundreds of classes opening up each month.