original_string: public function add_category($newparent, $newcategory, $newinfo, $return = false, $newinfoformat = FORMAT_HTML,
              $idnumber = null) {
          global $DB;
          if (empty($newcategory)) {
              print_error('categorynamecantbeblank', 'question');
          }
          list($parentid, $contextid) = explode(',', $newparent);
          //moodle_form makes sure select element output is legal no need for further cleaning
          require_capability('moodle/question:managecategory', context::instance_by_id($contextid));
  
          if ($parentid) {
              if(!($DB->get_field('question_categories', 'contextid', array('id' => $parentid)) == $contextid)) {
                  print_error('cannotinsertquestioncatecontext', 'question', '', array('cat'=>$newcategory, 'ctx'=>$contextid));
              }
          }
  
          if ((string) $idnumber === '') {
              $idnumber = null;
          } else if (!empty($contextid)) {
              // While this check already exists in the form validation, this is a backstop preventing unnecessary errors.
              if ($DB->record_exists('question_categories',
                      ['idnumber' => $idnumber, 'contextid' => $contextid])) {
                  $idnumber = null;
              }
          }
  
          $cat = new stdClass();
          $cat->parent = $parentid;
          $cat->contextid = $contextid;
          $cat->name = $newcategory;
          $cat->info = $newinfo;
          $cat->infoformat = $newinfoformat;
          $cat->sortorder = 999;
          $cat->stamp = make_unique_id_code();
          if ($idnumber) {
              $cat->idnumber = $idnumber;
          }
          $categoryid = $DB->insert_record("question_categories", $cat);
  
          // Log the creation of this category.
          $category = new stdClass();
          $category->id = $categoryid;
          $category->contextid = $contextid;
          $event = \core\event\question_category_created::create_from_question_category_instance($category);
          $event->trigger();
  
          if ($return) {
              return $categoryid;
          } else {
              redirect($this->pageurl);//always redirect after successful action
          }
      }
code: public function add_category($newparent, $newcategory, $newinfo, $return = false, $newinfoformat = FORMAT_HTML,
              $idnumber = null) {
          global $DB;
          if (empty($newcategory)) {
              print_error('categorynamecantbeblank', 'question');
          }
          list($parentid, $contextid) = explode(',', $newparent);
          //moodle_form makes sure select element output is legal no need for further cleaning
          require_capability('moodle/question:managecategory', context::instance_by_id($contextid));
  
          if ($parentid) {
              if(!($DB->get_field('question_categories', 'contextid', array('id' => $parentid)) == $contextid)) {
                  print_error('cannotinsertquestioncatecontext', 'question', '', array('cat'=>$newcategory, 'ctx'=>$contextid));
              }
          }
  
          if ((string) $idnumber === '') {
              $idnumber = null;
          } else if (!empty($contextid)) {
              // While this check already exists in the form validation, this is a backstop preventing unnecessary errors.
              if ($DB->record_exists('question_categories',
                      ['idnumber' => $idnumber, 'contextid' => $contextid])) {
                  $idnumber = null;
              }
          }
  
          $cat = new stdClass();
          $cat->parent = $parentid;
          $cat->contextid = $contextid;
          $cat->name = $newcategory;
          $cat->info = $newinfo;
          $cat->infoformat = $newinfoformat;
          $cat->sortorder = 999;
          $cat->stamp = make_unique_id_code();
          if ($idnumber) {
              $cat->idnumber = $idnumber;
          }
          $categoryid = $DB->insert_record("question_categories", $cat);
  
          // Log the creation of this category.
          $category = new stdClass();
          $category->id = $categoryid;
          $category->contextid = $contextid;
          $event = \core\event\question_category_created::create_from_question_category_instance($category);
          $event->trigger();
  
          if ($return) {
              return $categoryid;
          } else {
              redirect($this->pageurl);//always redirect after successful action
          }
      }
code_tokens: public function add_category ( $ newparent , $ newcategory , $ newinfo , $ return = false , $ newinfoformat = FORMAT_HTML , $ idnumber = null ) { global $ DB ; if ( empty ( $ newcategory ) ) { print_error ( 'categorynamecantbeblank' , 'question' ) ; } list ( $ parentid , $ contextid ) = explode ( ',' , $ newparent ) ; //moodle_form makes sure select element output is legal no need for further cleaning require_capability ( 'moodle/question:managecategory' , context :: instance_by_id ( $ contextid ) ) ; if ( $ parentid ) { if ( ! ( $ DB -> get_field ( 'question_categories' , 'contextid' , array ( 'id' => $ parentid ) ) == $ contextid ) ) { print_error ( 'cannotinsertquestioncatecontext' , 'question' , '' , array ( 'cat' => $ newcategory , 'ctx' => $ contextid ) ) ; } } if ( ( string ) $ idnumber === '' ) { $ idnumber = null ; } else if ( ! empty ( $ contextid ) ) { // While this check already exists in the form validation, this is a backstop preventing unnecessary errors. if ( $ DB -> record_exists ( 'question_categories' , [ 'idnumber' => $ idnumber , 'contextid' => $ contextid ] ) ) { $ idnumber = null ; } } $ cat = new stdClass ( ) ; $ cat -> parent = $ parentid ; $ cat -> contextid = $ contextid ; $ cat -> name = $ newcategory ; $ cat -> info = $ newinfo ; $ cat -> infoformat = $ newinfoformat ; $ cat -> sortorder = 999 ; $ cat -> stamp = make_unique_id_code ( ) ; if ( $ idnumber ) { $ cat -> idnumber = $ idnumber ; } $ categoryid = $ DB -> insert_record ( "question_categories" , $ cat ) ; // Log the creation of this category. $ category = new stdClass ( ) ; $ category -> id = $ categoryid ; $ category -> contextid = $ contextid ; $ event = \ core \ event \ question_category_created :: create_from_question_category_instance ( $ category ) ; $ event -> trigger ( ) ; if ( $ return ) { return $ categoryid ; } else { redirect ( $ this -> pageurl ) ; //always redirect after successful action } }
docstring: Creates a new category with given params
docstring_tokens: Creates a new category with given params