【问题标题】:Insert sections in a Moodle course在 Moodle 课程中插入部分
【发布时间】:2016-10-07 22:22:59
【问题描述】:

我正在尝试使用 脚本Moodle 课程中创建部分,但我做不到。我正在使用 Moodle course_create_sections_if_missing 函数,但无法让部分显示在网络上但在数据库中创建。

我使用的代码如下:

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;
}

因此,如果有人知道如何做或有任何可能有用的示例或文档,那将会很有帮助。

提前致谢。

【问题讨论】:

    标签: moodle


    【解决方案1】:

    您需要更新表格中的章节编号mdl_course_format_options

    试试这个:

    $sql = "update mdl_course_format_options set value = ? where courseid = ? and name ='numsections'";
    $result = $DB->execute($sql, array($numSections , $course));
    

    【讨论】:

      【解决方案2】:

      我尝试了@aleix 建议的代码,它对我来说很好,这就是代码的样子:

      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); 
      
      
                      if($res){   
                          $idSec = $idSec + 1;
                          $res2 = updateNumSectionsCourse($comunidad_moodle, $idSec);
                      }        
                  }else{
                      m("There aren't sections for the course");
                  }
      
              }else{
                  m("section already exists: ". $nameSection);
              }   
          }
      }else{
          m("No sections for the course. ");
      } 
      

      而函数updateNumSectionsCourse是这样的:

      /**
       * Update the number of sections for a course.
       *
       * @param $course id of the course.
       * @param $numSections number of sections.
       *
       * @return result
       */
      function updateNumSectionsCourse($course, $numSections){
      
          global $DB;
      
          $sql = "update mdl_course_format_options set value = ? where courseid = ? and name ='numsections'";
          $result = $DB->execute($sql, array($numSections , $course));
      
          return $result;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-16
        • 1970-01-01
        • 1970-01-01
        • 2022-01-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多