Fixing copy promotions bug see BT#2800

skala
Julio Montoya 14 years ago
parent 0d04e8be7b
commit 366a861e2c
  1. 2
      main/admin/promotions.php
  2. 30
      main/coursecopy/classes/CourseRestorer.class.php
  3. 1
      main/inc/lib/career.lib.php
  4. 23
      main/inc/lib/course.lib.php
  5. 4
      main/inc/lib/model.lib.php
  6. 96
      main/inc/lib/promotion.lib.php
  7. 13
      main/inc/lib/sessionmanager.lib.php

@ -160,7 +160,7 @@ if (isset($_GET['action']) && $_GET['action'] == 'add') {
if (api_get_session_id() != 0 && !api_is_allowed_to_session_edit(false, true)) { if (api_get_session_id() != 0 && !api_is_allowed_to_session_edit(false, true)) {
api_not_allowed(); api_not_allowed();
} }
$res = $promotion->copy($_GET['id'], null, true); $res = $promotion->copy($_GET['id'], null, true);
if ($res) { if ($res) {
Display::display_confirmation_message(get_lang('ItemCopied').' - '.get_lang('ExerciseAndLPsAreInvisibleInTheNewCourse')); Display::display_confirmation_message(get_lang('ItemCopied').' - '.get_lang('ExerciseAndLPsAreInvisibleInTheNewCourse'));
} }

@ -54,12 +54,18 @@ class CourseRestorer
* FILE_OVERWRITE) * FILE_OVERWRITE)
*/ */
var $file_option; var $file_option;
var $set_tools_invisible_by_default;
var $skip_content;
/** /**
* Create a new CourseRestorer * Create a new CourseRestorer
*/ */
function CourseRestorer($course) { function CourseRestorer($course) {
$this->course = $course; $this->course = $course;
$this->file_option = FILE_RENAME; $this->file_option = FILE_RENAME;
$this->set_tools_invisible_by_default = false;
$this->skip_content = array();
} }
/** /**
* Set the file-option * Set the file-option
@ -156,8 +162,7 @@ class CourseRestorer
foreach ($this->course->resources as $type => $resources) { foreach ($this->course->resources as $type => $resources) {
if (is_array($resources)) { if (is_array($resources)) {
foreach ($resources as $id => $resource) { foreach ($resources as $id => $resource) {
foreach ($resource->item_properties as $property) foreach ($resource->item_properties as $property) {
{
// First check if there isn't allready a record for this resource // First check if there isn't allready a record for this resource
$sql = "SELECT * FROM $table WHERE tool = '".$property['tool']."' AND ref = '".$resource->destination_id."'"; $sql = "SELECT * FROM $table WHERE tool = '".$property['tool']."' AND ref = '".$resource->destination_id."'";
@ -1332,8 +1337,7 @@ class CourseRestorer
/** /**
* Restore learnpaths * Restore learnpaths
*/ */
function restore_learnpaths($session_id = 0, $respect_base_content = false) function restore_learnpaths($session_id = 0, $respect_base_content = false) {
{
if ($this->course->has_resources(RESOURCE_LEARNPATH)) { if ($this->course->has_resources(RESOURCE_LEARNPATH)) {
$table_main = Database::get_course_table(TABLE_LP_MAIN, $this->course->destination_db); $table_main = Database::get_course_table(TABLE_LP_MAIN, $this->course->destination_db);
$table_item = Database::get_course_table(TABLE_LP_ITEM, $this->course->destination_db); $table_item = Database::get_course_table(TABLE_LP_ITEM, $this->course->destination_db);
@ -1373,6 +1377,14 @@ class CourseRestorer
} }
} }
} }
if (isset($this->skip_content['skip_lp_dates'])) {
if ($this->skip_content['skip_lp_dates']) {
$lp->modified_on = '0000-00-00 00:00:00';
$lp->publicated_on = '0000-00-00 00:00:00';
$lp->expired_on = '0000-00-00 00:00:00';
}
}
$sql = "INSERT INTO ".$table_main." SET " . $sql = "INSERT INTO ".$table_main." SET " .
"lp_type = '".$lp->lp_type."', " . "lp_type = '".$lp->lp_type."', " .
@ -1403,10 +1415,14 @@ class CourseRestorer
$new_lp_id = Database::insert_id(); $new_lp_id = Database::insert_id();
if($lp->visibility) { if ($lp->visibility) {
$sql = "INSERT INTO $table_tool SET name='".Database::escape_string($lp->name)."', link='newscorm/lp_controller.php?action=view&lp_id=$new_lp_id', image='scormbuilder.gif', visibility='1', admin='0', address='squaregrey.gif'"; $sql = "INSERT INTO $table_tool SET name='".Database::escape_string($lp->name)."', link='newscorm/lp_controller.php?action=view&lp_id=$new_lp_id', image='scormbuilder.gif', visibility='1', admin='0', address='squaregrey.gif'";
Database::query($sql); Database::query($sql);
} }
if ($new_lp_id) {
api_item_property_update($this->destination_course_info, TOOL_LEARNPATH, $new_lp_id, 'LearnpathAdded', api_get_user_id(), 0, 0, 0, 0, $session_id);
}
$new_item_ids = array(); $new_item_ids = array();
$parent_item_ids = array(); $parent_item_ids = array();

