Partial fix when creating a course with contents.

Remove sql and replace using functions.
1.10.x
Julio Montoya 11 years ago
parent f638eb064c
commit af4d4cbddf
  1. 16
      main/inc/lib/AnnouncementManager.php
  2. 148
      main/inc/lib/add_course.lib.inc.php
  3. 86
      main/inc/lib/link.lib.php

@ -348,12 +348,13 @@ class AnnouncementManager
$last_id = Database::insert($tbl_announcement, $params); $last_id = Database::insert($tbl_announcement, $params);
$sql = "UPDATE $tbl_announcement SET id = iid WHERE iid = $last_id";
Database::query($sql);
if (empty($last_id)) { if (empty($last_id)) {
return false; return false;
} else { } else {
$sql = "UPDATE $tbl_announcement SET id = iid WHERE iid = $last_id";
Database::query($sql);
if (!empty($file)) { if (!empty($file)) {
self::add_announcement_attachment_file( self::add_announcement_attachment_file(
$last_id, $last_id,
@ -531,7 +532,7 @@ class AnnouncementManager
*/ */
public static function edit_announcement($id, $emailTitle, $newContent, $to, $file = array(), $file_comment = '', $sendToUsersInSession = false) public static function edit_announcement($id, $emailTitle, $newContent, $to, $file = array(), $file_comment = '', $sendToUsersInSession = false)
{ {
global $_course; $_course = api_get_course_info();
$course_id = api_get_course_int_id(); $course_id = api_get_course_int_id();
$tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY); $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
@ -1109,7 +1110,7 @@ class AnnouncementManager
*/ */
public static function sent_to($tool, $id) public static function sent_to($tool, $id)
{ {
global $tbl_item_property; $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
$tool = Database::escape_string($tool); $tool = Database::escape_string($tool);
$id = intval($id); $id = intval($id);
@ -1134,12 +1135,15 @@ class AnnouncementManager
$sent_to_user[] = $row['to_user_id']; $sent_to_user[] = $row['to_user_id'];
} }
} }
if (isset($sent_to_group)) { if (isset($sent_to_group)) {
$sent_to['groups'] = $sent_to_group; $sent_to['groups'] = $sent_to_group;
} }
if (isset($sent_to_user)) { if (isset($sent_to_user)) {
$sent_to['users'] = $sent_to_user; $sent_to['users'] = $sent_to_user;
} }
return $sent_to; return $sent_to;
} }
@ -1225,7 +1229,7 @@ class AnnouncementManager
*/ */
public static function edit_announcement_attachment_file($id_attach, $file, $file_comment) public static function edit_announcement_attachment_file($id_attach, $file, $file_comment)
{ {
global $_course; $_course = api_get_course_info();
$tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT); $tbl_announcement_attachment = Database::get_course_table(TABLE_ANNOUNCEMENT_ATTACHMENT);
$return = 0; $return = 0;
$course_id = api_get_course_int_id(); $course_id = api_get_course_int_id();

@ -430,6 +430,8 @@ class AddCourse
if (empty($course_id)) { if (empty($course_id)) {
return false; return false;
} }
$courseInfo = api_get_course_info_by_id($course_id);
$now = api_get_utc_datetime(time()); $now = api_get_utc_datetime(time());
$tbl_course_homepage = Database::get_course_table(TABLE_TOOL_LIST); $tbl_course_homepage = Database::get_course_table(TABLE_TOOL_LIST);
@ -636,16 +638,13 @@ class AddCourse
); );
} }
// Smartblogs (Kevin Van Den Haute :: kevin@develop-it.be)
$sql = "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id) $sql = "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
VALUES ($course_id, 24,'" . TOOL_BLOGS . "','blog/blog_admin.php','blog_admin.gif','" . self::string2binary( VALUES ($course_id, 24,'" . TOOL_BLOGS . "','blog/blog_admin.php','blog_admin.gif','" . self::string2binary(
api_get_setting('course_create_active_tools', 'blogs') api_get_setting('course_create_active_tools', 'blogs')
) . "','1','squaregrey.gif','NO','_self','admin','0')"; ) . "','1','squaregrey.gif','NO','_self','admin','0')";
Database::query($sql); Database::query($sql);
// end of Smartblogs
/* Course homepage tools for course admin only */
/* Course homepage tools for course admin only */
Database::query( Database::query(
"INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id) "INSERT INTO $tbl_course_homepage (c_id, id, name, link, image, visibility, admin, address, added_tool, target, category, session_id)
VALUES ($course_id, 25, '" . TOOL_TRACKING . "','tracking/courseLog.php','statistics.gif','$visible_for_course_admin','1','', 'NO','_self','admin','0')" VALUES ($course_id, 25, '" . TOOL_TRACKING . "','tracking/courseLog.php','statistics.gif','$visible_for_course_admin','1','', 'NO','_self','admin','0')"
@ -764,9 +763,7 @@ class AddCourse
'certificates', 'certificates',
); );
$default_course_path = api_get_path( $default_course_path = api_get_path(SYS_CODE_PATH) . 'default_course_document/';
SYS_CODE_PATH
) . 'default_course_document/';
$default_document_array = array(); $default_document_array = array();
foreach ($folders_to_copy_from_default_course as $folder) { foreach ($folders_to_copy_from_default_course as $folder) {
@ -812,14 +809,10 @@ class AddCourse
} }
$course_documents_folder = $sys_course_path . $course_repository . "/document/$media_type/"; $course_documents_folder = $sys_course_path . $course_repository . "/document/$media_type/";
$default_course_path = api_get_path( $default_course_path = api_get_path(SYS_CODE_PATH) . 'default_course_document' . $path_documents;
SYS_CODE_PATH
) . 'default_course_document' . $path_documents;
//echo 'try '.$course_documents_folder; echo '<br />';
if (!is_dir($course_documents_folder)) { if (!is_dir($course_documents_folder)) {
//Creating index.html // Creating index.html
mkdir($course_documents_folder, $perm); mkdir($course_documents_folder, $perm);
$fd = fopen( $fd = fopen(
$course_documents_folder . 'index.html', $course_documents_folder . 'index.html',
@ -869,11 +862,16 @@ class AddCourse
} }
Database::query( Database::query(
"INSERT INTO $TABLETOOLDOCUMENT (c_id, path,title,filetype,size) VALUES ($course_id,'$path_documents" . $folder_path . "','" . $title . "','folder','0')" "INSERT INTO $TABLETOOLDOCUMENT (c_id, path,title,filetype,size)
VALUES ($course_id,'$path_documents" . $folder_path . "','" . $title . "','folder','0')"
); );
$image_id = Database:: insert_id(); $image_id = Database:: insert_id();
Database::query( Database::query(
"INSERT INTO $TABLEITEMPROPERTY (c_id, tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,lastedit_user_id,to_group_id,to_user_id,visibility) VALUES ($course_id,'document',1,'$now','$now',$image_id,'DocumentAdded',1,0,NULL,0)" "INSERT INTO $TABLEITEMPROPERTY (c_id, tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,lastedit_user_id,to_group_id,to_user_id,visibility)
VALUES ($course_id,'document',1,'$now','$now',$image_id,'DocumentAdded',1,0,NULL,0)"
); );
} }
} }
@ -905,17 +903,25 @@ class AddCourse
//Inserting file in the DB //Inserting file in the DB
Database::query( Database::query(
"INSERT INTO $TABLETOOLDOCUMENT (c_id, path,title,filetype,size) VALUES ($course_id,'$path_documents" . $value["file"] . "','" . $temp[count( "INSERT INTO $TABLETOOLDOCUMENT (c_id, path,title,filetype,size)
$temp VALUES ($course_id,'$path_documents" . $value["file"] . "','" . $temp[count($temp) - 1] . "','file','$file_size')"
) - 1] . "','file','$file_size')"
); );
$image_id = Database:: insert_id(); $image_id = Database:: insert_id();
if ($path_documents . $value['file'] == '/certificates/default.html') { if ($image_id) {
$example_cert_id = $image_id;
$sql = "UPDATE $TABLETOOLDOCUMENT SET id = iid WHERE iid = $image_id";
Database::query($sql);
if ($path_documents . $value['file'] == '/certificates/default.html') {
$example_cert_id = $image_id;
}
Database::query(
"INSERT INTO $TABLEITEMPROPERTY (c_id, tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,lastedit_user_id,to_group_id,to_user_id,visibility) VALUES ($course_id,'document',1,'$now','$now',$image_id,'DocumentAdded',1,0,NULL,1)"
);
$docId = Database:: insert_id();
$sql = "UPDATE $TABLEITEMPROPERTY SET id = iid WHERE iid = $docId";
Database::query($sql);
} }
Database::query(
"INSERT INTO $TABLEITEMPROPERTY (c_id, tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,lastedit_user_id,to_group_id,to_user_id,visibility) VALUES ($course_id,'document',1,'$now','$now',$image_id,'DocumentAdded',1,0,NULL,1)"
);
} }
} }
} }
@ -923,60 +929,57 @@ class AddCourse
} }
} }
/* Agenda tool */ $agenda = new Agenda();
$agenda->setType('course');
Database::query( $agenda->set_course($courseInfo);
"INSERT INTO $TABLETOOLAGENDA VALUES ($course_id, NULL, '" . self::lang2db( $agenda->addEvent(
get_lang('AgendaCreationTitle') $now,
) . "', '" . self::lang2db( $now,
get_lang('AgendaCreationContenu') 0,
) . "', now(), now(), NULL, 0, 0)" get_lang('AgendaCreationTitle'),
get_lang('AgendaCreationContenu')
); );
// We need to add the item properties too!
$insert_id = Database:: insert_id();
$sql = "INSERT INTO $TABLEITEMPROPERTY (c_id, tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,lastedit_user_id,to_group_id,to_user_id,visibility) VALUES ($course_id, '" . TOOL_CALENDAR_EVENT . "',1,'$now','$now',$insert_id,'AgendaAdded',1,0,NULL,1)";
Database::query($sql);
/* Links tool */ /* Links tool */
$add_google_link_sql = "INSERT INTO $TABLETOOLLINK (c_id, url, title, description, category_id, display_order, on_homepage, target) $link = new Link();
VALUES ($course_id, 'http://www.google.com','Google','" . self::lang2db( $link->setCourse($courseInfo);
get_lang('Google') $links = [
) . "','0','0','0','_self')"; [
Database::query($add_google_link_sql); 'c_id' => $course_id,
'url' => 'http://www.google.com',
// We need to add the item properties too! 'title' => 'Google',
$insert_id = Database:: insert_id(); 'description' => get_lang('Google') ,
$sql = "INSERT INTO $TABLEITEMPROPERTY (c_id, tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,lastedit_user_id,to_group_id,to_user_id,visibility) 'category_id' => 0,
VALUES ($course_id, '" . TOOL_LINK . "',1,'$now','$now',$insert_id,'LinkAdded',1,0,NULL,1)"; 'on_homepage' => 0,
Database::query($sql); 'target' => '_self',
'session_id' => 0
$add_wikipedia_link_sql = "INSERT INTO $TABLETOOLLINK (c_id, url, title, description, category_id, display_order, on_homepage, target) ],
VALUES ($course_id, 'http://www.wikipedia.org','Wikipedia','" . self::lang2db( [
get_lang('Wikipedia') 'c_id' => $course_id,
) . "','0','1','0','_self')"; 'url' => 'http://www.wikipedia.org',
Database::query($add_wikipedia_link_sql); 'title' => 'Wikipedia',
'description' => get_lang('Wikipedia') ,
// We need to add the item properties too! 'category_id' => 0,
$insert_id = Database:: insert_id(); 'on_homepage' => 0,
$sql = "INSERT INTO $TABLEITEMPROPERTY (tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,lastedit_user_id,to_group_id,to_user_id,visibility) VALUES ('" . TOOL_LINK . "',1,'$now','$now',$insert_id,'LinkAdded',1,0,NULL,1)"; 'target' => '_self',
Database::query($sql); 'session_id' => 0
]
/* Annoucement tool */ ];
$sql = "INSERT INTO $TABLETOOLANNOUNCEMENTS (c_id, title,content,end_date,display_order,email_sent) foreach($links as $params) {
VALUES ($course_id, '" . self::lang2db( $link->save($params);
get_lang('AnnouncementExampleTitle') }
) . "', '" . self::lang2db(
get_lang('AnnouncementEx')
) . "', '$now', '1','0')";
Database::query($sql);
// We need to add the item properties too! /* Announcement tool */
$insert_id = Database:: insert_id(); AnnouncementManager::add_announcement(
$sql = "INSERT INTO $TABLEITEMPROPERTY (c_id, tool,insert_user_id,insert_date,lastedit_date,ref,lastedit_type,lastedit_user_id,to_group_id,to_user_id,visibility) get_lang('AnnouncementExampleTitle'),
VALUES ($course_id, '" . TOOL_ANNOUNCEMENT . "',1,'$now','$now',$insert_id,'AnnouncementAdded',1,0,NULL,1)"; get_lang('AnnouncementEx'),
Database::query($sql); ['everyone' => 'everyone'],
null,
null,
$now
);
/* Introduction text */ /* Introduction text */
@ -1103,8 +1106,7 @@ class AddCourse
); );
/* Gradebook tool */ /* Gradebook tool */
$course = api_get_course_info_by_id($course_id); $course_code = $courseInfo['code'];
$course_code = $course['code'];
// father gradebook // father gradebook
Database::query( Database::query(
"INSERT INTO $TABLEGRADEBOOK (name, description, user_id, course_code, parent_id, weight, visible, certif_min_score, session_id, document_id) VALUES ('$course_code','',1,'$course_code',0,100,0,75,NULL,$example_cert_id)" "INSERT INTO $TABLEGRADEBOOK (name, description, user_id, course_code, parent_id, weight, visible, certif_min_score, session_id, document_id) VALUES ('$course_code','',1,'$course_code',0,100,0,75,NULL,$example_cert_id)"

@ -34,6 +34,7 @@ class Link extends Model
'session_id' 'session_id'
); );
public $required = array('url', 'title'); public $required = array('url', 'title');
private $course;
/** /**
* *
@ -43,6 +44,22 @@ class Link extends Model
$this->table = Database::get_course_table(TABLE_LINK); $this->table = Database::get_course_table(TABLE_LINK);
} }
/**
* @param array $course
*/
public function setCourse($course)
{
$this->course = $course;
}
/**
* @return array
*/
public function getCourse()
{
return !empty($this->course) ? $this->course : api_get_course_info();
}
/** /**
* Organize the saving of a link, using the parent's save method and * Organize the saving of a link, using the parent's save method and
* updating the item_property table * updating the item_property table
@ -54,12 +71,30 @@ class Link extends Model
*/ */
public function save($params, $show_query = null) public function save($params, $show_query = null)
{ {
$course_info = api_get_course_info(); $course_info = $this->getCourse();
$courseId = $course_info['real_id'];
$params['session_id'] = api_get_session_id(); $params['session_id'] = api_get_session_id();
$params['category_id'] = isset($params['category_id']) ? $params['category_id'] : 0; $params['category_id'] = isset($params['category_id']) ? $params['category_id'] : 0;
$sql = "SELECT MAX(display_order)
FROM ".$this->table."
WHERE
c_id = $courseId AND
category_id = '" . intval($params['category_id'])."'";
$result = Database:: query($sql);
list ($orderMax) = Database:: fetch_row($result);
$order = $orderMax + 1;
$params['display_order'] = $order;
$id = parent::save($params, $show_query); $id = parent::save($params, $show_query);
if (!empty($id)) { if (!empty($id)) {
// iid
$sql = "UPDATE ".$this->table." SET id = iid WHERE iid = $id";
Database:: query($sql);
api_set_default_visibility($id, TOOL_LINK);
api_item_property_update( api_item_property_update(
$course_info, $course_info,
TOOL_LINK, TOOL_LINK,
@ -167,46 +202,21 @@ class Link extends Model
return false; return false;
} else { } else {
// Looking for the largest order number for this category. // Looking for the largest order number for this category.
$result = Database:: query(
"SELECT MAX(display_order) FROM " . $tbl_link . "
WHERE
c_id = $course_id AND
category_id = '" . intval($selectcategory)."'"
);
list ($orderMax) = Database:: fetch_row($result);
$order = $orderMax + 1;
$sql = "INSERT INTO " . $tbl_link . " $link = new Link();
(c_id, url, title, description, category_id, display_order, on_homepage, target, session_id) $params = [
VALUES 'c_id' => $course_id,
(" . $course_id . ", 'url' => $urllink,
'" . Database:: escape_string($urllink) . "', 'title' => $title,
'" . Database:: escape_string($title) . "', 'description' => $description ,
'" . Database:: escape_string($description) . "', 'category_id' => $selectcategory,
'" . Database:: escape_string($selectcategory) . "', 'on_homepage' => $onhomepage,
'" . Database:: escape_string($order) . "', 'target' => $target,
'" . Database:: escape_string($onhomepage) . "', 'session_id' => $session_id,
'" . Database:: escape_string($target) . "', ];
'" . Database:: escape_string($session_id) . "')"; $link_id = $link->save($params);
$catlinkstatus = get_lang('LinkAdded'); $catlinkstatus = get_lang('LinkAdded');
Database:: query($sql);
$link_id = Database:: insert_id();
// iid
$sql = "UPDATE $tbl_link SET id = iid WHERE iid = $link_id";
Database:: query($sql);
if ($link_id) {
api_set_default_visibility($link_id, TOOL_LINK);
api_item_property_update(
$_course,
TOOL_LINK,
$link_id,
'LinkAdded',
api_get_user_id()
);
}
if ((api_get_setting('search_enabled') == 'true') && if ((api_get_setting('search_enabled') == 'true') &&
$link_id && extension_loaded('xapian') $link_id && extension_loaded('xapian')

Loading…
Cancel
Save