Creating new tasks in chamilo course-sessions

In the category of little scripts that can make your life easier when managing huge Chamilo portals, this is a little one that creates one tasks-folder called "ALP" for the "Assignments" (work) tool in each active course for a portal where you have thousands of sessions, with one course per session. The script works for version 1.9.8 of Chamilo, but might need some adaptations to run on an older version, in particular considering the addDir() function from the main/work/work.lib.php library which was created there recently. You should put the file into any "one-level" directory under the Chamilo base, so that it can find "../main/inc/global.inc.php", and then run it on the command line using php5-cli, or load it from a webserver accessing it directly by its path. The script only affects active sessions (sessions with a start and end dates that are "around" the current date). The file is downloadable as a .doc file for safety purposes. Download it and rename it to ".php" before you open it, otherwise you'll get an error: create_tasks <?php /** * This script creates tasks directories in each course, at the session level, * only for sessions active at the time of running the script * In order for the script to set the right permissions, it has to be launched * either as www-data or root * @author Yannick Warnier */ require_once '../main/inc/global.inc.php'; require_once '../main/work/work.lib.php'; $date = date('Y-m-d h:i:s'); $workName = 'ALP'; $courseInfos = array(); /** * Get the sessions list */ $sd = 'date_start'; $ed = 'date_end'; $sql = "SELECT id FROM session WHERE $sd '$date'"; $res = Database::query($sql); if ($res === false) { die("Error querying sessions: ".Database::error($res)."n"); } /** * Get the course-session couple */ $sessionCourse = array(); while ($row = Database::fetch_assoc($res)) { $sql2 = "SELECT c.id AS cid, c.code as ccode FROM course c, session_rel_course s WHERE s.id_session = ".$row['id']." AND s.course_code = c.code"; $res2 = Database::query($sql2); if ($res2 === false) { die("Error querying courses for session ".$row['id'].": ".Database::error($res2)."n"); } if (Database::num_rows($res2) > 0) { while ($row2 = Database::fetch_assoc($res2)) { $sessionCourse[$row['id']] = $row2['cid']; if (empty($courseInfos[$row2['ccode']])) { $courseInfos[$row2['cid']] = api_get_course_info($row2['ccode']); } } } } /** * Now create the tasks using the addDir function */ foreach ($sessionCourse as $sid => $cid) { $sql = "SELECT id, title FROM c_student_publication WHERE filetype = 'folder' AND c_id = $cid AND session_id = $sid"; $res = Database::query($sql); if ($res === false) { echo "Error querying table c_student_publication: $sqln"; echo "The error message was: ".Database::error($res)."n"; continue; } if (Database::num_rows($res) > 0) { //Task found, skip $row = Database::fetch_assoc($res); echo "Task ".$row['title']." already found in course $cid, session $sidn"; continue; } $params = array( 'new_dir' => $workName, 'description' => '', 'qualification' => 0, 'weight' => 0, 'allow_text_assignment' => 0 ); $res = addDir($params, 1, $courseInfos[$cid], null, $sid); if ($res === false) { echo "Could not create task $workName in course $cid, session $sid, for some reasonn"; } else { echo "Task $workName created in course $cid, session $sid. Task ID is $resn"; } } echo "All done!n";