我正在尝试使用脚本在Moodle课程中创建部分,但我不能。 我正在使用Moodle course_create_sections_if_missing函数但无法获取部分显示在Web上但在数据库中它们是创建的。
我正在使用的代码如下:
if($numSections >0){ foreach ($sections as $nameSection) { $idSection = checkSections($moodle_course , $nameSection); if($idSection == -1){ m("crear section: ". $nameSection); $maxSection = getMaxSections($moodle_course ); if($maxSection != -1){ m("max section: " . $maxSection); $idSec = $maxSection + 1; course_create_sections_if_missing($moodle_course , $idSec); $res = updateNameSection($moodle_course , $nameSection, $idSec); m("result update: " . $res); }else{ m("There aren't sections for the course"); } }else{ m("section already exists: ". $nameSection); } } }else{ m("No sections for the course. "); }注意:m(“”)函数负责在控制台上显示文本。
函数checkSections是这样的:
/** * Check if there is a section. * * @param $course id of the course. * @param $section name of the section * * @return id of section */ function checkSections($course , $section){ global $DB; $id = null; $sql = "SELECT id FROM mdl_course_sections where course = ? and name = ? "; $sections = $DB->get_records_sql($sql, array($course, $section)); foreach ( $sections as $r ) { $id = $r->id; } if(is_null($id)) return -1; else return $id; }函数getMaxSections是这样的:
/** * Returns the id of the highest section for a course. * * @param $course id of the course. * * @return max id of section */ function getMaxSections($course){ global $DB; $id = null; $sql = "SELECT max(section) as sec FROM mdl_course_sections where course = ? "; $sections = $DB->get_records_sql($sql, array($course)); foreach ( $sections as $r ) { $id = $r->sec; } if(is_null($id)) return -1; else return $id; }函数updateNameSection是这样的:
/** * Update the name of a section . * * @param $course id of the course. * @param $nameSection name of the section. * @param $idSection id of the section. * * @return result */ function updateNameSection($course, $nameSection , $idSection){ global $DB; $id = null; $sql = "update mdl_course_sections set name = ? where course = ? and section = ?"; $result = $DB->execute($sql, array($nameSection , $course, $idSection)); return $result; }因此,如果有人知道如何做或有任何可能有用的示例或文档,这将是有帮助的。
提前致谢。
I am trying to create sections within a Moodle course using a script but I can not. I am using Moodle course_create_sections_if_missing function but can not get sections are displayed on the web but in the database they are created.
The code I'm using is as follows:
if($numSections >0){ foreach ($sections as $nameSection) { $idSection = checkSections($moodle_course , $nameSection); if($idSection == -1){ m("crear section: ". $nameSection); $maxSection = getMaxSections($moodle_course ); if($maxSection != -1){ m("max section: " . $maxSection); $idSec = $maxSection + 1; course_create_sections_if_missing($moodle_course , $idSec); $res = updateNameSection($moodle_course , $nameSection, $idSec); m("result update: " . $res); }else{ m("There aren't sections for the course"); } }else{ m("section already exists: ". $nameSection); } } }else{ m("No sections for the course. "); }Note: the m ( "") function is responsible for displaying the text on the console.
the function checkSections is this:
/** * Check if there is a section. * * @param $course id of the course. * @param $section name of the section * * @return id of section */ function checkSections($course , $section){ global $DB; $id = null; $sql = "SELECT id FROM mdl_course_sections where course = ? and name = ? "; $sections = $DB->get_records_sql($sql, array($course, $section)); foreach ( $sections as $r ) { $id = $r->id; } if(is_null($id)) return -1; else return $id; }the function getMaxSections is this:
/** * Returns the id of the highest section for a course. * * @param $course id of the course. * * @return max id of section */ function getMaxSections($course){ global $DB; $id = null; $sql = "SELECT max(section) as sec FROM mdl_course_sections where course = ? "; $sections = $DB->get_records_sql($sql, array($course)); foreach ( $sections as $r ) { $id = $r->sec; } if(is_null($id)) return -1; else return $id; }the function updateNameSection is this:
/** * Update the name of a section . * * @param $course id of the course. * @param $nameSection name of the section. * @param $idSection id of the section. * * @return result */ function updateNameSection($course, $nameSection , $idSection){ global $DB; $id = null; $sql = "update mdl_course_sections set name = ? where course = ? and section = ?"; $result = $DB->execute($sql, array($nameSection , $course, $idSection)); return $result; }So if anyone knows how to do it or have any examples or documentation that may be useful, it would be helpful.
Thanks in advance.
最满意答案
您需要更新表mdl_course_format_options上的节号
尝试这个:
$sql = "update mdl_course_format_options set value = ? where courseid = ? and name ='numsections'"; $result = $DB->execute($sql, array($numSections , $course));You need to update the sections number on the table mdl_course_format_options
Try this:
$sql = "update mdl_course_format_options set value = ? where courseid = ? and name ='numsections'"; $result = $DB->execute($sql, array($numSections , $course));更多推荐
发布评论