@ -169,6 +169,7 @@ class Career extends Model {
if (!empty($id)) { if (!empty($id)) {
event_system(LOG_CAREER_CREATE, LOG_CAREER_ID, $id, api_get_utc_datetime(), api_get_user_id()); event_system(LOG_CAREER_CREATE, LOG_CAREER_ID, $id, api_get_utc_datetime(), api_get_user_id());
} }
return $id;
} }
public function delete($id) { public function delete($id) {

@ -3146,16 +3146,15 @@ class CourseManager {
return $output; return $output;
} }
/** /**
* *
* @param string source course code * @param string source course code
* @param int source session id * @param int source session id
* @param string destination course code * @param string destination course code
* @param int destination session id * @param int destination session id
* @return bool * @return bool
*/ */
function copy_course($source_course_code, $source_session_id, $destination_course_code, $destination_session_id) { function copy_course($source_course_code, $source_session_id, $destination_course_code, $destination_session_id, $params = array()) {
require_once api_get_path(SYS_CODE_PATH).'coursecopy/classes/CourseBuilder.class.php'; require_once api_get_path(SYS_CODE_PATH).'coursecopy/classes/CourseBuilder.class.php';
require_once api_get_path(SYS_CODE_PATH).'coursecopy/classes/CourseRestorer.class.php'; require_once api_get_path(SYS_CODE_PATH).'coursecopy/classes/CourseRestorer.class.php';
require_once api_get_path(SYS_CODE_PATH).'coursecopy/classes/CourseSelectForm.class.php'; require_once api_get_path(SYS_CODE_PATH).'coursecopy/classes/CourseSelectForm.class.php';
@ -3165,7 +3164,8 @@ class CourseManager {
if (!empty($course_info)) { if (!empty($course_info)) {
$cb = new CourseBuilder('', $course_info); $cb = new CourseBuilder('', $course_info);
$course = $cb->build($source_session_id, $source_course_code, true); $course = $cb->build($source_session_id, $source_course_code, true);
$course_restorer = new CourseRestorer($course); $course_restorer = new CourseRestorer($course);
$course_restorer->skip_content = $params;
$course_restorer->restore($destination_course_code, $destination_session_id, true, true); $course_restorer->restore($destination_course_code, $destination_session_id, true, true);
return true; return true;
} }
@ -3174,21 +3174,24 @@ class CourseManager {
/** /**
* A simpler version of the copy_course, the function creates an empty course * A simpler version of the copy_course, the function creates an empty course with an autogenerated course code
* *
* @param string course title * @param string new course title
* @param string source course code * @param string source course code
* @param int source session id * @param int source session id
* @param int destination session id
* @param bool new copied tools (Exercises and LPs)will be set to invisible by default?
*
* @return array * @return array
*/ */
function copy_course_simple($new_title, $source_course_code, $source_session_id = 0, $destination_session_id = 0) { function copy_course_simple($new_title, $source_course_code, $source_session_id = 0, $destination_session_id = 0, $params = array()) {
$source_course_info = api_get_course_info($source_course_code); $source_course_info = api_get_course_info($source_course_code);
if (!empty($source_course_info)) { if (!empty($source_course_info)) {
$new_course_code = self::generate_nice_next_course_code($source_course_code); $new_course_code = self::generate_nice_next_course_code($source_course_code);
if ($new_course_code) { if ($new_course_code) {
$new_course_info = self::create_course($new_title, $new_course_code, false); $new_course_info = self::create_course($new_title, $new_course_code, false);
if (!empty($new_course_info['code'])) { if (!empty($new_course_info['code'])) {
$result = self::copy_course($source_course_code, $source_session_id, $new_course_info['code'], $destination_session_id); $result = self::copy_course($source_course_code, $source_session_id, $new_course_info['code'], $destination_session_id, $params);
if ($result) { if ($result) {
return $new_course_info; return $new_course_info;
} }

@ -100,14 +100,14 @@ class Model {
* @return bool * @return bool
* *
*/ */
public function save($params) { public function save($params, $show_query = false) {
$params = $this->clean_parameters($params); $params = $this->clean_parameters($params);
if (in_array('created_at', $this->columns)) { if (in_array('created_at', $this->columns)) {
$params['created_at'] = api_get_utc_datetime(); $params['created_at'] = api_get_utc_datetime();
} }
if (!empty($params)) { if (!empty($params)) {
$id = Database::insert($this->table, $params); $id = Database::insert($this->table, $params, $show_query);
if (is_numeric($id)){ if (is_numeric($id)){
return $id; return $id;
} }

@ -35,55 +35,58 @@ class Promotion extends Model {
* @return integer New promotion ID on success, false on failure * @return integer New promotion ID on success, false on failure
*/ */
public function copy($id, $career_id = null, $copy_sessions = false) { public function copy($id, $career_id = null, $copy_sessions = false) {
$promotion = $this->get($id); $pid = false;
$new = array(); $promotion = $this->get($id);
foreach ($promotion as $key => $val) { if (!empty($promotion)) {
switch ($key) { $new = array();
case 'id': foreach ($promotion as $key => $val) {
case 'updated_at': switch ($key) {
break; case 'id':
case 'name': case 'updated_at':
$val .= ' '.get_lang('Copy'); break;
$new[$key] = $val; case 'name':
break; $val .= ' '.get_lang('Copy');
case 'created_at': $new[$key] = $val;
$val = api_get_utc_datetime(); break;
$new[$key] = $val; case 'created_at':
$val = api_get_utc_datetime();
$new[$key] = $val;
break;
case 'career_id':
if (!empty($career_id)) {
$val = (int)$career_id;
}
$new[$key] = $val;
default:
$new[$key] = $val;
break; break;
case 'career_id': }
if (!empty($career_id)) {
$val = (int)$career_id;
}
$new[$key] = $val;
default:
$new[$key] = $val;
break;
} }
}
$pid = false; if ($copy_sessions) {
if ($copy_sessions) { /**
/** * When copying a session we do:
* When copying a session we do: * 1. Copy a new session from the source
* 1. Copy a new session from the source * 2. Copy all courses from the session (no user data, no user list)
* 2. Copy all courses from the session (no user data, no user list) * 3. Create the promotion
* 3. Create the promotion */
*/ require_once api_get_path(LIBRARY_PATH).'sessionmanager.lib.php';
require_once api_get_path(LIBRARY_PATH).'sessionmanager.lib.php'; $session_list = SessionManager::get_all_sessions_by_promotion($id);
$session_list = SessionManager::get_all_sessions_by_promotion($id);
if (!empty($session_list)) {
if (!empty($session_list)) { $pid = $this->save($new);
$pid = $this->save($new); if (!empty($pid)) {
if (!empty($pid)) { foreach($session_list as $item) {
foreach($session_list as $item) { $sid = SessionManager::copy_session($item['id'], true, false, true, true);
$sid = SessionManager::copy_session($item['id'], true, false, true, true); if ($sid != 0) {
if ($sid != 0) { SessionManager::suscribe_sessions_to_promotion($pid, array($sid));
SessionManager::suscribe_sessions_to_promotion($pid, array($sid)); }
} }
} }
} }
} else {
$pid = $this->save($new);
} }
} else {
$pid = $this->save($new);
} }
return $pid; return $pid;
} }
@ -196,11 +199,12 @@ class Promotion extends Model {
return $form; return $form;
} }
public function save($params) { public function save($params, $show_query = false) {
$id = parent::save($params); $id = parent::save($params, $show_query);
if (!empty($id)) { if (!empty($id)) {
event_system(LOG_PROMOTION_CREATE, LOG_PROMOTION_ID, $id, api_get_utc_datetime(), api_get_user_id()); event_system(LOG_PROMOTION_CREATE, LOG_PROMOTION_ID, $id, api_get_utc_datetime(), api_get_user_id());
} }
return $id;
} }
public function delete($id) { public function delete($id) {

@ -1453,7 +1453,7 @@ class SessionManager {
* @param $set_exercises_lp_invisible * @param $set_exercises_lp_invisible
* @return unknown_type * @return unknown_type
*/ */
public function copy_session($id, $copy_courses=true, $copy_users = true, $create_new_courses = false, $set_exercises_lp_invisible = false) { public function copy_session($id, $copy_courses = true, $copy_users = true, $create_new_courses = false, $set_exercises_lp_invisible = false) {
$id = intval($id); $id = intval($id);
$s = self::fetch($id); $s = self::fetch($id);
$s['year_start'] = substr($s['date_start'],0,4); $s['year_start'] = substr($s['date_start'],0,4);
@ -1502,9 +1502,12 @@ class SessionManager {
if (function_exists('ini_set')) { if (function_exists('ini_set')) {
ini_set('memory_limit','256M'); ini_set('memory_limit','256M');
ini_set('max_execution_time',0); ini_set('max_execution_time',0);
} }
$params = array();
$params['skip_lp_dates'] = true;
foreach ($short_courses as $course_data) { foreach ($short_courses as $course_data) {
$course_info = CourseManager::copy_course_simple($course_data['title'].' '.get_lang('Copy'), $course_data['course_code'], $id, $sid); $course_info = CourseManager::copy_course_simple($course_data['title'].' '.get_lang('Copy'), $course_data['course_code'], $id, $sid, $params);
if ($course_info) { if ($course_info) {
//By default new elements are invisible //By default new elements are invisible
if ($set_exercises_lp_invisible) { if ($set_exercises_lp_invisible) {
@ -1513,8 +1516,8 @@ class SessionManager {
$flat_list = $list->get_flat_list(); $flat_list = $list->get_flat_list();
if (!empty($flat_list)) { if (!empty($flat_list)) {
foreach($flat_list as $lp_id => $data) { foreach($flat_list as $lp_id => $data) {
api_item_property_update($course_info, TOOL_LEARNPATH, $lp_id, 'invisible', api_get_user_id(), 0 ,0, $sid); api_item_property_update($course_info, TOOL_LEARNPATH, $lp_id, 'invisible', api_get_user_id(), 0 ,0, 0, 0, $sid);
api_item_property_update($course_info, TOOL_LEARNPATH, $lp_id, 'invisible', api_get_user_id(), 0 ,0); api_item_property_update($course_info, TOOL_LEARNPATH, $lp_id, 'invisible', api_get_user_id(), 0 ,0, 0, 0);
} }
} }
$quiz_table = Database::get_course_table(TABLE_QUIZ_TEST, $course_info['db_name']); $quiz_table = Database::get_course_table(TABLE_QUIZ_TEST, $course_info['db_name']);

Loading…
Cancel
Save